OSDN Git Service

PR c++/49867
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215   (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217   (cp_lexer *);
218 static void cp_lexer_start_debugging
219   (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224    about passing NULL to functions that require non-NULL arguments
225    (fputs, fprintf).  It will never be used, so all we need is a value
226    of the right type that's guaranteed not to be NULL.  */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233   (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236   (cp_token *);
237
238 /* Manifest constants.  */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables.  */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written.  */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250    sizeof, typeof, or alignof.  */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE.  If NUM is 0, dump all the
255    tokens.  */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260   unsigned i;
261   cp_token *token;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (num == 0)
266     num = VEC_length (cp_token, buffer);
267
268   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269     {
270       cp_lexer_print_token (file, token);
271       switch (token->type)
272         {
273           case CPP_SEMICOLON:
274           case CPP_OPEN_BRACE:
275           case CPP_CLOSE_BRACE:
276           case CPP_EOF:
277             fputc ('\n', file);
278             break;
279
280           default:
281             fputc (' ', file);
282         }
283     }
284
285   if (i == num && i < VEC_length (cp_token, buffer))
286     {
287       fprintf (file, " ... ");
288       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289                             VEC_length (cp_token, buffer) - 1));
290     }
291
292   fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr.  */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301   cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it.  */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311   cp_lexer *lexer;
312
313   c_common_no_more_pch ();
314
315   /* Allocate the memory.  */
316   lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319   /* Initially we are not debugging.  */
320   lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323                                    CP_SAVED_TOKEN_STACK);
324
325   /* Create the buffer.  */
326   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328   return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333    preprocessor.  */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338   cp_lexer *lexer;
339   cp_token token;
340
341   /* It's possible that parsing the first pragma will load a PCH file,
342      which is a GC collection point.  So we have to do that before
343      allocating any memory.  */
344   cp_parser_initial_pragma (&token);
345
346   lexer = cp_lexer_alloc ();
347
348   /* Put the first token in the buffer.  */
349   VEC_quick_push (cp_token, lexer->buffer, &token);
350
351   /* Get the remaining tokens from the preprocessor.  */
352   while (token.type != CPP_EOF)
353     {
354       cp_lexer_get_preprocessor_token (lexer, &token);
355       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356     }
357
358   lexer->last_token = VEC_address (cp_token, lexer->buffer)
359                       + VEC_length (cp_token, lexer->buffer)
360                       - 1;
361   lexer->next_token = VEC_length (cp_token, lexer->buffer)
362                       ? VEC_address (cp_token, lexer->buffer)
363                       : &eof_token;
364
365   /* Subsequent preprocessor diagnostics should use compiler
366      diagnostic functions to get the compiler source location.  */
367   done_lexing = true;
368
369   gcc_assert (!lexer->next_token->purged_p);
370   return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379   cp_token *first = cache->first;
380   cp_token *last = cache->last;
381   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383   /* We do not own the buffer.  */
384   lexer->buffer = NULL;
385   lexer->next_token = first == last ? &eof_token : first;
386   lexer->last_token = last;
387
388   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389                                    CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392   /* Initially we are not debugging.  */
393   lexer->debugging_p = false;
394 #endif
395
396   gcc_assert (!lexer->next_token->purged_p);
397   return lexer;
398 }
399
400 /* Frees all resources associated with LEXER.  */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405   VEC_free (cp_token, gc, lexer->buffer);
406   VEC_free (cp_token_position, heap, lexer->saved_tokens);
407   ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output.  */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417   return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425   gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427   return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433   return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439   lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445   if (lexer->next_token == &eof_token)
446     return lexer->last_token - 1;
447   else
448     return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454   cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456   return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens.  */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN.  Return true
468    if we reach EOF.  If LEXER is NULL, assume we are handling an
469    initial #pragma pch_preprocess, and thus want the lexer to return
470    processed strings.  */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475   static int is_extern_c = 0;
476
477    /* Get a new token from the preprocessor.  */
478   token->type
479     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481   token->keyword = RID_MAX;
482   token->pragma_kind = PRAGMA_NONE;
483   token->purged_p = false;
484
485   /* On some systems, some header files are surrounded by an
486      implicit extern "C" block.  Set a flag in the token if it
487      comes from such a header.  */
488   is_extern_c += pending_lang_change;
489   pending_lang_change = 0;
490   token->implicit_extern_c = is_extern_c > 0;
491
492   /* Check to see if this token is a keyword.  */
493   if (token->type == CPP_NAME)
494     {
495       if (C_IS_RESERVED_WORD (token->u.value))
496         {
497           /* Mark this token as a keyword.  */
498           token->type = CPP_KEYWORD;
499           /* Record which keyword.  */
500           token->keyword = C_RID_CODE (token->u.value);
501         }
502       else
503         {
504           if (warn_cxx0x_compat
505               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507             {
508               /* Warn about the C++0x keyword (but still treat it as
509                  an identifier).  */
510               warning (OPT_Wc__0x_compat, 
511                        "identifier %qE will become a keyword in C++0x",
512                        token->u.value);
513
514               /* Clear out the C_RID_CODE so we don't warn about this
515                  particular identifier-turned-keyword again.  */
516               C_SET_RID_CODE (token->u.value, RID_MAX);
517             }
518
519           token->ambiguous_p = false;
520           token->keyword = RID_MAX;
521         }
522     }
523   else if (token->type == CPP_AT_NAME)
524     {
525       /* This only happens in Objective-C++; it must be a keyword.  */
526       token->type = CPP_KEYWORD;
527       switch (C_RID_CODE (token->u.value))
528         {
529           /* Replace 'class' with '@class', 'private' with '@private',
530              etc.  This prevents confusion with the C++ keyword
531              'class', and makes the tokens consistent with other
532              Objective-C 'AT' keywords.  For example '@class' is
533              reported as RID_AT_CLASS which is consistent with
534              '@synchronized', which is reported as
535              RID_AT_SYNCHRONIZED.
536           */
537         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
538         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
539         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
541         case RID_THROW:     token->keyword = RID_AT_THROW; break;
542         case RID_TRY:       token->keyword = RID_AT_TRY; break;
543         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
544         default:            token->keyword = C_RID_CODE (token->u.value);
545         }
546     }
547   else if (token->type == CPP_PRAGMA)
548     {
549       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
550       token->pragma_kind = ((enum pragma_kind)
551                             TREE_INT_CST_LOW (token->u.value));
552       token->u.value = NULL_TREE;
553     }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN.  */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560   if (token->type != CPP_EOF)
561     {
562       input_location = token->location;
563     }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567    consume it.  */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578   return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE.  */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586   return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE.  */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594   return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD.  */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602   return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD.  */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610   return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier.  */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618   cp_token *token;
619
620   token = cp_lexer_peek_token (lexer);
621   switch (token->keyword) 
622     {
623       /* auto specifier: storage-class-specifier in C++,
624          simple-type-specifier in C++0x.  */
625     case RID_AUTO:
626       /* Storage classes.  */
627     case RID_REGISTER:
628     case RID_STATIC:
629     case RID_EXTERN:
630     case RID_MUTABLE:
631     case RID_THREAD:
632       /* Elaborated type specifiers.  */
633     case RID_ENUM:
634     case RID_CLASS:
635     case RID_STRUCT:
636     case RID_UNION:
637     case RID_TYPENAME:
638       /* Simple type specifiers.  */
639     case RID_CHAR:
640     case RID_CHAR16:
641     case RID_CHAR32:
642     case RID_WCHAR:
643     case RID_BOOL:
644     case RID_SHORT:
645     case RID_INT:
646     case RID_LONG:
647     case RID_INT128:
648     case RID_SIGNED:
649     case RID_UNSIGNED:
650     case RID_FLOAT:
651     case RID_DOUBLE:
652     case RID_VOID:
653       /* GNU extensions.  */ 
654     case RID_ATTRIBUTE:
655     case RID_TYPEOF:
656       /* C++0x extensions.  */
657     case RID_DECLTYPE:
658     case RID_UNDERLYING_TYPE:
659       return true;
660
661     default:
662       return false;
663     }
664 }
665
666 /* Returns TRUE iff the token T begins a decltype type.  */
667
668 static bool
669 token_is_decltype (cp_token *t)
670 {
671   return (t->keyword == RID_DECLTYPE
672           || t->type == CPP_DECLTYPE);
673 }
674
675 /* Returns TRUE iff the next token begins a decltype type.  */
676
677 static bool
678 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
679 {
680   cp_token *t = cp_lexer_peek_token (lexer);
681   return token_is_decltype (t);
682 }
683
684 /* Return a pointer to the Nth token in the token stream.  If N is 1,
685    then this is precisely equivalent to cp_lexer_peek_token (except
686    that it is not inline).  One would like to disallow that case, but
687    there is one case (cp_parser_nth_token_starts_template_id) where
688    the caller passes a variable for N and it might be 1.  */
689
690 static cp_token *
691 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
692 {
693   cp_token *token;
694
695   /* N is 1-based, not zero-based.  */
696   gcc_assert (n > 0);
697
698   if (cp_lexer_debugging_p (lexer))
699     fprintf (cp_lexer_debug_stream,
700              "cp_lexer: peeking ahead %ld at token: ", (long)n);
701
702   --n;
703   token = lexer->next_token;
704   gcc_assert (!n || token != &eof_token);
705   while (n != 0)
706     {
707       ++token;
708       if (token == lexer->last_token)
709         {
710           token = &eof_token;
711           break;
712         }
713
714       if (!token->purged_p)
715         --n;
716     }
717
718   if (cp_lexer_debugging_p (lexer))
719     {
720       cp_lexer_print_token (cp_lexer_debug_stream, token);
721       putc ('\n', cp_lexer_debug_stream);
722     }
723
724   return token;
725 }
726
727 /* Return the next token, and advance the lexer's next_token pointer
728    to point to the next non-purged token.  */
729
730 static cp_token *
731 cp_lexer_consume_token (cp_lexer* lexer)
732 {
733   cp_token *token = lexer->next_token;
734
735   gcc_assert (token != &eof_token);
736   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
737
738   do
739     {
740       lexer->next_token++;
741       if (lexer->next_token == lexer->last_token)
742         {
743           lexer->next_token = &eof_token;
744           break;
745         }
746
747     }
748   while (lexer->next_token->purged_p);
749
750   cp_lexer_set_source_position_from_token (token);
751
752   /* Provide debugging output.  */
753   if (cp_lexer_debugging_p (lexer))
754     {
755       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
756       cp_lexer_print_token (cp_lexer_debug_stream, token);
757       putc ('\n', cp_lexer_debug_stream);
758     }
759
760   return token;
761 }
762
763 /* Permanently remove the next token from the token stream, and
764    advance the next_token pointer to refer to the next non-purged
765    token.  */
766
767 static void
768 cp_lexer_purge_token (cp_lexer *lexer)
769 {
770   cp_token *tok = lexer->next_token;
771
772   gcc_assert (tok != &eof_token);
773   tok->purged_p = true;
774   tok->location = UNKNOWN_LOCATION;
775   tok->u.value = NULL_TREE;
776   tok->keyword = RID_MAX;
777
778   do
779     {
780       tok++;
781       if (tok == lexer->last_token)
782         {
783           tok = &eof_token;
784           break;
785         }
786     }
787   while (tok->purged_p);
788   lexer->next_token = tok;
789 }
790
791 /* Permanently remove all tokens after TOK, up to, but not
792    including, the token that will be returned next by
793    cp_lexer_peek_token.  */
794
795 static void
796 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
797 {
798   cp_token *peek = lexer->next_token;
799
800   if (peek == &eof_token)
801     peek = lexer->last_token;
802
803   gcc_assert (tok < peek);
804
805   for ( tok += 1; tok != peek; tok += 1)
806     {
807       tok->purged_p = true;
808       tok->location = UNKNOWN_LOCATION;
809       tok->u.value = NULL_TREE;
810       tok->keyword = RID_MAX;
811     }
812 }
813
814 /* Begin saving tokens.  All tokens consumed after this point will be
815    preserved.  */
816
817 static void
818 cp_lexer_save_tokens (cp_lexer* lexer)
819 {
820   /* Provide debugging output.  */
821   if (cp_lexer_debugging_p (lexer))
822     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
823
824   VEC_safe_push (cp_token_position, heap,
825                  lexer->saved_tokens, lexer->next_token);
826 }
827
828 /* Commit to the portion of the token stream most recently saved.  */
829
830 static void
831 cp_lexer_commit_tokens (cp_lexer* lexer)
832 {
833   /* Provide debugging output.  */
834   if (cp_lexer_debugging_p (lexer))
835     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
836
837   VEC_pop (cp_token_position, lexer->saved_tokens);
838 }
839
840 /* Return all tokens saved since the last call to cp_lexer_save_tokens
841    to the token stream.  Stop saving tokens.  */
842
843 static void
844 cp_lexer_rollback_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
849
850   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
851 }
852
853 /* Print a representation of the TOKEN on the STREAM.  */
854
855 #ifdef ENABLE_CHECKING
856
857 static void
858 cp_lexer_print_token (FILE * stream, cp_token *token)
859 {
860   /* We don't use cpp_type2name here because the parser defines
861      a few tokens of its own.  */
862   static const char *const token_names[] = {
863     /* cpplib-defined token types */
864 #define OP(e, s) #e,
865 #define TK(e, s) #e,
866     TTYPE_TABLE
867 #undef OP
868 #undef TK
869     /* C++ parser token types - see "Manifest constants", above.  */
870     "KEYWORD",
871     "TEMPLATE_ID",
872     "NESTED_NAME_SPECIFIER",
873   };
874
875   /* For some tokens, print the associated data.  */
876   switch (token->type)
877     {
878     case CPP_KEYWORD:
879       /* Some keywords have a value that is not an IDENTIFIER_NODE.
880          For example, `struct' is mapped to an INTEGER_CST.  */
881       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
882         break;
883       /* else fall through */
884     case CPP_NAME:
885       fputs (IDENTIFIER_POINTER (token->u.value), stream);
886       break;
887
888     case CPP_STRING:
889     case CPP_STRING16:
890     case CPP_STRING32:
891     case CPP_WSTRING:
892     case CPP_UTF8STRING:
893       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
894       break;
895
896     case CPP_NUMBER:
897       print_generic_expr (stream, token->u.value, 0);
898       break;
899
900     default:
901       /* If we have a name for the token, print it out.  Otherwise, we
902          simply give the numeric code.  */
903       if (token->type < ARRAY_SIZE(token_names))
904         fputs (token_names[token->type], stream);
905       else
906         fprintf (stream, "[%d]", token->type);
907       break;
908     }
909 }
910
911 /* Start emitting debugging information.  */
912
913 static void
914 cp_lexer_start_debugging (cp_lexer* lexer)
915 {
916   lexer->debugging_p = true;
917 }
918
919 /* Stop emitting debugging information.  */
920
921 static void
922 cp_lexer_stop_debugging (cp_lexer* lexer)
923 {
924   lexer->debugging_p = false;
925 }
926
927 #endif /* ENABLE_CHECKING */
928
929 /* Create a new cp_token_cache, representing a range of tokens.  */
930
931 static cp_token_cache *
932 cp_token_cache_new (cp_token *first, cp_token *last)
933 {
934   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
935   cache->first = first;
936   cache->last = last;
937   return cache;
938 }
939
940 \f
941 /* Decl-specifiers.  */
942
943 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
944
945 static void
946 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
947 {
948   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
949 }
950
951 /* Declarators.  */
952
953 /* Nothing other than the parser should be creating declarators;
954    declarators are a semi-syntactic representation of C++ entities.
955    Other parts of the front end that need to create entities (like
956    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
957
958 static cp_declarator *make_call_declarator
959   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
960 static cp_declarator *make_array_declarator
961   (cp_declarator *, tree);
962 static cp_declarator *make_pointer_declarator
963   (cp_cv_quals, cp_declarator *);
964 static cp_declarator *make_reference_declarator
965   (cp_cv_quals, cp_declarator *, bool);
966 static cp_parameter_declarator *make_parameter_declarator
967   (cp_decl_specifier_seq *, cp_declarator *, tree);
968 static cp_declarator *make_ptrmem_declarator
969   (cp_cv_quals, tree, cp_declarator *);
970
971 /* An erroneous declarator.  */
972 static cp_declarator *cp_error_declarator;
973
974 /* The obstack on which declarators and related data structures are
975    allocated.  */
976 static struct obstack declarator_obstack;
977
978 /* Alloc BYTES from the declarator memory pool.  */
979
980 static inline void *
981 alloc_declarator (size_t bytes)
982 {
983   return obstack_alloc (&declarator_obstack, bytes);
984 }
985
986 /* Allocate a declarator of the indicated KIND.  Clear fields that are
987    common to all declarators.  */
988
989 static cp_declarator *
990 make_declarator (cp_declarator_kind kind)
991 {
992   cp_declarator *declarator;
993
994   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
995   declarator->kind = kind;
996   declarator->attributes = NULL_TREE;
997   declarator->declarator = NULL;
998   declarator->parameter_pack_p = false;
999   declarator->id_loc = UNKNOWN_LOCATION;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for a generalized identifier.  If
1005    QUALIFYING_SCOPE is non-NULL, the identifier is
1006    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1007    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1008    is, if any.   */
1009
1010 static cp_declarator *
1011 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1012                     special_function_kind sfk)
1013 {
1014   cp_declarator *declarator;
1015
1016   /* It is valid to write:
1017
1018        class C { void f(); };
1019        typedef C D;
1020        void D::f();
1021
1022      The standard is not clear about whether `typedef const C D' is
1023      legal; as of 2002-09-15 the committee is considering that
1024      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1025      well.  */
1026   if (qualifying_scope && TYPE_P (qualifying_scope))
1027     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1028
1029   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1030               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1031               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1032
1033   declarator = make_declarator (cdk_id);
1034   declarator->u.id.qualifying_scope = qualifying_scope;
1035   declarator->u.id.unqualified_name = unqualified_name;
1036   declarator->u.id.sfk = sfk;
1037   
1038   return declarator;
1039 }
1040
1041 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1042    of modifiers such as const or volatile to apply to the pointer
1043    type, represented as identifiers.  */
1044
1045 cp_declarator *
1046 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1047 {
1048   cp_declarator *declarator;
1049
1050   declarator = make_declarator (cdk_pointer);
1051   declarator->declarator = target;
1052   declarator->u.pointer.qualifiers = cv_qualifiers;
1053   declarator->u.pointer.class_type = NULL_TREE;
1054   if (target)
1055     {
1056       declarator->id_loc = target->id_loc;
1057       declarator->parameter_pack_p = target->parameter_pack_p;
1058       target->parameter_pack_p = false;
1059     }
1060   else
1061     declarator->parameter_pack_p = false;
1062
1063   return declarator;
1064 }
1065
1066 /* Like make_pointer_declarator -- but for references.  */
1067
1068 cp_declarator *
1069 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1070                            bool rvalue_ref)
1071 {
1072   cp_declarator *declarator;
1073
1074   declarator = make_declarator (cdk_reference);
1075   declarator->declarator = target;
1076   declarator->u.reference.qualifiers = cv_qualifiers;
1077   declarator->u.reference.rvalue_ref = rvalue_ref;
1078   if (target)
1079     {
1080       declarator->id_loc = target->id_loc;
1081       declarator->parameter_pack_p = target->parameter_pack_p;
1082       target->parameter_pack_p = false;
1083     }
1084   else
1085     declarator->parameter_pack_p = false;
1086
1087   return declarator;
1088 }
1089
1090 /* Like make_pointer_declarator -- but for a pointer to a non-static
1091    member of CLASS_TYPE.  */
1092
1093 cp_declarator *
1094 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1095                         cp_declarator *pointee)
1096 {
1097   cp_declarator *declarator;
1098
1099   declarator = make_declarator (cdk_ptrmem);
1100   declarator->declarator = pointee;
1101   declarator->u.pointer.qualifiers = cv_qualifiers;
1102   declarator->u.pointer.class_type = class_type;
1103
1104   if (pointee)
1105     {
1106       declarator->parameter_pack_p = pointee->parameter_pack_p;
1107       pointee->parameter_pack_p = false;
1108     }
1109   else
1110     declarator->parameter_pack_p = false;
1111
1112   return declarator;
1113 }
1114
1115 /* Make a declarator for the function given by TARGET, with the
1116    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1117    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1118    indicates what exceptions can be thrown.  */
1119
1120 cp_declarator *
1121 make_call_declarator (cp_declarator *target,
1122                       tree parms,
1123                       cp_cv_quals cv_qualifiers,
1124                       cp_virt_specifiers virt_specifiers,
1125                       tree exception_specification,
1126                       tree late_return_type)
1127 {
1128   cp_declarator *declarator;
1129
1130   declarator = make_declarator (cdk_function);
1131   declarator->declarator = target;
1132   declarator->u.function.parameters = parms;
1133   declarator->u.function.qualifiers = cv_qualifiers;
1134   declarator->u.function.virt_specifiers = virt_specifiers;
1135   declarator->u.function.exception_specification = exception_specification;
1136   declarator->u.function.late_return_type = late_return_type;
1137   if (target)
1138     {
1139       declarator->id_loc = target->id_loc;
1140       declarator->parameter_pack_p = target->parameter_pack_p;
1141       target->parameter_pack_p = false;
1142     }
1143   else
1144     declarator->parameter_pack_p = false;
1145
1146   return declarator;
1147 }
1148
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150    defined by ELEMENT.  */
1151
1152 cp_declarator *
1153 make_array_declarator (cp_declarator *element, tree bounds)
1154 {
1155   cp_declarator *declarator;
1156
1157   declarator = make_declarator (cdk_array);
1158   declarator->declarator = element;
1159   declarator->u.array.bounds = bounds;
1160   if (element)
1161     {
1162       declarator->id_loc = element->id_loc;
1163       declarator->parameter_pack_p = element->parameter_pack_p;
1164       element->parameter_pack_p = false;
1165     }
1166   else
1167     declarator->parameter_pack_p = false;
1168
1169   return declarator;
1170 }
1171
1172 /* Determine whether the declarator we've seen so far can be a
1173    parameter pack, when followed by an ellipsis.  */
1174 static bool 
1175 declarator_can_be_parameter_pack (cp_declarator *declarator)
1176 {
1177   /* Search for a declarator name, or any other declarator that goes
1178      after the point where the ellipsis could appear in a parameter
1179      pack. If we find any of these, then this declarator can not be
1180      made into a parameter pack.  */
1181   bool found = false;
1182   while (declarator && !found)
1183     {
1184       switch ((int)declarator->kind)
1185         {
1186         case cdk_id:
1187         case cdk_array:
1188           found = true;
1189           break;
1190
1191         case cdk_error:
1192           return true;
1193
1194         default:
1195           declarator = declarator->declarator;
1196           break;
1197         }
1198     }
1199
1200   return !found;
1201 }
1202
1203 cp_parameter_declarator *no_parameters;
1204
1205 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1206    DECLARATOR and DEFAULT_ARGUMENT.  */
1207
1208 cp_parameter_declarator *
1209 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1210                            cp_declarator *declarator,
1211                            tree default_argument)
1212 {
1213   cp_parameter_declarator *parameter;
1214
1215   parameter = ((cp_parameter_declarator *)
1216                alloc_declarator (sizeof (cp_parameter_declarator)));
1217   parameter->next = NULL;
1218   if (decl_specifiers)
1219     parameter->decl_specifiers = *decl_specifiers;
1220   else
1221     clear_decl_specs (&parameter->decl_specifiers);
1222   parameter->declarator = declarator;
1223   parameter->default_argument = default_argument;
1224   parameter->ellipsis_p = false;
1225
1226   return parameter;
1227 }
1228
1229 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1230
1231 static bool
1232 function_declarator_p (const cp_declarator *declarator)
1233 {
1234   while (declarator)
1235     {
1236       if (declarator->kind == cdk_function
1237           && declarator->declarator->kind == cdk_id)
1238         return true;
1239       if (declarator->kind == cdk_id
1240           || declarator->kind == cdk_error)
1241         return false;
1242       declarator = declarator->declarator;
1243     }
1244   return false;
1245 }
1246  
1247 /* The parser.  */
1248
1249 /* Overview
1250    --------
1251
1252    A cp_parser parses the token stream as specified by the C++
1253    grammar.  Its job is purely parsing, not semantic analysis.  For
1254    example, the parser breaks the token stream into declarators,
1255    expressions, statements, and other similar syntactic constructs.
1256    It does not check that the types of the expressions on either side
1257    of an assignment-statement are compatible, or that a function is
1258    not declared with a parameter of type `void'.
1259
1260    The parser invokes routines elsewhere in the compiler to perform
1261    semantic analysis and to build up the abstract syntax tree for the
1262    code processed.
1263
1264    The parser (and the template instantiation code, which is, in a
1265    way, a close relative of parsing) are the only parts of the
1266    compiler that should be calling push_scope and pop_scope, or
1267    related functions.  The parser (and template instantiation code)
1268    keeps track of what scope is presently active; everything else
1269    should simply honor that.  (The code that generates static
1270    initializers may also need to set the scope, in order to check
1271    access control correctly when emitting the initializers.)
1272
1273    Methodology
1274    -----------
1275
1276    The parser is of the standard recursive-descent variety.  Upcoming
1277    tokens in the token stream are examined in order to determine which
1278    production to use when parsing a non-terminal.  Some C++ constructs
1279    require arbitrary look ahead to disambiguate.  For example, it is
1280    impossible, in the general case, to tell whether a statement is an
1281    expression or declaration without scanning the entire statement.
1282    Therefore, the parser is capable of "parsing tentatively."  When the
1283    parser is not sure what construct comes next, it enters this mode.
1284    Then, while we attempt to parse the construct, the parser queues up
1285    error messages, rather than issuing them immediately, and saves the
1286    tokens it consumes.  If the construct is parsed successfully, the
1287    parser "commits", i.e., it issues any queued error messages and
1288    the tokens that were being preserved are permanently discarded.
1289    If, however, the construct is not parsed successfully, the parser
1290    rolls back its state completely so that it can resume parsing using
1291    a different alternative.
1292
1293    Future Improvements
1294    -------------------
1295
1296    The performance of the parser could probably be improved substantially.
1297    We could often eliminate the need to parse tentatively by looking ahead
1298    a little bit.  In some places, this approach might not entirely eliminate
1299    the need to parse tentatively, but it might still speed up the average
1300    case.  */
1301
1302 /* Flags that are passed to some parsing functions.  These values can
1303    be bitwise-ored together.  */
1304
1305 enum
1306 {
1307   /* No flags.  */
1308   CP_PARSER_FLAGS_NONE = 0x0,
1309   /* The construct is optional.  If it is not present, then no error
1310      should be issued.  */
1311   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1312   /* When parsing a type-specifier, treat user-defined type-names
1313      as non-type identifiers.  */
1314   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1315   /* When parsing a type-specifier, do not try to parse a class-specifier
1316      or enum-specifier.  */
1317   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1318   /* When parsing a decl-specifier-seq, only allow type-specifier or
1319      constexpr.  */
1320   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1321 };
1322
1323 /* This type is used for parameters and variables which hold
1324    combinations of the above flags.  */
1325 typedef int cp_parser_flags;
1326
1327 /* The different kinds of declarators we want to parse.  */
1328
1329 typedef enum cp_parser_declarator_kind
1330 {
1331   /* We want an abstract declarator.  */
1332   CP_PARSER_DECLARATOR_ABSTRACT,
1333   /* We want a named declarator.  */
1334   CP_PARSER_DECLARATOR_NAMED,
1335   /* We don't mind, but the name must be an unqualified-id.  */
1336   CP_PARSER_DECLARATOR_EITHER
1337 } cp_parser_declarator_kind;
1338
1339 /* The precedence values used to parse binary expressions.  The minimum value
1340    of PREC must be 1, because zero is reserved to quickly discriminate
1341    binary operators from other tokens.  */
1342
1343 enum cp_parser_prec
1344 {
1345   PREC_NOT_OPERATOR,
1346   PREC_LOGICAL_OR_EXPRESSION,
1347   PREC_LOGICAL_AND_EXPRESSION,
1348   PREC_INCLUSIVE_OR_EXPRESSION,
1349   PREC_EXCLUSIVE_OR_EXPRESSION,
1350   PREC_AND_EXPRESSION,
1351   PREC_EQUALITY_EXPRESSION,
1352   PREC_RELATIONAL_EXPRESSION,
1353   PREC_SHIFT_EXPRESSION,
1354   PREC_ADDITIVE_EXPRESSION,
1355   PREC_MULTIPLICATIVE_EXPRESSION,
1356   PREC_PM_EXPRESSION,
1357   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1358 };
1359
1360 /* A mapping from a token type to a corresponding tree node type, with a
1361    precedence value.  */
1362
1363 typedef struct cp_parser_binary_operations_map_node
1364 {
1365   /* The token type.  */
1366   enum cpp_ttype token_type;
1367   /* The corresponding tree code.  */
1368   enum tree_code tree_type;
1369   /* The precedence of this operator.  */
1370   enum cp_parser_prec prec;
1371 } cp_parser_binary_operations_map_node;
1372
1373 typedef struct cp_parser_expression_stack_entry
1374 {
1375   /* Left hand side of the binary operation we are currently
1376      parsing.  */
1377   tree lhs;
1378   /* Original tree code for left hand side, if it was a binary
1379      expression itself (used for -Wparentheses).  */
1380   enum tree_code lhs_type;
1381   /* Tree code for the binary operation we are parsing.  */
1382   enum tree_code tree_type;
1383   /* Precedence of the binary operation we are parsing.  */
1384   enum cp_parser_prec prec;
1385 } cp_parser_expression_stack_entry;
1386
1387 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1388    entries because precedence levels on the stack are monotonically
1389    increasing.  */
1390 typedef struct cp_parser_expression_stack_entry
1391   cp_parser_expression_stack[NUM_PREC_VALUES];
1392
1393 /* Prototypes.  */
1394
1395 /* Constructors and destructors.  */
1396
1397 static cp_parser_context *cp_parser_context_new
1398   (cp_parser_context *);
1399
1400 /* Class variables.  */
1401
1402 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1403
1404 /* The operator-precedence table used by cp_parser_binary_expression.
1405    Transformed into an associative array (binops_by_token) by
1406    cp_parser_new.  */
1407
1408 static const cp_parser_binary_operations_map_node binops[] = {
1409   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1410   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1411
1412   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1413   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1414   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1415
1416   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1417   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1418
1419   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1420   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1421
1422   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1423   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1424   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1425   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1426
1427   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1428   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1429
1430   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1431
1432   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1433
1434   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1435
1436   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1437
1438   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1439 };
1440
1441 /* The same as binops, but initialized by cp_parser_new so that
1442    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1443    for speed.  */
1444 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1445
1446 /* Constructors and destructors.  */
1447
1448 /* Construct a new context.  The context below this one on the stack
1449    is given by NEXT.  */
1450
1451 static cp_parser_context *
1452 cp_parser_context_new (cp_parser_context* next)
1453 {
1454   cp_parser_context *context;
1455
1456   /* Allocate the storage.  */
1457   if (cp_parser_context_free_list != NULL)
1458     {
1459       /* Pull the first entry from the free list.  */
1460       context = cp_parser_context_free_list;
1461       cp_parser_context_free_list = context->next;
1462       memset (context, 0, sizeof (*context));
1463     }
1464   else
1465     context = ggc_alloc_cleared_cp_parser_context ();
1466
1467   /* No errors have occurred yet in this context.  */
1468   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1469   /* If this is not the bottommost context, copy information that we
1470      need from the previous context.  */
1471   if (next)
1472     {
1473       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1474          expression, then we are parsing one in this context, too.  */
1475       context->object_type = next->object_type;
1476       /* Thread the stack.  */
1477       context->next = next;
1478     }
1479
1480   return context;
1481 }
1482
1483 /* Managing the unparsed function queues.  */
1484
1485 #define unparsed_funs_with_default_args \
1486   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1487 #define unparsed_funs_with_definitions \
1488   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1489
1490 static void
1491 push_unparsed_function_queues (cp_parser *parser)
1492 {
1493   VEC_safe_push (cp_unparsed_functions_entry, gc,
1494                  parser->unparsed_queues, NULL);
1495   unparsed_funs_with_default_args = NULL;
1496   unparsed_funs_with_definitions = make_tree_vector ();
1497 }
1498
1499 static void
1500 pop_unparsed_function_queues (cp_parser *parser)
1501 {
1502   release_tree_vector (unparsed_funs_with_definitions);
1503   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1504 }
1505
1506 /* Prototypes.  */
1507
1508 /* Constructors and destructors.  */
1509
1510 static cp_parser *cp_parser_new
1511   (void);
1512
1513 /* Routines to parse various constructs.
1514
1515    Those that return `tree' will return the error_mark_node (rather
1516    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517    Sometimes, they will return an ordinary node if error-recovery was
1518    attempted, even though a parse error occurred.  So, to check
1519    whether or not a parse error occurred, you should always use
1520    cp_parser_error_occurred.  If the construct is optional (indicated
1521    either by an `_opt' in the name of the function that does the
1522    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523    the construct is not present.  */
1524
1525 /* Lexical conventions [gram.lex]  */
1526
1527 static tree cp_parser_identifier
1528   (cp_parser *);
1529 static tree cp_parser_string_literal
1530   (cp_parser *, bool, bool);
1531
1532 /* Basic concepts [gram.basic]  */
1533
1534 static bool cp_parser_translation_unit
1535   (cp_parser *);
1536
1537 /* Expressions [gram.expr]  */
1538
1539 static tree cp_parser_primary_expression
1540   (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542   (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544   (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546   (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548   (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_qualifying_entity
1550   (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552   (cp_parser *, bool, bool, bool, cp_id_kind *);
1553 static tree cp_parser_postfix_open_square_expression
1554   (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1557 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1558   (cp_parser *, int, bool, bool, bool *);
1559 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1560 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1561 static void cp_parser_pseudo_destructor_name
1562   (cp_parser *, tree *, tree *);
1563 static tree cp_parser_unary_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static enum tree_code cp_parser_unary_operator
1566   (cp_token *);
1567 static tree cp_parser_new_expression
1568   (cp_parser *);
1569 static VEC(tree,gc) *cp_parser_new_placement
1570   (cp_parser *);
1571 static tree cp_parser_new_type_id
1572   (cp_parser *, tree *);
1573 static cp_declarator *cp_parser_new_declarator_opt
1574   (cp_parser *);
1575 static cp_declarator *cp_parser_direct_new_declarator
1576   (cp_parser *);
1577 static VEC(tree,gc) *cp_parser_new_initializer
1578   (cp_parser *);
1579 static tree cp_parser_delete_expression
1580   (cp_parser *);
1581 static tree cp_parser_cast_expression
1582   (cp_parser *, bool, bool, cp_id_kind *);
1583 static tree cp_parser_binary_expression
1584   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1585 static tree cp_parser_question_colon_clause
1586   (cp_parser *, tree);
1587 static tree cp_parser_assignment_expression
1588   (cp_parser *, bool, cp_id_kind *);
1589 static enum tree_code cp_parser_assignment_operator_opt
1590   (cp_parser *);
1591 static tree cp_parser_expression
1592   (cp_parser *, bool, cp_id_kind *);
1593 static tree cp_parser_constant_expression
1594   (cp_parser *, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1596   (cp_parser *);
1597 static tree cp_parser_lambda_expression
1598   (cp_parser *);
1599 static void cp_parser_lambda_introducer
1600   (cp_parser *, tree);
1601 static bool cp_parser_lambda_declarator_opt
1602   (cp_parser *, tree);
1603 static void cp_parser_lambda_body
1604   (cp_parser *, tree);
1605
1606 /* Statements [gram.stmt.stmt]  */
1607
1608 static void cp_parser_statement
1609   (cp_parser *, tree, bool, bool *);
1610 static void cp_parser_label_for_labeled_statement
1611   (cp_parser *);
1612 static tree cp_parser_expression_statement
1613   (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615   (cp_parser *, tree, bool, bool);
1616 static void cp_parser_statement_seq_opt
1617   (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1619   (cp_parser *, bool *);
1620 static tree cp_parser_condition
1621   (cp_parser *);
1622 static tree cp_parser_iteration_statement
1623   (cp_parser *);
1624 static bool cp_parser_for_init_statement
1625   (cp_parser *, tree *decl);
1626 static tree cp_parser_for
1627   (cp_parser *);
1628 static tree cp_parser_c_for
1629   (cp_parser *, tree, tree);
1630 static tree cp_parser_range_for
1631   (cp_parser *, tree, tree, tree);
1632 static tree cp_parser_perform_range_for_lookup
1633   (tree, tree *, tree *);
1634 static tree cp_parser_range_for_member_function
1635   (tree, tree);
1636 static tree cp_parser_jump_statement
1637   (cp_parser *);
1638 static void cp_parser_declaration_statement
1639   (cp_parser *);
1640
1641 static tree cp_parser_implicitly_scoped_statement
1642   (cp_parser *, bool *);
1643 static void cp_parser_already_scoped_statement
1644   (cp_parser *);
1645
1646 /* Declarations [gram.dcl.dcl] */
1647
1648 static void cp_parser_declaration_seq_opt
1649   (cp_parser *);
1650 static void cp_parser_declaration
1651   (cp_parser *);
1652 static void cp_parser_block_declaration
1653   (cp_parser *, bool);
1654 static void cp_parser_simple_declaration
1655   (cp_parser *, bool, tree *);
1656 static void cp_parser_decl_specifier_seq
1657   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1658 static tree cp_parser_storage_class_specifier_opt
1659   (cp_parser *);
1660 static tree cp_parser_function_specifier_opt
1661   (cp_parser *, cp_decl_specifier_seq *);
1662 static tree cp_parser_type_specifier
1663   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1664    int *, bool *);
1665 static tree cp_parser_simple_type_specifier
1666   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1667 static tree cp_parser_type_name
1668   (cp_parser *);
1669 static tree cp_parser_nonclass_name 
1670   (cp_parser* parser);
1671 static tree cp_parser_elaborated_type_specifier
1672   (cp_parser *, bool, bool);
1673 static tree cp_parser_enum_specifier
1674   (cp_parser *);
1675 static void cp_parser_enumerator_list
1676   (cp_parser *, tree);
1677 static void cp_parser_enumerator_definition
1678   (cp_parser *, tree);
1679 static tree cp_parser_namespace_name
1680   (cp_parser *);
1681 static void cp_parser_namespace_definition
1682   (cp_parser *);
1683 static void cp_parser_namespace_body
1684   (cp_parser *);
1685 static tree cp_parser_qualified_namespace_specifier
1686   (cp_parser *);
1687 static void cp_parser_namespace_alias_definition
1688   (cp_parser *);
1689 static bool cp_parser_using_declaration
1690   (cp_parser *, bool);
1691 static void cp_parser_using_directive
1692   (cp_parser *);
1693 static void cp_parser_asm_definition
1694   (cp_parser *);
1695 static void cp_parser_linkage_specification
1696   (cp_parser *);
1697 static void cp_parser_static_assert
1698   (cp_parser *, bool);
1699 static tree cp_parser_decltype
1700   (cp_parser *);
1701
1702 /* Declarators [gram.dcl.decl] */
1703
1704 static tree cp_parser_init_declarator
1705   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1706 static cp_declarator *cp_parser_declarator
1707   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1708 static cp_declarator *cp_parser_direct_declarator
1709   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1710 static enum tree_code cp_parser_ptr_operator
1711   (cp_parser *, tree *, cp_cv_quals *);
1712 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1713   (cp_parser *);
1714 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1715   (cp_parser *);
1716 static tree cp_parser_late_return_type_opt
1717   (cp_parser *, cp_cv_quals);
1718 static tree cp_parser_declarator_id
1719   (cp_parser *, bool);
1720 static tree cp_parser_type_id
1721   (cp_parser *);
1722 static tree cp_parser_template_type_arg
1723   (cp_parser *);
1724 static tree cp_parser_trailing_type_id (cp_parser *);
1725 static tree cp_parser_type_id_1
1726   (cp_parser *, bool, bool);
1727 static void cp_parser_type_specifier_seq
1728   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1729 static tree cp_parser_parameter_declaration_clause
1730   (cp_parser *);
1731 static tree cp_parser_parameter_declaration_list
1732   (cp_parser *, bool *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration
1734   (cp_parser *, bool, bool *);
1735 static tree cp_parser_default_argument 
1736   (cp_parser *, bool);
1737 static void cp_parser_function_body
1738   (cp_parser *);
1739 static tree cp_parser_initializer
1740   (cp_parser *, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742   (cp_parser *, bool *);
1743 static tree cp_parser_braced_list
1744   (cp_parser*, bool*);
1745 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1746   (cp_parser *, bool *);
1747
1748 static bool cp_parser_ctor_initializer_opt_and_function_body
1749   (cp_parser *);
1750
1751 /* Classes [gram.class] */
1752
1753 static tree cp_parser_class_name
1754   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1755 static tree cp_parser_class_specifier
1756   (cp_parser *);
1757 static tree cp_parser_class_head
1758   (cp_parser *, bool *, tree *, tree *);
1759 static enum tag_types cp_parser_class_key
1760   (cp_parser *);
1761 static void cp_parser_member_specification_opt
1762   (cp_parser *);
1763 static void cp_parser_member_declaration
1764   (cp_parser *);
1765 static tree cp_parser_pure_specifier
1766   (cp_parser *);
1767 static tree cp_parser_constant_initializer
1768   (cp_parser *);
1769
1770 /* Derived classes [gram.class.derived] */
1771
1772 static tree cp_parser_base_clause
1773   (cp_parser *);
1774 static tree cp_parser_base_specifier
1775   (cp_parser *);
1776
1777 /* Special member functions [gram.special] */
1778
1779 static tree cp_parser_conversion_function_id
1780   (cp_parser *);
1781 static tree cp_parser_conversion_type_id
1782   (cp_parser *);
1783 static cp_declarator *cp_parser_conversion_declarator_opt
1784   (cp_parser *);
1785 static bool cp_parser_ctor_initializer_opt
1786   (cp_parser *);
1787 static void cp_parser_mem_initializer_list
1788   (cp_parser *);
1789 static tree cp_parser_mem_initializer
1790   (cp_parser *);
1791 static tree cp_parser_mem_initializer_id
1792   (cp_parser *);
1793
1794 /* Overloading [gram.over] */
1795
1796 static tree cp_parser_operator_function_id
1797   (cp_parser *);
1798 static tree cp_parser_operator
1799   (cp_parser *);
1800
1801 /* Templates [gram.temp] */
1802
1803 static void cp_parser_template_declaration
1804   (cp_parser *, bool);
1805 static tree cp_parser_template_parameter_list
1806   (cp_parser *);
1807 static tree cp_parser_template_parameter
1808   (cp_parser *, bool *, bool *);
1809 static tree cp_parser_type_parameter
1810   (cp_parser *, bool *);
1811 static tree cp_parser_template_id
1812   (cp_parser *, bool, bool, bool);
1813 static tree cp_parser_template_name
1814   (cp_parser *, bool, bool, bool, bool *);
1815 static tree cp_parser_template_argument_list
1816   (cp_parser *);
1817 static tree cp_parser_template_argument
1818   (cp_parser *);
1819 static void cp_parser_explicit_instantiation
1820   (cp_parser *);
1821 static void cp_parser_explicit_specialization
1822   (cp_parser *);
1823
1824 /* Exception handling [gram.exception] */
1825
1826 static tree cp_parser_try_block
1827   (cp_parser *);
1828 static bool cp_parser_function_try_block
1829   (cp_parser *);
1830 static void cp_parser_handler_seq
1831   (cp_parser *);
1832 static void cp_parser_handler
1833   (cp_parser *);
1834 static tree cp_parser_exception_declaration
1835   (cp_parser *);
1836 static tree cp_parser_throw_expression
1837   (cp_parser *);
1838 static tree cp_parser_exception_specification_opt
1839   (cp_parser *);
1840 static tree cp_parser_type_id_list
1841   (cp_parser *);
1842
1843 /* GNU Extensions */
1844
1845 static tree cp_parser_asm_specification_opt
1846   (cp_parser *);
1847 static tree cp_parser_asm_operand_list
1848   (cp_parser *);
1849 static tree cp_parser_asm_clobber_list
1850   (cp_parser *);
1851 static tree cp_parser_asm_label_list
1852   (cp_parser *);
1853 static tree cp_parser_attributes_opt
1854   (cp_parser *);
1855 static tree cp_parser_attribute_list
1856   (cp_parser *);
1857 static bool cp_parser_extension_opt
1858   (cp_parser *, int *);
1859 static void cp_parser_label_declaration
1860   (cp_parser *);
1861
1862 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1863 static bool cp_parser_pragma
1864   (cp_parser *, enum pragma_context);
1865
1866 /* Objective-C++ Productions */
1867
1868 static tree cp_parser_objc_message_receiver
1869   (cp_parser *);
1870 static tree cp_parser_objc_message_args
1871   (cp_parser *);
1872 static tree cp_parser_objc_message_expression
1873   (cp_parser *);
1874 static tree cp_parser_objc_encode_expression
1875   (cp_parser *);
1876 static tree cp_parser_objc_defs_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_protocol_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_selector_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_expression
1883   (cp_parser *);
1884 static bool cp_parser_objc_selector_p
1885   (enum cpp_ttype);
1886 static tree cp_parser_objc_selector
1887   (cp_parser *);
1888 static tree cp_parser_objc_protocol_refs_opt
1889   (cp_parser *);
1890 static void cp_parser_objc_declaration
1891   (cp_parser *, tree);
1892 static tree cp_parser_objc_statement
1893   (cp_parser *);
1894 static bool cp_parser_objc_valid_prefix_attributes
1895   (cp_parser *, tree *);
1896 static void cp_parser_objc_at_property_declaration 
1897   (cp_parser *) ;
1898 static void cp_parser_objc_at_synthesize_declaration 
1899   (cp_parser *) ;
1900 static void cp_parser_objc_at_dynamic_declaration
1901   (cp_parser *) ;
1902 static tree cp_parser_objc_struct_declaration
1903   (cp_parser *) ;
1904
1905 /* Utility Routines */
1906
1907 static tree cp_parser_lookup_name
1908   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1909 static tree cp_parser_lookup_name_simple
1910   (cp_parser *, tree, location_t);
1911 static tree cp_parser_maybe_treat_template_as_class
1912   (tree, bool);
1913 static bool cp_parser_check_declarator_template_parameters
1914   (cp_parser *, cp_declarator *, location_t);
1915 static bool cp_parser_check_template_parameters
1916   (cp_parser *, unsigned, location_t, cp_declarator *);
1917 static tree cp_parser_simple_cast_expression
1918   (cp_parser *);
1919 static tree cp_parser_global_scope_opt
1920   (cp_parser *, bool);
1921 static bool cp_parser_constructor_declarator_p
1922   (cp_parser *, bool);
1923 static tree cp_parser_function_definition_from_specifiers_and_declarator
1924   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1925 static tree cp_parser_function_definition_after_declarator
1926   (cp_parser *, bool);
1927 static void cp_parser_template_declaration_after_export
1928   (cp_parser *, bool);
1929 static void cp_parser_perform_template_parameter_access_checks
1930   (VEC (deferred_access_check,gc)*);
1931 static tree cp_parser_single_declaration
1932   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1933 static tree cp_parser_functional_cast
1934   (cp_parser *, tree);
1935 static tree cp_parser_save_member_function_body
1936   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1937 static tree cp_parser_enclosed_template_argument_list
1938   (cp_parser *);
1939 static void cp_parser_save_default_args
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_for_member
1942   (cp_parser *, tree);
1943 static void cp_parser_late_parsing_default_args
1944   (cp_parser *, tree);
1945 static tree cp_parser_sizeof_operand
1946   (cp_parser *, enum rid);
1947 static tree cp_parser_trait_expr
1948   (cp_parser *, enum rid);
1949 static bool cp_parser_declares_only_class_p
1950   (cp_parser *);
1951 static void cp_parser_set_storage_class
1952   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1953 static void cp_parser_set_decl_spec_type
1954   (cp_decl_specifier_seq *, tree, location_t, bool);
1955 static bool cp_parser_friend_p
1956   (const cp_decl_specifier_seq *);
1957 static void cp_parser_required_error
1958   (cp_parser *, required_token, bool);
1959 static cp_token *cp_parser_require
1960   (cp_parser *, enum cpp_ttype, required_token);
1961 static cp_token *cp_parser_require_keyword
1962   (cp_parser *, enum rid, required_token);
1963 static bool cp_parser_token_starts_function_definition_p
1964   (cp_token *);
1965 static bool cp_parser_next_token_starts_class_definition_p
1966   (cp_parser *);
1967 static bool cp_parser_next_token_ends_template_argument_p
1968   (cp_parser *);
1969 static bool cp_parser_nth_token_starts_template_argument_list_p
1970   (cp_parser *, size_t);
1971 static enum tag_types cp_parser_token_is_class_key
1972   (cp_token *);
1973 static void cp_parser_check_class_key
1974   (enum tag_types, tree type);
1975 static void cp_parser_check_access_in_redeclaration
1976   (tree type, location_t location);
1977 static bool cp_parser_optional_template_keyword
1978   (cp_parser *);
1979 static void cp_parser_pre_parsed_nested_name_specifier
1980   (cp_parser *);
1981 static bool cp_parser_cache_group
1982   (cp_parser *, enum cpp_ttype, unsigned);
1983 static void cp_parser_parse_tentatively
1984   (cp_parser *);
1985 static void cp_parser_commit_to_tentative_parse
1986   (cp_parser *);
1987 static void cp_parser_abort_tentative_parse
1988   (cp_parser *);
1989 static bool cp_parser_parse_definitely
1990   (cp_parser *);
1991 static inline bool cp_parser_parsing_tentatively
1992   (cp_parser *);
1993 static bool cp_parser_uncommitted_to_tentative_parse_p
1994   (cp_parser *);
1995 static void cp_parser_error
1996   (cp_parser *, const char *);
1997 static void cp_parser_name_lookup_error
1998   (cp_parser *, tree, tree, name_lookup_error, location_t);
1999 static bool cp_parser_simulate_error
2000   (cp_parser *);
2001 static bool cp_parser_check_type_definition
2002   (cp_parser *);
2003 static void cp_parser_check_for_definition_in_return_type
2004   (cp_declarator *, tree, location_t type_location);
2005 static void cp_parser_check_for_invalid_template_id
2006   (cp_parser *, tree, location_t location);
2007 static bool cp_parser_non_integral_constant_expression
2008   (cp_parser *, non_integral_constant);
2009 static void cp_parser_diagnose_invalid_type_name
2010   (cp_parser *, tree, tree, location_t);
2011 static bool cp_parser_parse_and_diagnose_invalid_type_name
2012   (cp_parser *);
2013 static int cp_parser_skip_to_closing_parenthesis
2014   (cp_parser *, bool, bool, bool);
2015 static void cp_parser_skip_to_end_of_statement
2016   (cp_parser *);
2017 static void cp_parser_consume_semicolon_at_end_of_statement
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_block_or_statement
2020   (cp_parser *);
2021 static bool cp_parser_skip_to_closing_brace
2022   (cp_parser *);
2023 static void cp_parser_skip_to_end_of_template_parameter_list
2024   (cp_parser *);
2025 static void cp_parser_skip_to_pragma_eol
2026   (cp_parser*, cp_token *);
2027 static bool cp_parser_error_occurred
2028   (cp_parser *);
2029 static bool cp_parser_allow_gnu_extensions_p
2030   (cp_parser *);
2031 static bool cp_parser_is_string_literal
2032   (cp_token *);
2033 static bool cp_parser_is_keyword
2034   (cp_token *, enum rid);
2035 static tree cp_parser_make_typename_type
2036   (cp_parser *, tree, tree, location_t location);
2037 static cp_declarator * cp_parser_make_indirect_declarator
2038   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2039
2040 /* Returns nonzero if we are parsing tentatively.  */
2041
2042 static inline bool
2043 cp_parser_parsing_tentatively (cp_parser* parser)
2044 {
2045   return parser->context->next != NULL;
2046 }
2047
2048 /* Returns nonzero if TOKEN is a string literal.  */
2049
2050 static bool
2051 cp_parser_is_string_literal (cp_token* token)
2052 {
2053   return (token->type == CPP_STRING ||
2054           token->type == CPP_STRING16 ||
2055           token->type == CPP_STRING32 ||
2056           token->type == CPP_WSTRING ||
2057           token->type == CPP_UTF8STRING);
2058 }
2059
2060 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2061
2062 static bool
2063 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2064 {
2065   return token->keyword == keyword;
2066 }
2067
2068 /* If not parsing tentatively, issue a diagnostic of the form
2069       FILE:LINE: MESSAGE before TOKEN
2070    where TOKEN is the next token in the input stream.  MESSAGE
2071    (specified by the caller) is usually of the form "expected
2072    OTHER-TOKEN".  */
2073
2074 static void
2075 cp_parser_error (cp_parser* parser, const char* gmsgid)
2076 {
2077   if (!cp_parser_simulate_error (parser))
2078     {
2079       cp_token *token = cp_lexer_peek_token (parser->lexer);
2080       /* This diagnostic makes more sense if it is tagged to the line
2081          of the token we just peeked at.  */
2082       cp_lexer_set_source_position_from_token (token);
2083
2084       if (token->type == CPP_PRAGMA)
2085         {
2086           error_at (token->location,
2087                     "%<#pragma%> is not allowed here");
2088           cp_parser_skip_to_pragma_eol (parser, token);
2089           return;
2090         }
2091
2092       c_parse_error (gmsgid,
2093                      /* Because c_parser_error does not understand
2094                         CPP_KEYWORD, keywords are treated like
2095                         identifiers.  */
2096                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2097                      token->u.value, token->flags);
2098     }
2099 }
2100
2101 /* Issue an error about name-lookup failing.  NAME is the
2102    IDENTIFIER_NODE DECL is the result of
2103    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2104    the thing that we hoped to find.  */
2105
2106 static void
2107 cp_parser_name_lookup_error (cp_parser* parser,
2108                              tree name,
2109                              tree decl,
2110                              name_lookup_error desired,
2111                              location_t location)
2112 {
2113   /* If name lookup completely failed, tell the user that NAME was not
2114      declared.  */
2115   if (decl == error_mark_node)
2116     {
2117       if (parser->scope && parser->scope != global_namespace)
2118         error_at (location, "%<%E::%E%> has not been declared",
2119                   parser->scope, name);
2120       else if (parser->scope == global_namespace)
2121         error_at (location, "%<::%E%> has not been declared", name);
2122       else if (parser->object_scope
2123                && !CLASS_TYPE_P (parser->object_scope))
2124         error_at (location, "request for member %qE in non-class type %qT",
2125                   name, parser->object_scope);
2126       else if (parser->object_scope)
2127         error_at (location, "%<%T::%E%> has not been declared",
2128                   parser->object_scope, name);
2129       else
2130         error_at (location, "%qE has not been declared", name);
2131     }
2132   else if (parser->scope && parser->scope != global_namespace)
2133     {
2134       switch (desired)
2135         {
2136           case NLE_TYPE:
2137             error_at (location, "%<%E::%E%> is not a type",
2138                                 parser->scope, name);
2139             break;
2140           case NLE_CXX98:
2141             error_at (location, "%<%E::%E%> is not a class or namespace",
2142                                 parser->scope, name);
2143             break;
2144           case NLE_NOT_CXX98:
2145             error_at (location,
2146                       "%<%E::%E%> is not a class, namespace, or enumeration",
2147                       parser->scope, name);
2148             break;
2149           default:
2150             gcc_unreachable ();
2151             
2152         }
2153     }
2154   else if (parser->scope == global_namespace)
2155     {
2156       switch (desired)
2157         {
2158           case NLE_TYPE:
2159             error_at (location, "%<::%E%> is not a type", name);
2160             break;
2161           case NLE_CXX98:
2162             error_at (location, "%<::%E%> is not a class or namespace", name);
2163             break;
2164           case NLE_NOT_CXX98:
2165             error_at (location,
2166                       "%<::%E%> is not a class, namespace, or enumeration",
2167                       name);
2168             break;
2169           default:
2170             gcc_unreachable ();
2171         }
2172     }
2173   else
2174     {
2175       switch (desired)
2176         {
2177           case NLE_TYPE:
2178             error_at (location, "%qE is not a type", name);
2179             break;
2180           case NLE_CXX98:
2181             error_at (location, "%qE is not a class or namespace", name);
2182             break;
2183           case NLE_NOT_CXX98:
2184             error_at (location,
2185                       "%qE is not a class, namespace, or enumeration", name);
2186             break;
2187           default:
2188             gcc_unreachable ();
2189         }
2190     }
2191 }
2192
2193 /* If we are parsing tentatively, remember that an error has occurred
2194    during this tentative parse.  Returns true if the error was
2195    simulated; false if a message should be issued by the caller.  */
2196
2197 static bool
2198 cp_parser_simulate_error (cp_parser* parser)
2199 {
2200   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2201     {
2202       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2203       return true;
2204     }
2205   return false;
2206 }
2207
2208 /* Check for repeated decl-specifiers.  */
2209
2210 static void
2211 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2212                            location_t location)
2213 {
2214   int ds;
2215
2216   for (ds = ds_first; ds != ds_last; ++ds)
2217     {
2218       unsigned count = decl_specs->specs[ds];
2219       if (count < 2)
2220         continue;
2221       /* The "long" specifier is a special case because of "long long".  */
2222       if (ds == ds_long)
2223         {
2224           if (count > 2)
2225             error_at (location, "%<long long long%> is too long for GCC");
2226           else 
2227             pedwarn_cxx98 (location, OPT_Wlong_long, 
2228                            "ISO C++ 1998 does not support %<long long%>");
2229         }
2230       else if (count > 1)
2231         {
2232           static const char *const decl_spec_names[] = {
2233             "signed",
2234             "unsigned",
2235             "short",
2236             "long",
2237             "const",
2238             "volatile",
2239             "restrict",
2240             "inline",
2241             "virtual",
2242             "explicit",
2243             "friend",
2244             "typedef",
2245             "constexpr",
2246             "__complex",
2247             "__thread"
2248           };
2249           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2250         }
2251     }
2252 }
2253
2254 /* This function is called when a type is defined.  If type
2255    definitions are forbidden at this point, an error message is
2256    issued.  */
2257
2258 static bool
2259 cp_parser_check_type_definition (cp_parser* parser)
2260 {
2261   /* If types are forbidden here, issue a message.  */
2262   if (parser->type_definition_forbidden_message)
2263     {
2264       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2265          in the message need to be interpreted.  */
2266       error (parser->type_definition_forbidden_message);
2267       return false;
2268     }
2269   return true;
2270 }
2271
2272 /* This function is called when the DECLARATOR is processed.  The TYPE
2273    was a type defined in the decl-specifiers.  If it is invalid to
2274    define a type in the decl-specifiers for DECLARATOR, an error is
2275    issued. TYPE_LOCATION is the location of TYPE and is used
2276    for error reporting.  */
2277
2278 static void
2279 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2280                                                tree type, location_t type_location)
2281 {
2282   /* [dcl.fct] forbids type definitions in return types.
2283      Unfortunately, it's not easy to know whether or not we are
2284      processing a return type until after the fact.  */
2285   while (declarator
2286          && (declarator->kind == cdk_pointer
2287              || declarator->kind == cdk_reference
2288              || declarator->kind == cdk_ptrmem))
2289     declarator = declarator->declarator;
2290   if (declarator
2291       && declarator->kind == cdk_function)
2292     {
2293       error_at (type_location,
2294                 "new types may not be defined in a return type");
2295       inform (type_location, 
2296               "(perhaps a semicolon is missing after the definition of %qT)",
2297               type);
2298     }
2299 }
2300
2301 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2302    "<" in any valid C++ program.  If the next token is indeed "<",
2303    issue a message warning the user about what appears to be an
2304    invalid attempt to form a template-id. LOCATION is the location
2305    of the type-specifier (TYPE) */
2306
2307 static void
2308 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2309                                          tree type, location_t location)
2310 {
2311   cp_token_position start = 0;
2312
2313   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2314     {
2315       if (TYPE_P (type))
2316         error_at (location, "%qT is not a template", type);
2317       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2318         error_at (location, "%qE is not a template", type);
2319       else
2320         error_at (location, "invalid template-id");
2321       /* Remember the location of the invalid "<".  */
2322       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2323         start = cp_lexer_token_position (parser->lexer, true);
2324       /* Consume the "<".  */
2325       cp_lexer_consume_token (parser->lexer);
2326       /* Parse the template arguments.  */
2327       cp_parser_enclosed_template_argument_list (parser);
2328       /* Permanently remove the invalid template arguments so that
2329          this error message is not issued again.  */
2330       if (start)
2331         cp_lexer_purge_tokens_after (parser->lexer, start);
2332     }
2333 }
2334
2335 /* If parsing an integral constant-expression, issue an error message
2336    about the fact that THING appeared and return true.  Otherwise,
2337    return false.  In either case, set
2338    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2339
2340 static bool
2341 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2342                                             non_integral_constant thing)
2343 {
2344   parser->non_integral_constant_expression_p = true;
2345   if (parser->integral_constant_expression_p)
2346     {
2347       if (!parser->allow_non_integral_constant_expression_p)
2348         {
2349           const char *msg = NULL;
2350           switch (thing)
2351             {
2352               case NIC_FLOAT:
2353                 error ("floating-point literal "
2354                        "cannot appear in a constant-expression");
2355                 return true;
2356               case NIC_CAST:
2357                 error ("a cast to a type other than an integral or "
2358                        "enumeration type cannot appear in a "
2359                        "constant-expression");
2360                 return true;
2361               case NIC_TYPEID:
2362                 error ("%<typeid%> operator "
2363                        "cannot appear in a constant-expression");
2364                 return true;
2365               case NIC_NCC:
2366                 error ("non-constant compound literals "
2367                        "cannot appear in a constant-expression");
2368                 return true;
2369               case NIC_FUNC_CALL:
2370                 error ("a function call "
2371                        "cannot appear in a constant-expression");
2372                 return true;
2373               case NIC_INC:
2374                 error ("an increment "
2375                        "cannot appear in a constant-expression");
2376                 return true;
2377               case NIC_DEC:
2378                 error ("an decrement "
2379                        "cannot appear in a constant-expression");
2380                 return true;
2381               case NIC_ARRAY_REF:
2382                 error ("an array reference "
2383                        "cannot appear in a constant-expression");
2384                 return true;
2385               case NIC_ADDR_LABEL:
2386                 error ("the address of a label "
2387                        "cannot appear in a constant-expression");
2388                 return true;
2389               case NIC_OVERLOADED:
2390                 error ("calls to overloaded operators "
2391                        "cannot appear in a constant-expression");
2392                 return true;
2393               case NIC_ASSIGNMENT:
2394                 error ("an assignment cannot appear in a constant-expression");
2395                 return true;
2396               case NIC_COMMA:
2397                 error ("a comma operator "
2398                        "cannot appear in a constant-expression");
2399                 return true;
2400               case NIC_CONSTRUCTOR:
2401                 error ("a call to a constructor "
2402                        "cannot appear in a constant-expression");
2403                 return true;
2404               case NIC_THIS:
2405                 msg = "this";
2406                 break;
2407               case NIC_FUNC_NAME:
2408                 msg = "__FUNCTION__";
2409                 break;
2410               case NIC_PRETTY_FUNC:
2411                 msg = "__PRETTY_FUNCTION__";
2412                 break;
2413               case NIC_C99_FUNC:
2414                 msg = "__func__";
2415                 break;
2416               case NIC_VA_ARG:
2417                 msg = "va_arg";
2418                 break;
2419               case NIC_ARROW:
2420                 msg = "->";
2421                 break;
2422               case NIC_POINT:
2423                 msg = ".";
2424                 break;
2425               case NIC_STAR:
2426                 msg = "*";
2427                 break;
2428               case NIC_ADDR:
2429                 msg = "&";
2430                 break;
2431               case NIC_PREINCREMENT:
2432                 msg = "++";
2433                 break;
2434               case NIC_PREDECREMENT:
2435                 msg = "--";
2436                 break;
2437               case NIC_NEW:
2438                 msg = "new";
2439                 break;
2440               case NIC_DEL:
2441                 msg = "delete";
2442                 break;
2443               default:
2444                 gcc_unreachable ();
2445             }
2446           if (msg)
2447             error ("%qs cannot appear in a constant-expression", msg);
2448           return true;
2449         }
2450     }
2451   return false;
2452 }
2453
2454 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2455    qualifying scope (or NULL, if none) for ID.  This function commits
2456    to the current active tentative parse, if any.  (Otherwise, the
2457    problematic construct might be encountered again later, resulting
2458    in duplicate error messages.) LOCATION is the location of ID.  */
2459
2460 static void
2461 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2462                                       tree scope, tree id,
2463                                       location_t location)
2464 {
2465   tree decl, old_scope;
2466   cp_parser_commit_to_tentative_parse (parser);
2467   /* Try to lookup the identifier.  */
2468   old_scope = parser->scope;
2469   parser->scope = scope;
2470   decl = cp_parser_lookup_name_simple (parser, id, location);
2471   parser->scope = old_scope;
2472   /* If the lookup found a template-name, it means that the user forgot
2473   to specify an argument list. Emit a useful error message.  */
2474   if (TREE_CODE (decl) == TEMPLATE_DECL)
2475     error_at (location,
2476               "invalid use of template-name %qE without an argument list",
2477               decl);
2478   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2479     error_at (location, "invalid use of destructor %qD as a type", id);
2480   else if (TREE_CODE (decl) == TYPE_DECL)
2481     /* Something like 'unsigned A a;'  */
2482     error_at (location, "invalid combination of multiple type-specifiers");
2483   else if (!parser->scope)
2484     {
2485       /* Issue an error message.  */
2486       error_at (location, "%qE does not name a type", id);
2487       /* If we're in a template class, it's possible that the user was
2488          referring to a type from a base class.  For example:
2489
2490            template <typename T> struct A { typedef T X; };
2491            template <typename T> struct B : public A<T> { X x; };
2492
2493          The user should have said "typename A<T>::X".  */
2494       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2495         inform (location, "C++0x %<constexpr%> only available with "
2496                 "-std=c++0x or -std=gnu++0x");
2497       else if (processing_template_decl && current_class_type
2498                && TYPE_BINFO (current_class_type))
2499         {
2500           tree b;
2501
2502           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2503                b;
2504                b = TREE_CHAIN (b))
2505             {
2506               tree base_type = BINFO_TYPE (b);
2507               if (CLASS_TYPE_P (base_type)
2508                   && dependent_type_p (base_type))
2509                 {
2510                   tree field;
2511                   /* Go from a particular instantiation of the
2512                      template (which will have an empty TYPE_FIELDs),
2513                      to the main version.  */
2514                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2515                   for (field = TYPE_FIELDS (base_type);
2516                        field;
2517                        field = DECL_CHAIN (field))
2518                     if (TREE_CODE (field) == TYPE_DECL
2519                         && DECL_NAME (field) == id)
2520                       {
2521                         inform (location, 
2522                                 "(perhaps %<typename %T::%E%> was intended)",
2523                                 BINFO_TYPE (b), id);
2524                         break;
2525                       }
2526                   if (field)
2527                     break;
2528                 }
2529             }
2530         }
2531     }
2532   /* Here we diagnose qualified-ids where the scope is actually correct,
2533      but the identifier does not resolve to a valid type name.  */
2534   else if (parser->scope != error_mark_node)
2535     {
2536       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2537         error_at (location, "%qE in namespace %qE does not name a type",
2538                   id, parser->scope);
2539       else if (CLASS_TYPE_P (parser->scope)
2540                && constructor_name_p (id, parser->scope))
2541         {
2542           /* A<T>::A<T>() */
2543           error_at (location, "%<%T::%E%> names the constructor, not"
2544                     " the type", parser->scope, id);
2545           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2546             error_at (location, "and %qT has no template constructors",
2547                       parser->scope);
2548         }
2549       else if (TYPE_P (parser->scope)
2550                && dependent_scope_p (parser->scope))
2551         error_at (location, "need %<typename%> before %<%T::%E%> because "
2552                   "%qT is a dependent scope",
2553                   parser->scope, id, parser->scope);
2554       else if (TYPE_P (parser->scope))
2555         error_at (location, "%qE in %q#T does not name a type",
2556                   id, parser->scope);
2557       else
2558         gcc_unreachable ();
2559     }
2560 }
2561
2562 /* Check for a common situation where a type-name should be present,
2563    but is not, and issue a sensible error message.  Returns true if an
2564    invalid type-name was detected.
2565
2566    The situation handled by this function are variable declarations of the
2567    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2568    Usually, `ID' should name a type, but if we got here it means that it
2569    does not. We try to emit the best possible error message depending on
2570    how exactly the id-expression looks like.  */
2571
2572 static bool
2573 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2574 {
2575   tree id;
2576   cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578   /* Avoid duplicate error about ambiguous lookup.  */
2579   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2580     {
2581       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2582       if (next->type == CPP_NAME && next->ambiguous_p)
2583         goto out;
2584     }
2585
2586   cp_parser_parse_tentatively (parser);
2587   id = cp_parser_id_expression (parser,
2588                                 /*template_keyword_p=*/false,
2589                                 /*check_dependency_p=*/true,
2590                                 /*template_p=*/NULL,
2591                                 /*declarator_p=*/true,
2592                                 /*optional_p=*/false);
2593   /* If the next token is a (, this is a function with no explicit return
2594      type, i.e. constructor, destructor or conversion op.  */
2595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2596       || TREE_CODE (id) == TYPE_DECL)
2597     {
2598       cp_parser_abort_tentative_parse (parser);
2599       return false;
2600     }
2601   if (!cp_parser_parse_definitely (parser))
2602     return false;
2603
2604   /* Emit a diagnostic for the invalid type.  */
2605   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2606                                         id, token->location);
2607  out:
2608   /* If we aren't in the middle of a declarator (i.e. in a
2609      parameter-declaration-clause), skip to the end of the declaration;
2610      there's no point in trying to process it.  */
2611   if (!parser->in_declarator_p)
2612     cp_parser_skip_to_end_of_block_or_statement (parser);
2613   return true;
2614 }
2615
2616 /* Consume tokens up to, and including, the next non-nested closing `)'.
2617    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2618    are doing error recovery. Returns -1 if OR_COMMA is true and we
2619    found an unnested comma.  */
2620
2621 static int
2622 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2623                                        bool recovering,
2624                                        bool or_comma,
2625                                        bool consume_paren)
2626 {
2627   unsigned paren_depth = 0;
2628   unsigned brace_depth = 0;
2629   unsigned square_depth = 0;
2630
2631   if (recovering && !or_comma
2632       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2633     return 0;
2634
2635   while (true)
2636     {
2637       cp_token * token = cp_lexer_peek_token (parser->lexer);
2638
2639       switch (token->type)
2640         {
2641         case CPP_EOF:
2642         case CPP_PRAGMA_EOL:
2643           /* If we've run out of tokens, then there is no closing `)'.  */
2644           return 0;
2645
2646         /* This is good for lambda expression capture-lists.  */
2647         case CPP_OPEN_SQUARE:
2648           ++square_depth;
2649           break;
2650         case CPP_CLOSE_SQUARE:
2651           if (!square_depth--)
2652             return 0;
2653           break;
2654
2655         case CPP_SEMICOLON:
2656           /* This matches the processing in skip_to_end_of_statement.  */
2657           if (!brace_depth)
2658             return 0;
2659           break;
2660
2661         case CPP_OPEN_BRACE:
2662           ++brace_depth;
2663           break;
2664         case CPP_CLOSE_BRACE:
2665           if (!brace_depth--)
2666             return 0;
2667           break;
2668
2669         case CPP_COMMA:
2670           if (recovering && or_comma && !brace_depth && !paren_depth
2671               && !square_depth)
2672             return -1;
2673           break;
2674
2675         case CPP_OPEN_PAREN:
2676           if (!brace_depth)
2677             ++paren_depth;
2678           break;
2679
2680         case CPP_CLOSE_PAREN:
2681           if (!brace_depth && !paren_depth--)
2682             {
2683               if (consume_paren)
2684                 cp_lexer_consume_token (parser->lexer);
2685               return 1;
2686             }
2687           break;
2688
2689         default:
2690           break;
2691         }
2692
2693       /* Consume the token.  */
2694       cp_lexer_consume_token (parser->lexer);
2695     }
2696 }
2697
2698 /* Consume tokens until we reach the end of the current statement.
2699    Normally, that will be just before consuming a `;'.  However, if a
2700    non-nested `}' comes first, then we stop before consuming that.  */
2701
2702 static void
2703 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2704 {
2705   unsigned nesting_depth = 0;
2706
2707   while (true)
2708     {
2709       cp_token *token = cp_lexer_peek_token (parser->lexer);
2710
2711       switch (token->type)
2712         {
2713         case CPP_EOF:
2714         case CPP_PRAGMA_EOL:
2715           /* If we've run out of tokens, stop.  */
2716           return;
2717
2718         case CPP_SEMICOLON:
2719           /* If the next token is a `;', we have reached the end of the
2720              statement.  */
2721           if (!nesting_depth)
2722             return;
2723           break;
2724
2725         case CPP_CLOSE_BRACE:
2726           /* If this is a non-nested '}', stop before consuming it.
2727              That way, when confronted with something like:
2728
2729                { 3 + }
2730
2731              we stop before consuming the closing '}', even though we
2732              have not yet reached a `;'.  */
2733           if (nesting_depth == 0)
2734             return;
2735
2736           /* If it is the closing '}' for a block that we have
2737              scanned, stop -- but only after consuming the token.
2738              That way given:
2739
2740                 void f g () { ... }
2741                 typedef int I;
2742
2743              we will stop after the body of the erroneously declared
2744              function, but before consuming the following `typedef'
2745              declaration.  */
2746           if (--nesting_depth == 0)
2747             {
2748               cp_lexer_consume_token (parser->lexer);
2749               return;
2750             }
2751
2752         case CPP_OPEN_BRACE:
2753           ++nesting_depth;
2754           break;
2755
2756         default:
2757           break;
2758         }
2759
2760       /* Consume the token.  */
2761       cp_lexer_consume_token (parser->lexer);
2762     }
2763 }
2764
2765 /* This function is called at the end of a statement or declaration.
2766    If the next token is a semicolon, it is consumed; otherwise, error
2767    recovery is attempted.  */
2768
2769 static void
2770 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2771 {
2772   /* Look for the trailing `;'.  */
2773   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2774     {
2775       /* If there is additional (erroneous) input, skip to the end of
2776          the statement.  */
2777       cp_parser_skip_to_end_of_statement (parser);
2778       /* If the next token is now a `;', consume it.  */
2779       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2780         cp_lexer_consume_token (parser->lexer);
2781     }
2782 }
2783
2784 /* Skip tokens until we have consumed an entire block, or until we
2785    have consumed a non-nested `;'.  */
2786
2787 static void
2788 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2789 {
2790   int nesting_depth = 0;
2791
2792   while (nesting_depth >= 0)
2793     {
2794       cp_token *token = cp_lexer_peek_token (parser->lexer);
2795
2796       switch (token->type)
2797         {
2798         case CPP_EOF:
2799         case CPP_PRAGMA_EOL:
2800           /* If we've run out of tokens, stop.  */
2801           return;
2802
2803         case CPP_SEMICOLON:
2804           /* Stop if this is an unnested ';'. */
2805           if (!nesting_depth)
2806             nesting_depth = -1;
2807           break;
2808
2809         case CPP_CLOSE_BRACE:
2810           /* Stop if this is an unnested '}', or closes the outermost
2811              nesting level.  */
2812           nesting_depth--;
2813           if (nesting_depth < 0)
2814             return;
2815           if (!nesting_depth)
2816             nesting_depth = -1;
2817           break;
2818
2819         case CPP_OPEN_BRACE:
2820           /* Nest. */
2821           nesting_depth++;
2822           break;
2823
2824         default:
2825           break;
2826         }
2827
2828       /* Consume the token.  */
2829       cp_lexer_consume_token (parser->lexer);
2830     }
2831 }
2832
2833 /* Skip tokens until a non-nested closing curly brace is the next
2834    token, or there are no more tokens. Return true in the first case,
2835    false otherwise.  */
2836
2837 static bool
2838 cp_parser_skip_to_closing_brace (cp_parser *parser)
2839 {
2840   unsigned nesting_depth = 0;
2841
2842   while (true)
2843     {
2844       cp_token *token = cp_lexer_peek_token (parser->lexer);
2845
2846       switch (token->type)
2847         {
2848         case CPP_EOF:
2849         case CPP_PRAGMA_EOL:
2850           /* If we've run out of tokens, stop.  */
2851           return false;
2852
2853         case CPP_CLOSE_BRACE:
2854           /* If the next token is a non-nested `}', then we have reached
2855              the end of the current block.  */
2856           if (nesting_depth-- == 0)
2857             return true;
2858           break;
2859
2860         case CPP_OPEN_BRACE:
2861           /* If it the next token is a `{', then we are entering a new
2862              block.  Consume the entire block.  */
2863           ++nesting_depth;
2864           break;
2865
2866         default:
2867           break;
2868         }
2869
2870       /* Consume the token.  */
2871       cp_lexer_consume_token (parser->lexer);
2872     }
2873 }
2874
2875 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2876    parameter is the PRAGMA token, allowing us to purge the entire pragma
2877    sequence.  */
2878
2879 static void
2880 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2881 {
2882   cp_token *token;
2883
2884   parser->lexer->in_pragma = false;
2885
2886   do
2887     token = cp_lexer_consume_token (parser->lexer);
2888   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2889
2890   /* Ensure that the pragma is not parsed again.  */
2891   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2892 }
2893
2894 /* Require pragma end of line, resyncing with it as necessary.  The
2895    arguments are as for cp_parser_skip_to_pragma_eol.  */
2896
2897 static void
2898 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2899 {
2900   parser->lexer->in_pragma = false;
2901   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2902     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2903 }
2904
2905 /* This is a simple wrapper around make_typename_type. When the id is
2906    an unresolved identifier node, we can provide a superior diagnostic
2907    using cp_parser_diagnose_invalid_type_name.  */
2908
2909 static tree
2910 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2911                               tree id, location_t id_location)
2912 {
2913   tree result;
2914   if (TREE_CODE (id) == IDENTIFIER_NODE)
2915     {
2916       result = make_typename_type (scope, id, typename_type,
2917                                    /*complain=*/tf_none);
2918       if (result == error_mark_node)
2919         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2920       return result;
2921     }
2922   return make_typename_type (scope, id, typename_type, tf_error);
2923 }
2924
2925 /* This is a wrapper around the
2926    make_{pointer,ptrmem,reference}_declarator functions that decides
2927    which one to call based on the CODE and CLASS_TYPE arguments. The
2928    CODE argument should be one of the values returned by
2929    cp_parser_ptr_operator. */
2930 static cp_declarator *
2931 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2932                                     cp_cv_quals cv_qualifiers,
2933                                     cp_declarator *target)
2934 {
2935   if (code == ERROR_MARK)
2936     return cp_error_declarator;
2937
2938   if (code == INDIRECT_REF)
2939     if (class_type == NULL_TREE)
2940       return make_pointer_declarator (cv_qualifiers, target);
2941     else
2942       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2943   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2944     return make_reference_declarator (cv_qualifiers, target, false);
2945   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2946     return make_reference_declarator (cv_qualifiers, target, true);
2947   gcc_unreachable ();
2948 }
2949
2950 /* Create a new C++ parser.  */
2951
2952 static cp_parser *
2953 cp_parser_new (void)
2954 {
2955   cp_parser *parser;
2956   cp_lexer *lexer;
2957   unsigned i;
2958
2959   /* cp_lexer_new_main is called before doing GC allocation because
2960      cp_lexer_new_main might load a PCH file.  */
2961   lexer = cp_lexer_new_main ();
2962
2963   /* Initialize the binops_by_token so that we can get the tree
2964      directly from the token.  */
2965   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2966     binops_by_token[binops[i].token_type] = binops[i];
2967
2968   parser = ggc_alloc_cleared_cp_parser ();
2969   parser->lexer = lexer;
2970   parser->context = cp_parser_context_new (NULL);
2971
2972   /* For now, we always accept GNU extensions.  */
2973   parser->allow_gnu_extensions_p = 1;
2974
2975   /* The `>' token is a greater-than operator, not the end of a
2976      template-id.  */
2977   parser->greater_than_is_operator_p = true;
2978
2979   parser->default_arg_ok_p = true;
2980
2981   /* We are not parsing a constant-expression.  */
2982   parser->integral_constant_expression_p = false;
2983   parser->allow_non_integral_constant_expression_p = false;
2984   parser->non_integral_constant_expression_p = false;
2985
2986   /* Local variable names are not forbidden.  */
2987   parser->local_variables_forbidden_p = false;
2988
2989   /* We are not processing an `extern "C"' declaration.  */
2990   parser->in_unbraced_linkage_specification_p = false;
2991
2992   /* We are not processing a declarator.  */
2993   parser->in_declarator_p = false;
2994
2995   /* We are not processing a template-argument-list.  */
2996   parser->in_template_argument_list_p = false;
2997
2998   /* We are not in an iteration statement.  */
2999   parser->in_statement = 0;
3000
3001   /* We are not in a switch statement.  */
3002   parser->in_switch_statement_p = false;
3003
3004   /* We are not parsing a type-id inside an expression.  */
3005   parser->in_type_id_in_expr_p = false;
3006
3007   /* Declarations aren't implicitly extern "C".  */
3008   parser->implicit_extern_c = false;
3009
3010   /* String literals should be translated to the execution character set.  */
3011   parser->translate_strings_p = true;
3012
3013   /* We are not parsing a function body.  */
3014   parser->in_function_body = false;
3015
3016   /* We can correct until told otherwise.  */
3017   parser->colon_corrects_to_scope_p = true;
3018
3019   /* The unparsed function queue is empty.  */
3020   push_unparsed_function_queues (parser);
3021
3022   /* There are no classes being defined.  */
3023   parser->num_classes_being_defined = 0;
3024
3025   /* No template parameters apply.  */
3026   parser->num_template_parameter_lists = 0;
3027
3028   return parser;
3029 }
3030
3031 /* Create a cp_lexer structure which will emit the tokens in CACHE
3032    and push it onto the parser's lexer stack.  This is used for delayed
3033    parsing of in-class method bodies and default arguments, and should
3034    not be confused with tentative parsing.  */
3035 static void
3036 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3037 {
3038   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3039   lexer->next = parser->lexer;
3040   parser->lexer = lexer;
3041
3042   /* Move the current source position to that of the first token in the
3043      new lexer.  */
3044   cp_lexer_set_source_position_from_token (lexer->next_token);
3045 }
3046
3047 /* Pop the top lexer off the parser stack.  This is never used for the
3048    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3049 static void
3050 cp_parser_pop_lexer (cp_parser *parser)
3051 {
3052   cp_lexer *lexer = parser->lexer;
3053   parser->lexer = lexer->next;
3054   cp_lexer_destroy (lexer);
3055
3056   /* Put the current source position back where it was before this
3057      lexer was pushed.  */
3058   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3059 }
3060
3061 /* Lexical conventions [gram.lex]  */
3062
3063 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3064    identifier.  */
3065
3066 static tree
3067 cp_parser_identifier (cp_parser* parser)
3068 {
3069   cp_token *token;
3070
3071   /* Look for the identifier.  */
3072   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3073   /* Return the value.  */
3074   return token ? token->u.value : error_mark_node;
3075 }
3076
3077 /* Parse a sequence of adjacent string constants.  Returns a
3078    TREE_STRING representing the combined, nul-terminated string
3079    constant.  If TRANSLATE is true, translate the string to the
3080    execution character set.  If WIDE_OK is true, a wide string is
3081    invalid here.
3082
3083    C++98 [lex.string] says that if a narrow string literal token is
3084    adjacent to a wide string literal token, the behavior is undefined.
3085    However, C99 6.4.5p4 says that this results in a wide string literal.
3086    We follow C99 here, for consistency with the C front end.
3087
3088    This code is largely lifted from lex_string() in c-lex.c.
3089
3090    FUTURE: ObjC++ will need to handle @-strings here.  */
3091 static tree
3092 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3093 {
3094   tree value;
3095   size_t count;
3096   struct obstack str_ob;
3097   cpp_string str, istr, *strs;
3098   cp_token *tok;
3099   enum cpp_ttype type;
3100
3101   tok = cp_lexer_peek_token (parser->lexer);
3102   if (!cp_parser_is_string_literal (tok))
3103     {
3104       cp_parser_error (parser, "expected string-literal");
3105       return error_mark_node;
3106     }
3107
3108   type = tok->type;
3109
3110   /* Try to avoid the overhead of creating and destroying an obstack
3111      for the common case of just one string.  */
3112   if (!cp_parser_is_string_literal
3113       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3114     {
3115       cp_lexer_consume_token (parser->lexer);
3116
3117       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3118       str.len = TREE_STRING_LENGTH (tok->u.value);
3119       count = 1;
3120
3121       strs = &str;
3122     }
3123   else
3124     {
3125       gcc_obstack_init (&str_ob);
3126       count = 0;
3127
3128       do
3129         {
3130           cp_lexer_consume_token (parser->lexer);
3131           count++;
3132           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3133           str.len = TREE_STRING_LENGTH (tok->u.value);
3134
3135           if (type != tok->type)
3136             {
3137               if (type == CPP_STRING)
3138                 type = tok->type;
3139               else if (tok->type != CPP_STRING)
3140                 error_at (tok->location,
3141                           "unsupported non-standard concatenation "
3142                           "of string literals");
3143             }
3144
3145           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3146
3147           tok = cp_lexer_peek_token (parser->lexer);
3148         }
3149       while (cp_parser_is_string_literal (tok));
3150
3151       strs = (cpp_string *) obstack_finish (&str_ob);
3152     }
3153
3154   if (type != CPP_STRING && !wide_ok)
3155     {
3156       cp_parser_error (parser, "a wide string is invalid in this context");
3157       type = CPP_STRING;
3158     }
3159
3160   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3161       (parse_in, strs, count, &istr, type))
3162     {
3163       value = build_string (istr.len, (const char *)istr.text);
3164       free (CONST_CAST (unsigned char *, istr.text));
3165
3166       switch (type)
3167         {
3168         default:
3169         case CPP_STRING:
3170         case CPP_UTF8STRING:
3171           TREE_TYPE (value) = char_array_type_node;
3172           break;
3173         case CPP_STRING16:
3174           TREE_TYPE (value) = char16_array_type_node;
3175           break;
3176         case CPP_STRING32:
3177           TREE_TYPE (value) = char32_array_type_node;
3178           break;
3179         case CPP_WSTRING:
3180           TREE_TYPE (value) = wchar_array_type_node;
3181           break;
3182         }
3183
3184       value = fix_string_type (value);
3185     }
3186   else
3187     /* cpp_interpret_string has issued an error.  */
3188     value = error_mark_node;
3189
3190   if (count > 1)
3191     obstack_free (&str_ob, 0);
3192
3193   return value;
3194 }
3195
3196
3197 /* Basic concepts [gram.basic]  */
3198
3199 /* Parse a translation-unit.
3200
3201    translation-unit:
3202      declaration-seq [opt]
3203
3204    Returns TRUE if all went well.  */
3205
3206 static bool
3207 cp_parser_translation_unit (cp_parser* parser)
3208 {
3209   /* The address of the first non-permanent object on the declarator
3210      obstack.  */
3211   static void *declarator_obstack_base;
3212
3213   bool success;
3214
3215   /* Create the declarator obstack, if necessary.  */
3216   if (!cp_error_declarator)
3217     {
3218       gcc_obstack_init (&declarator_obstack);
3219       /* Create the error declarator.  */
3220       cp_error_declarator = make_declarator (cdk_error);
3221       /* Create the empty parameter list.  */
3222       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3223       /* Remember where the base of the declarator obstack lies.  */
3224       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3225     }
3226
3227   cp_parser_declaration_seq_opt (parser);
3228
3229   /* If there are no tokens left then all went well.  */
3230   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3231     {
3232       /* Get rid of the token array; we don't need it any more.  */
3233       cp_lexer_destroy (parser->lexer);
3234       parser->lexer = NULL;
3235
3236       /* This file might have been a context that's implicitly extern
3237          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3238       if (parser->implicit_extern_c)
3239         {
3240           pop_lang_context ();
3241           parser->implicit_extern_c = false;
3242         }
3243
3244       /* Finish up.  */
3245       finish_translation_unit ();
3246
3247       success = true;
3248     }
3249   else
3250     {
3251       cp_parser_error (parser, "expected declaration");
3252       success = false;
3253     }
3254
3255   /* Make sure the declarator obstack was fully cleaned up.  */
3256   gcc_assert (obstack_next_free (&declarator_obstack)
3257               == declarator_obstack_base);
3258
3259   /* All went well.  */
3260   return success;
3261 }
3262
3263 /* Expressions [gram.expr] */
3264
3265 /* Parse a primary-expression.
3266
3267    primary-expression:
3268      literal
3269      this
3270      ( expression )
3271      id-expression
3272
3273    GNU Extensions:
3274
3275    primary-expression:
3276      ( compound-statement )
3277      __builtin_va_arg ( assignment-expression , type-id )
3278      __builtin_offsetof ( type-id , offsetof-expression )
3279
3280    C++ Extensions:
3281      __has_nothrow_assign ( type-id )   
3282      __has_nothrow_constructor ( type-id )
3283      __has_nothrow_copy ( type-id )
3284      __has_trivial_assign ( type-id )   
3285      __has_trivial_constructor ( type-id )
3286      __has_trivial_copy ( type-id )
3287      __has_trivial_destructor ( type-id )
3288      __has_virtual_destructor ( type-id )     
3289      __is_abstract ( type-id )
3290      __is_base_of ( type-id , type-id )
3291      __is_class ( type-id )
3292      __is_convertible_to ( type-id , type-id )     
3293      __is_empty ( type-id )
3294      __is_enum ( type-id )
3295      __is_literal_type ( type-id )
3296      __is_pod ( type-id )
3297      __is_polymorphic ( type-id )
3298      __is_std_layout ( type-id )
3299      __is_trivial ( type-id )
3300      __is_union ( type-id )
3301
3302    Objective-C++ Extension:
3303
3304    primary-expression:
3305      objc-expression
3306
3307    literal:
3308      __null
3309
3310    ADDRESS_P is true iff this expression was immediately preceded by
3311    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3312    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3313    true iff this expression is a template argument.
3314
3315    Returns a representation of the expression.  Upon return, *IDK
3316    indicates what kind of id-expression (if any) was present.  */
3317
3318 static tree
3319 cp_parser_primary_expression (cp_parser *parser,
3320                               bool address_p,
3321                               bool cast_p,
3322                               bool template_arg_p,
3323                               cp_id_kind *idk)
3324 {
3325   cp_token *token = NULL;
3326
3327   /* Assume the primary expression is not an id-expression.  */
3328   *idk = CP_ID_KIND_NONE;
3329
3330   /* Peek at the next token.  */
3331   token = cp_lexer_peek_token (parser->lexer);
3332   switch (token->type)
3333     {
3334       /* literal:
3335            integer-literal
3336            character-literal
3337            floating-literal
3338            string-literal
3339            boolean-literal  */
3340     case CPP_CHAR:
3341     case CPP_CHAR16:
3342     case CPP_CHAR32:
3343     case CPP_WCHAR:
3344     case CPP_NUMBER:
3345       token = cp_lexer_consume_token (parser->lexer);
3346       if (TREE_CODE (token->u.value) == FIXED_CST)
3347         {
3348           error_at (token->location,
3349                     "fixed-point types not supported in C++");
3350           return error_mark_node;
3351         }
3352       /* Floating-point literals are only allowed in an integral
3353          constant expression if they are cast to an integral or
3354          enumeration type.  */
3355       if (TREE_CODE (token->u.value) == REAL_CST
3356           && parser->integral_constant_expression_p
3357           && pedantic)
3358         {
3359           /* CAST_P will be set even in invalid code like "int(2.7 +
3360              ...)".   Therefore, we have to check that the next token
3361              is sure to end the cast.  */
3362           if (cast_p)
3363             {
3364               cp_token *next_token;
3365
3366               next_token = cp_lexer_peek_token (parser->lexer);
3367               if (/* The comma at the end of an
3368                      enumerator-definition.  */
3369                   next_token->type != CPP_COMMA
3370                   /* The curly brace at the end of an enum-specifier.  */
3371                   && next_token->type != CPP_CLOSE_BRACE
3372                   /* The end of a statement.  */
3373                   && next_token->type != CPP_SEMICOLON
3374                   /* The end of the cast-expression.  */
3375                   && next_token->type != CPP_CLOSE_PAREN
3376                   /* The end of an array bound.  */
3377                   && next_token->type != CPP_CLOSE_SQUARE
3378                   /* The closing ">" in a template-argument-list.  */
3379                   && (next_token->type != CPP_GREATER
3380                       || parser->greater_than_is_operator_p)
3381                   /* C++0x only: A ">>" treated like two ">" tokens,
3382                      in a template-argument-list.  */
3383                   && (next_token->type != CPP_RSHIFT
3384                       || (cxx_dialect == cxx98)
3385                       || parser->greater_than_is_operator_p))
3386                 cast_p = false;
3387             }
3388
3389           /* If we are within a cast, then the constraint that the
3390              cast is to an integral or enumeration type will be
3391              checked at that point.  If we are not within a cast, then
3392              this code is invalid.  */
3393           if (!cast_p)
3394             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3395         }
3396       return token->u.value;
3397
3398     case CPP_STRING:
3399     case CPP_STRING16:
3400     case CPP_STRING32:
3401     case CPP_WSTRING:
3402     case CPP_UTF8STRING:
3403       /* ??? Should wide strings be allowed when parser->translate_strings_p
3404          is false (i.e. in attributes)?  If not, we can kill the third
3405          argument to cp_parser_string_literal.  */
3406       return cp_parser_string_literal (parser,
3407                                        parser->translate_strings_p,
3408                                        true);
3409
3410     case CPP_OPEN_PAREN:
3411       {
3412         tree expr;
3413         bool saved_greater_than_is_operator_p;
3414
3415         /* Consume the `('.  */
3416         cp_lexer_consume_token (parser->lexer);
3417         /* Within a parenthesized expression, a `>' token is always
3418            the greater-than operator.  */
3419         saved_greater_than_is_operator_p
3420           = parser->greater_than_is_operator_p;
3421         parser->greater_than_is_operator_p = true;
3422         /* If we see `( { ' then we are looking at the beginning of
3423            a GNU statement-expression.  */
3424         if (cp_parser_allow_gnu_extensions_p (parser)
3425             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3426           {
3427             /* Statement-expressions are not allowed by the standard.  */
3428             pedwarn (token->location, OPT_pedantic, 
3429                      "ISO C++ forbids braced-groups within expressions");
3430
3431             /* And they're not allowed outside of a function-body; you
3432                cannot, for example, write:
3433
3434                  int i = ({ int j = 3; j + 1; });
3435
3436                at class or namespace scope.  */
3437             if (!parser->in_function_body
3438                 || parser->in_template_argument_list_p)
3439               {
3440                 error_at (token->location,
3441                           "statement-expressions are not allowed outside "
3442                           "functions nor in template-argument lists");
3443                 cp_parser_skip_to_end_of_block_or_statement (parser);
3444                 expr = error_mark_node;
3445               }
3446             else
3447               {
3448                 /* Start the statement-expression.  */
3449                 expr = begin_stmt_expr ();
3450                 /* Parse the compound-statement.  */
3451                 cp_parser_compound_statement (parser, expr, false, false);
3452                 /* Finish up.  */
3453                 expr = finish_stmt_expr (expr, false);
3454               }
3455           }
3456         else
3457           {
3458             /* Parse the parenthesized expression.  */
3459             expr = cp_parser_expression (parser, cast_p, idk);
3460             /* Let the front end know that this expression was
3461                enclosed in parentheses. This matters in case, for
3462                example, the expression is of the form `A::B', since
3463                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3464                not.  */
3465             finish_parenthesized_expr (expr);
3466             /* DR 705: Wrapping an unqualified name in parentheses
3467                suppresses arg-dependent lookup.  We want to pass back
3468                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3469                (c++/37862), but none of the others.  */
3470             if (*idk != CP_ID_KIND_QUALIFIED)
3471               *idk = CP_ID_KIND_NONE;
3472           }
3473         /* The `>' token might be the end of a template-id or
3474            template-parameter-list now.  */
3475         parser->greater_than_is_operator_p
3476           = saved_greater_than_is_operator_p;
3477         /* Consume the `)'.  */
3478         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3479           cp_parser_skip_to_end_of_statement (parser);
3480
3481         return expr;
3482       }
3483
3484     case CPP_OPEN_SQUARE:
3485       if (c_dialect_objc ())
3486         /* We have an Objective-C++ message. */
3487         return cp_parser_objc_expression (parser);
3488       {
3489         tree lam = cp_parser_lambda_expression (parser);
3490         /* Don't warn about a failed tentative parse.  */
3491         if (cp_parser_error_occurred (parser))
3492           return error_mark_node;
3493         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3494         return lam;
3495       }
3496
3497     case CPP_OBJC_STRING:
3498       if (c_dialect_objc ())
3499         /* We have an Objective-C++ string literal. */
3500         return cp_parser_objc_expression (parser);
3501       cp_parser_error (parser, "expected primary-expression");
3502       return error_mark_node;
3503
3504     case CPP_KEYWORD:
3505       switch (token->keyword)
3506         {
3507           /* These two are the boolean literals.  */
3508         case RID_TRUE:
3509           cp_lexer_consume_token (parser->lexer);
3510           return boolean_true_node;
3511         case RID_FALSE:
3512           cp_lexer_consume_token (parser->lexer);
3513           return boolean_false_node;
3514
3515           /* The `__null' literal.  */
3516         case RID_NULL:
3517           cp_lexer_consume_token (parser->lexer);
3518           return null_node;
3519
3520           /* The `nullptr' literal.  */
3521         case RID_NULLPTR:
3522           cp_lexer_consume_token (parser->lexer);
3523           return nullptr_node;
3524
3525           /* Recognize the `this' keyword.  */
3526         case RID_THIS:
3527           cp_lexer_consume_token (parser->lexer);
3528           if (parser->local_variables_forbidden_p)
3529             {
3530               error_at (token->location,
3531                         "%<this%> may not be used in this context");
3532               return error_mark_node;
3533             }
3534           /* Pointers cannot appear in constant-expressions.  */
3535           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3536             return error_mark_node;
3537           return finish_this_expr ();
3538
3539           /* The `operator' keyword can be the beginning of an
3540              id-expression.  */
3541         case RID_OPERATOR:
3542           goto id_expression;
3543
3544         case RID_FUNCTION_NAME:
3545         case RID_PRETTY_FUNCTION_NAME:
3546         case RID_C99_FUNCTION_NAME:
3547           {
3548             non_integral_constant name;
3549
3550             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3551                __func__ are the names of variables -- but they are
3552                treated specially.  Therefore, they are handled here,
3553                rather than relying on the generic id-expression logic
3554                below.  Grammatically, these names are id-expressions.
3555
3556                Consume the token.  */
3557             token = cp_lexer_consume_token (parser->lexer);
3558
3559             switch (token->keyword)
3560               {
3561               case RID_FUNCTION_NAME:
3562                 name = NIC_FUNC_NAME;
3563                 break;
3564               case RID_PRETTY_FUNCTION_NAME:
3565                 name = NIC_PRETTY_FUNC;
3566                 break;
3567               case RID_C99_FUNCTION_NAME:
3568                 name = NIC_C99_FUNC;
3569                 break;
3570               default:
3571                 gcc_unreachable ();
3572               }
3573
3574             if (cp_parser_non_integral_constant_expression (parser, name))
3575               return error_mark_node;
3576
3577             /* Look up the name.  */
3578             return finish_fname (token->u.value);
3579           }
3580
3581         case RID_VA_ARG:
3582           {
3583             tree expression;
3584             tree type;
3585
3586             /* The `__builtin_va_arg' construct is used to handle
3587                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3588             cp_lexer_consume_token (parser->lexer);
3589             /* Look for the opening `('.  */
3590             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3591             /* Now, parse the assignment-expression.  */
3592             expression = cp_parser_assignment_expression (parser,
3593                                                           /*cast_p=*/false, NULL);
3594             /* Look for the `,'.  */
3595             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3596             /* Parse the type-id.  */
3597             type = cp_parser_type_id (parser);
3598             /* Look for the closing `)'.  */
3599             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3600             /* Using `va_arg' in a constant-expression is not
3601                allowed.  */
3602             if (cp_parser_non_integral_constant_expression (parser,
3603                                                             NIC_VA_ARG))
3604               return error_mark_node;
3605             return build_x_va_arg (expression, type);
3606           }
3607
3608         case RID_OFFSETOF:
3609           return cp_parser_builtin_offsetof (parser);
3610
3611         case RID_HAS_NOTHROW_ASSIGN:
3612         case RID_HAS_NOTHROW_CONSTRUCTOR:
3613         case RID_HAS_NOTHROW_COPY:        
3614         case RID_HAS_TRIVIAL_ASSIGN:
3615         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3616         case RID_HAS_TRIVIAL_COPY:        
3617         case RID_HAS_TRIVIAL_DESTRUCTOR:
3618         case RID_HAS_VIRTUAL_DESTRUCTOR:
3619         case RID_IS_ABSTRACT:
3620         case RID_IS_BASE_OF:
3621         case RID_IS_CLASS:
3622         case RID_IS_CONVERTIBLE_TO:
3623         case RID_IS_EMPTY:
3624         case RID_IS_ENUM:
3625         case RID_IS_LITERAL_TYPE:
3626         case RID_IS_POD:
3627         case RID_IS_POLYMORPHIC:
3628         case RID_IS_STD_LAYOUT:
3629         case RID_IS_TRIVIAL:
3630         case RID_IS_UNION:
3631           return cp_parser_trait_expr (parser, token->keyword);
3632
3633         /* Objective-C++ expressions.  */
3634         case RID_AT_ENCODE:
3635         case RID_AT_PROTOCOL:
3636         case RID_AT_SELECTOR:
3637           return cp_parser_objc_expression (parser);
3638
3639         case RID_TEMPLATE:
3640           if (parser->in_function_body
3641               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3642                   == CPP_LESS))
3643             {
3644               error_at (token->location,
3645                         "a template declaration cannot appear at block scope");
3646               cp_parser_skip_to_end_of_block_or_statement (parser);
3647               return error_mark_node;
3648             }
3649         default:
3650           cp_parser_error (parser, "expected primary-expression");
3651           return error_mark_node;
3652         }
3653
3654       /* An id-expression can start with either an identifier, a
3655          `::' as the beginning of a qualified-id, or the "operator"
3656          keyword.  */
3657     case CPP_NAME:
3658     case CPP_SCOPE:
3659     case CPP_TEMPLATE_ID:
3660     case CPP_NESTED_NAME_SPECIFIER:
3661       {
3662         tree id_expression;
3663         tree decl;
3664         const char *error_msg;
3665         bool template_p;
3666         bool done;
3667         cp_token *id_expr_token;
3668
3669       id_expression:
3670         /* Parse the id-expression.  */
3671         id_expression
3672           = cp_parser_id_expression (parser,
3673                                      /*template_keyword_p=*/false,
3674                                      /*check_dependency_p=*/true,
3675                                      &template_p,
3676                                      /*declarator_p=*/false,
3677                                      /*optional_p=*/false);
3678         if (id_expression == error_mark_node)
3679           return error_mark_node;
3680         id_expr_token = token;
3681         token = cp_lexer_peek_token (parser->lexer);
3682         done = (token->type != CPP_OPEN_SQUARE
3683                 && token->type != CPP_OPEN_PAREN
3684                 && token->type != CPP_DOT
3685                 && token->type != CPP_DEREF
3686                 && token->type != CPP_PLUS_PLUS
3687                 && token->type != CPP_MINUS_MINUS);
3688         /* If we have a template-id, then no further lookup is
3689            required.  If the template-id was for a template-class, we
3690            will sometimes have a TYPE_DECL at this point.  */
3691         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3692                  || TREE_CODE (id_expression) == TYPE_DECL)
3693           decl = id_expression;
3694         /* Look up the name.  */
3695         else
3696           {
3697             tree ambiguous_decls;
3698
3699             /* If we already know that this lookup is ambiguous, then
3700                we've already issued an error message; there's no reason
3701                to check again.  */
3702             if (id_expr_token->type == CPP_NAME
3703                 && id_expr_token->ambiguous_p)
3704               {
3705                 cp_parser_simulate_error (parser);
3706                 return error_mark_node;
3707               }
3708
3709             decl = cp_parser_lookup_name (parser, id_expression,
3710                                           none_type,
3711                                           template_p,
3712                                           /*is_namespace=*/false,
3713                                           /*check_dependency=*/true,
3714                                           &ambiguous_decls,
3715                                           id_expr_token->location);
3716             /* If the lookup was ambiguous, an error will already have
3717                been issued.  */
3718             if (ambiguous_decls)
3719               return error_mark_node;
3720
3721             /* In Objective-C++, we may have an Objective-C 2.0
3722                dot-syntax for classes here.  */
3723             if (c_dialect_objc ()
3724                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3725                 && TREE_CODE (decl) == TYPE_DECL
3726                 && objc_is_class_name (decl))
3727               {
3728                 tree component;
3729                 cp_lexer_consume_token (parser->lexer);
3730                 component = cp_parser_identifier (parser);
3731                 if (component == error_mark_node)
3732                   return error_mark_node;
3733
3734                 return objc_build_class_component_ref (id_expression, component);
3735               }
3736
3737             /* In Objective-C++, an instance variable (ivar) may be preferred
3738                to whatever cp_parser_lookup_name() found.  */
3739             decl = objc_lookup_ivar (decl, id_expression);
3740
3741             /* If name lookup gives us a SCOPE_REF, then the
3742                qualifying scope was dependent.  */
3743             if (TREE_CODE (decl) == SCOPE_REF)
3744               {
3745                 /* At this point, we do not know if DECL is a valid
3746                    integral constant expression.  We assume that it is
3747                    in fact such an expression, so that code like:
3748
3749                       template <int N> struct A {
3750                         int a[B<N>::i];
3751                       };
3752                      
3753                    is accepted.  At template-instantiation time, we
3754                    will check that B<N>::i is actually a constant.  */
3755                 return decl;
3756               }
3757             /* Check to see if DECL is a local variable in a context
3758                where that is forbidden.  */
3759             if (parser->local_variables_forbidden_p
3760                 && local_variable_p (decl))
3761               {
3762                 /* It might be that we only found DECL because we are
3763                    trying to be generous with pre-ISO scoping rules.
3764                    For example, consider:
3765
3766                      int i;
3767                      void g() {
3768                        for (int i = 0; i < 10; ++i) {}
3769                        extern void f(int j = i);
3770                      }
3771
3772                    Here, name look up will originally find the out
3773                    of scope `i'.  We need to issue a warning message,
3774                    but then use the global `i'.  */
3775                 decl = check_for_out_of_scope_variable (decl);
3776                 if (local_variable_p (decl))
3777                   {
3778                     error_at (id_expr_token->location,
3779                               "local variable %qD may not appear in this context",
3780                               decl);
3781                     return error_mark_node;
3782                   }
3783               }
3784           }
3785
3786         decl = (finish_id_expression
3787                 (id_expression, decl, parser->scope,
3788                  idk,
3789                  parser->integral_constant_expression_p,
3790                  parser->allow_non_integral_constant_expression_p,
3791                  &parser->non_integral_constant_expression_p,
3792                  template_p, done, address_p,
3793                  template_arg_p,
3794                  &error_msg,
3795                  id_expr_token->location));
3796         if (error_msg)
3797           cp_parser_error (parser, error_msg);
3798         return decl;
3799       }
3800
3801       /* Anything else is an error.  */
3802     default:
3803       cp_parser_error (parser, "expected primary-expression");
3804       return error_mark_node;
3805     }
3806 }
3807
3808 /* Parse an id-expression.
3809
3810    id-expression:
3811      unqualified-id
3812      qualified-id
3813
3814    qualified-id:
3815      :: [opt] nested-name-specifier template [opt] unqualified-id
3816      :: identifier
3817      :: operator-function-id
3818      :: template-id
3819
3820    Return a representation of the unqualified portion of the
3821    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3822    a `::' or nested-name-specifier.
3823
3824    Often, if the id-expression was a qualified-id, the caller will
3825    want to make a SCOPE_REF to represent the qualified-id.  This
3826    function does not do this in order to avoid wastefully creating
3827    SCOPE_REFs when they are not required.
3828
3829    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3830    `template' keyword.
3831
3832    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3833    uninstantiated templates.
3834
3835    If *TEMPLATE_P is non-NULL, it is set to true iff the
3836    `template' keyword is used to explicitly indicate that the entity
3837    named is a template.
3838
3839    If DECLARATOR_P is true, the id-expression is appearing as part of
3840    a declarator, rather than as part of an expression.  */
3841
3842 static tree
3843 cp_parser_id_expression (cp_parser *parser,
3844                          bool template_keyword_p,
3845                          bool check_dependency_p,
3846                          bool *template_p,
3847                          bool declarator_p,
3848                          bool optional_p)
3849 {
3850   bool global_scope_p;
3851   bool nested_name_specifier_p;
3852
3853   /* Assume the `template' keyword was not used.  */
3854   if (template_p)
3855     *template_p = template_keyword_p;
3856
3857   /* Look for the optional `::' operator.  */
3858   global_scope_p
3859     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3860        != NULL_TREE);
3861   /* Look for the optional nested-name-specifier.  */
3862   nested_name_specifier_p
3863     = (cp_parser_nested_name_specifier_opt (parser,
3864                                             /*typename_keyword_p=*/false,
3865                                             check_dependency_p,
3866                                             /*type_p=*/false,
3867                                             declarator_p)
3868        != NULL_TREE);
3869   /* If there is a nested-name-specifier, then we are looking at
3870      the first qualified-id production.  */
3871   if (nested_name_specifier_p)
3872     {
3873       tree saved_scope;
3874       tree saved_object_scope;
3875       tree saved_qualifying_scope;
3876       tree unqualified_id;
3877       bool is_template;
3878
3879       /* See if the next token is the `template' keyword.  */
3880       if (!template_p)
3881         template_p = &is_template;
3882       *template_p = cp_parser_optional_template_keyword (parser);
3883       /* Name lookup we do during the processing of the
3884          unqualified-id might obliterate SCOPE.  */
3885       saved_scope = parser->scope;
3886       saved_object_scope = parser->object_scope;
3887       saved_qualifying_scope = parser->qualifying_scope;
3888       /* Process the final unqualified-id.  */
3889       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3890                                                  check_dependency_p,
3891                                                  declarator_p,
3892                                                  /*optional_p=*/false);
3893       /* Restore the SAVED_SCOPE for our caller.  */
3894       parser->scope = saved_scope;
3895       parser->object_scope = saved_object_scope;
3896       parser->qualifying_scope = saved_qualifying_scope;
3897
3898       return unqualified_id;
3899     }
3900   /* Otherwise, if we are in global scope, then we are looking at one
3901      of the other qualified-id productions.  */
3902   else if (global_scope_p)
3903     {
3904       cp_token *token;
3905       tree id;
3906
3907       /* Peek at the next token.  */
3908       token = cp_lexer_peek_token (parser->lexer);
3909
3910       /* If it's an identifier, and the next token is not a "<", then
3911          we can avoid the template-id case.  This is an optimization
3912          for this common case.  */
3913       if (token->type == CPP_NAME
3914           && !cp_parser_nth_token_starts_template_argument_list_p
3915                (parser, 2))
3916         return cp_parser_identifier (parser);
3917
3918       cp_parser_parse_tentatively (parser);
3919       /* Try a template-id.  */
3920       id = cp_parser_template_id (parser,
3921                                   /*template_keyword_p=*/false,
3922                                   /*check_dependency_p=*/true,
3923                                   declarator_p);
3924       /* If that worked, we're done.  */
3925       if (cp_parser_parse_definitely (parser))
3926         return id;
3927
3928       /* Peek at the next token.  (Changes in the token buffer may
3929          have invalidated the pointer obtained above.)  */
3930       token = cp_lexer_peek_token (parser->lexer);
3931
3932       switch (token->type)
3933         {
3934         case CPP_NAME:
3935           return cp_parser_identifier (parser);
3936
3937         case CPP_KEYWORD:
3938           if (token->keyword == RID_OPERATOR)
3939             return cp_parser_operator_function_id (parser);
3940           /* Fall through.  */
3941
3942         default:
3943           cp_parser_error (parser, "expected id-expression");
3944           return error_mark_node;
3945         }
3946     }
3947   else
3948     return cp_parser_unqualified_id (parser, template_keyword_p,
3949                                      /*check_dependency_p=*/true,
3950                                      declarator_p,
3951                                      optional_p);
3952 }
3953
3954 /* Parse an unqualified-id.
3955
3956    unqualified-id:
3957      identifier
3958      operator-function-id
3959      conversion-function-id
3960      ~ class-name
3961      template-id
3962
3963    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3964    keyword, in a construct like `A::template ...'.
3965
3966    Returns a representation of unqualified-id.  For the `identifier'
3967    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3968    production a BIT_NOT_EXPR is returned; the operand of the
3969    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3970    other productions, see the documentation accompanying the
3971    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3972    names are looked up in uninstantiated templates.  If DECLARATOR_P
3973    is true, the unqualified-id is appearing as part of a declarator,
3974    rather than as part of an expression.  */
3975
3976 static tree
3977 cp_parser_unqualified_id (cp_parser* parser,
3978                           bool template_keyword_p,
3979                           bool check_dependency_p,
3980                           bool declarator_p,
3981                           bool optional_p)
3982 {
3983   cp_token *token;
3984
3985   /* Peek at the next token.  */
3986   token = cp_lexer_peek_token (parser->lexer);
3987
3988   switch (token->type)
3989     {
3990     case CPP_NAME:
3991       {
3992         tree id;
3993
3994         /* We don't know yet whether or not this will be a
3995            template-id.  */
3996         cp_parser_parse_tentatively (parser);
3997         /* Try a template-id.  */
3998         id = cp_parser_template_id (parser, template_keyword_p,
3999                                     check_dependency_p,
4000                                     declarator_p);
4001         /* If it worked, we're done.  */
4002         if (cp_parser_parse_definitely (parser))
4003           return id;
4004         /* Otherwise, it's an ordinary identifier.  */
4005         return cp_parser_identifier (parser);
4006       }
4007
4008     case CPP_TEMPLATE_ID:
4009       return cp_parser_template_id (parser, template_keyword_p,
4010                                     check_dependency_p,
4011                                     declarator_p);
4012
4013     case CPP_COMPL:
4014       {
4015         tree type_decl;
4016         tree qualifying_scope;
4017         tree object_scope;
4018         tree scope;
4019         bool done;
4020
4021         /* Consume the `~' token.  */
4022         cp_lexer_consume_token (parser->lexer);
4023         /* Parse the class-name.  The standard, as written, seems to
4024            say that:
4025
4026              template <typename T> struct S { ~S (); };
4027              template <typename T> S<T>::~S() {}
4028
4029            is invalid, since `~' must be followed by a class-name, but
4030            `S<T>' is dependent, and so not known to be a class.
4031            That's not right; we need to look in uninstantiated
4032            templates.  A further complication arises from:
4033
4034              template <typename T> void f(T t) {
4035                t.T::~T();
4036              }
4037
4038            Here, it is not possible to look up `T' in the scope of `T'
4039            itself.  We must look in both the current scope, and the
4040            scope of the containing complete expression.
4041
4042            Yet another issue is:
4043
4044              struct S {
4045                int S;
4046                ~S();
4047              };
4048
4049              S::~S() {}
4050
4051            The standard does not seem to say that the `S' in `~S'
4052            should refer to the type `S' and not the data member
4053            `S::S'.  */
4054
4055         /* DR 244 says that we look up the name after the "~" in the
4056            same scope as we looked up the qualifying name.  That idea
4057            isn't fully worked out; it's more complicated than that.  */
4058         scope = parser->scope;
4059         object_scope = parser->object_scope;
4060         qualifying_scope = parser->qualifying_scope;
4061
4062         /* Check for invalid scopes.  */
4063         if (scope == error_mark_node)
4064           {
4065             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4066               cp_lexer_consume_token (parser->lexer);
4067             return error_mark_node;
4068           }
4069         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4070           {
4071             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4072               error_at (token->location,
4073                         "scope %qT before %<~%> is not a class-name",
4074                         scope);
4075             cp_parser_simulate_error (parser);
4076             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4077               cp_lexer_consume_token (parser->lexer);
4078             return error_mark_node;
4079           }
4080         gcc_assert (!scope || TYPE_P (scope));
4081
4082         /* If the name is of the form "X::~X" it's OK even if X is a
4083            typedef.  */
4084         token = cp_lexer_peek_token (parser->lexer);
4085         if (scope
4086             && token->type == CPP_NAME
4087             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4088                 != CPP_LESS)
4089             && (token->u.value == TYPE_IDENTIFIER (scope)
4090                 || (CLASS_TYPE_P (scope)
4091                     && constructor_name_p (token->u.value, scope))))
4092           {
4093             cp_lexer_consume_token (parser->lexer);
4094             return build_nt (BIT_NOT_EXPR, scope);
4095           }
4096
4097         /* If there was an explicit qualification (S::~T), first look
4098            in the scope given by the qualification (i.e., S).
4099
4100            Note: in the calls to cp_parser_class_name below we pass
4101            typename_type so that lookup finds the injected-class-name
4102            rather than the constructor.  */
4103         done = false;
4104         type_decl = NULL_TREE;
4105         if (scope)
4106           {
4107             cp_parser_parse_tentatively (parser);
4108             type_decl = cp_parser_class_name (parser,
4109                                               /*typename_keyword_p=*/false,
4110                                               /*template_keyword_p=*/false,
4111                                               typename_type,
4112                                               /*check_dependency=*/false,
4113                                               /*class_head_p=*/false,
4114                                               declarator_p);
4115             if (cp_parser_parse_definitely (parser))
4116               done = true;
4117           }
4118         /* In "N::S::~S", look in "N" as well.  */
4119         if (!done && scope && qualifying_scope)
4120           {
4121             cp_parser_parse_tentatively (parser);
4122             parser->scope = qualifying_scope;
4123             parser->object_scope = NULL_TREE;
4124             parser->qualifying_scope = NULL_TREE;
4125             type_decl
4126               = cp_parser_class_name (parser,
4127                                       /*typename_keyword_p=*/false,
4128                                       /*template_keyword_p=*/false,
4129                                       typename_type,
4130                                       /*check_dependency=*/false,
4131                                       /*class_head_p=*/false,
4132                                       declarator_p);
4133             if (cp_parser_parse_definitely (parser))
4134               done = true;
4135           }
4136         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4137         else if (!done && object_scope)
4138           {
4139             cp_parser_parse_tentatively (parser);
4140             parser->scope = object_scope;
4141             parser->object_scope = NULL_TREE;
4142             parser->qualifying_scope = NULL_TREE;
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 (cp_parser_parse_definitely (parser))
4152               done = true;
4153           }
4154         /* Look in the surrounding context.  */
4155         if (!done)
4156           {
4157             parser->scope = NULL_TREE;
4158             parser->object_scope = NULL_TREE;
4159             parser->qualifying_scope = NULL_TREE;
4160             if (processing_template_decl)
4161               cp_parser_parse_tentatively (parser);
4162             type_decl
4163               = cp_parser_class_name (parser,
4164                                       /*typename_keyword_p=*/false,
4165                                       /*template_keyword_p=*/false,
4166                                       typename_type,
4167                                       /*check_dependency=*/false,
4168                                       /*class_head_p=*/false,
4169                                       declarator_p);
4170             if (processing_template_decl
4171                 && ! cp_parser_parse_definitely (parser))
4172               {
4173                 /* We couldn't find a type with this name, so just accept
4174                    it and check for a match at instantiation time.  */
4175                 type_decl = cp_parser_identifier (parser);
4176                 if (type_decl != error_mark_node)
4177                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4178                 return type_decl;
4179               }
4180           }
4181         /* If an error occurred, assume that the name of the
4182            destructor is the same as the name of the qualifying
4183            class.  That allows us to keep parsing after running
4184            into ill-formed destructor names.  */
4185         if (type_decl == error_mark_node && scope)
4186           return build_nt (BIT_NOT_EXPR, scope);
4187         else if (type_decl == error_mark_node)
4188           return error_mark_node;
4189
4190         /* Check that destructor name and scope match.  */
4191         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4192           {
4193             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4194               error_at (token->location,
4195                         "declaration of %<~%T%> as member of %qT",
4196                         type_decl, scope);
4197             cp_parser_simulate_error (parser);
4198             return error_mark_node;
4199           }
4200
4201         /* [class.dtor]
4202
4203            A typedef-name that names a class shall not be used as the
4204            identifier in the declarator for a destructor declaration.  */
4205         if (declarator_p
4206             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4207             && !DECL_SELF_REFERENCE_P (type_decl)
4208             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4209           error_at (token->location,
4210                     "typedef-name %qD used as destructor declarator",
4211                     type_decl);
4212
4213         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4214       }
4215
4216     case CPP_KEYWORD:
4217       if (token->keyword == RID_OPERATOR)
4218         {
4219           tree id;
4220
4221           /* This could be a template-id, so we try that first.  */
4222           cp_parser_parse_tentatively (parser);
4223           /* Try a template-id.  */
4224           id = cp_parser_template_id (parser, template_keyword_p,
4225                                       /*check_dependency_p=*/true,
4226                                       declarator_p);
4227           /* If that worked, we're done.  */
4228           if (cp_parser_parse_definitely (parser))
4229             return id;
4230           /* We still don't know whether we're looking at an
4231              operator-function-id or a conversion-function-id.  */
4232           cp_parser_parse_tentatively (parser);
4233           /* Try an operator-function-id.  */
4234           id = cp_parser_operator_function_id (parser);
4235           /* If that didn't work, try a conversion-function-id.  */
4236           if (!cp_parser_parse_definitely (parser))
4237             id = cp_parser_conversion_function_id (parser);
4238
4239           return id;
4240         }
4241       /* Fall through.  */
4242
4243     default:
4244       if (optional_p)
4245         return NULL_TREE;
4246       cp_parser_error (parser, "expected unqualified-id");
4247       return error_mark_node;
4248     }
4249 }
4250
4251 /* Parse an (optional) nested-name-specifier.
4252
4253    nested-name-specifier: [C++98]
4254      class-or-namespace-name :: nested-name-specifier [opt]
4255      class-or-namespace-name :: template nested-name-specifier [opt]
4256
4257    nested-name-specifier: [C++0x]
4258      type-name ::
4259      namespace-name ::
4260      nested-name-specifier identifier ::
4261      nested-name-specifier template [opt] simple-template-id ::
4262
4263    PARSER->SCOPE should be set appropriately before this function is
4264    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4265    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4266    in name lookups.
4267
4268    Sets PARSER->SCOPE to the class (TYPE) or namespace
4269    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4270    it unchanged if there is no nested-name-specifier.  Returns the new
4271    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4272
4273    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4274    part of a declaration and/or decl-specifier.  */
4275
4276 static tree
4277 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4278                                      bool typename_keyword_p,
4279                                      bool check_dependency_p,
4280                                      bool type_p,
4281                                      bool is_declaration)
4282 {
4283   bool success = false;
4284   cp_token_position start = 0;
4285   cp_token *token;
4286
4287   /* Remember where the nested-name-specifier starts.  */
4288   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4289     {
4290       start = cp_lexer_token_position (parser->lexer, false);
4291       push_deferring_access_checks (dk_deferred);
4292     }
4293
4294   while (true)
4295     {
4296       tree new_scope;
4297       tree old_scope;
4298       tree saved_qualifying_scope;
4299       bool template_keyword_p;
4300
4301       /* Spot cases that cannot be the beginning of a
4302          nested-name-specifier.  */
4303       token = cp_lexer_peek_token (parser->lexer);
4304
4305       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4306          the already parsed nested-name-specifier.  */
4307       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4308         {
4309           /* Grab the nested-name-specifier and continue the loop.  */
4310           cp_parser_pre_parsed_nested_name_specifier (parser);
4311           /* If we originally encountered this nested-name-specifier
4312              with IS_DECLARATION set to false, we will not have
4313              resolved TYPENAME_TYPEs, so we must do so here.  */
4314           if (is_declaration
4315               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4316             {
4317               new_scope = resolve_typename_type (parser->scope,
4318                                                  /*only_current_p=*/false);
4319               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4320                 parser->scope = new_scope;
4321             }
4322           success = true;
4323           continue;
4324         }
4325
4326       /* Spot cases that cannot be the beginning of a
4327          nested-name-specifier.  On the second and subsequent times
4328          through the loop, we look for the `template' keyword.  */
4329       if (success && token->keyword == RID_TEMPLATE)
4330         ;
4331       /* A template-id can start a nested-name-specifier.  */
4332       else if (token->type == CPP_TEMPLATE_ID)
4333         ;
4334       /* DR 743: decltype can be used in a nested-name-specifier.  */
4335       else if (token_is_decltype (token))
4336         ;
4337       else
4338         {
4339           /* If the next token is not an identifier, then it is
4340              definitely not a type-name or namespace-name.  */
4341           if (token->type != CPP_NAME)
4342             break;
4343           /* If the following token is neither a `<' (to begin a
4344              template-id), nor a `::', then we are not looking at a
4345              nested-name-specifier.  */
4346           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4347
4348           if (token->type == CPP_COLON
4349               && parser->colon_corrects_to_scope_p
4350               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4351             {
4352               error_at (token->location,
4353                         "found %<:%> in nested-name-specifier, expected %<::%>");
4354               token->type = CPP_SCOPE;
4355             }
4356
4357           if (token->type != CPP_SCOPE
4358               && !cp_parser_nth_token_starts_template_argument_list_p
4359                   (parser, 2))
4360             break;
4361         }
4362
4363       /* The nested-name-specifier is optional, so we parse
4364          tentatively.  */
4365       cp_parser_parse_tentatively (parser);
4366
4367       /* Look for the optional `template' keyword, if this isn't the
4368          first time through the loop.  */
4369       if (success)
4370         template_keyword_p = cp_parser_optional_template_keyword (parser);
4371       else
4372         template_keyword_p = false;
4373
4374       /* Save the old scope since the name lookup we are about to do
4375          might destroy it.  */
4376       old_scope = parser->scope;
4377       saved_qualifying_scope = parser->qualifying_scope;
4378       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4379          look up names in "X<T>::I" in order to determine that "Y" is
4380          a template.  So, if we have a typename at this point, we make
4381          an effort to look through it.  */
4382       if (is_declaration
4383           && !typename_keyword_p
4384           && parser->scope
4385           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4386         parser->scope = resolve_typename_type (parser->scope,
4387                                                /*only_current_p=*/false);
4388       /* Parse the qualifying entity.  */
4389       new_scope
4390         = cp_parser_qualifying_entity (parser,
4391                                        typename_keyword_p,
4392                                        template_keyword_p,
4393                                        check_dependency_p,
4394                                        type_p,
4395                                        is_declaration);
4396       /* Look for the `::' token.  */
4397       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4398
4399       /* If we found what we wanted, we keep going; otherwise, we're
4400          done.  */
4401       if (!cp_parser_parse_definitely (parser))
4402         {
4403           bool error_p = false;
4404
4405           /* Restore the OLD_SCOPE since it was valid before the
4406              failed attempt at finding the last
4407              class-or-namespace-name.  */
4408           parser->scope = old_scope;
4409           parser->qualifying_scope = saved_qualifying_scope;
4410
4411           /* If the next token is a decltype, and the one after that is a
4412              `::', then the decltype has failed to resolve to a class or
4413              enumeration type.  Give this error even when parsing
4414              tentatively since it can't possibly be valid--and we're going
4415              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4416              won't get another chance.*/
4417           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4418               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4419                   == CPP_SCOPE))
4420             {
4421               token = cp_lexer_consume_token (parser->lexer);
4422               error_at (token->location, "decltype evaluates to %qT, "
4423                         "which is not a class or enumeration type",
4424                         token->u.value);
4425               parser->scope = error_mark_node;
4426               error_p = true;
4427               /* As below.  */
4428               success = true;
4429               cp_lexer_consume_token (parser->lexer);
4430             }
4431
4432           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4433             break;
4434           /* If the next token is an identifier, and the one after
4435              that is a `::', then any valid interpretation would have
4436              found a class-or-namespace-name.  */
4437           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4438                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4439                      == CPP_SCOPE)
4440                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4441                      != CPP_COMPL))
4442             {
4443               token = cp_lexer_consume_token (parser->lexer);
4444               if (!error_p)
4445                 {
4446                   if (!token->ambiguous_p)
4447                     {
4448                       tree decl;
4449                       tree ambiguous_decls;
4450
4451                       decl = cp_parser_lookup_name (parser, token->u.value,
4452                                                     none_type,
4453                                                     /*is_template=*/false,
4454                                                     /*is_namespace=*/false,
4455                                                     /*check_dependency=*/true,
4456                                                     &ambiguous_decls,
4457                                                     token->location);
4458                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4459                         error_at (token->location,
4460                                   "%qD used without template parameters",
4461                                   decl);
4462                       else if (ambiguous_decls)
4463                         {
4464                           error_at (token->location,
4465                                     "reference to %qD is ambiguous",
4466                                     token->u.value);
4467                           print_candidates (ambiguous_decls);
4468                           decl = error_mark_node;
4469                         }
4470                       else
4471                         {
4472                           if (cxx_dialect != cxx98)
4473                             cp_parser_name_lookup_error
4474                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4475                              token->location);
4476                           else
4477                             cp_parser_name_lookup_error
4478                             (parser, token->u.value, decl, NLE_CXX98,
4479                              token->location);
4480                         }
4481                     }
4482                   parser->scope = error_mark_node;
4483                   error_p = true;
4484                   /* Treat this as a successful nested-name-specifier
4485                      due to:
4486
4487                      [basic.lookup.qual]
4488
4489                      If the name found is not a class-name (clause
4490                      _class_) or namespace-name (_namespace.def_), the
4491                      program is ill-formed.  */
4492                   success = true;
4493                 }
4494               cp_lexer_consume_token (parser->lexer);
4495             }
4496           break;
4497         }
4498       /* We've found one valid nested-name-specifier.  */
4499       success = true;
4500       /* Name lookup always gives us a DECL.  */
4501       if (TREE_CODE (new_scope) == TYPE_DECL)
4502         new_scope = TREE_TYPE (new_scope);
4503       /* Uses of "template" must be followed by actual templates.  */
4504       if (template_keyword_p
4505           && !(CLASS_TYPE_P (new_scope)
4506                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4507                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4508                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4509           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4510                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4511                    == TEMPLATE_ID_EXPR)))
4512         permerror (input_location, TYPE_P (new_scope)
4513                    ? "%qT is not a template"
4514                    : "%qD is not a template",
4515                    new_scope);
4516       /* If it is a class scope, try to complete it; we are about to
4517          be looking up names inside the class.  */
4518       if (TYPE_P (new_scope)
4519           /* Since checking types for dependency can be expensive,
4520              avoid doing it if the type is already complete.  */
4521           && !COMPLETE_TYPE_P (new_scope)
4522           /* Do not try to complete dependent types.  */
4523           && !dependent_type_p (new_scope))
4524         {
4525           new_scope = complete_type (new_scope);
4526           /* If it is a typedef to current class, use the current
4527              class instead, as the typedef won't have any names inside
4528              it yet.  */
4529           if (!COMPLETE_TYPE_P (new_scope)
4530               && currently_open_class (new_scope))
4531             new_scope = TYPE_MAIN_VARIANT (new_scope);
4532         }
4533       /* Make sure we look in the right scope the next time through
4534          the loop.  */
4535       parser->scope = new_scope;
4536     }
4537
4538   /* If parsing tentatively, replace the sequence of tokens that makes
4539      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4540      token.  That way, should we re-parse the token stream, we will
4541      not have to repeat the effort required to do the parse, nor will
4542      we issue duplicate error messages.  */
4543   if (success && start)
4544     {
4545       cp_token *token;
4546
4547       token = cp_lexer_token_at (parser->lexer, start);
4548       /* Reset the contents of the START token.  */
4549       token->type = CPP_NESTED_NAME_SPECIFIER;
4550       /* Retrieve any deferred checks.  Do not pop this access checks yet
4551          so the memory will not be reclaimed during token replacing below.  */
4552       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4553       token->u.tree_check_value->value = parser->scope;
4554       token->u.tree_check_value->checks = get_deferred_access_checks ();
4555       token->u.tree_check_value->qualifying_scope =
4556         parser->qualifying_scope;
4557       token->keyword = RID_MAX;
4558
4559       /* Purge all subsequent tokens.  */
4560       cp_lexer_purge_tokens_after (parser->lexer, start);
4561     }
4562
4563   if (start)
4564     pop_to_parent_deferring_access_checks ();
4565
4566   return success ? parser->scope : NULL_TREE;
4567 }
4568
4569 /* Parse a nested-name-specifier.  See
4570    cp_parser_nested_name_specifier_opt for details.  This function
4571    behaves identically, except that it will an issue an error if no
4572    nested-name-specifier is present.  */
4573
4574 static tree
4575 cp_parser_nested_name_specifier (cp_parser *parser,
4576                                  bool typename_keyword_p,
4577                                  bool check_dependency_p,
4578                                  bool type_p,
4579                                  bool is_declaration)
4580 {
4581   tree scope;
4582
4583   /* Look for the nested-name-specifier.  */
4584   scope = cp_parser_nested_name_specifier_opt (parser,
4585                                                typename_keyword_p,
4586                                                check_dependency_p,
4587                                                type_p,
4588                                                is_declaration);
4589   /* If it was not present, issue an error message.  */
4590   if (!scope)
4591     {
4592       cp_parser_error (parser, "expected nested-name-specifier");
4593       parser->scope = NULL_TREE;
4594     }
4595
4596   return scope;
4597 }
4598
4599 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4600    this is either a class-name or a namespace-name (which corresponds
4601    to the class-or-namespace-name production in the grammar). For
4602    C++0x, it can also be a type-name that refers to an enumeration
4603    type.
4604
4605    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4606    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4607    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4608    TYPE_P is TRUE iff the next name should be taken as a class-name,
4609    even the same name is declared to be another entity in the same
4610    scope.
4611
4612    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4613    specified by the class-or-namespace-name.  If neither is found the
4614    ERROR_MARK_NODE is returned.  */
4615
4616 static tree
4617 cp_parser_qualifying_entity (cp_parser *parser,
4618                              bool typename_keyword_p,
4619                              bool template_keyword_p,
4620                              bool check_dependency_p,
4621                              bool type_p,
4622                              bool is_declaration)
4623 {
4624   tree saved_scope;
4625   tree saved_qualifying_scope;
4626   tree saved_object_scope;
4627   tree scope;
4628   bool only_class_p;
4629   bool successful_parse_p;
4630
4631   /* DR 743: decltype can appear in a nested-name-specifier.  */
4632   if (cp_lexer_next_token_is_decltype (parser->lexer))
4633     {
4634       scope = cp_parser_decltype (parser);
4635       if (TREE_CODE (scope) != ENUMERAL_TYPE
4636           && !MAYBE_CLASS_TYPE_P (scope))
4637         {
4638           cp_parser_simulate_error (parser);
4639           return error_mark_node;
4640         }
4641       if (TYPE_NAME (scope))
4642         scope = TYPE_NAME (scope);
4643       return scope;
4644     }
4645
4646   /* Before we try to parse the class-name, we must save away the
4647      current PARSER->SCOPE since cp_parser_class_name will destroy
4648      it.  */
4649   saved_scope = parser->scope;
4650   saved_qualifying_scope = parser->qualifying_scope;
4651   saved_object_scope = parser->object_scope;
4652   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4653      there is no need to look for a namespace-name.  */
4654   only_class_p = template_keyword_p 
4655     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4656   if (!only_class_p)
4657     cp_parser_parse_tentatively (parser);
4658   scope = cp_parser_class_name (parser,
4659                                 typename_keyword_p,
4660                                 template_keyword_p,
4661                                 type_p ? class_type : none_type,
4662                                 check_dependency_p,
4663                                 /*class_head_p=*/false,
4664                                 is_declaration);
4665   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4666   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4667   if (!only_class_p 
4668       && cxx_dialect != cxx98
4669       && !successful_parse_p)
4670     {
4671       /* Restore the saved scope.  */
4672       parser->scope = saved_scope;
4673       parser->qualifying_scope = saved_qualifying_scope;
4674       parser->object_scope = saved_object_scope;
4675
4676       /* Parse tentatively.  */
4677       cp_parser_parse_tentatively (parser);
4678      
4679       /* Parse a typedef-name or enum-name.  */
4680       scope = cp_parser_nonclass_name (parser);
4681
4682       /* "If the name found does not designate a namespace or a class,
4683          enumeration, or dependent type, the program is ill-formed."
4684
4685          We cover classes and dependent types above and namespaces below,
4686          so this code is only looking for enums.  */
4687       if (!scope || TREE_CODE (scope) != TYPE_DECL
4688           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4689         cp_parser_simulate_error (parser);
4690
4691       successful_parse_p = cp_parser_parse_definitely (parser);
4692     }
4693   /* If that didn't work, try for a namespace-name.  */
4694   if (!only_class_p && !successful_parse_p)
4695     {
4696       /* Restore the saved scope.  */
4697       parser->scope = saved_scope;
4698       parser->qualifying_scope = saved_qualifying_scope;
4699       parser->object_scope = saved_object_scope;
4700       /* If we are not looking at an identifier followed by the scope
4701          resolution operator, then this is not part of a
4702          nested-name-specifier.  (Note that this function is only used
4703          to parse the components of a nested-name-specifier.)  */
4704       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4705           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4706         return error_mark_node;
4707       scope = cp_parser_namespace_name (parser);
4708     }
4709
4710   return scope;
4711 }
4712
4713 /* Parse a postfix-expression.
4714
4715    postfix-expression:
4716      primary-expression
4717      postfix-expression [ expression ]
4718      postfix-expression ( expression-list [opt] )
4719      simple-type-specifier ( expression-list [opt] )
4720      typename :: [opt] nested-name-specifier identifier
4721        ( expression-list [opt] )
4722      typename :: [opt] nested-name-specifier template [opt] template-id
4723        ( expression-list [opt] )
4724      postfix-expression . template [opt] id-expression
4725      postfix-expression -> template [opt] id-expression
4726      postfix-expression . pseudo-destructor-name
4727      postfix-expression -> pseudo-destructor-name
4728      postfix-expression ++
4729      postfix-expression --
4730      dynamic_cast < type-id > ( expression )
4731      static_cast < type-id > ( expression )
4732      reinterpret_cast < type-id > ( expression )
4733      const_cast < type-id > ( expression )
4734      typeid ( expression )
4735      typeid ( type-id )
4736
4737    GNU Extension:
4738
4739    postfix-expression:
4740      ( type-id ) { initializer-list , [opt] }
4741
4742    This extension is a GNU version of the C99 compound-literal
4743    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4744    but they are essentially the same concept.)
4745
4746    If ADDRESS_P is true, the postfix expression is the operand of the
4747    `&' operator.  CAST_P is true if this expression is the target of a
4748    cast.
4749
4750    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4751    class member access expressions [expr.ref].
4752
4753    Returns a representation of the expression.  */
4754
4755 static tree
4756 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4757                               bool member_access_only_p,
4758                               cp_id_kind * pidk_return)
4759 {
4760   cp_token *token;
4761   enum rid keyword;
4762   cp_id_kind idk = CP_ID_KIND_NONE;
4763   tree postfix_expression = NULL_TREE;
4764   bool is_member_access = false;
4765
4766   /* Peek at the next token.  */
4767   token = cp_lexer_peek_token (parser->lexer);
4768   /* Some of the productions are determined by keywords.  */
4769   keyword = token->keyword;
4770   switch (keyword)
4771     {
4772     case RID_DYNCAST:
4773     case RID_STATCAST:
4774     case RID_REINTCAST:
4775     case RID_CONSTCAST:
4776       {
4777         tree type;
4778         tree expression;
4779         const char *saved_message;
4780
4781         /* All of these can be handled in the same way from the point
4782            of view of parsing.  Begin by consuming the token
4783            identifying the cast.  */
4784         cp_lexer_consume_token (parser->lexer);
4785
4786         /* New types cannot be defined in the cast.  */
4787         saved_message = parser->type_definition_forbidden_message;
4788         parser->type_definition_forbidden_message
4789           = G_("types may not be defined in casts");
4790
4791         /* Look for the opening `<'.  */
4792         cp_parser_require (parser, CPP_LESS, RT_LESS);
4793         /* Parse the type to which we are casting.  */
4794         type = cp_parser_type_id (parser);
4795         /* Look for the closing `>'.  */
4796         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4797         /* Restore the old message.  */
4798         parser->type_definition_forbidden_message = saved_message;
4799
4800         /* And the expression which is being cast.  */
4801         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4802         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4803         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4804
4805         /* Only type conversions to integral or enumeration types
4806            can be used in constant-expressions.  */
4807         if (!cast_valid_in_integral_constant_expression_p (type)
4808             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4809           return error_mark_node;
4810
4811         switch (keyword)
4812           {
4813           case RID_DYNCAST:
4814             postfix_expression
4815               = build_dynamic_cast (type, expression, tf_warning_or_error);
4816             break;
4817           case RID_STATCAST:
4818             postfix_expression
4819               = build_static_cast (type, expression, tf_warning_or_error);
4820             break;
4821           case RID_REINTCAST:
4822             postfix_expression
4823               = build_reinterpret_cast (type, expression, 
4824                                         tf_warning_or_error);
4825             break;
4826           case RID_CONSTCAST:
4827             postfix_expression
4828               = build_const_cast (type, expression, tf_warning_or_error);
4829             break;
4830           default:
4831             gcc_unreachable ();
4832           }
4833       }
4834       break;
4835
4836     case RID_TYPEID:
4837       {
4838         tree type;
4839         const char *saved_message;
4840         bool saved_in_type_id_in_expr_p;
4841
4842         /* Consume the `typeid' token.  */
4843         cp_lexer_consume_token (parser->lexer);
4844         /* Look for the `(' token.  */
4845         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4846         /* Types cannot be defined in a `typeid' expression.  */
4847         saved_message = parser->type_definition_forbidden_message;
4848         parser->type_definition_forbidden_message
4849           = G_("types may not be defined in a %<typeid%> expression");
4850         /* We can't be sure yet whether we're looking at a type-id or an
4851            expression.  */
4852         cp_parser_parse_tentatively (parser);
4853         /* Try a type-id first.  */
4854         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4855         parser->in_type_id_in_expr_p = true;
4856         type = cp_parser_type_id (parser);
4857         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4858         /* Look for the `)' token.  Otherwise, we can't be sure that
4859            we're not looking at an expression: consider `typeid (int
4860            (3))', for example.  */
4861         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4862         /* If all went well, simply lookup the type-id.  */
4863         if (cp_parser_parse_definitely (parser))
4864           postfix_expression = get_typeid (type);
4865         /* Otherwise, fall back to the expression variant.  */
4866         else
4867           {
4868             tree expression;
4869
4870             /* Look for an expression.  */
4871             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4872             /* Compute its typeid.  */
4873             postfix_expression = build_typeid (expression);
4874             /* Look for the `)' token.  */
4875             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4876           }
4877         /* Restore the saved message.  */
4878         parser->type_definition_forbidden_message = saved_message;
4879         /* `typeid' may not appear in an integral constant expression.  */
4880         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4881           return error_mark_node;
4882       }
4883       break;
4884
4885     case RID_TYPENAME:
4886       {
4887         tree type;
4888         /* The syntax permitted here is the same permitted for an
4889            elaborated-type-specifier.  */
4890         type = cp_parser_elaborated_type_specifier (parser,
4891                                                     /*is_friend=*/false,
4892                                                     /*is_declaration=*/false);
4893         postfix_expression = cp_parser_functional_cast (parser, type);
4894       }
4895       break;
4896
4897     default:
4898       {
4899         tree type;
4900
4901         /* If the next thing is a simple-type-specifier, we may be
4902            looking at a functional cast.  We could also be looking at
4903            an id-expression.  So, we try the functional cast, and if
4904            that doesn't work we fall back to the primary-expression.  */
4905         cp_parser_parse_tentatively (parser);
4906         /* Look for the simple-type-specifier.  */
4907         type = cp_parser_simple_type_specifier (parser,
4908                                                 /*decl_specs=*/NULL,
4909                                                 CP_PARSER_FLAGS_NONE);
4910         /* Parse the cast itself.  */
4911         if (!cp_parser_error_occurred (parser))
4912           postfix_expression
4913             = cp_parser_functional_cast (parser, type);
4914         /* If that worked, we're done.  */
4915         if (cp_parser_parse_definitely (parser))
4916           break;
4917
4918         /* If the functional-cast didn't work out, try a
4919            compound-literal.  */
4920         if (cp_parser_allow_gnu_extensions_p (parser)
4921             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4922           {
4923             VEC(constructor_elt,gc) *initializer_list = NULL;
4924             bool saved_in_type_id_in_expr_p;
4925
4926             cp_parser_parse_tentatively (parser);
4927             /* Consume the `('.  */
4928             cp_lexer_consume_token (parser->lexer);
4929             /* Parse the type.  */
4930             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4931             parser->in_type_id_in_expr_p = true;
4932             type = cp_parser_type_id (parser);
4933             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4934             /* Look for the `)'.  */
4935             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4936             /* Look for the `{'.  */
4937             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4938             /* If things aren't going well, there's no need to
4939                keep going.  */
4940             if (!cp_parser_error_occurred (parser))
4941               {
4942                 bool non_constant_p;
4943                 /* Parse the initializer-list.  */
4944                 initializer_list
4945                   = cp_parser_initializer_list (parser, &non_constant_p);
4946                 /* Allow a trailing `,'.  */
4947                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4948                   cp_lexer_consume_token (parser->lexer);
4949                 /* Look for the final `}'.  */
4950                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4951               }
4952             /* If that worked, we're definitely looking at a
4953                compound-literal expression.  */
4954             if (cp_parser_parse_definitely (parser))
4955               {
4956                 /* Warn the user that a compound literal is not
4957                    allowed in standard C++.  */
4958                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4959                 /* For simplicity, we disallow compound literals in
4960                    constant-expressions.  We could
4961                    allow compound literals of integer type, whose
4962                    initializer was a constant, in constant
4963                    expressions.  Permitting that usage, as a further
4964                    extension, would not change the meaning of any
4965                    currently accepted programs.  (Of course, as
4966                    compound literals are not part of ISO C++, the
4967                    standard has nothing to say.)  */
4968                 if (cp_parser_non_integral_constant_expression (parser,
4969                                                                 NIC_NCC))
4970                   {
4971                     postfix_expression = error_mark_node;
4972                     break;
4973                   }
4974                 /* Form the representation of the compound-literal.  */
4975                 postfix_expression
4976                   = (finish_compound_literal
4977                      (type, build_constructor (init_list_type_node,
4978                                                initializer_list),
4979                       tf_warning_or_error));
4980                 break;
4981               }
4982           }
4983
4984         /* It must be a primary-expression.  */
4985         postfix_expression
4986           = cp_parser_primary_expression (parser, address_p, cast_p,
4987                                           /*template_arg_p=*/false,
4988                                           &idk);
4989       }
4990       break;
4991     }
4992
4993   /* Keep looping until the postfix-expression is complete.  */
4994   while (true)
4995     {
4996       if (idk == CP_ID_KIND_UNQUALIFIED
4997           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4998           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4999         /* It is not a Koenig lookup function call.  */
5000         postfix_expression
5001           = unqualified_name_lookup_error (postfix_expression);
5002
5003       /* Peek at the next token.  */
5004       token = cp_lexer_peek_token (parser->lexer);
5005
5006       switch (token->type)
5007         {
5008         case CPP_OPEN_SQUARE:
5009           postfix_expression
5010             = cp_parser_postfix_open_square_expression (parser,
5011                                                         postfix_expression,
5012                                                         false);
5013           idk = CP_ID_KIND_NONE;
5014           is_member_access = false;
5015           break;
5016
5017         case CPP_OPEN_PAREN:
5018           /* postfix-expression ( expression-list [opt] ) */
5019           {
5020             bool koenig_p;
5021             bool is_builtin_constant_p;
5022             bool saved_integral_constant_expression_p = false;
5023             bool saved_non_integral_constant_expression_p = false;
5024             VEC(tree,gc) *args;
5025
5026             is_member_access = false;
5027
5028             is_builtin_constant_p
5029               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5030             if (is_builtin_constant_p)
5031               {
5032                 /* The whole point of __builtin_constant_p is to allow
5033                    non-constant expressions to appear as arguments.  */
5034                 saved_integral_constant_expression_p
5035                   = parser->integral_constant_expression_p;
5036                 saved_non_integral_constant_expression_p
5037                   = parser->non_integral_constant_expression_p;
5038                 parser->integral_constant_expression_p = false;
5039               }
5040             args = (cp_parser_parenthesized_expression_list
5041                     (parser, non_attr,
5042                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5043                      /*non_constant_p=*/NULL));
5044             if (is_builtin_constant_p)
5045               {
5046                 parser->integral_constant_expression_p
5047                   = saved_integral_constant_expression_p;
5048                 parser->non_integral_constant_expression_p
5049                   = saved_non_integral_constant_expression_p;
5050               }
5051
5052             if (args == NULL)
5053               {
5054                 postfix_expression = error_mark_node;
5055                 break;
5056               }
5057
5058             /* Function calls are not permitted in
5059                constant-expressions.  */
5060             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5061                 && cp_parser_non_integral_constant_expression (parser,
5062                                                                NIC_FUNC_CALL))
5063               {
5064                 postfix_expression = error_mark_node;
5065                 release_tree_vector (args);
5066                 break;
5067               }
5068
5069             koenig_p = false;
5070             if (idk == CP_ID_KIND_UNQUALIFIED
5071                 || idk == CP_ID_KIND_TEMPLATE_ID)
5072               {
5073                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5074                   {
5075                     if (!VEC_empty (tree, args))
5076                       {
5077                         koenig_p = true;
5078                         if (!any_type_dependent_arguments_p (args))
5079                           postfix_expression
5080                             = perform_koenig_lookup (postfix_expression, args,
5081                                                      /*include_std=*/false,
5082                                                      tf_warning_or_error);
5083                       }
5084                     else
5085                       postfix_expression
5086                         = unqualified_fn_lookup_error (postfix_expression);
5087                   }
5088                 /* We do not perform argument-dependent lookup if
5089                    normal lookup finds a non-function, in accordance
5090                    with the expected resolution of DR 218.  */
5091                 else if (!VEC_empty (tree, args)
5092                          && is_overloaded_fn (postfix_expression))
5093                   {
5094                     tree fn = get_first_fn (postfix_expression);
5095                     fn = STRIP_TEMPLATE (fn);
5096
5097                     /* Do not do argument dependent lookup if regular
5098                        lookup finds a member function or a block-scope
5099                        function declaration.  [basic.lookup.argdep]/3  */
5100                     if (!DECL_FUNCTION_MEMBER_P (fn)
5101                         && !DECL_LOCAL_FUNCTION_P (fn))
5102                       {
5103                         koenig_p = true;
5104                         if (!any_type_dependent_arguments_p (args))
5105                           postfix_expression
5106                             = perform_koenig_lookup (postfix_expression, args,
5107                                                      /*include_std=*/false,
5108                                                      tf_warning_or_error);
5109                       }
5110                   }
5111               }
5112
5113             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5114               {
5115                 tree instance = TREE_OPERAND (postfix_expression, 0);
5116                 tree fn = TREE_OPERAND (postfix_expression, 1);
5117
5118                 if (processing_template_decl
5119                     && (type_dependent_expression_p (instance)
5120                         || (!BASELINK_P (fn)
5121                             && TREE_CODE (fn) != FIELD_DECL)
5122                         || type_dependent_expression_p (fn)
5123                         || any_type_dependent_arguments_p (args)))
5124                   {
5125                     postfix_expression
5126                       = build_nt_call_vec (postfix_expression, args);
5127                     release_tree_vector (args);
5128                     break;
5129                   }
5130
5131                 if (BASELINK_P (fn))
5132                   {
5133                   postfix_expression
5134                     = (build_new_method_call
5135                        (instance, fn, &args, NULL_TREE,
5136                         (idk == CP_ID_KIND_QUALIFIED
5137                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5138                          : LOOKUP_NORMAL),
5139                         /*fn_p=*/NULL,
5140                         tf_warning_or_error));
5141                   }
5142                 else
5143                   postfix_expression
5144                     = finish_call_expr (postfix_expression, &args,
5145                                         /*disallow_virtual=*/false,
5146                                         /*koenig_p=*/false,
5147                                         tf_warning_or_error);
5148               }
5149             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5150                      || TREE_CODE (postfix_expression) == MEMBER_REF
5151                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5152               postfix_expression = (build_offset_ref_call_from_tree
5153                                     (postfix_expression, &args));
5154             else if (idk == CP_ID_KIND_QUALIFIED)
5155               /* A call to a static class member, or a namespace-scope
5156                  function.  */
5157               postfix_expression
5158                 = finish_call_expr (postfix_expression, &args,
5159                                     /*disallow_virtual=*/true,
5160                                     koenig_p,
5161                                     tf_warning_or_error);
5162             else
5163               /* All other function calls.  */
5164               postfix_expression
5165                 = finish_call_expr (postfix_expression, &args,
5166                                     /*disallow_virtual=*/false,
5167                                     koenig_p,
5168                                     tf_warning_or_error);
5169
5170             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5171             idk = CP_ID_KIND_NONE;
5172
5173             release_tree_vector (args);
5174           }
5175           break;
5176
5177         case CPP_DOT:
5178         case CPP_DEREF:
5179           /* postfix-expression . template [opt] id-expression
5180              postfix-expression . pseudo-destructor-name
5181              postfix-expression -> template [opt] id-expression
5182              postfix-expression -> pseudo-destructor-name */
5183
5184           /* Consume the `.' or `->' operator.  */
5185           cp_lexer_consume_token (parser->lexer);
5186
5187           postfix_expression
5188             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5189                                                       postfix_expression,
5190                                                       false, &idk,
5191                                                       token->location);
5192
5193           is_member_access = true;
5194           break;
5195
5196         case CPP_PLUS_PLUS:
5197           /* postfix-expression ++  */
5198           /* Consume the `++' token.  */
5199           cp_lexer_consume_token (parser->lexer);
5200           /* Generate a representation for the complete expression.  */
5201           postfix_expression
5202             = finish_increment_expr (postfix_expression,
5203                                      POSTINCREMENT_EXPR);
5204           /* Increments may not appear in constant-expressions.  */
5205           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5206             postfix_expression = error_mark_node;
5207           idk = CP_ID_KIND_NONE;
5208           is_member_access = false;
5209           break;
5210
5211         case CPP_MINUS_MINUS:
5212           /* postfix-expression -- */
5213           /* Consume the `--' token.  */
5214           cp_lexer_consume_token (parser->lexer);
5215           /* Generate a representation for the complete expression.  */
5216           postfix_expression
5217             = finish_increment_expr (postfix_expression,
5218                                      POSTDECREMENT_EXPR);
5219           /* Decrements may not appear in constant-expressions.  */
5220           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5221             postfix_expression = error_mark_node;
5222           idk = CP_ID_KIND_NONE;
5223           is_member_access = false;
5224           break;
5225
5226         default:
5227           if (pidk_return != NULL)
5228             * pidk_return = idk;
5229           if (member_access_only_p)
5230             return is_member_access? postfix_expression : error_mark_node;
5231           else
5232             return postfix_expression;
5233         }
5234     }
5235
5236   /* We should never get here.  */
5237   gcc_unreachable ();
5238   return error_mark_node;
5239 }
5240
5241 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5242    by cp_parser_builtin_offsetof.  We're looking for
5243
5244      postfix-expression [ expression ]
5245
5246    FOR_OFFSETOF is set if we're being called in that context, which
5247    changes how we deal with integer constant expressions.  */
5248
5249 static tree
5250 cp_parser_postfix_open_square_expression (cp_parser *parser,
5251                                           tree postfix_expression,
5252                                           bool for_offsetof)
5253 {
5254   tree index;
5255
5256   /* Consume the `[' token.  */
5257   cp_lexer_consume_token (parser->lexer);
5258
5259   /* Parse the index expression.  */
5260   /* ??? For offsetof, there is a question of what to allow here.  If
5261      offsetof is not being used in an integral constant expression context,
5262      then we *could* get the right answer by computing the value at runtime.
5263      If we are in an integral constant expression context, then we might
5264      could accept any constant expression; hard to say without analysis.
5265      Rather than open the barn door too wide right away, allow only integer
5266      constant expressions here.  */
5267   if (for_offsetof)
5268     index = cp_parser_constant_expression (parser, false, NULL);
5269   else
5270     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5271
5272   /* Look for the closing `]'.  */
5273   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5274
5275   /* Build the ARRAY_REF.  */
5276   postfix_expression = grok_array_decl (postfix_expression, index);
5277
5278   /* When not doing offsetof, array references are not permitted in
5279      constant-expressions.  */
5280   if (!for_offsetof
5281       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5282     postfix_expression = error_mark_node;
5283
5284   return postfix_expression;
5285 }
5286
5287 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5288    by cp_parser_builtin_offsetof.  We're looking for
5289
5290      postfix-expression . template [opt] id-expression
5291      postfix-expression . pseudo-destructor-name
5292      postfix-expression -> template [opt] id-expression
5293      postfix-expression -> pseudo-destructor-name
5294
5295    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5296    limits what of the above we'll actually accept, but nevermind.
5297    TOKEN_TYPE is the "." or "->" token, which will already have been
5298    removed from the stream.  */
5299
5300 static tree
5301 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5302                                         enum cpp_ttype token_type,
5303                                         tree postfix_expression,
5304                                         bool for_offsetof, cp_id_kind *idk,
5305                                         location_t location)
5306 {
5307   tree name;
5308   bool dependent_p;
5309   bool pseudo_destructor_p;
5310   tree scope = NULL_TREE;
5311
5312   /* If this is a `->' operator, dereference the pointer.  */
5313   if (token_type == CPP_DEREF)
5314     postfix_expression = build_x_arrow (postfix_expression);
5315   /* Check to see whether or not the expression is type-dependent.  */
5316   dependent_p = type_dependent_expression_p (postfix_expression);
5317   /* The identifier following the `->' or `.' is not qualified.  */
5318   parser->scope = NULL_TREE;
5319   parser->qualifying_scope = NULL_TREE;
5320   parser->object_scope = NULL_TREE;
5321   *idk = CP_ID_KIND_NONE;
5322
5323   /* Enter the scope corresponding to the type of the object
5324      given by the POSTFIX_EXPRESSION.  */
5325   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5326     {
5327       scope = TREE_TYPE (postfix_expression);
5328       /* According to the standard, no expression should ever have
5329          reference type.  Unfortunately, we do not currently match
5330          the standard in this respect in that our internal representation
5331          of an expression may have reference type even when the standard
5332          says it does not.  Therefore, we have to manually obtain the
5333          underlying type here.  */
5334       scope = non_reference (scope);
5335       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5336       if (scope == unknown_type_node)
5337         {
5338           error_at (location, "%qE does not have class type",
5339                     postfix_expression);
5340           scope = NULL_TREE;
5341         }
5342       /* Unlike the object expression in other contexts, *this is not
5343          required to be of complete type for purposes of class member
5344          access (5.2.5) outside the member function body.  */
5345       else if (scope != current_class_ref
5346                && !(processing_template_decl && scope == current_class_type))
5347         scope = complete_type_or_else (scope, NULL_TREE);
5348       /* Let the name lookup machinery know that we are processing a
5349          class member access expression.  */
5350       parser->context->object_type = scope;
5351       /* If something went wrong, we want to be able to discern that case,
5352          as opposed to the case where there was no SCOPE due to the type
5353          of expression being dependent.  */
5354       if (!scope)
5355         scope = error_mark_node;
5356       /* If the SCOPE was erroneous, make the various semantic analysis
5357          functions exit quickly -- and without issuing additional error
5358          messages.  */
5359       if (scope == error_mark_node)
5360         postfix_expression = error_mark_node;
5361     }
5362
5363   /* Assume this expression is not a pseudo-destructor access.  */
5364   pseudo_destructor_p = false;
5365
5366   /* If the SCOPE is a scalar type, then, if this is a valid program,
5367      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5368      is type dependent, it can be pseudo-destructor-name or something else.
5369      Try to parse it as pseudo-destructor-name first.  */
5370   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5371     {
5372       tree s;
5373       tree type;
5374
5375       cp_parser_parse_tentatively (parser);
5376       /* Parse the pseudo-destructor-name.  */
5377       s = NULL_TREE;
5378       cp_parser_pseudo_destructor_name (parser, &s, &type);
5379       if (dependent_p
5380           && (cp_parser_error_occurred (parser)
5381               || TREE_CODE (type) != TYPE_DECL
5382               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5383         cp_parser_abort_tentative_parse (parser);
5384       else if (cp_parser_parse_definitely (parser))
5385         {
5386           pseudo_destructor_p = true;
5387           postfix_expression
5388             = finish_pseudo_destructor_expr (postfix_expression,
5389                                              s, TREE_TYPE (type));
5390         }
5391     }
5392
5393   if (!pseudo_destructor_p)
5394     {
5395       /* If the SCOPE is not a scalar type, we are looking at an
5396          ordinary class member access expression, rather than a
5397          pseudo-destructor-name.  */
5398       bool template_p;
5399       cp_token *token = cp_lexer_peek_token (parser->lexer);
5400       /* Parse the id-expression.  */
5401       name = (cp_parser_id_expression
5402               (parser,
5403                cp_parser_optional_template_keyword (parser),
5404                /*check_dependency_p=*/true,
5405                &template_p,
5406                /*declarator_p=*/false,
5407                /*optional_p=*/false));
5408       /* In general, build a SCOPE_REF if the member name is qualified.
5409          However, if the name was not dependent and has already been
5410          resolved; there is no need to build the SCOPE_REF.  For example;
5411
5412              struct X { void f(); };
5413              template <typename T> void f(T* t) { t->X::f(); }
5414
5415          Even though "t" is dependent, "X::f" is not and has been resolved
5416          to a BASELINK; there is no need to include scope information.  */
5417
5418       /* But we do need to remember that there was an explicit scope for
5419          virtual function calls.  */
5420       if (parser->scope)
5421         *idk = CP_ID_KIND_QUALIFIED;
5422
5423       /* If the name is a template-id that names a type, we will get a
5424          TYPE_DECL here.  That is invalid code.  */
5425       if (TREE_CODE (name) == TYPE_DECL)
5426         {
5427           error_at (token->location, "invalid use of %qD", name);
5428           postfix_expression = error_mark_node;
5429         }
5430       else
5431         {
5432           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5433             {
5434               name = build_qualified_name (/*type=*/NULL_TREE,
5435                                            parser->scope,
5436                                            name,
5437                                            template_p);
5438               parser->scope = NULL_TREE;
5439               parser->qualifying_scope = NULL_TREE;
5440               parser->object_scope = NULL_TREE;
5441             }
5442           if (scope && name && BASELINK_P (name))
5443             adjust_result_of_qualified_name_lookup
5444               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5445           postfix_expression
5446             = finish_class_member_access_expr (postfix_expression, name,
5447                                                template_p, 
5448                                                tf_warning_or_error);
5449         }
5450     }
5451
5452   /* We no longer need to look up names in the scope of the object on
5453      the left-hand side of the `.' or `->' operator.  */
5454   parser->context->object_type = NULL_TREE;
5455
5456   /* Outside of offsetof, these operators may not appear in
5457      constant-expressions.  */
5458   if (!for_offsetof
5459       && (cp_parser_non_integral_constant_expression
5460           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5461     postfix_expression = error_mark_node;
5462
5463   return postfix_expression;
5464 }
5465
5466 /* Parse a parenthesized expression-list.
5467
5468    expression-list:
5469      assignment-expression
5470      expression-list, assignment-expression
5471
5472    attribute-list:
5473      expression-list
5474      identifier
5475      identifier, expression-list
5476
5477    CAST_P is true if this expression is the target of a cast.
5478
5479    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5480    argument pack.
5481
5482    Returns a vector of trees.  Each element is a representation of an
5483    assignment-expression.  NULL is returned if the ( and or ) are
5484    missing.  An empty, but allocated, vector is returned on no
5485    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5486    if we are parsing an attribute list for an attribute that wants a
5487    plain identifier argument, normal_attr for an attribute that wants
5488    an expression, or non_attr if we aren't parsing an attribute list.  If
5489    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5490    not all of the expressions in the list were constant.  */
5491
5492 static VEC(tree,gc) *
5493 cp_parser_parenthesized_expression_list (cp_parser* parser,
5494                                          int is_attribute_list,
5495                                          bool cast_p,
5496                                          bool allow_expansion_p,
5497                                          bool *non_constant_p)
5498 {
5499   VEC(tree,gc) *expression_list;
5500   bool fold_expr_p = is_attribute_list != non_attr;
5501   tree identifier = NULL_TREE;
5502   bool saved_greater_than_is_operator_p;
5503
5504   /* Assume all the expressions will be constant.  */
5505   if (non_constant_p)
5506     *non_constant_p = false;
5507
5508   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5509     return NULL;
5510
5511   expression_list = make_tree_vector ();
5512
5513   /* Within a parenthesized expression, a `>' token is always
5514      the greater-than operator.  */
5515   saved_greater_than_is_operator_p
5516     = parser->greater_than_is_operator_p;
5517   parser->greater_than_is_operator_p = true;
5518
5519   /* Consume expressions until there are no more.  */
5520   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5521     while (true)
5522       {
5523         tree expr;
5524
5525         /* At the beginning of attribute lists, check to see if the
5526            next token is an identifier.  */
5527         if (is_attribute_list == id_attr
5528             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5529           {
5530             cp_token *token;
5531
5532             /* Consume the identifier.  */
5533             token = cp_lexer_consume_token (parser->lexer);
5534             /* Save the identifier.  */
5535             identifier = token->u.value;
5536           }
5537         else
5538           {
5539             bool expr_non_constant_p;
5540
5541             /* Parse the next assignment-expression.  */
5542             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5543               {
5544                 /* A braced-init-list.  */
5545                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5546                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5547                 if (non_constant_p && expr_non_constant_p)
5548                   *non_constant_p = true;
5549               }
5550             else if (non_constant_p)
5551               {
5552                 expr = (cp_parser_constant_expression
5553                         (parser, /*allow_non_constant_p=*/true,
5554                          &expr_non_constant_p));
5555                 if (expr_non_constant_p)
5556                   *non_constant_p = true;
5557               }
5558             else
5559               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5560
5561             if (fold_expr_p)
5562               expr = fold_non_dependent_expr (expr);
5563
5564             /* If we have an ellipsis, then this is an expression
5565                expansion.  */
5566             if (allow_expansion_p
5567                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5568               {
5569                 /* Consume the `...'.  */
5570                 cp_lexer_consume_token (parser->lexer);
5571
5572                 /* Build the argument pack.  */
5573                 expr = make_pack_expansion (expr);
5574               }
5575
5576              /* Add it to the list.  We add error_mark_node
5577                 expressions to the list, so that we can still tell if
5578                 the correct form for a parenthesized expression-list
5579                 is found. That gives better errors.  */
5580             VEC_safe_push (tree, gc, expression_list, expr);
5581
5582             if (expr == error_mark_node)
5583               goto skip_comma;
5584           }
5585
5586         /* After the first item, attribute lists look the same as
5587            expression lists.  */
5588         is_attribute_list = non_attr;
5589
5590       get_comma:;
5591         /* If the next token isn't a `,', then we are done.  */
5592         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5593           break;
5594
5595         /* Otherwise, consume the `,' and keep going.  */
5596         cp_lexer_consume_token (parser->lexer);
5597       }
5598
5599   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5600     {
5601       int ending;
5602
5603     skip_comma:;
5604       /* We try and resync to an unnested comma, as that will give the
5605          user better diagnostics.  */
5606       ending = cp_parser_skip_to_closing_parenthesis (parser,
5607                                                       /*recovering=*/true,
5608                                                       /*or_comma=*/true,
5609                                                       /*consume_paren=*/true);
5610       if (ending < 0)
5611         goto get_comma;
5612       if (!ending)
5613         {
5614           parser->greater_than_is_operator_p
5615             = saved_greater_than_is_operator_p;
5616           return NULL;
5617         }
5618     }
5619
5620   parser->greater_than_is_operator_p
5621     = saved_greater_than_is_operator_p;
5622
5623   if (identifier)
5624     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5625
5626   return expression_list;
5627 }
5628
5629 /* Parse a pseudo-destructor-name.
5630
5631    pseudo-destructor-name:
5632      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5633      :: [opt] nested-name-specifier template template-id :: ~ type-name
5634      :: [opt] nested-name-specifier [opt] ~ type-name
5635
5636    If either of the first two productions is used, sets *SCOPE to the
5637    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5638    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5639    or ERROR_MARK_NODE if the parse fails.  */
5640
5641 static void
5642 cp_parser_pseudo_destructor_name (cp_parser* parser,
5643                                   tree* scope,
5644                                   tree* type)
5645 {
5646   bool nested_name_specifier_p;
5647
5648   /* Assume that things will not work out.  */
5649   *type = error_mark_node;
5650
5651   /* Look for the optional `::' operator.  */
5652   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5653   /* Look for the optional nested-name-specifier.  */
5654   nested_name_specifier_p
5655     = (cp_parser_nested_name_specifier_opt (parser,
5656                                             /*typename_keyword_p=*/false,
5657                                             /*check_dependency_p=*/true,
5658                                             /*type_p=*/false,
5659                                             /*is_declaration=*/false)
5660        != NULL_TREE);
5661   /* Now, if we saw a nested-name-specifier, we might be doing the
5662      second production.  */
5663   if (nested_name_specifier_p
5664       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5665     {
5666       /* Consume the `template' keyword.  */
5667       cp_lexer_consume_token (parser->lexer);
5668       /* Parse the template-id.  */
5669       cp_parser_template_id (parser,
5670                              /*template_keyword_p=*/true,
5671                              /*check_dependency_p=*/false,
5672                              /*is_declaration=*/true);
5673       /* Look for the `::' token.  */
5674       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5675     }
5676   /* If the next token is not a `~', then there might be some
5677      additional qualification.  */
5678   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5679     {
5680       /* At this point, we're looking for "type-name :: ~".  The type-name
5681          must not be a class-name, since this is a pseudo-destructor.  So,
5682          it must be either an enum-name, or a typedef-name -- both of which
5683          are just identifiers.  So, we peek ahead to check that the "::"
5684          and "~" tokens are present; if they are not, then we can avoid
5685          calling type_name.  */
5686       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5687           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5688           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5689         {
5690           cp_parser_error (parser, "non-scalar type");
5691           return;
5692         }
5693
5694       /* Look for the type-name.  */
5695       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5696       if (*scope == error_mark_node)
5697         return;
5698
5699       /* Look for the `::' token.  */
5700       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5701     }
5702   else
5703     *scope = NULL_TREE;
5704
5705   /* Look for the `~'.  */
5706   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5707
5708   /* Once we see the ~, this has to be a pseudo-destructor.  */
5709   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5710     cp_parser_commit_to_tentative_parse (parser);
5711
5712   /* Look for the type-name again.  We are not responsible for
5713      checking that it matches the first type-name.  */
5714   *type = cp_parser_nonclass_name (parser);
5715 }
5716
5717 /* Parse a unary-expression.
5718
5719    unary-expression:
5720      postfix-expression
5721      ++ cast-expression
5722      -- cast-expression
5723      unary-operator cast-expression
5724      sizeof unary-expression
5725      sizeof ( type-id )
5726      alignof ( type-id )  [C++0x]
5727      new-expression
5728      delete-expression
5729
5730    GNU Extensions:
5731
5732    unary-expression:
5733      __extension__ cast-expression
5734      __alignof__ unary-expression
5735      __alignof__ ( type-id )
5736      alignof unary-expression  [C++0x]
5737      __real__ cast-expression
5738      __imag__ cast-expression
5739      && identifier
5740
5741    ADDRESS_P is true iff the unary-expression is appearing as the
5742    operand of the `&' operator.   CAST_P is true if this expression is
5743    the target of a cast.
5744
5745    Returns a representation of the expression.  */
5746
5747 static tree
5748 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5749                             cp_id_kind * pidk)
5750 {
5751   cp_token *token;
5752   enum tree_code unary_operator;
5753
5754   /* Peek at the next token.  */
5755   token = cp_lexer_peek_token (parser->lexer);
5756   /* Some keywords give away the kind of expression.  */
5757   if (token->type == CPP_KEYWORD)
5758     {
5759       enum rid keyword = token->keyword;
5760
5761       switch (keyword)
5762         {
5763         case RID_ALIGNOF:
5764         case RID_SIZEOF:
5765           {
5766             tree operand;
5767             enum tree_code op;
5768
5769             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5770             /* Consume the token.  */
5771             cp_lexer_consume_token (parser->lexer);
5772             /* Parse the operand.  */
5773             operand = cp_parser_sizeof_operand (parser, keyword);
5774
5775             if (TYPE_P (operand))
5776               return cxx_sizeof_or_alignof_type (operand, op, true);
5777             else
5778               {
5779                 /* ISO C++ defines alignof only with types, not with
5780                    expressions. So pedwarn if alignof is used with a non-
5781                    type expression. However, __alignof__ is ok.  */
5782                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5783                   pedwarn (token->location, OPT_pedantic,
5784                            "ISO C++ does not allow %<alignof%> "
5785                            "with a non-type");
5786
5787                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5788               }
5789           }
5790
5791         case RID_NEW:
5792           return cp_parser_new_expression (parser);
5793
5794         case RID_DELETE:
5795           return cp_parser_delete_expression (parser);
5796
5797         case RID_EXTENSION:
5798           {
5799             /* The saved value of the PEDANTIC flag.  */
5800             int saved_pedantic;
5801             tree expr;
5802
5803             /* Save away the PEDANTIC flag.  */
5804             cp_parser_extension_opt (parser, &saved_pedantic);
5805             /* Parse the cast-expression.  */
5806             expr = cp_parser_simple_cast_expression (parser);
5807             /* Restore the PEDANTIC flag.  */
5808             pedantic = saved_pedantic;
5809
5810             return expr;
5811           }
5812
5813         case RID_REALPART:
5814         case RID_IMAGPART:
5815           {
5816             tree expression;
5817
5818             /* Consume the `__real__' or `__imag__' token.  */
5819             cp_lexer_consume_token (parser->lexer);
5820             /* Parse the cast-expression.  */
5821             expression = cp_parser_simple_cast_expression (parser);
5822             /* Create the complete representation.  */
5823             return build_x_unary_op ((keyword == RID_REALPART
5824                                       ? REALPART_EXPR : IMAGPART_EXPR),
5825                                      expression,
5826                                      tf_warning_or_error);
5827           }
5828           break;
5829
5830         case RID_NOEXCEPT:
5831           {
5832             tree expr;
5833             const char *saved_message;
5834             bool saved_integral_constant_expression_p;
5835             bool saved_non_integral_constant_expression_p;
5836             bool saved_greater_than_is_operator_p;
5837
5838             cp_lexer_consume_token (parser->lexer);
5839             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5840
5841             saved_message = parser->type_definition_forbidden_message;
5842             parser->type_definition_forbidden_message
5843               = G_("types may not be defined in %<noexcept%> expressions");
5844
5845             saved_integral_constant_expression_p
5846               = parser->integral_constant_expression_p;
5847             saved_non_integral_constant_expression_p
5848               = parser->non_integral_constant_expression_p;
5849             parser->integral_constant_expression_p = false;
5850
5851             saved_greater_than_is_operator_p
5852               = parser->greater_than_is_operator_p;
5853             parser->greater_than_is_operator_p = true;
5854
5855             ++cp_unevaluated_operand;
5856             ++c_inhibit_evaluation_warnings;
5857             expr = cp_parser_expression (parser, false, NULL);
5858             --c_inhibit_evaluation_warnings;
5859             --cp_unevaluated_operand;
5860
5861             parser->greater_than_is_operator_p
5862               = saved_greater_than_is_operator_p;
5863
5864             parser->integral_constant_expression_p
5865               = saved_integral_constant_expression_p;
5866             parser->non_integral_constant_expression_p
5867               = saved_non_integral_constant_expression_p;
5868
5869             parser->type_definition_forbidden_message = saved_message;
5870
5871             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5872             return finish_noexcept_expr (expr, tf_warning_or_error);
5873           }
5874
5875         default:
5876           break;
5877         }
5878     }
5879
5880   /* Look for the `:: new' and `:: delete', which also signal the
5881      beginning of a new-expression, or delete-expression,
5882      respectively.  If the next token is `::', then it might be one of
5883      these.  */
5884   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5885     {
5886       enum rid keyword;
5887
5888       /* See if the token after the `::' is one of the keywords in
5889          which we're interested.  */
5890       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5891       /* If it's `new', we have a new-expression.  */
5892       if (keyword == RID_NEW)
5893         return cp_parser_new_expression (parser);
5894       /* Similarly, for `delete'.  */
5895       else if (keyword == RID_DELETE)
5896         return cp_parser_delete_expression (parser);
5897     }
5898
5899   /* Look for a unary operator.  */
5900   unary_operator = cp_parser_unary_operator (token);
5901   /* The `++' and `--' operators can be handled similarly, even though
5902      they are not technically unary-operators in the grammar.  */
5903   if (unary_operator == ERROR_MARK)
5904     {
5905       if (token->type == CPP_PLUS_PLUS)
5906         unary_operator = PREINCREMENT_EXPR;
5907       else if (token->type == CPP_MINUS_MINUS)
5908         unary_operator = PREDECREMENT_EXPR;
5909       /* Handle the GNU address-of-label extension.  */
5910       else if (cp_parser_allow_gnu_extensions_p (parser)
5911                && token->type == CPP_AND_AND)
5912         {
5913           tree identifier;
5914           tree expression;
5915           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5916
5917           /* Consume the '&&' token.  */
5918           cp_lexer_consume_token (parser->lexer);
5919           /* Look for the identifier.  */
5920           identifier = cp_parser_identifier (parser);
5921           /* Create an expression representing the address.  */
5922           expression = finish_label_address_expr (identifier, loc);
5923           if (cp_parser_non_integral_constant_expression (parser,
5924                                                           NIC_ADDR_LABEL))
5925             expression = error_mark_node;
5926           return expression;
5927         }
5928     }
5929   if (unary_operator != ERROR_MARK)
5930     {
5931       tree cast_expression;
5932       tree expression = error_mark_node;
5933       non_integral_constant non_constant_p = NIC_NONE;
5934
5935       /* Consume the operator token.  */
5936       token = cp_lexer_consume_token (parser->lexer);
5937       /* Parse the cast-expression.  */
5938       cast_expression
5939         = cp_parser_cast_expression (parser,
5940                                      unary_operator == ADDR_EXPR,
5941                                      /*cast_p=*/false, pidk);
5942       /* Now, build an appropriate representation.  */
5943       switch (unary_operator)
5944         {
5945         case INDIRECT_REF:
5946           non_constant_p = NIC_STAR;
5947           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5948                                              tf_warning_or_error);
5949           break;
5950
5951         case ADDR_EXPR:
5952            non_constant_p = NIC_ADDR;
5953           /* Fall through.  */
5954         case BIT_NOT_EXPR:
5955           expression = build_x_unary_op (unary_operator, cast_expression,
5956                                          tf_warning_or_error);
5957           break;
5958
5959         case PREINCREMENT_EXPR:
5960         case PREDECREMENT_EXPR:
5961           non_constant_p = unary_operator == PREINCREMENT_EXPR
5962                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5963           /* Fall through.  */
5964         case UNARY_PLUS_EXPR:
5965         case NEGATE_EXPR:
5966         case TRUTH_NOT_EXPR:
5967           expression = finish_unary_op_expr (unary_operator, cast_expression);
5968           break;
5969
5970         default:
5971           gcc_unreachable ();
5972         }
5973
5974       if (non_constant_p != NIC_NONE
5975           && cp_parser_non_integral_constant_expression (parser,
5976                                                          non_constant_p))
5977         expression = error_mark_node;
5978
5979       return expression;
5980     }
5981
5982   return cp_parser_postfix_expression (parser, address_p, cast_p,
5983                                        /*member_access_only_p=*/false,
5984                                        pidk);
5985 }
5986
5987 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5988    unary-operator, the corresponding tree code is returned.  */
5989
5990 static enum tree_code
5991 cp_parser_unary_operator (cp_token* token)
5992 {
5993   switch (token->type)
5994     {
5995     case CPP_MULT:
5996       return INDIRECT_REF;
5997
5998     case CPP_AND:
5999       return ADDR_EXPR;
6000
6001     case CPP_PLUS:
6002       return UNARY_PLUS_EXPR;
6003
6004     case CPP_MINUS:
6005       return NEGATE_EXPR;
6006
6007     case CPP_NOT:
6008       return TRUTH_NOT_EXPR;
6009
6010     case CPP_COMPL:
6011       return BIT_NOT_EXPR;
6012
6013     default:
6014       return ERROR_MARK;
6015     }
6016 }
6017
6018 /* Parse a new-expression.
6019
6020    new-expression:
6021      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6022      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6023
6024    Returns a representation of the expression.  */
6025
6026 static tree
6027 cp_parser_new_expression (cp_parser* parser)
6028 {
6029   bool global_scope_p;
6030   VEC(tree,gc) *placement;
6031   tree type;
6032   VEC(tree,gc) *initializer;
6033   tree nelts;
6034   tree ret;
6035
6036   /* Look for the optional `::' operator.  */
6037   global_scope_p
6038     = (cp_parser_global_scope_opt (parser,
6039                                    /*current_scope_valid_p=*/false)
6040        != NULL_TREE);
6041   /* Look for the `new' operator.  */
6042   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6043   /* There's no easy way to tell a new-placement from the
6044      `( type-id )' construct.  */
6045   cp_parser_parse_tentatively (parser);
6046   /* Look for a new-placement.  */
6047   placement = cp_parser_new_placement (parser);
6048   /* If that didn't work out, there's no new-placement.  */
6049   if (!cp_parser_parse_definitely (parser))
6050     {
6051       if (placement != NULL)
6052         release_tree_vector (placement);
6053       placement = NULL;
6054     }
6055
6056   /* If the next token is a `(', then we have a parenthesized
6057      type-id.  */
6058   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6059     {
6060       cp_token *token;
6061       /* Consume the `('.  */
6062       cp_lexer_consume_token (parser->lexer);
6063       /* Parse the type-id.  */
6064       type = cp_parser_type_id (parser);
6065       /* Look for the closing `)'.  */
6066       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6067       token = cp_lexer_peek_token (parser->lexer);
6068       /* There should not be a direct-new-declarator in this production,
6069          but GCC used to allowed this, so we check and emit a sensible error
6070          message for this case.  */
6071       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6072         {
6073           error_at (token->location,
6074                     "array bound forbidden after parenthesized type-id");
6075           inform (token->location, 
6076                   "try removing the parentheses around the type-id");
6077           cp_parser_direct_new_declarator (parser);
6078         }
6079       nelts = NULL_TREE;
6080     }
6081   /* Otherwise, there must be a new-type-id.  */
6082   else
6083     type = cp_parser_new_type_id (parser, &nelts);
6084
6085   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6086   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6087       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6088     initializer = cp_parser_new_initializer (parser);
6089   else
6090     initializer = NULL;
6091
6092   /* A new-expression may not appear in an integral constant
6093      expression.  */
6094   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6095     ret = error_mark_node;
6096   else
6097     {
6098       /* Create a representation of the new-expression.  */
6099       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6100                        tf_warning_or_error);
6101     }
6102
6103   if (placement != NULL)
6104     release_tree_vector (placement);
6105   if (initializer != NULL)
6106     release_tree_vector (initializer);
6107
6108   return ret;
6109 }
6110
6111 /* Parse a new-placement.
6112
6113    new-placement:
6114      ( expression-list )
6115
6116    Returns the same representation as for an expression-list.  */
6117
6118 static VEC(tree,gc) *
6119 cp_parser_new_placement (cp_parser* parser)
6120 {
6121   VEC(tree,gc) *expression_list;
6122
6123   /* Parse the expression-list.  */
6124   expression_list = (cp_parser_parenthesized_expression_list
6125                      (parser, non_attr, /*cast_p=*/false,
6126                       /*allow_expansion_p=*/true,
6127                       /*non_constant_p=*/NULL));
6128
6129   return expression_list;
6130 }
6131
6132 /* Parse a new-type-id.
6133
6134    new-type-id:
6135      type-specifier-seq new-declarator [opt]
6136
6137    Returns the TYPE allocated.  If the new-type-id indicates an array
6138    type, *NELTS is set to the number of elements in the last array
6139    bound; the TYPE will not include the last array bound.  */
6140
6141 static tree
6142 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6143 {
6144   cp_decl_specifier_seq type_specifier_seq;
6145   cp_declarator *new_declarator;
6146   cp_declarator *declarator;
6147   cp_declarator *outer_declarator;
6148   const char *saved_message;
6149   tree type;
6150
6151   /* The type-specifier sequence must not contain type definitions.
6152      (It cannot contain declarations of new types either, but if they
6153      are not definitions we will catch that because they are not
6154      complete.)  */
6155   saved_message = parser->type_definition_forbidden_message;
6156   parser->type_definition_forbidden_message
6157     = G_("types may not be defined in a new-type-id");
6158   /* Parse the type-specifier-seq.  */
6159   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6160                                 /*is_trailing_return=*/false,
6161                                 &type_specifier_seq);
6162   /* Restore the old message.  */
6163   parser->type_definition_forbidden_message = saved_message;
6164   /* Parse the new-declarator.  */
6165   new_declarator = cp_parser_new_declarator_opt (parser);
6166
6167   /* Determine the number of elements in the last array dimension, if
6168      any.  */
6169   *nelts = NULL_TREE;
6170   /* Skip down to the last array dimension.  */
6171   declarator = new_declarator;
6172   outer_declarator = NULL;
6173   while (declarator && (declarator->kind == cdk_pointer
6174                         || declarator->kind == cdk_ptrmem))
6175     {
6176       outer_declarator = declarator;
6177       declarator = declarator->declarator;
6178     }
6179   while (declarator
6180          && declarator->kind == cdk_array
6181          && declarator->declarator
6182          && declarator->declarator->kind == cdk_array)
6183     {
6184       outer_declarator = declarator;
6185       declarator = declarator->declarator;
6186     }
6187
6188   if (declarator && declarator->kind == cdk_array)
6189     {
6190       *nelts = declarator->u.array.bounds;
6191       if (*nelts == error_mark_node)
6192         *nelts = integer_one_node;
6193
6194       if (outer_declarator)
6195         outer_declarator->declarator = declarator->declarator;
6196       else
6197         new_declarator = NULL;
6198     }
6199
6200   type = groktypename (&type_specifier_seq, new_declarator, false);
6201   return type;
6202 }
6203
6204 /* Parse an (optional) new-declarator.
6205
6206    new-declarator:
6207      ptr-operator new-declarator [opt]
6208      direct-new-declarator
6209
6210    Returns the declarator.  */
6211
6212 static cp_declarator *
6213 cp_parser_new_declarator_opt (cp_parser* parser)
6214 {
6215   enum tree_code code;
6216   tree type;
6217   cp_cv_quals cv_quals;
6218
6219   /* We don't know if there's a ptr-operator next, or not.  */
6220   cp_parser_parse_tentatively (parser);
6221   /* Look for a ptr-operator.  */
6222   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6223   /* If that worked, look for more new-declarators.  */
6224   if (cp_parser_parse_definitely (parser))
6225     {
6226       cp_declarator *declarator;
6227
6228       /* Parse another optional declarator.  */
6229       declarator = cp_parser_new_declarator_opt (parser);
6230
6231       return cp_parser_make_indirect_declarator
6232         (code, type, cv_quals, declarator);
6233     }
6234
6235   /* If the next token is a `[', there is a direct-new-declarator.  */
6236   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6237     return cp_parser_direct_new_declarator (parser);
6238
6239   return NULL;
6240 }
6241
6242 /* Parse a direct-new-declarator.
6243
6244    direct-new-declarator:
6245      [ expression ]
6246      direct-new-declarator [constant-expression]
6247
6248    */
6249
6250 static cp_declarator *
6251 cp_parser_direct_new_declarator (cp_parser* parser)
6252 {
6253   cp_declarator *declarator = NULL;
6254
6255   while (true)
6256     {
6257       tree expression;
6258
6259       /* Look for the opening `['.  */
6260       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6261       /* The first expression is not required to be constant.  */
6262       if (!declarator)
6263         {
6264           cp_token *token = cp_lexer_peek_token (parser->lexer);
6265           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6266           /* The standard requires that the expression have integral
6267              type.  DR 74 adds enumeration types.  We believe that the
6268              real intent is that these expressions be handled like the
6269              expression in a `switch' condition, which also allows
6270              classes with a single conversion to integral or
6271              enumeration type.  */
6272           if (!processing_template_decl)
6273             {
6274               expression
6275                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6276                                               expression,
6277                                               /*complain=*/true);
6278               if (!expression)
6279                 {
6280                   error_at (token->location,
6281                             "expression in new-declarator must have integral "
6282                             "or enumeration type");
6283                   expression = error_mark_node;
6284                 }
6285             }
6286         }
6287       /* But all the other expressions must be.  */
6288       else
6289         expression
6290           = cp_parser_constant_expression (parser,
6291                                            /*allow_non_constant=*/false,
6292                                            NULL);
6293       /* Look for the closing `]'.  */
6294       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6295
6296       /* Add this bound to the declarator.  */
6297       declarator = make_array_declarator (declarator, expression);
6298
6299       /* If the next token is not a `[', then there are no more
6300          bounds.  */
6301       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6302         break;
6303     }
6304
6305   return declarator;
6306 }
6307
6308 /* Parse a new-initializer.
6309
6310    new-initializer:
6311      ( expression-list [opt] )
6312      braced-init-list
6313
6314    Returns a representation of the expression-list.  */
6315
6316 static VEC(tree,gc) *
6317 cp_parser_new_initializer (cp_parser* parser)
6318 {
6319   VEC(tree,gc) *expression_list;
6320
6321   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6322     {
6323       tree t;
6324       bool expr_non_constant_p;
6325       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6326       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6327       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6328       expression_list = make_tree_vector_single (t);
6329     }
6330   else
6331     expression_list = (cp_parser_parenthesized_expression_list
6332                        (parser, non_attr, /*cast_p=*/false,
6333                         /*allow_expansion_p=*/true,
6334                         /*non_constant_p=*/NULL));
6335
6336   return expression_list;
6337 }
6338
6339 /* Parse a delete-expression.
6340
6341    delete-expression:
6342      :: [opt] delete cast-expression
6343      :: [opt] delete [ ] cast-expression
6344
6345    Returns a representation of the expression.  */
6346
6347 static tree
6348 cp_parser_delete_expression (cp_parser* parser)
6349 {
6350   bool global_scope_p;
6351   bool array_p;
6352   tree expression;
6353
6354   /* Look for the optional `::' operator.  */
6355   global_scope_p
6356     = (cp_parser_global_scope_opt (parser,
6357                                    /*current_scope_valid_p=*/false)
6358        != NULL_TREE);
6359   /* Look for the `delete' keyword.  */
6360   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6361   /* See if the array syntax is in use.  */
6362   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6363     {
6364       /* Consume the `[' token.  */
6365       cp_lexer_consume_token (parser->lexer);
6366       /* Look for the `]' token.  */
6367       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6368       /* Remember that this is the `[]' construct.  */
6369       array_p = true;
6370     }
6371   else
6372     array_p = false;
6373
6374   /* Parse the cast-expression.  */
6375   expression = cp_parser_simple_cast_expression (parser);
6376
6377   /* A delete-expression may not appear in an integral constant
6378      expression.  */
6379   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6380     return error_mark_node;
6381
6382   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6383                         tf_warning_or_error);
6384 }
6385
6386 /* Returns true if TOKEN may start a cast-expression and false
6387    otherwise.  */
6388
6389 static bool
6390 cp_parser_token_starts_cast_expression (cp_token *token)
6391 {
6392   switch (token->type)
6393     {
6394     case CPP_COMMA:
6395     case CPP_SEMICOLON:
6396     case CPP_QUERY:
6397     case CPP_COLON:
6398     case CPP_CLOSE_SQUARE:
6399     case CPP_CLOSE_PAREN:
6400     case CPP_CLOSE_BRACE:
6401     case CPP_DOT:
6402     case CPP_DOT_STAR:
6403     case CPP_DEREF:
6404     case CPP_DEREF_STAR:
6405     case CPP_DIV:
6406     case CPP_MOD:
6407     case CPP_LSHIFT:
6408     case CPP_RSHIFT:
6409     case CPP_LESS:
6410     case CPP_GREATER:
6411     case CPP_LESS_EQ:
6412     case CPP_GREATER_EQ:
6413     case CPP_EQ_EQ:
6414     case CPP_NOT_EQ:
6415     case CPP_EQ:
6416     case CPP_MULT_EQ:
6417     case CPP_DIV_EQ:
6418     case CPP_MOD_EQ:
6419     case CPP_PLUS_EQ:
6420     case CPP_MINUS_EQ:
6421     case CPP_RSHIFT_EQ:
6422     case CPP_LSHIFT_EQ:
6423     case CPP_AND_EQ:
6424     case CPP_XOR_EQ:
6425     case CPP_OR_EQ:
6426     case CPP_XOR:
6427     case CPP_OR:
6428     case CPP_OR_OR:
6429     case CPP_EOF:
6430       return false;
6431
6432       /* '[' may start a primary-expression in obj-c++.  */
6433     case CPP_OPEN_SQUARE:
6434       return c_dialect_objc ();
6435
6436     default:
6437       return true;
6438     }
6439 }
6440
6441 /* Parse a cast-expression.
6442
6443    cast-expression:
6444      unary-expression
6445      ( type-id ) cast-expression
6446
6447    ADDRESS_P is true iff the unary-expression is appearing as the
6448    operand of the `&' operator.   CAST_P is true if this expression is
6449    the target of a cast.
6450
6451    Returns a representation of the expression.  */
6452
6453 static tree
6454 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6455                            cp_id_kind * pidk)
6456 {
6457   /* If it's a `(', then we might be looking at a cast.  */
6458   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6459     {
6460       tree type = NULL_TREE;
6461       tree expr = NULL_TREE;
6462       bool compound_literal_p;
6463       const char *saved_message;
6464
6465       /* There's no way to know yet whether or not this is a cast.
6466          For example, `(int (3))' is a unary-expression, while `(int)
6467          3' is a cast.  So, we resort to parsing tentatively.  */
6468       cp_parser_parse_tentatively (parser);
6469       /* Types may not be defined in a cast.  */
6470       saved_message = parser->type_definition_forbidden_message;
6471       parser->type_definition_forbidden_message
6472         = G_("types may not be defined in casts");
6473       /* Consume the `('.  */
6474       cp_lexer_consume_token (parser->lexer);
6475       /* A very tricky bit is that `(struct S) { 3 }' is a
6476          compound-literal (which we permit in C++ as an extension).
6477          But, that construct is not a cast-expression -- it is a
6478          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6479          is legal; if the compound-literal were a cast-expression,
6480          you'd need an extra set of parentheses.)  But, if we parse
6481          the type-id, and it happens to be a class-specifier, then we
6482          will commit to the parse at that point, because we cannot
6483          undo the action that is done when creating a new class.  So,
6484          then we cannot back up and do a postfix-expression.
6485
6486          Therefore, we scan ahead to the closing `)', and check to see
6487          if the token after the `)' is a `{'.  If so, we are not
6488          looking at a cast-expression.
6489
6490          Save tokens so that we can put them back.  */
6491       cp_lexer_save_tokens (parser->lexer);
6492       /* Skip tokens until the next token is a closing parenthesis.
6493          If we find the closing `)', and the next token is a `{', then
6494          we are looking at a compound-literal.  */
6495       compound_literal_p
6496         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6497                                                   /*consume_paren=*/true)
6498            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6499       /* Roll back the tokens we skipped.  */
6500       cp_lexer_rollback_tokens (parser->lexer);
6501       /* If we were looking at a compound-literal, simulate an error
6502          so that the call to cp_parser_parse_definitely below will
6503          fail.  */
6504       if (compound_literal_p)
6505         cp_parser_simulate_error (parser);
6506       else
6507         {
6508           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6509           parser->in_type_id_in_expr_p = true;
6510           /* Look for the type-id.  */
6511           type = cp_parser_type_id (parser);
6512           /* Look for the closing `)'.  */
6513           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6514           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6515         }
6516
6517       /* Restore the saved message.  */
6518       parser->type_definition_forbidden_message = saved_message;
6519
6520       /* At this point this can only be either a cast or a
6521          parenthesized ctor such as `(T ())' that looks like a cast to
6522          function returning T.  */
6523       if (!cp_parser_error_occurred (parser)
6524           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6525                                                      (parser->lexer)))
6526         {
6527           cp_parser_parse_definitely (parser);
6528           expr = cp_parser_cast_expression (parser,
6529                                             /*address_p=*/false,
6530                                             /*cast_p=*/true, pidk);
6531
6532           /* Warn about old-style casts, if so requested.  */
6533           if (warn_old_style_cast
6534               && !in_system_header
6535               && !VOID_TYPE_P (type)
6536               && current_lang_name != lang_name_c)
6537             warning (OPT_Wold_style_cast, "use of old-style cast");
6538
6539           /* Only type conversions to integral or enumeration types
6540              can be used in constant-expressions.  */
6541           if (!cast_valid_in_integral_constant_expression_p (type)
6542               && cp_parser_non_integral_constant_expression (parser,
6543                                                              NIC_CAST))
6544             return error_mark_node;
6545
6546           /* Perform the cast.  */
6547           expr = build_c_cast (input_location, type, expr);
6548           return expr;
6549         }
6550       else 
6551         cp_parser_abort_tentative_parse (parser);
6552     }
6553
6554   /* If we get here, then it's not a cast, so it must be a
6555      unary-expression.  */
6556   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6557 }
6558
6559 /* Parse a binary expression of the general form:
6560
6561    pm-expression:
6562      cast-expression
6563      pm-expression .* cast-expression
6564      pm-expression ->* cast-expression
6565
6566    multiplicative-expression:
6567      pm-expression
6568      multiplicative-expression * pm-expression
6569      multiplicative-expression / pm-expression
6570      multiplicative-expression % pm-expression
6571
6572    additive-expression:
6573      multiplicative-expression
6574      additive-expression + multiplicative-expression
6575      additive-expression - multiplicative-expression
6576
6577    shift-expression:
6578      additive-expression
6579      shift-expression << additive-expression
6580      shift-expression >> additive-expression
6581
6582    relational-expression:
6583      shift-expression
6584      relational-expression < shift-expression
6585      relational-expression > shift-expression
6586      relational-expression <= shift-expression
6587      relational-expression >= shift-expression
6588
6589   GNU Extension:
6590
6591    relational-expression:
6592      relational-expression <? shift-expression
6593      relational-expression >? shift-expression
6594
6595    equality-expression:
6596      relational-expression
6597      equality-expression == relational-expression
6598      equality-expression != relational-expression
6599
6600    and-expression:
6601      equality-expression
6602      and-expression & equality-expression
6603
6604    exclusive-or-expression:
6605      and-expression
6606      exclusive-or-expression ^ and-expression
6607
6608    inclusive-or-expression:
6609      exclusive-or-expression
6610      inclusive-or-expression | exclusive-or-expression
6611
6612    logical-and-expression:
6613      inclusive-or-expression
6614      logical-and-expression && inclusive-or-expression
6615
6616    logical-or-expression:
6617      logical-and-expression
6618      logical-or-expression || logical-and-expression
6619
6620    All these are implemented with a single function like:
6621
6622    binary-expression:
6623      simple-cast-expression
6624      binary-expression <token> binary-expression
6625
6626    CAST_P is true if this expression is the target of a cast.
6627
6628    The binops_by_token map is used to get the tree codes for each <token> type.
6629    binary-expressions are associated according to a precedence table.  */
6630
6631 #define TOKEN_PRECEDENCE(token)                              \
6632 (((token->type == CPP_GREATER                                \
6633    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6634   && !parser->greater_than_is_operator_p)                    \
6635  ? PREC_NOT_OPERATOR                                         \
6636  : binops_by_token[token->type].prec)
6637
6638 static tree
6639 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6640                              bool no_toplevel_fold_p,
6641                              enum cp_parser_prec prec,
6642                              cp_id_kind * pidk)
6643 {
6644   cp_parser_expression_stack stack;
6645   cp_parser_expression_stack_entry *sp = &stack[0];
6646   tree lhs, rhs;
6647   cp_token *token;
6648   enum tree_code tree_type, lhs_type, rhs_type;
6649   enum cp_parser_prec new_prec, lookahead_prec;
6650   tree overload;
6651
6652   /* Parse the first expression.  */
6653   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6654   lhs_type = ERROR_MARK;
6655
6656   for (;;)
6657     {
6658       /* Get an operator token.  */
6659       token = cp_lexer_peek_token (parser->lexer);
6660
6661       if (warn_cxx0x_compat
6662           && token->type == CPP_RSHIFT
6663           && !parser->greater_than_is_operator_p)
6664         {
6665           if (warning_at (token->location, OPT_Wc__0x_compat, 
6666                           "%<>>%> operator will be treated as"
6667                           " two right angle brackets in C++0x"))
6668             inform (token->location,
6669                     "suggest parentheses around %<>>%> expression");
6670         }
6671
6672       new_prec = TOKEN_PRECEDENCE (token);
6673
6674       /* Popping an entry off the stack means we completed a subexpression:
6675          - either we found a token which is not an operator (`>' where it is not
6676            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6677            will happen repeatedly;
6678          - or, we found an operator which has lower priority.  This is the case
6679            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6680            parsing `3 * 4'.  */
6681       if (new_prec <= prec)
6682         {
6683           if (sp == stack)
6684             break;
6685           else
6686             goto pop;
6687         }
6688
6689      get_rhs:
6690       tree_type = binops_by_token[token->type].tree_type;
6691
6692       /* We used the operator token.  */
6693       cp_lexer_consume_token (parser->lexer);
6694
6695       /* For "false && x" or "true || x", x will never be executed;
6696          disable warnings while evaluating it.  */
6697       if (tree_type == TRUTH_ANDIF_EXPR)
6698         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6699       else if (tree_type == TRUTH_ORIF_EXPR)
6700         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6701
6702       /* Extract another operand.  It may be the RHS of this expression
6703          or the LHS of a new, higher priority expression.  */
6704       rhs = cp_parser_simple_cast_expression (parser);
6705       rhs_type = ERROR_MARK;
6706
6707       /* Get another operator token.  Look up its precedence to avoid
6708          building a useless (immediately popped) stack entry for common
6709          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6710       token = cp_lexer_peek_token (parser->lexer);
6711       lookahead_prec = TOKEN_PRECEDENCE (token);
6712       if (lookahead_prec > new_prec)
6713         {
6714           /* ... and prepare to parse the RHS of the new, higher priority
6715              expression.  Since precedence levels on the stack are
6716              monotonically increasing, we do not have to care about
6717              stack overflows.  */
6718           sp->prec = prec;
6719           sp->tree_type = tree_type;
6720           sp->lhs = lhs;
6721           sp->lhs_type = lhs_type;
6722           sp++;
6723           lhs = rhs;
6724           lhs_type = rhs_type;
6725           prec = new_prec;
6726           new_prec = lookahead_prec;
6727           goto get_rhs;
6728
6729          pop:
6730           lookahead_prec = new_prec;
6731           /* If the stack is not empty, we have parsed into LHS the right side
6732              (`4' in the example above) of an expression we had suspended.
6733              We can use the information on the stack to recover the LHS (`3')
6734              from the stack together with the tree code (`MULT_EXPR'), and
6735              the precedence of the higher level subexpression
6736              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6737              which will be used to actually build the additive expression.  */
6738           --sp;
6739           prec = sp->prec;
6740           tree_type = sp->tree_type;
6741           rhs = lhs;
6742           rhs_type = lhs_type;
6743           lhs = sp->lhs;
6744           lhs_type = sp->lhs_type;
6745         }
6746
6747       /* Undo the disabling of warnings done above.  */
6748       if (tree_type == TRUTH_ANDIF_EXPR)
6749         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6750       else if (tree_type == TRUTH_ORIF_EXPR)
6751         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6752
6753       overload = NULL;
6754       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6755          ERROR_MARK for everything that is not a binary expression.
6756          This makes warn_about_parentheses miss some warnings that
6757          involve unary operators.  For unary expressions we should
6758          pass the correct tree_code unless the unary expression was
6759          surrounded by parentheses.
6760       */
6761       if (no_toplevel_fold_p
6762           && lookahead_prec <= prec
6763           && sp == stack
6764           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6765         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6766       else
6767         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6768                                  &overload, tf_warning_or_error);
6769       lhs_type = tree_type;
6770
6771       /* If the binary operator required the use of an overloaded operator,
6772          then this expression cannot be an integral constant-expression.
6773          An overloaded operator can be used even if both operands are
6774          otherwise permissible in an integral constant-expression if at
6775          least one of the operands is of enumeration type.  */
6776
6777       if (overload
6778           && cp_parser_non_integral_constant_expression (parser,
6779                                                          NIC_OVERLOADED))
6780         return error_mark_node;
6781     }
6782
6783   return lhs;
6784 }
6785
6786
6787 /* Parse the `? expression : assignment-expression' part of a
6788    conditional-expression.  The LOGICAL_OR_EXPR is the
6789    logical-or-expression that started the conditional-expression.
6790    Returns a representation of the entire conditional-expression.
6791
6792    This routine is used by cp_parser_assignment_expression.
6793
6794      ? expression : assignment-expression
6795
6796    GNU Extensions:
6797
6798      ? : assignment-expression */
6799
6800 static tree
6801 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6802 {
6803   tree expr;
6804   tree assignment_expr;
6805   struct cp_token *token;
6806
6807   /* Consume the `?' token.  */
6808   cp_lexer_consume_token (parser->lexer);
6809   token = cp_lexer_peek_token (parser->lexer);
6810   if (cp_parser_allow_gnu_extensions_p (parser)
6811       && token->type == CPP_COLON)
6812     {
6813       pedwarn (token->location, OPT_pedantic, 
6814                "ISO C++ does not allow ?: with omitted middle operand");
6815       /* Implicit true clause.  */
6816       expr = NULL_TREE;
6817       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6818       warn_for_omitted_condop (token->location, logical_or_expr);
6819     }
6820   else
6821     {
6822       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6823       parser->colon_corrects_to_scope_p = false;
6824       /* Parse the expression.  */
6825       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6826       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6827       c_inhibit_evaluation_warnings +=
6828         ((logical_or_expr == truthvalue_true_node)
6829          - (logical_or_expr == truthvalue_false_node));
6830       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6831     }
6832
6833   /* The next token should be a `:'.  */
6834   cp_parser_require (parser, CPP_COLON, RT_COLON);
6835   /* Parse the assignment-expression.  */
6836   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6837   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6838
6839   /* Build the conditional-expression.  */
6840   return build_x_conditional_expr (logical_or_expr,
6841                                    expr,
6842                                    assignment_expr,
6843                                    tf_warning_or_error);
6844 }
6845
6846 /* Parse an assignment-expression.
6847
6848    assignment-expression:
6849      conditional-expression
6850      logical-or-expression assignment-operator assignment_expression
6851      throw-expression
6852
6853    CAST_P is true if this expression is the target of a cast.
6854
6855    Returns a representation for the expression.  */
6856
6857 static tree
6858 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6859                                  cp_id_kind * pidk)
6860 {
6861   tree expr;
6862
6863   /* If the next token is the `throw' keyword, then we're looking at
6864      a throw-expression.  */
6865   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6866     expr = cp_parser_throw_expression (parser);
6867   /* Otherwise, it must be that we are looking at a
6868      logical-or-expression.  */
6869   else
6870     {
6871       /* Parse the binary expressions (logical-or-expression).  */
6872       expr = cp_parser_binary_expression (parser, cast_p, false,
6873                                           PREC_NOT_OPERATOR, pidk);
6874       /* If the next token is a `?' then we're actually looking at a
6875          conditional-expression.  */
6876       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6877         return cp_parser_question_colon_clause (parser, expr);
6878       else
6879         {
6880           enum tree_code assignment_operator;
6881
6882           /* If it's an assignment-operator, we're using the second
6883              production.  */
6884           assignment_operator
6885             = cp_parser_assignment_operator_opt (parser);
6886           if (assignment_operator != ERROR_MARK)
6887             {
6888               bool non_constant_p;
6889
6890               /* Parse the right-hand side of the assignment.  */
6891               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6892
6893               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6894                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6895
6896               /* An assignment may not appear in a
6897                  constant-expression.  */
6898               if (cp_parser_non_integral_constant_expression (parser,
6899                                                               NIC_ASSIGNMENT))
6900                 return error_mark_node;
6901               /* Build the assignment expression.  */
6902               expr = build_x_modify_expr (expr,
6903                                           assignment_operator,
6904                                           rhs,
6905                                           tf_warning_or_error);
6906             }
6907         }
6908     }
6909
6910   return expr;
6911 }
6912
6913 /* Parse an (optional) assignment-operator.
6914
6915    assignment-operator: one of
6916      = *= /= %= += -= >>= <<= &= ^= |=
6917
6918    GNU Extension:
6919
6920    assignment-operator: one of
6921      <?= >?=
6922
6923    If the next token is an assignment operator, the corresponding tree
6924    code is returned, and the token is consumed.  For example, for
6925    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6926    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6927    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6928    operator, ERROR_MARK is returned.  */
6929
6930 static enum tree_code
6931 cp_parser_assignment_operator_opt (cp_parser* parser)
6932 {
6933   enum tree_code op;
6934   cp_token *token;
6935
6936   /* Peek at the next token.  */
6937   token = cp_lexer_peek_token (parser->lexer);
6938
6939   switch (token->type)
6940     {
6941     case CPP_EQ:
6942       op = NOP_EXPR;
6943       break;
6944
6945     case CPP_MULT_EQ:
6946       op = MULT_EXPR;
6947       break;
6948
6949     case CPP_DIV_EQ:
6950       op = TRUNC_DIV_EXPR;
6951       break;
6952
6953     case CPP_MOD_EQ:
6954       op = TRUNC_MOD_EXPR;
6955       break;
6956
6957     case CPP_PLUS_EQ:
6958       op = PLUS_EXPR;
6959       break;
6960
6961     case CPP_MINUS_EQ:
6962       op = MINUS_EXPR;
6963       break;
6964
6965     case CPP_RSHIFT_EQ:
6966       op = RSHIFT_EXPR;
6967       break;
6968
6969     case CPP_LSHIFT_EQ:
6970       op = LSHIFT_EXPR;
6971       break;
6972
6973     case CPP_AND_EQ:
6974       op = BIT_AND_EXPR;
6975       break;
6976
6977     case CPP_XOR_EQ:
6978       op = BIT_XOR_EXPR;
6979       break;
6980
6981     case CPP_OR_EQ:
6982       op = BIT_IOR_EXPR;
6983       break;
6984
6985     default:
6986       /* Nothing else is an assignment operator.  */
6987       op = ERROR_MARK;
6988     }
6989
6990   /* If it was an assignment operator, consume it.  */
6991   if (op != ERROR_MARK)
6992     cp_lexer_consume_token (parser->lexer);
6993
6994   return op;
6995 }
6996
6997 /* Parse an expression.
6998
6999    expression:
7000      assignment-expression
7001      expression , assignment-expression
7002
7003    CAST_P is true if this expression is the target of a cast.
7004
7005    Returns a representation of the expression.  */
7006
7007 static tree
7008 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7009 {
7010   tree expression = NULL_TREE;
7011
7012   while (true)
7013     {
7014       tree assignment_expression;
7015
7016       /* Parse the next assignment-expression.  */
7017       assignment_expression
7018         = cp_parser_assignment_expression (parser, cast_p, pidk);
7019       /* If this is the first assignment-expression, we can just
7020          save it away.  */
7021       if (!expression)
7022         expression = assignment_expression;
7023       else
7024         expression = build_x_compound_expr (expression,
7025                                             assignment_expression,
7026                                             tf_warning_or_error);
7027       /* If the next token is not a comma, then we are done with the
7028          expression.  */
7029       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7030         break;
7031       /* Consume the `,'.  */
7032       cp_lexer_consume_token (parser->lexer);
7033       /* A comma operator cannot appear in a constant-expression.  */
7034       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7035         expression = error_mark_node;
7036     }
7037
7038   return expression;
7039 }
7040
7041 /* Parse a constant-expression.
7042
7043    constant-expression:
7044      conditional-expression
7045
7046   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7047   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7048   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7049   is false, NON_CONSTANT_P should be NULL.  */
7050
7051 static tree
7052 cp_parser_constant_expression (cp_parser* parser,
7053                                bool allow_non_constant_p,
7054                                bool *non_constant_p)
7055 {
7056   bool saved_integral_constant_expression_p;
7057   bool saved_allow_non_integral_constant_expression_p;
7058   bool saved_non_integral_constant_expression_p;
7059   tree expression;
7060
7061   /* It might seem that we could simply parse the
7062      conditional-expression, and then check to see if it were
7063      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7064      one that the compiler can figure out is constant, possibly after
7065      doing some simplifications or optimizations.  The standard has a
7066      precise definition of constant-expression, and we must honor
7067      that, even though it is somewhat more restrictive.
7068
7069      For example:
7070
7071        int i[(2, 3)];
7072
7073      is not a legal declaration, because `(2, 3)' is not a
7074      constant-expression.  The `,' operator is forbidden in a
7075      constant-expression.  However, GCC's constant-folding machinery
7076      will fold this operation to an INTEGER_CST for `3'.  */
7077
7078   /* Save the old settings.  */
7079   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7080   saved_allow_non_integral_constant_expression_p
7081     = parser->allow_non_integral_constant_expression_p;
7082   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7083   /* We are now parsing a constant-expression.  */
7084   parser->integral_constant_expression_p = true;
7085   parser->allow_non_integral_constant_expression_p
7086     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7087   parser->non_integral_constant_expression_p = false;
7088   /* Although the grammar says "conditional-expression", we parse an
7089      "assignment-expression", which also permits "throw-expression"
7090      and the use of assignment operators.  In the case that
7091      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7092      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7093      actually essential that we look for an assignment-expression.
7094      For example, cp_parser_initializer_clauses uses this function to
7095      determine whether a particular assignment-expression is in fact
7096      constant.  */
7097   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7098   /* Restore the old settings.  */
7099   parser->integral_constant_expression_p
7100     = saved_integral_constant_expression_p;
7101   parser->allow_non_integral_constant_expression_p
7102     = saved_allow_non_integral_constant_expression_p;
7103   if (cxx_dialect >= cxx0x)
7104     {
7105       /* Require an rvalue constant expression here; that's what our
7106          callers expect.  Reference constant expressions are handled
7107          separately in e.g. cp_parser_template_argument.  */
7108       bool is_const = potential_rvalue_constant_expression (expression);
7109       parser->non_integral_constant_expression_p = !is_const;
7110       if (!is_const && !allow_non_constant_p)
7111         require_potential_rvalue_constant_expression (expression);
7112     }
7113   if (allow_non_constant_p)
7114     *non_constant_p = parser->non_integral_constant_expression_p;
7115   parser->non_integral_constant_expression_p
7116     = saved_non_integral_constant_expression_p;
7117
7118   return expression;
7119 }
7120
7121 /* Parse __builtin_offsetof.
7122
7123    offsetof-expression:
7124      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7125
7126    offsetof-member-designator:
7127      id-expression
7128      | offsetof-member-designator "." id-expression
7129      | offsetof-member-designator "[" expression "]"
7130      | offsetof-member-designator "->" id-expression  */
7131
7132 static tree
7133 cp_parser_builtin_offsetof (cp_parser *parser)
7134 {
7135   int save_ice_p, save_non_ice_p;
7136   tree type, expr;
7137   cp_id_kind dummy;
7138   cp_token *token;
7139
7140   /* We're about to accept non-integral-constant things, but will
7141      definitely yield an integral constant expression.  Save and
7142      restore these values around our local parsing.  */
7143   save_ice_p = parser->integral_constant_expression_p;
7144   save_non_ice_p = parser->non_integral_constant_expression_p;
7145
7146   /* Consume the "__builtin_offsetof" token.  */
7147   cp_lexer_consume_token (parser->lexer);
7148   /* Consume the opening `('.  */
7149   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7150   /* Parse the type-id.  */
7151   type = cp_parser_type_id (parser);
7152   /* Look for the `,'.  */
7153   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7154   token = cp_lexer_peek_token (parser->lexer);
7155
7156   /* Build the (type *)null that begins the traditional offsetof macro.  */
7157   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7158                             tf_warning_or_error);
7159
7160   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7161   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7162                                                  true, &dummy, token->location);
7163   while (true)
7164     {
7165       token = cp_lexer_peek_token (parser->lexer);
7166       switch (token->type)
7167         {
7168         case CPP_OPEN_SQUARE:
7169           /* offsetof-member-designator "[" expression "]" */
7170           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7171           break;
7172
7173         case CPP_DEREF:
7174           /* offsetof-member-designator "->" identifier */
7175           expr = grok_array_decl (expr, integer_zero_node);
7176           /* FALLTHRU */
7177
7178         case CPP_DOT:
7179           /* offsetof-member-designator "." identifier */
7180           cp_lexer_consume_token (parser->lexer);
7181           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7182                                                          expr, true, &dummy,
7183                                                          token->location);
7184           break;
7185
7186         case CPP_CLOSE_PAREN:
7187           /* Consume the ")" token.  */
7188           cp_lexer_consume_token (parser->lexer);
7189           goto success;
7190
7191         default:
7192           /* Error.  We know the following require will fail, but
7193              that gives the proper error message.  */
7194           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7195           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7196           expr = error_mark_node;
7197           goto failure;
7198         }
7199     }
7200
7201  success:
7202   /* If we're processing a template, we can't finish the semantics yet.
7203      Otherwise we can fold the entire expression now.  */
7204   if (processing_template_decl)
7205     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7206   else
7207     expr = finish_offsetof (expr);
7208
7209  failure:
7210   parser->integral_constant_expression_p = save_ice_p;
7211   parser->non_integral_constant_expression_p = save_non_ice_p;
7212
7213   return expr;
7214 }
7215
7216 /* Parse a trait expression.
7217
7218    Returns a representation of the expression, the underlying type
7219    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7220
7221 static tree
7222 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7223 {
7224   cp_trait_kind kind;
7225   tree type1, type2 = NULL_TREE;
7226   bool binary = false;
7227   cp_decl_specifier_seq decl_specs;
7228
7229   switch (keyword)
7230     {
7231     case RID_HAS_NOTHROW_ASSIGN:
7232       kind = CPTK_HAS_NOTHROW_ASSIGN;
7233       break;
7234     case RID_HAS_NOTHROW_CONSTRUCTOR:
7235       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7236       break;
7237     case RID_HAS_NOTHROW_COPY:
7238       kind = CPTK_HAS_NOTHROW_COPY;
7239       break;
7240     case RID_HAS_TRIVIAL_ASSIGN:
7241       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7242       break;
7243     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7244       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7245       break;
7246     case RID_HAS_TRIVIAL_COPY:
7247       kind = CPTK_HAS_TRIVIAL_COPY;
7248       break;
7249     case RID_HAS_TRIVIAL_DESTRUCTOR:
7250       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7251       break;
7252     case RID_HAS_VIRTUAL_DESTRUCTOR:
7253       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7254       break;
7255     case RID_IS_ABSTRACT:
7256       kind = CPTK_IS_ABSTRACT;
7257       break;
7258     case RID_IS_BASE_OF:
7259       kind = CPTK_IS_BASE_OF;
7260       binary = true;
7261       break;
7262     case RID_IS_CLASS:
7263       kind = CPTK_IS_CLASS;
7264       break;
7265     case RID_IS_CONVERTIBLE_TO:
7266       kind = CPTK_IS_CONVERTIBLE_TO;
7267       binary = true;
7268       break;
7269     case RID_IS_EMPTY:
7270       kind = CPTK_IS_EMPTY;
7271       break;
7272     case RID_IS_ENUM:
7273       kind = CPTK_IS_ENUM;
7274       break;
7275     case RID_IS_LITERAL_TYPE:
7276       kind = CPTK_IS_LITERAL_TYPE;
7277       break;
7278     case RID_IS_POD:
7279       kind = CPTK_IS_POD;
7280       break;
7281     case RID_IS_POLYMORPHIC:
7282       kind = CPTK_IS_POLYMORPHIC;
7283       break;
7284     case RID_IS_STD_LAYOUT:
7285       kind = CPTK_IS_STD_LAYOUT;
7286       break;
7287     case RID_IS_TRIVIAL:
7288       kind = CPTK_IS_TRIVIAL;
7289       break;
7290     case RID_IS_UNION:
7291       kind = CPTK_IS_UNION;
7292       break;
7293     case RID_UNDERLYING_TYPE:
7294       kind = CPTK_UNDERLYING_TYPE;
7295       break;
7296     default:
7297       gcc_unreachable ();
7298     }
7299
7300   /* Consume the token.  */
7301   cp_lexer_consume_token (parser->lexer);
7302
7303   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7304
7305   type1 = cp_parser_type_id (parser);
7306
7307   if (type1 == error_mark_node)
7308     return error_mark_node;
7309
7310   /* Build a trivial decl-specifier-seq.  */
7311   clear_decl_specs (&decl_specs);
7312   decl_specs.type = type1;
7313
7314   /* Call grokdeclarator to figure out what type this is.  */
7315   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7316                           /*initialized=*/0, /*attrlist=*/NULL);
7317
7318   if (binary)
7319     {
7320       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7321  
7322       type2 = cp_parser_type_id (parser);
7323
7324       if (type2 == error_mark_node)
7325         return error_mark_node;
7326
7327       /* Build a trivial decl-specifier-seq.  */
7328       clear_decl_specs (&decl_specs);
7329       decl_specs.type = type2;
7330
7331       /* Call grokdeclarator to figure out what type this is.  */
7332       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7333                               /*initialized=*/0, /*attrlist=*/NULL);
7334     }
7335
7336   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7337
7338   /* Complete the trait expression, which may mean either processing
7339      the trait expr now or saving it for template instantiation.  */
7340   return kind != CPTK_UNDERLYING_TYPE
7341     ? finish_trait_expr (kind, type1, type2)
7342     : finish_underlying_type (type1);
7343 }
7344
7345 /* Lambdas that appear in variable initializer or default argument scope
7346    get that in their mangling, so we need to record it.  We might as well
7347    use the count for function and namespace scopes as well.  */
7348 static GTY(()) tree lambda_scope;
7349 static GTY(()) int lambda_count;
7350 typedef struct GTY(()) tree_int
7351 {
7352   tree t;
7353   int i;
7354 } tree_int;
7355 DEF_VEC_O(tree_int);
7356 DEF_VEC_ALLOC_O(tree_int,gc);
7357 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7358
7359 static void
7360 start_lambda_scope (tree decl)
7361 {
7362   tree_int ti;
7363   gcc_assert (decl);
7364   /* Once we're inside a function, we ignore other scopes and just push
7365      the function again so that popping works properly.  */
7366   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7367     decl = current_function_decl;
7368   ti.t = lambda_scope;
7369   ti.i = lambda_count;
7370   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7371   if (lambda_scope != decl)
7372     {
7373       /* Don't reset the count if we're still in the same function.  */
7374       lambda_scope = decl;
7375       lambda_count = 0;
7376     }
7377 }
7378
7379 static void
7380 record_lambda_scope (tree lambda)
7381 {
7382   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7383   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7384 }
7385
7386 static void
7387 finish_lambda_scope (void)
7388 {
7389   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7390   if (lambda_scope != p->t)
7391     {
7392       lambda_scope = p->t;
7393       lambda_count = p->i;
7394     }
7395   VEC_pop (tree_int, lambda_scope_stack);
7396 }
7397
7398 /* Parse a lambda expression.
7399
7400    lambda-expression:
7401      lambda-introducer lambda-declarator [opt] compound-statement
7402
7403    Returns a representation of the expression.  */
7404
7405 static tree
7406 cp_parser_lambda_expression (cp_parser* parser)
7407 {
7408   tree lambda_expr = build_lambda_expr ();
7409   tree type;
7410   bool ok;
7411
7412   LAMBDA_EXPR_LOCATION (lambda_expr)
7413     = cp_lexer_peek_token (parser->lexer)->location;
7414
7415   if (cp_unevaluated_operand)
7416     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7417               "lambda-expression in unevaluated context");
7418
7419   /* We may be in the middle of deferred access check.  Disable
7420      it now.  */
7421   push_deferring_access_checks (dk_no_deferred);
7422
7423   cp_parser_lambda_introducer (parser, lambda_expr);
7424
7425   type = begin_lambda_type (lambda_expr);
7426
7427   record_lambda_scope (lambda_expr);
7428
7429   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7430   determine_visibility (TYPE_NAME (type));
7431
7432   /* Now that we've started the type, add the capture fields for any
7433      explicit captures.  */
7434   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7435
7436   {
7437     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7438     unsigned int saved_num_template_parameter_lists
7439         = parser->num_template_parameter_lists;
7440     unsigned char in_statement = parser->in_statement;
7441     bool in_switch_statement_p = parser->in_switch_statement_p;
7442
7443     parser->num_template_parameter_lists = 0;
7444     parser->in_statement = 0;
7445     parser->in_switch_statement_p = false;
7446
7447     /* By virtue of defining a local class, a lambda expression has access to
7448        the private variables of enclosing classes.  */
7449
7450     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7451
7452     if (ok)
7453       cp_parser_lambda_body (parser, lambda_expr);
7454     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7455       cp_parser_skip_to_end_of_block_or_statement (parser);
7456
7457     /* The capture list was built up in reverse order; fix that now.  */
7458     {
7459       tree newlist = NULL_TREE;
7460       tree elt, next;
7461
7462       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7463            elt; elt = next)
7464         {
7465           next = TREE_CHAIN (elt);
7466           TREE_CHAIN (elt) = newlist;
7467           newlist = elt;
7468         }
7469       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7470     }
7471
7472     if (ok)
7473       maybe_add_lambda_conv_op (type);
7474
7475     type = finish_struct (type, /*attributes=*/NULL_TREE);
7476
7477     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7478     parser->in_statement = in_statement;
7479     parser->in_switch_statement_p = in_switch_statement_p;
7480   }
7481
7482   pop_deferring_access_checks ();
7483
7484   /* This field is only used during parsing of the lambda.  */
7485   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7486
7487   /* This lambda shouldn't have any proxies left at this point.  */
7488   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7489   /* And now that we're done, push proxies for an enclosing lambda.  */
7490   insert_pending_capture_proxies ();
7491
7492   if (ok)
7493     return build_lambda_object (lambda_expr);
7494   else
7495     return error_mark_node;
7496 }
7497
7498 /* Parse the beginning of a lambda expression.
7499
7500    lambda-introducer:
7501      [ lambda-capture [opt] ]
7502
7503    LAMBDA_EXPR is the current representation of the lambda expression.  */
7504
7505 static void
7506 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7507 {
7508   /* Need commas after the first capture.  */
7509   bool first = true;
7510
7511   /* Eat the leading `['.  */
7512   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7513
7514   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7515   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7516       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7517     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7518   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7519     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7520
7521   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7522     {
7523       cp_lexer_consume_token (parser->lexer);
7524       first = false;
7525     }
7526
7527   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7528     {
7529       cp_token* capture_token;
7530       tree capture_id;
7531       tree capture_init_expr;
7532       cp_id_kind idk = CP_ID_KIND_NONE;
7533       bool explicit_init_p = false;
7534
7535       enum capture_kind_type
7536       {
7537         BY_COPY,
7538         BY_REFERENCE
7539       };
7540       enum capture_kind_type capture_kind = BY_COPY;
7541
7542       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7543         {
7544           error ("expected end of capture-list");
7545           return;
7546         }
7547
7548       if (first)
7549         first = false;
7550       else
7551         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7552
7553       /* Possibly capture `this'.  */
7554       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7555         {
7556           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7557           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7558             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7559                      "with by-copy capture default");
7560           cp_lexer_consume_token (parser->lexer);
7561           add_capture (lambda_expr,
7562                        /*id=*/this_identifier,
7563                        /*initializer=*/finish_this_expr(),
7564                        /*by_reference_p=*/false,
7565                        explicit_init_p);
7566           continue;
7567         }
7568
7569       /* Remember whether we want to capture as a reference or not.  */
7570       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7571         {
7572           capture_kind = BY_REFERENCE;
7573           cp_lexer_consume_token (parser->lexer);
7574         }
7575
7576       /* Get the identifier.  */
7577       capture_token = cp_lexer_peek_token (parser->lexer);
7578       capture_id = cp_parser_identifier (parser);
7579
7580       if (capture_id == error_mark_node)
7581         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7582            delimiters, but I modified this to stop on unnested ']' as well.  It
7583            was already changed to stop on unnested '}', so the
7584            "closing_parenthesis" name is no more misleading with my change.  */
7585         {
7586           cp_parser_skip_to_closing_parenthesis (parser,
7587                                                  /*recovering=*/true,
7588                                                  /*or_comma=*/true,
7589                                                  /*consume_paren=*/true);
7590           break;
7591         }
7592
7593       /* Find the initializer for this capture.  */
7594       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7595         {
7596           /* An explicit expression exists.  */
7597           cp_lexer_consume_token (parser->lexer);
7598           pedwarn (input_location, OPT_pedantic,
7599                    "ISO C++ does not allow initializers "
7600                    "in lambda expression capture lists");
7601           capture_init_expr = cp_parser_assignment_expression (parser,
7602                                                                /*cast_p=*/true,
7603                                                                &idk);
7604           explicit_init_p = true;
7605         }
7606       else
7607         {
7608           const char* error_msg;
7609
7610           /* Turn the identifier into an id-expression.  */
7611           capture_init_expr
7612             = cp_parser_lookup_name
7613                 (parser,
7614                  capture_id,
7615                  none_type,
7616                  /*is_template=*/false,
7617                  /*is_namespace=*/false,
7618                  /*check_dependency=*/true,
7619                  /*ambiguous_decls=*/NULL,
7620                  capture_token->location);
7621
7622           capture_init_expr
7623             = finish_id_expression
7624                 (capture_id,
7625                  capture_init_expr,
7626                  parser->scope,
7627                  &idk,
7628                  /*integral_constant_expression_p=*/false,
7629                  /*allow_non_integral_constant_expression_p=*/false,
7630                  /*non_integral_constant_expression_p=*/NULL,
7631                  /*template_p=*/false,
7632                  /*done=*/true,
7633                  /*address_p=*/false,
7634                  /*template_arg_p=*/false,
7635                  &error_msg,
7636                  capture_token->location);
7637         }
7638
7639       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7640         capture_init_expr
7641           = unqualified_name_lookup_error (capture_init_expr);
7642
7643       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7644           && !explicit_init_p)
7645         {
7646           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7647               && capture_kind == BY_COPY)
7648             pedwarn (capture_token->location, 0, "explicit by-copy capture "
7649                      "of %qD redundant with by-copy capture default",
7650                      capture_id);
7651           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7652               && capture_kind == BY_REFERENCE)
7653             pedwarn (capture_token->location, 0, "explicit by-reference "
7654                      "capture of %qD redundant with by-reference capture "
7655                      "default", capture_id);
7656         }
7657
7658       add_capture (lambda_expr,
7659                    capture_id,
7660                    capture_init_expr,
7661                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7662                    explicit_init_p);
7663     }
7664
7665   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7666 }
7667
7668 /* Parse the (optional) middle of a lambda expression.
7669
7670    lambda-declarator:
7671      ( parameter-declaration-clause [opt] )
7672        attribute-specifier [opt]
7673        mutable [opt]
7674        exception-specification [opt]
7675        lambda-return-type-clause [opt]
7676
7677    LAMBDA_EXPR is the current representation of the lambda expression.  */
7678
7679 static bool
7680 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7681 {
7682   /* 5.1.1.4 of the standard says:
7683        If a lambda-expression does not include a lambda-declarator, it is as if
7684        the lambda-declarator were ().
7685      This means an empty parameter list, no attributes, and no exception
7686      specification.  */
7687   tree param_list = void_list_node;
7688   tree attributes = NULL_TREE;
7689   tree exception_spec = NULL_TREE;
7690   tree t;
7691
7692   /* The lambda-declarator is optional, but must begin with an opening
7693      parenthesis if present.  */
7694   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7695     {
7696       cp_lexer_consume_token (parser->lexer);
7697
7698       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7699
7700       /* Parse parameters.  */
7701       param_list = cp_parser_parameter_declaration_clause (parser);
7702
7703       /* Default arguments shall not be specified in the
7704          parameter-declaration-clause of a lambda-declarator.  */
7705       for (t = param_list; t; t = TREE_CHAIN (t))
7706         if (TREE_PURPOSE (t))
7707           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7708                    "default argument specified for lambda parameter");
7709
7710       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7711
7712       attributes = cp_parser_attributes_opt (parser);
7713
7714       /* Parse optional `mutable' keyword.  */
7715       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7716         {
7717           cp_lexer_consume_token (parser->lexer);
7718           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7719         }
7720
7721       /* Parse optional exception specification.  */
7722       exception_spec = cp_parser_exception_specification_opt (parser);
7723
7724       /* Parse optional trailing return type.  */
7725       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7726         {
7727           cp_lexer_consume_token (parser->lexer);
7728           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7729         }
7730
7731       /* The function parameters must be in scope all the way until after the
7732          trailing-return-type in case of decltype.  */
7733       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7734         pop_binding (DECL_NAME (t), t);
7735
7736       leave_scope ();
7737     }
7738
7739   /* Create the function call operator.
7740
7741      Messing with declarators like this is no uglier than building up the
7742      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7743      other code.  */
7744   {
7745     cp_decl_specifier_seq return_type_specs;
7746     cp_declarator* declarator;
7747     tree fco;
7748     int quals;
7749     void *p;
7750
7751     clear_decl_specs (&return_type_specs);
7752     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7753       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7754     else
7755       /* Maybe we will deduce the return type later, but we can use void
7756          as a placeholder return type anyways.  */
7757       return_type_specs.type = void_type_node;
7758
7759     p = obstack_alloc (&declarator_obstack, 0);
7760
7761     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7762                                      sfk_none);
7763
7764     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7765              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7766     declarator = make_call_declarator (declarator, param_list, quals,
7767                                        VIRT_SPEC_UNSPECIFIED,
7768                                        exception_spec,
7769                                        /*late_return_type=*/NULL_TREE);
7770     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7771
7772     fco = grokmethod (&return_type_specs,
7773                       declarator,
7774                       attributes);
7775     if (fco != error_mark_node)
7776       {
7777         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7778         DECL_ARTIFICIAL (fco) = 1;
7779         /* Give the object parameter a different name.  */
7780         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7781       }
7782
7783     finish_member_declaration (fco);
7784
7785     obstack_free (&declarator_obstack, p);
7786
7787     return (fco != error_mark_node);
7788   }
7789 }
7790
7791 /* Parse the body of a lambda expression, which is simply
7792
7793    compound-statement
7794
7795    but which requires special handling.
7796    LAMBDA_EXPR is the current representation of the lambda expression.  */
7797
7798 static void
7799 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7800 {
7801   bool nested = (current_function_decl != NULL_TREE);
7802   if (nested)
7803     push_function_context ();
7804   else
7805     /* Still increment function_depth so that we don't GC in the
7806        middle of an expression.  */
7807     ++function_depth;
7808
7809   /* Finish the function call operator
7810      - class_specifier
7811      + late_parsing_for_member
7812      + function_definition_after_declarator
7813      + ctor_initializer_opt_and_function_body  */
7814   {
7815     tree fco = lambda_function (lambda_expr);
7816     tree body;
7817     bool done = false;
7818     tree compound_stmt;
7819     tree cap;
7820
7821     /* Let the front end know that we are going to be defining this
7822        function.  */
7823     start_preparsed_function (fco,
7824                               NULL_TREE,
7825                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7826
7827     start_lambda_scope (fco);
7828     body = begin_function_body ();
7829
7830     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7831       goto out;
7832
7833     /* Push the proxies for any explicit captures.  */
7834     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7835          cap = TREE_CHAIN (cap))
7836       build_capture_proxy (TREE_PURPOSE (cap));
7837
7838     compound_stmt = begin_compound_stmt (0);
7839
7840     /* 5.1.1.4 of the standard says:
7841          If a lambda-expression does not include a trailing-return-type, it
7842          is as if the trailing-return-type denotes the following type:
7843           * if the compound-statement is of the form
7844                { return attribute-specifier [opt] expression ; }
7845              the type of the returned expression after lvalue-to-rvalue
7846              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7847              (_conv.array_ 4.2), and function-to-pointer conversion
7848              (_conv.func_ 4.3);
7849           * otherwise, void.  */
7850
7851     /* In a lambda that has neither a lambda-return-type-clause
7852        nor a deducible form, errors should be reported for return statements
7853        in the body.  Since we used void as the placeholder return type, parsing
7854        the body as usual will give such desired behavior.  */
7855     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7856         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7857         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7858       {
7859         tree expr = NULL_TREE;
7860         cp_id_kind idk = CP_ID_KIND_NONE;
7861
7862         /* Parse tentatively in case there's more after the initial return
7863            statement.  */
7864         cp_parser_parse_tentatively (parser);
7865
7866         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7867
7868         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7869
7870         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7871         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7872
7873         if (cp_parser_parse_definitely (parser))
7874           {
7875             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7876
7877             /* Will get error here if type not deduced yet.  */
7878             finish_return_stmt (expr);
7879
7880             done = true;
7881           }
7882       }
7883
7884     if (!done)
7885       {
7886         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7887           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7888         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7889           cp_parser_label_declaration (parser);
7890         cp_parser_statement_seq_opt (parser, NULL_TREE);
7891         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7892         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7893       }
7894
7895     finish_compound_stmt (compound_stmt);
7896
7897   out:
7898     finish_function_body (body);
7899     finish_lambda_scope ();
7900
7901     /* Finish the function and generate code for it if necessary.  */
7902     expand_or_defer_fn (finish_function (/*inline*/2));
7903   }
7904
7905   if (nested)
7906     pop_function_context();
7907   else
7908     --function_depth;
7909 }
7910
7911 /* Statements [gram.stmt.stmt]  */
7912
7913 /* Parse a statement.
7914
7915    statement:
7916      labeled-statement
7917      expression-statement
7918      compound-statement
7919      selection-statement
7920      iteration-statement
7921      jump-statement
7922      declaration-statement
7923      try-block
7924
7925   IN_COMPOUND is true when the statement is nested inside a
7926   cp_parser_compound_statement; this matters for certain pragmas.
7927
7928   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7929   is a (possibly labeled) if statement which is not enclosed in braces
7930   and has an else clause.  This is used to implement -Wparentheses.  */
7931
7932 static void
7933 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7934                      bool in_compound, bool *if_p)
7935 {
7936   tree statement;
7937   cp_token *token;
7938   location_t statement_location;
7939
7940  restart:
7941   if (if_p != NULL)
7942     *if_p = false;
7943   /* There is no statement yet.  */
7944   statement = NULL_TREE;
7945   /* Peek at the next token.  */
7946   token = cp_lexer_peek_token (parser->lexer);
7947   /* Remember the location of the first token in the statement.  */
7948   statement_location = token->location;
7949   /* If this is a keyword, then that will often determine what kind of
7950      statement we have.  */
7951   if (token->type == CPP_KEYWORD)
7952     {
7953       enum rid keyword = token->keyword;
7954
7955       switch (keyword)
7956         {
7957         case RID_CASE:
7958         case RID_DEFAULT:
7959           /* Looks like a labeled-statement with a case label.
7960              Parse the label, and then use tail recursion to parse
7961              the statement.  */
7962           cp_parser_label_for_labeled_statement (parser);
7963           goto restart;
7964
7965         case RID_IF:
7966         case RID_SWITCH:
7967           statement = cp_parser_selection_statement (parser, if_p);
7968           break;
7969
7970         case RID_WHILE:
7971         case RID_DO:
7972         case RID_FOR:
7973           statement = cp_parser_iteration_statement (parser);
7974           break;
7975
7976         case RID_BREAK:
7977         case RID_CONTINUE:
7978         case RID_RETURN:
7979         case RID_GOTO:
7980           statement = cp_parser_jump_statement (parser);
7981           break;
7982
7983           /* Objective-C++ exception-handling constructs.  */
7984         case RID_AT_TRY:
7985         case RID_AT_CATCH:
7986         case RID_AT_FINALLY:
7987         case RID_AT_SYNCHRONIZED:
7988         case RID_AT_THROW:
7989           statement = cp_parser_objc_statement (parser);
7990           break;
7991
7992         case RID_TRY:
7993           statement = cp_parser_try_block (parser);
7994           break;
7995
7996         case RID_NAMESPACE:
7997           /* This must be a namespace alias definition.  */
7998           cp_parser_declaration_statement (parser);
7999           return;
8000           
8001         default:
8002           /* It might be a keyword like `int' that can start a
8003              declaration-statement.  */
8004           break;
8005         }
8006     }
8007   else if (token->type == CPP_NAME)
8008     {
8009       /* If the next token is a `:', then we are looking at a
8010          labeled-statement.  */
8011       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8012       if (token->type == CPP_COLON)
8013         {
8014           /* Looks like a labeled-statement with an ordinary label.
8015              Parse the label, and then use tail recursion to parse
8016              the statement.  */
8017           cp_parser_label_for_labeled_statement (parser);
8018           goto restart;
8019         }
8020     }
8021   /* Anything that starts with a `{' must be a compound-statement.  */
8022   else if (token->type == CPP_OPEN_BRACE)
8023     statement = cp_parser_compound_statement (parser, NULL, false, false);
8024   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8025      a statement all its own.  */
8026   else if (token->type == CPP_PRAGMA)
8027     {
8028       /* Only certain OpenMP pragmas are attached to statements, and thus
8029          are considered statements themselves.  All others are not.  In
8030          the context of a compound, accept the pragma as a "statement" and
8031          return so that we can check for a close brace.  Otherwise we
8032          require a real statement and must go back and read one.  */
8033       if (in_compound)
8034         cp_parser_pragma (parser, pragma_compound);
8035       else if (!cp_parser_pragma (parser, pragma_stmt))
8036         goto restart;
8037       return;
8038     }
8039   else if (token->type == CPP_EOF)
8040     {
8041       cp_parser_error (parser, "expected statement");
8042       return;
8043     }
8044
8045   /* Everything else must be a declaration-statement or an
8046      expression-statement.  Try for the declaration-statement
8047      first, unless we are looking at a `;', in which case we know that
8048      we have an expression-statement.  */
8049   if (!statement)
8050     {
8051       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8052         {
8053           cp_parser_parse_tentatively (parser);
8054           /* Try to parse the declaration-statement.  */
8055           cp_parser_declaration_statement (parser);
8056           /* If that worked, we're done.  */
8057           if (cp_parser_parse_definitely (parser))
8058             return;
8059         }
8060       /* Look for an expression-statement instead.  */
8061       statement = cp_parser_expression_statement (parser, in_statement_expr);
8062     }
8063
8064   /* Set the line number for the statement.  */
8065   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8066     SET_EXPR_LOCATION (statement, statement_location);
8067 }
8068
8069 /* Parse the label for a labeled-statement, i.e.
8070
8071    identifier :
8072    case constant-expression :
8073    default :
8074
8075    GNU Extension:
8076    case constant-expression ... constant-expression : statement
8077
8078    When a label is parsed without errors, the label is added to the
8079    parse tree by the finish_* functions, so this function doesn't
8080    have to return the label.  */
8081
8082 static void
8083 cp_parser_label_for_labeled_statement (cp_parser* parser)
8084 {
8085   cp_token *token;
8086   tree label = NULL_TREE;
8087   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8088
8089   /* The next token should be an identifier.  */
8090   token = cp_lexer_peek_token (parser->lexer);
8091   if (token->type != CPP_NAME
8092       && token->type != CPP_KEYWORD)
8093     {
8094       cp_parser_error (parser, "expected labeled-statement");
8095       return;
8096     }
8097
8098   parser->colon_corrects_to_scope_p = false;
8099   switch (token->keyword)
8100     {
8101     case RID_CASE:
8102       {
8103         tree expr, expr_hi;
8104         cp_token *ellipsis;
8105
8106         /* Consume the `case' token.  */
8107         cp_lexer_consume_token (parser->lexer);
8108         /* Parse the constant-expression.  */
8109         expr = cp_parser_constant_expression (parser,
8110                                               /*allow_non_constant_p=*/false,
8111                                               NULL);
8112
8113         ellipsis = cp_lexer_peek_token (parser->lexer);
8114         if (ellipsis->type == CPP_ELLIPSIS)
8115           {
8116             /* Consume the `...' token.  */
8117             cp_lexer_consume_token (parser->lexer);
8118             expr_hi =
8119               cp_parser_constant_expression (parser,
8120                                              /*allow_non_constant_p=*/false,
8121                                              NULL);
8122             /* We don't need to emit warnings here, as the common code
8123                will do this for us.  */
8124           }
8125         else
8126           expr_hi = NULL_TREE;
8127
8128         if (parser->in_switch_statement_p)
8129           finish_case_label (token->location, expr, expr_hi);
8130         else
8131           error_at (token->location,
8132                     "case label %qE not within a switch statement",
8133                     expr);
8134       }
8135       break;
8136
8137     case RID_DEFAULT:
8138       /* Consume the `default' token.  */
8139       cp_lexer_consume_token (parser->lexer);
8140
8141       if (parser->in_switch_statement_p)
8142         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8143       else
8144         error_at (token->location, "case label not within a switch statement");
8145       break;
8146
8147     default:
8148       /* Anything else must be an ordinary label.  */
8149       label = finish_label_stmt (cp_parser_identifier (parser));
8150       break;
8151     }
8152
8153   /* Require the `:' token.  */
8154   cp_parser_require (parser, CPP_COLON, RT_COLON);
8155
8156   /* An ordinary label may optionally be followed by attributes.
8157      However, this is only permitted if the attributes are then
8158      followed by a semicolon.  This is because, for backward
8159      compatibility, when parsing
8160        lab: __attribute__ ((unused)) int i;
8161      we want the attribute to attach to "i", not "lab".  */
8162   if (label != NULL_TREE
8163       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8164     {
8165       tree attrs;
8166
8167       cp_parser_parse_tentatively (parser);
8168       attrs = cp_parser_attributes_opt (parser);
8169       if (attrs == NULL_TREE
8170           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8171         cp_parser_abort_tentative_parse (parser);
8172       else if (!cp_parser_parse_definitely (parser))
8173         ;
8174       else
8175         cplus_decl_attributes (&label, attrs, 0);
8176     }
8177
8178   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8179 }
8180
8181 /* Parse an expression-statement.
8182
8183    expression-statement:
8184      expression [opt] ;
8185
8186    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8187    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8188    indicates whether this expression-statement is part of an
8189    expression statement.  */
8190
8191 static tree
8192 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8193 {
8194   tree statement = NULL_TREE;
8195   cp_token *token = cp_lexer_peek_token (parser->lexer);
8196
8197   /* If the next token is a ';', then there is no expression
8198      statement.  */
8199   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8200     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8201
8202   /* Give a helpful message for "A<T>::type t;" and the like.  */
8203   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8204       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8205     {
8206       if (TREE_CODE (statement) == SCOPE_REF)
8207         error_at (token->location, "need %<typename%> before %qE because "
8208                   "%qT is a dependent scope",
8209                   statement, TREE_OPERAND (statement, 0));
8210       else if (is_overloaded_fn (statement)
8211                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8212         {
8213           /* A::A a; */
8214           tree fn = get_first_fn (statement);
8215           error_at (token->location,
8216                     "%<%T::%D%> names the constructor, not the type",
8217                     DECL_CONTEXT (fn), DECL_NAME (fn));
8218         }
8219     }
8220
8221   /* Consume the final `;'.  */
8222   cp_parser_consume_semicolon_at_end_of_statement (parser);
8223
8224   if (in_statement_expr
8225       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8226     /* This is the final expression statement of a statement
8227        expression.  */
8228     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8229   else if (statement)
8230     statement = finish_expr_stmt (statement);
8231   else
8232     finish_stmt ();
8233
8234   return statement;
8235 }
8236
8237 /* Parse a compound-statement.
8238
8239    compound-statement:
8240      { statement-seq [opt] }
8241
8242    GNU extension:
8243
8244    compound-statement:
8245      { label-declaration-seq [opt] statement-seq [opt] }
8246
8247    label-declaration-seq:
8248      label-declaration
8249      label-declaration-seq label-declaration
8250
8251    Returns a tree representing the statement.  */
8252
8253 static tree
8254 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8255                               bool in_try, bool function_body)
8256 {
8257   tree compound_stmt;
8258
8259   /* Consume the `{'.  */
8260   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8261     return error_mark_node;
8262   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8263       && !function_body)
8264     pedwarn (input_location, OPT_pedantic,
8265              "compound-statement in constexpr function");
8266   /* Begin the compound-statement.  */
8267   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8268   /* If the next keyword is `__label__' we have a label declaration.  */
8269   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8270     cp_parser_label_declaration (parser);
8271   /* Parse an (optional) statement-seq.  */
8272   cp_parser_statement_seq_opt (parser, in_statement_expr);
8273   /* Finish the compound-statement.  */
8274   finish_compound_stmt (compound_stmt);
8275   /* Consume the `}'.  */
8276   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8277
8278   return compound_stmt;
8279 }
8280
8281 /* Parse an (optional) statement-seq.
8282
8283    statement-seq:
8284      statement
8285      statement-seq [opt] statement  */
8286
8287 static void
8288 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8289 {
8290   /* Scan statements until there aren't any more.  */
8291   while (true)
8292     {
8293       cp_token *token = cp_lexer_peek_token (parser->lexer);
8294
8295       /* If we are looking at a `}', then we have run out of
8296          statements; the same is true if we have reached the end
8297          of file, or have stumbled upon a stray '@end'.  */
8298       if (token->type == CPP_CLOSE_BRACE
8299           || token->type == CPP_EOF
8300           || token->type == CPP_PRAGMA_EOL
8301           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8302         break;
8303       
8304       /* If we are in a compound statement and find 'else' then
8305          something went wrong.  */
8306       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8307         {
8308           if (parser->in_statement & IN_IF_STMT) 
8309             break;
8310           else
8311             {
8312               token = cp_lexer_consume_token (parser->lexer);
8313               error_at (token->location, "%<else%> without a previous %<if%>");
8314             }
8315         }
8316
8317       /* Parse the statement.  */
8318       cp_parser_statement (parser, in_statement_expr, true, NULL);
8319     }
8320 }
8321
8322 /* Parse a selection-statement.
8323
8324    selection-statement:
8325      if ( condition ) statement
8326      if ( condition ) statement else statement
8327      switch ( condition ) statement
8328
8329    Returns the new IF_STMT or SWITCH_STMT.
8330
8331    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8332    is a (possibly labeled) if statement which is not enclosed in
8333    braces and has an else clause.  This is used to implement
8334    -Wparentheses.  */
8335
8336 static tree
8337 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8338 {
8339   cp_token *token;
8340   enum rid keyword;
8341
8342   if (if_p != NULL)
8343     *if_p = false;
8344
8345   /* Peek at the next token.  */
8346   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8347
8348   /* See what kind of keyword it is.  */
8349   keyword = token->keyword;
8350   switch (keyword)
8351     {
8352     case RID_IF:
8353     case RID_SWITCH:
8354       {
8355         tree statement;
8356         tree condition;
8357
8358         /* Look for the `('.  */
8359         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8360           {
8361             cp_parser_skip_to_end_of_statement (parser);
8362             return error_mark_node;
8363           }
8364
8365         /* Begin the selection-statement.  */
8366         if (keyword == RID_IF)
8367           statement = begin_if_stmt ();
8368         else
8369           statement = begin_switch_stmt ();
8370
8371         /* Parse the condition.  */
8372         condition = cp_parser_condition (parser);
8373         /* Look for the `)'.  */
8374         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8375           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8376                                                  /*consume_paren=*/true);
8377
8378         if (keyword == RID_IF)
8379           {
8380             bool nested_if;
8381             unsigned char in_statement;
8382
8383             /* Add the condition.  */
8384             finish_if_stmt_cond (condition, statement);
8385
8386             /* Parse the then-clause.  */
8387             in_statement = parser->in_statement;
8388             parser->in_statement |= IN_IF_STMT;
8389             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8390               {
8391                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8392                 add_stmt (build_empty_stmt (loc));
8393                 cp_lexer_consume_token (parser->lexer);
8394                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8395                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8396                               "empty body in an %<if%> statement");
8397                 nested_if = false;
8398               }
8399             else
8400               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8401             parser->in_statement = in_statement;
8402
8403             finish_then_clause (statement);
8404
8405             /* If the next token is `else', parse the else-clause.  */
8406             if (cp_lexer_next_token_is_keyword (parser->lexer,
8407                                                 RID_ELSE))
8408               {
8409                 /* Consume the `else' keyword.  */
8410                 cp_lexer_consume_token (parser->lexer);
8411                 begin_else_clause (statement);
8412                 /* Parse the else-clause.  */
8413                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8414                   {
8415                     location_t loc;
8416                     loc = cp_lexer_peek_token (parser->lexer)->location;
8417                     warning_at (loc,
8418                                 OPT_Wempty_body, "suggest braces around "
8419                                 "empty body in an %<else%> statement");
8420                     add_stmt (build_empty_stmt (loc));
8421                     cp_lexer_consume_token (parser->lexer);
8422                   }
8423                 else
8424                   cp_parser_implicitly_scoped_statement (parser, NULL);
8425
8426                 finish_else_clause (statement);
8427
8428                 /* If we are currently parsing a then-clause, then
8429                    IF_P will not be NULL.  We set it to true to
8430                    indicate that this if statement has an else clause.
8431                    This may trigger the Wparentheses warning below
8432                    when we get back up to the parent if statement.  */
8433                 if (if_p != NULL)
8434                   *if_p = true;
8435               }
8436             else
8437               {
8438                 /* This if statement does not have an else clause.  If
8439                    NESTED_IF is true, then the then-clause is an if
8440                    statement which does have an else clause.  We warn
8441                    about the potential ambiguity.  */
8442                 if (nested_if)
8443                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8444                               "suggest explicit braces to avoid ambiguous"
8445                               " %<else%>");
8446               }
8447
8448             /* Now we're all done with the if-statement.  */
8449             finish_if_stmt (statement);
8450           }
8451         else
8452           {
8453             bool in_switch_statement_p;
8454             unsigned char in_statement;
8455
8456             /* Add the condition.  */
8457             finish_switch_cond (condition, statement);
8458
8459             /* Parse the body of the switch-statement.  */
8460             in_switch_statement_p = parser->in_switch_statement_p;
8461             in_statement = parser->in_statement;
8462             parser->in_switch_statement_p = true;
8463             parser->in_statement |= IN_SWITCH_STMT;
8464             cp_parser_implicitly_scoped_statement (parser, NULL);
8465             parser->in_switch_statement_p = in_switch_statement_p;
8466             parser->in_statement = in_statement;
8467
8468             /* Now we're all done with the switch-statement.  */
8469             finish_switch_stmt (statement);
8470           }
8471
8472         return statement;
8473       }
8474       break;
8475
8476     default:
8477       cp_parser_error (parser, "expected selection-statement");
8478       return error_mark_node;
8479     }
8480 }
8481
8482 /* Parse a condition.
8483
8484    condition:
8485      expression
8486      type-specifier-seq declarator = initializer-clause
8487      type-specifier-seq declarator braced-init-list
8488
8489    GNU Extension:
8490
8491    condition:
8492      type-specifier-seq declarator asm-specification [opt]
8493        attributes [opt] = assignment-expression
8494
8495    Returns the expression that should be tested.  */
8496
8497 static tree
8498 cp_parser_condition (cp_parser* parser)
8499 {
8500   cp_decl_specifier_seq type_specifiers;
8501   const char *saved_message;
8502   int declares_class_or_enum;
8503
8504   /* Try the declaration first.  */
8505   cp_parser_parse_tentatively (parser);
8506   /* New types are not allowed in the type-specifier-seq for a
8507      condition.  */
8508   saved_message = parser->type_definition_forbidden_message;
8509   parser->type_definition_forbidden_message
8510     = G_("types may not be defined in conditions");
8511   /* Parse the type-specifier-seq.  */
8512   cp_parser_decl_specifier_seq (parser,
8513                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8514                                 &type_specifiers,
8515                                 &declares_class_or_enum);
8516   /* Restore the saved message.  */
8517   parser->type_definition_forbidden_message = saved_message;
8518   /* If all is well, we might be looking at a declaration.  */
8519   if (!cp_parser_error_occurred (parser))
8520     {
8521       tree decl;
8522       tree asm_specification;
8523       tree attributes;
8524       cp_declarator *declarator;
8525       tree initializer = NULL_TREE;
8526
8527       /* Parse the declarator.  */
8528       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8529                                          /*ctor_dtor_or_conv_p=*/NULL,
8530                                          /*parenthesized_p=*/NULL,
8531                                          /*member_p=*/false);
8532       /* Parse the attributes.  */
8533       attributes = cp_parser_attributes_opt (parser);
8534       /* Parse the asm-specification.  */
8535       asm_specification = cp_parser_asm_specification_opt (parser);
8536       /* If the next token is not an `=' or '{', then we might still be
8537          looking at an expression.  For example:
8538
8539            if (A(a).x)
8540
8541          looks like a decl-specifier-seq and a declarator -- but then
8542          there is no `=', so this is an expression.  */
8543       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8544           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8545         cp_parser_simulate_error (parser);
8546         
8547       /* If we did see an `=' or '{', then we are looking at a declaration
8548          for sure.  */
8549       if (cp_parser_parse_definitely (parser))
8550         {
8551           tree pushed_scope;
8552           bool non_constant_p;
8553           bool flags = LOOKUP_ONLYCONVERTING;
8554
8555           /* Create the declaration.  */
8556           decl = start_decl (declarator, &type_specifiers,
8557                              /*initialized_p=*/true,
8558                              attributes, /*prefix_attributes=*/NULL_TREE,
8559                              &pushed_scope);
8560
8561           /* Parse the initializer.  */
8562           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8563             {
8564               initializer = cp_parser_braced_list (parser, &non_constant_p);
8565               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8566               flags = 0;
8567             }
8568           else
8569             {
8570               /* Consume the `='.  */
8571               cp_parser_require (parser, CPP_EQ, RT_EQ);
8572               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8573             }
8574           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8575             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8576
8577           /* Process the initializer.  */
8578           cp_finish_decl (decl,
8579                           initializer, !non_constant_p,
8580                           asm_specification,
8581                           flags);
8582
8583           if (pushed_scope)
8584             pop_scope (pushed_scope);
8585
8586           return convert_from_reference (decl);
8587         }
8588     }
8589   /* If we didn't even get past the declarator successfully, we are
8590      definitely not looking at a declaration.  */
8591   else
8592     cp_parser_abort_tentative_parse (parser);
8593
8594   /* Otherwise, we are looking at an expression.  */
8595   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8596 }
8597
8598 /* Parses a for-statement or range-for-statement until the closing ')',
8599    not included. */
8600
8601 static tree
8602 cp_parser_for (cp_parser *parser)
8603 {
8604   tree init, scope, decl;
8605   bool is_range_for;
8606
8607   /* Begin the for-statement.  */
8608   scope = begin_for_scope (&init);
8609
8610   /* Parse the initialization.  */
8611   is_range_for = cp_parser_for_init_statement (parser, &decl);
8612
8613   if (is_range_for)
8614     return cp_parser_range_for (parser, scope, init, decl);
8615   else
8616     return cp_parser_c_for (parser, scope, init);
8617 }
8618
8619 static tree
8620 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8621 {
8622   /* Normal for loop */
8623   tree condition = NULL_TREE;
8624   tree expression = NULL_TREE;
8625   tree stmt;
8626
8627   stmt = begin_for_stmt (scope, init);
8628   /* The for-init-statement has already been parsed in
8629      cp_parser_for_init_statement, so no work is needed here.  */
8630   finish_for_init_stmt (stmt);
8631
8632   /* If there's a condition, process it.  */
8633   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8634     condition = cp_parser_condition (parser);
8635   finish_for_cond (condition, stmt);
8636   /* Look for the `;'.  */
8637   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8638
8639   /* If there's an expression, process it.  */
8640   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8641     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8642   finish_for_expr (expression, stmt);
8643
8644   return stmt;
8645 }
8646
8647 /* Tries to parse a range-based for-statement:
8648
8649   range-based-for:
8650     decl-specifier-seq declarator : expression
8651
8652   The decl-specifier-seq declarator and the `:' are already parsed by
8653   cp_parser_for_init_statement. If processing_template_decl it returns a
8654   newly created RANGE_FOR_STMT; if not, it is converted to a
8655   regular FOR_STMT.  */
8656
8657 static tree
8658 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8659 {
8660   tree stmt, range_expr;
8661
8662   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8663     {
8664       bool expr_non_constant_p;
8665       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8666     }
8667   else
8668     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8669
8670   /* If in template, STMT is converted to a normal for-statement
8671      at instantiation. If not, it is done just ahead. */
8672   if (processing_template_decl)
8673     {
8674       stmt = begin_range_for_stmt (scope, init);
8675       finish_range_for_decl (stmt, range_decl, range_expr);
8676     }
8677   else
8678     {
8679       stmt = begin_for_stmt (scope, init);
8680       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8681     }
8682   return stmt;
8683 }
8684
8685 /* Converts a range-based for-statement into a normal
8686    for-statement, as per the definition.
8687
8688       for (RANGE_DECL : RANGE_EXPR)
8689         BLOCK
8690
8691    should be equivalent to:
8692
8693       {
8694         auto &&__range = RANGE_EXPR;
8695         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8696               __begin != __end;
8697               ++__begin)
8698           {
8699               RANGE_DECL = *__begin;
8700               BLOCK
8701           }
8702       }
8703
8704    If RANGE_EXPR is an array:
8705         BEGIN_EXPR = __range
8706         END_EXPR = __range + ARRAY_SIZE(__range)
8707    Else if RANGE_EXPR has a member 'begin' or 'end':
8708         BEGIN_EXPR = __range.begin()
8709         END_EXPR = __range.end()
8710    Else:
8711         BEGIN_EXPR = begin(__range)
8712         END_EXPR = end(__range);
8713
8714    If __range has a member 'begin' but not 'end', or vice versa, we must
8715    still use the second alternative (it will surely fail, however).
8716    When calling begin()/end() in the third alternative we must use
8717    argument dependent lookup, but always considering 'std' as an associated
8718    namespace.  */
8719
8720 tree
8721 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8722 {
8723   tree range_type, range_temp;
8724   tree begin, end;
8725   tree iter_type, begin_expr, end_expr;
8726   tree condition, expression;
8727
8728   if (range_decl == error_mark_node || range_expr == error_mark_node)
8729     /* If an error happened previously do nothing or else a lot of
8730        unhelpful errors would be issued.  */
8731     begin_expr = end_expr = iter_type = error_mark_node;
8732   else
8733     {
8734       /* Find out the type deduced by the declaration
8735          `auto &&__range = range_expr'.  */
8736       range_type = cp_build_reference_type (make_auto (), true);
8737       range_type = do_auto_deduction (range_type, range_expr,
8738                                       type_uses_auto (range_type));
8739
8740       /* Create the __range variable.  */
8741       range_temp = build_decl (input_location, VAR_DECL,
8742                                get_identifier ("__for_range"), range_type);
8743       TREE_USED (range_temp) = 1;
8744       DECL_ARTIFICIAL (range_temp) = 1;
8745       pushdecl (range_temp);
8746       cp_finish_decl (range_temp, range_expr,
8747                       /*is_constant_init*/false, NULL_TREE,
8748                       LOOKUP_ONLYCONVERTING);
8749
8750       range_temp = convert_from_reference (range_temp);
8751       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8752                                                       &begin_expr, &end_expr);
8753     }
8754
8755   /* The new for initialization statement.  */
8756   begin = build_decl (input_location, VAR_DECL,
8757                       get_identifier ("__for_begin"), iter_type);
8758   TREE_USED (begin) = 1;
8759   DECL_ARTIFICIAL (begin) = 1;
8760   pushdecl (begin);
8761   cp_finish_decl (begin, begin_expr,
8762                   /*is_constant_init*/false, NULL_TREE,
8763                   LOOKUP_ONLYCONVERTING);
8764
8765   end = build_decl (input_location, VAR_DECL,
8766                     get_identifier ("__for_end"), iter_type);
8767   TREE_USED (end) = 1;
8768   DECL_ARTIFICIAL (end) = 1;
8769   pushdecl (end);
8770   cp_finish_decl (end, end_expr,
8771                   /*is_constant_init*/false, NULL_TREE,
8772                   LOOKUP_ONLYCONVERTING);
8773
8774   finish_for_init_stmt (statement);
8775
8776   /* The new for condition.  */
8777   condition = build_x_binary_op (NE_EXPR,
8778                                  begin, ERROR_MARK,
8779                                  end, ERROR_MARK,
8780                                  NULL, tf_warning_or_error);
8781   finish_for_cond (condition, statement);
8782
8783   /* The new increment expression.  */
8784   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8785   finish_for_expr (expression, statement);
8786
8787   /* The declaration is initialized with *__begin inside the loop body.  */
8788   cp_finish_decl (range_decl,
8789                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8790                   /*is_constant_init*/false, NULL_TREE,
8791                   LOOKUP_ONLYCONVERTING);
8792
8793   return statement;
8794 }
8795
8796 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8797    We need to solve both at the same time because the method used
8798    depends on the existence of members begin or end.
8799    Returns the type deduced for the iterator expression.  */
8800
8801 static tree
8802 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8803 {
8804   if (error_operand_p (range))
8805     {
8806       *begin = *end = error_mark_node;
8807       return error_mark_node;
8808     }
8809
8810   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8811     {
8812       error ("range-based %<for%> expression of type %qT "
8813              "has incomplete type", TREE_TYPE (range));
8814       *begin = *end = error_mark_node;
8815       return error_mark_node;
8816     }
8817   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8818     {
8819       /* If RANGE is an array, we will use pointer arithmetic.  */
8820       *begin = range;
8821       *end = build_binary_op (input_location, PLUS_EXPR,
8822                               range,
8823                               array_type_nelts_top (TREE_TYPE (range)),
8824                               0);
8825       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8826     }
8827   else
8828     {
8829       /* If it is not an array, we must do a bit of magic.  */
8830       tree id_begin, id_end;
8831       tree member_begin, member_end;
8832
8833       *begin = *end = error_mark_node;
8834
8835       id_begin = get_identifier ("begin");
8836       id_end = get_identifier ("end");
8837       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8838                                     /*protect=*/2, /*want_type=*/false);
8839       member_end = lookup_member (TREE_TYPE (range), id_end,
8840                                   /*protect=*/2, /*want_type=*/false);
8841
8842       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8843         {
8844           /* Use the member functions.  */
8845           if (member_begin != NULL_TREE)
8846             *begin = cp_parser_range_for_member_function (range, id_begin);
8847           else
8848             error ("range-based %<for%> expression of type %qT has an "
8849                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8850
8851           if (member_end != NULL_TREE)
8852             *end = cp_parser_range_for_member_function (range, id_end);
8853           else
8854             error ("range-based %<for%> expression of type %qT has a "
8855                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8856         }
8857       else
8858         {
8859           /* Use global functions with ADL.  */
8860           VEC(tree,gc) *vec;
8861           vec = make_tree_vector ();
8862
8863           VEC_safe_push (tree, gc, vec, range);
8864
8865           member_begin = perform_koenig_lookup (id_begin, vec,
8866                                                 /*include_std=*/true,
8867                                                 tf_warning_or_error);
8868           *begin = finish_call_expr (member_begin, &vec, false, true,
8869                                      tf_warning_or_error);
8870           member_end = perform_koenig_lookup (id_end, vec,
8871                                               /*include_std=*/true,
8872                                               tf_warning_or_error);
8873           *end = finish_call_expr (member_end, &vec, false, true,
8874                                    tf_warning_or_error);
8875
8876           release_tree_vector (vec);
8877         }
8878
8879       /* Last common checks.  */
8880       if (*begin == error_mark_node || *end == error_mark_node)
8881         {
8882           /* If one of the expressions is an error do no more checks.  */
8883           *begin = *end = error_mark_node;
8884           return error_mark_node;
8885         }
8886       else
8887         {
8888           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8889           /* The unqualified type of the __begin and __end temporaries should
8890              be the same, as required by the multiple auto declaration.  */
8891           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8892             error ("inconsistent begin/end types in range-based %<for%> "
8893                    "statement: %qT and %qT",
8894                    TREE_TYPE (*begin), TREE_TYPE (*end));
8895           return iter_type;
8896         }
8897     }
8898 }
8899
8900 /* Helper function for cp_parser_perform_range_for_lookup.
8901    Builds a tree for RANGE.IDENTIFIER().  */
8902
8903 static tree
8904 cp_parser_range_for_member_function (tree range, tree identifier)
8905 {
8906   tree member, res;
8907   VEC(tree,gc) *vec;
8908
8909   member = finish_class_member_access_expr (range, identifier,
8910                                             false, tf_warning_or_error);
8911   if (member == error_mark_node)
8912     return error_mark_node;
8913
8914   vec = make_tree_vector ();
8915   res = finish_call_expr (member, &vec,
8916                           /*disallow_virtual=*/false,
8917                           /*koenig_p=*/false,
8918                           tf_warning_or_error);
8919   release_tree_vector (vec);
8920   return res;
8921 }
8922
8923 /* Parse an iteration-statement.
8924
8925    iteration-statement:
8926      while ( condition ) statement
8927      do statement while ( expression ) ;
8928      for ( for-init-statement condition [opt] ; expression [opt] )
8929        statement
8930
8931    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8932
8933 static tree
8934 cp_parser_iteration_statement (cp_parser* parser)
8935 {
8936   cp_token *token;
8937   enum rid keyword;
8938   tree statement;
8939   unsigned char in_statement;
8940
8941   /* Peek at the next token.  */
8942   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8943   if (!token)
8944     return error_mark_node;
8945
8946   /* Remember whether or not we are already within an iteration
8947      statement.  */
8948   in_statement = parser->in_statement;
8949
8950   /* See what kind of keyword it is.  */
8951   keyword = token->keyword;
8952   switch (keyword)
8953     {
8954     case RID_WHILE:
8955       {
8956         tree condition;
8957
8958         /* Begin the while-statement.  */
8959         statement = begin_while_stmt ();
8960         /* Look for the `('.  */
8961         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8962         /* Parse the condition.  */
8963         condition = cp_parser_condition (parser);
8964         finish_while_stmt_cond (condition, statement);
8965         /* Look for the `)'.  */
8966         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8967         /* Parse the dependent statement.  */
8968         parser->in_statement = IN_ITERATION_STMT;
8969         cp_parser_already_scoped_statement (parser);
8970         parser->in_statement = in_statement;
8971         /* We're done with the while-statement.  */
8972         finish_while_stmt (statement);
8973       }
8974       break;
8975
8976     case RID_DO:
8977       {
8978         tree expression;
8979
8980         /* Begin the do-statement.  */
8981         statement = begin_do_stmt ();
8982         /* Parse the body of the do-statement.  */
8983         parser->in_statement = IN_ITERATION_STMT;
8984         cp_parser_implicitly_scoped_statement (parser, NULL);
8985         parser->in_statement = in_statement;
8986         finish_do_body (statement);
8987         /* Look for the `while' keyword.  */
8988         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8989         /* Look for the `('.  */
8990         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8991         /* Parse the expression.  */
8992         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8993         /* We're done with the do-statement.  */
8994         finish_do_stmt (expression, statement);
8995         /* Look for the `)'.  */
8996         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8997         /* Look for the `;'.  */
8998         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8999       }
9000       break;
9001
9002     case RID_FOR:
9003       {
9004         /* Look for the `('.  */
9005         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9006
9007         statement = cp_parser_for (parser);
9008
9009         /* Look for the `)'.  */
9010         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9011
9012         /* Parse the body of the for-statement.  */
9013         parser->in_statement = IN_ITERATION_STMT;
9014         cp_parser_already_scoped_statement (parser);
9015         parser->in_statement = in_statement;
9016
9017         /* We're done with the for-statement.  */
9018         finish_for_stmt (statement);
9019       }
9020       break;
9021
9022     default:
9023       cp_parser_error (parser, "expected iteration-statement");
9024       statement = error_mark_node;
9025       break;
9026     }
9027
9028   return statement;
9029 }
9030
9031 /* Parse a for-init-statement or the declarator of a range-based-for.
9032    Returns true if a range-based-for declaration is seen.
9033
9034    for-init-statement:
9035      expression-statement
9036      simple-declaration  */
9037
9038 static bool
9039 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9040 {
9041   /* If the next token is a `;', then we have an empty
9042      expression-statement.  Grammatically, this is also a
9043      simple-declaration, but an invalid one, because it does not
9044      declare anything.  Therefore, if we did not handle this case
9045      specially, we would issue an error message about an invalid
9046      declaration.  */
9047   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9048     {
9049       bool is_range_for = false;
9050       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9051
9052       parser->colon_corrects_to_scope_p = false;
9053
9054       /* We're going to speculatively look for a declaration, falling back
9055          to an expression, if necessary.  */
9056       cp_parser_parse_tentatively (parser);
9057       /* Parse the declaration.  */
9058       cp_parser_simple_declaration (parser,
9059                                     /*function_definition_allowed_p=*/false,
9060                                     decl);
9061       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9062       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9063         {
9064           /* It is a range-for, consume the ':' */
9065           cp_lexer_consume_token (parser->lexer);
9066           is_range_for = true;
9067           if (cxx_dialect < cxx0x)
9068             {
9069               error_at (cp_lexer_peek_token (parser->lexer)->location,
9070                         "range-based %<for%> loops are not allowed "
9071                         "in C++98 mode");
9072               *decl = error_mark_node;
9073             }
9074         }
9075       else
9076           /* The ';' is not consumed yet because we told
9077              cp_parser_simple_declaration not to.  */
9078           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9079
9080       if (cp_parser_parse_definitely (parser))
9081         return is_range_for;
9082       /* If the tentative parse failed, then we shall need to look for an
9083          expression-statement.  */
9084     }
9085   /* If we are here, it is an expression-statement.  */
9086   cp_parser_expression_statement (parser, NULL_TREE);
9087   return false;
9088 }
9089
9090 /* Parse a jump-statement.
9091
9092    jump-statement:
9093      break ;
9094      continue ;
9095      return expression [opt] ;
9096      return braced-init-list ;
9097      goto identifier ;
9098
9099    GNU extension:
9100
9101    jump-statement:
9102      goto * expression ;
9103
9104    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9105
9106 static tree
9107 cp_parser_jump_statement (cp_parser* parser)
9108 {
9109   tree statement = error_mark_node;
9110   cp_token *token;
9111   enum rid keyword;
9112   unsigned char in_statement;
9113
9114   /* Peek at the next token.  */
9115   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9116   if (!token)
9117     return error_mark_node;
9118
9119   /* See what kind of keyword it is.  */
9120   keyword = token->keyword;
9121   switch (keyword)
9122     {
9123     case RID_BREAK:
9124       in_statement = parser->in_statement & ~IN_IF_STMT;      
9125       switch (in_statement)
9126         {
9127         case 0:
9128           error_at (token->location, "break statement not within loop or switch");
9129           break;
9130         default:
9131           gcc_assert ((in_statement & IN_SWITCH_STMT)
9132                       || in_statement == IN_ITERATION_STMT);
9133           statement = finish_break_stmt ();
9134           break;
9135         case IN_OMP_BLOCK:
9136           error_at (token->location, "invalid exit from OpenMP structured block");
9137           break;
9138         case IN_OMP_FOR:
9139           error_at (token->location, "break statement used with OpenMP for loop");
9140           break;
9141         }
9142       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9143       break;
9144
9145     case RID_CONTINUE:
9146       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9147         {
9148         case 0:
9149           error_at (token->location, "continue statement not within a loop");
9150           break;
9151         case IN_ITERATION_STMT:
9152         case IN_OMP_FOR:
9153           statement = finish_continue_stmt ();
9154           break;
9155         case IN_OMP_BLOCK:
9156           error_at (token->location, "invalid exit from OpenMP structured block");
9157           break;
9158         default:
9159           gcc_unreachable ();
9160         }
9161       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9162       break;
9163
9164     case RID_RETURN:
9165       {
9166         tree expr;
9167         bool expr_non_constant_p;
9168
9169         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9170           {
9171             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9172             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9173           }
9174         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9175           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9176         else
9177           /* If the next token is a `;', then there is no
9178              expression.  */
9179           expr = NULL_TREE;
9180         /* Build the return-statement.  */
9181         statement = finish_return_stmt (expr);
9182         /* Look for the final `;'.  */
9183         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9184       }
9185       break;
9186
9187     case RID_GOTO:
9188       /* Create the goto-statement.  */
9189       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9190         {
9191           /* Issue a warning about this use of a GNU extension.  */
9192           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9193           /* Consume the '*' token.  */
9194           cp_lexer_consume_token (parser->lexer);
9195           /* Parse the dependent expression.  */
9196           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9197         }
9198       else
9199         finish_goto_stmt (cp_parser_identifier (parser));
9200       /* Look for the final `;'.  */
9201       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9202       break;
9203
9204     default:
9205       cp_parser_error (parser, "expected jump-statement");
9206       break;
9207     }
9208
9209   return statement;
9210 }
9211
9212 /* Parse a declaration-statement.
9213
9214    declaration-statement:
9215      block-declaration  */
9216
9217 static void
9218 cp_parser_declaration_statement (cp_parser* parser)
9219 {
9220   void *p;
9221
9222   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9223   p = obstack_alloc (&declarator_obstack, 0);
9224
9225  /* Parse the block-declaration.  */
9226   cp_parser_block_declaration (parser, /*statement_p=*/true);
9227
9228   /* Free any declarators allocated.  */
9229   obstack_free (&declarator_obstack, p);
9230
9231   /* Finish off the statement.  */
9232   finish_stmt ();
9233 }
9234
9235 /* Some dependent statements (like `if (cond) statement'), are
9236    implicitly in their own scope.  In other words, if the statement is
9237    a single statement (as opposed to a compound-statement), it is
9238    none-the-less treated as if it were enclosed in braces.  Any
9239    declarations appearing in the dependent statement are out of scope
9240    after control passes that point.  This function parses a statement,
9241    but ensures that is in its own scope, even if it is not a
9242    compound-statement.
9243
9244    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9245    is a (possibly labeled) if statement which is not enclosed in
9246    braces and has an else clause.  This is used to implement
9247    -Wparentheses.
9248
9249    Returns the new statement.  */
9250
9251 static tree
9252 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9253 {
9254   tree statement;
9255
9256   if (if_p != NULL)
9257     *if_p = false;
9258
9259   /* Mark if () ; with a special NOP_EXPR.  */
9260   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9261     {
9262       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9263       cp_lexer_consume_token (parser->lexer);
9264       statement = add_stmt (build_empty_stmt (loc));
9265     }
9266   /* if a compound is opened, we simply parse the statement directly.  */
9267   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9268     statement = cp_parser_compound_statement (parser, NULL, false, false);
9269   /* If the token is not a `{', then we must take special action.  */
9270   else
9271     {
9272       /* Create a compound-statement.  */
9273       statement = begin_compound_stmt (0);
9274       /* Parse the dependent-statement.  */
9275       cp_parser_statement (parser, NULL_TREE, false, if_p);
9276       /* Finish the dummy compound-statement.  */
9277       finish_compound_stmt (statement);
9278     }
9279
9280   /* Return the statement.  */
9281   return statement;
9282 }
9283
9284 /* For some dependent statements (like `while (cond) statement'), we
9285    have already created a scope.  Therefore, even if the dependent
9286    statement is a compound-statement, we do not want to create another
9287    scope.  */
9288
9289 static void
9290 cp_parser_already_scoped_statement (cp_parser* parser)
9291 {
9292   /* If the token is a `{', then we must take special action.  */
9293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9294     cp_parser_statement (parser, NULL_TREE, false, NULL);
9295   else
9296     {
9297       /* Avoid calling cp_parser_compound_statement, so that we
9298          don't create a new scope.  Do everything else by hand.  */
9299       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9300       /* If the next keyword is `__label__' we have a label declaration.  */
9301       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9302         cp_parser_label_declaration (parser);
9303       /* Parse an (optional) statement-seq.  */
9304       cp_parser_statement_seq_opt (parser, NULL_TREE);
9305       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9306     }
9307 }
9308
9309 /* Declarations [gram.dcl.dcl] */
9310
9311 /* Parse an optional declaration-sequence.
9312
9313    declaration-seq:
9314      declaration
9315      declaration-seq declaration  */
9316
9317 static void
9318 cp_parser_declaration_seq_opt (cp_parser* parser)
9319 {
9320   while (true)
9321     {
9322       cp_token *token;
9323
9324       token = cp_lexer_peek_token (parser->lexer);
9325
9326       if (token->type == CPP_CLOSE_BRACE
9327           || token->type == CPP_EOF
9328           || token->type == CPP_PRAGMA_EOL)
9329         break;
9330
9331       if (token->type == CPP_SEMICOLON)
9332         {
9333           /* A declaration consisting of a single semicolon is
9334              invalid.  Allow it unless we're being pedantic.  */
9335           cp_lexer_consume_token (parser->lexer);
9336           if (!in_system_header)
9337             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9338           continue;
9339         }
9340
9341       /* If we're entering or exiting a region that's implicitly
9342          extern "C", modify the lang context appropriately.  */
9343       if (!parser->implicit_extern_c && token->implicit_extern_c)
9344         {
9345           push_lang_context (lang_name_c);
9346           parser->implicit_extern_c = true;
9347         }
9348       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9349         {
9350           pop_lang_context ();
9351           parser->implicit_extern_c = false;
9352         }
9353
9354       if (token->type == CPP_PRAGMA)
9355         {
9356           /* A top-level declaration can consist solely of a #pragma.
9357              A nested declaration cannot, so this is done here and not
9358              in cp_parser_declaration.  (A #pragma at block scope is
9359              handled in cp_parser_statement.)  */
9360           cp_parser_pragma (parser, pragma_external);
9361           continue;
9362         }
9363
9364       /* Parse the declaration itself.  */
9365       cp_parser_declaration (parser);
9366     }
9367 }
9368
9369 /* Parse a declaration.
9370
9371    declaration:
9372      block-declaration
9373      function-definition
9374      template-declaration
9375      explicit-instantiation
9376      explicit-specialization
9377      linkage-specification
9378      namespace-definition
9379
9380    GNU extension:
9381
9382    declaration:
9383       __extension__ declaration */
9384
9385 static void
9386 cp_parser_declaration (cp_parser* parser)
9387 {
9388   cp_token token1;
9389   cp_token token2;
9390   int saved_pedantic;
9391   void *p;
9392   tree attributes = NULL_TREE;
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_declaration (parser);
9399       /* Restore the PEDANTIC flag.  */
9400       pedantic = saved_pedantic;
9401
9402       return;
9403     }
9404
9405   /* Try to figure out what kind of declaration is present.  */
9406   token1 = *cp_lexer_peek_token (parser->lexer);
9407
9408   if (token1.type != CPP_EOF)
9409     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9410   else
9411     {
9412       token2.type = CPP_EOF;
9413       token2.keyword = RID_MAX;
9414     }
9415
9416   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9417   p = obstack_alloc (&declarator_obstack, 0);
9418
9419   /* If the next token is `extern' and the following token is a string
9420      literal, then we have a linkage specification.  */
9421   if (token1.keyword == RID_EXTERN
9422       && cp_parser_is_string_literal (&token2))
9423     cp_parser_linkage_specification (parser);
9424   /* If the next token is `template', then we have either a template
9425      declaration, an explicit instantiation, or an explicit
9426      specialization.  */
9427   else if (token1.keyword == RID_TEMPLATE)
9428     {
9429       /* `template <>' indicates a template specialization.  */
9430       if (token2.type == CPP_LESS
9431           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9432         cp_parser_explicit_specialization (parser);
9433       /* `template <' indicates a template declaration.  */
9434       else if (token2.type == CPP_LESS)
9435         cp_parser_template_declaration (parser, /*member_p=*/false);
9436       /* Anything else must be an explicit instantiation.  */
9437       else
9438         cp_parser_explicit_instantiation (parser);
9439     }
9440   /* If the next token is `export', then we have a template
9441      declaration.  */
9442   else if (token1.keyword == RID_EXPORT)
9443     cp_parser_template_declaration (parser, /*member_p=*/false);
9444   /* If the next token is `extern', 'static' or 'inline' and the one
9445      after that is `template', we have a GNU extended explicit
9446      instantiation directive.  */
9447   else if (cp_parser_allow_gnu_extensions_p (parser)
9448            && (token1.keyword == RID_EXTERN
9449                || token1.keyword == RID_STATIC
9450                || token1.keyword == RID_INLINE)
9451            && token2.keyword == RID_TEMPLATE)
9452     cp_parser_explicit_instantiation (parser);
9453   /* If the next token is `namespace', check for a named or unnamed
9454      namespace definition.  */
9455   else if (token1.keyword == RID_NAMESPACE
9456            && (/* A named namespace definition.  */
9457                (token2.type == CPP_NAME
9458                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9459                     != CPP_EQ))
9460                /* An unnamed namespace definition.  */
9461                || token2.type == CPP_OPEN_BRACE
9462                || token2.keyword == RID_ATTRIBUTE))
9463     cp_parser_namespace_definition (parser);
9464   /* An inline (associated) namespace definition.  */
9465   else if (token1.keyword == RID_INLINE
9466            && token2.keyword == RID_NAMESPACE)
9467     cp_parser_namespace_definition (parser);
9468   /* Objective-C++ declaration/definition.  */
9469   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9470     cp_parser_objc_declaration (parser, NULL_TREE);
9471   else if (c_dialect_objc ()
9472            && token1.keyword == RID_ATTRIBUTE
9473            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9474     cp_parser_objc_declaration (parser, attributes);
9475   /* We must have either a block declaration or a function
9476      definition.  */
9477   else
9478     /* Try to parse a block-declaration, or a function-definition.  */
9479     cp_parser_block_declaration (parser, /*statement_p=*/false);
9480
9481   /* Free any declarators allocated.  */
9482   obstack_free (&declarator_obstack, p);
9483 }
9484
9485 /* Parse a block-declaration.
9486
9487    block-declaration:
9488      simple-declaration
9489      asm-definition
9490      namespace-alias-definition
9491      using-declaration
9492      using-directive
9493
9494    GNU Extension:
9495
9496    block-declaration:
9497      __extension__ block-declaration
9498
9499    C++0x Extension:
9500
9501    block-declaration:
9502      static_assert-declaration
9503
9504    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9505    part of a declaration-statement.  */
9506
9507 static void
9508 cp_parser_block_declaration (cp_parser *parser,
9509                              bool      statement_p)
9510 {
9511   cp_token *token1;
9512   int saved_pedantic;
9513
9514   /* Check for the `__extension__' keyword.  */
9515   if (cp_parser_extension_opt (parser, &saved_pedantic))
9516     {
9517       /* Parse the qualified declaration.  */
9518       cp_parser_block_declaration (parser, statement_p);
9519       /* Restore the PEDANTIC flag.  */
9520       pedantic = saved_pedantic;
9521
9522       return;
9523     }
9524
9525   /* Peek at the next token to figure out which kind of declaration is
9526      present.  */
9527   token1 = cp_lexer_peek_token (parser->lexer);
9528
9529   /* If the next keyword is `asm', we have an asm-definition.  */
9530   if (token1->keyword == RID_ASM)
9531     {
9532       if (statement_p)
9533         cp_parser_commit_to_tentative_parse (parser);
9534       cp_parser_asm_definition (parser);
9535     }
9536   /* If the next keyword is `namespace', we have a
9537      namespace-alias-definition.  */
9538   else if (token1->keyword == RID_NAMESPACE)
9539     cp_parser_namespace_alias_definition (parser);
9540   /* If the next keyword is `using', we have either a
9541      using-declaration or a using-directive.  */
9542   else if (token1->keyword == RID_USING)
9543     {
9544       cp_token *token2;
9545
9546       if (statement_p)
9547         cp_parser_commit_to_tentative_parse (parser);
9548       /* If the token after `using' is `namespace', then we have a
9549          using-directive.  */
9550       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9551       if (token2->keyword == RID_NAMESPACE)
9552         cp_parser_using_directive (parser);
9553       /* Otherwise, it's a using-declaration.  */
9554       else
9555         cp_parser_using_declaration (parser,
9556                                      /*access_declaration_p=*/false);
9557     }
9558   /* If the next keyword is `__label__' we have a misplaced label
9559      declaration.  */
9560   else if (token1->keyword == RID_LABEL)
9561     {
9562       cp_lexer_consume_token (parser->lexer);
9563       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9564       cp_parser_skip_to_end_of_statement (parser);
9565       /* If the next token is now a `;', consume it.  */
9566       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9567         cp_lexer_consume_token (parser->lexer);
9568     }
9569   /* If the next token is `static_assert' we have a static assertion.  */
9570   else if (token1->keyword == RID_STATIC_ASSERT)
9571     cp_parser_static_assert (parser, /*member_p=*/false);
9572   /* Anything else must be a simple-declaration.  */
9573   else
9574     cp_parser_simple_declaration (parser, !statement_p,
9575                                   /*maybe_range_for_decl*/NULL);
9576 }
9577
9578 /* Parse a simple-declaration.
9579
9580    simple-declaration:
9581      decl-specifier-seq [opt] init-declarator-list [opt] ;
9582
9583    init-declarator-list:
9584      init-declarator
9585      init-declarator-list , init-declarator
9586
9587    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9588    function-definition as a simple-declaration.
9589
9590    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9591    parsed declaration if it is an uninitialized single declarator not followed
9592    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9593    if present, will not be consumed.  */
9594
9595 static void
9596 cp_parser_simple_declaration (cp_parser* parser,
9597                               bool function_definition_allowed_p,
9598                               tree *maybe_range_for_decl)
9599 {
9600   cp_decl_specifier_seq decl_specifiers;
9601   int declares_class_or_enum;
9602   bool saw_declarator;
9603
9604   if (maybe_range_for_decl)
9605     *maybe_range_for_decl = NULL_TREE;
9606
9607   /* Defer access checks until we know what is being declared; the
9608      checks for names appearing in the decl-specifier-seq should be
9609      done as if we were in the scope of the thing being declared.  */
9610   push_deferring_access_checks (dk_deferred);
9611
9612   /* Parse the decl-specifier-seq.  We have to keep track of whether
9613      or not the decl-specifier-seq declares a named class or
9614      enumeration type, since that is the only case in which the
9615      init-declarator-list is allowed to be empty.
9616
9617      [dcl.dcl]
9618
9619      In a simple-declaration, the optional init-declarator-list can be
9620      omitted only when declaring a class or enumeration, that is when
9621      the decl-specifier-seq contains either a class-specifier, an
9622      elaborated-type-specifier, or an enum-specifier.  */
9623   cp_parser_decl_specifier_seq (parser,
9624                                 CP_PARSER_FLAGS_OPTIONAL,
9625                                 &decl_specifiers,
9626                                 &declares_class_or_enum);
9627   /* We no longer need to defer access checks.  */
9628   stop_deferring_access_checks ();
9629
9630   /* In a block scope, a valid declaration must always have a
9631      decl-specifier-seq.  By not trying to parse declarators, we can
9632      resolve the declaration/expression ambiguity more quickly.  */
9633   if (!function_definition_allowed_p
9634       && !decl_specifiers.any_specifiers_p)
9635     {
9636       cp_parser_error (parser, "expected declaration");
9637       goto done;
9638     }
9639
9640   /* If the next two tokens are both identifiers, the code is
9641      erroneous. The usual cause of this situation is code like:
9642
9643        T t;
9644
9645      where "T" should name a type -- but does not.  */
9646   if (!decl_specifiers.any_type_specifiers_p
9647       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9648     {
9649       /* If parsing tentatively, we should commit; we really are
9650          looking at a declaration.  */
9651       cp_parser_commit_to_tentative_parse (parser);
9652       /* Give up.  */
9653       goto done;
9654     }
9655
9656   /* If we have seen at least one decl-specifier, and the next token
9657      is not a parenthesis, then we must be looking at a declaration.
9658      (After "int (" we might be looking at a functional cast.)  */
9659   if (decl_specifiers.any_specifiers_p
9660       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9661       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9662       && !cp_parser_error_occurred (parser))
9663     cp_parser_commit_to_tentative_parse (parser);
9664
9665   /* Keep going until we hit the `;' at the end of the simple
9666      declaration.  */
9667   saw_declarator = false;
9668   while (cp_lexer_next_token_is_not (parser->lexer,
9669                                      CPP_SEMICOLON))
9670     {
9671       cp_token *token;
9672       bool function_definition_p;
9673       tree decl;
9674
9675       if (saw_declarator)
9676         {
9677           /* If we are processing next declarator, coma is expected */
9678           token = cp_lexer_peek_token (parser->lexer);
9679           gcc_assert (token->type == CPP_COMMA);
9680           cp_lexer_consume_token (parser->lexer);
9681           if (maybe_range_for_decl)
9682             *maybe_range_for_decl = error_mark_node;
9683         }
9684       else
9685         saw_declarator = true;
9686
9687       /* Parse the init-declarator.  */
9688       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9689                                         /*checks=*/NULL,
9690                                         function_definition_allowed_p,
9691                                         /*member_p=*/false,
9692                                         declares_class_or_enum,
9693                                         &function_definition_p,
9694                                         maybe_range_for_decl);
9695       /* If an error occurred while parsing tentatively, exit quickly.
9696          (That usually happens when in the body of a function; each
9697          statement is treated as a declaration-statement until proven
9698          otherwise.)  */
9699       if (cp_parser_error_occurred (parser))
9700         goto done;
9701       /* Handle function definitions specially.  */
9702       if (function_definition_p)
9703         {
9704           /* If the next token is a `,', then we are probably
9705              processing something like:
9706
9707                void f() {}, *p;
9708
9709              which is erroneous.  */
9710           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9711             {
9712               cp_token *token = cp_lexer_peek_token (parser->lexer);
9713               error_at (token->location,
9714                         "mixing"
9715                         " declarations and function-definitions is forbidden");
9716             }
9717           /* Otherwise, we're done with the list of declarators.  */
9718           else
9719             {
9720               pop_deferring_access_checks ();
9721               return;
9722             }
9723         }
9724       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9725         *maybe_range_for_decl = decl;
9726       /* The next token should be either a `,' or a `;'.  */
9727       token = cp_lexer_peek_token (parser->lexer);
9728       /* If it's a `,', there are more declarators to come.  */
9729       if (token->type == CPP_COMMA)
9730         /* will be consumed next time around */;
9731       /* If it's a `;', we are done.  */
9732       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9733         break;
9734       /* Anything else is an error.  */
9735       else
9736         {
9737           /* If we have already issued an error message we don't need
9738              to issue another one.  */
9739           if (decl != error_mark_node
9740               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9741             cp_parser_error (parser, "expected %<,%> or %<;%>");
9742           /* Skip tokens until we reach the end of the statement.  */
9743           cp_parser_skip_to_end_of_statement (parser);
9744           /* If the next token is now a `;', consume it.  */
9745           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9746             cp_lexer_consume_token (parser->lexer);
9747           goto done;
9748         }
9749       /* After the first time around, a function-definition is not
9750          allowed -- even if it was OK at first.  For example:
9751
9752            int i, f() {}
9753
9754          is not valid.  */
9755       function_definition_allowed_p = false;
9756     }
9757
9758   /* Issue an error message if no declarators are present, and the
9759      decl-specifier-seq does not itself declare a class or
9760      enumeration.  */
9761   if (!saw_declarator)
9762     {
9763       if (cp_parser_declares_only_class_p (parser))
9764         shadow_tag (&decl_specifiers);
9765       /* Perform any deferred access checks.  */
9766       perform_deferred_access_checks ();
9767     }
9768
9769   /* Consume the `;'.  */
9770   if (!maybe_range_for_decl)
9771       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9772
9773  done:
9774   pop_deferring_access_checks ();
9775 }
9776
9777 /* Parse a decl-specifier-seq.
9778
9779    decl-specifier-seq:
9780      decl-specifier-seq [opt] decl-specifier
9781
9782    decl-specifier:
9783      storage-class-specifier
9784      type-specifier
9785      function-specifier
9786      friend
9787      typedef
9788
9789    GNU Extension:
9790
9791    decl-specifier:
9792      attributes
9793
9794    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9795
9796    The parser flags FLAGS is used to control type-specifier parsing.
9797
9798    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9799    flags:
9800
9801      1: one of the decl-specifiers is an elaborated-type-specifier
9802         (i.e., a type declaration)
9803      2: one of the decl-specifiers is an enum-specifier or a
9804         class-specifier (i.e., a type definition)
9805
9806    */
9807
9808 static void
9809 cp_parser_decl_specifier_seq (cp_parser* parser,
9810                               cp_parser_flags flags,
9811                               cp_decl_specifier_seq *decl_specs,
9812                               int* declares_class_or_enum)
9813 {
9814   bool constructor_possible_p = !parser->in_declarator_p;
9815   cp_token *start_token = NULL;
9816
9817   /* Clear DECL_SPECS.  */
9818   clear_decl_specs (decl_specs);
9819
9820   /* Assume no class or enumeration type is declared.  */
9821   *declares_class_or_enum = 0;
9822
9823   /* Keep reading specifiers until there are no more to read.  */
9824   while (true)
9825     {
9826       bool constructor_p;
9827       bool found_decl_spec;
9828       cp_token *token;
9829
9830       /* Peek at the next token.  */
9831       token = cp_lexer_peek_token (parser->lexer);
9832
9833       /* Save the first token of the decl spec list for error
9834          reporting.  */
9835       if (!start_token)
9836         start_token = token;
9837       /* Handle attributes.  */
9838       if (token->keyword == RID_ATTRIBUTE)
9839         {
9840           /* Parse the attributes.  */
9841           decl_specs->attributes
9842             = chainon (decl_specs->attributes,
9843                        cp_parser_attributes_opt (parser));
9844           continue;
9845         }
9846       /* Assume we will find a decl-specifier keyword.  */
9847       found_decl_spec = true;
9848       /* If the next token is an appropriate keyword, we can simply
9849          add it to the list.  */
9850       switch (token->keyword)
9851         {
9852           /* decl-specifier:
9853                friend
9854                constexpr */
9855         case RID_FRIEND:
9856           if (!at_class_scope_p ())
9857             {
9858               error_at (token->location, "%<friend%> used outside of class");
9859               cp_lexer_purge_token (parser->lexer);
9860             }
9861           else
9862             {
9863               ++decl_specs->specs[(int) ds_friend];
9864               /* Consume the token.  */
9865               cp_lexer_consume_token (parser->lexer);
9866             }
9867           break;
9868
9869         case RID_CONSTEXPR:
9870           ++decl_specs->specs[(int) ds_constexpr];
9871           cp_lexer_consume_token (parser->lexer);
9872           break;
9873
9874           /* function-specifier:
9875                inline
9876                virtual
9877                explicit  */
9878         case RID_INLINE:
9879         case RID_VIRTUAL:
9880         case RID_EXPLICIT:
9881           cp_parser_function_specifier_opt (parser, decl_specs);
9882           break;
9883
9884           /* decl-specifier:
9885                typedef  */
9886         case RID_TYPEDEF:
9887           ++decl_specs->specs[(int) ds_typedef];
9888           /* Consume the token.  */
9889           cp_lexer_consume_token (parser->lexer);
9890           /* A constructor declarator cannot appear in a typedef.  */
9891           constructor_possible_p = false;
9892           /* The "typedef" keyword can only occur in a declaration; we
9893              may as well commit at this point.  */
9894           cp_parser_commit_to_tentative_parse (parser);
9895
9896           if (decl_specs->storage_class != sc_none)
9897             decl_specs->conflicting_specifiers_p = true;
9898           break;
9899
9900           /* storage-class-specifier:
9901                auto
9902                register
9903                static
9904                extern
9905                mutable
9906
9907              GNU Extension:
9908                thread  */
9909         case RID_AUTO:
9910           if (cxx_dialect == cxx98) 
9911             {
9912               /* Consume the token.  */
9913               cp_lexer_consume_token (parser->lexer);
9914
9915               /* Complain about `auto' as a storage specifier, if
9916                  we're complaining about C++0x compatibility.  */
9917               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9918                           " will change meaning in C++0x; please remove it");
9919
9920               /* Set the storage class anyway.  */
9921               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9922                                            token->location);
9923             }
9924           else
9925             /* C++0x auto type-specifier.  */
9926             found_decl_spec = false;
9927           break;
9928
9929         case RID_REGISTER:
9930         case RID_STATIC:
9931         case RID_EXTERN:
9932         case RID_MUTABLE:
9933           /* Consume the token.  */
9934           cp_lexer_consume_token (parser->lexer);
9935           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9936                                        token->location);
9937           break;
9938         case RID_THREAD:
9939           /* Consume the token.  */
9940           cp_lexer_consume_token (parser->lexer);
9941           ++decl_specs->specs[(int) ds_thread];
9942           break;
9943
9944         default:
9945           /* We did not yet find a decl-specifier yet.  */
9946           found_decl_spec = false;
9947           break;
9948         }
9949
9950       if (found_decl_spec
9951           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9952           && token->keyword != RID_CONSTEXPR)
9953         error ("decl-specifier invalid in condition");
9954
9955       /* Constructors are a special case.  The `S' in `S()' is not a
9956          decl-specifier; it is the beginning of the declarator.  */
9957       constructor_p
9958         = (!found_decl_spec
9959            && constructor_possible_p
9960            && (cp_parser_constructor_declarator_p
9961                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9962
9963       /* If we don't have a DECL_SPEC yet, then we must be looking at
9964          a type-specifier.  */
9965       if (!found_decl_spec && !constructor_p)
9966         {
9967           int decl_spec_declares_class_or_enum;
9968           bool is_cv_qualifier;
9969           tree type_spec;
9970
9971           type_spec
9972             = cp_parser_type_specifier (parser, flags,
9973                                         decl_specs,
9974                                         /*is_declaration=*/true,
9975                                         &decl_spec_declares_class_or_enum,
9976                                         &is_cv_qualifier);
9977           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9978
9979           /* If this type-specifier referenced a user-defined type
9980              (a typedef, class-name, etc.), then we can't allow any
9981              more such type-specifiers henceforth.
9982
9983              [dcl.spec]
9984
9985              The longest sequence of decl-specifiers that could
9986              possibly be a type name is taken as the
9987              decl-specifier-seq of a declaration.  The sequence shall
9988              be self-consistent as described below.
9989
9990              [dcl.type]
9991
9992              As a general rule, at most one type-specifier is allowed
9993              in the complete decl-specifier-seq of a declaration.  The
9994              only exceptions are the following:
9995
9996              -- const or volatile can be combined with any other
9997                 type-specifier.
9998
9999              -- signed or unsigned can be combined with char, long,
10000                 short, or int.
10001
10002              -- ..
10003
10004              Example:
10005
10006                typedef char* Pc;
10007                void g (const int Pc);
10008
10009              Here, Pc is *not* part of the decl-specifier seq; it's
10010              the declarator.  Therefore, once we see a type-specifier
10011              (other than a cv-qualifier), we forbid any additional
10012              user-defined types.  We *do* still allow things like `int
10013              int' to be considered a decl-specifier-seq, and issue the
10014              error message later.  */
10015           if (type_spec && !is_cv_qualifier)
10016             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10017           /* A constructor declarator cannot follow a type-specifier.  */
10018           if (type_spec)
10019             {
10020               constructor_possible_p = false;
10021               found_decl_spec = true;
10022               if (!is_cv_qualifier)
10023                 decl_specs->any_type_specifiers_p = true;
10024             }
10025         }
10026
10027       /* If we still do not have a DECL_SPEC, then there are no more
10028          decl-specifiers.  */
10029       if (!found_decl_spec)
10030         break;
10031
10032       decl_specs->any_specifiers_p = true;
10033       /* After we see one decl-specifier, further decl-specifiers are
10034          always optional.  */
10035       flags |= CP_PARSER_FLAGS_OPTIONAL;
10036     }
10037
10038   cp_parser_check_decl_spec (decl_specs, start_token->location);
10039
10040   /* Don't allow a friend specifier with a class definition.  */
10041   if (decl_specs->specs[(int) ds_friend] != 0
10042       && (*declares_class_or_enum & 2))
10043     error_at (start_token->location,
10044               "class definition may not be declared a friend");
10045 }
10046
10047 /* Parse an (optional) storage-class-specifier.
10048
10049    storage-class-specifier:
10050      auto
10051      register
10052      static
10053      extern
10054      mutable
10055
10056    GNU Extension:
10057
10058    storage-class-specifier:
10059      thread
10060
10061    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10062
10063 static tree
10064 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10065 {
10066   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10067     {
10068     case RID_AUTO:
10069       if (cxx_dialect != cxx98)
10070         return NULL_TREE;
10071       /* Fall through for C++98.  */
10072
10073     case RID_REGISTER:
10074     case RID_STATIC:
10075     case RID_EXTERN:
10076     case RID_MUTABLE:
10077     case RID_THREAD:
10078       /* Consume the token.  */
10079       return cp_lexer_consume_token (parser->lexer)->u.value;
10080
10081     default:
10082       return NULL_TREE;
10083     }
10084 }
10085
10086 /* Parse an (optional) function-specifier.
10087
10088    function-specifier:
10089      inline
10090      virtual
10091      explicit
10092
10093    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10094    Updates DECL_SPECS, if it is non-NULL.  */
10095
10096 static tree
10097 cp_parser_function_specifier_opt (cp_parser* parser,
10098                                   cp_decl_specifier_seq *decl_specs)
10099 {
10100   cp_token *token = cp_lexer_peek_token (parser->lexer);
10101   switch (token->keyword)
10102     {
10103     case RID_INLINE:
10104       if (decl_specs)
10105         ++decl_specs->specs[(int) ds_inline];
10106       break;
10107
10108     case RID_VIRTUAL:
10109       /* 14.5.2.3 [temp.mem]
10110
10111          A member function template shall not be virtual.  */
10112       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10113         error_at (token->location, "templates may not be %<virtual%>");
10114       else if (decl_specs)
10115         ++decl_specs->specs[(int) ds_virtual];
10116       break;
10117
10118     case RID_EXPLICIT:
10119       if (decl_specs)
10120         ++decl_specs->specs[(int) ds_explicit];
10121       break;
10122
10123     default:
10124       return NULL_TREE;
10125     }
10126
10127   /* Consume the token.  */
10128   return cp_lexer_consume_token (parser->lexer)->u.value;
10129 }
10130
10131 /* Parse a linkage-specification.
10132
10133    linkage-specification:
10134      extern string-literal { declaration-seq [opt] }
10135      extern string-literal declaration  */
10136
10137 static void
10138 cp_parser_linkage_specification (cp_parser* parser)
10139 {
10140   tree linkage;
10141
10142   /* Look for the `extern' keyword.  */
10143   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10144
10145   /* Look for the string-literal.  */
10146   linkage = cp_parser_string_literal (parser, false, false);
10147
10148   /* Transform the literal into an identifier.  If the literal is a
10149      wide-character string, or contains embedded NULs, then we can't
10150      handle it as the user wants.  */
10151   if (strlen (TREE_STRING_POINTER (linkage))
10152       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10153     {
10154       cp_parser_error (parser, "invalid linkage-specification");
10155       /* Assume C++ linkage.  */
10156       linkage = lang_name_cplusplus;
10157     }
10158   else
10159     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10160
10161   /* We're now using the new linkage.  */
10162   push_lang_context (linkage);
10163
10164   /* If the next token is a `{', then we're using the first
10165      production.  */
10166   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10167     {
10168       /* Consume the `{' token.  */
10169       cp_lexer_consume_token (parser->lexer);
10170       /* Parse the declarations.  */
10171       cp_parser_declaration_seq_opt (parser);
10172       /* Look for the closing `}'.  */
10173       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10174     }
10175   /* Otherwise, there's just one declaration.  */
10176   else
10177     {
10178       bool saved_in_unbraced_linkage_specification_p;
10179
10180       saved_in_unbraced_linkage_specification_p
10181         = parser->in_unbraced_linkage_specification_p;
10182       parser->in_unbraced_linkage_specification_p = true;
10183       cp_parser_declaration (parser);
10184       parser->in_unbraced_linkage_specification_p
10185         = saved_in_unbraced_linkage_specification_p;
10186     }
10187
10188   /* We're done with the linkage-specification.  */
10189   pop_lang_context ();
10190 }
10191
10192 /* Parse a static_assert-declaration.
10193
10194    static_assert-declaration:
10195      static_assert ( constant-expression , string-literal ) ; 
10196
10197    If MEMBER_P, this static_assert is a class member.  */
10198
10199 static void 
10200 cp_parser_static_assert(cp_parser *parser, bool member_p)
10201 {
10202   tree condition;
10203   tree message;
10204   cp_token *token;
10205   location_t saved_loc;
10206   bool dummy;
10207
10208   /* Peek at the `static_assert' token so we can keep track of exactly
10209      where the static assertion started.  */
10210   token = cp_lexer_peek_token (parser->lexer);
10211   saved_loc = token->location;
10212
10213   /* Look for the `static_assert' keyword.  */
10214   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10215                                   RT_STATIC_ASSERT))
10216     return;
10217
10218   /*  We know we are in a static assertion; commit to any tentative
10219       parse.  */
10220   if (cp_parser_parsing_tentatively (parser))
10221     cp_parser_commit_to_tentative_parse (parser);
10222
10223   /* Parse the `(' starting the static assertion condition.  */
10224   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10225
10226   /* Parse the constant-expression.  Allow a non-constant expression
10227      here in order to give better diagnostics in finish_static_assert.  */
10228   condition = 
10229     cp_parser_constant_expression (parser,
10230                                    /*allow_non_constant_p=*/true,
10231                                    /*non_constant_p=*/&dummy);
10232
10233   /* Parse the separating `,'.  */
10234   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10235
10236   /* Parse the string-literal message.  */
10237   message = cp_parser_string_literal (parser, 
10238                                       /*translate=*/false,
10239                                       /*wide_ok=*/true);
10240
10241   /* A `)' completes the static assertion.  */
10242   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10243     cp_parser_skip_to_closing_parenthesis (parser, 
10244                                            /*recovering=*/true, 
10245                                            /*or_comma=*/false,
10246                                            /*consume_paren=*/true);
10247
10248   /* A semicolon terminates the declaration.  */
10249   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10250
10251   /* Complete the static assertion, which may mean either processing 
10252      the static assert now or saving it for template instantiation.  */
10253   finish_static_assert (condition, message, saved_loc, member_p);
10254 }
10255
10256 /* Parse a `decltype' type. Returns the type. 
10257
10258    simple-type-specifier:
10259      decltype ( expression )  */
10260
10261 static tree
10262 cp_parser_decltype (cp_parser *parser)
10263 {
10264   tree expr;
10265   bool id_expression_or_member_access_p = false;
10266   const char *saved_message;
10267   bool saved_integral_constant_expression_p;
10268   bool saved_non_integral_constant_expression_p;
10269   cp_token *id_expr_start_token;
10270   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10271
10272   if (start_token->type == CPP_DECLTYPE)
10273     {
10274       /* Already parsed.  */
10275       cp_lexer_consume_token (parser->lexer);
10276       return start_token->u.value;
10277     }
10278
10279   /* Look for the `decltype' token.  */
10280   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10281     return error_mark_node;
10282
10283   /* Types cannot be defined in a `decltype' expression.  Save away the
10284      old message.  */
10285   saved_message = parser->type_definition_forbidden_message;
10286
10287   /* And create the new one.  */
10288   parser->type_definition_forbidden_message
10289     = G_("types may not be defined in %<decltype%> expressions");
10290
10291   /* The restrictions on constant-expressions do not apply inside
10292      decltype expressions.  */
10293   saved_integral_constant_expression_p
10294     = parser->integral_constant_expression_p;
10295   saved_non_integral_constant_expression_p
10296     = parser->non_integral_constant_expression_p;
10297   parser->integral_constant_expression_p = false;
10298
10299   /* Do not actually evaluate the expression.  */
10300   ++cp_unevaluated_operand;
10301
10302   /* Do not warn about problems with the expression.  */
10303   ++c_inhibit_evaluation_warnings;
10304
10305   /* Parse the opening `('.  */
10306   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10307     return error_mark_node;
10308   
10309   /* First, try parsing an id-expression.  */
10310   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10311   cp_parser_parse_tentatively (parser);
10312   expr = cp_parser_id_expression (parser,
10313                                   /*template_keyword_p=*/false,
10314                                   /*check_dependency_p=*/true,
10315                                   /*template_p=*/NULL,
10316                                   /*declarator_p=*/false,
10317                                   /*optional_p=*/false);
10318
10319   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10320     {
10321       bool non_integral_constant_expression_p = false;
10322       tree id_expression = expr;
10323       cp_id_kind idk;
10324       const char *error_msg;
10325
10326       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10327         /* Lookup the name we got back from the id-expression.  */
10328         expr = cp_parser_lookup_name (parser, expr,
10329                                       none_type,
10330                                       /*is_template=*/false,
10331                                       /*is_namespace=*/false,
10332                                       /*check_dependency=*/true,
10333                                       /*ambiguous_decls=*/NULL,
10334                                       id_expr_start_token->location);
10335
10336       if (expr
10337           && expr != error_mark_node
10338           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10339           && TREE_CODE (expr) != TYPE_DECL
10340           && (TREE_CODE (expr) != BIT_NOT_EXPR
10341               || !TYPE_P (TREE_OPERAND (expr, 0)))
10342           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10343         {
10344           /* Complete lookup of the id-expression.  */
10345           expr = (finish_id_expression
10346                   (id_expression, expr, parser->scope, &idk,
10347                    /*integral_constant_expression_p=*/false,
10348                    /*allow_non_integral_constant_expression_p=*/true,
10349                    &non_integral_constant_expression_p,
10350                    /*template_p=*/false,
10351                    /*done=*/true,
10352                    /*address_p=*/false,
10353                    /*template_arg_p=*/false,
10354                    &error_msg,
10355                    id_expr_start_token->location));
10356
10357           if (expr == error_mark_node)
10358             /* We found an id-expression, but it was something that we
10359                should not have found. This is an error, not something
10360                we can recover from, so note that we found an
10361                id-expression and we'll recover as gracefully as
10362                possible.  */
10363             id_expression_or_member_access_p = true;
10364         }
10365
10366       if (expr 
10367           && expr != error_mark_node
10368           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10369         /* We have an id-expression.  */
10370         id_expression_or_member_access_p = true;
10371     }
10372
10373   if (!id_expression_or_member_access_p)
10374     {
10375       /* Abort the id-expression parse.  */
10376       cp_parser_abort_tentative_parse (parser);
10377
10378       /* Parsing tentatively, again.  */
10379       cp_parser_parse_tentatively (parser);
10380
10381       /* Parse a class member access.  */
10382       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10383                                            /*cast_p=*/false,
10384                                            /*member_access_only_p=*/true, NULL);
10385
10386       if (expr 
10387           && expr != error_mark_node
10388           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10389         /* We have an id-expression.  */
10390         id_expression_or_member_access_p = true;
10391     }
10392
10393   if (id_expression_or_member_access_p)
10394     /* We have parsed the complete id-expression or member access.  */
10395     cp_parser_parse_definitely (parser);
10396   else
10397     {
10398       bool saved_greater_than_is_operator_p;
10399
10400       /* Abort our attempt to parse an id-expression or member access
10401          expression.  */
10402       cp_parser_abort_tentative_parse (parser);
10403
10404       /* Within a parenthesized expression, a `>' token is always
10405          the greater-than operator.  */
10406       saved_greater_than_is_operator_p
10407         = parser->greater_than_is_operator_p;
10408       parser->greater_than_is_operator_p = true;
10409
10410       /* Parse a full expression.  */
10411       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10412
10413       /* The `>' token might be the end of a template-id or
10414          template-parameter-list now.  */
10415       parser->greater_than_is_operator_p
10416         = saved_greater_than_is_operator_p;
10417     }
10418
10419   /* Go back to evaluating expressions.  */
10420   --cp_unevaluated_operand;
10421   --c_inhibit_evaluation_warnings;
10422
10423   /* Restore the old message and the integral constant expression
10424      flags.  */
10425   parser->type_definition_forbidden_message = saved_message;
10426   parser->integral_constant_expression_p
10427     = saved_integral_constant_expression_p;
10428   parser->non_integral_constant_expression_p
10429     = saved_non_integral_constant_expression_p;
10430
10431   /* Parse to the closing `)'.  */
10432   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10433     {
10434       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10435                                              /*consume_paren=*/true);
10436       return error_mark_node;
10437     }
10438
10439   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
10440                                tf_warning_or_error);
10441
10442   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
10443      it again.  */
10444   start_token->type = CPP_DECLTYPE;
10445   start_token->u.value = expr;
10446   start_token->keyword = RID_MAX;
10447   cp_lexer_purge_tokens_after (parser->lexer, start_token);
10448
10449   return expr;
10450 }
10451
10452 /* Special member functions [gram.special] */
10453
10454 /* Parse a conversion-function-id.
10455
10456    conversion-function-id:
10457      operator conversion-type-id
10458
10459    Returns an IDENTIFIER_NODE representing the operator.  */
10460
10461 static tree
10462 cp_parser_conversion_function_id (cp_parser* parser)
10463 {
10464   tree type;
10465   tree saved_scope;
10466   tree saved_qualifying_scope;
10467   tree saved_object_scope;
10468   tree pushed_scope = NULL_TREE;
10469
10470   /* Look for the `operator' token.  */
10471   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10472     return error_mark_node;
10473   /* When we parse the conversion-type-id, the current scope will be
10474      reset.  However, we need that information in able to look up the
10475      conversion function later, so we save it here.  */
10476   saved_scope = parser->scope;
10477   saved_qualifying_scope = parser->qualifying_scope;
10478   saved_object_scope = parser->object_scope;
10479   /* We must enter the scope of the class so that the names of
10480      entities declared within the class are available in the
10481      conversion-type-id.  For example, consider:
10482
10483        struct S {
10484          typedef int I;
10485          operator I();
10486        };
10487
10488        S::operator I() { ... }
10489
10490      In order to see that `I' is a type-name in the definition, we
10491      must be in the scope of `S'.  */
10492   if (saved_scope)
10493     pushed_scope = push_scope (saved_scope);
10494   /* Parse the conversion-type-id.  */
10495   type = cp_parser_conversion_type_id (parser);
10496   /* Leave the scope of the class, if any.  */
10497   if (pushed_scope)
10498     pop_scope (pushed_scope);
10499   /* Restore the saved scope.  */
10500   parser->scope = saved_scope;
10501   parser->qualifying_scope = saved_qualifying_scope;
10502   parser->object_scope = saved_object_scope;
10503   /* If the TYPE is invalid, indicate failure.  */
10504   if (type == error_mark_node)
10505     return error_mark_node;
10506   return mangle_conv_op_name_for_type (type);
10507 }
10508
10509 /* Parse a conversion-type-id:
10510
10511    conversion-type-id:
10512      type-specifier-seq conversion-declarator [opt]
10513
10514    Returns the TYPE specified.  */
10515
10516 static tree
10517 cp_parser_conversion_type_id (cp_parser* parser)
10518 {
10519   tree attributes;
10520   cp_decl_specifier_seq type_specifiers;
10521   cp_declarator *declarator;
10522   tree type_specified;
10523
10524   /* Parse the attributes.  */
10525   attributes = cp_parser_attributes_opt (parser);
10526   /* Parse the type-specifiers.  */
10527   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10528                                 /*is_trailing_return=*/false,
10529                                 &type_specifiers);
10530   /* If that didn't work, stop.  */
10531   if (type_specifiers.type == error_mark_node)
10532     return error_mark_node;
10533   /* Parse the conversion-declarator.  */
10534   declarator = cp_parser_conversion_declarator_opt (parser);
10535
10536   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10537                                     /*initialized=*/0, &attributes);
10538   if (attributes)
10539     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10540
10541   /* Don't give this error when parsing tentatively.  This happens to
10542      work because we always parse this definitively once.  */
10543   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10544       && type_uses_auto (type_specified))
10545     {
10546       error ("invalid use of %<auto%> in conversion operator");
10547       return error_mark_node;
10548     }
10549
10550   return type_specified;
10551 }
10552
10553 /* Parse an (optional) conversion-declarator.
10554
10555    conversion-declarator:
10556      ptr-operator conversion-declarator [opt]
10557
10558    */
10559
10560 static cp_declarator *
10561 cp_parser_conversion_declarator_opt (cp_parser* parser)
10562 {
10563   enum tree_code code;
10564   tree class_type;
10565   cp_cv_quals cv_quals;
10566
10567   /* We don't know if there's a ptr-operator next, or not.  */
10568   cp_parser_parse_tentatively (parser);
10569   /* Try the ptr-operator.  */
10570   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10571   /* If it worked, look for more conversion-declarators.  */
10572   if (cp_parser_parse_definitely (parser))
10573     {
10574       cp_declarator *declarator;
10575
10576       /* Parse another optional declarator.  */
10577       declarator = cp_parser_conversion_declarator_opt (parser);
10578
10579       return cp_parser_make_indirect_declarator
10580         (code, class_type, cv_quals, declarator);
10581    }
10582
10583   return NULL;
10584 }
10585
10586 /* Parse an (optional) ctor-initializer.
10587
10588    ctor-initializer:
10589      : mem-initializer-list
10590
10591    Returns TRUE iff the ctor-initializer was actually present.  */
10592
10593 static bool
10594 cp_parser_ctor_initializer_opt (cp_parser* parser)
10595 {
10596   /* If the next token is not a `:', then there is no
10597      ctor-initializer.  */
10598   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10599     {
10600       /* Do default initialization of any bases and members.  */
10601       if (DECL_CONSTRUCTOR_P (current_function_decl))
10602         finish_mem_initializers (NULL_TREE);
10603
10604       return false;
10605     }
10606
10607   /* Consume the `:' token.  */
10608   cp_lexer_consume_token (parser->lexer);
10609   /* And the mem-initializer-list.  */
10610   cp_parser_mem_initializer_list (parser);
10611
10612   return true;
10613 }
10614
10615 /* Parse a mem-initializer-list.
10616
10617    mem-initializer-list:
10618      mem-initializer ... [opt]
10619      mem-initializer ... [opt] , mem-initializer-list  */
10620
10621 static void
10622 cp_parser_mem_initializer_list (cp_parser* parser)
10623 {
10624   tree mem_initializer_list = NULL_TREE;
10625   cp_token *token = cp_lexer_peek_token (parser->lexer);
10626
10627   /* Let the semantic analysis code know that we are starting the
10628      mem-initializer-list.  */
10629   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10630     error_at (token->location,
10631               "only constructors take member initializers");
10632
10633   /* Loop through the list.  */
10634   while (true)
10635     {
10636       tree mem_initializer;
10637
10638       token = cp_lexer_peek_token (parser->lexer);
10639       /* Parse the mem-initializer.  */
10640       mem_initializer = cp_parser_mem_initializer (parser);
10641       /* If the next token is a `...', we're expanding member initializers. */
10642       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10643         {
10644           /* Consume the `...'. */
10645           cp_lexer_consume_token (parser->lexer);
10646
10647           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10648              can be expanded but members cannot. */
10649           if (mem_initializer != error_mark_node
10650               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10651             {
10652               error_at (token->location,
10653                         "cannot expand initializer for member %<%D%>",
10654                         TREE_PURPOSE (mem_initializer));
10655               mem_initializer = error_mark_node;
10656             }
10657
10658           /* Construct the pack expansion type. */
10659           if (mem_initializer != error_mark_node)
10660             mem_initializer = make_pack_expansion (mem_initializer);
10661         }
10662       /* Add it to the list, unless it was erroneous.  */
10663       if (mem_initializer != error_mark_node)
10664         {
10665           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10666           mem_initializer_list = mem_initializer;
10667         }
10668       /* If the next token is not a `,', we're done.  */
10669       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10670         break;
10671       /* Consume the `,' token.  */
10672       cp_lexer_consume_token (parser->lexer);
10673     }
10674
10675   /* Perform semantic analysis.  */
10676   if (DECL_CONSTRUCTOR_P (current_function_decl))
10677     finish_mem_initializers (mem_initializer_list);
10678 }
10679
10680 /* Parse a mem-initializer.
10681
10682    mem-initializer:
10683      mem-initializer-id ( expression-list [opt] )
10684      mem-initializer-id braced-init-list
10685
10686    GNU extension:
10687
10688    mem-initializer:
10689      ( expression-list [opt] )
10690
10691    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10692    class) or FIELD_DECL (for a non-static data member) to initialize;
10693    the TREE_VALUE is the expression-list.  An empty initialization
10694    list is represented by void_list_node.  */
10695
10696 static tree
10697 cp_parser_mem_initializer (cp_parser* parser)
10698 {
10699   tree mem_initializer_id;
10700   tree expression_list;
10701   tree member;
10702   cp_token *token = cp_lexer_peek_token (parser->lexer);
10703
10704   /* Find out what is being initialized.  */
10705   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10706     {
10707       permerror (token->location,
10708                  "anachronistic old-style base class initializer");
10709       mem_initializer_id = NULL_TREE;
10710     }
10711   else
10712     {
10713       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10714       if (mem_initializer_id == error_mark_node)
10715         return mem_initializer_id;
10716     }
10717   member = expand_member_init (mem_initializer_id);
10718   if (member && !DECL_P (member))
10719     in_base_initializer = 1;
10720
10721   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10722     {
10723       bool expr_non_constant_p;
10724       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10725       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10726       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10727       expression_list = build_tree_list (NULL_TREE, expression_list);
10728     }
10729   else
10730     {
10731       VEC(tree,gc)* vec;
10732       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10733                                                      /*cast_p=*/false,
10734                                                      /*allow_expansion_p=*/true,
10735                                                      /*non_constant_p=*/NULL);
10736       if (vec == NULL)
10737         return error_mark_node;
10738       expression_list = build_tree_list_vec (vec);
10739       release_tree_vector (vec);
10740     }
10741
10742   if (expression_list == error_mark_node)
10743     return error_mark_node;
10744   if (!expression_list)
10745     expression_list = void_type_node;
10746
10747   in_base_initializer = 0;
10748
10749   return member ? build_tree_list (member, expression_list) : error_mark_node;
10750 }
10751
10752 /* Parse a mem-initializer-id.
10753
10754    mem-initializer-id:
10755      :: [opt] nested-name-specifier [opt] class-name
10756      identifier
10757
10758    Returns a TYPE indicating the class to be initializer for the first
10759    production.  Returns an IDENTIFIER_NODE indicating the data member
10760    to be initialized for the second production.  */
10761
10762 static tree
10763 cp_parser_mem_initializer_id (cp_parser* parser)
10764 {
10765   bool global_scope_p;
10766   bool nested_name_specifier_p;
10767   bool template_p = false;
10768   tree id;
10769
10770   cp_token *token = cp_lexer_peek_token (parser->lexer);
10771
10772   /* `typename' is not allowed in this context ([temp.res]).  */
10773   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10774     {
10775       error_at (token->location, 
10776                 "keyword %<typename%> not allowed in this context (a qualified "
10777                 "member initializer is implicitly a type)");
10778       cp_lexer_consume_token (parser->lexer);
10779     }
10780   /* Look for the optional `::' operator.  */
10781   global_scope_p
10782     = (cp_parser_global_scope_opt (parser,
10783                                    /*current_scope_valid_p=*/false)
10784        != NULL_TREE);
10785   /* Look for the optional nested-name-specifier.  The simplest way to
10786      implement:
10787
10788        [temp.res]
10789
10790        The keyword `typename' is not permitted in a base-specifier or
10791        mem-initializer; in these contexts a qualified name that
10792        depends on a template-parameter is implicitly assumed to be a
10793        type name.
10794
10795      is to assume that we have seen the `typename' keyword at this
10796      point.  */
10797   nested_name_specifier_p
10798     = (cp_parser_nested_name_specifier_opt (parser,
10799                                             /*typename_keyword_p=*/true,
10800                                             /*check_dependency_p=*/true,
10801                                             /*type_p=*/true,
10802                                             /*is_declaration=*/true)
10803        != NULL_TREE);
10804   if (nested_name_specifier_p)
10805     template_p = cp_parser_optional_template_keyword (parser);
10806   /* If there is a `::' operator or a nested-name-specifier, then we
10807      are definitely looking for a class-name.  */
10808   if (global_scope_p || nested_name_specifier_p)
10809     return cp_parser_class_name (parser,
10810                                  /*typename_keyword_p=*/true,
10811                                  /*template_keyword_p=*/template_p,
10812                                  typename_type,
10813                                  /*check_dependency_p=*/true,
10814                                  /*class_head_p=*/false,
10815                                  /*is_declaration=*/true);
10816   /* Otherwise, we could also be looking for an ordinary identifier.  */
10817   cp_parser_parse_tentatively (parser);
10818   /* Try a class-name.  */
10819   id = cp_parser_class_name (parser,
10820                              /*typename_keyword_p=*/true,
10821                              /*template_keyword_p=*/false,
10822                              none_type,
10823                              /*check_dependency_p=*/true,
10824                              /*class_head_p=*/false,
10825                              /*is_declaration=*/true);
10826   /* If we found one, we're done.  */
10827   if (cp_parser_parse_definitely (parser))
10828     return id;
10829   /* Otherwise, look for an ordinary identifier.  */
10830   return cp_parser_identifier (parser);
10831 }
10832
10833 /* Overloading [gram.over] */
10834
10835 /* Parse an operator-function-id.
10836
10837    operator-function-id:
10838      operator operator
10839
10840    Returns an IDENTIFIER_NODE for the operator which is a
10841    human-readable spelling of the identifier, e.g., `operator +'.  */
10842
10843 static tree
10844 cp_parser_operator_function_id (cp_parser* parser)
10845 {
10846   /* Look for the `operator' keyword.  */
10847   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10848     return error_mark_node;
10849   /* And then the name of the operator itself.  */
10850   return cp_parser_operator (parser);
10851 }
10852
10853 /* Parse an operator.
10854
10855    operator:
10856      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10857      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10858      || ++ -- , ->* -> () []
10859
10860    GNU Extensions:
10861
10862    operator:
10863      <? >? <?= >?=
10864
10865    Returns an IDENTIFIER_NODE for the operator which is a
10866    human-readable spelling of the identifier, e.g., `operator +'.  */
10867
10868 static tree
10869 cp_parser_operator (cp_parser* parser)
10870 {
10871   tree id = NULL_TREE;
10872   cp_token *token;
10873
10874   /* Peek at the next token.  */
10875   token = cp_lexer_peek_token (parser->lexer);
10876   /* Figure out which operator we have.  */
10877   switch (token->type)
10878     {
10879     case CPP_KEYWORD:
10880       {
10881         enum tree_code op;
10882
10883         /* The keyword should be either `new' or `delete'.  */
10884         if (token->keyword == RID_NEW)
10885           op = NEW_EXPR;
10886         else if (token->keyword == RID_DELETE)
10887           op = DELETE_EXPR;
10888         else
10889           break;
10890
10891         /* Consume the `new' or `delete' token.  */
10892         cp_lexer_consume_token (parser->lexer);
10893
10894         /* Peek at the next token.  */
10895         token = cp_lexer_peek_token (parser->lexer);
10896         /* If it's a `[' token then this is the array variant of the
10897            operator.  */
10898         if (token->type == CPP_OPEN_SQUARE)
10899           {
10900             /* Consume the `[' token.  */
10901             cp_lexer_consume_token (parser->lexer);
10902             /* Look for the `]' token.  */
10903             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10904             id = ansi_opname (op == NEW_EXPR
10905                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10906           }
10907         /* Otherwise, we have the non-array variant.  */
10908         else
10909           id = ansi_opname (op);
10910
10911         return id;
10912       }
10913
10914     case CPP_PLUS:
10915       id = ansi_opname (PLUS_EXPR);
10916       break;
10917
10918     case CPP_MINUS:
10919       id = ansi_opname (MINUS_EXPR);
10920       break;
10921
10922     case CPP_MULT:
10923       id = ansi_opname (MULT_EXPR);
10924       break;
10925
10926     case CPP_DIV:
10927       id = ansi_opname (TRUNC_DIV_EXPR);
10928       break;
10929
10930     case CPP_MOD:
10931       id = ansi_opname (TRUNC_MOD_EXPR);
10932       break;
10933
10934     case CPP_XOR:
10935       id = ansi_opname (BIT_XOR_EXPR);
10936       break;
10937
10938     case CPP_AND:
10939       id = ansi_opname (BIT_AND_EXPR);
10940       break;
10941
10942     case CPP_OR:
10943       id = ansi_opname (BIT_IOR_EXPR);
10944       break;
10945
10946     case CPP_COMPL:
10947       id = ansi_opname (BIT_NOT_EXPR);
10948       break;
10949
10950     case CPP_NOT:
10951       id = ansi_opname (TRUTH_NOT_EXPR);
10952       break;
10953
10954     case CPP_EQ:
10955       id = ansi_assopname (NOP_EXPR);
10956       break;
10957
10958     case CPP_LESS:
10959       id = ansi_opname (LT_EXPR);
10960       break;
10961
10962     case CPP_GREATER:
10963       id = ansi_opname (GT_EXPR);
10964       break;
10965
10966     case CPP_PLUS_EQ:
10967       id = ansi_assopname (PLUS_EXPR);
10968       break;
10969
10970     case CPP_MINUS_EQ:
10971       id = ansi_assopname (MINUS_EXPR);
10972       break;
10973
10974     case CPP_MULT_EQ:
10975       id = ansi_assopname (MULT_EXPR);
10976       break;
10977
10978     case CPP_DIV_EQ:
10979       id = ansi_assopname (TRUNC_DIV_EXPR);
10980       break;
10981
10982     case CPP_MOD_EQ:
10983       id = ansi_assopname (TRUNC_MOD_EXPR);
10984       break;
10985
10986     case CPP_XOR_EQ:
10987       id = ansi_assopname (BIT_XOR_EXPR);
10988       break;
10989
10990     case CPP_AND_EQ:
10991       id = ansi_assopname (BIT_AND_EXPR);
10992       break;
10993
10994     case CPP_OR_EQ:
10995       id = ansi_assopname (BIT_IOR_EXPR);
10996       break;
10997
10998     case CPP_LSHIFT:
10999       id = ansi_opname (LSHIFT_EXPR);
11000       break;
11001
11002     case CPP_RSHIFT:
11003       id = ansi_opname (RSHIFT_EXPR);
11004       break;
11005
11006     case CPP_LSHIFT_EQ:
11007       id = ansi_assopname (LSHIFT_EXPR);
11008       break;
11009
11010     case CPP_RSHIFT_EQ:
11011       id = ansi_assopname (RSHIFT_EXPR);
11012       break;
11013
11014     case CPP_EQ_EQ:
11015       id = ansi_opname (EQ_EXPR);
11016       break;
11017
11018     case CPP_NOT_EQ:
11019       id = ansi_opname (NE_EXPR);
11020       break;
11021
11022     case CPP_LESS_EQ:
11023       id = ansi_opname (LE_EXPR);
11024       break;
11025
11026     case CPP_GREATER_EQ:
11027       id = ansi_opname (GE_EXPR);
11028       break;
11029
11030     case CPP_AND_AND:
11031       id = ansi_opname (TRUTH_ANDIF_EXPR);
11032       break;
11033
11034     case CPP_OR_OR:
11035       id = ansi_opname (TRUTH_ORIF_EXPR);
11036       break;
11037
11038     case CPP_PLUS_PLUS:
11039       id = ansi_opname (POSTINCREMENT_EXPR);
11040       break;
11041
11042     case CPP_MINUS_MINUS:
11043       id = ansi_opname (PREDECREMENT_EXPR);
11044       break;
11045
11046     case CPP_COMMA:
11047       id = ansi_opname (COMPOUND_EXPR);
11048       break;
11049
11050     case CPP_DEREF_STAR:
11051       id = ansi_opname (MEMBER_REF);
11052       break;
11053
11054     case CPP_DEREF:
11055       id = ansi_opname (COMPONENT_REF);
11056       break;
11057
11058     case CPP_OPEN_PAREN:
11059       /* Consume the `('.  */
11060       cp_lexer_consume_token (parser->lexer);
11061       /* Look for the matching `)'.  */
11062       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11063       return ansi_opname (CALL_EXPR);
11064
11065     case CPP_OPEN_SQUARE:
11066       /* Consume the `['.  */
11067       cp_lexer_consume_token (parser->lexer);
11068       /* Look for the matching `]'.  */
11069       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11070       return ansi_opname (ARRAY_REF);
11071
11072     default:
11073       /* Anything else is an error.  */
11074       break;
11075     }
11076
11077   /* If we have selected an identifier, we need to consume the
11078      operator token.  */
11079   if (id)
11080     cp_lexer_consume_token (parser->lexer);
11081   /* Otherwise, no valid operator name was present.  */
11082   else
11083     {
11084       cp_parser_error (parser, "expected operator");
11085       id = error_mark_node;
11086     }
11087
11088   return id;
11089 }
11090
11091 /* Parse a template-declaration.
11092
11093    template-declaration:
11094      export [opt] template < template-parameter-list > declaration
11095
11096    If MEMBER_P is TRUE, this template-declaration occurs within a
11097    class-specifier.
11098
11099    The grammar rule given by the standard isn't correct.  What
11100    is really meant is:
11101
11102    template-declaration:
11103      export [opt] template-parameter-list-seq
11104        decl-specifier-seq [opt] init-declarator [opt] ;
11105      export [opt] template-parameter-list-seq
11106        function-definition
11107
11108    template-parameter-list-seq:
11109      template-parameter-list-seq [opt]
11110      template < template-parameter-list >  */
11111
11112 static void
11113 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11114 {
11115   /* Check for `export'.  */
11116   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11117     {
11118       /* Consume the `export' token.  */
11119       cp_lexer_consume_token (parser->lexer);
11120       /* Warn that we do not support `export'.  */
11121       warning (0, "keyword %<export%> not implemented, and will be ignored");
11122     }
11123
11124   cp_parser_template_declaration_after_export (parser, member_p);
11125 }
11126
11127 /* Parse a template-parameter-list.
11128
11129    template-parameter-list:
11130      template-parameter
11131      template-parameter-list , template-parameter
11132
11133    Returns a TREE_LIST.  Each node represents a template parameter.
11134    The nodes are connected via their TREE_CHAINs.  */
11135
11136 static tree
11137 cp_parser_template_parameter_list (cp_parser* parser)
11138 {
11139   tree parameter_list = NULL_TREE;
11140
11141   begin_template_parm_list ();
11142
11143   /* The loop below parses the template parms.  We first need to know
11144      the total number of template parms to be able to compute proper
11145      canonical types of each dependent type. So after the loop, when
11146      we know the total number of template parms,
11147      end_template_parm_list computes the proper canonical types and
11148      fixes up the dependent types accordingly.  */
11149   while (true)
11150     {
11151       tree parameter;
11152       bool is_non_type;
11153       bool is_parameter_pack;
11154       location_t parm_loc;
11155
11156       /* Parse the template-parameter.  */
11157       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11158       parameter = cp_parser_template_parameter (parser, 
11159                                                 &is_non_type,
11160                                                 &is_parameter_pack);
11161       /* Add it to the list.  */
11162       if (parameter != error_mark_node)
11163         parameter_list = process_template_parm (parameter_list,
11164                                                 parm_loc,
11165                                                 parameter,
11166                                                 is_non_type,
11167                                                 is_parameter_pack,
11168                                                 0);
11169       else
11170        {
11171          tree err_parm = build_tree_list (parameter, parameter);
11172          parameter_list = chainon (parameter_list, err_parm);
11173        }
11174
11175       /* If the next token is not a `,', we're done.  */
11176       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11177         break;
11178       /* Otherwise, consume the `,' token.  */
11179       cp_lexer_consume_token (parser->lexer);
11180     }
11181
11182   return end_template_parm_list (parameter_list);
11183 }
11184
11185 /* Parse a template-parameter.
11186
11187    template-parameter:
11188      type-parameter
11189      parameter-declaration
11190
11191    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11192    the parameter.  The TREE_PURPOSE is the default value, if any.
11193    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11194    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11195    set to true iff this parameter is a parameter pack. */
11196
11197 static tree
11198 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11199                               bool *is_parameter_pack)
11200 {
11201   cp_token *token;
11202   cp_parameter_declarator *parameter_declarator;
11203   cp_declarator *id_declarator;
11204   tree parm;
11205
11206   /* Assume it is a type parameter or a template parameter.  */
11207   *is_non_type = false;
11208   /* Assume it not a parameter pack. */
11209   *is_parameter_pack = false;
11210   /* Peek at the next token.  */
11211   token = cp_lexer_peek_token (parser->lexer);
11212   /* If it is `class' or `template', we have a type-parameter.  */
11213   if (token->keyword == RID_TEMPLATE)
11214     return cp_parser_type_parameter (parser, is_parameter_pack);
11215   /* If it is `class' or `typename' we do not know yet whether it is a
11216      type parameter or a non-type parameter.  Consider:
11217
11218        template <typename T, typename T::X X> ...
11219
11220      or:
11221
11222        template <class C, class D*> ...
11223
11224      Here, the first parameter is a type parameter, and the second is
11225      a non-type parameter.  We can tell by looking at the token after
11226      the identifier -- if it is a `,', `=', or `>' then we have a type
11227      parameter.  */
11228   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11229     {
11230       /* Peek at the token after `class' or `typename'.  */
11231       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11232       /* If it's an ellipsis, we have a template type parameter
11233          pack. */
11234       if (token->type == CPP_ELLIPSIS)
11235         return cp_parser_type_parameter (parser, is_parameter_pack);
11236       /* If it's an identifier, skip it.  */
11237       if (token->type == CPP_NAME)
11238         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11239       /* Now, see if the token looks like the end of a template
11240          parameter.  */
11241       if (token->type == CPP_COMMA
11242           || token->type == CPP_EQ
11243           || token->type == CPP_GREATER)
11244         return cp_parser_type_parameter (parser, is_parameter_pack);
11245     }
11246
11247   /* Otherwise, it is a non-type parameter.
11248
11249      [temp.param]
11250
11251      When parsing a default template-argument for a non-type
11252      template-parameter, the first non-nested `>' is taken as the end
11253      of the template parameter-list rather than a greater-than
11254      operator.  */
11255   *is_non_type = true;
11256   parameter_declarator
11257      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11258                                         /*parenthesized_p=*/NULL);
11259
11260   /* If the parameter declaration is marked as a parameter pack, set
11261      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11262      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11263      grokdeclarator. */
11264   if (parameter_declarator
11265       && parameter_declarator->declarator
11266       && parameter_declarator->declarator->parameter_pack_p)
11267     {
11268       *is_parameter_pack = true;
11269       parameter_declarator->declarator->parameter_pack_p = false;
11270     }
11271
11272   /* If the next token is an ellipsis, and we don't already have it
11273      marked as a parameter pack, then we have a parameter pack (that
11274      has no declarator).  */
11275   if (!*is_parameter_pack
11276       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11277       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11278     {
11279       /* Consume the `...'.  */
11280       cp_lexer_consume_token (parser->lexer);
11281       maybe_warn_variadic_templates ();
11282       
11283       *is_parameter_pack = true;
11284     }
11285   /* We might end up with a pack expansion as the type of the non-type
11286      template parameter, in which case this is a non-type template
11287      parameter pack.  */
11288   else if (parameter_declarator
11289            && parameter_declarator->decl_specifiers.type
11290            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11291     {
11292       *is_parameter_pack = true;
11293       parameter_declarator->decl_specifiers.type = 
11294         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11295     }
11296
11297   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11298     {
11299       /* Parameter packs cannot have default arguments.  However, a
11300          user may try to do so, so we'll parse them and give an
11301          appropriate diagnostic here.  */
11302
11303       /* Consume the `='.  */
11304       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11305       cp_lexer_consume_token (parser->lexer);
11306       
11307       /* Find the name of the parameter pack.  */     
11308       id_declarator = parameter_declarator->declarator;
11309       while (id_declarator && id_declarator->kind != cdk_id)
11310         id_declarator = id_declarator->declarator;
11311       
11312       if (id_declarator && id_declarator->kind == cdk_id)
11313         error_at (start_token->location,
11314                   "template parameter pack %qD cannot have a default argument",
11315                   id_declarator->u.id.unqualified_name);
11316       else
11317         error_at (start_token->location,
11318                   "template parameter pack cannot have a default argument");
11319       
11320       /* Parse the default argument, but throw away the result.  */
11321       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11322     }
11323
11324   parm = grokdeclarator (parameter_declarator->declarator,
11325                          &parameter_declarator->decl_specifiers,
11326                          TPARM, /*initialized=*/0,
11327                          /*attrlist=*/NULL);
11328   if (parm == error_mark_node)
11329     return error_mark_node;
11330
11331   return build_tree_list (parameter_declarator->default_argument, parm);
11332 }
11333
11334 /* Parse a type-parameter.
11335
11336    type-parameter:
11337      class identifier [opt]
11338      class identifier [opt] = type-id
11339      typename identifier [opt]
11340      typename identifier [opt] = type-id
11341      template < template-parameter-list > class identifier [opt]
11342      template < template-parameter-list > class identifier [opt]
11343        = id-expression
11344
11345    GNU Extension (variadic templates):
11346
11347    type-parameter:
11348      class ... identifier [opt]
11349      typename ... identifier [opt]
11350
11351    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11352    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11353    the declaration of the parameter.
11354
11355    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11356
11357 static tree
11358 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11359 {
11360   cp_token *token;
11361   tree parameter;
11362
11363   /* Look for a keyword to tell us what kind of parameter this is.  */
11364   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11365   if (!token)
11366     return error_mark_node;
11367
11368   switch (token->keyword)
11369     {
11370     case RID_CLASS:
11371     case RID_TYPENAME:
11372       {
11373         tree identifier;
11374         tree default_argument;
11375
11376         /* If the next token is an ellipsis, we have a template
11377            argument pack. */
11378         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11379           {
11380             /* Consume the `...' token. */
11381             cp_lexer_consume_token (parser->lexer);
11382             maybe_warn_variadic_templates ();
11383
11384             *is_parameter_pack = true;
11385           }
11386
11387         /* If the next token is an identifier, then it names the
11388            parameter.  */
11389         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11390           identifier = cp_parser_identifier (parser);
11391         else
11392           identifier = NULL_TREE;
11393
11394         /* Create the parameter.  */
11395         parameter = finish_template_type_parm (class_type_node, identifier);
11396
11397         /* If the next token is an `=', we have a default argument.  */
11398         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11399           {
11400             /* Consume the `=' token.  */
11401             cp_lexer_consume_token (parser->lexer);
11402             /* Parse the default-argument.  */
11403             push_deferring_access_checks (dk_no_deferred);
11404             default_argument = cp_parser_type_id (parser);
11405
11406             /* Template parameter packs cannot have default
11407                arguments. */
11408             if (*is_parameter_pack)
11409               {
11410                 if (identifier)
11411                   error_at (token->location,
11412                             "template parameter pack %qD cannot have a "
11413                             "default argument", identifier);
11414                 else
11415                   error_at (token->location,
11416                             "template parameter packs cannot have "
11417                             "default arguments");
11418                 default_argument = NULL_TREE;
11419               }
11420             pop_deferring_access_checks ();
11421           }
11422         else
11423           default_argument = NULL_TREE;
11424
11425         /* Create the combined representation of the parameter and the
11426            default argument.  */
11427         parameter = build_tree_list (default_argument, parameter);
11428       }
11429       break;
11430
11431     case RID_TEMPLATE:
11432       {
11433         tree identifier;
11434         tree default_argument;
11435
11436         /* Look for the `<'.  */
11437         cp_parser_require (parser, CPP_LESS, RT_LESS);
11438         /* Parse the template-parameter-list.  */
11439         cp_parser_template_parameter_list (parser);
11440         /* Look for the `>'.  */
11441         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11442         /* Look for the `class' keyword.  */
11443         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11444         /* If the next token is an ellipsis, we have a template
11445            argument pack. */
11446         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11447           {
11448             /* Consume the `...' token. */
11449             cp_lexer_consume_token (parser->lexer);
11450             maybe_warn_variadic_templates ();
11451
11452             *is_parameter_pack = true;
11453           }
11454         /* If the next token is an `=', then there is a
11455            default-argument.  If the next token is a `>', we are at
11456            the end of the parameter-list.  If the next token is a `,',
11457            then we are at the end of this parameter.  */
11458         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11459             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11460             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11461           {
11462             identifier = cp_parser_identifier (parser);
11463             /* Treat invalid names as if the parameter were nameless.  */
11464             if (identifier == error_mark_node)
11465               identifier = NULL_TREE;
11466           }
11467         else
11468           identifier = NULL_TREE;
11469
11470         /* Create the template parameter.  */
11471         parameter = finish_template_template_parm (class_type_node,
11472                                                    identifier);
11473
11474         /* If the next token is an `=', then there is a
11475            default-argument.  */
11476         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11477           {
11478             bool is_template;
11479
11480             /* Consume the `='.  */
11481             cp_lexer_consume_token (parser->lexer);
11482             /* Parse the id-expression.  */
11483             push_deferring_access_checks (dk_no_deferred);
11484             /* save token before parsing the id-expression, for error
11485                reporting */
11486             token = cp_lexer_peek_token (parser->lexer);
11487             default_argument
11488               = cp_parser_id_expression (parser,
11489                                          /*template_keyword_p=*/false,
11490                                          /*check_dependency_p=*/true,
11491                                          /*template_p=*/&is_template,
11492                                          /*declarator_p=*/false,
11493                                          /*optional_p=*/false);
11494             if (TREE_CODE (default_argument) == TYPE_DECL)
11495               /* If the id-expression was a template-id that refers to
11496                  a template-class, we already have the declaration here,
11497                  so no further lookup is needed.  */
11498                  ;
11499             else
11500               /* Look up the name.  */
11501               default_argument
11502                 = cp_parser_lookup_name (parser, default_argument,
11503                                          none_type,
11504                                          /*is_template=*/is_template,
11505                                          /*is_namespace=*/false,
11506                                          /*check_dependency=*/true,
11507                                          /*ambiguous_decls=*/NULL,
11508                                          token->location);
11509             /* See if the default argument is valid.  */
11510             default_argument
11511               = check_template_template_default_arg (default_argument);
11512
11513             /* Template parameter packs cannot have default
11514                arguments. */
11515             if (*is_parameter_pack)
11516               {
11517                 if (identifier)
11518                   error_at (token->location,
11519                             "template parameter pack %qD cannot "
11520                             "have a default argument",
11521                             identifier);
11522                 else
11523                   error_at (token->location, "template parameter packs cannot "
11524                             "have default arguments");
11525                 default_argument = NULL_TREE;
11526               }
11527             pop_deferring_access_checks ();
11528           }
11529         else
11530           default_argument = NULL_TREE;
11531
11532         /* Create the combined representation of the parameter and the
11533            default argument.  */
11534         parameter = build_tree_list (default_argument, parameter);
11535       }
11536       break;
11537
11538     default:
11539       gcc_unreachable ();
11540       break;
11541     }
11542
11543   return parameter;
11544 }
11545
11546 /* Parse a template-id.
11547
11548    template-id:
11549      template-name < template-argument-list [opt] >
11550
11551    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11552    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11553    returned.  Otherwise, if the template-name names a function, or set
11554    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11555    names a class, returns a TYPE_DECL for the specialization.
11556
11557    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11558    uninstantiated templates.  */
11559
11560 static tree
11561 cp_parser_template_id (cp_parser *parser,
11562                        bool template_keyword_p,
11563                        bool check_dependency_p,
11564                        bool is_declaration)
11565 {
11566   int i;
11567   tree templ;
11568   tree arguments;
11569   tree template_id;
11570   cp_token_position start_of_id = 0;
11571   deferred_access_check *chk;
11572   VEC (deferred_access_check,gc) *access_check;
11573   cp_token *next_token = NULL, *next_token_2 = NULL;
11574   bool is_identifier;
11575
11576   /* If the next token corresponds to a template-id, there is no need
11577      to reparse it.  */
11578   next_token = cp_lexer_peek_token (parser->lexer);
11579   if (next_token->type == CPP_TEMPLATE_ID)
11580     {
11581       struct tree_check *check_value;
11582
11583       /* Get the stored value.  */
11584       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11585       /* Perform any access checks that were deferred.  */
11586       access_check = check_value->checks;
11587       if (access_check)
11588         {
11589           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11590             perform_or_defer_access_check (chk->binfo,
11591                                            chk->decl,
11592                                            chk->diag_decl);
11593         }
11594       /* Return the stored value.  */
11595       return check_value->value;
11596     }
11597
11598   /* Avoid performing name lookup if there is no possibility of
11599      finding a template-id.  */
11600   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11601       || (next_token->type == CPP_NAME
11602           && !cp_parser_nth_token_starts_template_argument_list_p
11603                (parser, 2)))
11604     {
11605       cp_parser_error (parser, "expected template-id");
11606       return error_mark_node;
11607     }
11608
11609   /* Remember where the template-id starts.  */
11610   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11611     start_of_id = cp_lexer_token_position (parser->lexer, false);
11612
11613   push_deferring_access_checks (dk_deferred);
11614
11615   /* Parse the template-name.  */
11616   is_identifier = false;
11617   templ = cp_parser_template_name (parser, template_keyword_p,
11618                                    check_dependency_p,
11619                                    is_declaration,
11620                                    &is_identifier);
11621   if (templ == error_mark_node || is_identifier)
11622     {
11623       pop_deferring_access_checks ();
11624       return templ;
11625     }
11626
11627   /* If we find the sequence `[:' after a template-name, it's probably
11628      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11629      parse correctly the argument list.  */
11630   next_token = cp_lexer_peek_token (parser->lexer);
11631   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11632   if (next_token->type == CPP_OPEN_SQUARE
11633       && next_token->flags & DIGRAPH
11634       && next_token_2->type == CPP_COLON
11635       && !(next_token_2->flags & PREV_WHITE))
11636     {
11637       cp_parser_parse_tentatively (parser);
11638       /* Change `:' into `::'.  */
11639       next_token_2->type = CPP_SCOPE;
11640       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11641          CPP_LESS.  */
11642       cp_lexer_consume_token (parser->lexer);
11643
11644       /* Parse the arguments.  */
11645       arguments = cp_parser_enclosed_template_argument_list (parser);
11646       if (!cp_parser_parse_definitely (parser))
11647         {
11648           /* If we couldn't parse an argument list, then we revert our changes
11649              and return simply an error. Maybe this is not a template-id
11650              after all.  */
11651           next_token_2->type = CPP_COLON;
11652           cp_parser_error (parser, "expected %<<%>");
11653           pop_deferring_access_checks ();
11654           return error_mark_node;
11655         }
11656       /* Otherwise, emit an error about the invalid digraph, but continue
11657          parsing because we got our argument list.  */
11658       if (permerror (next_token->location,
11659                      "%<<::%> cannot begin a template-argument list"))
11660         {
11661           static bool hint = false;
11662           inform (next_token->location,
11663                   "%<<:%> is an alternate spelling for %<[%>."
11664                   " Insert whitespace between %<<%> and %<::%>");
11665           if (!hint && !flag_permissive)
11666             {
11667               inform (next_token->location, "(if you use %<-fpermissive%>"
11668                       " G++ will accept your code)");
11669               hint = true;
11670             }
11671         }
11672     }
11673   else
11674     {
11675       /* Look for the `<' that starts the template-argument-list.  */
11676       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11677         {
11678           pop_deferring_access_checks ();
11679           return error_mark_node;
11680         }
11681       /* Parse the arguments.  */
11682       arguments = cp_parser_enclosed_template_argument_list (parser);
11683     }
11684
11685   /* Build a representation of the specialization.  */
11686   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11687     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11688   else if (DECL_CLASS_TEMPLATE_P (templ)
11689            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11690     {
11691       bool entering_scope;
11692       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11693          template (rather than some instantiation thereof) only if
11694          is not nested within some other construct.  For example, in
11695          "template <typename T> void f(T) { A<T>::", A<T> is just an
11696          instantiation of A.  */
11697       entering_scope = (template_parm_scope_p ()
11698                         && cp_lexer_next_token_is (parser->lexer,
11699                                                    CPP_SCOPE));
11700       template_id
11701         = finish_template_type (templ, arguments, entering_scope);
11702     }
11703   else
11704     {
11705       /* If it's not a class-template or a template-template, it should be
11706          a function-template.  */
11707       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11708                    || TREE_CODE (templ) == OVERLOAD
11709                    || BASELINK_P (templ)));
11710
11711       template_id = lookup_template_function (templ, arguments);
11712     }
11713
11714   /* If parsing tentatively, replace the sequence of tokens that makes
11715      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11716      should we re-parse the token stream, we will not have to repeat
11717      the effort required to do the parse, nor will we issue duplicate
11718      error messages about problems during instantiation of the
11719      template.  */
11720   if (start_of_id)
11721     {
11722       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11723
11724       /* Reset the contents of the START_OF_ID token.  */
11725       token->type = CPP_TEMPLATE_ID;
11726       /* Retrieve any deferred checks.  Do not pop this access checks yet
11727          so the memory will not be reclaimed during token replacing below.  */
11728       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11729       token->u.tree_check_value->value = template_id;
11730       token->u.tree_check_value->checks = get_deferred_access_checks ();
11731       token->keyword = RID_MAX;
11732
11733       /* Purge all subsequent tokens.  */
11734       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11735
11736       /* ??? Can we actually assume that, if template_id ==
11737          error_mark_node, we will have issued a diagnostic to the
11738          user, as opposed to simply marking the tentative parse as
11739          failed?  */
11740       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11741         error_at (token->location, "parse error in template argument list");
11742     }
11743
11744   pop_deferring_access_checks ();
11745   return template_id;
11746 }
11747
11748 /* Parse a template-name.
11749
11750    template-name:
11751      identifier
11752
11753    The standard should actually say:
11754
11755    template-name:
11756      identifier
11757      operator-function-id
11758
11759    A defect report has been filed about this issue.
11760
11761    A conversion-function-id cannot be a template name because they cannot
11762    be part of a template-id. In fact, looking at this code:
11763
11764    a.operator K<int>()
11765
11766    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11767    It is impossible to call a templated conversion-function-id with an
11768    explicit argument list, since the only allowed template parameter is
11769    the type to which it is converting.
11770
11771    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11772    `template' keyword, in a construction like:
11773
11774      T::template f<3>()
11775
11776    In that case `f' is taken to be a template-name, even though there
11777    is no way of knowing for sure.
11778
11779    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11780    name refers to a set of overloaded functions, at least one of which
11781    is a template, or an IDENTIFIER_NODE with the name of the template,
11782    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11783    names are looked up inside uninstantiated templates.  */
11784
11785 static tree
11786 cp_parser_template_name (cp_parser* parser,
11787                          bool template_keyword_p,
11788                          bool check_dependency_p,
11789                          bool is_declaration,
11790                          bool *is_identifier)
11791 {
11792   tree identifier;
11793   tree decl;
11794   tree fns;
11795   cp_token *token = cp_lexer_peek_token (parser->lexer);
11796
11797   /* If the next token is `operator', then we have either an
11798      operator-function-id or a conversion-function-id.  */
11799   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11800     {
11801       /* We don't know whether we're looking at an
11802          operator-function-id or a conversion-function-id.  */
11803       cp_parser_parse_tentatively (parser);
11804       /* Try an operator-function-id.  */
11805       identifier = cp_parser_operator_function_id (parser);
11806       /* If that didn't work, try a conversion-function-id.  */
11807       if (!cp_parser_parse_definitely (parser))
11808         {
11809           cp_parser_error (parser, "expected template-name");
11810           return error_mark_node;
11811         }
11812     }
11813   /* Look for the identifier.  */
11814   else
11815     identifier = cp_parser_identifier (parser);
11816
11817   /* If we didn't find an identifier, we don't have a template-id.  */
11818   if (identifier == error_mark_node)
11819     return error_mark_node;
11820
11821   /* If the name immediately followed the `template' keyword, then it
11822      is a template-name.  However, if the next token is not `<', then
11823      we do not treat it as a template-name, since it is not being used
11824      as part of a template-id.  This enables us to handle constructs
11825      like:
11826
11827        template <typename T> struct S { S(); };
11828        template <typename T> S<T>::S();
11829
11830      correctly.  We would treat `S' as a template -- if it were `S<T>'
11831      -- but we do not if there is no `<'.  */
11832
11833   if (processing_template_decl
11834       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11835     {
11836       /* In a declaration, in a dependent context, we pretend that the
11837          "template" keyword was present in order to improve error
11838          recovery.  For example, given:
11839
11840            template <typename T> void f(T::X<int>);
11841
11842          we want to treat "X<int>" as a template-id.  */
11843       if (is_declaration
11844           && !template_keyword_p
11845           && parser->scope && TYPE_P (parser->scope)
11846           && check_dependency_p
11847           && dependent_scope_p (parser->scope)
11848           /* Do not do this for dtors (or ctors), since they never
11849              need the template keyword before their name.  */
11850           && !constructor_name_p (identifier, parser->scope))
11851         {
11852           cp_token_position start = 0;
11853
11854           /* Explain what went wrong.  */
11855           error_at (token->location, "non-template %qD used as template",
11856                     identifier);
11857           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11858                   parser->scope, identifier);
11859           /* If parsing tentatively, find the location of the "<" token.  */
11860           if (cp_parser_simulate_error (parser))
11861             start = cp_lexer_token_position (parser->lexer, true);
11862           /* Parse the template arguments so that we can issue error
11863              messages about them.  */
11864           cp_lexer_consume_token (parser->lexer);
11865           cp_parser_enclosed_template_argument_list (parser);
11866           /* Skip tokens until we find a good place from which to
11867              continue parsing.  */
11868           cp_parser_skip_to_closing_parenthesis (parser,
11869                                                  /*recovering=*/true,
11870                                                  /*or_comma=*/true,
11871                                                  /*consume_paren=*/false);
11872           /* If parsing tentatively, permanently remove the
11873              template argument list.  That will prevent duplicate
11874              error messages from being issued about the missing
11875              "template" keyword.  */
11876           if (start)
11877             cp_lexer_purge_tokens_after (parser->lexer, start);
11878           if (is_identifier)
11879             *is_identifier = true;
11880           return identifier;
11881         }
11882
11883       /* If the "template" keyword is present, then there is generally
11884          no point in doing name-lookup, so we just return IDENTIFIER.
11885          But, if the qualifying scope is non-dependent then we can
11886          (and must) do name-lookup normally.  */
11887       if (template_keyword_p
11888           && (!parser->scope
11889               || (TYPE_P (parser->scope)
11890                   && dependent_type_p (parser->scope))))
11891         return identifier;
11892     }
11893
11894   /* Look up the name.  */
11895   decl = cp_parser_lookup_name (parser, identifier,
11896                                 none_type,
11897                                 /*is_template=*/true,
11898                                 /*is_namespace=*/false,
11899                                 check_dependency_p,
11900                                 /*ambiguous_decls=*/NULL,
11901                                 token->location);
11902
11903   /* If DECL is a template, then the name was a template-name.  */
11904   if (TREE_CODE (decl) == TEMPLATE_DECL)
11905     ;
11906   else
11907     {
11908       tree fn = NULL_TREE;
11909
11910       /* The standard does not explicitly indicate whether a name that
11911          names a set of overloaded declarations, some of which are
11912          templates, is a template-name.  However, such a name should
11913          be a template-name; otherwise, there is no way to form a
11914          template-id for the overloaded templates.  */
11915       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11916       if (TREE_CODE (fns) == OVERLOAD)
11917         for (fn = fns; fn; fn = OVL_NEXT (fn))
11918           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11919             break;
11920
11921       if (!fn)
11922         {
11923           /* The name does not name a template.  */
11924           cp_parser_error (parser, "expected template-name");
11925           return error_mark_node;
11926         }
11927     }
11928
11929   /* If DECL is dependent, and refers to a function, then just return
11930      its name; we will look it up again during template instantiation.  */
11931   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11932     {
11933       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11934       if (TYPE_P (scope) && dependent_type_p (scope))
11935         return identifier;
11936     }
11937
11938   return decl;
11939 }
11940
11941 /* Parse a template-argument-list.
11942
11943    template-argument-list:
11944      template-argument ... [opt]
11945      template-argument-list , template-argument ... [opt]
11946
11947    Returns a TREE_VEC containing the arguments.  */
11948
11949 static tree
11950 cp_parser_template_argument_list (cp_parser* parser)
11951 {
11952   tree fixed_args[10];
11953   unsigned n_args = 0;
11954   unsigned alloced = 10;
11955   tree *arg_ary = fixed_args;
11956   tree vec;
11957   bool saved_in_template_argument_list_p;
11958   bool saved_ice_p;
11959   bool saved_non_ice_p;
11960
11961   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11962   parser->in_template_argument_list_p = true;
11963   /* Even if the template-id appears in an integral
11964      constant-expression, the contents of the argument list do
11965      not.  */
11966   saved_ice_p = parser->integral_constant_expression_p;
11967   parser->integral_constant_expression_p = false;
11968   saved_non_ice_p = parser->non_integral_constant_expression_p;
11969   parser->non_integral_constant_expression_p = false;
11970   /* Parse the arguments.  */
11971   do
11972     {
11973       tree argument;
11974
11975       if (n_args)
11976         /* Consume the comma.  */
11977         cp_lexer_consume_token (parser->lexer);
11978
11979       /* Parse the template-argument.  */
11980       argument = cp_parser_template_argument (parser);
11981
11982       /* If the next token is an ellipsis, we're expanding a template
11983          argument pack. */
11984       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11985         {
11986           if (argument == error_mark_node)
11987             {
11988               cp_token *token = cp_lexer_peek_token (parser->lexer);
11989               error_at (token->location,
11990                         "expected parameter pack before %<...%>");
11991             }
11992           /* Consume the `...' token. */
11993           cp_lexer_consume_token (parser->lexer);
11994
11995           /* Make the argument into a TYPE_PACK_EXPANSION or
11996              EXPR_PACK_EXPANSION. */
11997           argument = make_pack_expansion (argument);
11998         }
11999
12000       if (n_args == alloced)
12001         {
12002           alloced *= 2;
12003
12004           if (arg_ary == fixed_args)
12005             {
12006               arg_ary = XNEWVEC (tree, alloced);
12007               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12008             }
12009           else
12010             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12011         }
12012       arg_ary[n_args++] = argument;
12013     }
12014   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12015
12016   vec = make_tree_vec (n_args);
12017
12018   while (n_args--)
12019     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12020
12021   if (arg_ary != fixed_args)
12022     free (arg_ary);
12023   parser->non_integral_constant_expression_p = saved_non_ice_p;
12024   parser->integral_constant_expression_p = saved_ice_p;
12025   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12026 #ifdef ENABLE_CHECKING
12027   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12028 #endif
12029   return vec;
12030 }
12031
12032 /* Parse a template-argument.
12033
12034    template-argument:
12035      assignment-expression
12036      type-id
12037      id-expression
12038
12039    The representation is that of an assignment-expression, type-id, or
12040    id-expression -- except that the qualified id-expression is
12041    evaluated, so that the value returned is either a DECL or an
12042    OVERLOAD.
12043
12044    Although the standard says "assignment-expression", it forbids
12045    throw-expressions or assignments in the template argument.
12046    Therefore, we use "conditional-expression" instead.  */
12047
12048 static tree
12049 cp_parser_template_argument (cp_parser* parser)
12050 {
12051   tree argument;
12052   bool template_p;
12053   bool address_p;
12054   bool maybe_type_id = false;
12055   cp_token *token = NULL, *argument_start_token = NULL;
12056   cp_id_kind idk;
12057
12058   /* There's really no way to know what we're looking at, so we just
12059      try each alternative in order.
12060
12061        [temp.arg]
12062
12063        In a template-argument, an ambiguity between a type-id and an
12064        expression is resolved to a type-id, regardless of the form of
12065        the corresponding template-parameter.
12066
12067      Therefore, we try a type-id first.  */
12068   cp_parser_parse_tentatively (parser);
12069   argument = cp_parser_template_type_arg (parser);
12070   /* If there was no error parsing the type-id but the next token is a
12071      '>>', our behavior depends on which dialect of C++ we're
12072      parsing. In C++98, we probably found a typo for '> >'. But there
12073      are type-id which are also valid expressions. For instance:
12074
12075      struct X { int operator >> (int); };
12076      template <int V> struct Foo {};
12077      Foo<X () >> 5> r;
12078
12079      Here 'X()' is a valid type-id of a function type, but the user just
12080      wanted to write the expression "X() >> 5". Thus, we remember that we
12081      found a valid type-id, but we still try to parse the argument as an
12082      expression to see what happens. 
12083
12084      In C++0x, the '>>' will be considered two separate '>'
12085      tokens.  */
12086   if (!cp_parser_error_occurred (parser)
12087       && cxx_dialect == cxx98
12088       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12089     {
12090       maybe_type_id = true;
12091       cp_parser_abort_tentative_parse (parser);
12092     }
12093   else
12094     {
12095       /* If the next token isn't a `,' or a `>', then this argument wasn't
12096       really finished. This means that the argument is not a valid
12097       type-id.  */
12098       if (!cp_parser_next_token_ends_template_argument_p (parser))
12099         cp_parser_error (parser, "expected template-argument");
12100       /* If that worked, we're done.  */
12101       if (cp_parser_parse_definitely (parser))
12102         return argument;
12103     }
12104   /* We're still not sure what the argument will be.  */
12105   cp_parser_parse_tentatively (parser);
12106   /* Try a template.  */
12107   argument_start_token = cp_lexer_peek_token (parser->lexer);
12108   argument = cp_parser_id_expression (parser,
12109                                       /*template_keyword_p=*/false,
12110                                       /*check_dependency_p=*/true,
12111                                       &template_p,
12112                                       /*declarator_p=*/false,
12113                                       /*optional_p=*/false);
12114   /* If the next token isn't a `,' or a `>', then this argument wasn't
12115      really finished.  */
12116   if (!cp_parser_next_token_ends_template_argument_p (parser))
12117     cp_parser_error (parser, "expected template-argument");
12118   if (!cp_parser_error_occurred (parser))
12119     {
12120       /* Figure out what is being referred to.  If the id-expression
12121          was for a class template specialization, then we will have a
12122          TYPE_DECL at this point.  There is no need to do name lookup
12123          at this point in that case.  */
12124       if (TREE_CODE (argument) != TYPE_DECL)
12125         argument = cp_parser_lookup_name (parser, argument,
12126                                           none_type,
12127                                           /*is_template=*/template_p,
12128                                           /*is_namespace=*/false,
12129                                           /*check_dependency=*/true,
12130                                           /*ambiguous_decls=*/NULL,
12131                                           argument_start_token->location);
12132       if (TREE_CODE (argument) != TEMPLATE_DECL
12133           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12134         cp_parser_error (parser, "expected template-name");
12135     }
12136   if (cp_parser_parse_definitely (parser))
12137     return argument;
12138   /* It must be a non-type argument.  There permitted cases are given
12139      in [temp.arg.nontype]:
12140
12141      -- an integral constant-expression of integral or enumeration
12142         type; or
12143
12144      -- the name of a non-type template-parameter; or
12145
12146      -- the name of an object or function with external linkage...
12147
12148      -- the address of an object or function with external linkage...
12149
12150      -- a pointer to member...  */
12151   /* Look for a non-type template parameter.  */
12152   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12153     {
12154       cp_parser_parse_tentatively (parser);
12155       argument = cp_parser_primary_expression (parser,
12156                                                /*address_p=*/false,
12157                                                /*cast_p=*/false,
12158                                                /*template_arg_p=*/true,
12159                                                &idk);
12160       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12161           || !cp_parser_next_token_ends_template_argument_p (parser))
12162         cp_parser_simulate_error (parser);
12163       if (cp_parser_parse_definitely (parser))
12164         return argument;
12165     }
12166
12167   /* If the next token is "&", the argument must be the address of an
12168      object or function with external linkage.  */
12169   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12170   if (address_p)
12171     cp_lexer_consume_token (parser->lexer);
12172   /* See if we might have an id-expression.  */
12173   token = cp_lexer_peek_token (parser->lexer);
12174   if (token->type == CPP_NAME
12175       || token->keyword == RID_OPERATOR
12176       || token->type == CPP_SCOPE
12177       || token->type == CPP_TEMPLATE_ID
12178       || token->type == CPP_NESTED_NAME_SPECIFIER)
12179     {
12180       cp_parser_parse_tentatively (parser);
12181       argument = cp_parser_primary_expression (parser,
12182                                                address_p,
12183                                                /*cast_p=*/false,
12184                                                /*template_arg_p=*/true,
12185                                                &idk);
12186       if (cp_parser_error_occurred (parser)
12187           || !cp_parser_next_token_ends_template_argument_p (parser))
12188         cp_parser_abort_tentative_parse (parser);
12189       else
12190         {
12191           tree probe;
12192
12193           if (TREE_CODE (argument) == INDIRECT_REF)
12194             {
12195               gcc_assert (REFERENCE_REF_P (argument));
12196               argument = TREE_OPERAND (argument, 0);
12197             }
12198
12199           /* If we're in a template, we represent a qualified-id referring
12200              to a static data member as a SCOPE_REF even if the scope isn't
12201              dependent so that we can check access control later.  */
12202           probe = argument;
12203           if (TREE_CODE (probe) == SCOPE_REF)
12204             probe = TREE_OPERAND (probe, 1);
12205           if (TREE_CODE (probe) == VAR_DECL)
12206             {
12207               /* A variable without external linkage might still be a
12208                  valid constant-expression, so no error is issued here
12209                  if the external-linkage check fails.  */
12210               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12211                 cp_parser_simulate_error (parser);
12212             }
12213           else if (is_overloaded_fn (argument))
12214             /* All overloaded functions are allowed; if the external
12215                linkage test does not pass, an error will be issued
12216                later.  */
12217             ;
12218           else if (address_p
12219                    && (TREE_CODE (argument) == OFFSET_REF
12220                        || TREE_CODE (argument) == SCOPE_REF))
12221             /* A pointer-to-member.  */
12222             ;
12223           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12224             ;
12225           else
12226             cp_parser_simulate_error (parser);
12227
12228           if (cp_parser_parse_definitely (parser))
12229             {
12230               if (address_p)
12231                 argument = build_x_unary_op (ADDR_EXPR, argument,
12232                                              tf_warning_or_error);
12233               return argument;
12234             }
12235         }
12236     }
12237   /* If the argument started with "&", there are no other valid
12238      alternatives at this point.  */
12239   if (address_p)
12240     {
12241       cp_parser_error (parser, "invalid non-type template argument");
12242       return error_mark_node;
12243     }
12244
12245   /* If the argument wasn't successfully parsed as a type-id followed
12246      by '>>', the argument can only be a constant expression now.
12247      Otherwise, we try parsing the constant-expression tentatively,
12248      because the argument could really be a type-id.  */
12249   if (maybe_type_id)
12250     cp_parser_parse_tentatively (parser);
12251   argument = cp_parser_constant_expression (parser,
12252                                             /*allow_non_constant_p=*/false,
12253                                             /*non_constant_p=*/NULL);
12254   argument = fold_non_dependent_expr (argument);
12255   if (!maybe_type_id)
12256     return argument;
12257   if (!cp_parser_next_token_ends_template_argument_p (parser))
12258     cp_parser_error (parser, "expected template-argument");
12259   if (cp_parser_parse_definitely (parser))
12260     return argument;
12261   /* We did our best to parse the argument as a non type-id, but that
12262      was the only alternative that matched (albeit with a '>' after
12263      it). We can assume it's just a typo from the user, and a
12264      diagnostic will then be issued.  */
12265   return cp_parser_template_type_arg (parser);
12266 }
12267
12268 /* Parse an explicit-instantiation.
12269
12270    explicit-instantiation:
12271      template declaration
12272
12273    Although the standard says `declaration', what it really means is:
12274
12275    explicit-instantiation:
12276      template decl-specifier-seq [opt] declarator [opt] ;
12277
12278    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12279    supposed to be allowed.  A defect report has been filed about this
12280    issue.
12281
12282    GNU Extension:
12283
12284    explicit-instantiation:
12285      storage-class-specifier template
12286        decl-specifier-seq [opt] declarator [opt] ;
12287      function-specifier template
12288        decl-specifier-seq [opt] declarator [opt] ;  */
12289
12290 static void
12291 cp_parser_explicit_instantiation (cp_parser* parser)
12292 {
12293   int declares_class_or_enum;
12294   cp_decl_specifier_seq decl_specifiers;
12295   tree extension_specifier = NULL_TREE;
12296
12297   timevar_push (TV_TEMPLATE_INST);
12298
12299   /* Look for an (optional) storage-class-specifier or
12300      function-specifier.  */
12301   if (cp_parser_allow_gnu_extensions_p (parser))
12302     {
12303       extension_specifier
12304         = cp_parser_storage_class_specifier_opt (parser);
12305       if (!extension_specifier)
12306         extension_specifier
12307           = cp_parser_function_specifier_opt (parser,
12308                                               /*decl_specs=*/NULL);
12309     }
12310
12311   /* Look for the `template' keyword.  */
12312   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12313   /* Let the front end know that we are processing an explicit
12314      instantiation.  */
12315   begin_explicit_instantiation ();
12316   /* [temp.explicit] says that we are supposed to ignore access
12317      control while processing explicit instantiation directives.  */
12318   push_deferring_access_checks (dk_no_check);
12319   /* Parse a decl-specifier-seq.  */
12320   cp_parser_decl_specifier_seq (parser,
12321                                 CP_PARSER_FLAGS_OPTIONAL,
12322                                 &decl_specifiers,
12323                                 &declares_class_or_enum);
12324   /* If there was exactly one decl-specifier, and it declared a class,
12325      and there's no declarator, then we have an explicit type
12326      instantiation.  */
12327   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12328     {
12329       tree type;
12330
12331       type = check_tag_decl (&decl_specifiers);
12332       /* Turn access control back on for names used during
12333          template instantiation.  */
12334       pop_deferring_access_checks ();
12335       if (type)
12336         do_type_instantiation (type, extension_specifier,
12337                                /*complain=*/tf_error);
12338     }
12339   else
12340     {
12341       cp_declarator *declarator;
12342       tree decl;
12343
12344       /* Parse the declarator.  */
12345       declarator
12346         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12347                                 /*ctor_dtor_or_conv_p=*/NULL,
12348                                 /*parenthesized_p=*/NULL,
12349                                 /*member_p=*/false);
12350       if (declares_class_or_enum & 2)
12351         cp_parser_check_for_definition_in_return_type (declarator,
12352                                                        decl_specifiers.type,
12353                                                        decl_specifiers.type_location);
12354       if (declarator != cp_error_declarator)
12355         {
12356           if (decl_specifiers.specs[(int)ds_inline])
12357             permerror (input_location, "explicit instantiation shall not use"
12358                        " %<inline%> specifier");
12359           if (decl_specifiers.specs[(int)ds_constexpr])
12360             permerror (input_location, "explicit instantiation shall not use"
12361                        " %<constexpr%> specifier");
12362
12363           decl = grokdeclarator (declarator, &decl_specifiers,
12364                                  NORMAL, 0, &decl_specifiers.attributes);
12365           /* Turn access control back on for names used during
12366              template instantiation.  */
12367           pop_deferring_access_checks ();
12368           /* Do the explicit instantiation.  */
12369           do_decl_instantiation (decl, extension_specifier);
12370         }
12371       else
12372         {
12373           pop_deferring_access_checks ();
12374           /* Skip the body of the explicit instantiation.  */
12375           cp_parser_skip_to_end_of_statement (parser);
12376         }
12377     }
12378   /* We're done with the instantiation.  */
12379   end_explicit_instantiation ();
12380
12381   cp_parser_consume_semicolon_at_end_of_statement (parser);
12382
12383   timevar_pop (TV_TEMPLATE_INST);
12384 }
12385
12386 /* Parse an explicit-specialization.
12387
12388    explicit-specialization:
12389      template < > declaration
12390
12391    Although the standard says `declaration', what it really means is:
12392
12393    explicit-specialization:
12394      template <> decl-specifier [opt] init-declarator [opt] ;
12395      template <> function-definition
12396      template <> explicit-specialization
12397      template <> template-declaration  */
12398
12399 static void
12400 cp_parser_explicit_specialization (cp_parser* parser)
12401 {
12402   bool need_lang_pop;
12403   cp_token *token = cp_lexer_peek_token (parser->lexer);
12404
12405   /* Look for the `template' keyword.  */
12406   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12407   /* Look for the `<'.  */
12408   cp_parser_require (parser, CPP_LESS, RT_LESS);
12409   /* Look for the `>'.  */
12410   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12411   /* We have processed another parameter list.  */
12412   ++parser->num_template_parameter_lists;
12413   /* [temp]
12414
12415      A template ... explicit specialization ... shall not have C
12416      linkage.  */
12417   if (current_lang_name == lang_name_c)
12418     {
12419       error_at (token->location, "template specialization with C linkage");
12420       /* Give it C++ linkage to avoid confusing other parts of the
12421          front end.  */
12422       push_lang_context (lang_name_cplusplus);
12423       need_lang_pop = true;
12424     }
12425   else
12426     need_lang_pop = false;
12427   /* Let the front end know that we are beginning a specialization.  */
12428   if (!begin_specialization ())
12429     {
12430       end_specialization ();
12431       return;
12432     }
12433
12434   /* If the next keyword is `template', we need to figure out whether
12435      or not we're looking a template-declaration.  */
12436   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12437     {
12438       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12439           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12440         cp_parser_template_declaration_after_export (parser,
12441                                                      /*member_p=*/false);
12442       else
12443         cp_parser_explicit_specialization (parser);
12444     }
12445   else
12446     /* Parse the dependent declaration.  */
12447     cp_parser_single_declaration (parser,
12448                                   /*checks=*/NULL,
12449                                   /*member_p=*/false,
12450                                   /*explicit_specialization_p=*/true,
12451                                   /*friend_p=*/NULL);
12452   /* We're done with the specialization.  */
12453   end_specialization ();
12454   /* For the erroneous case of a template with C linkage, we pushed an
12455      implicit C++ linkage scope; exit that scope now.  */
12456   if (need_lang_pop)
12457     pop_lang_context ();
12458   /* We're done with this parameter list.  */
12459   --parser->num_template_parameter_lists;
12460 }
12461
12462 /* Parse a type-specifier.
12463
12464    type-specifier:
12465      simple-type-specifier
12466      class-specifier
12467      enum-specifier
12468      elaborated-type-specifier
12469      cv-qualifier
12470
12471    GNU Extension:
12472
12473    type-specifier:
12474      __complex__
12475
12476    Returns a representation of the type-specifier.  For a
12477    class-specifier, enum-specifier, or elaborated-type-specifier, a
12478    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12479
12480    The parser flags FLAGS is used to control type-specifier parsing.
12481
12482    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12483    in a decl-specifier-seq.
12484
12485    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12486    class-specifier, enum-specifier, or elaborated-type-specifier, then
12487    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12488    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12489    zero.
12490
12491    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12492    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12493    is set to FALSE.  */
12494
12495 static tree
12496 cp_parser_type_specifier (cp_parser* parser,
12497                           cp_parser_flags flags,
12498                           cp_decl_specifier_seq *decl_specs,
12499                           bool is_declaration,
12500                           int* declares_class_or_enum,
12501                           bool* is_cv_qualifier)
12502 {
12503   tree type_spec = NULL_TREE;
12504   cp_token *token;
12505   enum rid keyword;
12506   cp_decl_spec ds = ds_last;
12507
12508   /* Assume this type-specifier does not declare a new type.  */
12509   if (declares_class_or_enum)
12510     *declares_class_or_enum = 0;
12511   /* And that it does not specify a cv-qualifier.  */
12512   if (is_cv_qualifier)
12513     *is_cv_qualifier = false;
12514   /* Peek at the next token.  */
12515   token = cp_lexer_peek_token (parser->lexer);
12516
12517   /* If we're looking at a keyword, we can use that to guide the
12518      production we choose.  */
12519   keyword = token->keyword;
12520   switch (keyword)
12521     {
12522     case RID_ENUM:
12523       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12524         goto elaborated_type_specifier;
12525
12526       /* Look for the enum-specifier.  */
12527       type_spec = cp_parser_enum_specifier (parser);
12528       /* If that worked, we're done.  */
12529       if (type_spec)
12530         {
12531           if (declares_class_or_enum)
12532             *declares_class_or_enum = 2;
12533           if (decl_specs)
12534             cp_parser_set_decl_spec_type (decl_specs,
12535                                           type_spec,
12536                                           token->location,
12537                                           /*user_defined_p=*/true);
12538           return type_spec;
12539         }
12540       else
12541         goto elaborated_type_specifier;
12542
12543       /* Any of these indicate either a class-specifier, or an
12544          elaborated-type-specifier.  */
12545     case RID_CLASS:
12546     case RID_STRUCT:
12547     case RID_UNION:
12548       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12549         goto elaborated_type_specifier;
12550
12551       /* Parse tentatively so that we can back up if we don't find a
12552          class-specifier.  */
12553       cp_parser_parse_tentatively (parser);
12554       /* Look for the class-specifier.  */
12555       type_spec = cp_parser_class_specifier (parser);
12556       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12557       /* If that worked, we're done.  */
12558       if (cp_parser_parse_definitely (parser))
12559         {
12560           if (declares_class_or_enum)
12561             *declares_class_or_enum = 2;
12562           if (decl_specs)
12563             cp_parser_set_decl_spec_type (decl_specs,
12564                                           type_spec,
12565                                           token->location,
12566                                           /*user_defined_p=*/true);
12567           return type_spec;
12568         }
12569
12570       /* Fall through.  */
12571     elaborated_type_specifier:
12572       /* We're declaring (not defining) a class or enum.  */
12573       if (declares_class_or_enum)
12574         *declares_class_or_enum = 1;
12575
12576       /* Fall through.  */
12577     case RID_TYPENAME:
12578       /* Look for an elaborated-type-specifier.  */
12579       type_spec
12580         = (cp_parser_elaborated_type_specifier
12581            (parser,
12582             decl_specs && decl_specs->specs[(int) ds_friend],
12583             is_declaration));
12584       if (decl_specs)
12585         cp_parser_set_decl_spec_type (decl_specs,
12586                                       type_spec,
12587                                       token->location,
12588                                       /*user_defined_p=*/true);
12589       return type_spec;
12590
12591     case RID_CONST:
12592       ds = ds_const;
12593       if (is_cv_qualifier)
12594         *is_cv_qualifier = true;
12595       break;
12596
12597     case RID_VOLATILE:
12598       ds = ds_volatile;
12599       if (is_cv_qualifier)
12600         *is_cv_qualifier = true;
12601       break;
12602
12603     case RID_RESTRICT:
12604       ds = ds_restrict;
12605       if (is_cv_qualifier)
12606         *is_cv_qualifier = true;
12607       break;
12608
12609     case RID_COMPLEX:
12610       /* The `__complex__' keyword is a GNU extension.  */
12611       ds = ds_complex;
12612       break;
12613
12614     default:
12615       break;
12616     }
12617
12618   /* Handle simple keywords.  */
12619   if (ds != ds_last)
12620     {
12621       if (decl_specs)
12622         {
12623           ++decl_specs->specs[(int)ds];
12624           decl_specs->any_specifiers_p = true;
12625         }
12626       return cp_lexer_consume_token (parser->lexer)->u.value;
12627     }
12628
12629   /* If we do not already have a type-specifier, assume we are looking
12630      at a simple-type-specifier.  */
12631   type_spec = cp_parser_simple_type_specifier (parser,
12632                                                decl_specs,
12633                                                flags);
12634
12635   /* If we didn't find a type-specifier, and a type-specifier was not
12636      optional in this context, issue an error message.  */
12637   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12638     {
12639       cp_parser_error (parser, "expected type specifier");
12640       return error_mark_node;
12641     }
12642
12643   return type_spec;
12644 }
12645
12646 /* Parse a simple-type-specifier.
12647
12648    simple-type-specifier:
12649      :: [opt] nested-name-specifier [opt] type-name
12650      :: [opt] nested-name-specifier template template-id
12651      char
12652      wchar_t
12653      bool
12654      short
12655      int
12656      long
12657      signed
12658      unsigned
12659      float
12660      double
12661      void
12662
12663    C++0x Extension:
12664
12665    simple-type-specifier:
12666      auto
12667      decltype ( expression )   
12668      char16_t
12669      char32_t
12670      __underlying_type ( type-id )
12671
12672    GNU Extension:
12673
12674    simple-type-specifier:
12675      __int128
12676      __typeof__ unary-expression
12677      __typeof__ ( type-id )
12678
12679    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12680    appropriately updated.  */
12681
12682 static tree
12683 cp_parser_simple_type_specifier (cp_parser* parser,
12684                                  cp_decl_specifier_seq *decl_specs,
12685                                  cp_parser_flags flags)
12686 {
12687   tree type = NULL_TREE;
12688   cp_token *token;
12689
12690   /* Peek at the next token.  */
12691   token = cp_lexer_peek_token (parser->lexer);
12692
12693   /* If we're looking at a keyword, things are easy.  */
12694   switch (token->keyword)
12695     {
12696     case RID_CHAR:
12697       if (decl_specs)
12698         decl_specs->explicit_char_p = true;
12699       type = char_type_node;
12700       break;
12701     case RID_CHAR16:
12702       type = char16_type_node;
12703       break;
12704     case RID_CHAR32:
12705       type = char32_type_node;
12706       break;
12707     case RID_WCHAR:
12708       type = wchar_type_node;
12709       break;
12710     case RID_BOOL:
12711       type = boolean_type_node;
12712       break;
12713     case RID_SHORT:
12714       if (decl_specs)
12715         ++decl_specs->specs[(int) ds_short];
12716       type = short_integer_type_node;
12717       break;
12718     case RID_INT:
12719       if (decl_specs)
12720         decl_specs->explicit_int_p = true;
12721       type = integer_type_node;
12722       break;
12723     case RID_INT128:
12724       if (!int128_integer_type_node)
12725         break;
12726       if (decl_specs)
12727         decl_specs->explicit_int128_p = true;
12728       type = int128_integer_type_node;
12729       break;
12730     case RID_LONG:
12731       if (decl_specs)
12732         ++decl_specs->specs[(int) ds_long];
12733       type = long_integer_type_node;
12734       break;
12735     case RID_SIGNED:
12736       if (decl_specs)
12737         ++decl_specs->specs[(int) ds_signed];
12738       type = integer_type_node;
12739       break;
12740     case RID_UNSIGNED:
12741       if (decl_specs)
12742         ++decl_specs->specs[(int) ds_unsigned];
12743       type = unsigned_type_node;
12744       break;
12745     case RID_FLOAT:
12746       type = float_type_node;
12747       break;
12748     case RID_DOUBLE:
12749       type = double_type_node;
12750       break;
12751     case RID_VOID:
12752       type = void_type_node;
12753       break;
12754       
12755     case RID_AUTO:
12756       maybe_warn_cpp0x (CPP0X_AUTO);
12757       type = make_auto ();
12758       break;
12759
12760     case RID_DECLTYPE:
12761       /* Since DR 743, decltype can either be a simple-type-specifier by
12762          itself or begin a nested-name-specifier.  Parsing it will replace
12763          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
12764          handling below decide what to do.  */
12765       cp_parser_decltype (parser);
12766       cp_lexer_set_token_position (parser->lexer, token);
12767       break;
12768
12769     case RID_TYPEOF:
12770       /* Consume the `typeof' token.  */
12771       cp_lexer_consume_token (parser->lexer);
12772       /* Parse the operand to `typeof'.  */
12773       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12774       /* If it is not already a TYPE, take its type.  */
12775       if (!TYPE_P (type))
12776         type = finish_typeof (type);
12777
12778       if (decl_specs)
12779         cp_parser_set_decl_spec_type (decl_specs, type,
12780                                       token->location,
12781                                       /*user_defined_p=*/true);
12782
12783       return type;
12784
12785     case RID_UNDERLYING_TYPE:
12786       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12787
12788       if (decl_specs)
12789         cp_parser_set_decl_spec_type (decl_specs, type,
12790                                       token->location,
12791                                       /*user_defined_p=*/true);
12792
12793       return type;
12794
12795     default:
12796       break;
12797     }
12798
12799   /* If token is an already-parsed decltype not followed by ::,
12800      it's a simple-type-specifier.  */
12801   if (token->type == CPP_DECLTYPE
12802       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
12803     {
12804       type = token->u.value;
12805       if (decl_specs)
12806         cp_parser_set_decl_spec_type (decl_specs, type,
12807                                       token->location,
12808                                       /*user_defined_p=*/true);
12809       cp_lexer_consume_token (parser->lexer);
12810       return type;
12811     }
12812
12813   /* If the type-specifier was for a built-in type, we're done.  */
12814   if (type)
12815     {
12816       /* Record the type.  */
12817       if (decl_specs
12818           && (token->keyword != RID_SIGNED
12819               && token->keyword != RID_UNSIGNED
12820               && token->keyword != RID_SHORT
12821               && token->keyword != RID_LONG))
12822         cp_parser_set_decl_spec_type (decl_specs,
12823                                       type,
12824                                       token->location,
12825                                       /*user_defined=*/false);
12826       if (decl_specs)
12827         decl_specs->any_specifiers_p = true;
12828
12829       /* Consume the token.  */
12830       cp_lexer_consume_token (parser->lexer);
12831
12832       /* There is no valid C++ program where a non-template type is
12833          followed by a "<".  That usually indicates that the user thought
12834          that the type was a template.  */
12835       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12836
12837       return TYPE_NAME (type);
12838     }
12839
12840   /* The type-specifier must be a user-defined type.  */
12841   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12842     {
12843       bool qualified_p;
12844       bool global_p;
12845
12846       /* Don't gobble tokens or issue error messages if this is an
12847          optional type-specifier.  */
12848       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12849         cp_parser_parse_tentatively (parser);
12850
12851       /* Look for the optional `::' operator.  */
12852       global_p
12853         = (cp_parser_global_scope_opt (parser,
12854                                        /*current_scope_valid_p=*/false)
12855            != NULL_TREE);
12856       /* Look for the nested-name specifier.  */
12857       qualified_p
12858         = (cp_parser_nested_name_specifier_opt (parser,
12859                                                 /*typename_keyword_p=*/false,
12860                                                 /*check_dependency_p=*/true,
12861                                                 /*type_p=*/false,
12862                                                 /*is_declaration=*/false)
12863            != NULL_TREE);
12864       token = cp_lexer_peek_token (parser->lexer);
12865       /* If we have seen a nested-name-specifier, and the next token
12866          is `template', then we are using the template-id production.  */
12867       if (parser->scope
12868           && cp_parser_optional_template_keyword (parser))
12869         {
12870           /* Look for the template-id.  */
12871           type = cp_parser_template_id (parser,
12872                                         /*template_keyword_p=*/true,
12873                                         /*check_dependency_p=*/true,
12874                                         /*is_declaration=*/false);
12875           /* If the template-id did not name a type, we are out of
12876              luck.  */
12877           if (TREE_CODE (type) != TYPE_DECL)
12878             {
12879               cp_parser_error (parser, "expected template-id for type");
12880               type = NULL_TREE;
12881             }
12882         }
12883       /* Otherwise, look for a type-name.  */
12884       else
12885         type = cp_parser_type_name (parser);
12886       /* Keep track of all name-lookups performed in class scopes.  */
12887       if (type
12888           && !global_p
12889           && !qualified_p
12890           && TREE_CODE (type) == TYPE_DECL
12891           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12892         maybe_note_name_used_in_class (DECL_NAME (type), type);
12893       /* If it didn't work out, we don't have a TYPE.  */
12894       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12895           && !cp_parser_parse_definitely (parser))
12896         type = NULL_TREE;
12897       if (type && decl_specs)
12898         cp_parser_set_decl_spec_type (decl_specs, type,
12899                                       token->location,
12900                                       /*user_defined=*/true);
12901     }
12902
12903   /* If we didn't get a type-name, issue an error message.  */
12904   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12905     {
12906       cp_parser_error (parser, "expected type-name");
12907       return error_mark_node;
12908     }
12909
12910   if (type && type != error_mark_node)
12911     {
12912       /* See if TYPE is an Objective-C type, and if so, parse and
12913          accept any protocol references following it.  Do this before
12914          the cp_parser_check_for_invalid_template_id() call, because
12915          Objective-C types can be followed by '<...>' which would
12916          enclose protocol names rather than template arguments, and so
12917          everything is fine.  */
12918       if (c_dialect_objc () && !parser->scope
12919           && (objc_is_id (type) || objc_is_class_name (type)))
12920         {
12921           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12922           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12923
12924           /* Clobber the "unqualified" type previously entered into
12925              DECL_SPECS with the new, improved protocol-qualified version.  */
12926           if (decl_specs)
12927             decl_specs->type = qual_type;
12928
12929           return qual_type;
12930         }
12931
12932       /* There is no valid C++ program where a non-template type is
12933          followed by a "<".  That usually indicates that the user
12934          thought that the type was a template.  */
12935       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12936                                                token->location);
12937     }
12938
12939   return type;
12940 }
12941
12942 /* Parse a type-name.
12943
12944    type-name:
12945      class-name
12946      enum-name
12947      typedef-name
12948
12949    enum-name:
12950      identifier
12951
12952    typedef-name:
12953      identifier
12954
12955    Returns a TYPE_DECL for the type.  */
12956
12957 static tree
12958 cp_parser_type_name (cp_parser* parser)
12959 {
12960   tree type_decl;
12961
12962   /* We can't know yet whether it is a class-name or not.  */
12963   cp_parser_parse_tentatively (parser);
12964   /* Try a class-name.  */
12965   type_decl = cp_parser_class_name (parser,
12966                                     /*typename_keyword_p=*/false,
12967                                     /*template_keyword_p=*/false,
12968                                     none_type,
12969                                     /*check_dependency_p=*/true,
12970                                     /*class_head_p=*/false,
12971                                     /*is_declaration=*/false);
12972   /* If it's not a class-name, keep looking.  */
12973   if (!cp_parser_parse_definitely (parser))
12974     {
12975       /* It must be a typedef-name or an enum-name.  */
12976       return cp_parser_nonclass_name (parser);
12977     }
12978
12979   return type_decl;
12980 }
12981
12982 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12983
12984    enum-name:
12985      identifier
12986
12987    typedef-name:
12988      identifier
12989
12990    Returns a TYPE_DECL for the type.  */
12991
12992 static tree
12993 cp_parser_nonclass_name (cp_parser* parser)
12994 {
12995   tree type_decl;
12996   tree identifier;
12997
12998   cp_token *token = cp_lexer_peek_token (parser->lexer);
12999   identifier = cp_parser_identifier (parser);
13000   if (identifier == error_mark_node)
13001     return error_mark_node;
13002
13003   /* Look up the type-name.  */
13004   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13005
13006   if (TREE_CODE (type_decl) != TYPE_DECL
13007       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13008     {
13009       /* See if this is an Objective-C type.  */
13010       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13011       tree type = objc_get_protocol_qualified_type (identifier, protos);
13012       if (type)
13013         type_decl = TYPE_NAME (type);
13014     }
13015
13016   /* Issue an error if we did not find a type-name.  */
13017   if (TREE_CODE (type_decl) != TYPE_DECL
13018       /* In Objective-C, we have the complication that class names are
13019          normally type names and start declarations (eg, the
13020          "NSObject" in "NSObject *object;"), but can be used in an
13021          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13022          is an expression.  So, a classname followed by a dot is not a
13023          valid type-name.  */
13024       || (objc_is_class_name (TREE_TYPE (type_decl))
13025           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13026     {
13027       if (!cp_parser_simulate_error (parser))
13028         cp_parser_name_lookup_error (parser, identifier, type_decl,
13029                                      NLE_TYPE, token->location);
13030       return error_mark_node;
13031     }
13032   /* Remember that the name was used in the definition of the
13033      current class so that we can check later to see if the
13034      meaning would have been different after the class was
13035      entirely defined.  */
13036   else if (type_decl != error_mark_node
13037            && !parser->scope)
13038     maybe_note_name_used_in_class (identifier, type_decl);
13039   
13040   return type_decl;
13041 }
13042
13043 /* Parse an elaborated-type-specifier.  Note that the grammar given
13044    here incorporates the resolution to DR68.
13045
13046    elaborated-type-specifier:
13047      class-key :: [opt] nested-name-specifier [opt] identifier
13048      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13049      enum-key :: [opt] nested-name-specifier [opt] identifier
13050      typename :: [opt] nested-name-specifier identifier
13051      typename :: [opt] nested-name-specifier template [opt]
13052        template-id
13053
13054    GNU extension:
13055
13056    elaborated-type-specifier:
13057      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13058      class-key attributes :: [opt] nested-name-specifier [opt]
13059                template [opt] template-id
13060      enum attributes :: [opt] nested-name-specifier [opt] identifier
13061
13062    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13063    declared `friend'.  If IS_DECLARATION is TRUE, then this
13064    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13065    something is being declared.
13066
13067    Returns the TYPE specified.  */
13068
13069 static tree
13070 cp_parser_elaborated_type_specifier (cp_parser* parser,
13071                                      bool is_friend,
13072                                      bool is_declaration)
13073 {
13074   enum tag_types tag_type;
13075   tree identifier;
13076   tree type = NULL_TREE;
13077   tree attributes = NULL_TREE;
13078   tree globalscope;
13079   cp_token *token = NULL;
13080
13081   /* See if we're looking at the `enum' keyword.  */
13082   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13083     {
13084       /* Consume the `enum' token.  */
13085       cp_lexer_consume_token (parser->lexer);
13086       /* Remember that it's an enumeration type.  */
13087       tag_type = enum_type;
13088       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13089          enums) is used here.  */
13090       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13091           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13092         {
13093             pedwarn (input_location, 0, "elaborated-type-specifier "
13094                       "for a scoped enum must not use the %<%D%> keyword",
13095                       cp_lexer_peek_token (parser->lexer)->u.value);
13096           /* Consume the `struct' or `class' and parse it anyway.  */
13097           cp_lexer_consume_token (parser->lexer);
13098         }
13099       /* Parse the attributes.  */
13100       attributes = cp_parser_attributes_opt (parser);
13101     }
13102   /* Or, it might be `typename'.  */
13103   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13104                                            RID_TYPENAME))
13105     {
13106       /* Consume the `typename' token.  */
13107       cp_lexer_consume_token (parser->lexer);
13108       /* Remember that it's a `typename' type.  */
13109       tag_type = typename_type;
13110     }
13111   /* Otherwise it must be a class-key.  */
13112   else
13113     {
13114       tag_type = cp_parser_class_key (parser);
13115       if (tag_type == none_type)
13116         return error_mark_node;
13117       /* Parse the attributes.  */
13118       attributes = cp_parser_attributes_opt (parser);
13119     }
13120
13121   /* Look for the `::' operator.  */
13122   globalscope =  cp_parser_global_scope_opt (parser,
13123                                              /*current_scope_valid_p=*/false);
13124   /* Look for the nested-name-specifier.  */
13125   if (tag_type == typename_type && !globalscope)
13126     {
13127       if (!cp_parser_nested_name_specifier (parser,
13128                                            /*typename_keyword_p=*/true,
13129                                            /*check_dependency_p=*/true,
13130                                            /*type_p=*/true,
13131                                             is_declaration))
13132         return error_mark_node;
13133     }
13134   else
13135     /* Even though `typename' is not present, the proposed resolution
13136        to Core Issue 180 says that in `class A<T>::B', `B' should be
13137        considered a type-name, even if `A<T>' is dependent.  */
13138     cp_parser_nested_name_specifier_opt (parser,
13139                                          /*typename_keyword_p=*/true,
13140                                          /*check_dependency_p=*/true,
13141                                          /*type_p=*/true,
13142                                          is_declaration);
13143  /* For everything but enumeration types, consider a template-id.
13144     For an enumeration type, consider only a plain identifier.  */
13145   if (tag_type != enum_type)
13146     {
13147       bool template_p = false;
13148       tree decl;
13149
13150       /* Allow the `template' keyword.  */
13151       template_p = cp_parser_optional_template_keyword (parser);
13152       /* If we didn't see `template', we don't know if there's a
13153          template-id or not.  */
13154       if (!template_p)
13155         cp_parser_parse_tentatively (parser);
13156       /* Parse the template-id.  */
13157       token = cp_lexer_peek_token (parser->lexer);
13158       decl = cp_parser_template_id (parser, template_p,
13159                                     /*check_dependency_p=*/true,
13160                                     is_declaration);
13161       /* If we didn't find a template-id, look for an ordinary
13162          identifier.  */
13163       if (!template_p && !cp_parser_parse_definitely (parser))
13164         ;
13165       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13166          in effect, then we must assume that, upon instantiation, the
13167          template will correspond to a class.  */
13168       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13169                && tag_type == typename_type)
13170         type = make_typename_type (parser->scope, decl,
13171                                    typename_type,
13172                                    /*complain=*/tf_error);
13173       /* If the `typename' keyword is in effect and DECL is not a type
13174          decl. Then type is non existant.   */
13175       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13176         type = NULL_TREE; 
13177       else 
13178         type = TREE_TYPE (decl);
13179     }
13180
13181   if (!type)
13182     {
13183       token = cp_lexer_peek_token (parser->lexer);
13184       identifier = cp_parser_identifier (parser);
13185
13186       if (identifier == error_mark_node)
13187         {
13188           parser->scope = NULL_TREE;
13189           return error_mark_node;
13190         }
13191
13192       /* For a `typename', we needn't call xref_tag.  */
13193       if (tag_type == typename_type
13194           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13195         return cp_parser_make_typename_type (parser, parser->scope,
13196                                              identifier,
13197                                              token->location);
13198       /* Look up a qualified name in the usual way.  */
13199       if (parser->scope)
13200         {
13201           tree decl;
13202           tree ambiguous_decls;
13203
13204           decl = cp_parser_lookup_name (parser, identifier,
13205                                         tag_type,
13206                                         /*is_template=*/false,
13207                                         /*is_namespace=*/false,
13208                                         /*check_dependency=*/true,
13209                                         &ambiguous_decls,
13210                                         token->location);
13211
13212           /* If the lookup was ambiguous, an error will already have been
13213              issued.  */
13214           if (ambiguous_decls)
13215             return error_mark_node;
13216
13217           /* If we are parsing friend declaration, DECL may be a
13218              TEMPLATE_DECL tree node here.  However, we need to check
13219              whether this TEMPLATE_DECL results in valid code.  Consider
13220              the following example:
13221
13222                namespace N {
13223                  template <class T> class C {};
13224                }
13225                class X {
13226                  template <class T> friend class N::C; // #1, valid code
13227                };
13228                template <class T> class Y {
13229                  friend class N::C;                    // #2, invalid code
13230                };
13231
13232              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13233              name lookup of `N::C'.  We see that friend declaration must
13234              be template for the code to be valid.  Note that
13235              processing_template_decl does not work here since it is
13236              always 1 for the above two cases.  */
13237
13238           decl = (cp_parser_maybe_treat_template_as_class
13239                   (decl, /*tag_name_p=*/is_friend
13240                          && parser->num_template_parameter_lists));
13241
13242           if (TREE_CODE (decl) != TYPE_DECL)
13243             {
13244               cp_parser_diagnose_invalid_type_name (parser,
13245                                                     parser->scope,
13246                                                     identifier,
13247                                                     token->location);
13248               return error_mark_node;
13249             }
13250
13251           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13252             {
13253               bool allow_template = (parser->num_template_parameter_lists
13254                                       || DECL_SELF_REFERENCE_P (decl));
13255               type = check_elaborated_type_specifier (tag_type, decl, 
13256                                                       allow_template);
13257
13258               if (type == error_mark_node)
13259                 return error_mark_node;
13260             }
13261
13262           /* Forward declarations of nested types, such as
13263
13264                class C1::C2;
13265                class C1::C2::C3;
13266
13267              are invalid unless all components preceding the final '::'
13268              are complete.  If all enclosing types are complete, these
13269              declarations become merely pointless.
13270
13271              Invalid forward declarations of nested types are errors
13272              caught elsewhere in parsing.  Those that are pointless arrive
13273              here.  */
13274
13275           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13276               && !is_friend && !processing_explicit_instantiation)
13277             warning (0, "declaration %qD does not declare anything", decl);
13278
13279           type = TREE_TYPE (decl);
13280         }
13281       else
13282         {
13283           /* An elaborated-type-specifier sometimes introduces a new type and
13284              sometimes names an existing type.  Normally, the rule is that it
13285              introduces a new type only if there is not an existing type of
13286              the same name already in scope.  For example, given:
13287
13288                struct S {};
13289                void f() { struct S s; }
13290
13291              the `struct S' in the body of `f' is the same `struct S' as in
13292              the global scope; the existing definition is used.  However, if
13293              there were no global declaration, this would introduce a new
13294              local class named `S'.
13295
13296              An exception to this rule applies to the following code:
13297
13298                namespace N { struct S; }
13299
13300              Here, the elaborated-type-specifier names a new type
13301              unconditionally; even if there is already an `S' in the
13302              containing scope this declaration names a new type.
13303              This exception only applies if the elaborated-type-specifier
13304              forms the complete declaration:
13305
13306                [class.name]
13307
13308                A declaration consisting solely of `class-key identifier ;' is
13309                either a redeclaration of the name in the current scope or a
13310                forward declaration of the identifier as a class name.  It
13311                introduces the name into the current scope.
13312
13313              We are in this situation precisely when the next token is a `;'.
13314
13315              An exception to the exception is that a `friend' declaration does
13316              *not* name a new type; i.e., given:
13317
13318                struct S { friend struct T; };
13319
13320              `T' is not a new type in the scope of `S'.
13321
13322              Also, `new struct S' or `sizeof (struct S)' never results in the
13323              definition of a new type; a new type can only be declared in a
13324              declaration context.  */
13325
13326           tag_scope ts;
13327           bool template_p;
13328
13329           if (is_friend)
13330             /* Friends have special name lookup rules.  */
13331             ts = ts_within_enclosing_non_class;
13332           else if (is_declaration
13333                    && cp_lexer_next_token_is (parser->lexer,
13334                                               CPP_SEMICOLON))
13335             /* This is a `class-key identifier ;' */
13336             ts = ts_current;
13337           else
13338             ts = ts_global;
13339
13340           template_p =
13341             (parser->num_template_parameter_lists
13342              && (cp_parser_next_token_starts_class_definition_p (parser)
13343                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13344           /* An unqualified name was used to reference this type, so
13345              there were no qualifying templates.  */
13346           if (!cp_parser_check_template_parameters (parser,
13347                                                     /*num_templates=*/0,
13348                                                     token->location,
13349                                                     /*declarator=*/NULL))
13350             return error_mark_node;
13351           type = xref_tag (tag_type, identifier, ts, template_p);
13352         }
13353     }
13354
13355   if (type == error_mark_node)
13356     return error_mark_node;
13357
13358   /* Allow attributes on forward declarations of classes.  */
13359   if (attributes)
13360     {
13361       if (TREE_CODE (type) == TYPENAME_TYPE)
13362         warning (OPT_Wattributes,
13363                  "attributes ignored on uninstantiated type");
13364       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13365                && ! processing_explicit_instantiation)
13366         warning (OPT_Wattributes,
13367                  "attributes ignored on template instantiation");
13368       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13369         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13370       else
13371         warning (OPT_Wattributes,
13372                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13373     }
13374
13375   if (tag_type != enum_type)
13376     cp_parser_check_class_key (tag_type, type);
13377
13378   /* A "<" cannot follow an elaborated type specifier.  If that
13379      happens, the user was probably trying to form a template-id.  */
13380   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13381
13382   return type;
13383 }
13384
13385 /* Parse an enum-specifier.
13386
13387    enum-specifier:
13388      enum-head { enumerator-list [opt] }
13389
13390    enum-head:
13391      enum-key identifier [opt] enum-base [opt]
13392      enum-key nested-name-specifier identifier enum-base [opt]
13393
13394    enum-key:
13395      enum
13396      enum class   [C++0x]
13397      enum struct  [C++0x]
13398
13399    enum-base:   [C++0x]
13400      : type-specifier-seq
13401
13402    opaque-enum-specifier:
13403      enum-key identifier enum-base [opt] ;
13404
13405    GNU Extensions:
13406      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13407        { enumerator-list [opt] }attributes[opt]
13408
13409    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13410    if the token stream isn't an enum-specifier after all.  */
13411
13412 static tree
13413 cp_parser_enum_specifier (cp_parser* parser)
13414 {
13415   tree identifier;
13416   tree type = NULL_TREE;
13417   tree prev_scope;
13418   tree nested_name_specifier = NULL_TREE;
13419   tree attributes;
13420   bool scoped_enum_p = false;
13421   bool has_underlying_type = false;
13422   bool nested_being_defined = false;
13423   bool new_value_list = false;
13424   bool is_new_type = false;
13425   bool is_anonymous = false;
13426   tree underlying_type = NULL_TREE;
13427   cp_token *type_start_token = NULL;
13428   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13429
13430   parser->colon_corrects_to_scope_p = false;
13431
13432   /* Parse tentatively so that we can back up if we don't find a
13433      enum-specifier.  */
13434   cp_parser_parse_tentatively (parser);
13435
13436   /* Caller guarantees that the current token is 'enum', an identifier
13437      possibly follows, and the token after that is an opening brace.
13438      If we don't have an identifier, fabricate an anonymous name for
13439      the enumeration being defined.  */
13440   cp_lexer_consume_token (parser->lexer);
13441
13442   /* Parse the "class" or "struct", which indicates a scoped
13443      enumeration type in C++0x.  */
13444   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13445       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13446     {
13447       if (cxx_dialect < cxx0x)
13448         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13449
13450       /* Consume the `struct' or `class' token.  */
13451       cp_lexer_consume_token (parser->lexer);
13452
13453       scoped_enum_p = true;
13454     }
13455
13456   attributes = cp_parser_attributes_opt (parser);
13457
13458   /* Clear the qualification.  */
13459   parser->scope = NULL_TREE;
13460   parser->qualifying_scope = NULL_TREE;
13461   parser->object_scope = NULL_TREE;
13462
13463   /* Figure out in what scope the declaration is being placed.  */
13464   prev_scope = current_scope ();
13465
13466   type_start_token = cp_lexer_peek_token (parser->lexer);
13467
13468   push_deferring_access_checks (dk_no_check);
13469   nested_name_specifier
13470       = cp_parser_nested_name_specifier_opt (parser,
13471                                              /*typename_keyword_p=*/true,
13472                                              /*check_dependency_p=*/false,
13473                                              /*type_p=*/false,
13474                                              /*is_declaration=*/false);
13475
13476   if (nested_name_specifier)
13477     {
13478       tree name;
13479
13480       identifier = cp_parser_identifier (parser);
13481       name =  cp_parser_lookup_name (parser, identifier,
13482                                      enum_type,
13483                                      /*is_template=*/false,
13484                                      /*is_namespace=*/false,
13485                                      /*check_dependency=*/true,
13486                                      /*ambiguous_decls=*/NULL,
13487                                      input_location);
13488       if (name)
13489         {
13490           type = TREE_TYPE (name);
13491           if (TREE_CODE (type) == TYPENAME_TYPE)
13492             {
13493               /* Are template enums allowed in ISO? */
13494               if (template_parm_scope_p ())
13495                 pedwarn (type_start_token->location, OPT_pedantic,
13496                          "%qD is an enumeration template", name);
13497               /* ignore a typename reference, for it will be solved by name
13498                  in start_enum.  */
13499               type = NULL_TREE;
13500             }
13501         }
13502       else
13503         error_at (type_start_token->location,
13504                   "%qD is not an enumerator-name", identifier);
13505     }
13506   else
13507     {
13508       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13509         identifier = cp_parser_identifier (parser);
13510       else
13511         {
13512           identifier = make_anon_name ();
13513           is_anonymous = true;
13514         }
13515     }
13516   pop_deferring_access_checks ();
13517
13518   /* Check for the `:' that denotes a specified underlying type in C++0x.
13519      Note that a ':' could also indicate a bitfield width, however.  */
13520   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13521     {
13522       cp_decl_specifier_seq type_specifiers;
13523
13524       /* Consume the `:'.  */
13525       cp_lexer_consume_token (parser->lexer);
13526
13527       /* Parse the type-specifier-seq.  */
13528       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13529                                     /*is_trailing_return=*/false,
13530                                     &type_specifiers);
13531
13532       /* At this point this is surely not elaborated type specifier.  */
13533       if (!cp_parser_parse_definitely (parser))
13534         return NULL_TREE;
13535
13536       if (cxx_dialect < cxx0x)
13537         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13538
13539       has_underlying_type = true;
13540
13541       /* If that didn't work, stop.  */
13542       if (type_specifiers.type != error_mark_node)
13543         {
13544           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13545                                             /*initialized=*/0, NULL);
13546           if (underlying_type == error_mark_node)
13547             underlying_type = NULL_TREE;
13548         }
13549     }
13550
13551   /* Look for the `{' but don't consume it yet.  */
13552   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13553     {
13554       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13555         {
13556           cp_parser_error (parser, "expected %<{%>");
13557           if (has_underlying_type)
13558             {
13559               type = NULL_TREE;
13560               goto out;
13561             }
13562         }
13563       /* An opaque-enum-specifier must have a ';' here.  */
13564       if ((scoped_enum_p || underlying_type)
13565           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13566         {
13567           cp_parser_error (parser, "expected %<;%> or %<{%>");
13568           if (has_underlying_type)
13569             {
13570               type = NULL_TREE;
13571               goto out;
13572             }
13573         }
13574     }
13575
13576   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13577     return NULL_TREE;
13578
13579   if (nested_name_specifier)
13580     {
13581       if (CLASS_TYPE_P (nested_name_specifier))
13582         {
13583           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13584           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13585           push_scope (nested_name_specifier);
13586         }
13587       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13588         {
13589           push_nested_namespace (nested_name_specifier);
13590         }
13591     }
13592
13593   /* Issue an error message if type-definitions are forbidden here.  */
13594   if (!cp_parser_check_type_definition (parser))
13595     type = error_mark_node;
13596   else
13597     /* Create the new type.  We do this before consuming the opening
13598        brace so the enum will be recorded as being on the line of its
13599        tag (or the 'enum' keyword, if there is no tag).  */
13600     type = start_enum (identifier, type, underlying_type,
13601                        scoped_enum_p, &is_new_type);
13602
13603   /* If the next token is not '{' it is an opaque-enum-specifier or an
13604      elaborated-type-specifier.  */
13605   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13606     {
13607       timevar_push (TV_PARSE_ENUM);
13608       if (nested_name_specifier)
13609         {
13610           /* The following catches invalid code such as:
13611              enum class S<int>::E { A, B, C }; */
13612           if (!processing_specialization
13613               && CLASS_TYPE_P (nested_name_specifier)
13614               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13615             error_at (type_start_token->location, "cannot add an enumerator "
13616                       "list to a template instantiation");
13617
13618           /* If that scope does not contain the scope in which the
13619              class was originally declared, the program is invalid.  */
13620           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13621             {
13622               if (at_namespace_scope_p ())
13623                 error_at (type_start_token->location,
13624                           "declaration of %qD in namespace %qD which does not "
13625                           "enclose %qD",
13626                           type, prev_scope, nested_name_specifier);
13627               else
13628                 error_at (type_start_token->location,
13629                           "declaration of %qD in %qD which does not enclose %qD",
13630                           type, prev_scope, nested_name_specifier);
13631               type = error_mark_node;
13632             }
13633         }
13634
13635       if (scoped_enum_p)
13636         begin_scope (sk_scoped_enum, type);
13637
13638       /* Consume the opening brace.  */
13639       cp_lexer_consume_token (parser->lexer);
13640
13641       if (type == error_mark_node)
13642         ; /* Nothing to add */
13643       else if (OPAQUE_ENUM_P (type)
13644                || (cxx_dialect > cxx98 && processing_specialization))
13645         {
13646           new_value_list = true;
13647           SET_OPAQUE_ENUM_P (type, false);
13648           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13649         }
13650       else
13651         {
13652           error_at (type_start_token->location, "multiple definition of %q#T", type);
13653           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13654                     "previous definition here");
13655           type = error_mark_node;
13656         }
13657
13658       if (type == error_mark_node)
13659         cp_parser_skip_to_end_of_block_or_statement (parser);
13660       /* If the next token is not '}', then there are some enumerators.  */
13661       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13662         cp_parser_enumerator_list (parser, type);
13663
13664       /* Consume the final '}'.  */
13665       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13666
13667       if (scoped_enum_p)
13668         finish_scope ();
13669       timevar_pop (TV_PARSE_ENUM);
13670     }
13671   else
13672     {
13673       /* If a ';' follows, then it is an opaque-enum-specifier
13674         and additional restrictions apply.  */
13675       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13676         {
13677           if (is_anonymous)
13678             error_at (type_start_token->location,
13679                       "opaque-enum-specifier without name");
13680           else if (nested_name_specifier)
13681             error_at (type_start_token->location,
13682                       "opaque-enum-specifier must use a simple identifier");
13683         }
13684     }
13685
13686   /* Look for trailing attributes to apply to this enumeration, and
13687      apply them if appropriate.  */
13688   if (cp_parser_allow_gnu_extensions_p (parser))
13689     {
13690       tree trailing_attr = cp_parser_attributes_opt (parser);
13691       trailing_attr = chainon (trailing_attr, attributes);
13692       cplus_decl_attributes (&type,
13693                              trailing_attr,
13694                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13695     }
13696
13697   /* Finish up the enumeration.  */
13698   if (type != error_mark_node)
13699     {
13700       if (new_value_list)
13701         finish_enum_value_list (type);
13702       if (is_new_type)
13703         finish_enum (type);
13704     }
13705
13706   if (nested_name_specifier)
13707     {
13708       if (CLASS_TYPE_P (nested_name_specifier))
13709         {
13710           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13711           pop_scope (nested_name_specifier);
13712         }
13713       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13714         {
13715           pop_nested_namespace (nested_name_specifier);
13716         }
13717     }
13718  out:
13719   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13720   return type;
13721 }
13722
13723 /* Parse an enumerator-list.  The enumerators all have the indicated
13724    TYPE.
13725
13726    enumerator-list:
13727      enumerator-definition
13728      enumerator-list , enumerator-definition  */
13729
13730 static void
13731 cp_parser_enumerator_list (cp_parser* parser, tree type)
13732 {
13733   while (true)
13734     {
13735       /* Parse an enumerator-definition.  */
13736       cp_parser_enumerator_definition (parser, type);
13737
13738       /* If the next token is not a ',', we've reached the end of
13739          the list.  */
13740       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13741         break;
13742       /* Otherwise, consume the `,' and keep going.  */
13743       cp_lexer_consume_token (parser->lexer);
13744       /* If the next token is a `}', there is a trailing comma.  */
13745       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13746         {
13747           if (!in_system_header)
13748             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13749           break;
13750         }
13751     }
13752 }
13753
13754 /* Parse an enumerator-definition.  The enumerator has the indicated
13755    TYPE.
13756
13757    enumerator-definition:
13758      enumerator
13759      enumerator = constant-expression
13760
13761    enumerator:
13762      identifier  */
13763
13764 static void
13765 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13766 {
13767   tree identifier;
13768   tree value;
13769   location_t loc;
13770
13771   /* Save the input location because we are interested in the location
13772      of the identifier and not the location of the explicit value.  */
13773   loc = cp_lexer_peek_token (parser->lexer)->location;
13774
13775   /* Look for the identifier.  */
13776   identifier = cp_parser_identifier (parser);
13777   if (identifier == error_mark_node)
13778     return;
13779
13780   /* If the next token is an '=', then there is an explicit value.  */
13781   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13782     {
13783       /* Consume the `=' token.  */
13784       cp_lexer_consume_token (parser->lexer);
13785       /* Parse the value.  */
13786       value = cp_parser_constant_expression (parser,
13787                                              /*allow_non_constant_p=*/false,
13788                                              NULL);
13789     }
13790   else
13791     value = NULL_TREE;
13792
13793   /* If we are processing a template, make sure the initializer of the
13794      enumerator doesn't contain any bare template parameter pack.  */
13795   if (check_for_bare_parameter_packs (value))
13796     value = error_mark_node;
13797
13798   /* integral_constant_value will pull out this expression, so make sure
13799      it's folded as appropriate.  */
13800   value = fold_non_dependent_expr (value);
13801
13802   /* Create the enumerator.  */
13803   build_enumerator (identifier, value, type, loc);
13804 }
13805
13806 /* Parse a namespace-name.
13807
13808    namespace-name:
13809      original-namespace-name
13810      namespace-alias
13811
13812    Returns the NAMESPACE_DECL for the namespace.  */
13813
13814 static tree
13815 cp_parser_namespace_name (cp_parser* parser)
13816 {
13817   tree identifier;
13818   tree namespace_decl;
13819
13820   cp_token *token = cp_lexer_peek_token (parser->lexer);
13821
13822   /* Get the name of the namespace.  */
13823   identifier = cp_parser_identifier (parser);
13824   if (identifier == error_mark_node)
13825     return error_mark_node;
13826
13827   /* Look up the identifier in the currently active scope.  Look only
13828      for namespaces, due to:
13829
13830        [basic.lookup.udir]
13831
13832        When looking up a namespace-name in a using-directive or alias
13833        definition, only namespace names are considered.
13834
13835      And:
13836
13837        [basic.lookup.qual]
13838
13839        During the lookup of a name preceding the :: scope resolution
13840        operator, object, function, and enumerator names are ignored.
13841
13842      (Note that cp_parser_qualifying_entity only calls this
13843      function if the token after the name is the scope resolution
13844      operator.)  */
13845   namespace_decl = cp_parser_lookup_name (parser, identifier,
13846                                           none_type,
13847                                           /*is_template=*/false,
13848                                           /*is_namespace=*/true,
13849                                           /*check_dependency=*/true,
13850                                           /*ambiguous_decls=*/NULL,
13851                                           token->location);
13852   /* If it's not a namespace, issue an error.  */
13853   if (namespace_decl == error_mark_node
13854       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13855     {
13856       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13857         error_at (token->location, "%qD is not a namespace-name", identifier);
13858       cp_parser_error (parser, "expected namespace-name");
13859       namespace_decl = error_mark_node;
13860     }
13861
13862   return namespace_decl;
13863 }
13864
13865 /* Parse a namespace-definition.
13866
13867    namespace-definition:
13868      named-namespace-definition
13869      unnamed-namespace-definition
13870
13871    named-namespace-definition:
13872      original-namespace-definition
13873      extension-namespace-definition
13874
13875    original-namespace-definition:
13876      namespace identifier { namespace-body }
13877
13878    extension-namespace-definition:
13879      namespace original-namespace-name { namespace-body }
13880
13881    unnamed-namespace-definition:
13882      namespace { namespace-body } */
13883
13884 static void
13885 cp_parser_namespace_definition (cp_parser* parser)
13886 {
13887   tree identifier, attribs;
13888   bool has_visibility;
13889   bool is_inline;
13890
13891   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13892     {
13893       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13894       is_inline = true;
13895       cp_lexer_consume_token (parser->lexer);
13896     }
13897   else
13898     is_inline = false;
13899
13900   /* Look for the `namespace' keyword.  */
13901   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13902
13903   /* Get the name of the namespace.  We do not attempt to distinguish
13904      between an original-namespace-definition and an
13905      extension-namespace-definition at this point.  The semantic
13906      analysis routines are responsible for that.  */
13907   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13908     identifier = cp_parser_identifier (parser);
13909   else
13910     identifier = NULL_TREE;
13911
13912   /* Parse any specified attributes.  */
13913   attribs = cp_parser_attributes_opt (parser);
13914
13915   /* Look for the `{' to start the namespace.  */
13916   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13917   /* Start the namespace.  */
13918   push_namespace (identifier);
13919
13920   /* "inline namespace" is equivalent to a stub namespace definition
13921      followed by a strong using directive.  */
13922   if (is_inline)
13923     {
13924       tree name_space = current_namespace;
13925       /* Set up namespace association.  */
13926       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13927         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13928                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13929       /* Import the contents of the inline namespace.  */
13930       pop_namespace ();
13931       do_using_directive (name_space);
13932       push_namespace (identifier);
13933     }
13934
13935   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13936
13937   /* Parse the body of the namespace.  */
13938   cp_parser_namespace_body (parser);
13939
13940   if (has_visibility)
13941     pop_visibility (1);
13942
13943   /* Finish the namespace.  */
13944   pop_namespace ();
13945   /* Look for the final `}'.  */
13946   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13947 }
13948
13949 /* Parse a namespace-body.
13950
13951    namespace-body:
13952      declaration-seq [opt]  */
13953
13954 static void
13955 cp_parser_namespace_body (cp_parser* parser)
13956 {
13957   cp_parser_declaration_seq_opt (parser);
13958 }
13959
13960 /* Parse a namespace-alias-definition.
13961
13962    namespace-alias-definition:
13963      namespace identifier = qualified-namespace-specifier ;  */
13964
13965 static void
13966 cp_parser_namespace_alias_definition (cp_parser* parser)
13967 {
13968   tree identifier;
13969   tree namespace_specifier;
13970
13971   cp_token *token = cp_lexer_peek_token (parser->lexer);
13972
13973   /* Look for the `namespace' keyword.  */
13974   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13975   /* Look for the identifier.  */
13976   identifier = cp_parser_identifier (parser);
13977   if (identifier == error_mark_node)
13978     return;
13979   /* Look for the `=' token.  */
13980   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13981       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13982     {
13983       error_at (token->location, "%<namespace%> definition is not allowed here");
13984       /* Skip the definition.  */
13985       cp_lexer_consume_token (parser->lexer);
13986       if (cp_parser_skip_to_closing_brace (parser))
13987         cp_lexer_consume_token (parser->lexer);
13988       return;
13989     }
13990   cp_parser_require (parser, CPP_EQ, RT_EQ);
13991   /* Look for the qualified-namespace-specifier.  */
13992   namespace_specifier
13993     = cp_parser_qualified_namespace_specifier (parser);
13994   /* Look for the `;' token.  */
13995   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13996
13997   /* Register the alias in the symbol table.  */
13998   do_namespace_alias (identifier, namespace_specifier);
13999 }
14000
14001 /* Parse a qualified-namespace-specifier.
14002
14003    qualified-namespace-specifier:
14004      :: [opt] nested-name-specifier [opt] namespace-name
14005
14006    Returns a NAMESPACE_DECL corresponding to the specified
14007    namespace.  */
14008
14009 static tree
14010 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14011 {
14012   /* Look for the optional `::'.  */
14013   cp_parser_global_scope_opt (parser,
14014                               /*current_scope_valid_p=*/false);
14015
14016   /* Look for the optional nested-name-specifier.  */
14017   cp_parser_nested_name_specifier_opt (parser,
14018                                        /*typename_keyword_p=*/false,
14019                                        /*check_dependency_p=*/true,
14020                                        /*type_p=*/false,
14021                                        /*is_declaration=*/true);
14022
14023   return cp_parser_namespace_name (parser);
14024 }
14025
14026 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14027    access declaration.
14028
14029    using-declaration:
14030      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14031      using :: unqualified-id ;  
14032
14033    access-declaration:
14034      qualified-id ;  
14035
14036    */
14037
14038 static bool
14039 cp_parser_using_declaration (cp_parser* parser, 
14040                              bool access_declaration_p)
14041 {
14042   cp_token *token;
14043   bool typename_p = false;
14044   bool global_scope_p;
14045   tree decl;
14046   tree identifier;
14047   tree qscope;
14048
14049   if (access_declaration_p)
14050     cp_parser_parse_tentatively (parser);
14051   else
14052     {
14053       /* Look for the `using' keyword.  */
14054       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14055       
14056       /* Peek at the next token.  */
14057       token = cp_lexer_peek_token (parser->lexer);
14058       /* See if it's `typename'.  */
14059       if (token->keyword == RID_TYPENAME)
14060         {
14061           /* Remember that we've seen it.  */
14062           typename_p = true;
14063           /* Consume the `typename' token.  */
14064           cp_lexer_consume_token (parser->lexer);
14065         }
14066     }
14067
14068   /* Look for the optional global scope qualification.  */
14069   global_scope_p
14070     = (cp_parser_global_scope_opt (parser,
14071                                    /*current_scope_valid_p=*/false)
14072        != NULL_TREE);
14073
14074   /* If we saw `typename', or didn't see `::', then there must be a
14075      nested-name-specifier present.  */
14076   if (typename_p || !global_scope_p)
14077     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14078                                               /*check_dependency_p=*/true,
14079                                               /*type_p=*/false,
14080                                               /*is_declaration=*/true);
14081   /* Otherwise, we could be in either of the two productions.  In that
14082      case, treat the nested-name-specifier as optional.  */
14083   else
14084     qscope = cp_parser_nested_name_specifier_opt (parser,
14085                                                   /*typename_keyword_p=*/false,
14086                                                   /*check_dependency_p=*/true,
14087                                                   /*type_p=*/false,
14088                                                   /*is_declaration=*/true);
14089   if (!qscope)
14090     qscope = global_namespace;
14091
14092   if (access_declaration_p && cp_parser_error_occurred (parser))
14093     /* Something has already gone wrong; there's no need to parse
14094        further.  Since an error has occurred, the return value of
14095        cp_parser_parse_definitely will be false, as required.  */
14096     return cp_parser_parse_definitely (parser);
14097
14098   token = cp_lexer_peek_token (parser->lexer);
14099   /* Parse the unqualified-id.  */
14100   identifier = cp_parser_unqualified_id (parser,
14101                                          /*template_keyword_p=*/false,
14102                                          /*check_dependency_p=*/true,
14103                                          /*declarator_p=*/true,
14104                                          /*optional_p=*/false);
14105
14106   if (access_declaration_p)
14107     {
14108       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14109         cp_parser_simulate_error (parser);
14110       if (!cp_parser_parse_definitely (parser))
14111         return false;
14112     }
14113
14114   /* The function we call to handle a using-declaration is different
14115      depending on what scope we are in.  */
14116   if (qscope == error_mark_node || identifier == error_mark_node)
14117     ;
14118   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14119            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14120     /* [namespace.udecl]
14121
14122        A using declaration shall not name a template-id.  */
14123     error_at (token->location,
14124               "a template-id may not appear in a using-declaration");
14125   else
14126     {
14127       if (at_class_scope_p ())
14128         {
14129           /* Create the USING_DECL.  */
14130           decl = do_class_using_decl (parser->scope, identifier);
14131
14132           if (check_for_bare_parameter_packs (decl))
14133             return false;
14134           else
14135             /* Add it to the list of members in this class.  */
14136             finish_member_declaration (decl);
14137         }
14138       else
14139         {
14140           decl = cp_parser_lookup_name_simple (parser,
14141                                                identifier,
14142                                                token->location);
14143           if (decl == error_mark_node)
14144             cp_parser_name_lookup_error (parser, identifier,
14145                                          decl, NLE_NULL,
14146                                          token->location);
14147           else if (check_for_bare_parameter_packs (decl))
14148             return false;
14149           else if (!at_namespace_scope_p ())
14150             do_local_using_decl (decl, qscope, identifier);
14151           else
14152             do_toplevel_using_decl (decl, qscope, identifier);
14153         }
14154     }
14155
14156   /* Look for the final `;'.  */
14157   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14158   
14159   return true;
14160 }
14161
14162 /* Parse a using-directive.
14163
14164    using-directive:
14165      using namespace :: [opt] nested-name-specifier [opt]
14166        namespace-name ;  */
14167
14168 static void
14169 cp_parser_using_directive (cp_parser* parser)
14170 {
14171   tree namespace_decl;
14172   tree attribs;
14173
14174   /* Look for the `using' keyword.  */
14175   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14176   /* And the `namespace' keyword.  */
14177   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14178   /* Look for the optional `::' operator.  */
14179   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14180   /* And the optional nested-name-specifier.  */
14181   cp_parser_nested_name_specifier_opt (parser,
14182                                        /*typename_keyword_p=*/false,
14183                                        /*check_dependency_p=*/true,
14184                                        /*type_p=*/false,
14185                                        /*is_declaration=*/true);
14186   /* Get the namespace being used.  */
14187   namespace_decl = cp_parser_namespace_name (parser);
14188   /* And any specified attributes.  */
14189   attribs = cp_parser_attributes_opt (parser);
14190   /* Update the symbol table.  */
14191   parse_using_directive (namespace_decl, attribs);
14192   /* Look for the final `;'.  */
14193   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14194 }
14195
14196 /* Parse an asm-definition.
14197
14198    asm-definition:
14199      asm ( string-literal ) ;
14200
14201    GNU Extension:
14202
14203    asm-definition:
14204      asm volatile [opt] ( string-literal ) ;
14205      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14206      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14207                           : asm-operand-list [opt] ) ;
14208      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14209                           : asm-operand-list [opt]
14210                           : asm-clobber-list [opt] ) ;
14211      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14212                                : asm-clobber-list [opt]
14213                                : asm-goto-list ) ;  */
14214
14215 static void
14216 cp_parser_asm_definition (cp_parser* parser)
14217 {
14218   tree string;
14219   tree outputs = NULL_TREE;
14220   tree inputs = NULL_TREE;
14221   tree clobbers = NULL_TREE;
14222   tree labels = NULL_TREE;
14223   tree asm_stmt;
14224   bool volatile_p = false;
14225   bool extended_p = false;
14226   bool invalid_inputs_p = false;
14227   bool invalid_outputs_p = false;
14228   bool goto_p = false;
14229   required_token missing = RT_NONE;
14230
14231   /* Look for the `asm' keyword.  */
14232   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14233   /* See if the next token is `volatile'.  */
14234   if (cp_parser_allow_gnu_extensions_p (parser)
14235       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14236     {
14237       /* Remember that we saw the `volatile' keyword.  */
14238       volatile_p = true;
14239       /* Consume the token.  */
14240       cp_lexer_consume_token (parser->lexer);
14241     }
14242   if (cp_parser_allow_gnu_extensions_p (parser)
14243       && parser->in_function_body
14244       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14245     {
14246       /* Remember that we saw the `goto' keyword.  */
14247       goto_p = true;
14248       /* Consume the token.  */
14249       cp_lexer_consume_token (parser->lexer);
14250     }
14251   /* Look for the opening `('.  */
14252   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14253     return;
14254   /* Look for the string.  */
14255   string = cp_parser_string_literal (parser, false, false);
14256   if (string == error_mark_node)
14257     {
14258       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14259                                              /*consume_paren=*/true);
14260       return;
14261     }
14262
14263   /* If we're allowing GNU extensions, check for the extended assembly
14264      syntax.  Unfortunately, the `:' tokens need not be separated by
14265      a space in C, and so, for compatibility, we tolerate that here
14266      too.  Doing that means that we have to treat the `::' operator as
14267      two `:' tokens.  */
14268   if (cp_parser_allow_gnu_extensions_p (parser)
14269       && parser->in_function_body
14270       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14271           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14272     {
14273       bool inputs_p = false;
14274       bool clobbers_p = false;
14275       bool labels_p = false;
14276
14277       /* The extended syntax was used.  */
14278       extended_p = true;
14279
14280       /* Look for outputs.  */
14281       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14282         {
14283           /* Consume the `:'.  */
14284           cp_lexer_consume_token (parser->lexer);
14285           /* Parse the output-operands.  */
14286           if (cp_lexer_next_token_is_not (parser->lexer,
14287                                           CPP_COLON)
14288               && cp_lexer_next_token_is_not (parser->lexer,
14289                                              CPP_SCOPE)
14290               && cp_lexer_next_token_is_not (parser->lexer,
14291                                              CPP_CLOSE_PAREN)
14292               && !goto_p)
14293             outputs = cp_parser_asm_operand_list (parser);
14294
14295             if (outputs == error_mark_node)
14296               invalid_outputs_p = true;
14297         }
14298       /* If the next token is `::', there are no outputs, and the
14299          next token is the beginning of the inputs.  */
14300       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14301         /* The inputs are coming next.  */
14302         inputs_p = true;
14303
14304       /* Look for inputs.  */
14305       if (inputs_p
14306           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14307         {
14308           /* Consume the `:' or `::'.  */
14309           cp_lexer_consume_token (parser->lexer);
14310           /* Parse the output-operands.  */
14311           if (cp_lexer_next_token_is_not (parser->lexer,
14312                                           CPP_COLON)
14313               && cp_lexer_next_token_is_not (parser->lexer,
14314                                              CPP_SCOPE)
14315               && cp_lexer_next_token_is_not (parser->lexer,
14316                                              CPP_CLOSE_PAREN))
14317             inputs = cp_parser_asm_operand_list (parser);
14318
14319             if (inputs == error_mark_node)
14320               invalid_inputs_p = true;
14321         }
14322       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14323         /* The clobbers are coming next.  */
14324         clobbers_p = true;
14325
14326       /* Look for clobbers.  */
14327       if (clobbers_p
14328           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14329         {
14330           clobbers_p = true;
14331           /* Consume the `:' or `::'.  */
14332           cp_lexer_consume_token (parser->lexer);
14333           /* Parse the clobbers.  */
14334           if (cp_lexer_next_token_is_not (parser->lexer,
14335                                           CPP_COLON)
14336               && cp_lexer_next_token_is_not (parser->lexer,
14337                                              CPP_CLOSE_PAREN))
14338             clobbers = cp_parser_asm_clobber_list (parser);
14339         }
14340       else if (goto_p
14341                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14342         /* The labels are coming next.  */
14343         labels_p = true;
14344
14345       /* Look for labels.  */
14346       if (labels_p
14347           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14348         {
14349           labels_p = true;
14350           /* Consume the `:' or `::'.  */
14351           cp_lexer_consume_token (parser->lexer);
14352           /* Parse the labels.  */
14353           labels = cp_parser_asm_label_list (parser);
14354         }
14355
14356       if (goto_p && !labels_p)
14357         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14358     }
14359   else if (goto_p)
14360     missing = RT_COLON_SCOPE;
14361
14362   /* Look for the closing `)'.  */
14363   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14364                           missing ? missing : RT_CLOSE_PAREN))
14365     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14366                                            /*consume_paren=*/true);
14367   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14368
14369   if (!invalid_inputs_p && !invalid_outputs_p)
14370     {
14371       /* Create the ASM_EXPR.  */
14372       if (parser->in_function_body)
14373         {
14374           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14375                                       inputs, clobbers, labels);
14376           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14377           if (!extended_p)
14378             {
14379               tree temp = asm_stmt;
14380               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14381                 temp = TREE_OPERAND (temp, 0);
14382
14383               ASM_INPUT_P (temp) = 1;
14384             }
14385         }
14386       else
14387         cgraph_add_asm_node (string);
14388     }
14389 }
14390
14391 /* Declarators [gram.dcl.decl] */
14392
14393 /* Parse an init-declarator.
14394
14395    init-declarator:
14396      declarator initializer [opt]
14397
14398    GNU Extension:
14399
14400    init-declarator:
14401      declarator asm-specification [opt] attributes [opt] initializer [opt]
14402
14403    function-definition:
14404      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14405        function-body
14406      decl-specifier-seq [opt] declarator function-try-block
14407
14408    GNU Extension:
14409
14410    function-definition:
14411      __extension__ function-definition
14412
14413    The DECL_SPECIFIERS apply to this declarator.  Returns a
14414    representation of the entity declared.  If MEMBER_P is TRUE, then
14415    this declarator appears in a class scope.  The new DECL created by
14416    this declarator is returned.
14417
14418    The CHECKS are access checks that should be performed once we know
14419    what entity is being declared (and, therefore, what classes have
14420    befriended it).
14421
14422    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14423    for a function-definition here as well.  If the declarator is a
14424    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14425    be TRUE upon return.  By that point, the function-definition will
14426    have been completely parsed.
14427
14428    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14429    is FALSE.
14430
14431    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14432    parsed declaration if it is an uninitialized single declarator not followed
14433    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14434    if present, will not be consumed.  If returned, this declarator will be
14435    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14436
14437 static tree
14438 cp_parser_init_declarator (cp_parser* parser,
14439                            cp_decl_specifier_seq *decl_specifiers,
14440                            VEC (deferred_access_check,gc)* checks,
14441                            bool function_definition_allowed_p,
14442                            bool member_p,
14443                            int declares_class_or_enum,
14444                            bool* function_definition_p,
14445                            tree* maybe_range_for_decl)
14446 {
14447   cp_token *token = NULL, *asm_spec_start_token = NULL,
14448            *attributes_start_token = NULL;
14449   cp_declarator *declarator;
14450   tree prefix_attributes;
14451   tree attributes;
14452   tree asm_specification;
14453   tree initializer;
14454   tree decl = NULL_TREE;
14455   tree scope;
14456   int is_initialized;
14457   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14458      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14459      "(...)".  */
14460   enum cpp_ttype initialization_kind;
14461   bool is_direct_init = false;
14462   bool is_non_constant_init;
14463   int ctor_dtor_or_conv_p;
14464   bool friend_p;
14465   tree pushed_scope = NULL_TREE;
14466   bool range_for_decl_p = false;
14467
14468   /* Gather the attributes that were provided with the
14469      decl-specifiers.  */
14470   prefix_attributes = decl_specifiers->attributes;
14471
14472   /* Assume that this is not the declarator for a function
14473      definition.  */
14474   if (function_definition_p)
14475     *function_definition_p = false;
14476
14477   /* Defer access checks while parsing the declarator; we cannot know
14478      what names are accessible until we know what is being
14479      declared.  */
14480   resume_deferring_access_checks ();
14481
14482   /* Parse the declarator.  */
14483   token = cp_lexer_peek_token (parser->lexer);
14484   declarator
14485     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14486                             &ctor_dtor_or_conv_p,
14487                             /*parenthesized_p=*/NULL,
14488                             member_p);
14489   /* Gather up the deferred checks.  */
14490   stop_deferring_access_checks ();
14491
14492   /* If the DECLARATOR was erroneous, there's no need to go
14493      further.  */
14494   if (declarator == cp_error_declarator)
14495     return error_mark_node;
14496
14497   /* Check that the number of template-parameter-lists is OK.  */
14498   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14499                                                        token->location))
14500     return error_mark_node;
14501
14502   if (declares_class_or_enum & 2)
14503     cp_parser_check_for_definition_in_return_type (declarator,
14504                                                    decl_specifiers->type,
14505                                                    decl_specifiers->type_location);
14506
14507   /* Figure out what scope the entity declared by the DECLARATOR is
14508      located in.  `grokdeclarator' sometimes changes the scope, so
14509      we compute it now.  */
14510   scope = get_scope_of_declarator (declarator);
14511
14512   /* Perform any lookups in the declared type which were thought to be
14513      dependent, but are not in the scope of the declarator.  */
14514   decl_specifiers->type
14515     = maybe_update_decl_type (decl_specifiers->type, scope);
14516
14517   /* If we're allowing GNU extensions, look for an asm-specification
14518      and attributes.  */
14519   if (cp_parser_allow_gnu_extensions_p (parser))
14520     {
14521       /* Look for an asm-specification.  */
14522       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14523       asm_specification = cp_parser_asm_specification_opt (parser);
14524       /* And attributes.  */
14525       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14526       attributes = cp_parser_attributes_opt (parser);
14527     }
14528   else
14529     {
14530       asm_specification = NULL_TREE;
14531       attributes = NULL_TREE;
14532     }
14533
14534   /* Peek at the next token.  */
14535   token = cp_lexer_peek_token (parser->lexer);
14536   /* Check to see if the token indicates the start of a
14537      function-definition.  */
14538   if (function_declarator_p (declarator)
14539       && cp_parser_token_starts_function_definition_p (token))
14540     {
14541       if (!function_definition_allowed_p)
14542         {
14543           /* If a function-definition should not appear here, issue an
14544              error message.  */
14545           cp_parser_error (parser,
14546                            "a function-definition is not allowed here");
14547           return error_mark_node;
14548         }
14549       else
14550         {
14551           location_t func_brace_location
14552             = cp_lexer_peek_token (parser->lexer)->location;
14553
14554           /* Neither attributes nor an asm-specification are allowed
14555              on a function-definition.  */
14556           if (asm_specification)
14557             error_at (asm_spec_start_token->location,
14558                       "an asm-specification is not allowed "
14559                       "on a function-definition");
14560           if (attributes)
14561             error_at (attributes_start_token->location,
14562                       "attributes are not allowed on a function-definition");
14563           /* This is a function-definition.  */
14564           *function_definition_p = true;
14565
14566           /* Parse the function definition.  */
14567           if (member_p)
14568             decl = cp_parser_save_member_function_body (parser,
14569                                                         decl_specifiers,
14570                                                         declarator,
14571                                                         prefix_attributes);
14572           else
14573             decl
14574               = (cp_parser_function_definition_from_specifiers_and_declarator
14575                  (parser, decl_specifiers, prefix_attributes, declarator));
14576
14577           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14578             {
14579               /* This is where the prologue starts...  */
14580               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14581                 = func_brace_location;
14582             }
14583
14584           return decl;
14585         }
14586     }
14587
14588   /* [dcl.dcl]
14589
14590      Only in function declarations for constructors, destructors, and
14591      type conversions can the decl-specifier-seq be omitted.
14592
14593      We explicitly postpone this check past the point where we handle
14594      function-definitions because we tolerate function-definitions
14595      that are missing their return types in some modes.  */
14596   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14597     {
14598       cp_parser_error (parser,
14599                        "expected constructor, destructor, or type conversion");
14600       return error_mark_node;
14601     }
14602
14603   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14604   if (token->type == CPP_EQ
14605       || token->type == CPP_OPEN_PAREN
14606       || token->type == CPP_OPEN_BRACE)
14607     {
14608       is_initialized = SD_INITIALIZED;
14609       initialization_kind = token->type;
14610       if (maybe_range_for_decl)
14611         *maybe_range_for_decl = error_mark_node;
14612
14613       if (token->type == CPP_EQ
14614           && function_declarator_p (declarator))
14615         {
14616           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14617           if (t2->keyword == RID_DEFAULT)
14618             is_initialized = SD_DEFAULTED;
14619           else if (t2->keyword == RID_DELETE)
14620             is_initialized = SD_DELETED;
14621         }
14622     }
14623   else
14624     {
14625       /* If the init-declarator isn't initialized and isn't followed by a
14626          `,' or `;', it's not a valid init-declarator.  */
14627       if (token->type != CPP_COMMA
14628           && token->type != CPP_SEMICOLON)
14629         {
14630           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14631             range_for_decl_p = true;
14632           else
14633             {
14634               cp_parser_error (parser, "expected initializer");
14635               return error_mark_node;
14636             }
14637         }
14638       is_initialized = SD_UNINITIALIZED;
14639       initialization_kind = CPP_EOF;
14640     }
14641
14642   /* Because start_decl has side-effects, we should only call it if we
14643      know we're going ahead.  By this point, we know that we cannot
14644      possibly be looking at any other construct.  */
14645   cp_parser_commit_to_tentative_parse (parser);
14646
14647   /* If the decl specifiers were bad, issue an error now that we're
14648      sure this was intended to be a declarator.  Then continue
14649      declaring the variable(s), as int, to try to cut down on further
14650      errors.  */
14651   if (decl_specifiers->any_specifiers_p
14652       && decl_specifiers->type == error_mark_node)
14653     {
14654       cp_parser_error (parser, "invalid type in declaration");
14655       decl_specifiers->type = integer_type_node;
14656     }
14657
14658   /* Check to see whether or not this declaration is a friend.  */
14659   friend_p = cp_parser_friend_p (decl_specifiers);
14660
14661   /* Enter the newly declared entry in the symbol table.  If we're
14662      processing a declaration in a class-specifier, we wait until
14663      after processing the initializer.  */
14664   if (!member_p)
14665     {
14666       if (parser->in_unbraced_linkage_specification_p)
14667         decl_specifiers->storage_class = sc_extern;
14668       decl = start_decl (declarator, decl_specifiers,
14669                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14670                          attributes, prefix_attributes,
14671                          &pushed_scope);
14672       /* Adjust location of decl if declarator->id_loc is more appropriate:
14673          set, and decl wasn't merged with another decl, in which case its
14674          location would be different from input_location, and more accurate.  */
14675       if (DECL_P (decl)
14676           && declarator->id_loc != UNKNOWN_LOCATION
14677           && DECL_SOURCE_LOCATION (decl) == input_location)
14678         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14679     }
14680   else if (scope)
14681     /* Enter the SCOPE.  That way unqualified names appearing in the
14682        initializer will be looked up in SCOPE.  */
14683     pushed_scope = push_scope (scope);
14684
14685   /* Perform deferred access control checks, now that we know in which
14686      SCOPE the declared entity resides.  */
14687   if (!member_p && decl)
14688     {
14689       tree saved_current_function_decl = NULL_TREE;
14690
14691       /* If the entity being declared is a function, pretend that we
14692          are in its scope.  If it is a `friend', it may have access to
14693          things that would not otherwise be accessible.  */
14694       if (TREE_CODE (decl) == FUNCTION_DECL)
14695         {
14696           saved_current_function_decl = current_function_decl;
14697           current_function_decl = decl;
14698         }
14699
14700       /* Perform access checks for template parameters.  */
14701       cp_parser_perform_template_parameter_access_checks (checks);
14702
14703       /* Perform the access control checks for the declarator and the
14704          decl-specifiers.  */
14705       perform_deferred_access_checks ();
14706
14707       /* Restore the saved value.  */
14708       if (TREE_CODE (decl) == FUNCTION_DECL)
14709         current_function_decl = saved_current_function_decl;
14710     }
14711
14712   /* Parse the initializer.  */
14713   initializer = NULL_TREE;
14714   is_direct_init = false;
14715   is_non_constant_init = true;
14716   if (is_initialized)
14717     {
14718       if (function_declarator_p (declarator))
14719         {
14720           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14721            if (initialization_kind == CPP_EQ)
14722              initializer = cp_parser_pure_specifier (parser);
14723            else
14724              {
14725                /* If the declaration was erroneous, we don't really
14726                   know what the user intended, so just silently
14727                   consume the initializer.  */
14728                if (decl != error_mark_node)
14729                  error_at (initializer_start_token->location,
14730                            "initializer provided for function");
14731                cp_parser_skip_to_closing_parenthesis (parser,
14732                                                       /*recovering=*/true,
14733                                                       /*or_comma=*/false,
14734                                                       /*consume_paren=*/true);
14735              }
14736         }
14737       else
14738         {
14739           /* We want to record the extra mangling scope for in-class
14740              initializers of class members and initializers of static data
14741              member templates.  The former is a C++0x feature which isn't
14742              implemented yet, and I expect it will involve deferring
14743              parsing of the initializer until end of class as with default
14744              arguments.  So right here we only handle the latter.  */
14745           if (!member_p && processing_template_decl)
14746             start_lambda_scope (decl);
14747           initializer = cp_parser_initializer (parser,
14748                                                &is_direct_init,
14749                                                &is_non_constant_init);
14750           if (!member_p && processing_template_decl)
14751             finish_lambda_scope ();
14752         }
14753     }
14754
14755   /* The old parser allows attributes to appear after a parenthesized
14756      initializer.  Mark Mitchell proposed removing this functionality
14757      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14758      attributes -- but ignores them.  */
14759   if (cp_parser_allow_gnu_extensions_p (parser)
14760       && initialization_kind == CPP_OPEN_PAREN)
14761     if (cp_parser_attributes_opt (parser))
14762       warning (OPT_Wattributes,
14763                "attributes after parenthesized initializer ignored");
14764
14765   /* For an in-class declaration, use `grokfield' to create the
14766      declaration.  */
14767   if (member_p)
14768     {
14769       if (pushed_scope)
14770         {
14771           pop_scope (pushed_scope);
14772           pushed_scope = NULL_TREE;
14773         }
14774       decl = grokfield (declarator, decl_specifiers,
14775                         initializer, !is_non_constant_init,
14776                         /*asmspec=*/NULL_TREE,
14777                         prefix_attributes);
14778       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14779         cp_parser_save_default_args (parser, decl);
14780     }
14781
14782   /* Finish processing the declaration.  But, skip member
14783      declarations.  */
14784   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14785     {
14786       cp_finish_decl (decl,
14787                       initializer, !is_non_constant_init,
14788                       asm_specification,
14789                       /* If the initializer is in parentheses, then this is
14790                          a direct-initialization, which means that an
14791                          `explicit' constructor is OK.  Otherwise, an
14792                          `explicit' constructor cannot be used.  */
14793                       ((is_direct_init || !is_initialized)
14794                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14795     }
14796   else if ((cxx_dialect != cxx98) && friend_p
14797            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14798     /* Core issue #226 (C++0x only): A default template-argument
14799        shall not be specified in a friend class template
14800        declaration. */
14801     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14802                              /*is_partial=*/0, /*is_friend_decl=*/1);
14803
14804   if (!friend_p && pushed_scope)
14805     pop_scope (pushed_scope);
14806
14807   return decl;
14808 }
14809
14810 /* Parse a declarator.
14811
14812    declarator:
14813      direct-declarator
14814      ptr-operator declarator
14815
14816    abstract-declarator:
14817      ptr-operator abstract-declarator [opt]
14818      direct-abstract-declarator
14819
14820    GNU Extensions:
14821
14822    declarator:
14823      attributes [opt] direct-declarator
14824      attributes [opt] ptr-operator declarator
14825
14826    abstract-declarator:
14827      attributes [opt] ptr-operator abstract-declarator [opt]
14828      attributes [opt] direct-abstract-declarator
14829
14830    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14831    detect constructor, destructor or conversion operators. It is set
14832    to -1 if the declarator is a name, and +1 if it is a
14833    function. Otherwise it is set to zero. Usually you just want to
14834    test for >0, but internally the negative value is used.
14835
14836    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14837    a decl-specifier-seq unless it declares a constructor, destructor,
14838    or conversion.  It might seem that we could check this condition in
14839    semantic analysis, rather than parsing, but that makes it difficult
14840    to handle something like `f()'.  We want to notice that there are
14841    no decl-specifiers, and therefore realize that this is an
14842    expression, not a declaration.)
14843
14844    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14845    the declarator is a direct-declarator of the form "(...)".
14846
14847    MEMBER_P is true iff this declarator is a member-declarator.  */
14848
14849 static cp_declarator *
14850 cp_parser_declarator (cp_parser* parser,
14851                       cp_parser_declarator_kind dcl_kind,
14852                       int* ctor_dtor_or_conv_p,
14853                       bool* parenthesized_p,
14854                       bool member_p)
14855 {
14856   cp_declarator *declarator;
14857   enum tree_code code;
14858   cp_cv_quals cv_quals;
14859   tree class_type;
14860   tree attributes = NULL_TREE;
14861
14862   /* Assume this is not a constructor, destructor, or type-conversion
14863      operator.  */
14864   if (ctor_dtor_or_conv_p)
14865     *ctor_dtor_or_conv_p = 0;
14866
14867   if (cp_parser_allow_gnu_extensions_p (parser))
14868     attributes = cp_parser_attributes_opt (parser);
14869
14870   /* Check for the ptr-operator production.  */
14871   cp_parser_parse_tentatively (parser);
14872   /* Parse the ptr-operator.  */
14873   code = cp_parser_ptr_operator (parser,
14874                                  &class_type,
14875                                  &cv_quals);
14876   /* If that worked, then we have a ptr-operator.  */
14877   if (cp_parser_parse_definitely (parser))
14878     {
14879       /* If a ptr-operator was found, then this declarator was not
14880          parenthesized.  */
14881       if (parenthesized_p)
14882         *parenthesized_p = true;
14883       /* The dependent declarator is optional if we are parsing an
14884          abstract-declarator.  */
14885       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14886         cp_parser_parse_tentatively (parser);
14887
14888       /* Parse the dependent declarator.  */
14889       declarator = cp_parser_declarator (parser, dcl_kind,
14890                                          /*ctor_dtor_or_conv_p=*/NULL,
14891                                          /*parenthesized_p=*/NULL,
14892                                          /*member_p=*/false);
14893
14894       /* If we are parsing an abstract-declarator, we must handle the
14895          case where the dependent declarator is absent.  */
14896       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14897           && !cp_parser_parse_definitely (parser))
14898         declarator = NULL;
14899
14900       declarator = cp_parser_make_indirect_declarator
14901         (code, class_type, cv_quals, declarator);
14902     }
14903   /* Everything else is a direct-declarator.  */
14904   else
14905     {
14906       if (parenthesized_p)
14907         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14908                                                    CPP_OPEN_PAREN);
14909       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14910                                                 ctor_dtor_or_conv_p,
14911                                                 member_p);
14912     }
14913
14914   if (attributes && declarator && declarator != cp_error_declarator)
14915     declarator->attributes = attributes;
14916
14917   return declarator;
14918 }
14919
14920 /* Parse a direct-declarator or direct-abstract-declarator.
14921
14922    direct-declarator:
14923      declarator-id
14924      direct-declarator ( parameter-declaration-clause )
14925        cv-qualifier-seq [opt]
14926        exception-specification [opt]
14927      direct-declarator [ constant-expression [opt] ]
14928      ( declarator )
14929
14930    direct-abstract-declarator:
14931      direct-abstract-declarator [opt]
14932        ( parameter-declaration-clause )
14933        cv-qualifier-seq [opt]
14934        exception-specification [opt]
14935      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14936      ( abstract-declarator )
14937
14938    Returns a representation of the declarator.  DCL_KIND is
14939    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14940    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14941    we are parsing a direct-declarator.  It is
14942    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14943    of ambiguity we prefer an abstract declarator, as per
14944    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14945    cp_parser_declarator.  */
14946
14947 static cp_declarator *
14948 cp_parser_direct_declarator (cp_parser* parser,
14949                              cp_parser_declarator_kind dcl_kind,
14950                              int* ctor_dtor_or_conv_p,
14951                              bool member_p)
14952 {
14953   cp_token *token;
14954   cp_declarator *declarator = NULL;
14955   tree scope = NULL_TREE;
14956   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14957   bool saved_in_declarator_p = parser->in_declarator_p;
14958   bool first = true;
14959   tree pushed_scope = NULL_TREE;
14960
14961   while (true)
14962     {
14963       /* Peek at the next token.  */
14964       token = cp_lexer_peek_token (parser->lexer);
14965       if (token->type == CPP_OPEN_PAREN)
14966         {
14967           /* This is either a parameter-declaration-clause, or a
14968              parenthesized declarator. When we know we are parsing a
14969              named declarator, it must be a parenthesized declarator
14970              if FIRST is true. For instance, `(int)' is a
14971              parameter-declaration-clause, with an omitted
14972              direct-abstract-declarator. But `((*))', is a
14973              parenthesized abstract declarator. Finally, when T is a
14974              template parameter `(T)' is a
14975              parameter-declaration-clause, and not a parenthesized
14976              named declarator.
14977
14978              We first try and parse a parameter-declaration-clause,
14979              and then try a nested declarator (if FIRST is true).
14980
14981              It is not an error for it not to be a
14982              parameter-declaration-clause, even when FIRST is
14983              false. Consider,
14984
14985                int i (int);
14986                int i (3);
14987
14988              The first is the declaration of a function while the
14989              second is the definition of a variable, including its
14990              initializer.
14991
14992              Having seen only the parenthesis, we cannot know which of
14993              these two alternatives should be selected.  Even more
14994              complex are examples like:
14995
14996                int i (int (a));
14997                int i (int (3));
14998
14999              The former is a function-declaration; the latter is a
15000              variable initialization.
15001
15002              Thus again, we try a parameter-declaration-clause, and if
15003              that fails, we back out and return.  */
15004
15005           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15006             {
15007               tree params;
15008               unsigned saved_num_template_parameter_lists;
15009               bool is_declarator = false;
15010               tree t;
15011
15012               /* In a member-declarator, the only valid interpretation
15013                  of a parenthesis is the start of a
15014                  parameter-declaration-clause.  (It is invalid to
15015                  initialize a static data member with a parenthesized
15016                  initializer; only the "=" form of initialization is
15017                  permitted.)  */
15018               if (!member_p)
15019                 cp_parser_parse_tentatively (parser);
15020
15021               /* Consume the `('.  */
15022               cp_lexer_consume_token (parser->lexer);
15023               if (first)
15024                 {
15025                   /* If this is going to be an abstract declarator, we're
15026                      in a declarator and we can't have default args.  */
15027                   parser->default_arg_ok_p = false;
15028                   parser->in_declarator_p = true;
15029                 }
15030
15031               /* Inside the function parameter list, surrounding
15032                  template-parameter-lists do not apply.  */
15033               saved_num_template_parameter_lists
15034                 = parser->num_template_parameter_lists;
15035               parser->num_template_parameter_lists = 0;
15036
15037               begin_scope (sk_function_parms, NULL_TREE);
15038
15039               /* Parse the parameter-declaration-clause.  */
15040               params = cp_parser_parameter_declaration_clause (parser);
15041
15042               parser->num_template_parameter_lists
15043                 = saved_num_template_parameter_lists;
15044
15045               /* Consume the `)'.  */
15046               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15047
15048               /* If all went well, parse the cv-qualifier-seq and the
15049                  exception-specification.  */
15050               if (member_p || cp_parser_parse_definitely (parser))
15051                 {
15052                   cp_cv_quals cv_quals;
15053                   cp_virt_specifiers virt_specifiers;
15054                   tree exception_specification;
15055                   tree late_return;
15056
15057                   is_declarator = true;
15058
15059                   if (ctor_dtor_or_conv_p)
15060                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15061                   first = false;
15062
15063                   /* Parse the cv-qualifier-seq.  */
15064                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15065                   /* And the exception-specification.  */
15066                   exception_specification
15067                     = cp_parser_exception_specification_opt (parser);
15068                   /* Parse the virt-specifier-seq.  */
15069                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15070
15071                   late_return = (cp_parser_late_return_type_opt
15072                                  (parser, member_p ? cv_quals : -1));
15073
15074                   /* Create the function-declarator.  */
15075                   declarator = make_call_declarator (declarator,
15076                                                      params,
15077                                                      cv_quals,
15078                                                      virt_specifiers,
15079                                                      exception_specification,
15080                                                      late_return);
15081                   /* Any subsequent parameter lists are to do with
15082                      return type, so are not those of the declared
15083                      function.  */
15084                   parser->default_arg_ok_p = false;
15085                 }
15086
15087               /* Remove the function parms from scope.  */
15088               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15089                 pop_binding (DECL_NAME (t), t);
15090               leave_scope();
15091
15092               if (is_declarator)
15093                 /* Repeat the main loop.  */
15094                 continue;
15095             }
15096
15097           /* If this is the first, we can try a parenthesized
15098              declarator.  */
15099           if (first)
15100             {
15101               bool saved_in_type_id_in_expr_p;
15102
15103               parser->default_arg_ok_p = saved_default_arg_ok_p;
15104               parser->in_declarator_p = saved_in_declarator_p;
15105
15106               /* Consume the `('.  */
15107               cp_lexer_consume_token (parser->lexer);
15108               /* Parse the nested declarator.  */
15109               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15110               parser->in_type_id_in_expr_p = true;
15111               declarator
15112                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15113                                         /*parenthesized_p=*/NULL,
15114                                         member_p);
15115               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15116               first = false;
15117               /* Expect a `)'.  */
15118               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15119                 declarator = cp_error_declarator;
15120               if (declarator == cp_error_declarator)
15121                 break;
15122
15123               goto handle_declarator;
15124             }
15125           /* Otherwise, we must be done.  */
15126           else
15127             break;
15128         }
15129       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15130                && token->type == CPP_OPEN_SQUARE)
15131         {
15132           /* Parse an array-declarator.  */
15133           tree bounds;
15134
15135           if (ctor_dtor_or_conv_p)
15136             *ctor_dtor_or_conv_p = 0;
15137
15138           first = false;
15139           parser->default_arg_ok_p = false;
15140           parser->in_declarator_p = true;
15141           /* Consume the `['.  */
15142           cp_lexer_consume_token (parser->lexer);
15143           /* Peek at the next token.  */
15144           token = cp_lexer_peek_token (parser->lexer);
15145           /* If the next token is `]', then there is no
15146              constant-expression.  */
15147           if (token->type != CPP_CLOSE_SQUARE)
15148             {
15149               bool non_constant_p;
15150
15151               bounds
15152                 = cp_parser_constant_expression (parser,
15153                                                  /*allow_non_constant=*/true,
15154                                                  &non_constant_p);
15155               if (!non_constant_p)
15156                 /* OK */;
15157               /* Normally, the array bound must be an integral constant
15158                  expression.  However, as an extension, we allow VLAs
15159                  in function scopes as long as they aren't part of a
15160                  parameter declaration.  */
15161               else if (!parser->in_function_body
15162                        || current_binding_level->kind == sk_function_parms)
15163                 {
15164                   cp_parser_error (parser,
15165                                    "array bound is not an integer constant");
15166                   bounds = error_mark_node;
15167                 }
15168               else if (processing_template_decl && !error_operand_p (bounds))
15169                 {
15170                   /* Remember this wasn't a constant-expression.  */
15171                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15172                   TREE_SIDE_EFFECTS (bounds) = 1;
15173                 }
15174             }
15175           else
15176             bounds = NULL_TREE;
15177           /* Look for the closing `]'.  */
15178           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15179             {
15180               declarator = cp_error_declarator;
15181               break;
15182             }
15183
15184           declarator = make_array_declarator (declarator, bounds);
15185         }
15186       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15187         {
15188           {
15189             tree qualifying_scope;
15190             tree unqualified_name;
15191             special_function_kind sfk;
15192             bool abstract_ok;
15193             bool pack_expansion_p = false;
15194             cp_token *declarator_id_start_token;
15195
15196             /* Parse a declarator-id */
15197             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15198             if (abstract_ok)
15199               {
15200                 cp_parser_parse_tentatively (parser);
15201
15202                 /* If we see an ellipsis, we should be looking at a
15203                    parameter pack. */
15204                 if (token->type == CPP_ELLIPSIS)
15205                   {
15206                     /* Consume the `...' */
15207                     cp_lexer_consume_token (parser->lexer);
15208
15209                     pack_expansion_p = true;
15210                   }
15211               }
15212
15213             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15214             unqualified_name
15215               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15216             qualifying_scope = parser->scope;
15217             if (abstract_ok)
15218               {
15219                 bool okay = false;
15220
15221                 if (!unqualified_name && pack_expansion_p)
15222                   {
15223                     /* Check whether an error occurred. */
15224                     okay = !cp_parser_error_occurred (parser);
15225
15226                     /* We already consumed the ellipsis to mark a
15227                        parameter pack, but we have no way to report it,
15228                        so abort the tentative parse. We will be exiting
15229                        immediately anyway. */
15230                     cp_parser_abort_tentative_parse (parser);
15231                   }
15232                 else
15233                   okay = cp_parser_parse_definitely (parser);
15234
15235                 if (!okay)
15236                   unqualified_name = error_mark_node;
15237                 else if (unqualified_name
15238                          && (qualifying_scope
15239                              || (TREE_CODE (unqualified_name)
15240                                  != IDENTIFIER_NODE)))
15241                   {
15242                     cp_parser_error (parser, "expected unqualified-id");
15243                     unqualified_name = error_mark_node;
15244                   }
15245               }
15246
15247             if (!unqualified_name)
15248               return NULL;
15249             if (unqualified_name == error_mark_node)
15250               {
15251                 declarator = cp_error_declarator;
15252                 pack_expansion_p = false;
15253                 declarator->parameter_pack_p = false;
15254                 break;
15255               }
15256
15257             if (qualifying_scope && at_namespace_scope_p ()
15258                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15259               {
15260                 /* In the declaration of a member of a template class
15261                    outside of the class itself, the SCOPE will sometimes
15262                    be a TYPENAME_TYPE.  For example, given:
15263
15264                    template <typename T>
15265                    int S<T>::R::i = 3;
15266
15267                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15268                    this context, we must resolve S<T>::R to an ordinary
15269                    type, rather than a typename type.
15270
15271                    The reason we normally avoid resolving TYPENAME_TYPEs
15272                    is that a specialization of `S' might render
15273                    `S<T>::R' not a type.  However, if `S' is
15274                    specialized, then this `i' will not be used, so there
15275                    is no harm in resolving the types here.  */
15276                 tree type;
15277
15278                 /* Resolve the TYPENAME_TYPE.  */
15279                 type = resolve_typename_type (qualifying_scope,
15280                                               /*only_current_p=*/false);
15281                 /* If that failed, the declarator is invalid.  */
15282                 if (TREE_CODE (type) == TYPENAME_TYPE)
15283                   {
15284                     if (typedef_variant_p (type))
15285                       error_at (declarator_id_start_token->location,
15286                                 "cannot define member of dependent typedef "
15287                                 "%qT", type);
15288                     else
15289                       error_at (declarator_id_start_token->location,
15290                                 "%<%T::%E%> is not a type",
15291                                 TYPE_CONTEXT (qualifying_scope),
15292                                 TYPE_IDENTIFIER (qualifying_scope));
15293                   }
15294                 qualifying_scope = type;
15295               }
15296
15297             sfk = sfk_none;
15298
15299             if (unqualified_name)
15300               {
15301                 tree class_type;
15302
15303                 if (qualifying_scope
15304                     && CLASS_TYPE_P (qualifying_scope))
15305                   class_type = qualifying_scope;
15306                 else
15307                   class_type = current_class_type;
15308
15309                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15310                   {
15311                     tree name_type = TREE_TYPE (unqualified_name);
15312                     if (class_type && same_type_p (name_type, class_type))
15313                       {
15314                         if (qualifying_scope
15315                             && CLASSTYPE_USE_TEMPLATE (name_type))
15316                           {
15317                             error_at (declarator_id_start_token->location,
15318                                       "invalid use of constructor as a template");
15319                             inform (declarator_id_start_token->location,
15320                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15321                                     "name the constructor in a qualified name",
15322                                     class_type,
15323                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15324                                     class_type, name_type);
15325                             declarator = cp_error_declarator;
15326                             break;
15327                           }
15328                         else
15329                           unqualified_name = constructor_name (class_type);
15330                       }
15331                     else
15332                       {
15333                         /* We do not attempt to print the declarator
15334                            here because we do not have enough
15335                            information about its original syntactic
15336                            form.  */
15337                         cp_parser_error (parser, "invalid declarator");
15338                         declarator = cp_error_declarator;
15339                         break;
15340                       }
15341                   }
15342
15343                 if (class_type)
15344                   {
15345                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15346                       sfk = sfk_destructor;
15347                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15348                       sfk = sfk_conversion;
15349                     else if (/* There's no way to declare a constructor
15350                                 for an anonymous type, even if the type
15351                                 got a name for linkage purposes.  */
15352                              !TYPE_WAS_ANONYMOUS (class_type)
15353                              && constructor_name_p (unqualified_name,
15354                                                     class_type))
15355                       {
15356                         unqualified_name = constructor_name (class_type);
15357                         sfk = sfk_constructor;
15358                       }
15359                     else if (is_overloaded_fn (unqualified_name)
15360                              && DECL_CONSTRUCTOR_P (get_first_fn
15361                                                     (unqualified_name)))
15362                       sfk = sfk_constructor;
15363
15364                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15365                       *ctor_dtor_or_conv_p = -1;
15366                   }
15367               }
15368             declarator = make_id_declarator (qualifying_scope,
15369                                              unqualified_name,
15370                                              sfk);
15371             declarator->id_loc = token->location;
15372             declarator->parameter_pack_p = pack_expansion_p;
15373
15374             if (pack_expansion_p)
15375               maybe_warn_variadic_templates ();
15376           }
15377
15378         handle_declarator:;
15379           scope = get_scope_of_declarator (declarator);
15380           if (scope)
15381             /* Any names that appear after the declarator-id for a
15382                member are looked up in the containing scope.  */
15383             pushed_scope = push_scope (scope);
15384           parser->in_declarator_p = true;
15385           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15386               || (declarator && declarator->kind == cdk_id))
15387             /* Default args are only allowed on function
15388                declarations.  */
15389             parser->default_arg_ok_p = saved_default_arg_ok_p;
15390           else
15391             parser->default_arg_ok_p = false;
15392
15393           first = false;
15394         }
15395       /* We're done.  */
15396       else
15397         break;
15398     }
15399
15400   /* For an abstract declarator, we might wind up with nothing at this
15401      point.  That's an error; the declarator is not optional.  */
15402   if (!declarator)
15403     cp_parser_error (parser, "expected declarator");
15404
15405   /* If we entered a scope, we must exit it now.  */
15406   if (pushed_scope)
15407     pop_scope (pushed_scope);
15408
15409   parser->default_arg_ok_p = saved_default_arg_ok_p;
15410   parser->in_declarator_p = saved_in_declarator_p;
15411
15412   return declarator;
15413 }
15414
15415 /* Parse a ptr-operator.
15416
15417    ptr-operator:
15418      * cv-qualifier-seq [opt]
15419      &
15420      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15421
15422    GNU Extension:
15423
15424    ptr-operator:
15425      & cv-qualifier-seq [opt]
15426
15427    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15428    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15429    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15430    filled in with the TYPE containing the member.  *CV_QUALS is
15431    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15432    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15433    Note that the tree codes returned by this function have nothing
15434    to do with the types of trees that will be eventually be created
15435    to represent the pointer or reference type being parsed. They are
15436    just constants with suggestive names. */
15437 static enum tree_code
15438 cp_parser_ptr_operator (cp_parser* parser,
15439                         tree* type,
15440                         cp_cv_quals *cv_quals)
15441 {
15442   enum tree_code code = ERROR_MARK;
15443   cp_token *token;
15444
15445   /* Assume that it's not a pointer-to-member.  */
15446   *type = NULL_TREE;
15447   /* And that there are no cv-qualifiers.  */
15448   *cv_quals = TYPE_UNQUALIFIED;
15449
15450   /* Peek at the next token.  */
15451   token = cp_lexer_peek_token (parser->lexer);
15452
15453   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15454   if (token->type == CPP_MULT)
15455     code = INDIRECT_REF;
15456   else if (token->type == CPP_AND)
15457     code = ADDR_EXPR;
15458   else if ((cxx_dialect != cxx98) &&
15459            token->type == CPP_AND_AND) /* C++0x only */
15460     code = NON_LVALUE_EXPR;
15461
15462   if (code != ERROR_MARK)
15463     {
15464       /* Consume the `*', `&' or `&&'.  */
15465       cp_lexer_consume_token (parser->lexer);
15466
15467       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15468          `&', if we are allowing GNU extensions.  (The only qualifier
15469          that can legally appear after `&' is `restrict', but that is
15470          enforced during semantic analysis.  */
15471       if (code == INDIRECT_REF
15472           || cp_parser_allow_gnu_extensions_p (parser))
15473         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15474     }
15475   else
15476     {
15477       /* Try the pointer-to-member case.  */
15478       cp_parser_parse_tentatively (parser);
15479       /* Look for the optional `::' operator.  */
15480       cp_parser_global_scope_opt (parser,
15481                                   /*current_scope_valid_p=*/false);
15482       /* Look for the nested-name specifier.  */
15483       token = cp_lexer_peek_token (parser->lexer);
15484       cp_parser_nested_name_specifier (parser,
15485                                        /*typename_keyword_p=*/false,
15486                                        /*check_dependency_p=*/true,
15487                                        /*type_p=*/false,
15488                                        /*is_declaration=*/false);
15489       /* If we found it, and the next token is a `*', then we are
15490          indeed looking at a pointer-to-member operator.  */
15491       if (!cp_parser_error_occurred (parser)
15492           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15493         {
15494           /* Indicate that the `*' operator was used.  */
15495           code = INDIRECT_REF;
15496
15497           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15498             error_at (token->location, "%qD is a namespace", parser->scope);
15499           else
15500             {
15501               /* The type of which the member is a member is given by the
15502                  current SCOPE.  */
15503               *type = parser->scope;
15504               /* The next name will not be qualified.  */
15505               parser->scope = NULL_TREE;
15506               parser->qualifying_scope = NULL_TREE;
15507               parser->object_scope = NULL_TREE;
15508               /* Look for the optional cv-qualifier-seq.  */
15509               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15510             }
15511         }
15512       /* If that didn't work we don't have a ptr-operator.  */
15513       if (!cp_parser_parse_definitely (parser))
15514         cp_parser_error (parser, "expected ptr-operator");
15515     }
15516
15517   return code;
15518 }
15519
15520 /* Parse an (optional) cv-qualifier-seq.
15521
15522    cv-qualifier-seq:
15523      cv-qualifier cv-qualifier-seq [opt]
15524
15525    cv-qualifier:
15526      const
15527      volatile
15528
15529    GNU Extension:
15530
15531    cv-qualifier:
15532      __restrict__
15533
15534    Returns a bitmask representing the cv-qualifiers.  */
15535
15536 static cp_cv_quals
15537 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15538 {
15539   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15540
15541   while (true)
15542     {
15543       cp_token *token;
15544       cp_cv_quals cv_qualifier;
15545
15546       /* Peek at the next token.  */
15547       token = cp_lexer_peek_token (parser->lexer);
15548       /* See if it's a cv-qualifier.  */
15549       switch (token->keyword)
15550         {
15551         case RID_CONST:
15552           cv_qualifier = TYPE_QUAL_CONST;
15553           break;
15554
15555         case RID_VOLATILE:
15556           cv_qualifier = TYPE_QUAL_VOLATILE;
15557           break;
15558
15559         case RID_RESTRICT:
15560           cv_qualifier = TYPE_QUAL_RESTRICT;
15561           break;
15562
15563         default:
15564           cv_qualifier = TYPE_UNQUALIFIED;
15565           break;
15566         }
15567
15568       if (!cv_qualifier)
15569         break;
15570
15571       if (cv_quals & cv_qualifier)
15572         {
15573           error_at (token->location, "duplicate cv-qualifier");
15574           cp_lexer_purge_token (parser->lexer);
15575         }
15576       else
15577         {
15578           cp_lexer_consume_token (parser->lexer);
15579           cv_quals |= cv_qualifier;
15580         }
15581     }
15582
15583   return cv_quals;
15584 }
15585
15586 /* Parse an (optional) virt-specifier-seq.
15587
15588    virt-specifier-seq:
15589      virt-specifier virt-specifier-seq [opt]
15590
15591    virt-specifier:
15592      override
15593      final
15594
15595    Returns a bitmask representing the virt-specifiers.  */
15596
15597 static cp_virt_specifiers
15598 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15599 {
15600   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15601
15602   while (true)
15603     {
15604       cp_token *token;
15605       cp_virt_specifiers virt_specifier;
15606
15607       /* Peek at the next token.  */
15608       token = cp_lexer_peek_token (parser->lexer);
15609       /* See if it's a virt-specifier-qualifier.  */
15610       if (token->type != CPP_NAME)
15611         break;
15612       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15613         {
15614           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15615           virt_specifier = VIRT_SPEC_OVERRIDE;
15616         }
15617       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15618         {
15619           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15620           virt_specifier = VIRT_SPEC_FINAL;
15621         }
15622       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
15623         {
15624           virt_specifier = VIRT_SPEC_FINAL;
15625         }
15626       else
15627         break;
15628
15629       if (virt_specifiers & virt_specifier)
15630         {
15631           error_at (token->location, "duplicate virt-specifier");
15632           cp_lexer_purge_token (parser->lexer);
15633         }
15634       else
15635         {
15636           cp_lexer_consume_token (parser->lexer);
15637           virt_specifiers |= virt_specifier;
15638         }
15639     }
15640   return virt_specifiers;
15641 }
15642
15643 /* Parse a late-specified return type, if any.  This is not a separate
15644    non-terminal, but part of a function declarator, which looks like
15645
15646    -> trailing-type-specifier-seq abstract-declarator(opt)
15647
15648    Returns the type indicated by the type-id.
15649
15650    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15651    function.  */
15652
15653 static tree
15654 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15655 {
15656   cp_token *token;
15657   tree type;
15658
15659   /* Peek at the next token.  */
15660   token = cp_lexer_peek_token (parser->lexer);
15661   /* A late-specified return type is indicated by an initial '->'. */
15662   if (token->type != CPP_DEREF)
15663     return NULL_TREE;
15664
15665   /* Consume the ->.  */
15666   cp_lexer_consume_token (parser->lexer);
15667
15668   if (quals >= 0)
15669     {
15670       /* DR 1207: 'this' is in scope in the trailing return type.  */
15671       tree this_parm = build_this_parm (current_class_type, quals);
15672       gcc_assert (current_class_ptr == NULL_TREE);
15673       current_class_ref
15674         = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15675       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
15676       current_class_ptr = this_parm;
15677     }
15678
15679   type = cp_parser_trailing_type_id (parser);
15680
15681   if (current_class_type)
15682     current_class_ptr = current_class_ref = NULL_TREE;
15683
15684   return type;
15685 }
15686
15687 /* Parse a declarator-id.
15688
15689    declarator-id:
15690      id-expression
15691      :: [opt] nested-name-specifier [opt] type-name
15692
15693    In the `id-expression' case, the value returned is as for
15694    cp_parser_id_expression if the id-expression was an unqualified-id.
15695    If the id-expression was a qualified-id, then a SCOPE_REF is
15696    returned.  The first operand is the scope (either a NAMESPACE_DECL
15697    or TREE_TYPE), but the second is still just a representation of an
15698    unqualified-id.  */
15699
15700 static tree
15701 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15702 {
15703   tree id;
15704   /* The expression must be an id-expression.  Assume that qualified
15705      names are the names of types so that:
15706
15707        template <class T>
15708        int S<T>::R::i = 3;
15709
15710      will work; we must treat `S<T>::R' as the name of a type.
15711      Similarly, assume that qualified names are templates, where
15712      required, so that:
15713
15714        template <class T>
15715        int S<T>::R<T>::i = 3;
15716
15717      will work, too.  */
15718   id = cp_parser_id_expression (parser,
15719                                 /*template_keyword_p=*/false,
15720                                 /*check_dependency_p=*/false,
15721                                 /*template_p=*/NULL,
15722                                 /*declarator_p=*/true,
15723                                 optional_p);
15724   if (id && BASELINK_P (id))
15725     id = BASELINK_FUNCTIONS (id);
15726   return id;
15727 }
15728
15729 /* Parse a type-id.
15730
15731    type-id:
15732      type-specifier-seq abstract-declarator [opt]
15733
15734    Returns the TYPE specified.  */
15735
15736 static tree
15737 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15738                      bool is_trailing_return)
15739 {
15740   cp_decl_specifier_seq type_specifier_seq;
15741   cp_declarator *abstract_declarator;
15742
15743   /* Parse the type-specifier-seq.  */
15744   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15745                                 is_trailing_return,
15746                                 &type_specifier_seq);
15747   if (type_specifier_seq.type == error_mark_node)
15748     return error_mark_node;
15749
15750   /* There might or might not be an abstract declarator.  */
15751   cp_parser_parse_tentatively (parser);
15752   /* Look for the declarator.  */
15753   abstract_declarator
15754     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15755                             /*parenthesized_p=*/NULL,
15756                             /*member_p=*/false);
15757   /* Check to see if there really was a declarator.  */
15758   if (!cp_parser_parse_definitely (parser))
15759     abstract_declarator = NULL;
15760
15761   if (type_specifier_seq.type
15762       && type_uses_auto (type_specifier_seq.type))
15763     {
15764       /* A type-id with type 'auto' is only ok if the abstract declarator
15765          is a function declarator with a late-specified return type.  */
15766       if (abstract_declarator
15767           && abstract_declarator->kind == cdk_function
15768           && abstract_declarator->u.function.late_return_type)
15769         /* OK */;
15770       else
15771         {
15772           error ("invalid use of %<auto%>");
15773           return error_mark_node;
15774         }
15775     }
15776   
15777   return groktypename (&type_specifier_seq, abstract_declarator,
15778                        is_template_arg);
15779 }
15780
15781 static tree cp_parser_type_id (cp_parser *parser)
15782 {
15783   return cp_parser_type_id_1 (parser, false, false);
15784 }
15785
15786 static tree cp_parser_template_type_arg (cp_parser *parser)
15787 {
15788   tree r;
15789   const char *saved_message = parser->type_definition_forbidden_message;
15790   parser->type_definition_forbidden_message
15791     = G_("types may not be defined in template arguments");
15792   r = cp_parser_type_id_1 (parser, true, false);
15793   parser->type_definition_forbidden_message = saved_message;
15794   return r;
15795 }
15796
15797 static tree cp_parser_trailing_type_id (cp_parser *parser)
15798 {
15799   return cp_parser_type_id_1 (parser, false, true);
15800 }
15801
15802 /* Parse a type-specifier-seq.
15803
15804    type-specifier-seq:
15805      type-specifier type-specifier-seq [opt]
15806
15807    GNU extension:
15808
15809    type-specifier-seq:
15810      attributes type-specifier-seq [opt]
15811
15812    If IS_DECLARATION is true, we are at the start of a "condition" or
15813    exception-declaration, so we might be followed by a declarator-id.
15814
15815    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15816    i.e. we've just seen "->".
15817
15818    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15819
15820 static void
15821 cp_parser_type_specifier_seq (cp_parser* parser,
15822                               bool is_declaration,
15823                               bool is_trailing_return,
15824                               cp_decl_specifier_seq *type_specifier_seq)
15825 {
15826   bool seen_type_specifier = false;
15827   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15828   cp_token *start_token = NULL;
15829
15830   /* Clear the TYPE_SPECIFIER_SEQ.  */
15831   clear_decl_specs (type_specifier_seq);
15832
15833   /* In the context of a trailing return type, enum E { } is an
15834      elaborated-type-specifier followed by a function-body, not an
15835      enum-specifier.  */
15836   if (is_trailing_return)
15837     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15838
15839   /* Parse the type-specifiers and attributes.  */
15840   while (true)
15841     {
15842       tree type_specifier;
15843       bool is_cv_qualifier;
15844
15845       /* Check for attributes first.  */
15846       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15847         {
15848           type_specifier_seq->attributes =
15849             chainon (type_specifier_seq->attributes,
15850                      cp_parser_attributes_opt (parser));
15851           continue;
15852         }
15853
15854       /* record the token of the beginning of the type specifier seq,
15855          for error reporting purposes*/
15856      if (!start_token)
15857        start_token = cp_lexer_peek_token (parser->lexer);
15858
15859       /* Look for the type-specifier.  */
15860       type_specifier = cp_parser_type_specifier (parser,
15861                                                  flags,
15862                                                  type_specifier_seq,
15863                                                  /*is_declaration=*/false,
15864                                                  NULL,
15865                                                  &is_cv_qualifier);
15866       if (!type_specifier)
15867         {
15868           /* If the first type-specifier could not be found, this is not a
15869              type-specifier-seq at all.  */
15870           if (!seen_type_specifier)
15871             {
15872               cp_parser_error (parser, "expected type-specifier");
15873               type_specifier_seq->type = error_mark_node;
15874               return;
15875             }
15876           /* If subsequent type-specifiers could not be found, the
15877              type-specifier-seq is complete.  */
15878           break;
15879         }
15880
15881       seen_type_specifier = true;
15882       /* The standard says that a condition can be:
15883
15884             type-specifier-seq declarator = assignment-expression
15885
15886          However, given:
15887
15888            struct S {};
15889            if (int S = ...)
15890
15891          we should treat the "S" as a declarator, not as a
15892          type-specifier.  The standard doesn't say that explicitly for
15893          type-specifier-seq, but it does say that for
15894          decl-specifier-seq in an ordinary declaration.  Perhaps it
15895          would be clearer just to allow a decl-specifier-seq here, and
15896          then add a semantic restriction that if any decl-specifiers
15897          that are not type-specifiers appear, the program is invalid.  */
15898       if (is_declaration && !is_cv_qualifier)
15899         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15900     }
15901
15902   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15903 }
15904
15905 /* Parse a parameter-declaration-clause.
15906
15907    parameter-declaration-clause:
15908      parameter-declaration-list [opt] ... [opt]
15909      parameter-declaration-list , ...
15910
15911    Returns a representation for the parameter declarations.  A return
15912    value of NULL indicates a parameter-declaration-clause consisting
15913    only of an ellipsis.  */
15914
15915 static tree
15916 cp_parser_parameter_declaration_clause (cp_parser* parser)
15917 {
15918   tree parameters;
15919   cp_token *token;
15920   bool ellipsis_p;
15921   bool is_error;
15922
15923   /* Peek at the next token.  */
15924   token = cp_lexer_peek_token (parser->lexer);
15925   /* Check for trivial parameter-declaration-clauses.  */
15926   if (token->type == CPP_ELLIPSIS)
15927     {
15928       /* Consume the `...' token.  */
15929       cp_lexer_consume_token (parser->lexer);
15930       return NULL_TREE;
15931     }
15932   else if (token->type == CPP_CLOSE_PAREN)
15933     /* There are no parameters.  */
15934     {
15935 #ifndef NO_IMPLICIT_EXTERN_C
15936       if (in_system_header && current_class_type == NULL
15937           && current_lang_name == lang_name_c)
15938         return NULL_TREE;
15939       else
15940 #endif
15941         return void_list_node;
15942     }
15943   /* Check for `(void)', too, which is a special case.  */
15944   else if (token->keyword == RID_VOID
15945            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15946                == CPP_CLOSE_PAREN))
15947     {
15948       /* Consume the `void' token.  */
15949       cp_lexer_consume_token (parser->lexer);
15950       /* There are no parameters.  */
15951       return void_list_node;
15952     }
15953
15954   /* Parse the parameter-declaration-list.  */
15955   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15956   /* If a parse error occurred while parsing the
15957      parameter-declaration-list, then the entire
15958      parameter-declaration-clause is erroneous.  */
15959   if (is_error)
15960     return NULL;
15961
15962   /* Peek at the next token.  */
15963   token = cp_lexer_peek_token (parser->lexer);
15964   /* If it's a `,', the clause should terminate with an ellipsis.  */
15965   if (token->type == CPP_COMMA)
15966     {
15967       /* Consume the `,'.  */
15968       cp_lexer_consume_token (parser->lexer);
15969       /* Expect an ellipsis.  */
15970       ellipsis_p
15971         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15972     }
15973   /* It might also be `...' if the optional trailing `,' was
15974      omitted.  */
15975   else if (token->type == CPP_ELLIPSIS)
15976     {
15977       /* Consume the `...' token.  */
15978       cp_lexer_consume_token (parser->lexer);
15979       /* And remember that we saw it.  */
15980       ellipsis_p = true;
15981     }
15982   else
15983     ellipsis_p = false;
15984
15985   /* Finish the parameter list.  */
15986   if (!ellipsis_p)
15987     parameters = chainon (parameters, void_list_node);
15988
15989   return parameters;
15990 }
15991
15992 /* Parse a parameter-declaration-list.
15993
15994    parameter-declaration-list:
15995      parameter-declaration
15996      parameter-declaration-list , parameter-declaration
15997
15998    Returns a representation of the parameter-declaration-list, as for
15999    cp_parser_parameter_declaration_clause.  However, the
16000    `void_list_node' is never appended to the list.  Upon return,
16001    *IS_ERROR will be true iff an error occurred.  */
16002
16003 static tree
16004 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16005 {
16006   tree parameters = NULL_TREE;
16007   tree *tail = &parameters; 
16008   bool saved_in_unbraced_linkage_specification_p;
16009   int index = 0;
16010
16011   /* Assume all will go well.  */
16012   *is_error = false;
16013   /* The special considerations that apply to a function within an
16014      unbraced linkage specifications do not apply to the parameters
16015      to the function.  */
16016   saved_in_unbraced_linkage_specification_p 
16017     = parser->in_unbraced_linkage_specification_p;
16018   parser->in_unbraced_linkage_specification_p = false;
16019
16020   /* Look for more parameters.  */
16021   while (true)
16022     {
16023       cp_parameter_declarator *parameter;
16024       tree decl = error_mark_node;
16025       bool parenthesized_p = false;
16026       /* Parse the parameter.  */
16027       parameter
16028         = cp_parser_parameter_declaration (parser,
16029                                            /*template_parm_p=*/false,
16030                                            &parenthesized_p);
16031
16032       /* We don't know yet if the enclosing context is deprecated, so wait
16033          and warn in grokparms if appropriate.  */
16034       deprecated_state = DEPRECATED_SUPPRESS;
16035
16036       if (parameter)
16037         decl = grokdeclarator (parameter->declarator,
16038                                &parameter->decl_specifiers,
16039                                PARM,
16040                                parameter->default_argument != NULL_TREE,
16041                                &parameter->decl_specifiers.attributes);
16042
16043       deprecated_state = DEPRECATED_NORMAL;
16044
16045       /* If a parse error occurred parsing the parameter declaration,
16046          then the entire parameter-declaration-list is erroneous.  */
16047       if (decl == error_mark_node)
16048         {
16049           *is_error = true;
16050           parameters = error_mark_node;
16051           break;
16052         }
16053
16054       if (parameter->decl_specifiers.attributes)
16055         cplus_decl_attributes (&decl,
16056                                parameter->decl_specifiers.attributes,
16057                                0);
16058       if (DECL_NAME (decl))
16059         decl = pushdecl (decl);
16060
16061       if (decl != error_mark_node)
16062         {
16063           retrofit_lang_decl (decl);
16064           DECL_PARM_INDEX (decl) = ++index;
16065           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16066         }
16067
16068       /* Add the new parameter to the list.  */
16069       *tail = build_tree_list (parameter->default_argument, decl);
16070       tail = &TREE_CHAIN (*tail);
16071
16072       /* Peek at the next token.  */
16073       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16074           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16075           /* These are for Objective-C++ */
16076           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16077           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16078         /* The parameter-declaration-list is complete.  */
16079         break;
16080       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16081         {
16082           cp_token *token;
16083
16084           /* Peek at the next token.  */
16085           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16086           /* If it's an ellipsis, then the list is complete.  */
16087           if (token->type == CPP_ELLIPSIS)
16088             break;
16089           /* Otherwise, there must be more parameters.  Consume the
16090              `,'.  */
16091           cp_lexer_consume_token (parser->lexer);
16092           /* When parsing something like:
16093
16094                 int i(float f, double d)
16095
16096              we can tell after seeing the declaration for "f" that we
16097              are not looking at an initialization of a variable "i",
16098              but rather at the declaration of a function "i".
16099
16100              Due to the fact that the parsing of template arguments
16101              (as specified to a template-id) requires backtracking we
16102              cannot use this technique when inside a template argument
16103              list.  */
16104           if (!parser->in_template_argument_list_p
16105               && !parser->in_type_id_in_expr_p
16106               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16107               /* However, a parameter-declaration of the form
16108                  "foat(f)" (which is a valid declaration of a
16109                  parameter "f") can also be interpreted as an
16110                  expression (the conversion of "f" to "float").  */
16111               && !parenthesized_p)
16112             cp_parser_commit_to_tentative_parse (parser);
16113         }
16114       else
16115         {
16116           cp_parser_error (parser, "expected %<,%> or %<...%>");
16117           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16118             cp_parser_skip_to_closing_parenthesis (parser,
16119                                                    /*recovering=*/true,
16120                                                    /*or_comma=*/false,
16121                                                    /*consume_paren=*/false);
16122           break;
16123         }
16124     }
16125
16126   parser->in_unbraced_linkage_specification_p
16127     = saved_in_unbraced_linkage_specification_p;
16128
16129   return parameters;
16130 }
16131
16132 /* Parse a parameter declaration.
16133
16134    parameter-declaration:
16135      decl-specifier-seq ... [opt] declarator
16136      decl-specifier-seq declarator = assignment-expression
16137      decl-specifier-seq ... [opt] abstract-declarator [opt]
16138      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16139
16140    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16141    declares a template parameter.  (In that case, a non-nested `>'
16142    token encountered during the parsing of the assignment-expression
16143    is not interpreted as a greater-than operator.)
16144
16145    Returns a representation of the parameter, or NULL if an error
16146    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16147    true iff the declarator is of the form "(p)".  */
16148
16149 static cp_parameter_declarator *
16150 cp_parser_parameter_declaration (cp_parser *parser,
16151                                  bool template_parm_p,
16152                                  bool *parenthesized_p)
16153 {
16154   int declares_class_or_enum;
16155   cp_decl_specifier_seq decl_specifiers;
16156   cp_declarator *declarator;
16157   tree default_argument;
16158   cp_token *token = NULL, *declarator_token_start = NULL;
16159   const char *saved_message;
16160
16161   /* In a template parameter, `>' is not an operator.
16162
16163      [temp.param]
16164
16165      When parsing a default template-argument for a non-type
16166      template-parameter, the first non-nested `>' is taken as the end
16167      of the template parameter-list rather than a greater-than
16168      operator.  */
16169
16170   /* Type definitions may not appear in parameter types.  */
16171   saved_message = parser->type_definition_forbidden_message;
16172   parser->type_definition_forbidden_message
16173     = G_("types may not be defined in parameter types");
16174
16175   /* Parse the declaration-specifiers.  */
16176   cp_parser_decl_specifier_seq (parser,
16177                                 CP_PARSER_FLAGS_NONE,
16178                                 &decl_specifiers,
16179                                 &declares_class_or_enum);
16180
16181   /* Complain about missing 'typename' or other invalid type names.  */
16182   if (!decl_specifiers.any_type_specifiers_p)
16183     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16184
16185   /* If an error occurred, there's no reason to attempt to parse the
16186      rest of the declaration.  */
16187   if (cp_parser_error_occurred (parser))
16188     {
16189       parser->type_definition_forbidden_message = saved_message;
16190       return NULL;
16191     }
16192
16193   /* Peek at the next token.  */
16194   token = cp_lexer_peek_token (parser->lexer);
16195
16196   /* If the next token is a `)', `,', `=', `>', or `...', then there
16197      is no declarator. However, when variadic templates are enabled,
16198      there may be a declarator following `...'.  */
16199   if (token->type == CPP_CLOSE_PAREN
16200       || token->type == CPP_COMMA
16201       || token->type == CPP_EQ
16202       || token->type == CPP_GREATER)
16203     {
16204       declarator = NULL;
16205       if (parenthesized_p)
16206         *parenthesized_p = false;
16207     }
16208   /* Otherwise, there should be a declarator.  */
16209   else
16210     {
16211       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16212       parser->default_arg_ok_p = false;
16213
16214       /* After seeing a decl-specifier-seq, if the next token is not a
16215          "(", there is no possibility that the code is a valid
16216          expression.  Therefore, if parsing tentatively, we commit at
16217          this point.  */
16218       if (!parser->in_template_argument_list_p
16219           /* In an expression context, having seen:
16220
16221                (int((char ...
16222
16223              we cannot be sure whether we are looking at a
16224              function-type (taking a "char" as a parameter) or a cast
16225              of some object of type "char" to "int".  */
16226           && !parser->in_type_id_in_expr_p
16227           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16228           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16229           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16230         cp_parser_commit_to_tentative_parse (parser);
16231       /* Parse the declarator.  */
16232       declarator_token_start = token;
16233       declarator = cp_parser_declarator (parser,
16234                                          CP_PARSER_DECLARATOR_EITHER,
16235                                          /*ctor_dtor_or_conv_p=*/NULL,
16236                                          parenthesized_p,
16237                                          /*member_p=*/false);
16238       parser->default_arg_ok_p = saved_default_arg_ok_p;
16239       /* After the declarator, allow more attributes.  */
16240       decl_specifiers.attributes
16241         = chainon (decl_specifiers.attributes,
16242                    cp_parser_attributes_opt (parser));
16243     }
16244
16245   /* If the next token is an ellipsis, and we have not seen a
16246      declarator name, and the type of the declarator contains parameter
16247      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16248      a parameter pack expansion expression. Otherwise, leave the
16249      ellipsis for a C-style variadic function. */
16250   token = cp_lexer_peek_token (parser->lexer);
16251   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16252     {
16253       tree type = decl_specifiers.type;
16254
16255       if (type && DECL_P (type))
16256         type = TREE_TYPE (type);
16257
16258       if (type
16259           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16260           && declarator_can_be_parameter_pack (declarator)
16261           && (!declarator || !declarator->parameter_pack_p)
16262           && uses_parameter_packs (type))
16263         {
16264           /* Consume the `...'. */
16265           cp_lexer_consume_token (parser->lexer);
16266           maybe_warn_variadic_templates ();
16267           
16268           /* Build a pack expansion type */
16269           if (declarator)
16270             declarator->parameter_pack_p = true;
16271           else
16272             decl_specifiers.type = make_pack_expansion (type);
16273         }
16274     }
16275
16276   /* The restriction on defining new types applies only to the type
16277      of the parameter, not to the default argument.  */
16278   parser->type_definition_forbidden_message = saved_message;
16279
16280   /* If the next token is `=', then process a default argument.  */
16281   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16282     {
16283       /* Consume the `='.  */
16284       cp_lexer_consume_token (parser->lexer);
16285
16286       /* If we are defining a class, then the tokens that make up the
16287          default argument must be saved and processed later.  */
16288       if (!template_parm_p && at_class_scope_p ()
16289           && TYPE_BEING_DEFINED (current_class_type)
16290           && !LAMBDA_TYPE_P (current_class_type))
16291         {
16292           unsigned depth = 0;
16293           int maybe_template_id = 0;
16294           cp_token *first_token;
16295           cp_token *token;
16296
16297           /* Add tokens until we have processed the entire default
16298              argument.  We add the range [first_token, token).  */
16299           first_token = cp_lexer_peek_token (parser->lexer);
16300           while (true)
16301             {
16302               bool done = false;
16303
16304               /* Peek at the next token.  */
16305               token = cp_lexer_peek_token (parser->lexer);
16306               /* What we do depends on what token we have.  */
16307               switch (token->type)
16308                 {
16309                   /* In valid code, a default argument must be
16310                      immediately followed by a `,' `)', or `...'.  */
16311                 case CPP_COMMA:
16312                   if (depth == 0 && maybe_template_id)
16313                     {
16314                       /* If we've seen a '<', we might be in a
16315                          template-argument-list.  Until Core issue 325 is
16316                          resolved, we don't know how this situation ought
16317                          to be handled, so try to DTRT.  We check whether
16318                          what comes after the comma is a valid parameter
16319                          declaration list.  If it is, then the comma ends
16320                          the default argument; otherwise the default
16321                          argument continues.  */
16322                       bool error = false;
16323                       tree t;
16324
16325                       /* Set ITALP so cp_parser_parameter_declaration_list
16326                          doesn't decide to commit to this parse.  */
16327                       bool saved_italp = parser->in_template_argument_list_p;
16328                       parser->in_template_argument_list_p = true;
16329
16330                       cp_parser_parse_tentatively (parser);
16331                       cp_lexer_consume_token (parser->lexer);
16332                       begin_scope (sk_function_parms, NULL_TREE);
16333                       cp_parser_parameter_declaration_list (parser, &error);
16334                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16335                         pop_binding (DECL_NAME (t), t);
16336                       leave_scope ();
16337                       if (!cp_parser_error_occurred (parser) && !error)
16338                         done = true;
16339                       cp_parser_abort_tentative_parse (parser);
16340
16341                       parser->in_template_argument_list_p = saved_italp;
16342                       break;
16343                     }
16344                 case CPP_CLOSE_PAREN:
16345                 case CPP_ELLIPSIS:
16346                   /* If we run into a non-nested `;', `}', or `]',
16347                      then the code is invalid -- but the default
16348                      argument is certainly over.  */
16349                 case CPP_SEMICOLON:
16350                 case CPP_CLOSE_BRACE:
16351                 case CPP_CLOSE_SQUARE:
16352                   if (depth == 0)
16353                     done = true;
16354                   /* Update DEPTH, if necessary.  */
16355                   else if (token->type == CPP_CLOSE_PAREN
16356                            || token->type == CPP_CLOSE_BRACE
16357                            || token->type == CPP_CLOSE_SQUARE)
16358                     --depth;
16359                   break;
16360
16361                 case CPP_OPEN_PAREN:
16362                 case CPP_OPEN_SQUARE:
16363                 case CPP_OPEN_BRACE:
16364                   ++depth;
16365                   break;
16366
16367                 case CPP_LESS:
16368                   if (depth == 0)
16369                     /* This might be the comparison operator, or it might
16370                        start a template argument list.  */
16371                     ++maybe_template_id;
16372                   break;
16373
16374                 case CPP_RSHIFT:
16375                   if (cxx_dialect == cxx98)
16376                     break;
16377                   /* Fall through for C++0x, which treats the `>>'
16378                      operator like two `>' tokens in certain
16379                      cases.  */
16380
16381                 case CPP_GREATER:
16382                   if (depth == 0)
16383                     {
16384                       /* This might be an operator, or it might close a
16385                          template argument list.  But if a previous '<'
16386                          started a template argument list, this will have
16387                          closed it, so we can't be in one anymore.  */
16388                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16389                       if (maybe_template_id < 0)
16390                         maybe_template_id = 0;
16391                     }
16392                   break;
16393
16394                   /* If we run out of tokens, issue an error message.  */
16395                 case CPP_EOF:
16396                 case CPP_PRAGMA_EOL:
16397                   error_at (token->location, "file ends in default argument");
16398                   done = true;
16399                   break;
16400
16401                 case CPP_NAME:
16402                 case CPP_SCOPE:
16403                   /* In these cases, we should look for template-ids.
16404                      For example, if the default argument is
16405                      `X<int, double>()', we need to do name lookup to
16406                      figure out whether or not `X' is a template; if
16407                      so, the `,' does not end the default argument.
16408
16409                      That is not yet done.  */
16410                   break;
16411
16412                 default:
16413                   break;
16414                 }
16415
16416               /* If we've reached the end, stop.  */
16417               if (done)
16418                 break;
16419
16420               /* Add the token to the token block.  */
16421               token = cp_lexer_consume_token (parser->lexer);
16422             }
16423
16424           /* Create a DEFAULT_ARG to represent the unparsed default
16425              argument.  */
16426           default_argument = make_node (DEFAULT_ARG);
16427           DEFARG_TOKENS (default_argument)
16428             = cp_token_cache_new (first_token, token);
16429           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16430         }
16431       /* Outside of a class definition, we can just parse the
16432          assignment-expression.  */
16433       else
16434         {
16435           token = cp_lexer_peek_token (parser->lexer);
16436           default_argument 
16437             = cp_parser_default_argument (parser, template_parm_p);
16438         }
16439
16440       if (!parser->default_arg_ok_p)
16441         {
16442           if (flag_permissive)
16443             warning (0, "deprecated use of default argument for parameter of non-function");
16444           else
16445             {
16446               error_at (token->location,
16447                         "default arguments are only "
16448                         "permitted for function parameters");
16449               default_argument = NULL_TREE;
16450             }
16451         }
16452       else if ((declarator && declarator->parameter_pack_p)
16453                || (decl_specifiers.type
16454                    && PACK_EXPANSION_P (decl_specifiers.type)))
16455         {
16456           /* Find the name of the parameter pack.  */     
16457           cp_declarator *id_declarator = declarator;
16458           while (id_declarator && id_declarator->kind != cdk_id)
16459             id_declarator = id_declarator->declarator;
16460           
16461           if (id_declarator && id_declarator->kind == cdk_id)
16462             error_at (declarator_token_start->location,
16463                       template_parm_p 
16464                       ? "template parameter pack %qD"
16465                       " cannot have a default argument"
16466                       : "parameter pack %qD cannot have a default argument",
16467                       id_declarator->u.id.unqualified_name);
16468           else
16469             error_at (declarator_token_start->location,
16470                       template_parm_p 
16471                       ? "template parameter pack cannot have a default argument"
16472                       : "parameter pack cannot have a default argument");
16473           
16474           default_argument = NULL_TREE;
16475         }
16476     }
16477   else
16478     default_argument = NULL_TREE;
16479
16480   return make_parameter_declarator (&decl_specifiers,
16481                                     declarator,
16482                                     default_argument);
16483 }
16484
16485 /* Parse a default argument and return it.
16486
16487    TEMPLATE_PARM_P is true if this is a default argument for a
16488    non-type template parameter.  */
16489 static tree
16490 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16491 {
16492   tree default_argument = NULL_TREE;
16493   bool saved_greater_than_is_operator_p;
16494   bool saved_local_variables_forbidden_p;
16495
16496   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16497      set correctly.  */
16498   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16499   parser->greater_than_is_operator_p = !template_parm_p;
16500   /* Local variable names (and the `this' keyword) may not
16501      appear in a default argument.  */
16502   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16503   parser->local_variables_forbidden_p = true;
16504   /* Parse the assignment-expression.  */
16505   if (template_parm_p)
16506     push_deferring_access_checks (dk_no_deferred);
16507   default_argument
16508     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16509   if (template_parm_p)
16510     pop_deferring_access_checks ();
16511   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16512   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16513
16514   return default_argument;
16515 }
16516
16517 /* Parse a function-body.
16518
16519    function-body:
16520      compound_statement  */
16521
16522 static void
16523 cp_parser_function_body (cp_parser *parser)
16524 {
16525   cp_parser_compound_statement (parser, NULL, false, true);
16526 }
16527
16528 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16529    true if a ctor-initializer was present.  */
16530
16531 static bool
16532 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16533 {
16534   tree body, list;
16535   bool ctor_initializer_p;
16536   const bool check_body_p =
16537      DECL_CONSTRUCTOR_P (current_function_decl)
16538      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16539   tree last = NULL;
16540
16541   /* Begin the function body.  */
16542   body = begin_function_body ();
16543   /* Parse the optional ctor-initializer.  */
16544   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16545
16546   /* If we're parsing a constexpr constructor definition, we need
16547      to check that the constructor body is indeed empty.  However,
16548      before we get to cp_parser_function_body lot of junk has been
16549      generated, so we can't just check that we have an empty block.
16550      Rather we take a snapshot of the outermost block, and check whether
16551      cp_parser_function_body changed its state.  */
16552   if (check_body_p)
16553     {
16554       list = body;
16555       if (TREE_CODE (list) == BIND_EXPR)
16556         list = BIND_EXPR_BODY (list);
16557       if (TREE_CODE (list) == STATEMENT_LIST
16558           && STATEMENT_LIST_TAIL (list) != NULL)
16559         last = STATEMENT_LIST_TAIL (list)->stmt;
16560     }
16561   /* Parse the function-body.  */
16562   cp_parser_function_body (parser);
16563   if (check_body_p)
16564     check_constexpr_ctor_body (last, list);
16565   /* Finish the function body.  */
16566   finish_function_body (body);
16567
16568   return ctor_initializer_p;
16569 }
16570
16571 /* Parse an initializer.
16572
16573    initializer:
16574      = initializer-clause
16575      ( expression-list )
16576
16577    Returns an expression representing the initializer.  If no
16578    initializer is present, NULL_TREE is returned.
16579
16580    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16581    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16582    set to TRUE if there is no initializer present.  If there is an
16583    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16584    is set to true; otherwise it is set to false.  */
16585
16586 static tree
16587 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16588                        bool* non_constant_p)
16589 {
16590   cp_token *token;
16591   tree init;
16592
16593   /* Peek at the next token.  */
16594   token = cp_lexer_peek_token (parser->lexer);
16595
16596   /* Let our caller know whether or not this initializer was
16597      parenthesized.  */
16598   *is_direct_init = (token->type != CPP_EQ);
16599   /* Assume that the initializer is constant.  */
16600   *non_constant_p = false;
16601
16602   if (token->type == CPP_EQ)
16603     {
16604       /* Consume the `='.  */
16605       cp_lexer_consume_token (parser->lexer);
16606       /* Parse the initializer-clause.  */
16607       init = cp_parser_initializer_clause (parser, non_constant_p);
16608     }
16609   else if (token->type == CPP_OPEN_PAREN)
16610     {
16611       VEC(tree,gc) *vec;
16612       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16613                                                      /*cast_p=*/false,
16614                                                      /*allow_expansion_p=*/true,
16615                                                      non_constant_p);
16616       if (vec == NULL)
16617         return error_mark_node;
16618       init = build_tree_list_vec (vec);
16619       release_tree_vector (vec);
16620     }
16621   else if (token->type == CPP_OPEN_BRACE)
16622     {
16623       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16624       init = cp_parser_braced_list (parser, non_constant_p);
16625       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16626     }
16627   else
16628     {
16629       /* Anything else is an error.  */
16630       cp_parser_error (parser, "expected initializer");
16631       init = error_mark_node;
16632     }
16633
16634   return init;
16635 }
16636
16637 /* Parse an initializer-clause.
16638
16639    initializer-clause:
16640      assignment-expression
16641      braced-init-list
16642
16643    Returns an expression representing the initializer.
16644
16645    If the `assignment-expression' production is used the value
16646    returned is simply a representation for the expression.
16647
16648    Otherwise, calls cp_parser_braced_list.  */
16649
16650 static tree
16651 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16652 {
16653   tree initializer;
16654
16655   /* Assume the expression is constant.  */
16656   *non_constant_p = false;
16657
16658   /* If it is not a `{', then we are looking at an
16659      assignment-expression.  */
16660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16661     {
16662       initializer
16663         = cp_parser_constant_expression (parser,
16664                                         /*allow_non_constant_p=*/true,
16665                                         non_constant_p);
16666     }
16667   else
16668     initializer = cp_parser_braced_list (parser, non_constant_p);
16669
16670   return initializer;
16671 }
16672
16673 /* Parse a brace-enclosed initializer list.
16674
16675    braced-init-list:
16676      { initializer-list , [opt] }
16677      { }
16678
16679    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16680    the elements of the initializer-list (or NULL, if the last
16681    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16682    NULL_TREE.  There is no way to detect whether or not the optional
16683    trailing `,' was provided.  NON_CONSTANT_P is as for
16684    cp_parser_initializer.  */     
16685
16686 static tree
16687 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16688 {
16689   tree initializer;
16690
16691   /* Consume the `{' token.  */
16692   cp_lexer_consume_token (parser->lexer);
16693   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16694   initializer = make_node (CONSTRUCTOR);
16695   /* If it's not a `}', then there is a non-trivial initializer.  */
16696   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16697     {
16698       /* Parse the initializer list.  */
16699       CONSTRUCTOR_ELTS (initializer)
16700         = cp_parser_initializer_list (parser, non_constant_p);
16701       /* A trailing `,' token is allowed.  */
16702       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16703         cp_lexer_consume_token (parser->lexer);
16704     }
16705   /* Now, there should be a trailing `}'.  */
16706   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16707   TREE_TYPE (initializer) = init_list_type_node;
16708   return initializer;
16709 }
16710
16711 /* Parse an initializer-list.
16712
16713    initializer-list:
16714      initializer-clause ... [opt]
16715      initializer-list , initializer-clause ... [opt]
16716
16717    GNU Extension:
16718
16719    initializer-list:
16720      designation initializer-clause ...[opt]
16721      initializer-list , designation initializer-clause ...[opt]
16722
16723    designation:
16724      . identifier =
16725      identifier :
16726      [ constant-expression ] =
16727
16728    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16729    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16730    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16731    as for cp_parser_initializer.  */
16732
16733 static VEC(constructor_elt,gc) *
16734 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16735 {
16736   VEC(constructor_elt,gc) *v = NULL;
16737
16738   /* Assume all of the expressions are constant.  */
16739   *non_constant_p = false;
16740
16741   /* Parse the rest of the list.  */
16742   while (true)
16743     {
16744       cp_token *token;
16745       tree designator;
16746       tree initializer;
16747       bool clause_non_constant_p;
16748
16749       /* If the next token is an identifier and the following one is a
16750          colon, we are looking at the GNU designated-initializer
16751          syntax.  */
16752       if (cp_parser_allow_gnu_extensions_p (parser)
16753           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16754           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16755         {
16756           /* Warn the user that they are using an extension.  */
16757           pedwarn (input_location, OPT_pedantic, 
16758                    "ISO C++ does not allow designated initializers");
16759           /* Consume the identifier.  */
16760           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16761           /* Consume the `:'.  */
16762           cp_lexer_consume_token (parser->lexer);
16763         }
16764       /* Also handle the C99 syntax, '. id ='.  */
16765       else if (cp_parser_allow_gnu_extensions_p (parser)
16766                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
16767                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
16768                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
16769         {
16770           /* Warn the user that they are using an extension.  */
16771           pedwarn (input_location, OPT_pedantic,
16772                    "ISO C++ does not allow C99 designated initializers");
16773           /* Consume the `.'.  */
16774           cp_lexer_consume_token (parser->lexer);
16775           /* Consume the identifier.  */
16776           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16777           /* Consume the `='.  */
16778           cp_lexer_consume_token (parser->lexer);
16779         }
16780       /* Also handle C99 array designators, '[ const ] ='.  */
16781       else if (cp_parser_allow_gnu_extensions_p (parser)
16782                && !c_dialect_objc ()
16783                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16784         {
16785           cp_lexer_consume_token (parser->lexer);
16786           designator = cp_parser_constant_expression (parser, false, NULL);
16787           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16788           cp_parser_require (parser, CPP_EQ, RT_EQ);
16789         }
16790       else
16791         designator = NULL_TREE;
16792
16793       /* Parse the initializer.  */
16794       initializer = cp_parser_initializer_clause (parser,
16795                                                   &clause_non_constant_p);
16796       /* If any clause is non-constant, so is the entire initializer.  */
16797       if (clause_non_constant_p)
16798         *non_constant_p = true;
16799
16800       /* If we have an ellipsis, this is an initializer pack
16801          expansion.  */
16802       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16803         {
16804           /* Consume the `...'.  */
16805           cp_lexer_consume_token (parser->lexer);
16806
16807           /* Turn the initializer into an initializer expansion.  */
16808           initializer = make_pack_expansion (initializer);
16809         }
16810
16811       /* Add it to the vector.  */
16812       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
16813
16814       /* If the next token is not a comma, we have reached the end of
16815          the list.  */
16816       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16817         break;
16818
16819       /* Peek at the next token.  */
16820       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16821       /* If the next token is a `}', then we're still done.  An
16822          initializer-clause can have a trailing `,' after the
16823          initializer-list and before the closing `}'.  */
16824       if (token->type == CPP_CLOSE_BRACE)
16825         break;
16826
16827       /* Consume the `,' token.  */
16828       cp_lexer_consume_token (parser->lexer);
16829     }
16830
16831   return v;
16832 }
16833
16834 /* Classes [gram.class] */
16835
16836 /* Parse a class-name.
16837
16838    class-name:
16839      identifier
16840      template-id
16841
16842    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16843    to indicate that names looked up in dependent types should be
16844    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16845    keyword has been used to indicate that the name that appears next
16846    is a template.  TAG_TYPE indicates the explicit tag given before
16847    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16848    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16849    is the class being defined in a class-head.
16850
16851    Returns the TYPE_DECL representing the class.  */
16852
16853 static tree
16854 cp_parser_class_name (cp_parser *parser,
16855                       bool typename_keyword_p,
16856                       bool template_keyword_p,
16857                       enum tag_types tag_type,
16858                       bool check_dependency_p,
16859                       bool class_head_p,
16860                       bool is_declaration)
16861 {
16862   tree decl;
16863   tree scope;
16864   bool typename_p;
16865   cp_token *token;
16866   tree identifier = NULL_TREE;
16867
16868   /* All class-names start with an identifier.  */
16869   token = cp_lexer_peek_token (parser->lexer);
16870   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16871     {
16872       cp_parser_error (parser, "expected class-name");
16873       return error_mark_node;
16874     }
16875
16876   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16877      to a template-id, so we save it here.  */
16878   scope = parser->scope;
16879   if (scope == error_mark_node)
16880     return error_mark_node;
16881
16882   /* Any name names a type if we're following the `typename' keyword
16883      in a qualified name where the enclosing scope is type-dependent.  */
16884   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16885                 && dependent_type_p (scope));
16886   /* Handle the common case (an identifier, but not a template-id)
16887      efficiently.  */
16888   if (token->type == CPP_NAME
16889       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16890     {
16891       cp_token *identifier_token;
16892       bool ambiguous_p;
16893
16894       /* Look for the identifier.  */
16895       identifier_token = cp_lexer_peek_token (parser->lexer);
16896       ambiguous_p = identifier_token->ambiguous_p;
16897       identifier = cp_parser_identifier (parser);
16898       /* If the next token isn't an identifier, we are certainly not
16899          looking at a class-name.  */
16900       if (identifier == error_mark_node)
16901         decl = error_mark_node;
16902       /* If we know this is a type-name, there's no need to look it
16903          up.  */
16904       else if (typename_p)
16905         decl = identifier;
16906       else
16907         {
16908           tree ambiguous_decls;
16909           /* If we already know that this lookup is ambiguous, then
16910              we've already issued an error message; there's no reason
16911              to check again.  */
16912           if (ambiguous_p)
16913             {
16914               cp_parser_simulate_error (parser);
16915               return error_mark_node;
16916             }
16917           /* If the next token is a `::', then the name must be a type
16918              name.
16919
16920              [basic.lookup.qual]
16921
16922              During the lookup for a name preceding the :: scope
16923              resolution operator, object, function, and enumerator
16924              names are ignored.  */
16925           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16926             tag_type = typename_type;
16927           /* Look up the name.  */
16928           decl = cp_parser_lookup_name (parser, identifier,
16929                                         tag_type,
16930                                         /*is_template=*/false,
16931                                         /*is_namespace=*/false,
16932                                         check_dependency_p,
16933                                         &ambiguous_decls,
16934                                         identifier_token->location);
16935           if (ambiguous_decls)
16936             {
16937               if (cp_parser_parsing_tentatively (parser))
16938                 cp_parser_simulate_error (parser);
16939               return error_mark_node;
16940             }
16941         }
16942     }
16943   else
16944     {
16945       /* Try a template-id.  */
16946       decl = cp_parser_template_id (parser, template_keyword_p,
16947                                     check_dependency_p,
16948                                     is_declaration);
16949       if (decl == error_mark_node)
16950         return error_mark_node;
16951     }
16952
16953   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16954
16955   /* If this is a typename, create a TYPENAME_TYPE.  */
16956   if (typename_p && decl != error_mark_node)
16957     {
16958       decl = make_typename_type (scope, decl, typename_type,
16959                                  /*complain=*/tf_error);
16960       if (decl != error_mark_node)
16961         decl = TYPE_NAME (decl);
16962     }
16963
16964   /* Check to see that it is really the name of a class.  */
16965   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16966       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16967       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16968     /* Situations like this:
16969
16970          template <typename T> struct A {
16971            typename T::template X<int>::I i;
16972          };
16973
16974        are problematic.  Is `T::template X<int>' a class-name?  The
16975        standard does not seem to be definitive, but there is no other
16976        valid interpretation of the following `::'.  Therefore, those
16977        names are considered class-names.  */
16978     {
16979       decl = make_typename_type (scope, decl, tag_type, tf_error);
16980       if (decl != error_mark_node)
16981         decl = TYPE_NAME (decl);
16982     }
16983   else if (TREE_CODE (decl) != TYPE_DECL
16984            || TREE_TYPE (decl) == error_mark_node
16985            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16986            /* In Objective-C 2.0, a classname followed by '.' starts a
16987               dot-syntax expression, and it's not a type-name.  */
16988            || (c_dialect_objc ()
16989                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16990                && objc_is_class_name (decl)))
16991     decl = error_mark_node;
16992
16993   if (decl == error_mark_node)
16994     cp_parser_error (parser, "expected class-name");
16995   else if (identifier && !parser->scope)
16996     maybe_note_name_used_in_class (identifier, decl);
16997
16998   return decl;
16999 }
17000
17001 /* Parse a class-specifier.
17002
17003    class-specifier:
17004      class-head { member-specification [opt] }
17005
17006    Returns the TREE_TYPE representing the class.  */
17007
17008 static tree
17009 cp_parser_class_specifier_1 (cp_parser* parser)
17010 {
17011   tree type;
17012   tree attributes = NULL_TREE;
17013   bool nested_name_specifier_p;
17014   unsigned saved_num_template_parameter_lists;
17015   bool saved_in_function_body;
17016   unsigned char in_statement;
17017   bool in_switch_statement_p;
17018   bool saved_in_unbraced_linkage_specification_p;
17019   tree old_scope = NULL_TREE;
17020   tree scope = NULL_TREE;
17021   tree bases;
17022   cp_token *closing_brace;
17023
17024   push_deferring_access_checks (dk_no_deferred);
17025
17026   /* Parse the class-head.  */
17027   type = cp_parser_class_head (parser,
17028                                &nested_name_specifier_p,
17029                                &attributes,
17030                                &bases);
17031   /* If the class-head was a semantic disaster, skip the entire body
17032      of the class.  */
17033   if (!type)
17034     {
17035       cp_parser_skip_to_end_of_block_or_statement (parser);
17036       pop_deferring_access_checks ();
17037       return error_mark_node;
17038     }
17039
17040   /* Look for the `{'.  */
17041   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17042     {
17043       pop_deferring_access_checks ();
17044       return error_mark_node;
17045     }
17046
17047   /* Process the base classes. If they're invalid, skip the 
17048      entire class body.  */
17049   if (!xref_basetypes (type, bases))
17050     {
17051       /* Consuming the closing brace yields better error messages
17052          later on.  */
17053       if (cp_parser_skip_to_closing_brace (parser))
17054         cp_lexer_consume_token (parser->lexer);
17055       pop_deferring_access_checks ();
17056       return error_mark_node;
17057     }
17058
17059   /* Issue an error message if type-definitions are forbidden here.  */
17060   cp_parser_check_type_definition (parser);
17061   /* Remember that we are defining one more class.  */
17062   ++parser->num_classes_being_defined;
17063   /* Inside the class, surrounding template-parameter-lists do not
17064      apply.  */
17065   saved_num_template_parameter_lists
17066     = parser->num_template_parameter_lists;
17067   parser->num_template_parameter_lists = 0;
17068   /* We are not in a function body.  */
17069   saved_in_function_body = parser->in_function_body;
17070   parser->in_function_body = false;
17071   /* Or in a loop.  */
17072   in_statement = parser->in_statement;
17073   parser->in_statement = 0;
17074   /* Or in a switch.  */
17075   in_switch_statement_p = parser->in_switch_statement_p;
17076   parser->in_switch_statement_p = false;
17077   /* We are not immediately inside an extern "lang" block.  */
17078   saved_in_unbraced_linkage_specification_p
17079     = parser->in_unbraced_linkage_specification_p;
17080   parser->in_unbraced_linkage_specification_p = false;
17081
17082   /* Start the class.  */
17083   if (nested_name_specifier_p)
17084     {
17085       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17086       old_scope = push_inner_scope (scope);
17087     }
17088   type = begin_class_definition (type, attributes);
17089
17090   if (type == error_mark_node)
17091     /* If the type is erroneous, skip the entire body of the class.  */
17092     cp_parser_skip_to_closing_brace (parser);
17093   else
17094     /* Parse the member-specification.  */
17095     cp_parser_member_specification_opt (parser);
17096
17097   /* Look for the trailing `}'.  */
17098   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17099   /* Look for trailing attributes to apply to this class.  */
17100   if (cp_parser_allow_gnu_extensions_p (parser))
17101     attributes = cp_parser_attributes_opt (parser);
17102   if (type != error_mark_node)
17103     type = finish_struct (type, attributes);
17104   if (nested_name_specifier_p)
17105     pop_inner_scope (old_scope, scope);
17106
17107   /* We've finished a type definition.  Check for the common syntax
17108      error of forgetting a semicolon after the definition.  We need to
17109      be careful, as we can't just check for not-a-semicolon and be done
17110      with it; the user might have typed:
17111
17112      class X { } c = ...;
17113      class X { } *p = ...;
17114
17115      and so forth.  Instead, enumerate all the possible tokens that
17116      might follow this production; if we don't see one of them, then
17117      complain and silently insert the semicolon.  */
17118   {
17119     cp_token *token = cp_lexer_peek_token (parser->lexer);
17120     bool want_semicolon = true;
17121
17122     switch (token->type)
17123       {
17124       case CPP_NAME:
17125       case CPP_SEMICOLON:
17126       case CPP_MULT:
17127       case CPP_AND:
17128       case CPP_OPEN_PAREN:
17129       case CPP_CLOSE_PAREN:
17130       case CPP_COMMA:
17131         want_semicolon = false;
17132         break;
17133
17134         /* While it's legal for type qualifiers and storage class
17135            specifiers to follow type definitions in the grammar, only
17136            compiler testsuites contain code like that.  Assume that if
17137            we see such code, then what we're really seeing is a case
17138            like:
17139
17140            class X { }
17141            const <type> var = ...;
17142
17143            or
17144
17145            class Y { }
17146            static <type> func (...) ...
17147
17148            i.e. the qualifier or specifier applies to the next
17149            declaration.  To do so, however, we need to look ahead one
17150            more token to see if *that* token is a type specifier.
17151
17152            This code could be improved to handle:
17153
17154            class Z { }
17155            static const <type> var = ...;  */
17156       case CPP_KEYWORD:
17157         if (keyword_is_decl_specifier (token->keyword))
17158           {
17159             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17160
17161             /* Handling user-defined types here would be nice, but very
17162                tricky.  */
17163             want_semicolon
17164               = (lookahead->type == CPP_KEYWORD
17165                  && keyword_begins_type_specifier (lookahead->keyword));
17166           }
17167         break;
17168       default:
17169         break;
17170       }
17171
17172     /* If we don't have a type, then something is very wrong and we
17173        shouldn't try to do anything clever.  Likewise for not seeing the
17174        closing brace.  */
17175     if (closing_brace && TYPE_P (type) && want_semicolon)
17176       {
17177         cp_token_position prev
17178           = cp_lexer_previous_token_position (parser->lexer);
17179         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17180         location_t loc = prev_token->location;
17181
17182         if (CLASSTYPE_DECLARED_CLASS (type))
17183           error_at (loc, "expected %<;%> after class definition");
17184         else if (TREE_CODE (type) == RECORD_TYPE)
17185           error_at (loc, "expected %<;%> after struct definition");
17186         else if (TREE_CODE (type) == UNION_TYPE)
17187           error_at (loc, "expected %<;%> after union definition");
17188         else
17189           gcc_unreachable ();
17190
17191         /* Unget one token and smash it to look as though we encountered
17192            a semicolon in the input stream.  */
17193         cp_lexer_set_token_position (parser->lexer, prev);
17194         token = cp_lexer_peek_token (parser->lexer);
17195         token->type = CPP_SEMICOLON;
17196         token->keyword = RID_MAX;
17197       }
17198   }
17199
17200   /* If this class is not itself within the scope of another class,
17201      then we need to parse the bodies of all of the queued function
17202      definitions.  Note that the queued functions defined in a class
17203      are not always processed immediately following the
17204      class-specifier for that class.  Consider:
17205
17206        struct A {
17207          struct B { void f() { sizeof (A); } };
17208        };
17209
17210      If `f' were processed before the processing of `A' were
17211      completed, there would be no way to compute the size of `A'.
17212      Note that the nesting we are interested in here is lexical --
17213      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17214      for:
17215
17216        struct A { struct B; };
17217        struct A::B { void f() { } };
17218
17219      there is no need to delay the parsing of `A::B::f'.  */
17220   if (--parser->num_classes_being_defined == 0)
17221     {
17222       tree fn;
17223       tree class_type = NULL_TREE;
17224       tree pushed_scope = NULL_TREE;
17225       unsigned ix;
17226       cp_default_arg_entry *e;
17227
17228       /* In a first pass, parse default arguments to the functions.
17229          Then, in a second pass, parse the bodies of the functions.
17230          This two-phased approach handles cases like:
17231
17232             struct S {
17233               void f() { g(); }
17234               void g(int i = 3);
17235             };
17236
17237          */
17238       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17239                         ix, e)
17240         {
17241           fn = e->decl;
17242           /* If there are default arguments that have not yet been processed,
17243              take care of them now.  */
17244           if (class_type != e->class_type)
17245             {
17246               if (pushed_scope)
17247                 pop_scope (pushed_scope);
17248               class_type = e->class_type;
17249               pushed_scope = push_scope (class_type);
17250             }
17251           /* Make sure that any template parameters are in scope.  */
17252           maybe_begin_member_template_processing (fn);
17253           /* Parse the default argument expressions.  */
17254           cp_parser_late_parsing_default_args (parser, fn);
17255           /* Remove any template parameters from the symbol table.  */
17256           maybe_end_member_template_processing ();
17257         }
17258       if (pushed_scope)
17259         pop_scope (pushed_scope);
17260       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17261       /* Now parse the body of the functions.  */
17262       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17263         cp_parser_late_parsing_for_member (parser, fn);
17264       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17265     }
17266
17267   /* Put back any saved access checks.  */
17268   pop_deferring_access_checks ();
17269
17270   /* Restore saved state.  */
17271   parser->in_switch_statement_p = in_switch_statement_p;
17272   parser->in_statement = in_statement;
17273   parser->in_function_body = saved_in_function_body;
17274   parser->num_template_parameter_lists
17275     = saved_num_template_parameter_lists;
17276   parser->in_unbraced_linkage_specification_p
17277     = saved_in_unbraced_linkage_specification_p;
17278
17279   return type;
17280 }
17281
17282 static tree
17283 cp_parser_class_specifier (cp_parser* parser)
17284 {
17285   tree ret;
17286   timevar_push (TV_PARSE_STRUCT);
17287   ret = cp_parser_class_specifier_1 (parser);
17288   timevar_pop (TV_PARSE_STRUCT);
17289   return ret;
17290 }
17291
17292 /* Parse a class-head.
17293
17294    class-head:
17295      class-key identifier [opt] base-clause [opt]
17296      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17297      class-key nested-name-specifier [opt] template-id
17298        base-clause [opt]
17299
17300    class-virt-specifier:
17301      final
17302
17303    GNU Extensions:
17304      class-key attributes identifier [opt] base-clause [opt]
17305      class-key attributes nested-name-specifier identifier base-clause [opt]
17306      class-key attributes nested-name-specifier [opt] template-id
17307        base-clause [opt]
17308
17309    Upon return BASES is initialized to the list of base classes (or
17310    NULL, if there are none) in the same form returned by
17311    cp_parser_base_clause.
17312
17313    Returns the TYPE of the indicated class.  Sets
17314    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17315    involving a nested-name-specifier was used, and FALSE otherwise.
17316
17317    Returns error_mark_node if this is not a class-head.
17318
17319    Returns NULL_TREE if the class-head is syntactically valid, but
17320    semantically invalid in a way that means we should skip the entire
17321    body of the class.  */
17322
17323 static tree
17324 cp_parser_class_head (cp_parser* parser,
17325                       bool* nested_name_specifier_p,
17326                       tree *attributes_p,
17327                       tree *bases)
17328 {
17329   tree nested_name_specifier;
17330   enum tag_types class_key;
17331   tree id = NULL_TREE;
17332   tree type = NULL_TREE;
17333   tree attributes;
17334   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17335   bool template_id_p = false;
17336   bool qualified_p = false;
17337   bool invalid_nested_name_p = false;
17338   bool invalid_explicit_specialization_p = false;
17339   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17340   tree pushed_scope = NULL_TREE;
17341   unsigned num_templates;
17342   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17343   /* Assume no nested-name-specifier will be present.  */
17344   *nested_name_specifier_p = false;
17345   /* Assume no template parameter lists will be used in defining the
17346      type.  */
17347   num_templates = 0;
17348   parser->colon_corrects_to_scope_p = false;
17349
17350   *bases = NULL_TREE;
17351
17352   /* Look for the class-key.  */
17353   class_key = cp_parser_class_key (parser);
17354   if (class_key == none_type)
17355     return error_mark_node;
17356
17357   /* Parse the attributes.  */
17358   attributes = cp_parser_attributes_opt (parser);
17359
17360   /* If the next token is `::', that is invalid -- but sometimes
17361      people do try to write:
17362
17363        struct ::S {};
17364
17365      Handle this gracefully by accepting the extra qualifier, and then
17366      issuing an error about it later if this really is a
17367      class-head.  If it turns out just to be an elaborated type
17368      specifier, remain silent.  */
17369   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17370     qualified_p = true;
17371
17372   push_deferring_access_checks (dk_no_check);
17373
17374   /* Determine the name of the class.  Begin by looking for an
17375      optional nested-name-specifier.  */
17376   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17377   nested_name_specifier
17378     = cp_parser_nested_name_specifier_opt (parser,
17379                                            /*typename_keyword_p=*/false,
17380                                            /*check_dependency_p=*/false,
17381                                            /*type_p=*/false,
17382                                            /*is_declaration=*/false);
17383   /* If there was a nested-name-specifier, then there *must* be an
17384      identifier.  */
17385   if (nested_name_specifier)
17386     {
17387       type_start_token = cp_lexer_peek_token (parser->lexer);
17388       /* Although the grammar says `identifier', it really means
17389          `class-name' or `template-name'.  You are only allowed to
17390          define a class that has already been declared with this
17391          syntax.
17392
17393          The proposed resolution for Core Issue 180 says that wherever
17394          you see `class T::X' you should treat `X' as a type-name.
17395
17396          It is OK to define an inaccessible class; for example:
17397
17398            class A { class B; };
17399            class A::B {};
17400
17401          We do not know if we will see a class-name, or a
17402          template-name.  We look for a class-name first, in case the
17403          class-name is a template-id; if we looked for the
17404          template-name first we would stop after the template-name.  */
17405       cp_parser_parse_tentatively (parser);
17406       type = cp_parser_class_name (parser,
17407                                    /*typename_keyword_p=*/false,
17408                                    /*template_keyword_p=*/false,
17409                                    class_type,
17410                                    /*check_dependency_p=*/false,
17411                                    /*class_head_p=*/true,
17412                                    /*is_declaration=*/false);
17413       /* If that didn't work, ignore the nested-name-specifier.  */
17414       if (!cp_parser_parse_definitely (parser))
17415         {
17416           invalid_nested_name_p = true;
17417           type_start_token = cp_lexer_peek_token (parser->lexer);
17418           id = cp_parser_identifier (parser);
17419           if (id == error_mark_node)
17420             id = NULL_TREE;
17421         }
17422       /* If we could not find a corresponding TYPE, treat this
17423          declaration like an unqualified declaration.  */
17424       if (type == error_mark_node)
17425         nested_name_specifier = NULL_TREE;
17426       /* Otherwise, count the number of templates used in TYPE and its
17427          containing scopes.  */
17428       else
17429         {
17430           tree scope;
17431
17432           for (scope = TREE_TYPE (type);
17433                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17434                scope = (TYPE_P (scope)
17435                         ? TYPE_CONTEXT (scope)
17436                         : DECL_CONTEXT (scope)))
17437             if (TYPE_P (scope)
17438                 && CLASS_TYPE_P (scope)
17439                 && CLASSTYPE_TEMPLATE_INFO (scope)
17440                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17441                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17442               ++num_templates;
17443         }
17444     }
17445   /* Otherwise, the identifier is optional.  */
17446   else
17447     {
17448       /* We don't know whether what comes next is a template-id,
17449          an identifier, or nothing at all.  */
17450       cp_parser_parse_tentatively (parser);
17451       /* Check for a template-id.  */
17452       type_start_token = cp_lexer_peek_token (parser->lexer);
17453       id = cp_parser_template_id (parser,
17454                                   /*template_keyword_p=*/false,
17455                                   /*check_dependency_p=*/true,
17456                                   /*is_declaration=*/true);
17457       /* If that didn't work, it could still be an identifier.  */
17458       if (!cp_parser_parse_definitely (parser))
17459         {
17460           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17461             {
17462               type_start_token = cp_lexer_peek_token (parser->lexer);
17463               id = cp_parser_identifier (parser);
17464             }
17465           else
17466             id = NULL_TREE;
17467         }
17468       else
17469         {
17470           template_id_p = true;
17471           ++num_templates;
17472         }
17473     }
17474
17475   pop_deferring_access_checks ();
17476
17477   if (id)
17478     {
17479       cp_parser_check_for_invalid_template_id (parser, id,
17480                                                type_start_token->location);
17481       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17482     }
17483
17484   /* If it's not a `:' or a `{' then we can't really be looking at a
17485      class-head, since a class-head only appears as part of a
17486      class-specifier.  We have to detect this situation before calling
17487      xref_tag, since that has irreversible side-effects.  */
17488   if (!cp_parser_next_token_starts_class_definition_p (parser))
17489     {
17490       cp_parser_error (parser, "expected %<{%> or %<:%>");
17491       type = error_mark_node;
17492       goto out;
17493     }
17494
17495   /* At this point, we're going ahead with the class-specifier, even
17496      if some other problem occurs.  */
17497   cp_parser_commit_to_tentative_parse (parser);
17498   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17499     {
17500       cp_parser_error (parser,
17501                        "cannot specify %<override%> for a class");
17502       type = error_mark_node;
17503       goto out;
17504     }
17505   /* Issue the error about the overly-qualified name now.  */
17506   if (qualified_p)
17507     {
17508       cp_parser_error (parser,
17509                        "global qualification of class name is invalid");
17510       type = error_mark_node;
17511       goto out;
17512     }
17513   else if (invalid_nested_name_p)
17514     {
17515       cp_parser_error (parser,
17516                        "qualified name does not name a class");
17517       type = error_mark_node;
17518       goto out;
17519     }
17520   else if (nested_name_specifier)
17521     {
17522       tree scope;
17523
17524       /* Reject typedef-names in class heads.  */
17525       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17526         {
17527           error_at (type_start_token->location,
17528                     "invalid class name in declaration of %qD",
17529                     type);
17530           type = NULL_TREE;
17531           goto done;
17532         }
17533
17534       /* Figure out in what scope the declaration is being placed.  */
17535       scope = current_scope ();
17536       /* If that scope does not contain the scope in which the
17537          class was originally declared, the program is invalid.  */
17538       if (scope && !is_ancestor (scope, nested_name_specifier))
17539         {
17540           if (at_namespace_scope_p ())
17541             error_at (type_start_token->location,
17542                       "declaration of %qD in namespace %qD which does not "
17543                       "enclose %qD",
17544                       type, scope, nested_name_specifier);
17545           else
17546             error_at (type_start_token->location,
17547                       "declaration of %qD in %qD which does not enclose %qD",
17548                       type, scope, nested_name_specifier);
17549           type = NULL_TREE;
17550           goto done;
17551         }
17552       /* [dcl.meaning]
17553
17554          A declarator-id shall not be qualified except for the
17555          definition of a ... nested class outside of its class
17556          ... [or] the definition or explicit instantiation of a
17557          class member of a namespace outside of its namespace.  */
17558       if (scope == nested_name_specifier)
17559         {
17560           permerror (nested_name_specifier_token_start->location,
17561                      "extra qualification not allowed");
17562           nested_name_specifier = NULL_TREE;
17563           num_templates = 0;
17564         }
17565     }
17566   /* An explicit-specialization must be preceded by "template <>".  If
17567      it is not, try to recover gracefully.  */
17568   if (at_namespace_scope_p ()
17569       && parser->num_template_parameter_lists == 0
17570       && template_id_p)
17571     {
17572       error_at (type_start_token->location,
17573                 "an explicit specialization must be preceded by %<template <>%>");
17574       invalid_explicit_specialization_p = true;
17575       /* Take the same action that would have been taken by
17576          cp_parser_explicit_specialization.  */
17577       ++parser->num_template_parameter_lists;
17578       begin_specialization ();
17579     }
17580   /* There must be no "return" statements between this point and the
17581      end of this function; set "type "to the correct return value and
17582      use "goto done;" to return.  */
17583   /* Make sure that the right number of template parameters were
17584      present.  */
17585   if (!cp_parser_check_template_parameters (parser, num_templates,
17586                                             type_start_token->location,
17587                                             /*declarator=*/NULL))
17588     {
17589       /* If something went wrong, there is no point in even trying to
17590          process the class-definition.  */
17591       type = NULL_TREE;
17592       goto done;
17593     }
17594
17595   /* Look up the type.  */
17596   if (template_id_p)
17597     {
17598       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17599           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17600               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17601         {
17602           error_at (type_start_token->location,
17603                     "function template %qD redeclared as a class template", id);
17604           type = error_mark_node;
17605         }
17606       else
17607         {
17608           type = TREE_TYPE (id);
17609           type = maybe_process_partial_specialization (type);
17610         }
17611       if (nested_name_specifier)
17612         pushed_scope = push_scope (nested_name_specifier);
17613     }
17614   else if (nested_name_specifier)
17615     {
17616       tree class_type;
17617
17618       /* Given:
17619
17620             template <typename T> struct S { struct T };
17621             template <typename T> struct S<T>::T { };
17622
17623          we will get a TYPENAME_TYPE when processing the definition of
17624          `S::T'.  We need to resolve it to the actual type before we
17625          try to define it.  */
17626       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17627         {
17628           class_type = resolve_typename_type (TREE_TYPE (type),
17629                                               /*only_current_p=*/false);
17630           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17631             type = TYPE_NAME (class_type);
17632           else
17633             {
17634               cp_parser_error (parser, "could not resolve typename type");
17635               type = error_mark_node;
17636             }
17637         }
17638
17639       if (maybe_process_partial_specialization (TREE_TYPE (type))
17640           == error_mark_node)
17641         {
17642           type = NULL_TREE;
17643           goto done;
17644         }
17645
17646       class_type = current_class_type;
17647       /* Enter the scope indicated by the nested-name-specifier.  */
17648       pushed_scope = push_scope (nested_name_specifier);
17649       /* Get the canonical version of this type.  */
17650       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17651       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17652           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17653         {
17654           type = push_template_decl (type);
17655           if (type == error_mark_node)
17656             {
17657               type = NULL_TREE;
17658               goto done;
17659             }
17660         }
17661
17662       type = TREE_TYPE (type);
17663       *nested_name_specifier_p = true;
17664     }
17665   else      /* The name is not a nested name.  */
17666     {
17667       /* If the class was unnamed, create a dummy name.  */
17668       if (!id)
17669         id = make_anon_name ();
17670       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17671                        parser->num_template_parameter_lists);
17672     }
17673
17674   /* Indicate whether this class was declared as a `class' or as a
17675      `struct'.  */
17676   if (TREE_CODE (type) == RECORD_TYPE)
17677     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17678   cp_parser_check_class_key (class_key, type);
17679
17680   /* If this type was already complete, and we see another definition,
17681      that's an error.  */
17682   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17683     {
17684       error_at (type_start_token->location, "redefinition of %q#T",
17685                 type);
17686       error_at (type_start_token->location, "previous definition of %q+#T",
17687                 type);
17688       type = NULL_TREE;
17689       goto done;
17690     }
17691   else if (type == error_mark_node)
17692     type = NULL_TREE;
17693
17694   /* We will have entered the scope containing the class; the names of
17695      base classes should be looked up in that context.  For example:
17696
17697        struct A { struct B {}; struct C; };
17698        struct A::C : B {};
17699
17700      is valid.  */
17701
17702   /* Get the list of base-classes, if there is one.  */
17703   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17704     *bases = cp_parser_base_clause (parser);
17705
17706  done:
17707   /* Leave the scope given by the nested-name-specifier.  We will
17708      enter the class scope itself while processing the members.  */
17709   if (pushed_scope)
17710     pop_scope (pushed_scope);
17711
17712   if (invalid_explicit_specialization_p)
17713     {
17714       end_specialization ();
17715       --parser->num_template_parameter_lists;
17716     }
17717
17718   if (type)
17719     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17720   *attributes_p = attributes;
17721   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17722     CLASSTYPE_FINAL (type) = 1;
17723  out:
17724   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17725   return type;
17726 }
17727
17728 /* Parse a class-key.
17729
17730    class-key:
17731      class
17732      struct
17733      union
17734
17735    Returns the kind of class-key specified, or none_type to indicate
17736    error.  */
17737
17738 static enum tag_types
17739 cp_parser_class_key (cp_parser* parser)
17740 {
17741   cp_token *token;
17742   enum tag_types tag_type;
17743
17744   /* Look for the class-key.  */
17745   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17746   if (!token)
17747     return none_type;
17748
17749   /* Check to see if the TOKEN is a class-key.  */
17750   tag_type = cp_parser_token_is_class_key (token);
17751   if (!tag_type)
17752     cp_parser_error (parser, "expected class-key");
17753   return tag_type;
17754 }
17755
17756 /* Parse an (optional) member-specification.
17757
17758    member-specification:
17759      member-declaration member-specification [opt]
17760      access-specifier : member-specification [opt]  */
17761
17762 static void
17763 cp_parser_member_specification_opt (cp_parser* parser)
17764 {
17765   while (true)
17766     {
17767       cp_token *token;
17768       enum rid keyword;
17769
17770       /* Peek at the next token.  */
17771       token = cp_lexer_peek_token (parser->lexer);
17772       /* If it's a `}', or EOF then we've seen all the members.  */
17773       if (token->type == CPP_CLOSE_BRACE
17774           || token->type == CPP_EOF
17775           || token->type == CPP_PRAGMA_EOL)
17776         break;
17777
17778       /* See if this token is a keyword.  */
17779       keyword = token->keyword;
17780       switch (keyword)
17781         {
17782         case RID_PUBLIC:
17783         case RID_PROTECTED:
17784         case RID_PRIVATE:
17785           /* Consume the access-specifier.  */
17786           cp_lexer_consume_token (parser->lexer);
17787           /* Remember which access-specifier is active.  */
17788           current_access_specifier = token->u.value;
17789           /* Look for the `:'.  */
17790           cp_parser_require (parser, CPP_COLON, RT_COLON);
17791           break;
17792
17793         default:
17794           /* Accept #pragmas at class scope.  */
17795           if (token->type == CPP_PRAGMA)
17796             {
17797               cp_parser_pragma (parser, pragma_external);
17798               break;
17799             }
17800
17801           /* Otherwise, the next construction must be a
17802              member-declaration.  */
17803           cp_parser_member_declaration (parser);
17804         }
17805     }
17806 }
17807
17808 /* Parse a member-declaration.
17809
17810    member-declaration:
17811      decl-specifier-seq [opt] member-declarator-list [opt] ;
17812      function-definition ; [opt]
17813      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17814      using-declaration
17815      template-declaration
17816
17817    member-declarator-list:
17818      member-declarator
17819      member-declarator-list , member-declarator
17820
17821    member-declarator:
17822      declarator pure-specifier [opt]
17823      declarator constant-initializer [opt]
17824      identifier [opt] : constant-expression
17825
17826    GNU Extensions:
17827
17828    member-declaration:
17829      __extension__ member-declaration
17830
17831    member-declarator:
17832      declarator attributes [opt] pure-specifier [opt]
17833      declarator attributes [opt] constant-initializer [opt]
17834      identifier [opt] attributes [opt] : constant-expression  
17835
17836    C++0x Extensions:
17837
17838    member-declaration:
17839      static_assert-declaration  */
17840
17841 static void
17842 cp_parser_member_declaration (cp_parser* parser)
17843 {
17844   cp_decl_specifier_seq decl_specifiers;
17845   tree prefix_attributes;
17846   tree decl;
17847   int declares_class_or_enum;
17848   bool friend_p;
17849   cp_token *token = NULL;
17850   cp_token *decl_spec_token_start = NULL;
17851   cp_token *initializer_token_start = NULL;
17852   int saved_pedantic;
17853   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17854
17855   /* Check for the `__extension__' keyword.  */
17856   if (cp_parser_extension_opt (parser, &saved_pedantic))
17857     {
17858       /* Recurse.  */
17859       cp_parser_member_declaration (parser);
17860       /* Restore the old value of the PEDANTIC flag.  */
17861       pedantic = saved_pedantic;
17862
17863       return;
17864     }
17865
17866   /* Check for a template-declaration.  */
17867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17868     {
17869       /* An explicit specialization here is an error condition, and we
17870          expect the specialization handler to detect and report this.  */
17871       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17872           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17873         cp_parser_explicit_specialization (parser);
17874       else
17875         cp_parser_template_declaration (parser, /*member_p=*/true);
17876
17877       return;
17878     }
17879
17880   /* Check for a using-declaration.  */
17881   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17882     {
17883       /* Parse the using-declaration.  */
17884       cp_parser_using_declaration (parser,
17885                                    /*access_declaration_p=*/false);
17886       return;
17887     }
17888
17889   /* Check for @defs.  */
17890   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17891     {
17892       tree ivar, member;
17893       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17894       ivar = ivar_chains;
17895       while (ivar)
17896         {
17897           member = ivar;
17898           ivar = TREE_CHAIN (member);
17899           TREE_CHAIN (member) = NULL_TREE;
17900           finish_member_declaration (member);
17901         }
17902       return;
17903     }
17904
17905   /* If the next token is `static_assert' we have a static assertion.  */
17906   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17907     {
17908       cp_parser_static_assert (parser, /*member_p=*/true);
17909       return;
17910     }
17911
17912   parser->colon_corrects_to_scope_p = false;
17913
17914   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17915     goto out;
17916
17917   /* Parse the decl-specifier-seq.  */
17918   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17919   cp_parser_decl_specifier_seq (parser,
17920                                 CP_PARSER_FLAGS_OPTIONAL,
17921                                 &decl_specifiers,
17922                                 &declares_class_or_enum);
17923   prefix_attributes = decl_specifiers.attributes;
17924   decl_specifiers.attributes = NULL_TREE;
17925   /* Check for an invalid type-name.  */
17926   if (!decl_specifiers.any_type_specifiers_p
17927       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17928     goto out;
17929   /* If there is no declarator, then the decl-specifier-seq should
17930      specify a type.  */
17931   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17932     {
17933       /* If there was no decl-specifier-seq, and the next token is a
17934          `;', then we have something like:
17935
17936            struct S { ; };
17937
17938          [class.mem]
17939
17940          Each member-declaration shall declare at least one member
17941          name of the class.  */
17942       if (!decl_specifiers.any_specifiers_p)
17943         {
17944           cp_token *token = cp_lexer_peek_token (parser->lexer);
17945           if (!in_system_header_at (token->location))
17946             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17947         }
17948       else
17949         {
17950           tree type;
17951
17952           /* See if this declaration is a friend.  */
17953           friend_p = cp_parser_friend_p (&decl_specifiers);
17954           /* If there were decl-specifiers, check to see if there was
17955              a class-declaration.  */
17956           type = check_tag_decl (&decl_specifiers);
17957           /* Nested classes have already been added to the class, but
17958              a `friend' needs to be explicitly registered.  */
17959           if (friend_p)
17960             {
17961               /* If the `friend' keyword was present, the friend must
17962                  be introduced with a class-key.  */
17963                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17964                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17965                           "in C++03 a class-key must be used "
17966                           "when declaring a friend");
17967                /* In this case:
17968
17969                     template <typename T> struct A {
17970                       friend struct A<T>::B;
17971                     };
17972
17973                   A<T>::B will be represented by a TYPENAME_TYPE, and
17974                   therefore not recognized by check_tag_decl.  */
17975                if (!type)
17976                  {
17977                    type = decl_specifiers.type;
17978                    if (type && TREE_CODE (type) == TYPE_DECL)
17979                      type = TREE_TYPE (type);
17980                  }
17981                if (!type || !TYPE_P (type))
17982                  error_at (decl_spec_token_start->location,
17983                            "friend declaration does not name a class or "
17984                            "function");
17985                else
17986                  make_friend_class (current_class_type, type,
17987                                     /*complain=*/true);
17988             }
17989           /* If there is no TYPE, an error message will already have
17990              been issued.  */
17991           else if (!type || type == error_mark_node)
17992             ;
17993           /* An anonymous aggregate has to be handled specially; such
17994              a declaration really declares a data member (with a
17995              particular type), as opposed to a nested class.  */
17996           else if (ANON_AGGR_TYPE_P (type))
17997             {
17998               /* Remove constructors and such from TYPE, now that we
17999                  know it is an anonymous aggregate.  */
18000               fixup_anonymous_aggr (type);
18001               /* And make the corresponding data member.  */
18002               decl = build_decl (decl_spec_token_start->location,
18003                                  FIELD_DECL, NULL_TREE, type);
18004               /* Add it to the class.  */
18005               finish_member_declaration (decl);
18006             }
18007           else
18008             cp_parser_check_access_in_redeclaration
18009                                               (TYPE_NAME (type),
18010                                                decl_spec_token_start->location);
18011         }
18012     }
18013   else
18014     {
18015       bool assume_semicolon = false;
18016
18017       /* See if these declarations will be friends.  */
18018       friend_p = cp_parser_friend_p (&decl_specifiers);
18019
18020       /* Keep going until we hit the `;' at the end of the
18021          declaration.  */
18022       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18023         {
18024           tree attributes = NULL_TREE;
18025           tree first_attribute;
18026
18027           /* Peek at the next token.  */
18028           token = cp_lexer_peek_token (parser->lexer);
18029
18030           /* Check for a bitfield declaration.  */
18031           if (token->type == CPP_COLON
18032               || (token->type == CPP_NAME
18033                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18034                   == CPP_COLON))
18035             {
18036               tree identifier;
18037               tree width;
18038
18039               /* Get the name of the bitfield.  Note that we cannot just
18040                  check TOKEN here because it may have been invalidated by
18041                  the call to cp_lexer_peek_nth_token above.  */
18042               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18043                 identifier = cp_parser_identifier (parser);
18044               else
18045                 identifier = NULL_TREE;
18046
18047               /* Consume the `:' token.  */
18048               cp_lexer_consume_token (parser->lexer);
18049               /* Get the width of the bitfield.  */
18050               width
18051                 = cp_parser_constant_expression (parser,
18052                                                  /*allow_non_constant=*/false,
18053                                                  NULL);
18054
18055               /* Look for attributes that apply to the bitfield.  */
18056               attributes = cp_parser_attributes_opt (parser);
18057               /* Remember which attributes are prefix attributes and
18058                  which are not.  */
18059               first_attribute = attributes;
18060               /* Combine the attributes.  */
18061               attributes = chainon (prefix_attributes, attributes);
18062
18063               /* Create the bitfield declaration.  */
18064               decl = grokbitfield (identifier
18065                                    ? make_id_declarator (NULL_TREE,
18066                                                          identifier,
18067                                                          sfk_none)
18068                                    : NULL,
18069                                    &decl_specifiers,
18070                                    width,
18071                                    attributes);
18072             }
18073           else
18074             {
18075               cp_declarator *declarator;
18076               tree initializer;
18077               tree asm_specification;
18078               int ctor_dtor_or_conv_p;
18079
18080               /* Parse the declarator.  */
18081               declarator
18082                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18083                                         &ctor_dtor_or_conv_p,
18084                                         /*parenthesized_p=*/NULL,
18085                                         /*member_p=*/true);
18086
18087               /* If something went wrong parsing the declarator, make sure
18088                  that we at least consume some tokens.  */
18089               if (declarator == cp_error_declarator)
18090                 {
18091                   /* Skip to the end of the statement.  */
18092                   cp_parser_skip_to_end_of_statement (parser);
18093                   /* If the next token is not a semicolon, that is
18094                      probably because we just skipped over the body of
18095                      a function.  So, we consume a semicolon if
18096                      present, but do not issue an error message if it
18097                      is not present.  */
18098                   if (cp_lexer_next_token_is (parser->lexer,
18099                                               CPP_SEMICOLON))
18100                     cp_lexer_consume_token (parser->lexer);
18101                   goto out;
18102                 }
18103
18104               if (declares_class_or_enum & 2)
18105                 cp_parser_check_for_definition_in_return_type
18106                                             (declarator, decl_specifiers.type,
18107                                              decl_specifiers.type_location);
18108
18109               /* Look for an asm-specification.  */
18110               asm_specification = cp_parser_asm_specification_opt (parser);
18111               /* Look for attributes that apply to the declaration.  */
18112               attributes = cp_parser_attributes_opt (parser);
18113               /* Remember which attributes are prefix attributes and
18114                  which are not.  */
18115               first_attribute = attributes;
18116               /* Combine the attributes.  */
18117               attributes = chainon (prefix_attributes, attributes);
18118
18119               /* If it's an `=', then we have a constant-initializer or a
18120                  pure-specifier.  It is not correct to parse the
18121                  initializer before registering the member declaration
18122                  since the member declaration should be in scope while
18123                  its initializer is processed.  However, the rest of the
18124                  front end does not yet provide an interface that allows
18125                  us to handle this correctly.  */
18126               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18127                 {
18128                   /* In [class.mem]:
18129
18130                      A pure-specifier shall be used only in the declaration of
18131                      a virtual function.
18132
18133                      A member-declarator can contain a constant-initializer
18134                      only if it declares a static member of integral or
18135                      enumeration type.
18136
18137                      Therefore, if the DECLARATOR is for a function, we look
18138                      for a pure-specifier; otherwise, we look for a
18139                      constant-initializer.  When we call `grokfield', it will
18140                      perform more stringent semantics checks.  */
18141                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18142                   if (function_declarator_p (declarator))
18143                     initializer = cp_parser_pure_specifier (parser);
18144                   else
18145                     /* Parse the initializer.  */
18146                     initializer = cp_parser_constant_initializer (parser);
18147                 }
18148               /* Otherwise, there is no initializer.  */
18149               else
18150                 initializer = NULL_TREE;
18151
18152               /* See if we are probably looking at a function
18153                  definition.  We are certainly not looking at a
18154                  member-declarator.  Calling `grokfield' has
18155                  side-effects, so we must not do it unless we are sure
18156                  that we are looking at a member-declarator.  */
18157               if (cp_parser_token_starts_function_definition_p
18158                   (cp_lexer_peek_token (parser->lexer)))
18159                 {
18160                   /* The grammar does not allow a pure-specifier to be
18161                      used when a member function is defined.  (It is
18162                      possible that this fact is an oversight in the
18163                      standard, since a pure function may be defined
18164                      outside of the class-specifier.  */
18165                   if (initializer)
18166                     error_at (initializer_token_start->location,
18167                               "pure-specifier on function-definition");
18168                   decl = cp_parser_save_member_function_body (parser,
18169                                                               &decl_specifiers,
18170                                                               declarator,
18171                                                               attributes);
18172                   /* If the member was not a friend, declare it here.  */
18173                   if (!friend_p)
18174                     finish_member_declaration (decl);
18175                   /* Peek at the next token.  */
18176                   token = cp_lexer_peek_token (parser->lexer);
18177                   /* If the next token is a semicolon, consume it.  */
18178                   if (token->type == CPP_SEMICOLON)
18179                     cp_lexer_consume_token (parser->lexer);
18180                   goto out;
18181                 }
18182               else
18183                 if (declarator->kind == cdk_function)
18184                   declarator->id_loc = token->location;
18185                 /* Create the declaration.  */
18186                 decl = grokfield (declarator, &decl_specifiers,
18187                                   initializer, /*init_const_expr_p=*/true,
18188                                   asm_specification,
18189                                   attributes);
18190             }
18191
18192           /* Reset PREFIX_ATTRIBUTES.  */
18193           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18194             attributes = TREE_CHAIN (attributes);
18195           if (attributes)
18196             TREE_CHAIN (attributes) = NULL_TREE;
18197
18198           /* If there is any qualification still in effect, clear it
18199              now; we will be starting fresh with the next declarator.  */
18200           parser->scope = NULL_TREE;
18201           parser->qualifying_scope = NULL_TREE;
18202           parser->object_scope = NULL_TREE;
18203           /* If it's a `,', then there are more declarators.  */
18204           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18205             cp_lexer_consume_token (parser->lexer);
18206           /* If the next token isn't a `;', then we have a parse error.  */
18207           else if (cp_lexer_next_token_is_not (parser->lexer,
18208                                                CPP_SEMICOLON))
18209             {
18210               /* The next token might be a ways away from where the
18211                  actual semicolon is missing.  Find the previous token
18212                  and use that for our error position.  */
18213               cp_token *token = cp_lexer_previous_token (parser->lexer);
18214               error_at (token->location,
18215                         "expected %<;%> at end of member declaration");
18216
18217               /* Assume that the user meant to provide a semicolon.  If
18218                  we were to cp_parser_skip_to_end_of_statement, we might
18219                  skip to a semicolon inside a member function definition
18220                  and issue nonsensical error messages.  */
18221               assume_semicolon = true;
18222             }
18223
18224           if (decl)
18225             {
18226               /* Add DECL to the list of members.  */
18227               if (!friend_p)
18228                 finish_member_declaration (decl);
18229
18230               if (TREE_CODE (decl) == FUNCTION_DECL)
18231                 cp_parser_save_default_args (parser, decl);
18232             }
18233
18234           if (assume_semicolon)
18235             goto out;
18236         }
18237     }
18238
18239   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18240  out:
18241   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18242 }
18243
18244 /* Parse a pure-specifier.
18245
18246    pure-specifier:
18247      = 0
18248
18249    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18250    Otherwise, ERROR_MARK_NODE is returned.  */
18251
18252 static tree
18253 cp_parser_pure_specifier (cp_parser* parser)
18254 {
18255   cp_token *token;
18256
18257   /* Look for the `=' token.  */
18258   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18259     return error_mark_node;
18260   /* Look for the `0' token.  */
18261   token = cp_lexer_peek_token (parser->lexer);
18262
18263   if (token->type == CPP_EOF
18264       || token->type == CPP_PRAGMA_EOL)
18265     return error_mark_node;
18266
18267   cp_lexer_consume_token (parser->lexer);
18268
18269   /* Accept = default or = delete in c++0x mode.  */
18270   if (token->keyword == RID_DEFAULT
18271       || token->keyword == RID_DELETE)
18272     {
18273       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18274       return token->u.value;
18275     }
18276
18277   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18278   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18279     {
18280       cp_parser_error (parser,
18281                        "invalid pure specifier (only %<= 0%> is allowed)");
18282       cp_parser_skip_to_end_of_statement (parser);
18283       return error_mark_node;
18284     }
18285   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18286     {
18287       error_at (token->location, "templates may not be %<virtual%>");
18288       return error_mark_node;
18289     }
18290
18291   return integer_zero_node;
18292 }
18293
18294 /* Parse a constant-initializer.
18295
18296    constant-initializer:
18297      = constant-expression
18298
18299    Returns a representation of the constant-expression.  */
18300
18301 static tree
18302 cp_parser_constant_initializer (cp_parser* parser)
18303 {
18304   /* Look for the `=' token.  */
18305   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18306     return error_mark_node;
18307
18308   /* It is invalid to write:
18309
18310        struct S { static const int i = { 7 }; };
18311
18312      */
18313   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18314     {
18315       cp_parser_error (parser,
18316                        "a brace-enclosed initializer is not allowed here");
18317       /* Consume the opening brace.  */
18318       cp_lexer_consume_token (parser->lexer);
18319       /* Skip the initializer.  */
18320       cp_parser_skip_to_closing_brace (parser);
18321       /* Look for the trailing `}'.  */
18322       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18323
18324       return error_mark_node;
18325     }
18326
18327   return cp_parser_constant_expression (parser,
18328                                         /*allow_non_constant=*/false,
18329                                         NULL);
18330 }
18331
18332 /* Derived classes [gram.class.derived] */
18333
18334 /* Parse a base-clause.
18335
18336    base-clause:
18337      : base-specifier-list
18338
18339    base-specifier-list:
18340      base-specifier ... [opt]
18341      base-specifier-list , base-specifier ... [opt]
18342
18343    Returns a TREE_LIST representing the base-classes, in the order in
18344    which they were declared.  The representation of each node is as
18345    described by cp_parser_base_specifier.
18346
18347    In the case that no bases are specified, this function will return
18348    NULL_TREE, not ERROR_MARK_NODE.  */
18349
18350 static tree
18351 cp_parser_base_clause (cp_parser* parser)
18352 {
18353   tree bases = NULL_TREE;
18354
18355   /* Look for the `:' that begins the list.  */
18356   cp_parser_require (parser, CPP_COLON, RT_COLON);
18357
18358   /* Scan the base-specifier-list.  */
18359   while (true)
18360     {
18361       cp_token *token;
18362       tree base;
18363       bool pack_expansion_p = false;
18364
18365       /* Look for the base-specifier.  */
18366       base = cp_parser_base_specifier (parser);
18367       /* Look for the (optional) ellipsis. */
18368       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18369         {
18370           /* Consume the `...'. */
18371           cp_lexer_consume_token (parser->lexer);
18372
18373           pack_expansion_p = true;
18374         }
18375
18376       /* Add BASE to the front of the list.  */
18377       if (base && base != error_mark_node)
18378         {
18379           if (pack_expansion_p)
18380             /* Make this a pack expansion type. */
18381             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18382
18383           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18384             {
18385               TREE_CHAIN (base) = bases;
18386               bases = base;
18387             }
18388         }
18389       /* Peek at the next token.  */
18390       token = cp_lexer_peek_token (parser->lexer);
18391       /* If it's not a comma, then the list is complete.  */
18392       if (token->type != CPP_COMMA)
18393         break;
18394       /* Consume the `,'.  */
18395       cp_lexer_consume_token (parser->lexer);
18396     }
18397
18398   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18399      base class had a qualified name.  However, the next name that
18400      appears is certainly not qualified.  */
18401   parser->scope = NULL_TREE;
18402   parser->qualifying_scope = NULL_TREE;
18403   parser->object_scope = NULL_TREE;
18404
18405   return nreverse (bases);
18406 }
18407
18408 /* Parse a base-specifier.
18409
18410    base-specifier:
18411      :: [opt] nested-name-specifier [opt] class-name
18412      virtual access-specifier [opt] :: [opt] nested-name-specifier
18413        [opt] class-name
18414      access-specifier virtual [opt] :: [opt] nested-name-specifier
18415        [opt] class-name
18416
18417    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18418    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18419    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18420    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18421
18422 static tree
18423 cp_parser_base_specifier (cp_parser* parser)
18424 {
18425   cp_token *token;
18426   bool done = false;
18427   bool virtual_p = false;
18428   bool duplicate_virtual_error_issued_p = false;
18429   bool duplicate_access_error_issued_p = false;
18430   bool class_scope_p, template_p;
18431   tree access = access_default_node;
18432   tree type;
18433
18434   /* Process the optional `virtual' and `access-specifier'.  */
18435   while (!done)
18436     {
18437       /* Peek at the next token.  */
18438       token = cp_lexer_peek_token (parser->lexer);
18439       /* Process `virtual'.  */
18440       switch (token->keyword)
18441         {
18442         case RID_VIRTUAL:
18443           /* If `virtual' appears more than once, issue an error.  */
18444           if (virtual_p && !duplicate_virtual_error_issued_p)
18445             {
18446               cp_parser_error (parser,
18447                                "%<virtual%> specified more than once in base-specified");
18448               duplicate_virtual_error_issued_p = true;
18449             }
18450
18451           virtual_p = true;
18452
18453           /* Consume the `virtual' token.  */
18454           cp_lexer_consume_token (parser->lexer);
18455
18456           break;
18457
18458         case RID_PUBLIC:
18459         case RID_PROTECTED:
18460         case RID_PRIVATE:
18461           /* If more than one access specifier appears, issue an
18462              error.  */
18463           if (access != access_default_node
18464               && !duplicate_access_error_issued_p)
18465             {
18466               cp_parser_error (parser,
18467                                "more than one access specifier in base-specified");
18468               duplicate_access_error_issued_p = true;
18469             }
18470
18471           access = ridpointers[(int) token->keyword];
18472
18473           /* Consume the access-specifier.  */
18474           cp_lexer_consume_token (parser->lexer);
18475
18476           break;
18477
18478         default:
18479           done = true;
18480           break;
18481         }
18482     }
18483   /* It is not uncommon to see programs mechanically, erroneously, use
18484      the 'typename' keyword to denote (dependent) qualified types
18485      as base classes.  */
18486   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18487     {
18488       token = cp_lexer_peek_token (parser->lexer);
18489       if (!processing_template_decl)
18490         error_at (token->location,
18491                   "keyword %<typename%> not allowed outside of templates");
18492       else
18493         error_at (token->location,
18494                   "keyword %<typename%> not allowed in this context "
18495                   "(the base class is implicitly a type)");
18496       cp_lexer_consume_token (parser->lexer);
18497     }
18498
18499   /* Look for the optional `::' operator.  */
18500   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18501   /* Look for the nested-name-specifier.  The simplest way to
18502      implement:
18503
18504        [temp.res]
18505
18506        The keyword `typename' is not permitted in a base-specifier or
18507        mem-initializer; in these contexts a qualified name that
18508        depends on a template-parameter is implicitly assumed to be a
18509        type name.
18510
18511      is to pretend that we have seen the `typename' keyword at this
18512      point.  */
18513   cp_parser_nested_name_specifier_opt (parser,
18514                                        /*typename_keyword_p=*/true,
18515                                        /*check_dependency_p=*/true,
18516                                        typename_type,
18517                                        /*is_declaration=*/true);
18518   /* If the base class is given by a qualified name, assume that names
18519      we see are type names or templates, as appropriate.  */
18520   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18521   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18522
18523   if (!parser->scope
18524       && cp_lexer_next_token_is_decltype (parser->lexer))
18525     /* DR 950 allows decltype as a base-specifier.  */
18526     type = cp_parser_decltype (parser);
18527   else
18528     {
18529       /* Otherwise, look for the class-name.  */
18530       type = cp_parser_class_name (parser,
18531                                    class_scope_p,
18532                                    template_p,
18533                                    typename_type,
18534                                    /*check_dependency_p=*/true,
18535                                    /*class_head_p=*/false,
18536                                    /*is_declaration=*/true);
18537       type = TREE_TYPE (type);
18538     }
18539
18540   if (type == error_mark_node)
18541     return error_mark_node;
18542
18543   return finish_base_specifier (type, access, virtual_p);
18544 }
18545
18546 /* Exception handling [gram.exception] */
18547
18548 /* Parse an (optional) exception-specification.
18549
18550    exception-specification:
18551      throw ( type-id-list [opt] )
18552
18553    Returns a TREE_LIST representing the exception-specification.  The
18554    TREE_VALUE of each node is a type.  */
18555
18556 static tree
18557 cp_parser_exception_specification_opt (cp_parser* parser)
18558 {
18559   cp_token *token;
18560   tree type_id_list;
18561   const char *saved_message;
18562
18563   /* Peek at the next token.  */
18564   token = cp_lexer_peek_token (parser->lexer);
18565
18566   /* Is it a noexcept-specification?  */
18567   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18568     {
18569       tree expr;
18570       cp_lexer_consume_token (parser->lexer);
18571
18572       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18573         {
18574           cp_lexer_consume_token (parser->lexer);
18575
18576           /* Types may not be defined in an exception-specification.  */
18577           saved_message = parser->type_definition_forbidden_message;
18578           parser->type_definition_forbidden_message
18579             = G_("types may not be defined in an exception-specification");
18580
18581           expr = cp_parser_constant_expression (parser, false, NULL);
18582
18583           /* Restore the saved message.  */
18584           parser->type_definition_forbidden_message = saved_message;
18585
18586           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18587         }
18588       else
18589         expr = boolean_true_node;
18590
18591       return build_noexcept_spec (expr, tf_warning_or_error);
18592     }
18593
18594   /* If it's not `throw', then there's no exception-specification.  */
18595   if (!cp_parser_is_keyword (token, RID_THROW))
18596     return NULL_TREE;
18597
18598 #if 0
18599   /* Enable this once a lot of code has transitioned to noexcept?  */
18600   if (cxx_dialect == cxx0x && !in_system_header)
18601     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18602              "deprecated in C++0x; use %<noexcept%> instead");
18603 #endif
18604
18605   /* Consume the `throw'.  */
18606   cp_lexer_consume_token (parser->lexer);
18607
18608   /* Look for the `('.  */
18609   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18610
18611   /* Peek at the next token.  */
18612   token = cp_lexer_peek_token (parser->lexer);
18613   /* If it's not a `)', then there is a type-id-list.  */
18614   if (token->type != CPP_CLOSE_PAREN)
18615     {
18616       /* Types may not be defined in an exception-specification.  */
18617       saved_message = parser->type_definition_forbidden_message;
18618       parser->type_definition_forbidden_message
18619         = G_("types may not be defined in an exception-specification");
18620       /* Parse the type-id-list.  */
18621       type_id_list = cp_parser_type_id_list (parser);
18622       /* Restore the saved message.  */
18623       parser->type_definition_forbidden_message = saved_message;
18624     }
18625   else
18626     type_id_list = empty_except_spec;
18627
18628   /* Look for the `)'.  */
18629   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18630
18631   return type_id_list;
18632 }
18633
18634 /* Parse an (optional) type-id-list.
18635
18636    type-id-list:
18637      type-id ... [opt]
18638      type-id-list , type-id ... [opt]
18639
18640    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18641    in the order that the types were presented.  */
18642
18643 static tree
18644 cp_parser_type_id_list (cp_parser* parser)
18645 {
18646   tree types = NULL_TREE;
18647
18648   while (true)
18649     {
18650       cp_token *token;
18651       tree type;
18652
18653       /* Get the next type-id.  */
18654       type = cp_parser_type_id (parser);
18655       /* Parse the optional ellipsis. */
18656       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18657         {
18658           /* Consume the `...'. */
18659           cp_lexer_consume_token (parser->lexer);
18660
18661           /* Turn the type into a pack expansion expression. */
18662           type = make_pack_expansion (type);
18663         }
18664       /* Add it to the list.  */
18665       types = add_exception_specifier (types, type, /*complain=*/1);
18666       /* Peek at the next token.  */
18667       token = cp_lexer_peek_token (parser->lexer);
18668       /* If it is not a `,', we are done.  */
18669       if (token->type != CPP_COMMA)
18670         break;
18671       /* Consume the `,'.  */
18672       cp_lexer_consume_token (parser->lexer);
18673     }
18674
18675   return nreverse (types);
18676 }
18677
18678 /* Parse a try-block.
18679
18680    try-block:
18681      try compound-statement handler-seq  */
18682
18683 static tree
18684 cp_parser_try_block (cp_parser* parser)
18685 {
18686   tree try_block;
18687
18688   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18689   try_block = begin_try_block ();
18690   cp_parser_compound_statement (parser, NULL, true, false);
18691   finish_try_block (try_block);
18692   cp_parser_handler_seq (parser);
18693   finish_handler_sequence (try_block);
18694
18695   return try_block;
18696 }
18697
18698 /* Parse a function-try-block.
18699
18700    function-try-block:
18701      try ctor-initializer [opt] function-body handler-seq  */
18702
18703 static bool
18704 cp_parser_function_try_block (cp_parser* parser)
18705 {
18706   tree compound_stmt;
18707   tree try_block;
18708   bool ctor_initializer_p;
18709
18710   /* Look for the `try' keyword.  */
18711   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18712     return false;
18713   /* Let the rest of the front end know where we are.  */
18714   try_block = begin_function_try_block (&compound_stmt);
18715   /* Parse the function-body.  */
18716   ctor_initializer_p
18717     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18718   /* We're done with the `try' part.  */
18719   finish_function_try_block (try_block);
18720   /* Parse the handlers.  */
18721   cp_parser_handler_seq (parser);
18722   /* We're done with the handlers.  */
18723   finish_function_handler_sequence (try_block, compound_stmt);
18724
18725   return ctor_initializer_p;
18726 }
18727
18728 /* Parse a handler-seq.
18729
18730    handler-seq:
18731      handler handler-seq [opt]  */
18732
18733 static void
18734 cp_parser_handler_seq (cp_parser* parser)
18735 {
18736   while (true)
18737     {
18738       cp_token *token;
18739
18740       /* Parse the handler.  */
18741       cp_parser_handler (parser);
18742       /* Peek at the next token.  */
18743       token = cp_lexer_peek_token (parser->lexer);
18744       /* If it's not `catch' then there are no more handlers.  */
18745       if (!cp_parser_is_keyword (token, RID_CATCH))
18746         break;
18747     }
18748 }
18749
18750 /* Parse a handler.
18751
18752    handler:
18753      catch ( exception-declaration ) compound-statement  */
18754
18755 static void
18756 cp_parser_handler (cp_parser* parser)
18757 {
18758   tree handler;
18759   tree declaration;
18760
18761   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18762   handler = begin_handler ();
18763   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18764   declaration = cp_parser_exception_declaration (parser);
18765   finish_handler_parms (declaration, handler);
18766   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18767   cp_parser_compound_statement (parser, NULL, false, false);
18768   finish_handler (handler);
18769 }
18770
18771 /* Parse an exception-declaration.
18772
18773    exception-declaration:
18774      type-specifier-seq declarator
18775      type-specifier-seq abstract-declarator
18776      type-specifier-seq
18777      ...
18778
18779    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18780    ellipsis variant is used.  */
18781
18782 static tree
18783 cp_parser_exception_declaration (cp_parser* parser)
18784 {
18785   cp_decl_specifier_seq type_specifiers;
18786   cp_declarator *declarator;
18787   const char *saved_message;
18788
18789   /* If it's an ellipsis, it's easy to handle.  */
18790   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18791     {
18792       /* Consume the `...' token.  */
18793       cp_lexer_consume_token (parser->lexer);
18794       return NULL_TREE;
18795     }
18796
18797   /* Types may not be defined in exception-declarations.  */
18798   saved_message = parser->type_definition_forbidden_message;
18799   parser->type_definition_forbidden_message
18800     = G_("types may not be defined in exception-declarations");
18801
18802   /* Parse the type-specifier-seq.  */
18803   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18804                                 /*is_trailing_return=*/false,
18805                                 &type_specifiers);
18806   /* If it's a `)', then there is no declarator.  */
18807   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18808     declarator = NULL;
18809   else
18810     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18811                                        /*ctor_dtor_or_conv_p=*/NULL,
18812                                        /*parenthesized_p=*/NULL,
18813                                        /*member_p=*/false);
18814
18815   /* Restore the saved message.  */
18816   parser->type_definition_forbidden_message = saved_message;
18817
18818   if (!type_specifiers.any_specifiers_p)
18819     return error_mark_node;
18820
18821   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18822 }
18823
18824 /* Parse a throw-expression.
18825
18826    throw-expression:
18827      throw assignment-expression [opt]
18828
18829    Returns a THROW_EXPR representing the throw-expression.  */
18830
18831 static tree
18832 cp_parser_throw_expression (cp_parser* parser)
18833 {
18834   tree expression;
18835   cp_token* token;
18836
18837   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18838   token = cp_lexer_peek_token (parser->lexer);
18839   /* Figure out whether or not there is an assignment-expression
18840      following the "throw" keyword.  */
18841   if (token->type == CPP_COMMA
18842       || token->type == CPP_SEMICOLON
18843       || token->type == CPP_CLOSE_PAREN
18844       || token->type == CPP_CLOSE_SQUARE
18845       || token->type == CPP_CLOSE_BRACE
18846       || token->type == CPP_COLON)
18847     expression = NULL_TREE;
18848   else
18849     expression = cp_parser_assignment_expression (parser,
18850                                                   /*cast_p=*/false, NULL);
18851
18852   return build_throw (expression);
18853 }
18854
18855 /* GNU Extensions */
18856
18857 /* Parse an (optional) asm-specification.
18858
18859    asm-specification:
18860      asm ( string-literal )
18861
18862    If the asm-specification is present, returns a STRING_CST
18863    corresponding to the string-literal.  Otherwise, returns
18864    NULL_TREE.  */
18865
18866 static tree
18867 cp_parser_asm_specification_opt (cp_parser* parser)
18868 {
18869   cp_token *token;
18870   tree asm_specification;
18871
18872   /* Peek at the next token.  */
18873   token = cp_lexer_peek_token (parser->lexer);
18874   /* If the next token isn't the `asm' keyword, then there's no
18875      asm-specification.  */
18876   if (!cp_parser_is_keyword (token, RID_ASM))
18877     return NULL_TREE;
18878
18879   /* Consume the `asm' token.  */
18880   cp_lexer_consume_token (parser->lexer);
18881   /* Look for the `('.  */
18882   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18883
18884   /* Look for the string-literal.  */
18885   asm_specification = cp_parser_string_literal (parser, false, false);
18886
18887   /* Look for the `)'.  */
18888   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18889
18890   return asm_specification;
18891 }
18892
18893 /* Parse an asm-operand-list.
18894
18895    asm-operand-list:
18896      asm-operand
18897      asm-operand-list , asm-operand
18898
18899    asm-operand:
18900      string-literal ( expression )
18901      [ string-literal ] string-literal ( expression )
18902
18903    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18904    each node is the expression.  The TREE_PURPOSE is itself a
18905    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18906    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18907    is a STRING_CST for the string literal before the parenthesis. Returns
18908    ERROR_MARK_NODE if any of the operands are invalid.  */
18909
18910 static tree
18911 cp_parser_asm_operand_list (cp_parser* parser)
18912 {
18913   tree asm_operands = NULL_TREE;
18914   bool invalid_operands = false;
18915
18916   while (true)
18917     {
18918       tree string_literal;
18919       tree expression;
18920       tree name;
18921
18922       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18923         {
18924           /* Consume the `[' token.  */
18925           cp_lexer_consume_token (parser->lexer);
18926           /* Read the operand name.  */
18927           name = cp_parser_identifier (parser);
18928           if (name != error_mark_node)
18929             name = build_string (IDENTIFIER_LENGTH (name),
18930                                  IDENTIFIER_POINTER (name));
18931           /* Look for the closing `]'.  */
18932           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18933         }
18934       else
18935         name = NULL_TREE;
18936       /* Look for the string-literal.  */
18937       string_literal = cp_parser_string_literal (parser, false, false);
18938
18939       /* Look for the `('.  */
18940       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18941       /* Parse the expression.  */
18942       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18943       /* Look for the `)'.  */
18944       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18945
18946       if (name == error_mark_node 
18947           || string_literal == error_mark_node 
18948           || expression == error_mark_node)
18949         invalid_operands = true;
18950
18951       /* Add this operand to the list.  */
18952       asm_operands = tree_cons (build_tree_list (name, string_literal),
18953                                 expression,
18954                                 asm_operands);
18955       /* If the next token is not a `,', there are no more
18956          operands.  */
18957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18958         break;
18959       /* Consume the `,'.  */
18960       cp_lexer_consume_token (parser->lexer);
18961     }
18962
18963   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18964 }
18965
18966 /* Parse an asm-clobber-list.
18967
18968    asm-clobber-list:
18969      string-literal
18970      asm-clobber-list , string-literal
18971
18972    Returns a TREE_LIST, indicating the clobbers in the order that they
18973    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18974
18975 static tree
18976 cp_parser_asm_clobber_list (cp_parser* parser)
18977 {
18978   tree clobbers = NULL_TREE;
18979
18980   while (true)
18981     {
18982       tree string_literal;
18983
18984       /* Look for the string literal.  */
18985       string_literal = cp_parser_string_literal (parser, false, false);
18986       /* Add it to the list.  */
18987       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18988       /* If the next token is not a `,', then the list is
18989          complete.  */
18990       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18991         break;
18992       /* Consume the `,' token.  */
18993       cp_lexer_consume_token (parser->lexer);
18994     }
18995
18996   return clobbers;
18997 }
18998
18999 /* Parse an asm-label-list.
19000
19001    asm-label-list:
19002      identifier
19003      asm-label-list , identifier
19004
19005    Returns a TREE_LIST, indicating the labels in the order that they
19006    appeared.  The TREE_VALUE of each node is a label.  */
19007
19008 static tree
19009 cp_parser_asm_label_list (cp_parser* parser)
19010 {
19011   tree labels = NULL_TREE;
19012
19013   while (true)
19014     {
19015       tree identifier, label, name;
19016
19017       /* Look for the identifier.  */
19018       identifier = cp_parser_identifier (parser);
19019       if (!error_operand_p (identifier))
19020         {
19021           label = lookup_label (identifier);
19022           if (TREE_CODE (label) == LABEL_DECL)
19023             {
19024               TREE_USED (label) = 1;
19025               check_goto (label);
19026               name = build_string (IDENTIFIER_LENGTH (identifier),
19027                                    IDENTIFIER_POINTER (identifier));
19028               labels = tree_cons (name, label, labels);
19029             }
19030         }
19031       /* If the next token is not a `,', then the list is
19032          complete.  */
19033       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19034         break;
19035       /* Consume the `,' token.  */
19036       cp_lexer_consume_token (parser->lexer);
19037     }
19038
19039   return nreverse (labels);
19040 }
19041
19042 /* Parse an (optional) series of attributes.
19043
19044    attributes:
19045      attributes attribute
19046
19047    attribute:
19048      __attribute__ (( attribute-list [opt] ))
19049
19050    The return value is as for cp_parser_attribute_list.  */
19051
19052 static tree
19053 cp_parser_attributes_opt (cp_parser* parser)
19054 {
19055   tree attributes = NULL_TREE;
19056
19057   while (true)
19058     {
19059       cp_token *token;
19060       tree attribute_list;
19061
19062       /* Peek at the next token.  */
19063       token = cp_lexer_peek_token (parser->lexer);
19064       /* If it's not `__attribute__', then we're done.  */
19065       if (token->keyword != RID_ATTRIBUTE)
19066         break;
19067
19068       /* Consume the `__attribute__' keyword.  */
19069       cp_lexer_consume_token (parser->lexer);
19070       /* Look for the two `(' tokens.  */
19071       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19072       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19073
19074       /* Peek at the next token.  */
19075       token = cp_lexer_peek_token (parser->lexer);
19076       if (token->type != CPP_CLOSE_PAREN)
19077         /* Parse the attribute-list.  */
19078         attribute_list = cp_parser_attribute_list (parser);
19079       else
19080         /* If the next token is a `)', then there is no attribute
19081            list.  */
19082         attribute_list = NULL;
19083
19084       /* Look for the two `)' tokens.  */
19085       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19086       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19087
19088       /* Add these new attributes to the list.  */
19089       attributes = chainon (attributes, attribute_list);
19090     }
19091
19092   return attributes;
19093 }
19094
19095 /* Parse an attribute-list.
19096
19097    attribute-list:
19098      attribute
19099      attribute-list , attribute
19100
19101    attribute:
19102      identifier
19103      identifier ( identifier )
19104      identifier ( identifier , expression-list )
19105      identifier ( expression-list )
19106
19107    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19108    to an attribute.  The TREE_PURPOSE of each node is the identifier
19109    indicating which attribute is in use.  The TREE_VALUE represents
19110    the arguments, if any.  */
19111
19112 static tree
19113 cp_parser_attribute_list (cp_parser* parser)
19114 {
19115   tree attribute_list = NULL_TREE;
19116   bool save_translate_strings_p = parser->translate_strings_p;
19117
19118   parser->translate_strings_p = false;
19119   while (true)
19120     {
19121       cp_token *token;
19122       tree identifier;
19123       tree attribute;
19124
19125       /* Look for the identifier.  We also allow keywords here; for
19126          example `__attribute__ ((const))' is legal.  */
19127       token = cp_lexer_peek_token (parser->lexer);
19128       if (token->type == CPP_NAME
19129           || token->type == CPP_KEYWORD)
19130         {
19131           tree arguments = NULL_TREE;
19132
19133           /* Consume the token.  */
19134           token = cp_lexer_consume_token (parser->lexer);
19135
19136           /* Save away the identifier that indicates which attribute
19137              this is.  */
19138           identifier = (token->type == CPP_KEYWORD) 
19139             /* For keywords, use the canonical spelling, not the
19140                parsed identifier.  */
19141             ? ridpointers[(int) token->keyword]
19142             : token->u.value;
19143           
19144           attribute = build_tree_list (identifier, NULL_TREE);
19145
19146           /* Peek at the next token.  */
19147           token = cp_lexer_peek_token (parser->lexer);
19148           /* If it's an `(', then parse the attribute arguments.  */
19149           if (token->type == CPP_OPEN_PAREN)
19150             {
19151               VEC(tree,gc) *vec;
19152               int attr_flag = (attribute_takes_identifier_p (identifier)
19153                                ? id_attr : normal_attr);
19154               vec = cp_parser_parenthesized_expression_list
19155                     (parser, attr_flag, /*cast_p=*/false,
19156                      /*allow_expansion_p=*/false,
19157                      /*non_constant_p=*/NULL);
19158               if (vec == NULL)
19159                 arguments = error_mark_node;
19160               else
19161                 {
19162                   arguments = build_tree_list_vec (vec);
19163                   release_tree_vector (vec);
19164                 }
19165               /* Save the arguments away.  */
19166               TREE_VALUE (attribute) = arguments;
19167             }
19168
19169           if (arguments != error_mark_node)
19170             {
19171               /* Add this attribute to the list.  */
19172               TREE_CHAIN (attribute) = attribute_list;
19173               attribute_list = attribute;
19174             }
19175
19176           token = cp_lexer_peek_token (parser->lexer);
19177         }
19178       /* Now, look for more attributes.  If the next token isn't a
19179          `,', we're done.  */
19180       if (token->type != CPP_COMMA)
19181         break;
19182
19183       /* Consume the comma and keep going.  */
19184       cp_lexer_consume_token (parser->lexer);
19185     }
19186   parser->translate_strings_p = save_translate_strings_p;
19187
19188   /* We built up the list in reverse order.  */
19189   return nreverse (attribute_list);
19190 }
19191
19192 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19193    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19194    current value of the PEDANTIC flag, regardless of whether or not
19195    the `__extension__' keyword is present.  The caller is responsible
19196    for restoring the value of the PEDANTIC flag.  */
19197
19198 static bool
19199 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19200 {
19201   /* Save the old value of the PEDANTIC flag.  */
19202   *saved_pedantic = pedantic;
19203
19204   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19205     {
19206       /* Consume the `__extension__' token.  */
19207       cp_lexer_consume_token (parser->lexer);
19208       /* We're not being pedantic while the `__extension__' keyword is
19209          in effect.  */
19210       pedantic = 0;
19211
19212       return true;
19213     }
19214
19215   return false;
19216 }
19217
19218 /* Parse a label declaration.
19219
19220    label-declaration:
19221      __label__ label-declarator-seq ;
19222
19223    label-declarator-seq:
19224      identifier , label-declarator-seq
19225      identifier  */
19226
19227 static void
19228 cp_parser_label_declaration (cp_parser* parser)
19229 {
19230   /* Look for the `__label__' keyword.  */
19231   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19232
19233   while (true)
19234     {
19235       tree identifier;
19236
19237       /* Look for an identifier.  */
19238       identifier = cp_parser_identifier (parser);
19239       /* If we failed, stop.  */
19240       if (identifier == error_mark_node)
19241         break;
19242       /* Declare it as a label.  */
19243       finish_label_decl (identifier);
19244       /* If the next token is a `;', stop.  */
19245       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19246         break;
19247       /* Look for the `,' separating the label declarations.  */
19248       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19249     }
19250
19251   /* Look for the final `;'.  */
19252   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19253 }
19254
19255 /* Support Functions */
19256
19257 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19258    NAME should have one of the representations used for an
19259    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19260    is returned.  If PARSER->SCOPE is a dependent type, then a
19261    SCOPE_REF is returned.
19262
19263    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19264    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19265    was formed.  Abstractly, such entities should not be passed to this
19266    function, because they do not need to be looked up, but it is
19267    simpler to check for this special case here, rather than at the
19268    call-sites.
19269
19270    In cases not explicitly covered above, this function returns a
19271    DECL, OVERLOAD, or baselink representing the result of the lookup.
19272    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19273    is returned.
19274
19275    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19276    (e.g., "struct") that was used.  In that case bindings that do not
19277    refer to types are ignored.
19278
19279    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19280    ignored.
19281
19282    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19283    are ignored.
19284
19285    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19286    types.
19287
19288    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19289    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19290    NULL_TREE otherwise.  */
19291
19292 static tree
19293 cp_parser_lookup_name (cp_parser *parser, tree name,
19294                        enum tag_types tag_type,
19295                        bool is_template,
19296                        bool is_namespace,
19297                        bool check_dependency,
19298                        tree *ambiguous_decls,
19299                        location_t name_location)
19300 {
19301   int flags = 0;
19302   tree decl;
19303   tree object_type = parser->context->object_type;
19304
19305   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19306     flags |= LOOKUP_COMPLAIN;
19307
19308   /* Assume that the lookup will be unambiguous.  */
19309   if (ambiguous_decls)
19310     *ambiguous_decls = NULL_TREE;
19311
19312   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19313      no longer valid.  Note that if we are parsing tentatively, and
19314      the parse fails, OBJECT_TYPE will be automatically restored.  */
19315   parser->context->object_type = NULL_TREE;
19316
19317   if (name == error_mark_node)
19318     return error_mark_node;
19319
19320   /* A template-id has already been resolved; there is no lookup to
19321      do.  */
19322   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19323     return name;
19324   if (BASELINK_P (name))
19325     {
19326       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19327                   == TEMPLATE_ID_EXPR);
19328       return name;
19329     }
19330
19331   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19332      it should already have been checked to make sure that the name
19333      used matches the type being destroyed.  */
19334   if (TREE_CODE (name) == BIT_NOT_EXPR)
19335     {
19336       tree type;
19337
19338       /* Figure out to which type this destructor applies.  */
19339       if (parser->scope)
19340         type = parser->scope;
19341       else if (object_type)
19342         type = object_type;
19343       else
19344         type = current_class_type;
19345       /* If that's not a class type, there is no destructor.  */
19346       if (!type || !CLASS_TYPE_P (type))
19347         return error_mark_node;
19348       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19349         lazily_declare_fn (sfk_destructor, type);
19350       if (!CLASSTYPE_DESTRUCTORS (type))
19351           return error_mark_node;
19352       /* If it was a class type, return the destructor.  */
19353       return CLASSTYPE_DESTRUCTORS (type);
19354     }
19355
19356   /* By this point, the NAME should be an ordinary identifier.  If
19357      the id-expression was a qualified name, the qualifying scope is
19358      stored in PARSER->SCOPE at this point.  */
19359   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19360
19361   /* Perform the lookup.  */
19362   if (parser->scope)
19363     {
19364       bool dependent_p;
19365
19366       if (parser->scope == error_mark_node)
19367         return error_mark_node;
19368
19369       /* If the SCOPE is dependent, the lookup must be deferred until
19370          the template is instantiated -- unless we are explicitly
19371          looking up names in uninstantiated templates.  Even then, we
19372          cannot look up the name if the scope is not a class type; it
19373          might, for example, be a template type parameter.  */
19374       dependent_p = (TYPE_P (parser->scope)
19375                      && dependent_scope_p (parser->scope));
19376       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19377           && dependent_p)
19378         /* Defer lookup.  */
19379         decl = error_mark_node;
19380       else
19381         {
19382           tree pushed_scope = NULL_TREE;
19383
19384           /* If PARSER->SCOPE is a dependent type, then it must be a
19385              class type, and we must not be checking dependencies;
19386              otherwise, we would have processed this lookup above.  So
19387              that PARSER->SCOPE is not considered a dependent base by
19388              lookup_member, we must enter the scope here.  */
19389           if (dependent_p)
19390             pushed_scope = push_scope (parser->scope);
19391
19392           /* If the PARSER->SCOPE is a template specialization, it
19393              may be instantiated during name lookup.  In that case,
19394              errors may be issued.  Even if we rollback the current
19395              tentative parse, those errors are valid.  */
19396           decl = lookup_qualified_name (parser->scope, name,
19397                                         tag_type != none_type,
19398                                         /*complain=*/true);
19399
19400           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19401              lookup result and the nested-name-specifier nominates a class C:
19402                * if the name specified after the nested-name-specifier, when
19403                looked up in C, is the injected-class-name of C (Clause 9), or
19404                * if the name specified after the nested-name-specifier is the
19405                same as the identifier or the simple-template-id's template-
19406                name in the last component of the nested-name-specifier,
19407              the name is instead considered to name the constructor of
19408              class C. [ Note: for example, the constructor is not an
19409              acceptable lookup result in an elaborated-type-specifier so
19410              the constructor would not be used in place of the
19411              injected-class-name. --end note ] Such a constructor name
19412              shall be used only in the declarator-id of a declaration that
19413              names a constructor or in a using-declaration.  */
19414           if (tag_type == none_type
19415               && DECL_SELF_REFERENCE_P (decl)
19416               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19417             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19418                                           tag_type != none_type,
19419                                           /*complain=*/true);
19420
19421           /* If we have a single function from a using decl, pull it out.  */
19422           if (TREE_CODE (decl) == OVERLOAD
19423               && !really_overloaded_fn (decl))
19424             decl = OVL_FUNCTION (decl);
19425
19426           if (pushed_scope)
19427             pop_scope (pushed_scope);
19428         }
19429
19430       /* If the scope is a dependent type and either we deferred lookup or
19431          we did lookup but didn't find the name, rememeber the name.  */
19432       if (decl == error_mark_node && TYPE_P (parser->scope)
19433           && dependent_type_p (parser->scope))
19434         {
19435           if (tag_type)
19436             {
19437               tree type;
19438
19439               /* The resolution to Core Issue 180 says that `struct
19440                  A::B' should be considered a type-name, even if `A'
19441                  is dependent.  */
19442               type = make_typename_type (parser->scope, name, tag_type,
19443                                          /*complain=*/tf_error);
19444               decl = TYPE_NAME (type);
19445             }
19446           else if (is_template
19447                    && (cp_parser_next_token_ends_template_argument_p (parser)
19448                        || cp_lexer_next_token_is (parser->lexer,
19449                                                   CPP_CLOSE_PAREN)))
19450             decl = make_unbound_class_template (parser->scope,
19451                                                 name, NULL_TREE,
19452                                                 /*complain=*/tf_error);
19453           else
19454             decl = build_qualified_name (/*type=*/NULL_TREE,
19455                                          parser->scope, name,
19456                                          is_template);
19457         }
19458       parser->qualifying_scope = parser->scope;
19459       parser->object_scope = NULL_TREE;
19460     }
19461   else if (object_type)
19462     {
19463       tree object_decl = NULL_TREE;
19464       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19465          OBJECT_TYPE is not a class.  */
19466       if (CLASS_TYPE_P (object_type))
19467         /* If the OBJECT_TYPE is a template specialization, it may
19468            be instantiated during name lookup.  In that case, errors
19469            may be issued.  Even if we rollback the current tentative
19470            parse, those errors are valid.  */
19471         object_decl = lookup_member (object_type,
19472                                      name,
19473                                      /*protect=*/0,
19474                                      tag_type != none_type);
19475       /* Look it up in the enclosing context, too.  */
19476       decl = lookup_name_real (name, tag_type != none_type,
19477                                /*nonclass=*/0,
19478                                /*block_p=*/true, is_namespace, flags);
19479       parser->object_scope = object_type;
19480       parser->qualifying_scope = NULL_TREE;
19481       if (object_decl)
19482         decl = object_decl;
19483     }
19484   else
19485     {
19486       decl = lookup_name_real (name, tag_type != none_type,
19487                                /*nonclass=*/0,
19488                                /*block_p=*/true, is_namespace, flags);
19489       parser->qualifying_scope = NULL_TREE;
19490       parser->object_scope = NULL_TREE;
19491     }
19492
19493   /* If the lookup failed, let our caller know.  */
19494   if (!decl || decl == error_mark_node)
19495     return error_mark_node;
19496
19497   /* Pull out the template from an injected-class-name (or multiple).  */
19498   if (is_template)
19499     decl = maybe_get_template_decl_from_type_decl (decl);
19500
19501   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19502   if (TREE_CODE (decl) == TREE_LIST)
19503     {
19504       if (ambiguous_decls)
19505         *ambiguous_decls = decl;
19506       /* The error message we have to print is too complicated for
19507          cp_parser_error, so we incorporate its actions directly.  */
19508       if (!cp_parser_simulate_error (parser))
19509         {
19510           error_at (name_location, "reference to %qD is ambiguous",
19511                     name);
19512           print_candidates (decl);
19513         }
19514       return error_mark_node;
19515     }
19516
19517   gcc_assert (DECL_P (decl)
19518               || TREE_CODE (decl) == OVERLOAD
19519               || TREE_CODE (decl) == SCOPE_REF
19520               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19521               || BASELINK_P (decl));
19522
19523   /* If we have resolved the name of a member declaration, check to
19524      see if the declaration is accessible.  When the name resolves to
19525      set of overloaded functions, accessibility is checked when
19526      overload resolution is done.
19527
19528      During an explicit instantiation, access is not checked at all,
19529      as per [temp.explicit].  */
19530   if (DECL_P (decl))
19531     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19532
19533   return decl;
19534 }
19535
19536 /* Like cp_parser_lookup_name, but for use in the typical case where
19537    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19538    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19539
19540 static tree
19541 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19542 {
19543   return cp_parser_lookup_name (parser, name,
19544                                 none_type,
19545                                 /*is_template=*/false,
19546                                 /*is_namespace=*/false,
19547                                 /*check_dependency=*/true,
19548                                 /*ambiguous_decls=*/NULL,
19549                                 location);
19550 }
19551
19552 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19553    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19554    true, the DECL indicates the class being defined in a class-head,
19555    or declared in an elaborated-type-specifier.
19556
19557    Otherwise, return DECL.  */
19558
19559 static tree
19560 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19561 {
19562   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19563      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19564
19565        struct A {
19566          template <typename T> struct B;
19567        };
19568
19569        template <typename T> struct A::B {};
19570
19571      Similarly, in an elaborated-type-specifier:
19572
19573        namespace N { struct X{}; }
19574
19575        struct A {
19576          template <typename T> friend struct N::X;
19577        };
19578
19579      However, if the DECL refers to a class type, and we are in
19580      the scope of the class, then the name lookup automatically
19581      finds the TYPE_DECL created by build_self_reference rather
19582      than a TEMPLATE_DECL.  For example, in:
19583
19584        template <class T> struct S {
19585          S s;
19586        };
19587
19588      there is no need to handle such case.  */
19589
19590   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19591     return DECL_TEMPLATE_RESULT (decl);
19592
19593   return decl;
19594 }
19595
19596 /* If too many, or too few, template-parameter lists apply to the
19597    declarator, issue an error message.  Returns TRUE if all went well,
19598    and FALSE otherwise.  */
19599
19600 static bool
19601 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19602                                                 cp_declarator *declarator,
19603                                                 location_t declarator_location)
19604 {
19605   unsigned num_templates;
19606
19607   /* We haven't seen any classes that involve template parameters yet.  */
19608   num_templates = 0;
19609
19610   switch (declarator->kind)
19611     {
19612     case cdk_id:
19613       if (declarator->u.id.qualifying_scope)
19614         {
19615           tree scope;
19616
19617           scope = declarator->u.id.qualifying_scope;
19618
19619           while (scope && CLASS_TYPE_P (scope))
19620             {
19621               /* You're supposed to have one `template <...>'
19622                  for every template class, but you don't need one
19623                  for a full specialization.  For example:
19624
19625                  template <class T> struct S{};
19626                  template <> struct S<int> { void f(); };
19627                  void S<int>::f () {}
19628
19629                  is correct; there shouldn't be a `template <>' for
19630                  the definition of `S<int>::f'.  */
19631               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19632                 /* If SCOPE does not have template information of any
19633                    kind, then it is not a template, nor is it nested
19634                    within a template.  */
19635                 break;
19636               if (explicit_class_specialization_p (scope))
19637                 break;
19638               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19639                 ++num_templates;
19640
19641               scope = TYPE_CONTEXT (scope);
19642             }
19643         }
19644       else if (TREE_CODE (declarator->u.id.unqualified_name)
19645                == TEMPLATE_ID_EXPR)
19646         /* If the DECLARATOR has the form `X<y>' then it uses one
19647            additional level of template parameters.  */
19648         ++num_templates;
19649
19650       return cp_parser_check_template_parameters 
19651         (parser, num_templates, declarator_location, declarator);
19652
19653
19654     case cdk_function:
19655     case cdk_array:
19656     case cdk_pointer:
19657     case cdk_reference:
19658     case cdk_ptrmem:
19659       return (cp_parser_check_declarator_template_parameters
19660               (parser, declarator->declarator, declarator_location));
19661
19662     case cdk_error:
19663       return true;
19664
19665     default:
19666       gcc_unreachable ();
19667     }
19668   return false;
19669 }
19670
19671 /* NUM_TEMPLATES were used in the current declaration.  If that is
19672    invalid, return FALSE and issue an error messages.  Otherwise,
19673    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19674    declarator and we can print more accurate diagnostics.  */
19675
19676 static bool
19677 cp_parser_check_template_parameters (cp_parser* parser,
19678                                      unsigned num_templates,
19679                                      location_t location,
19680                                      cp_declarator *declarator)
19681 {
19682   /* If there are the same number of template classes and parameter
19683      lists, that's OK.  */
19684   if (parser->num_template_parameter_lists == num_templates)
19685     return true;
19686   /* If there are more, but only one more, then we are referring to a
19687      member template.  That's OK too.  */
19688   if (parser->num_template_parameter_lists == num_templates + 1)
19689     return true;
19690   /* If there are more template classes than parameter lists, we have
19691      something like:
19692
19693        template <class T> void S<T>::R<T>::f ();  */
19694   if (parser->num_template_parameter_lists < num_templates)
19695     {
19696       if (declarator && !current_function_decl)
19697         error_at (location, "specializing member %<%T::%E%> "
19698                   "requires %<template<>%> syntax", 
19699                   declarator->u.id.qualifying_scope,
19700                   declarator->u.id.unqualified_name);
19701       else if (declarator)
19702         error_at (location, "invalid declaration of %<%T::%E%>",
19703                   declarator->u.id.qualifying_scope,
19704                   declarator->u.id.unqualified_name);
19705       else 
19706         error_at (location, "too few template-parameter-lists");
19707       return false;
19708     }
19709   /* Otherwise, there are too many template parameter lists.  We have
19710      something like:
19711
19712      template <class T> template <class U> void S::f();  */
19713   error_at (location, "too many template-parameter-lists");
19714   return false;
19715 }
19716
19717 /* Parse an optional `::' token indicating that the following name is
19718    from the global namespace.  If so, PARSER->SCOPE is set to the
19719    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19720    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19721    Returns the new value of PARSER->SCOPE, if the `::' token is
19722    present, and NULL_TREE otherwise.  */
19723
19724 static tree
19725 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19726 {
19727   cp_token *token;
19728
19729   /* Peek at the next token.  */
19730   token = cp_lexer_peek_token (parser->lexer);
19731   /* If we're looking at a `::' token then we're starting from the
19732      global namespace, not our current location.  */
19733   if (token->type == CPP_SCOPE)
19734     {
19735       /* Consume the `::' token.  */
19736       cp_lexer_consume_token (parser->lexer);
19737       /* Set the SCOPE so that we know where to start the lookup.  */
19738       parser->scope = global_namespace;
19739       parser->qualifying_scope = global_namespace;
19740       parser->object_scope = NULL_TREE;
19741
19742       return parser->scope;
19743     }
19744   else if (!current_scope_valid_p)
19745     {
19746       parser->scope = NULL_TREE;
19747       parser->qualifying_scope = NULL_TREE;
19748       parser->object_scope = NULL_TREE;
19749     }
19750
19751   return NULL_TREE;
19752 }
19753
19754 /* Returns TRUE if the upcoming token sequence is the start of a
19755    constructor declarator.  If FRIEND_P is true, the declarator is
19756    preceded by the `friend' specifier.  */
19757
19758 static bool
19759 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19760 {
19761   bool constructor_p;
19762   tree nested_name_specifier;
19763   cp_token *next_token;
19764
19765   /* The common case is that this is not a constructor declarator, so
19766      try to avoid doing lots of work if at all possible.  It's not
19767      valid declare a constructor at function scope.  */
19768   if (parser->in_function_body)
19769     return false;
19770   /* And only certain tokens can begin a constructor declarator.  */
19771   next_token = cp_lexer_peek_token (parser->lexer);
19772   if (next_token->type != CPP_NAME
19773       && next_token->type != CPP_SCOPE
19774       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19775       && next_token->type != CPP_TEMPLATE_ID)
19776     return false;
19777
19778   /* Parse tentatively; we are going to roll back all of the tokens
19779      consumed here.  */
19780   cp_parser_parse_tentatively (parser);
19781   /* Assume that we are looking at a constructor declarator.  */
19782   constructor_p = true;
19783
19784   /* Look for the optional `::' operator.  */
19785   cp_parser_global_scope_opt (parser,
19786                               /*current_scope_valid_p=*/false);
19787   /* Look for the nested-name-specifier.  */
19788   nested_name_specifier
19789     = (cp_parser_nested_name_specifier_opt (parser,
19790                                             /*typename_keyword_p=*/false,
19791                                             /*check_dependency_p=*/false,
19792                                             /*type_p=*/false,
19793                                             /*is_declaration=*/false));
19794   /* Outside of a class-specifier, there must be a
19795      nested-name-specifier.  */
19796   if (!nested_name_specifier &&
19797       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19798        || friend_p))
19799     constructor_p = false;
19800   else if (nested_name_specifier == error_mark_node)
19801     constructor_p = false;
19802
19803   /* If we have a class scope, this is easy; DR 147 says that S::S always
19804      names the constructor, and no other qualified name could.  */
19805   if (constructor_p && nested_name_specifier
19806       && CLASS_TYPE_P (nested_name_specifier))
19807     {
19808       tree id = cp_parser_unqualified_id (parser,
19809                                           /*template_keyword_p=*/false,
19810                                           /*check_dependency_p=*/false,
19811                                           /*declarator_p=*/true,
19812                                           /*optional_p=*/false);
19813       if (is_overloaded_fn (id))
19814         id = DECL_NAME (get_first_fn (id));
19815       if (!constructor_name_p (id, nested_name_specifier))
19816         constructor_p = false;
19817     }
19818   /* If we still think that this might be a constructor-declarator,
19819      look for a class-name.  */
19820   else if (constructor_p)
19821     {
19822       /* If we have:
19823
19824            template <typename T> struct S {
19825              S();
19826            };
19827
19828          we must recognize that the nested `S' names a class.  */
19829       tree type_decl;
19830       type_decl = cp_parser_class_name (parser,
19831                                         /*typename_keyword_p=*/false,
19832                                         /*template_keyword_p=*/false,
19833                                         none_type,
19834                                         /*check_dependency_p=*/false,
19835                                         /*class_head_p=*/false,
19836                                         /*is_declaration=*/false);
19837       /* If there was no class-name, then this is not a constructor.  */
19838       constructor_p = !cp_parser_error_occurred (parser);
19839
19840       /* If we're still considering a constructor, we have to see a `(',
19841          to begin the parameter-declaration-clause, followed by either a
19842          `)', an `...', or a decl-specifier.  We need to check for a
19843          type-specifier to avoid being fooled into thinking that:
19844
19845            S (f) (int);
19846
19847          is a constructor.  (It is actually a function named `f' that
19848          takes one parameter (of type `int') and returns a value of type
19849          `S'.  */
19850       if (constructor_p
19851           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19852         constructor_p = false;
19853
19854       if (constructor_p
19855           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19856           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19857           /* A parameter declaration begins with a decl-specifier,
19858              which is either the "attribute" keyword, a storage class
19859              specifier, or (usually) a type-specifier.  */
19860           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19861         {
19862           tree type;
19863           tree pushed_scope = NULL_TREE;
19864           unsigned saved_num_template_parameter_lists;
19865
19866           /* Names appearing in the type-specifier should be looked up
19867              in the scope of the class.  */
19868           if (current_class_type)
19869             type = NULL_TREE;
19870           else
19871             {
19872               type = TREE_TYPE (type_decl);
19873               if (TREE_CODE (type) == TYPENAME_TYPE)
19874                 {
19875                   type = resolve_typename_type (type,
19876                                                 /*only_current_p=*/false);
19877                   if (TREE_CODE (type) == TYPENAME_TYPE)
19878                     {
19879                       cp_parser_abort_tentative_parse (parser);
19880                       return false;
19881                     }
19882                 }
19883               pushed_scope = push_scope (type);
19884             }
19885
19886           /* Inside the constructor parameter list, surrounding
19887              template-parameter-lists do not apply.  */
19888           saved_num_template_parameter_lists
19889             = parser->num_template_parameter_lists;
19890           parser->num_template_parameter_lists = 0;
19891
19892           /* Look for the type-specifier.  */
19893           cp_parser_type_specifier (parser,
19894                                     CP_PARSER_FLAGS_NONE,
19895                                     /*decl_specs=*/NULL,
19896                                     /*is_declarator=*/true,
19897                                     /*declares_class_or_enum=*/NULL,
19898                                     /*is_cv_qualifier=*/NULL);
19899
19900           parser->num_template_parameter_lists
19901             = saved_num_template_parameter_lists;
19902
19903           /* Leave the scope of the class.  */
19904           if (pushed_scope)
19905             pop_scope (pushed_scope);
19906
19907           constructor_p = !cp_parser_error_occurred (parser);
19908         }
19909     }
19910
19911   /* We did not really want to consume any tokens.  */
19912   cp_parser_abort_tentative_parse (parser);
19913
19914   return constructor_p;
19915 }
19916
19917 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19918    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19919    they must be performed once we are in the scope of the function.
19920
19921    Returns the function defined.  */
19922
19923 static tree
19924 cp_parser_function_definition_from_specifiers_and_declarator
19925   (cp_parser* parser,
19926    cp_decl_specifier_seq *decl_specifiers,
19927    tree attributes,
19928    const cp_declarator *declarator)
19929 {
19930   tree fn;
19931   bool success_p;
19932
19933   /* Begin the function-definition.  */
19934   success_p = start_function (decl_specifiers, declarator, attributes);
19935
19936   /* The things we're about to see are not directly qualified by any
19937      template headers we've seen thus far.  */
19938   reset_specialization ();
19939
19940   /* If there were names looked up in the decl-specifier-seq that we
19941      did not check, check them now.  We must wait until we are in the
19942      scope of the function to perform the checks, since the function
19943      might be a friend.  */
19944   perform_deferred_access_checks ();
19945
19946   if (!success_p)
19947     {
19948       /* Skip the entire function.  */
19949       cp_parser_skip_to_end_of_block_or_statement (parser);
19950       fn = error_mark_node;
19951     }
19952   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19953     {
19954       /* Seen already, skip it.  An error message has already been output.  */
19955       cp_parser_skip_to_end_of_block_or_statement (parser);
19956       fn = current_function_decl;
19957       current_function_decl = NULL_TREE;
19958       /* If this is a function from a class, pop the nested class.  */
19959       if (current_class_name)
19960         pop_nested_class ();
19961     }
19962   else
19963     {
19964       timevar_id_t tv;
19965       if (DECL_DECLARED_INLINE_P (current_function_decl))
19966         tv = TV_PARSE_INLINE;
19967       else
19968         tv = TV_PARSE_FUNC;
19969       timevar_push (tv);
19970       fn = cp_parser_function_definition_after_declarator (parser,
19971                                                          /*inline_p=*/false);
19972       timevar_pop (tv);
19973     }
19974
19975   return fn;
19976 }
19977
19978 /* Parse the part of a function-definition that follows the
19979    declarator.  INLINE_P is TRUE iff this function is an inline
19980    function defined within a class-specifier.
19981
19982    Returns the function defined.  */
19983
19984 static tree
19985 cp_parser_function_definition_after_declarator (cp_parser* parser,
19986                                                 bool inline_p)
19987 {
19988   tree fn;
19989   bool ctor_initializer_p = false;
19990   bool saved_in_unbraced_linkage_specification_p;
19991   bool saved_in_function_body;
19992   unsigned saved_num_template_parameter_lists;
19993   cp_token *token;
19994
19995   saved_in_function_body = parser->in_function_body;
19996   parser->in_function_body = true;
19997   /* If the next token is `return', then the code may be trying to
19998      make use of the "named return value" extension that G++ used to
19999      support.  */
20000   token = cp_lexer_peek_token (parser->lexer);
20001   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20002     {
20003       /* Consume the `return' keyword.  */
20004       cp_lexer_consume_token (parser->lexer);
20005       /* Look for the identifier that indicates what value is to be
20006          returned.  */
20007       cp_parser_identifier (parser);
20008       /* Issue an error message.  */
20009       error_at (token->location,
20010                 "named return values are no longer supported");
20011       /* Skip tokens until we reach the start of the function body.  */
20012       while (true)
20013         {
20014           cp_token *token = cp_lexer_peek_token (parser->lexer);
20015           if (token->type == CPP_OPEN_BRACE
20016               || token->type == CPP_EOF
20017               || token->type == CPP_PRAGMA_EOL)
20018             break;
20019           cp_lexer_consume_token (parser->lexer);
20020         }
20021     }
20022   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20023      anything declared inside `f'.  */
20024   saved_in_unbraced_linkage_specification_p
20025     = parser->in_unbraced_linkage_specification_p;
20026   parser->in_unbraced_linkage_specification_p = false;
20027   /* Inside the function, surrounding template-parameter-lists do not
20028      apply.  */
20029   saved_num_template_parameter_lists
20030     = parser->num_template_parameter_lists;
20031   parser->num_template_parameter_lists = 0;
20032
20033   start_lambda_scope (current_function_decl);
20034
20035   /* If the next token is `try', then we are looking at a
20036      function-try-block.  */
20037   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20038     ctor_initializer_p = cp_parser_function_try_block (parser);
20039   /* A function-try-block includes the function-body, so we only do
20040      this next part if we're not processing a function-try-block.  */
20041   else
20042     ctor_initializer_p
20043       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20044
20045   finish_lambda_scope ();
20046
20047   /* Finish the function.  */
20048   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20049                         (inline_p ? 2 : 0));
20050   /* Generate code for it, if necessary.  */
20051   expand_or_defer_fn (fn);
20052   /* Restore the saved values.  */
20053   parser->in_unbraced_linkage_specification_p
20054     = saved_in_unbraced_linkage_specification_p;
20055   parser->num_template_parameter_lists
20056     = saved_num_template_parameter_lists;
20057   parser->in_function_body = saved_in_function_body;
20058
20059   return fn;
20060 }
20061
20062 /* Parse a template-declaration, assuming that the `export' (and
20063    `extern') keywords, if present, has already been scanned.  MEMBER_P
20064    is as for cp_parser_template_declaration.  */
20065
20066 static void
20067 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20068 {
20069   tree decl = NULL_TREE;
20070   VEC (deferred_access_check,gc) *checks;
20071   tree parameter_list;
20072   bool friend_p = false;
20073   bool need_lang_pop;
20074   cp_token *token;
20075
20076   /* Look for the `template' keyword.  */
20077   token = cp_lexer_peek_token (parser->lexer);
20078   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20079     return;
20080
20081   /* And the `<'.  */
20082   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20083     return;
20084   if (at_class_scope_p () && current_function_decl)
20085     {
20086       /* 14.5.2.2 [temp.mem]
20087
20088          A local class shall not have member templates.  */
20089       error_at (token->location,
20090                 "invalid declaration of member template in local class");
20091       cp_parser_skip_to_end_of_block_or_statement (parser);
20092       return;
20093     }
20094   /* [temp]
20095
20096      A template ... shall not have C linkage.  */
20097   if (current_lang_name == lang_name_c)
20098     {
20099       error_at (token->location, "template with C linkage");
20100       /* Give it C++ linkage to avoid confusing other parts of the
20101          front end.  */
20102       push_lang_context (lang_name_cplusplus);
20103       need_lang_pop = true;
20104     }
20105   else
20106     need_lang_pop = false;
20107
20108   /* We cannot perform access checks on the template parameter
20109      declarations until we know what is being declared, just as we
20110      cannot check the decl-specifier list.  */
20111   push_deferring_access_checks (dk_deferred);
20112
20113   /* If the next token is `>', then we have an invalid
20114      specialization.  Rather than complain about an invalid template
20115      parameter, issue an error message here.  */
20116   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20117     {
20118       cp_parser_error (parser, "invalid explicit specialization");
20119       begin_specialization ();
20120       parameter_list = NULL_TREE;
20121     }
20122   else
20123     {
20124       /* Parse the template parameters.  */
20125       parameter_list = cp_parser_template_parameter_list (parser);
20126       fixup_template_parms ();
20127     }
20128
20129   /* Get the deferred access checks from the parameter list.  These
20130      will be checked once we know what is being declared, as for a
20131      member template the checks must be performed in the scope of the
20132      class containing the member.  */
20133   checks = get_deferred_access_checks ();
20134
20135   /* Look for the `>'.  */
20136   cp_parser_skip_to_end_of_template_parameter_list (parser);
20137   /* We just processed one more parameter list.  */
20138   ++parser->num_template_parameter_lists;
20139   /* If the next token is `template', there are more template
20140      parameters.  */
20141   if (cp_lexer_next_token_is_keyword (parser->lexer,
20142                                       RID_TEMPLATE))
20143     cp_parser_template_declaration_after_export (parser, member_p);
20144   else
20145     {
20146       /* There are no access checks when parsing a template, as we do not
20147          know if a specialization will be a friend.  */
20148       push_deferring_access_checks (dk_no_check);
20149       token = cp_lexer_peek_token (parser->lexer);
20150       decl = cp_parser_single_declaration (parser,
20151                                            checks,
20152                                            member_p,
20153                                            /*explicit_specialization_p=*/false,
20154                                            &friend_p);
20155       pop_deferring_access_checks ();
20156
20157       /* If this is a member template declaration, let the front
20158          end know.  */
20159       if (member_p && !friend_p && decl)
20160         {
20161           if (TREE_CODE (decl) == TYPE_DECL)
20162             cp_parser_check_access_in_redeclaration (decl, token->location);
20163
20164           decl = finish_member_template_decl (decl);
20165         }
20166       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20167         make_friend_class (current_class_type, TREE_TYPE (decl),
20168                            /*complain=*/true);
20169     }
20170   /* We are done with the current parameter list.  */
20171   --parser->num_template_parameter_lists;
20172
20173   pop_deferring_access_checks ();
20174
20175   /* Finish up.  */
20176   finish_template_decl (parameter_list);
20177
20178   /* Register member declarations.  */
20179   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20180     finish_member_declaration (decl);
20181   /* For the erroneous case of a template with C linkage, we pushed an
20182      implicit C++ linkage scope; exit that scope now.  */
20183   if (need_lang_pop)
20184     pop_lang_context ();
20185   /* If DECL is a function template, we must return to parse it later.
20186      (Even though there is no definition, there might be default
20187      arguments that need handling.)  */
20188   if (member_p && decl
20189       && (TREE_CODE (decl) == FUNCTION_DECL
20190           || DECL_FUNCTION_TEMPLATE_P (decl)))
20191     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20192 }
20193
20194 /* Perform the deferred access checks from a template-parameter-list.
20195    CHECKS is a TREE_LIST of access checks, as returned by
20196    get_deferred_access_checks.  */
20197
20198 static void
20199 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20200 {
20201   ++processing_template_parmlist;
20202   perform_access_checks (checks);
20203   --processing_template_parmlist;
20204 }
20205
20206 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20207    `function-definition' sequence.  MEMBER_P is true, this declaration
20208    appears in a class scope.
20209
20210    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20211    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20212
20213 static tree
20214 cp_parser_single_declaration (cp_parser* parser,
20215                               VEC (deferred_access_check,gc)* checks,
20216                               bool member_p,
20217                               bool explicit_specialization_p,
20218                               bool* friend_p)
20219 {
20220   int declares_class_or_enum;
20221   tree decl = NULL_TREE;
20222   cp_decl_specifier_seq decl_specifiers;
20223   bool function_definition_p = false;
20224   cp_token *decl_spec_token_start;
20225
20226   /* This function is only used when processing a template
20227      declaration.  */
20228   gcc_assert (innermost_scope_kind () == sk_template_parms
20229               || innermost_scope_kind () == sk_template_spec);
20230
20231   /* Defer access checks until we know what is being declared.  */
20232   push_deferring_access_checks (dk_deferred);
20233
20234   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20235      alternative.  */
20236   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20237   cp_parser_decl_specifier_seq (parser,
20238                                 CP_PARSER_FLAGS_OPTIONAL,
20239                                 &decl_specifiers,
20240                                 &declares_class_or_enum);
20241   if (friend_p)
20242     *friend_p = cp_parser_friend_p (&decl_specifiers);
20243
20244   /* There are no template typedefs.  */
20245   if (decl_specifiers.specs[(int) ds_typedef])
20246     {
20247       error_at (decl_spec_token_start->location,
20248                 "template declaration of %<typedef%>");
20249       decl = error_mark_node;
20250     }
20251
20252   /* Gather up the access checks that occurred the
20253      decl-specifier-seq.  */
20254   stop_deferring_access_checks ();
20255
20256   /* Check for the declaration of a template class.  */
20257   if (declares_class_or_enum)
20258     {
20259       if (cp_parser_declares_only_class_p (parser))
20260         {
20261           decl = shadow_tag (&decl_specifiers);
20262
20263           /* In this case:
20264
20265                struct C {
20266                  friend template <typename T> struct A<T>::B;
20267                };
20268
20269              A<T>::B will be represented by a TYPENAME_TYPE, and
20270              therefore not recognized by shadow_tag.  */
20271           if (friend_p && *friend_p
20272               && !decl
20273               && decl_specifiers.type
20274               && TYPE_P (decl_specifiers.type))
20275             decl = decl_specifiers.type;
20276
20277           if (decl && decl != error_mark_node)
20278             decl = TYPE_NAME (decl);
20279           else
20280             decl = error_mark_node;
20281
20282           /* Perform access checks for template parameters.  */
20283           cp_parser_perform_template_parameter_access_checks (checks);
20284         }
20285     }
20286
20287   /* Complain about missing 'typename' or other invalid type names.  */
20288   if (!decl_specifiers.any_type_specifiers_p
20289       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20290     {
20291       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20292          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20293          the rest of this declaration.  */
20294       decl = error_mark_node;
20295       goto out;
20296     }
20297
20298   /* If it's not a template class, try for a template function.  If
20299      the next token is a `;', then this declaration does not declare
20300      anything.  But, if there were errors in the decl-specifiers, then
20301      the error might well have come from an attempted class-specifier.
20302      In that case, there's no need to warn about a missing declarator.  */
20303   if (!decl
20304       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20305           || decl_specifiers.type != error_mark_node))
20306     {
20307       decl = cp_parser_init_declarator (parser,
20308                                         &decl_specifiers,
20309                                         checks,
20310                                         /*function_definition_allowed_p=*/true,
20311                                         member_p,
20312                                         declares_class_or_enum,
20313                                         &function_definition_p,
20314                                         NULL);
20315
20316     /* 7.1.1-1 [dcl.stc]
20317
20318        A storage-class-specifier shall not be specified in an explicit
20319        specialization...  */
20320     if (decl
20321         && explicit_specialization_p
20322         && decl_specifiers.storage_class != sc_none)
20323       {
20324         error_at (decl_spec_token_start->location,
20325                   "explicit template specialization cannot have a storage class");
20326         decl = error_mark_node;
20327       }
20328     }
20329
20330   /* Look for a trailing `;' after the declaration.  */
20331   if (!function_definition_p
20332       && (decl == error_mark_node
20333           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20334     cp_parser_skip_to_end_of_block_or_statement (parser);
20335
20336  out:
20337   pop_deferring_access_checks ();
20338
20339   /* Clear any current qualification; whatever comes next is the start
20340      of something new.  */
20341   parser->scope = NULL_TREE;
20342   parser->qualifying_scope = NULL_TREE;
20343   parser->object_scope = NULL_TREE;
20344
20345   return decl;
20346 }
20347
20348 /* Parse a cast-expression that is not the operand of a unary "&".  */
20349
20350 static tree
20351 cp_parser_simple_cast_expression (cp_parser *parser)
20352 {
20353   return cp_parser_cast_expression (parser, /*address_p=*/false,
20354                                     /*cast_p=*/false, NULL);
20355 }
20356
20357 /* Parse a functional cast to TYPE.  Returns an expression
20358    representing the cast.  */
20359
20360 static tree
20361 cp_parser_functional_cast (cp_parser* parser, tree type)
20362 {
20363   VEC(tree,gc) *vec;
20364   tree expression_list;
20365   tree cast;
20366   bool nonconst_p;
20367
20368   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20369     {
20370       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20371       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20372       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20373       if (TREE_CODE (type) == TYPE_DECL)
20374         type = TREE_TYPE (type);
20375       return finish_compound_literal (type, expression_list,
20376                                       tf_warning_or_error);
20377     }
20378
20379
20380   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20381                                                  /*cast_p=*/true,
20382                                                  /*allow_expansion_p=*/true,
20383                                                  /*non_constant_p=*/NULL);
20384   if (vec == NULL)
20385     expression_list = error_mark_node;
20386   else
20387     {
20388       expression_list = build_tree_list_vec (vec);
20389       release_tree_vector (vec);
20390     }
20391
20392   cast = build_functional_cast (type, expression_list,
20393                                 tf_warning_or_error);
20394   /* [expr.const]/1: In an integral constant expression "only type
20395      conversions to integral or enumeration type can be used".  */
20396   if (TREE_CODE (type) == TYPE_DECL)
20397     type = TREE_TYPE (type);
20398   if (cast != error_mark_node
20399       && !cast_valid_in_integral_constant_expression_p (type)
20400       && cp_parser_non_integral_constant_expression (parser,
20401                                                      NIC_CONSTRUCTOR))
20402     return error_mark_node;
20403   return cast;
20404 }
20405
20406 /* Save the tokens that make up the body of a member function defined
20407    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20408    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20409    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20410    for the member function.  */
20411
20412 static tree
20413 cp_parser_save_member_function_body (cp_parser* parser,
20414                                      cp_decl_specifier_seq *decl_specifiers,
20415                                      cp_declarator *declarator,
20416                                      tree attributes)
20417 {
20418   cp_token *first;
20419   cp_token *last;
20420   tree fn;
20421
20422   /* Create the FUNCTION_DECL.  */
20423   fn = grokmethod (decl_specifiers, declarator, attributes);
20424   /* If something went badly wrong, bail out now.  */
20425   if (fn == error_mark_node)
20426     {
20427       /* If there's a function-body, skip it.  */
20428       if (cp_parser_token_starts_function_definition_p
20429           (cp_lexer_peek_token (parser->lexer)))
20430         cp_parser_skip_to_end_of_block_or_statement (parser);
20431       return error_mark_node;
20432     }
20433
20434   /* Remember it, if there default args to post process.  */
20435   cp_parser_save_default_args (parser, fn);
20436
20437   /* Save away the tokens that make up the body of the
20438      function.  */
20439   first = parser->lexer->next_token;
20440   /* We can have braced-init-list mem-initializers before the fn body.  */
20441   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20442     {
20443       cp_lexer_consume_token (parser->lexer);
20444       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20445              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20446         {
20447           /* cache_group will stop after an un-nested { } pair, too.  */
20448           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20449             break;
20450
20451           /* variadic mem-inits have ... after the ')'.  */
20452           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20453             cp_lexer_consume_token (parser->lexer);
20454         }
20455     }
20456   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20457   /* Handle function try blocks.  */
20458   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20459     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20460   last = parser->lexer->next_token;
20461
20462   /* Save away the inline definition; we will process it when the
20463      class is complete.  */
20464   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20465   DECL_PENDING_INLINE_P (fn) = 1;
20466
20467   /* We need to know that this was defined in the class, so that
20468      friend templates are handled correctly.  */
20469   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20470
20471   /* Add FN to the queue of functions to be parsed later.  */
20472   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20473
20474   return fn;
20475 }
20476
20477 /* Parse a template-argument-list, as well as the trailing ">" (but
20478    not the opening ">").  See cp_parser_template_argument_list for the
20479    return value.  */
20480
20481 static tree
20482 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20483 {
20484   tree arguments;
20485   tree saved_scope;
20486   tree saved_qualifying_scope;
20487   tree saved_object_scope;
20488   bool saved_greater_than_is_operator_p;
20489   int saved_unevaluated_operand;
20490   int saved_inhibit_evaluation_warnings;
20491
20492   /* [temp.names]
20493
20494      When parsing a template-id, the first non-nested `>' is taken as
20495      the end of the template-argument-list rather than a greater-than
20496      operator.  */
20497   saved_greater_than_is_operator_p
20498     = parser->greater_than_is_operator_p;
20499   parser->greater_than_is_operator_p = false;
20500   /* Parsing the argument list may modify SCOPE, so we save it
20501      here.  */
20502   saved_scope = parser->scope;
20503   saved_qualifying_scope = parser->qualifying_scope;
20504   saved_object_scope = parser->object_scope;
20505   /* We need to evaluate the template arguments, even though this
20506      template-id may be nested within a "sizeof".  */
20507   saved_unevaluated_operand = cp_unevaluated_operand;
20508   cp_unevaluated_operand = 0;
20509   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20510   c_inhibit_evaluation_warnings = 0;
20511   /* Parse the template-argument-list itself.  */
20512   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20513       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20514     arguments = NULL_TREE;
20515   else
20516     arguments = cp_parser_template_argument_list (parser);
20517   /* Look for the `>' that ends the template-argument-list. If we find
20518      a '>>' instead, it's probably just a typo.  */
20519   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20520     {
20521       if (cxx_dialect != cxx98)
20522         {
20523           /* In C++0x, a `>>' in a template argument list or cast
20524              expression is considered to be two separate `>'
20525              tokens. So, change the current token to a `>', but don't
20526              consume it: it will be consumed later when the outer
20527              template argument list (or cast expression) is parsed.
20528              Note that this replacement of `>' for `>>' is necessary
20529              even if we are parsing tentatively: in the tentative
20530              case, after calling
20531              cp_parser_enclosed_template_argument_list we will always
20532              throw away all of the template arguments and the first
20533              closing `>', either because the template argument list
20534              was erroneous or because we are replacing those tokens
20535              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20536              not have been thrown away) is needed either to close an
20537              outer template argument list or to complete a new-style
20538              cast.  */
20539           cp_token *token = cp_lexer_peek_token (parser->lexer);
20540           token->type = CPP_GREATER;
20541         }
20542       else if (!saved_greater_than_is_operator_p)
20543         {
20544           /* If we're in a nested template argument list, the '>>' has
20545             to be a typo for '> >'. We emit the error message, but we
20546             continue parsing and we push a '>' as next token, so that
20547             the argument list will be parsed correctly.  Note that the
20548             global source location is still on the token before the
20549             '>>', so we need to say explicitly where we want it.  */
20550           cp_token *token = cp_lexer_peek_token (parser->lexer);
20551           error_at (token->location, "%<>>%> should be %<> >%> "
20552                     "within a nested template argument list");
20553
20554           token->type = CPP_GREATER;
20555         }
20556       else
20557         {
20558           /* If this is not a nested template argument list, the '>>'
20559             is a typo for '>'. Emit an error message and continue.
20560             Same deal about the token location, but here we can get it
20561             right by consuming the '>>' before issuing the diagnostic.  */
20562           cp_token *token = cp_lexer_consume_token (parser->lexer);
20563           error_at (token->location,
20564                     "spurious %<>>%>, use %<>%> to terminate "
20565                     "a template argument list");
20566         }
20567     }
20568   else
20569     cp_parser_skip_to_end_of_template_parameter_list (parser);
20570   /* The `>' token might be a greater-than operator again now.  */
20571   parser->greater_than_is_operator_p
20572     = saved_greater_than_is_operator_p;
20573   /* Restore the SAVED_SCOPE.  */
20574   parser->scope = saved_scope;
20575   parser->qualifying_scope = saved_qualifying_scope;
20576   parser->object_scope = saved_object_scope;
20577   cp_unevaluated_operand = saved_unevaluated_operand;
20578   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20579
20580   return arguments;
20581 }
20582
20583 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20584    arguments, or the body of the function have not yet been parsed,
20585    parse them now.  */
20586
20587 static void
20588 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20589 {
20590   timevar_push (TV_PARSE_INMETH);
20591   /* If this member is a template, get the underlying
20592      FUNCTION_DECL.  */
20593   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20594     member_function = DECL_TEMPLATE_RESULT (member_function);
20595
20596   /* There should not be any class definitions in progress at this
20597      point; the bodies of members are only parsed outside of all class
20598      definitions.  */
20599   gcc_assert (parser->num_classes_being_defined == 0);
20600   /* While we're parsing the member functions we might encounter more
20601      classes.  We want to handle them right away, but we don't want
20602      them getting mixed up with functions that are currently in the
20603      queue.  */
20604   push_unparsed_function_queues (parser);
20605
20606   /* Make sure that any template parameters are in scope.  */
20607   maybe_begin_member_template_processing (member_function);
20608
20609   /* If the body of the function has not yet been parsed, parse it
20610      now.  */
20611   if (DECL_PENDING_INLINE_P (member_function))
20612     {
20613       tree function_scope;
20614       cp_token_cache *tokens;
20615
20616       /* The function is no longer pending; we are processing it.  */
20617       tokens = DECL_PENDING_INLINE_INFO (member_function);
20618       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20619       DECL_PENDING_INLINE_P (member_function) = 0;
20620
20621       /* If this is a local class, enter the scope of the containing
20622          function.  */
20623       function_scope = current_function_decl;
20624       if (function_scope)
20625         push_function_context ();
20626
20627       /* Push the body of the function onto the lexer stack.  */
20628       cp_parser_push_lexer_for_tokens (parser, tokens);
20629
20630       /* Let the front end know that we going to be defining this
20631          function.  */
20632       start_preparsed_function (member_function, NULL_TREE,
20633                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20634
20635       /* Don't do access checking if it is a templated function.  */
20636       if (processing_template_decl)
20637         push_deferring_access_checks (dk_no_check);
20638
20639       /* Now, parse the body of the function.  */
20640       cp_parser_function_definition_after_declarator (parser,
20641                                                       /*inline_p=*/true);
20642
20643       if (processing_template_decl)
20644         pop_deferring_access_checks ();
20645
20646       /* Leave the scope of the containing function.  */
20647       if (function_scope)
20648         pop_function_context ();
20649       cp_parser_pop_lexer (parser);
20650     }
20651
20652   /* Remove any template parameters from the symbol table.  */
20653   maybe_end_member_template_processing ();
20654
20655   /* Restore the queue.  */
20656   pop_unparsed_function_queues (parser);
20657   timevar_pop (TV_PARSE_INMETH);
20658 }
20659
20660 /* If DECL contains any default args, remember it on the unparsed
20661    functions queue.  */
20662
20663 static void
20664 cp_parser_save_default_args (cp_parser* parser, tree decl)
20665 {
20666   tree probe;
20667
20668   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20669        probe;
20670        probe = TREE_CHAIN (probe))
20671     if (TREE_PURPOSE (probe))
20672       {
20673         cp_default_arg_entry *entry
20674           = VEC_safe_push (cp_default_arg_entry, gc,
20675                            unparsed_funs_with_default_args, NULL);
20676         entry->class_type = current_class_type;
20677         entry->decl = decl;
20678         break;
20679       }
20680 }
20681
20682 /* FN is a FUNCTION_DECL which may contains a parameter with an
20683    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20684    assumes that the current scope is the scope in which the default
20685    argument should be processed.  */
20686
20687 static void
20688 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20689 {
20690   bool saved_local_variables_forbidden_p;
20691   tree parm, parmdecl;
20692
20693   /* While we're parsing the default args, we might (due to the
20694      statement expression extension) encounter more classes.  We want
20695      to handle them right away, but we don't want them getting mixed
20696      up with default args that are currently in the queue.  */
20697   push_unparsed_function_queues (parser);
20698
20699   /* Local variable names (and the `this' keyword) may not appear
20700      in a default argument.  */
20701   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20702   parser->local_variables_forbidden_p = true;
20703
20704   push_defarg_context (fn);
20705
20706   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20707          parmdecl = DECL_ARGUMENTS (fn);
20708        parm && parm != void_list_node;
20709        parm = TREE_CHAIN (parm),
20710          parmdecl = DECL_CHAIN (parmdecl))
20711     {
20712       cp_token_cache *tokens;
20713       tree default_arg = TREE_PURPOSE (parm);
20714       tree parsed_arg;
20715       VEC(tree,gc) *insts;
20716       tree copy;
20717       unsigned ix;
20718
20719       if (!default_arg)
20720         continue;
20721
20722       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20723         /* This can happen for a friend declaration for a function
20724            already declared with default arguments.  */
20725         continue;
20726
20727        /* Push the saved tokens for the default argument onto the parser's
20728           lexer stack.  */
20729       tokens = DEFARG_TOKENS (default_arg);
20730       cp_parser_push_lexer_for_tokens (parser, tokens);
20731
20732       start_lambda_scope (parmdecl);
20733
20734       /* Parse the assignment-expression.  */
20735       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20736       if (parsed_arg == error_mark_node)
20737         {
20738           cp_parser_pop_lexer (parser);
20739           continue;
20740         }
20741
20742       if (!processing_template_decl)
20743         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20744
20745       TREE_PURPOSE (parm) = parsed_arg;
20746
20747       /* Update any instantiations we've already created.  */
20748       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20749            VEC_iterate (tree, insts, ix, copy); ix++)
20750         TREE_PURPOSE (copy) = parsed_arg;
20751
20752       finish_lambda_scope ();
20753
20754       /* If the token stream has not been completely used up, then
20755          there was extra junk after the end of the default
20756          argument.  */
20757       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20758         cp_parser_error (parser, "expected %<,%>");
20759
20760       /* Revert to the main lexer.  */
20761       cp_parser_pop_lexer (parser);
20762     }
20763
20764   pop_defarg_context ();
20765
20766   /* Make sure no default arg is missing.  */
20767   check_default_args (fn);
20768
20769   /* Restore the state of local_variables_forbidden_p.  */
20770   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20771
20772   /* Restore the queue.  */
20773   pop_unparsed_function_queues (parser);
20774 }
20775
20776 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20777    either a TYPE or an expression, depending on the form of the
20778    input.  The KEYWORD indicates which kind of expression we have
20779    encountered.  */
20780
20781 static tree
20782 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20783 {
20784   tree expr = NULL_TREE;
20785   const char *saved_message;
20786   char *tmp;
20787   bool saved_integral_constant_expression_p;
20788   bool saved_non_integral_constant_expression_p;
20789   bool pack_expansion_p = false;
20790
20791   /* Types cannot be defined in a `sizeof' expression.  Save away the
20792      old message.  */
20793   saved_message = parser->type_definition_forbidden_message;
20794   /* And create the new one.  */
20795   tmp = concat ("types may not be defined in %<",
20796                 IDENTIFIER_POINTER (ridpointers[keyword]),
20797                 "%> expressions", NULL);
20798   parser->type_definition_forbidden_message = tmp;
20799
20800   /* The restrictions on constant-expressions do not apply inside
20801      sizeof expressions.  */
20802   saved_integral_constant_expression_p
20803     = parser->integral_constant_expression_p;
20804   saved_non_integral_constant_expression_p
20805     = parser->non_integral_constant_expression_p;
20806   parser->integral_constant_expression_p = false;
20807
20808   /* If it's a `...', then we are computing the length of a parameter
20809      pack.  */
20810   if (keyword == RID_SIZEOF
20811       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20812     {
20813       /* Consume the `...'.  */
20814       cp_lexer_consume_token (parser->lexer);
20815       maybe_warn_variadic_templates ();
20816
20817       /* Note that this is an expansion.  */
20818       pack_expansion_p = true;
20819     }
20820
20821   /* Do not actually evaluate the expression.  */
20822   ++cp_unevaluated_operand;
20823   ++c_inhibit_evaluation_warnings;
20824   /* If it's a `(', then we might be looking at the type-id
20825      construction.  */
20826   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20827     {
20828       tree type;
20829       bool saved_in_type_id_in_expr_p;
20830
20831       /* We can't be sure yet whether we're looking at a type-id or an
20832          expression.  */
20833       cp_parser_parse_tentatively (parser);
20834       /* Consume the `('.  */
20835       cp_lexer_consume_token (parser->lexer);
20836       /* Parse the type-id.  */
20837       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20838       parser->in_type_id_in_expr_p = true;
20839       type = cp_parser_type_id (parser);
20840       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20841       /* Now, look for the trailing `)'.  */
20842       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20843       /* If all went well, then we're done.  */
20844       if (cp_parser_parse_definitely (parser))
20845         {
20846           cp_decl_specifier_seq decl_specs;
20847
20848           /* Build a trivial decl-specifier-seq.  */
20849           clear_decl_specs (&decl_specs);
20850           decl_specs.type = type;
20851
20852           /* Call grokdeclarator to figure out what type this is.  */
20853           expr = grokdeclarator (NULL,
20854                                  &decl_specs,
20855                                  TYPENAME,
20856                                  /*initialized=*/0,
20857                                  /*attrlist=*/NULL);
20858         }
20859     }
20860
20861   /* If the type-id production did not work out, then we must be
20862      looking at the unary-expression production.  */
20863   if (!expr)
20864     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20865                                        /*cast_p=*/false, NULL);
20866
20867   if (pack_expansion_p)
20868     /* Build a pack expansion. */
20869     expr = make_pack_expansion (expr);
20870
20871   /* Go back to evaluating expressions.  */
20872   --cp_unevaluated_operand;
20873   --c_inhibit_evaluation_warnings;
20874
20875   /* Free the message we created.  */
20876   free (tmp);
20877   /* And restore the old one.  */
20878   parser->type_definition_forbidden_message = saved_message;
20879   parser->integral_constant_expression_p
20880     = saved_integral_constant_expression_p;
20881   parser->non_integral_constant_expression_p
20882     = saved_non_integral_constant_expression_p;
20883
20884   return expr;
20885 }
20886
20887 /* If the current declaration has no declarator, return true.  */
20888
20889 static bool
20890 cp_parser_declares_only_class_p (cp_parser *parser)
20891 {
20892   /* If the next token is a `;' or a `,' then there is no
20893      declarator.  */
20894   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20895           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20896 }
20897
20898 /* Update the DECL_SPECS to reflect the storage class indicated by
20899    KEYWORD.  */
20900
20901 static void
20902 cp_parser_set_storage_class (cp_parser *parser,
20903                              cp_decl_specifier_seq *decl_specs,
20904                              enum rid keyword,
20905                              location_t location)
20906 {
20907   cp_storage_class storage_class;
20908
20909   if (parser->in_unbraced_linkage_specification_p)
20910     {
20911       error_at (location, "invalid use of %qD in linkage specification",
20912                 ridpointers[keyword]);
20913       return;
20914     }
20915   else if (decl_specs->storage_class != sc_none)
20916     {
20917       decl_specs->conflicting_specifiers_p = true;
20918       return;
20919     }
20920
20921   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20922       && decl_specs->specs[(int) ds_thread])
20923     {
20924       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20925       decl_specs->specs[(int) ds_thread] = 0;
20926     }
20927
20928   switch (keyword)
20929     {
20930     case RID_AUTO:
20931       storage_class = sc_auto;
20932       break;
20933     case RID_REGISTER:
20934       storage_class = sc_register;
20935       break;
20936     case RID_STATIC:
20937       storage_class = sc_static;
20938       break;
20939     case RID_EXTERN:
20940       storage_class = sc_extern;
20941       break;
20942     case RID_MUTABLE:
20943       storage_class = sc_mutable;
20944       break;
20945     default:
20946       gcc_unreachable ();
20947     }
20948   decl_specs->storage_class = storage_class;
20949
20950   /* A storage class specifier cannot be applied alongside a typedef 
20951      specifier. If there is a typedef specifier present then set 
20952      conflicting_specifiers_p which will trigger an error later
20953      on in grokdeclarator. */
20954   if (decl_specs->specs[(int)ds_typedef])
20955     decl_specs->conflicting_specifiers_p = true;
20956 }
20957
20958 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20959    is true, the type is a user-defined type; otherwise it is a
20960    built-in type specified by a keyword.  */
20961
20962 static void
20963 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20964                               tree type_spec,
20965                               location_t location,
20966                               bool user_defined_p)
20967 {
20968   decl_specs->any_specifiers_p = true;
20969
20970   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20971      (with, for example, in "typedef int wchar_t;") we remember that
20972      this is what happened.  In system headers, we ignore these
20973      declarations so that G++ can work with system headers that are not
20974      C++-safe.  */
20975   if (decl_specs->specs[(int) ds_typedef]
20976       && !user_defined_p
20977       && (type_spec == boolean_type_node
20978           || type_spec == char16_type_node
20979           || type_spec == char32_type_node
20980           || type_spec == wchar_type_node)
20981       && (decl_specs->type
20982           || decl_specs->specs[(int) ds_long]
20983           || decl_specs->specs[(int) ds_short]
20984           || decl_specs->specs[(int) ds_unsigned]
20985           || decl_specs->specs[(int) ds_signed]))
20986     {
20987       decl_specs->redefined_builtin_type = type_spec;
20988       if (!decl_specs->type)
20989         {
20990           decl_specs->type = type_spec;
20991           decl_specs->user_defined_type_p = false;
20992           decl_specs->type_location = location;
20993         }
20994     }
20995   else if (decl_specs->type)
20996     decl_specs->multiple_types_p = true;
20997   else
20998     {
20999       decl_specs->type = type_spec;
21000       decl_specs->user_defined_type_p = user_defined_p;
21001       decl_specs->redefined_builtin_type = NULL_TREE;
21002       decl_specs->type_location = location;
21003     }
21004 }
21005
21006 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21007    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
21008
21009 static bool
21010 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21011 {
21012   return decl_specifiers->specs[(int) ds_friend] != 0;
21013 }
21014
21015 /* Issue an error message indicating that TOKEN_DESC was expected.
21016    If KEYWORD is true, it indicated this function is called by
21017    cp_parser_require_keword and the required token can only be
21018    a indicated keyword. */
21019
21020 static void
21021 cp_parser_required_error (cp_parser *parser,
21022                           required_token token_desc,
21023                           bool keyword)
21024 {
21025   switch (token_desc)
21026     {
21027       case RT_NEW:
21028         cp_parser_error (parser, "expected %<new%>");
21029         return;
21030       case RT_DELETE:
21031         cp_parser_error (parser, "expected %<delete%>");
21032         return;
21033       case RT_RETURN:
21034         cp_parser_error (parser, "expected %<return%>");
21035         return;
21036       case RT_WHILE:
21037         cp_parser_error (parser, "expected %<while%>");
21038         return;
21039       case RT_EXTERN:
21040         cp_parser_error (parser, "expected %<extern%>");
21041         return;
21042       case RT_STATIC_ASSERT:
21043         cp_parser_error (parser, "expected %<static_assert%>");
21044         return;
21045       case RT_DECLTYPE:
21046         cp_parser_error (parser, "expected %<decltype%>");
21047         return;
21048       case RT_OPERATOR:
21049         cp_parser_error (parser, "expected %<operator%>");
21050         return;
21051       case RT_CLASS:
21052         cp_parser_error (parser, "expected %<class%>");
21053         return;
21054       case RT_TEMPLATE:
21055         cp_parser_error (parser, "expected %<template%>");
21056         return;
21057       case RT_NAMESPACE:
21058         cp_parser_error (parser, "expected %<namespace%>");
21059         return;
21060       case RT_USING:
21061         cp_parser_error (parser, "expected %<using%>");
21062         return;
21063       case RT_ASM:
21064         cp_parser_error (parser, "expected %<asm%>");
21065         return;
21066       case RT_TRY:
21067         cp_parser_error (parser, "expected %<try%>");
21068         return;
21069       case RT_CATCH:
21070         cp_parser_error (parser, "expected %<catch%>");
21071         return;
21072       case RT_THROW:
21073         cp_parser_error (parser, "expected %<throw%>");
21074         return;
21075       case RT_LABEL:
21076         cp_parser_error (parser, "expected %<__label__%>");
21077         return;
21078       case RT_AT_TRY:
21079         cp_parser_error (parser, "expected %<@try%>");
21080         return;
21081       case RT_AT_SYNCHRONIZED:
21082         cp_parser_error (parser, "expected %<@synchronized%>");
21083         return;
21084       case RT_AT_THROW:
21085         cp_parser_error (parser, "expected %<@throw%>");
21086         return;
21087       default:
21088         break;
21089     }
21090   if (!keyword)
21091     {
21092       switch (token_desc)
21093         {
21094           case RT_SEMICOLON:
21095             cp_parser_error (parser, "expected %<;%>");
21096             return;
21097           case RT_OPEN_PAREN:
21098             cp_parser_error (parser, "expected %<(%>");
21099             return;
21100           case RT_CLOSE_BRACE:
21101             cp_parser_error (parser, "expected %<}%>");
21102             return;
21103           case RT_OPEN_BRACE:
21104             cp_parser_error (parser, "expected %<{%>");
21105             return;
21106           case RT_CLOSE_SQUARE:
21107             cp_parser_error (parser, "expected %<]%>");
21108             return;
21109           case RT_OPEN_SQUARE:
21110             cp_parser_error (parser, "expected %<[%>");
21111             return;
21112           case RT_COMMA:
21113             cp_parser_error (parser, "expected %<,%>");
21114             return;
21115           case RT_SCOPE:
21116             cp_parser_error (parser, "expected %<::%>");
21117             return;
21118           case RT_LESS:
21119             cp_parser_error (parser, "expected %<<%>");
21120             return;
21121           case RT_GREATER:
21122             cp_parser_error (parser, "expected %<>%>");
21123             return;
21124           case RT_EQ:
21125             cp_parser_error (parser, "expected %<=%>");
21126             return;
21127           case RT_ELLIPSIS:
21128             cp_parser_error (parser, "expected %<...%>");
21129             return;
21130           case RT_MULT:
21131             cp_parser_error (parser, "expected %<*%>");
21132             return;
21133           case RT_COMPL:
21134             cp_parser_error (parser, "expected %<~%>");
21135             return;
21136           case RT_COLON:
21137             cp_parser_error (parser, "expected %<:%>");
21138             return;
21139           case RT_COLON_SCOPE:
21140             cp_parser_error (parser, "expected %<:%> or %<::%>");
21141             return;
21142           case RT_CLOSE_PAREN:
21143             cp_parser_error (parser, "expected %<)%>");
21144             return;
21145           case RT_COMMA_CLOSE_PAREN:
21146             cp_parser_error (parser, "expected %<,%> or %<)%>");
21147             return;
21148           case RT_PRAGMA_EOL:
21149             cp_parser_error (parser, "expected end of line");
21150             return;
21151           case RT_NAME:
21152             cp_parser_error (parser, "expected identifier");
21153             return;
21154           case RT_SELECT:
21155             cp_parser_error (parser, "expected selection-statement");
21156             return;
21157           case RT_INTERATION:
21158             cp_parser_error (parser, "expected iteration-statement");
21159             return;
21160           case RT_JUMP:
21161             cp_parser_error (parser, "expected jump-statement");
21162             return;
21163           case RT_CLASS_KEY:
21164             cp_parser_error (parser, "expected class-key");
21165             return;
21166           case RT_CLASS_TYPENAME_TEMPLATE:
21167             cp_parser_error (parser,
21168                  "expected %<class%>, %<typename%>, or %<template%>");
21169             return;
21170           default:
21171             gcc_unreachable ();
21172         }
21173     }
21174   else
21175     gcc_unreachable ();
21176 }
21177
21178
21179
21180 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
21181    issue an error message indicating that TOKEN_DESC was expected.
21182
21183    Returns the token consumed, if the token had the appropriate type.
21184    Otherwise, returns NULL.  */
21185
21186 static cp_token *
21187 cp_parser_require (cp_parser* parser,
21188                    enum cpp_ttype type,
21189                    required_token token_desc)
21190 {
21191   if (cp_lexer_next_token_is (parser->lexer, type))
21192     return cp_lexer_consume_token (parser->lexer);
21193   else
21194     {
21195       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21196       if (!cp_parser_simulate_error (parser))
21197         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21198       return NULL;
21199     }
21200 }
21201
21202 /* An error message is produced if the next token is not '>'.
21203    All further tokens are skipped until the desired token is
21204    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21205
21206 static void
21207 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21208 {
21209   /* Current level of '< ... >'.  */
21210   unsigned level = 0;
21211   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21212   unsigned nesting_depth = 0;
21213
21214   /* Are we ready, yet?  If not, issue error message.  */
21215   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21216     return;
21217
21218   /* Skip tokens until the desired token is found.  */
21219   while (true)
21220     {
21221       /* Peek at the next token.  */
21222       switch (cp_lexer_peek_token (parser->lexer)->type)
21223         {
21224         case CPP_LESS:
21225           if (!nesting_depth)
21226             ++level;
21227           break;
21228
21229         case CPP_RSHIFT:
21230           if (cxx_dialect == cxx98)
21231             /* C++0x views the `>>' operator as two `>' tokens, but
21232                C++98 does not. */
21233             break;
21234           else if (!nesting_depth && level-- == 0)
21235             {
21236               /* We've hit a `>>' where the first `>' closes the
21237                  template argument list, and the second `>' is
21238                  spurious.  Just consume the `>>' and stop; we've
21239                  already produced at least one error.  */
21240               cp_lexer_consume_token (parser->lexer);
21241               return;
21242             }
21243           /* Fall through for C++0x, so we handle the second `>' in
21244              the `>>'.  */
21245
21246         case CPP_GREATER:
21247           if (!nesting_depth && level-- == 0)
21248             {
21249               /* We've reached the token we want, consume it and stop.  */
21250               cp_lexer_consume_token (parser->lexer);
21251               return;
21252             }
21253           break;
21254
21255         case CPP_OPEN_PAREN:
21256         case CPP_OPEN_SQUARE:
21257           ++nesting_depth;
21258           break;
21259
21260         case CPP_CLOSE_PAREN:
21261         case CPP_CLOSE_SQUARE:
21262           if (nesting_depth-- == 0)
21263             return;
21264           break;
21265
21266         case CPP_EOF:
21267         case CPP_PRAGMA_EOL:
21268         case CPP_SEMICOLON:
21269         case CPP_OPEN_BRACE:
21270         case CPP_CLOSE_BRACE:
21271           /* The '>' was probably forgotten, don't look further.  */
21272           return;
21273
21274         default:
21275           break;
21276         }
21277
21278       /* Consume this token.  */
21279       cp_lexer_consume_token (parser->lexer);
21280     }
21281 }
21282
21283 /* If the next token is the indicated keyword, consume it.  Otherwise,
21284    issue an error message indicating that TOKEN_DESC was expected.
21285
21286    Returns the token consumed, if the token had the appropriate type.
21287    Otherwise, returns NULL.  */
21288
21289 static cp_token *
21290 cp_parser_require_keyword (cp_parser* parser,
21291                            enum rid keyword,
21292                            required_token token_desc)
21293 {
21294   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21295
21296   if (token && token->keyword != keyword)
21297     {
21298       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21299       return NULL;
21300     }
21301
21302   return token;
21303 }
21304
21305 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21306    function-definition.  */
21307
21308 static bool
21309 cp_parser_token_starts_function_definition_p (cp_token* token)
21310 {
21311   return (/* An ordinary function-body begins with an `{'.  */
21312           token->type == CPP_OPEN_BRACE
21313           /* A ctor-initializer begins with a `:'.  */
21314           || token->type == CPP_COLON
21315           /* A function-try-block begins with `try'.  */
21316           || token->keyword == RID_TRY
21317           /* The named return value extension begins with `return'.  */
21318           || token->keyword == RID_RETURN);
21319 }
21320
21321 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21322    definition.  */
21323
21324 static bool
21325 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21326 {
21327   cp_token *token;
21328
21329   token = cp_lexer_peek_token (parser->lexer);
21330   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21331 }
21332
21333 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21334    C++0x) ending a template-argument.  */
21335
21336 static bool
21337 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21338 {
21339   cp_token *token;
21340
21341   token = cp_lexer_peek_token (parser->lexer);
21342   return (token->type == CPP_COMMA 
21343           || token->type == CPP_GREATER
21344           || token->type == CPP_ELLIPSIS
21345           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21346 }
21347
21348 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21349    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21350
21351 static bool
21352 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21353                                                      size_t n)
21354 {
21355   cp_token *token;
21356
21357   token = cp_lexer_peek_nth_token (parser->lexer, n);
21358   if (token->type == CPP_LESS)
21359     return true;
21360   /* Check for the sequence `<::' in the original code. It would be lexed as
21361      `[:', where `[' is a digraph, and there is no whitespace before
21362      `:'.  */
21363   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21364     {
21365       cp_token *token2;
21366       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21367       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21368         return true;
21369     }
21370   return false;
21371 }
21372
21373 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21374    or none_type otherwise.  */
21375
21376 static enum tag_types
21377 cp_parser_token_is_class_key (cp_token* token)
21378 {
21379   switch (token->keyword)
21380     {
21381     case RID_CLASS:
21382       return class_type;
21383     case RID_STRUCT:
21384       return record_type;
21385     case RID_UNION:
21386       return union_type;
21387
21388     default:
21389       return none_type;
21390     }
21391 }
21392
21393 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21394
21395 static void
21396 cp_parser_check_class_key (enum tag_types class_key, tree type)
21397 {
21398   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21399     permerror (input_location, "%qs tag used in naming %q#T",
21400             class_key == union_type ? "union"
21401              : class_key == record_type ? "struct" : "class",
21402              type);
21403 }
21404
21405 /* Issue an error message if DECL is redeclared with different
21406    access than its original declaration [class.access.spec/3].
21407    This applies to nested classes and nested class templates.
21408    [class.mem/1].  */
21409
21410 static void
21411 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21412 {
21413   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21414     return;
21415
21416   if ((TREE_PRIVATE (decl)
21417        != (current_access_specifier == access_private_node))
21418       || (TREE_PROTECTED (decl)
21419           != (current_access_specifier == access_protected_node)))
21420     error_at (location, "%qD redeclared with different access", decl);
21421 }
21422
21423 /* Look for the `template' keyword, as a syntactic disambiguator.
21424    Return TRUE iff it is present, in which case it will be
21425    consumed.  */
21426
21427 static bool
21428 cp_parser_optional_template_keyword (cp_parser *parser)
21429 {
21430   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21431     {
21432       /* The `template' keyword can only be used within templates;
21433          outside templates the parser can always figure out what is a
21434          template and what is not.  */
21435       if (!processing_template_decl)
21436         {
21437           cp_token *token = cp_lexer_peek_token (parser->lexer);
21438           error_at (token->location,
21439                     "%<template%> (as a disambiguator) is only allowed "
21440                     "within templates");
21441           /* If this part of the token stream is rescanned, the same
21442              error message would be generated.  So, we purge the token
21443              from the stream.  */
21444           cp_lexer_purge_token (parser->lexer);
21445           return false;
21446         }
21447       else
21448         {
21449           /* Consume the `template' keyword.  */
21450           cp_lexer_consume_token (parser->lexer);
21451           return true;
21452         }
21453     }
21454
21455   return false;
21456 }
21457
21458 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21459    set PARSER->SCOPE, and perform other related actions.  */
21460
21461 static void
21462 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21463 {
21464   int i;
21465   struct tree_check *check_value;
21466   deferred_access_check *chk;
21467   VEC (deferred_access_check,gc) *checks;
21468
21469   /* Get the stored value.  */
21470   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21471   /* Perform any access checks that were deferred.  */
21472   checks = check_value->checks;
21473   if (checks)
21474     {
21475       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21476         perform_or_defer_access_check (chk->binfo,
21477                                        chk->decl,
21478                                        chk->diag_decl);
21479     }
21480   /* Set the scope from the stored value.  */
21481   parser->scope = check_value->value;
21482   parser->qualifying_scope = check_value->qualifying_scope;
21483   parser->object_scope = NULL_TREE;
21484 }
21485
21486 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21487    encounter the end of a block before what we were looking for.  */
21488
21489 static bool
21490 cp_parser_cache_group (cp_parser *parser,
21491                        enum cpp_ttype end,
21492                        unsigned depth)
21493 {
21494   while (true)
21495     {
21496       cp_token *token = cp_lexer_peek_token (parser->lexer);
21497
21498       /* Abort a parenthesized expression if we encounter a semicolon.  */
21499       if ((end == CPP_CLOSE_PAREN || depth == 0)
21500           && token->type == CPP_SEMICOLON)
21501         return true;
21502       /* If we've reached the end of the file, stop.  */
21503       if (token->type == CPP_EOF
21504           || (end != CPP_PRAGMA_EOL
21505               && token->type == CPP_PRAGMA_EOL))
21506         return true;
21507       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21508         /* We've hit the end of an enclosing block, so there's been some
21509            kind of syntax error.  */
21510         return true;
21511
21512       /* Consume the token.  */
21513       cp_lexer_consume_token (parser->lexer);
21514       /* See if it starts a new group.  */
21515       if (token->type == CPP_OPEN_BRACE)
21516         {
21517           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21518           /* In theory this should probably check end == '}', but
21519              cp_parser_save_member_function_body needs it to exit
21520              after either '}' or ')' when called with ')'.  */
21521           if (depth == 0)
21522             return false;
21523         }
21524       else if (token->type == CPP_OPEN_PAREN)
21525         {
21526           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21527           if (depth == 0 && end == CPP_CLOSE_PAREN)
21528             return false;
21529         }
21530       else if (token->type == CPP_PRAGMA)
21531         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21532       else if (token->type == end)
21533         return false;
21534     }
21535 }
21536
21537 /* Begin parsing tentatively.  We always save tokens while parsing
21538    tentatively so that if the tentative parsing fails we can restore the
21539    tokens.  */
21540
21541 static void
21542 cp_parser_parse_tentatively (cp_parser* parser)
21543 {
21544   /* Enter a new parsing context.  */
21545   parser->context = cp_parser_context_new (parser->context);
21546   /* Begin saving tokens.  */
21547   cp_lexer_save_tokens (parser->lexer);
21548   /* In order to avoid repetitive access control error messages,
21549      access checks are queued up until we are no longer parsing
21550      tentatively.  */
21551   push_deferring_access_checks (dk_deferred);
21552 }
21553
21554 /* Commit to the currently active tentative parse.  */
21555
21556 static void
21557 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21558 {
21559   cp_parser_context *context;
21560   cp_lexer *lexer;
21561
21562   /* Mark all of the levels as committed.  */
21563   lexer = parser->lexer;
21564   for (context = parser->context; context->next; context = context->next)
21565     {
21566       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21567         break;
21568       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21569       while (!cp_lexer_saving_tokens (lexer))
21570         lexer = lexer->next;
21571       cp_lexer_commit_tokens (lexer);
21572     }
21573 }
21574
21575 /* Abort the currently active tentative parse.  All consumed tokens
21576    will be rolled back, and no diagnostics will be issued.  */
21577
21578 static void
21579 cp_parser_abort_tentative_parse (cp_parser* parser)
21580 {
21581   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21582               || errorcount > 0);
21583   cp_parser_simulate_error (parser);
21584   /* Now, pretend that we want to see if the construct was
21585      successfully parsed.  */
21586   cp_parser_parse_definitely (parser);
21587 }
21588
21589 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21590    token stream.  Otherwise, commit to the tokens we have consumed.
21591    Returns true if no error occurred; false otherwise.  */
21592
21593 static bool
21594 cp_parser_parse_definitely (cp_parser* parser)
21595 {
21596   bool error_occurred;
21597   cp_parser_context *context;
21598
21599   /* Remember whether or not an error occurred, since we are about to
21600      destroy that information.  */
21601   error_occurred = cp_parser_error_occurred (parser);
21602   /* Remove the topmost context from the stack.  */
21603   context = parser->context;
21604   parser->context = context->next;
21605   /* If no parse errors occurred, commit to the tentative parse.  */
21606   if (!error_occurred)
21607     {
21608       /* Commit to the tokens read tentatively, unless that was
21609          already done.  */
21610       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21611         cp_lexer_commit_tokens (parser->lexer);
21612
21613       pop_to_parent_deferring_access_checks ();
21614     }
21615   /* Otherwise, if errors occurred, roll back our state so that things
21616      are just as they were before we began the tentative parse.  */
21617   else
21618     {
21619       cp_lexer_rollback_tokens (parser->lexer);
21620       pop_deferring_access_checks ();
21621     }
21622   /* Add the context to the front of the free list.  */
21623   context->next = cp_parser_context_free_list;
21624   cp_parser_context_free_list = context;
21625
21626   return !error_occurred;
21627 }
21628
21629 /* Returns true if we are parsing tentatively and are not committed to
21630    this tentative parse.  */
21631
21632 static bool
21633 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21634 {
21635   return (cp_parser_parsing_tentatively (parser)
21636           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21637 }
21638
21639 /* Returns nonzero iff an error has occurred during the most recent
21640    tentative parse.  */
21641
21642 static bool
21643 cp_parser_error_occurred (cp_parser* parser)
21644 {
21645   return (cp_parser_parsing_tentatively (parser)
21646           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21647 }
21648
21649 /* Returns nonzero if GNU extensions are allowed.  */
21650
21651 static bool
21652 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21653 {
21654   return parser->allow_gnu_extensions_p;
21655 }
21656 \f
21657 /* Objective-C++ Productions */
21658
21659
21660 /* Parse an Objective-C expression, which feeds into a primary-expression
21661    above.
21662
21663    objc-expression:
21664      objc-message-expression
21665      objc-string-literal
21666      objc-encode-expression
21667      objc-protocol-expression
21668      objc-selector-expression
21669
21670   Returns a tree representation of the expression.  */
21671
21672 static tree
21673 cp_parser_objc_expression (cp_parser* parser)
21674 {
21675   /* Try to figure out what kind of declaration is present.  */
21676   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21677
21678   switch (kwd->type)
21679     {
21680     case CPP_OPEN_SQUARE:
21681       return cp_parser_objc_message_expression (parser);
21682
21683     case CPP_OBJC_STRING:
21684       kwd = cp_lexer_consume_token (parser->lexer);
21685       return objc_build_string_object (kwd->u.value);
21686
21687     case CPP_KEYWORD:
21688       switch (kwd->keyword)
21689         {
21690         case RID_AT_ENCODE:
21691           return cp_parser_objc_encode_expression (parser);
21692
21693         case RID_AT_PROTOCOL:
21694           return cp_parser_objc_protocol_expression (parser);
21695
21696         case RID_AT_SELECTOR:
21697           return cp_parser_objc_selector_expression (parser);
21698
21699         default:
21700           break;
21701         }
21702     default:
21703       error_at (kwd->location,
21704                 "misplaced %<@%D%> Objective-C++ construct",
21705                 kwd->u.value);
21706       cp_parser_skip_to_end_of_block_or_statement (parser);
21707     }
21708
21709   return error_mark_node;
21710 }
21711
21712 /* Parse an Objective-C message expression.
21713
21714    objc-message-expression:
21715      [ objc-message-receiver objc-message-args ]
21716
21717    Returns a representation of an Objective-C message.  */
21718
21719 static tree
21720 cp_parser_objc_message_expression (cp_parser* parser)
21721 {
21722   tree receiver, messageargs;
21723
21724   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21725   receiver = cp_parser_objc_message_receiver (parser);
21726   messageargs = cp_parser_objc_message_args (parser);
21727   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21728
21729   return objc_build_message_expr (receiver, messageargs);
21730 }
21731
21732 /* Parse an objc-message-receiver.
21733
21734    objc-message-receiver:
21735      expression
21736      simple-type-specifier
21737
21738   Returns a representation of the type or expression.  */
21739
21740 static tree
21741 cp_parser_objc_message_receiver (cp_parser* parser)
21742 {
21743   tree rcv;
21744
21745   /* An Objective-C message receiver may be either (1) a type
21746      or (2) an expression.  */
21747   cp_parser_parse_tentatively (parser);
21748   rcv = cp_parser_expression (parser, false, NULL);
21749
21750   if (cp_parser_parse_definitely (parser))
21751     return rcv;
21752
21753   rcv = cp_parser_simple_type_specifier (parser,
21754                                          /*decl_specs=*/NULL,
21755                                          CP_PARSER_FLAGS_NONE);
21756
21757   return objc_get_class_reference (rcv);
21758 }
21759
21760 /* Parse the arguments and selectors comprising an Objective-C message.
21761
21762    objc-message-args:
21763      objc-selector
21764      objc-selector-args
21765      objc-selector-args , objc-comma-args
21766
21767    objc-selector-args:
21768      objc-selector [opt] : assignment-expression
21769      objc-selector-args objc-selector [opt] : assignment-expression
21770
21771    objc-comma-args:
21772      assignment-expression
21773      objc-comma-args , assignment-expression
21774
21775    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21776    selector arguments and TREE_VALUE containing a list of comma
21777    arguments.  */
21778
21779 static tree
21780 cp_parser_objc_message_args (cp_parser* parser)
21781 {
21782   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21783   bool maybe_unary_selector_p = true;
21784   cp_token *token = cp_lexer_peek_token (parser->lexer);
21785
21786   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21787     {
21788       tree selector = NULL_TREE, arg;
21789
21790       if (token->type != CPP_COLON)
21791         selector = cp_parser_objc_selector (parser);
21792
21793       /* Detect if we have a unary selector.  */
21794       if (maybe_unary_selector_p
21795           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21796         return build_tree_list (selector, NULL_TREE);
21797
21798       maybe_unary_selector_p = false;
21799       cp_parser_require (parser, CPP_COLON, RT_COLON);
21800       arg = cp_parser_assignment_expression (parser, false, NULL);
21801
21802       sel_args
21803         = chainon (sel_args,
21804                    build_tree_list (selector, arg));
21805
21806       token = cp_lexer_peek_token (parser->lexer);
21807     }
21808
21809   /* Handle non-selector arguments, if any. */
21810   while (token->type == CPP_COMMA)
21811     {
21812       tree arg;
21813
21814       cp_lexer_consume_token (parser->lexer);
21815       arg = cp_parser_assignment_expression (parser, false, NULL);
21816
21817       addl_args
21818         = chainon (addl_args,
21819                    build_tree_list (NULL_TREE, arg));
21820
21821       token = cp_lexer_peek_token (parser->lexer);
21822     }
21823
21824   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21825     {
21826       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21827       return build_tree_list (error_mark_node, error_mark_node);
21828     }
21829
21830   return build_tree_list (sel_args, addl_args);
21831 }
21832
21833 /* Parse an Objective-C encode expression.
21834
21835    objc-encode-expression:
21836      @encode objc-typename
21837
21838    Returns an encoded representation of the type argument.  */
21839
21840 static tree
21841 cp_parser_objc_encode_expression (cp_parser* parser)
21842 {
21843   tree type;
21844   cp_token *token;
21845
21846   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21847   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21848   token = cp_lexer_peek_token (parser->lexer);
21849   type = complete_type (cp_parser_type_id (parser));
21850   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21851
21852   if (!type)
21853     {
21854       error_at (token->location, 
21855                 "%<@encode%> must specify a type as an argument");
21856       return error_mark_node;
21857     }
21858
21859   /* This happens if we find @encode(T) (where T is a template
21860      typename or something dependent on a template typename) when
21861      parsing a template.  In that case, we can't compile it
21862      immediately, but we rather create an AT_ENCODE_EXPR which will
21863      need to be instantiated when the template is used.
21864   */
21865   if (dependent_type_p (type))
21866     {
21867       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21868       TREE_READONLY (value) = 1;
21869       return value;
21870     }
21871
21872   return objc_build_encode_expr (type);
21873 }
21874
21875 /* Parse an Objective-C @defs expression.  */
21876
21877 static tree
21878 cp_parser_objc_defs_expression (cp_parser *parser)
21879 {
21880   tree name;
21881
21882   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21883   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21884   name = cp_parser_identifier (parser);
21885   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21886
21887   return objc_get_class_ivars (name);
21888 }
21889
21890 /* Parse an Objective-C protocol expression.
21891
21892   objc-protocol-expression:
21893     @protocol ( identifier )
21894
21895   Returns a representation of the protocol expression.  */
21896
21897 static tree
21898 cp_parser_objc_protocol_expression (cp_parser* parser)
21899 {
21900   tree proto;
21901
21902   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21903   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21904   proto = cp_parser_identifier (parser);
21905   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21906
21907   return objc_build_protocol_expr (proto);
21908 }
21909
21910 /* Parse an Objective-C selector expression.
21911
21912    objc-selector-expression:
21913      @selector ( objc-method-signature )
21914
21915    objc-method-signature:
21916      objc-selector
21917      objc-selector-seq
21918
21919    objc-selector-seq:
21920      objc-selector :
21921      objc-selector-seq objc-selector :
21922
21923   Returns a representation of the method selector.  */
21924
21925 static tree
21926 cp_parser_objc_selector_expression (cp_parser* parser)
21927 {
21928   tree sel_seq = NULL_TREE;
21929   bool maybe_unary_selector_p = true;
21930   cp_token *token;
21931   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21932
21933   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21934   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21935   token = cp_lexer_peek_token (parser->lexer);
21936
21937   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21938          || token->type == CPP_SCOPE)
21939     {
21940       tree selector = NULL_TREE;
21941
21942       if (token->type != CPP_COLON
21943           || token->type == CPP_SCOPE)
21944         selector = cp_parser_objc_selector (parser);
21945
21946       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21947           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21948         {
21949           /* Detect if we have a unary selector.  */
21950           if (maybe_unary_selector_p)
21951             {
21952               sel_seq = selector;
21953               goto finish_selector;
21954             }
21955           else
21956             {
21957               cp_parser_error (parser, "expected %<:%>");
21958             }
21959         }
21960       maybe_unary_selector_p = false;
21961       token = cp_lexer_consume_token (parser->lexer);
21962
21963       if (token->type == CPP_SCOPE)
21964         {
21965           sel_seq
21966             = chainon (sel_seq,
21967                        build_tree_list (selector, NULL_TREE));
21968           sel_seq
21969             = chainon (sel_seq,
21970                        build_tree_list (NULL_TREE, NULL_TREE));
21971         }
21972       else
21973         sel_seq
21974           = chainon (sel_seq,
21975                      build_tree_list (selector, NULL_TREE));
21976
21977       token = cp_lexer_peek_token (parser->lexer);
21978     }
21979
21980  finish_selector:
21981   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21982
21983   return objc_build_selector_expr (loc, sel_seq);
21984 }
21985
21986 /* Parse a list of identifiers.
21987
21988    objc-identifier-list:
21989      identifier
21990      objc-identifier-list , identifier
21991
21992    Returns a TREE_LIST of identifier nodes.  */
21993
21994 static tree
21995 cp_parser_objc_identifier_list (cp_parser* parser)
21996 {
21997   tree identifier;
21998   tree list;
21999   cp_token *sep;
22000
22001   identifier = cp_parser_identifier (parser);
22002   if (identifier == error_mark_node)
22003     return error_mark_node;      
22004
22005   list = build_tree_list (NULL_TREE, identifier);
22006   sep = cp_lexer_peek_token (parser->lexer);
22007
22008   while (sep->type == CPP_COMMA)
22009     {
22010       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22011       identifier = cp_parser_identifier (parser);
22012       if (identifier == error_mark_node)
22013         return list;
22014
22015       list = chainon (list, build_tree_list (NULL_TREE,
22016                                              identifier));
22017       sep = cp_lexer_peek_token (parser->lexer);
22018     }
22019   
22020   return list;
22021 }
22022
22023 /* Parse an Objective-C alias declaration.
22024
22025    objc-alias-declaration:
22026      @compatibility_alias identifier identifier ;
22027
22028    This function registers the alias mapping with the Objective-C front end.
22029    It returns nothing.  */
22030
22031 static void
22032 cp_parser_objc_alias_declaration (cp_parser* parser)
22033 {
22034   tree alias, orig;
22035
22036   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22037   alias = cp_parser_identifier (parser);
22038   orig = cp_parser_identifier (parser);
22039   objc_declare_alias (alias, orig);
22040   cp_parser_consume_semicolon_at_end_of_statement (parser);
22041 }
22042
22043 /* Parse an Objective-C class forward-declaration.
22044
22045    objc-class-declaration:
22046      @class objc-identifier-list ;
22047
22048    The function registers the forward declarations with the Objective-C
22049    front end.  It returns nothing.  */
22050
22051 static void
22052 cp_parser_objc_class_declaration (cp_parser* parser)
22053 {
22054   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22055   while (true)
22056     {
22057       tree id;
22058       
22059       id = cp_parser_identifier (parser);
22060       if (id == error_mark_node)
22061         break;
22062       
22063       objc_declare_class (id);
22064
22065       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22066         cp_lexer_consume_token (parser->lexer);
22067       else
22068         break;
22069     }
22070   cp_parser_consume_semicolon_at_end_of_statement (parser);
22071 }
22072
22073 /* Parse a list of Objective-C protocol references.
22074
22075    objc-protocol-refs-opt:
22076      objc-protocol-refs [opt]
22077
22078    objc-protocol-refs:
22079      < objc-identifier-list >
22080
22081    Returns a TREE_LIST of identifiers, if any.  */
22082
22083 static tree
22084 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22085 {
22086   tree protorefs = NULL_TREE;
22087
22088   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22089     {
22090       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22091       protorefs = cp_parser_objc_identifier_list (parser);
22092       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22093     }
22094
22095   return protorefs;
22096 }
22097
22098 /* Parse a Objective-C visibility specification.  */
22099
22100 static void
22101 cp_parser_objc_visibility_spec (cp_parser* parser)
22102 {
22103   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22104
22105   switch (vis->keyword)
22106     {
22107     case RID_AT_PRIVATE:
22108       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22109       break;
22110     case RID_AT_PROTECTED:
22111       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22112       break;
22113     case RID_AT_PUBLIC:
22114       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22115       break;
22116     case RID_AT_PACKAGE:
22117       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22118       break;
22119     default:
22120       return;
22121     }
22122
22123   /* Eat '@private'/'@protected'/'@public'.  */
22124   cp_lexer_consume_token (parser->lexer);
22125 }
22126
22127 /* Parse an Objective-C method type.  Return 'true' if it is a class
22128    (+) method, and 'false' if it is an instance (-) method.  */
22129
22130 static inline bool
22131 cp_parser_objc_method_type (cp_parser* parser)
22132 {
22133   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22134     return true;
22135   else
22136     return false;
22137 }
22138
22139 /* Parse an Objective-C protocol qualifier.  */
22140
22141 static tree
22142 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
22143 {
22144   tree quals = NULL_TREE, node;
22145   cp_token *token = cp_lexer_peek_token (parser->lexer);
22146
22147   node = token->u.value;
22148
22149   while (node && TREE_CODE (node) == IDENTIFIER_NODE
22150          && (node == ridpointers [(int) RID_IN]
22151              || node == ridpointers [(int) RID_OUT]
22152              || node == ridpointers [(int) RID_INOUT]
22153              || node == ridpointers [(int) RID_BYCOPY]
22154              || node == ridpointers [(int) RID_BYREF]
22155              || node == ridpointers [(int) RID_ONEWAY]))
22156     {
22157       quals = tree_cons (NULL_TREE, node, quals);
22158       cp_lexer_consume_token (parser->lexer);
22159       token = cp_lexer_peek_token (parser->lexer);
22160       node = token->u.value;
22161     }
22162
22163   return quals;
22164 }
22165
22166 /* Parse an Objective-C typename.  */
22167
22168 static tree
22169 cp_parser_objc_typename (cp_parser* parser)
22170 {
22171   tree type_name = NULL_TREE;
22172
22173   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22174     {
22175       tree proto_quals, cp_type = NULL_TREE;
22176
22177       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22178       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22179
22180       /* An ObjC type name may consist of just protocol qualifiers, in which
22181          case the type shall default to 'id'.  */
22182       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22183         {
22184           cp_type = cp_parser_type_id (parser);
22185           
22186           /* If the type could not be parsed, an error has already
22187              been produced.  For error recovery, behave as if it had
22188              not been specified, which will use the default type
22189              'id'.  */
22190           if (cp_type == error_mark_node)
22191             {
22192               cp_type = NULL_TREE;
22193               /* We need to skip to the closing parenthesis as
22194                  cp_parser_type_id() does not seem to do it for
22195                  us.  */
22196               cp_parser_skip_to_closing_parenthesis (parser,
22197                                                      /*recovering=*/true,
22198                                                      /*or_comma=*/false,
22199                                                      /*consume_paren=*/false);
22200             }
22201         }
22202
22203       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22204       type_name = build_tree_list (proto_quals, cp_type);
22205     }
22206
22207   return type_name;
22208 }
22209
22210 /* Check to see if TYPE refers to an Objective-C selector name.  */
22211
22212 static bool
22213 cp_parser_objc_selector_p (enum cpp_ttype type)
22214 {
22215   return (type == CPP_NAME || type == CPP_KEYWORD
22216           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22217           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22218           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22219           || type == CPP_XOR || type == CPP_XOR_EQ);
22220 }
22221
22222 /* Parse an Objective-C selector.  */
22223
22224 static tree
22225 cp_parser_objc_selector (cp_parser* parser)
22226 {
22227   cp_token *token = cp_lexer_consume_token (parser->lexer);
22228
22229   if (!cp_parser_objc_selector_p (token->type))
22230     {
22231       error_at (token->location, "invalid Objective-C++ selector name");
22232       return error_mark_node;
22233     }
22234
22235   /* C++ operator names are allowed to appear in ObjC selectors.  */
22236   switch (token->type)
22237     {
22238     case CPP_AND_AND: return get_identifier ("and");
22239     case CPP_AND_EQ: return get_identifier ("and_eq");
22240     case CPP_AND: return get_identifier ("bitand");
22241     case CPP_OR: return get_identifier ("bitor");
22242     case CPP_COMPL: return get_identifier ("compl");
22243     case CPP_NOT: return get_identifier ("not");
22244     case CPP_NOT_EQ: return get_identifier ("not_eq");
22245     case CPP_OR_OR: return get_identifier ("or");
22246     case CPP_OR_EQ: return get_identifier ("or_eq");
22247     case CPP_XOR: return get_identifier ("xor");
22248     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22249     default: return token->u.value;
22250     }
22251 }
22252
22253 /* Parse an Objective-C params list.  */
22254
22255 static tree
22256 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22257 {
22258   tree params = NULL_TREE;
22259   bool maybe_unary_selector_p = true;
22260   cp_token *token = cp_lexer_peek_token (parser->lexer);
22261
22262   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22263     {
22264       tree selector = NULL_TREE, type_name, identifier;
22265       tree parm_attr = NULL_TREE;
22266
22267       if (token->keyword == RID_ATTRIBUTE)
22268         break;
22269
22270       if (token->type != CPP_COLON)
22271         selector = cp_parser_objc_selector (parser);
22272
22273       /* Detect if we have a unary selector.  */
22274       if (maybe_unary_selector_p
22275           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22276         {
22277           params = selector; /* Might be followed by attributes.  */
22278           break;
22279         }
22280
22281       maybe_unary_selector_p = false;
22282       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22283         {
22284           /* Something went quite wrong.  There should be a colon
22285              here, but there is not.  Stop parsing parameters.  */
22286           break;
22287         }
22288       type_name = cp_parser_objc_typename (parser);
22289       /* New ObjC allows attributes on parameters too.  */
22290       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22291         parm_attr = cp_parser_attributes_opt (parser);
22292       identifier = cp_parser_identifier (parser);
22293
22294       params
22295         = chainon (params,
22296                    objc_build_keyword_decl (selector,
22297                                             type_name,
22298                                             identifier,
22299                                             parm_attr));
22300
22301       token = cp_lexer_peek_token (parser->lexer);
22302     }
22303
22304   if (params == NULL_TREE)
22305     {
22306       cp_parser_error (parser, "objective-c++ method declaration is expected");
22307       return error_mark_node;
22308     }
22309
22310   /* We allow tail attributes for the method.  */
22311   if (token->keyword == RID_ATTRIBUTE)
22312     {
22313       *attributes = cp_parser_attributes_opt (parser);
22314       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22315           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22316         return params;
22317       cp_parser_error (parser, 
22318                        "method attributes must be specified at the end");
22319       return error_mark_node;
22320     }
22321
22322   if (params == NULL_TREE)
22323     {
22324       cp_parser_error (parser, "objective-c++ method declaration is expected");
22325       return error_mark_node;
22326     }
22327   return params;
22328 }
22329
22330 /* Parse the non-keyword Objective-C params.  */
22331
22332 static tree
22333 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22334                                        tree* attributes)
22335 {
22336   tree params = make_node (TREE_LIST);
22337   cp_token *token = cp_lexer_peek_token (parser->lexer);
22338   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22339
22340   while (token->type == CPP_COMMA)
22341     {
22342       cp_parameter_declarator *parmdecl;
22343       tree parm;
22344
22345       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22346       token = cp_lexer_peek_token (parser->lexer);
22347
22348       if (token->type == CPP_ELLIPSIS)
22349         {
22350           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22351           *ellipsisp = true;
22352           token = cp_lexer_peek_token (parser->lexer);
22353           break;
22354         }
22355
22356       /* TODO: parse attributes for tail parameters.  */
22357       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22358       parm = grokdeclarator (parmdecl->declarator,
22359                              &parmdecl->decl_specifiers,
22360                              PARM, /*initialized=*/0,
22361                              /*attrlist=*/NULL);
22362
22363       chainon (params, build_tree_list (NULL_TREE, parm));
22364       token = cp_lexer_peek_token (parser->lexer);
22365     }
22366
22367   /* We allow tail attributes for the method.  */
22368   if (token->keyword == RID_ATTRIBUTE)
22369     {
22370       if (*attributes == NULL_TREE)
22371         {
22372           *attributes = cp_parser_attributes_opt (parser);
22373           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22374               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22375             return params;
22376         }
22377       else        
22378         /* We have an error, but parse the attributes, so that we can 
22379            carry on.  */
22380         *attributes = cp_parser_attributes_opt (parser);
22381
22382       cp_parser_error (parser, 
22383                        "method attributes must be specified at the end");
22384       return error_mark_node;
22385     }
22386
22387   return params;
22388 }
22389
22390 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22391
22392 static void
22393 cp_parser_objc_interstitial_code (cp_parser* parser)
22394 {
22395   cp_token *token = cp_lexer_peek_token (parser->lexer);
22396
22397   /* If the next token is `extern' and the following token is a string
22398      literal, then we have a linkage specification.  */
22399   if (token->keyword == RID_EXTERN
22400       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22401     cp_parser_linkage_specification (parser);
22402   /* Handle #pragma, if any.  */
22403   else if (token->type == CPP_PRAGMA)
22404     cp_parser_pragma (parser, pragma_external);
22405   /* Allow stray semicolons.  */
22406   else if (token->type == CPP_SEMICOLON)
22407     cp_lexer_consume_token (parser->lexer);
22408   /* Mark methods as optional or required, when building protocols.  */
22409   else if (token->keyword == RID_AT_OPTIONAL)
22410     {
22411       cp_lexer_consume_token (parser->lexer);
22412       objc_set_method_opt (true);
22413     }
22414   else if (token->keyword == RID_AT_REQUIRED)
22415     {
22416       cp_lexer_consume_token (parser->lexer);
22417       objc_set_method_opt (false);
22418     }
22419   else if (token->keyword == RID_NAMESPACE)
22420     cp_parser_namespace_definition (parser);
22421   /* Other stray characters must generate errors.  */
22422   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22423     {
22424       cp_lexer_consume_token (parser->lexer);
22425       error ("stray %qs between Objective-C++ methods",
22426              token->type == CPP_OPEN_BRACE ? "{" : "}");
22427     }
22428   /* Finally, try to parse a block-declaration, or a function-definition.  */
22429   else
22430     cp_parser_block_declaration (parser, /*statement_p=*/false);
22431 }
22432
22433 /* Parse a method signature.  */
22434
22435 static tree
22436 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22437 {
22438   tree rettype, kwdparms, optparms;
22439   bool ellipsis = false;
22440   bool is_class_method;
22441
22442   is_class_method = cp_parser_objc_method_type (parser);
22443   rettype = cp_parser_objc_typename (parser);
22444   *attributes = NULL_TREE;
22445   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22446   if (kwdparms == error_mark_node)
22447     return error_mark_node;
22448   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22449   if (optparms == error_mark_node)
22450     return error_mark_node;
22451
22452   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22453 }
22454
22455 static bool
22456 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22457 {
22458   tree tattr;  
22459   cp_lexer_save_tokens (parser->lexer);
22460   tattr = cp_parser_attributes_opt (parser);
22461   gcc_assert (tattr) ;
22462   
22463   /* If the attributes are followed by a method introducer, this is not allowed.
22464      Dump the attributes and flag the situation.  */
22465   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22466       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22467     return true;
22468
22469   /* Otherwise, the attributes introduce some interstitial code, possibly so
22470      rewind to allow that check.  */
22471   cp_lexer_rollback_tokens (parser->lexer);
22472   return false;  
22473 }
22474
22475 /* Parse an Objective-C method prototype list.  */
22476
22477 static void
22478 cp_parser_objc_method_prototype_list (cp_parser* parser)
22479 {
22480   cp_token *token = cp_lexer_peek_token (parser->lexer);
22481
22482   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22483     {
22484       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22485         {
22486           tree attributes, sig;
22487           bool is_class_method;
22488           if (token->type == CPP_PLUS)
22489             is_class_method = true;
22490           else
22491             is_class_method = false;
22492           sig = cp_parser_objc_method_signature (parser, &attributes);
22493           if (sig == error_mark_node)
22494             {
22495               cp_parser_skip_to_end_of_block_or_statement (parser);
22496               token = cp_lexer_peek_token (parser->lexer);
22497               continue;
22498             }
22499           objc_add_method_declaration (is_class_method, sig, attributes);
22500           cp_parser_consume_semicolon_at_end_of_statement (parser);
22501         }
22502       else if (token->keyword == RID_AT_PROPERTY)
22503         cp_parser_objc_at_property_declaration (parser);
22504       else if (token->keyword == RID_ATTRIBUTE 
22505                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22506         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22507                     OPT_Wattributes, 
22508                     "prefix attributes are ignored for methods");
22509       else
22510         /* Allow for interspersed non-ObjC++ code.  */
22511         cp_parser_objc_interstitial_code (parser);
22512
22513       token = cp_lexer_peek_token (parser->lexer);
22514     }
22515
22516   if (token->type != CPP_EOF)
22517     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22518   else
22519     cp_parser_error (parser, "expected %<@end%>");
22520
22521   objc_finish_interface ();
22522 }
22523
22524 /* Parse an Objective-C method definition list.  */
22525
22526 static void
22527 cp_parser_objc_method_definition_list (cp_parser* parser)
22528 {
22529   cp_token *token = cp_lexer_peek_token (parser->lexer);
22530
22531   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22532     {
22533       tree meth;
22534
22535       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22536         {
22537           cp_token *ptk;
22538           tree sig, attribute;
22539           bool is_class_method;
22540           if (token->type == CPP_PLUS)
22541             is_class_method = true;
22542           else
22543             is_class_method = false;
22544           push_deferring_access_checks (dk_deferred);
22545           sig = cp_parser_objc_method_signature (parser, &attribute);
22546           if (sig == error_mark_node)
22547             {
22548               cp_parser_skip_to_end_of_block_or_statement (parser);
22549               token = cp_lexer_peek_token (parser->lexer);
22550               continue;
22551             }
22552           objc_start_method_definition (is_class_method, sig, attribute,
22553                                         NULL_TREE);
22554
22555           /* For historical reasons, we accept an optional semicolon.  */
22556           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22557             cp_lexer_consume_token (parser->lexer);
22558
22559           ptk = cp_lexer_peek_token (parser->lexer);
22560           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22561                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22562             {
22563               perform_deferred_access_checks ();
22564               stop_deferring_access_checks ();
22565               meth = cp_parser_function_definition_after_declarator (parser,
22566                                                                      false);
22567               pop_deferring_access_checks ();
22568               objc_finish_method_definition (meth);
22569             }
22570         }
22571       /* The following case will be removed once @synthesize is
22572          completely implemented.  */
22573       else if (token->keyword == RID_AT_PROPERTY)
22574         cp_parser_objc_at_property_declaration (parser);
22575       else if (token->keyword == RID_AT_SYNTHESIZE)
22576         cp_parser_objc_at_synthesize_declaration (parser);
22577       else if (token->keyword == RID_AT_DYNAMIC)
22578         cp_parser_objc_at_dynamic_declaration (parser);
22579       else if (token->keyword == RID_ATTRIBUTE 
22580                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22581         warning_at (token->location, OPT_Wattributes,
22582                     "prefix attributes are ignored for methods");
22583       else
22584         /* Allow for interspersed non-ObjC++ code.  */
22585         cp_parser_objc_interstitial_code (parser);
22586
22587       token = cp_lexer_peek_token (parser->lexer);
22588     }
22589
22590   if (token->type != CPP_EOF)
22591     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22592   else
22593     cp_parser_error (parser, "expected %<@end%>");
22594
22595   objc_finish_implementation ();
22596 }
22597
22598 /* Parse Objective-C ivars.  */
22599
22600 static void
22601 cp_parser_objc_class_ivars (cp_parser* parser)
22602 {
22603   cp_token *token = cp_lexer_peek_token (parser->lexer);
22604
22605   if (token->type != CPP_OPEN_BRACE)
22606     return;     /* No ivars specified.  */
22607
22608   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22609   token = cp_lexer_peek_token (parser->lexer);
22610
22611   while (token->type != CPP_CLOSE_BRACE 
22612         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22613     {
22614       cp_decl_specifier_seq declspecs;
22615       int decl_class_or_enum_p;
22616       tree prefix_attributes;
22617
22618       cp_parser_objc_visibility_spec (parser);
22619
22620       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22621         break;
22622
22623       cp_parser_decl_specifier_seq (parser,
22624                                     CP_PARSER_FLAGS_OPTIONAL,
22625                                     &declspecs,
22626                                     &decl_class_or_enum_p);
22627
22628       /* auto, register, static, extern, mutable.  */
22629       if (declspecs.storage_class != sc_none)
22630         {
22631           cp_parser_error (parser, "invalid type for instance variable");         
22632           declspecs.storage_class = sc_none;
22633         }
22634
22635       /* __thread.  */
22636       if (declspecs.specs[(int) ds_thread])
22637         {
22638           cp_parser_error (parser, "invalid type for instance variable");
22639           declspecs.specs[(int) ds_thread] = 0;
22640         }
22641       
22642       /* typedef.  */
22643       if (declspecs.specs[(int) ds_typedef])
22644         {
22645           cp_parser_error (parser, "invalid type for instance variable");
22646           declspecs.specs[(int) ds_typedef] = 0;
22647         }
22648
22649       prefix_attributes = declspecs.attributes;
22650       declspecs.attributes = NULL_TREE;
22651
22652       /* Keep going until we hit the `;' at the end of the
22653          declaration.  */
22654       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22655         {
22656           tree width = NULL_TREE, attributes, first_attribute, decl;
22657           cp_declarator *declarator = NULL;
22658           int ctor_dtor_or_conv_p;
22659
22660           /* Check for a (possibly unnamed) bitfield declaration.  */
22661           token = cp_lexer_peek_token (parser->lexer);
22662           if (token->type == CPP_COLON)
22663             goto eat_colon;
22664
22665           if (token->type == CPP_NAME
22666               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22667                   == CPP_COLON))
22668             {
22669               /* Get the name of the bitfield.  */
22670               declarator = make_id_declarator (NULL_TREE,
22671                                                cp_parser_identifier (parser),
22672                                                sfk_none);
22673
22674              eat_colon:
22675               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22676               /* Get the width of the bitfield.  */
22677               width
22678                 = cp_parser_constant_expression (parser,
22679                                                  /*allow_non_constant=*/false,
22680                                                  NULL);
22681             }
22682           else
22683             {
22684               /* Parse the declarator.  */
22685               declarator
22686                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22687                                         &ctor_dtor_or_conv_p,
22688                                         /*parenthesized_p=*/NULL,
22689                                         /*member_p=*/false);
22690             }
22691
22692           /* Look for attributes that apply to the ivar.  */
22693           attributes = cp_parser_attributes_opt (parser);
22694           /* Remember which attributes are prefix attributes and
22695              which are not.  */
22696           first_attribute = attributes;
22697           /* Combine the attributes.  */
22698           attributes = chainon (prefix_attributes, attributes);
22699
22700           if (width)
22701               /* Create the bitfield declaration.  */
22702               decl = grokbitfield (declarator, &declspecs,
22703                                    width,
22704                                    attributes);
22705           else
22706             decl = grokfield (declarator, &declspecs,
22707                               NULL_TREE, /*init_const_expr_p=*/false,
22708                               NULL_TREE, attributes);
22709
22710           /* Add the instance variable.  */
22711           if (decl != error_mark_node && decl != NULL_TREE)
22712             objc_add_instance_variable (decl);
22713
22714           /* Reset PREFIX_ATTRIBUTES.  */
22715           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22716             attributes = TREE_CHAIN (attributes);
22717           if (attributes)
22718             TREE_CHAIN (attributes) = NULL_TREE;
22719
22720           token = cp_lexer_peek_token (parser->lexer);
22721
22722           if (token->type == CPP_COMMA)
22723             {
22724               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22725               continue;
22726             }
22727           break;
22728         }
22729
22730       cp_parser_consume_semicolon_at_end_of_statement (parser);
22731       token = cp_lexer_peek_token (parser->lexer);
22732     }
22733
22734   if (token->keyword == RID_AT_END)
22735     cp_parser_error (parser, "expected %<}%>");
22736
22737   /* Do not consume the RID_AT_END, so it will be read again as terminating
22738      the @interface of @implementation.  */ 
22739   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22740     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22741     
22742   /* For historical reasons, we accept an optional semicolon.  */
22743   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22744     cp_lexer_consume_token (parser->lexer);
22745 }
22746
22747 /* Parse an Objective-C protocol declaration.  */
22748
22749 static void
22750 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22751 {
22752   tree proto, protorefs;
22753   cp_token *tok;
22754
22755   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22756   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22757     {
22758       tok = cp_lexer_peek_token (parser->lexer);
22759       error_at (tok->location, "identifier expected after %<@protocol%>");
22760       cp_parser_consume_semicolon_at_end_of_statement (parser);
22761       return;
22762     }
22763
22764   /* See if we have a forward declaration or a definition.  */
22765   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22766
22767   /* Try a forward declaration first.  */
22768   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22769     {
22770       while (true)
22771         {
22772           tree id;
22773           
22774           id = cp_parser_identifier (parser);
22775           if (id == error_mark_node)
22776             break;
22777           
22778           objc_declare_protocol (id, attributes);
22779           
22780           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22781             cp_lexer_consume_token (parser->lexer);
22782           else
22783             break;
22784         }
22785       cp_parser_consume_semicolon_at_end_of_statement (parser);
22786     }
22787
22788   /* Ok, we got a full-fledged definition (or at least should).  */
22789   else
22790     {
22791       proto = cp_parser_identifier (parser);
22792       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22793       objc_start_protocol (proto, protorefs, attributes);
22794       cp_parser_objc_method_prototype_list (parser);
22795     }
22796 }
22797
22798 /* Parse an Objective-C superclass or category.  */
22799
22800 static void
22801 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22802                                        bool iface_p,
22803                                        tree *super,
22804                                        tree *categ, bool *is_class_extension)
22805 {
22806   cp_token *next = cp_lexer_peek_token (parser->lexer);
22807
22808   *super = *categ = NULL_TREE;
22809   *is_class_extension = false;
22810   if (next->type == CPP_COLON)
22811     {
22812       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22813       *super = cp_parser_identifier (parser);
22814     }
22815   else if (next->type == CPP_OPEN_PAREN)
22816     {
22817       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22818
22819       /* If there is no category name, and this is an @interface, we
22820          have a class extension.  */
22821       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22822         {
22823           *categ = NULL_TREE;
22824           *is_class_extension = true;
22825         }
22826       else
22827         *categ = cp_parser_identifier (parser);
22828
22829       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22830     }
22831 }
22832
22833 /* Parse an Objective-C class interface.  */
22834
22835 static void
22836 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22837 {
22838   tree name, super, categ, protos;
22839   bool is_class_extension;
22840
22841   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22842   name = cp_parser_identifier (parser);
22843   if (name == error_mark_node)
22844     {
22845       /* It's hard to recover because even if valid @interface stuff
22846          is to follow, we can't compile it (or validate it) if we
22847          don't even know which class it refers to.  Let's assume this
22848          was a stray '@interface' token in the stream and skip it.
22849       */
22850       return;
22851     }
22852   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22853                                          &is_class_extension);
22854   protos = cp_parser_objc_protocol_refs_opt (parser);
22855
22856   /* We have either a class or a category on our hands.  */
22857   if (categ || is_class_extension)
22858     objc_start_category_interface (name, categ, protos, attributes);
22859   else
22860     {
22861       objc_start_class_interface (name, super, protos, attributes);
22862       /* Handle instance variable declarations, if any.  */
22863       cp_parser_objc_class_ivars (parser);
22864       objc_continue_interface ();
22865     }
22866
22867   cp_parser_objc_method_prototype_list (parser);
22868 }
22869
22870 /* Parse an Objective-C class implementation.  */
22871
22872 static void
22873 cp_parser_objc_class_implementation (cp_parser* parser)
22874 {
22875   tree name, super, categ;
22876   bool is_class_extension;
22877
22878   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22879   name = cp_parser_identifier (parser);
22880   if (name == error_mark_node)
22881     {
22882       /* It's hard to recover because even if valid @implementation
22883          stuff is to follow, we can't compile it (or validate it) if
22884          we don't even know which class it refers to.  Let's assume
22885          this was a stray '@implementation' token in the stream and
22886          skip it.
22887       */
22888       return;
22889     }
22890   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22891                                          &is_class_extension);
22892
22893   /* We have either a class or a category on our hands.  */
22894   if (categ)
22895     objc_start_category_implementation (name, categ);
22896   else
22897     {
22898       objc_start_class_implementation (name, super);
22899       /* Handle instance variable declarations, if any.  */
22900       cp_parser_objc_class_ivars (parser);
22901       objc_continue_implementation ();
22902     }
22903
22904   cp_parser_objc_method_definition_list (parser);
22905 }
22906
22907 /* Consume the @end token and finish off the implementation.  */
22908
22909 static void
22910 cp_parser_objc_end_implementation (cp_parser* parser)
22911 {
22912   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22913   objc_finish_implementation ();
22914 }
22915
22916 /* Parse an Objective-C declaration.  */
22917
22918 static void
22919 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22920 {
22921   /* Try to figure out what kind of declaration is present.  */
22922   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22923
22924   if (attributes)
22925     switch (kwd->keyword)
22926       {
22927         case RID_AT_ALIAS:
22928         case RID_AT_CLASS:
22929         case RID_AT_END:
22930           error_at (kwd->location, "attributes may not be specified before"
22931                     " the %<@%D%> Objective-C++ keyword",
22932                     kwd->u.value);
22933           attributes = NULL;
22934           break;
22935         case RID_AT_IMPLEMENTATION:
22936           warning_at (kwd->location, OPT_Wattributes,
22937                       "prefix attributes are ignored before %<@%D%>",
22938                       kwd->u.value);
22939           attributes = NULL;
22940         default:
22941           break;
22942       }
22943
22944   switch (kwd->keyword)
22945     {
22946     case RID_AT_ALIAS:
22947       cp_parser_objc_alias_declaration (parser);
22948       break;
22949     case RID_AT_CLASS:
22950       cp_parser_objc_class_declaration (parser);
22951       break;
22952     case RID_AT_PROTOCOL:
22953       cp_parser_objc_protocol_declaration (parser, attributes);
22954       break;
22955     case RID_AT_INTERFACE:
22956       cp_parser_objc_class_interface (parser, attributes);
22957       break;
22958     case RID_AT_IMPLEMENTATION:
22959       cp_parser_objc_class_implementation (parser);
22960       break;
22961     case RID_AT_END:
22962       cp_parser_objc_end_implementation (parser);
22963       break;
22964     default:
22965       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22966                 kwd->u.value);
22967       cp_parser_skip_to_end_of_block_or_statement (parser);
22968     }
22969 }
22970
22971 /* Parse an Objective-C try-catch-finally statement.
22972
22973    objc-try-catch-finally-stmt:
22974      @try compound-statement objc-catch-clause-seq [opt]
22975        objc-finally-clause [opt]
22976
22977    objc-catch-clause-seq:
22978      objc-catch-clause objc-catch-clause-seq [opt]
22979
22980    objc-catch-clause:
22981      @catch ( objc-exception-declaration ) compound-statement
22982
22983    objc-finally-clause:
22984      @finally compound-statement
22985
22986    objc-exception-declaration:
22987      parameter-declaration
22988      '...'
22989
22990    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22991
22992    Returns NULL_TREE.
22993
22994    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22995    for C.  Keep them in sync.  */   
22996
22997 static tree
22998 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22999 {
23000   location_t location;
23001   tree stmt;
23002
23003   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23004   location = cp_lexer_peek_token (parser->lexer)->location;
23005   objc_maybe_warn_exceptions (location);
23006   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23007      node, lest it get absorbed into the surrounding block.  */
23008   stmt = push_stmt_list ();
23009   cp_parser_compound_statement (parser, NULL, false, false);
23010   objc_begin_try_stmt (location, pop_stmt_list (stmt));
23011
23012   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23013     {
23014       cp_parameter_declarator *parm;
23015       tree parameter_declaration = error_mark_node;
23016       bool seen_open_paren = false;
23017
23018       cp_lexer_consume_token (parser->lexer);
23019       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23020         seen_open_paren = true;
23021       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23022         {
23023           /* We have "@catch (...)" (where the '...' are literally
23024              what is in the code).  Skip the '...'.
23025              parameter_declaration is set to NULL_TREE, and
23026              objc_being_catch_clauses() knows that that means
23027              '...'.  */
23028           cp_lexer_consume_token (parser->lexer);
23029           parameter_declaration = NULL_TREE;
23030         }
23031       else
23032         {
23033           /* We have "@catch (NSException *exception)" or something
23034              like that.  Parse the parameter declaration.  */
23035           parm = cp_parser_parameter_declaration (parser, false, NULL);
23036           if (parm == NULL)
23037             parameter_declaration = error_mark_node;
23038           else
23039             parameter_declaration = grokdeclarator (parm->declarator,
23040                                                     &parm->decl_specifiers,
23041                                                     PARM, /*initialized=*/0,
23042                                                     /*attrlist=*/NULL);
23043         }
23044       if (seen_open_paren)
23045         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23046       else
23047         {
23048           /* If there was no open parenthesis, we are recovering from
23049              an error, and we are trying to figure out what mistake
23050              the user has made.  */
23051
23052           /* If there is an immediate closing parenthesis, the user
23053              probably forgot the opening one (ie, they typed "@catch
23054              NSException *e)".  Parse the closing parenthesis and keep
23055              going.  */
23056           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23057             cp_lexer_consume_token (parser->lexer);
23058           
23059           /* If these is no immediate closing parenthesis, the user
23060              probably doesn't know that parenthesis are required at
23061              all (ie, they typed "@catch NSException *e").  So, just
23062              forget about the closing parenthesis and keep going.  */
23063         }
23064       objc_begin_catch_clause (parameter_declaration);
23065       cp_parser_compound_statement (parser, NULL, false, false);
23066       objc_finish_catch_clause ();
23067     }
23068   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23069     {
23070       cp_lexer_consume_token (parser->lexer);
23071       location = cp_lexer_peek_token (parser->lexer)->location;
23072       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23073          node, lest it get absorbed into the surrounding block.  */
23074       stmt = push_stmt_list ();
23075       cp_parser_compound_statement (parser, NULL, false, false);
23076       objc_build_finally_clause (location, pop_stmt_list (stmt));
23077     }
23078
23079   return objc_finish_try_stmt ();
23080 }
23081
23082 /* Parse an Objective-C synchronized statement.
23083
23084    objc-synchronized-stmt:
23085      @synchronized ( expression ) compound-statement
23086
23087    Returns NULL_TREE.  */
23088
23089 static tree
23090 cp_parser_objc_synchronized_statement (cp_parser *parser)
23091 {
23092   location_t location;
23093   tree lock, stmt;
23094
23095   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23096
23097   location = cp_lexer_peek_token (parser->lexer)->location;
23098   objc_maybe_warn_exceptions (location);
23099   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23100   lock = cp_parser_expression (parser, false, NULL);
23101   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23102
23103   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23104      node, lest it get absorbed into the surrounding block.  */
23105   stmt = push_stmt_list ();
23106   cp_parser_compound_statement (parser, NULL, false, false);
23107
23108   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23109 }
23110
23111 /* Parse an Objective-C throw statement.
23112
23113    objc-throw-stmt:
23114      @throw assignment-expression [opt] ;
23115
23116    Returns a constructed '@throw' statement.  */
23117
23118 static tree
23119 cp_parser_objc_throw_statement (cp_parser *parser)
23120 {
23121   tree expr = NULL_TREE;
23122   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23123
23124   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23125
23126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23127     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23128
23129   cp_parser_consume_semicolon_at_end_of_statement (parser);
23130
23131   return objc_build_throw_stmt (loc, expr);
23132 }
23133
23134 /* Parse an Objective-C statement.  */
23135
23136 static tree
23137 cp_parser_objc_statement (cp_parser * parser)
23138 {
23139   /* Try to figure out what kind of declaration is present.  */
23140   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23141
23142   switch (kwd->keyword)
23143     {
23144     case RID_AT_TRY:
23145       return cp_parser_objc_try_catch_finally_statement (parser);
23146     case RID_AT_SYNCHRONIZED:
23147       return cp_parser_objc_synchronized_statement (parser);
23148     case RID_AT_THROW:
23149       return cp_parser_objc_throw_statement (parser);
23150     default:
23151       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23152                kwd->u.value);
23153       cp_parser_skip_to_end_of_block_or_statement (parser);
23154     }
23155
23156   return error_mark_node;
23157 }
23158
23159 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
23160    look ahead to see if an objc keyword follows the attributes.  This
23161    is to detect the use of prefix attributes on ObjC @interface and 
23162    @protocol.  */
23163
23164 static bool
23165 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23166 {
23167   cp_lexer_save_tokens (parser->lexer);
23168   *attrib = cp_parser_attributes_opt (parser);
23169   gcc_assert (*attrib);
23170   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23171     {
23172       cp_lexer_commit_tokens (parser->lexer);
23173       return true;
23174     }
23175   cp_lexer_rollback_tokens (parser->lexer);
23176   return false;  
23177 }
23178
23179 /* This routine is a minimal replacement for
23180    c_parser_struct_declaration () used when parsing the list of
23181    types/names or ObjC++ properties.  For example, when parsing the
23182    code
23183
23184    @property (readonly) int a, b, c;
23185
23186    this function is responsible for parsing "int a, int b, int c" and
23187    returning the declarations as CHAIN of DECLs.
23188
23189    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23190    similar parsing.  */
23191 static tree
23192 cp_parser_objc_struct_declaration (cp_parser *parser)
23193 {
23194   tree decls = NULL_TREE;
23195   cp_decl_specifier_seq declspecs;
23196   int decl_class_or_enum_p;
23197   tree prefix_attributes;
23198
23199   cp_parser_decl_specifier_seq (parser,
23200                                 CP_PARSER_FLAGS_NONE,
23201                                 &declspecs,
23202                                 &decl_class_or_enum_p);
23203
23204   if (declspecs.type == error_mark_node)
23205     return error_mark_node;
23206
23207   /* auto, register, static, extern, mutable.  */
23208   if (declspecs.storage_class != sc_none)
23209     {
23210       cp_parser_error (parser, "invalid type for property");
23211       declspecs.storage_class = sc_none;
23212     }
23213   
23214   /* __thread.  */
23215   if (declspecs.specs[(int) ds_thread])
23216     {
23217       cp_parser_error (parser, "invalid type for property");
23218       declspecs.specs[(int) ds_thread] = 0;
23219     }
23220   
23221   /* typedef.  */
23222   if (declspecs.specs[(int) ds_typedef])
23223     {
23224       cp_parser_error (parser, "invalid type for property");
23225       declspecs.specs[(int) ds_typedef] = 0;
23226     }
23227
23228   prefix_attributes = declspecs.attributes;
23229   declspecs.attributes = NULL_TREE;
23230
23231   /* Keep going until we hit the `;' at the end of the declaration. */
23232   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23233     {
23234       tree attributes, first_attribute, decl;
23235       cp_declarator *declarator;
23236       cp_token *token;
23237
23238       /* Parse the declarator.  */
23239       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23240                                          NULL, NULL, false);
23241
23242       /* Look for attributes that apply to the ivar.  */
23243       attributes = cp_parser_attributes_opt (parser);
23244       /* Remember which attributes are prefix attributes and
23245          which are not.  */
23246       first_attribute = attributes;
23247       /* Combine the attributes.  */
23248       attributes = chainon (prefix_attributes, attributes);
23249       
23250       decl = grokfield (declarator, &declspecs,
23251                         NULL_TREE, /*init_const_expr_p=*/false,
23252                         NULL_TREE, attributes);
23253
23254       if (decl == error_mark_node || decl == NULL_TREE)
23255         return error_mark_node;
23256       
23257       /* Reset PREFIX_ATTRIBUTES.  */
23258       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23259         attributes = TREE_CHAIN (attributes);
23260       if (attributes)
23261         TREE_CHAIN (attributes) = NULL_TREE;
23262
23263       DECL_CHAIN (decl) = decls;
23264       decls = decl;
23265
23266       token = cp_lexer_peek_token (parser->lexer);
23267       if (token->type == CPP_COMMA)
23268         {
23269           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23270           continue;
23271         }
23272       else
23273         break;
23274     }
23275   return decls;
23276 }
23277
23278 /* Parse an Objective-C @property declaration.  The syntax is:
23279
23280    objc-property-declaration:
23281      '@property' objc-property-attributes[opt] struct-declaration ;
23282
23283    objc-property-attributes:
23284     '(' objc-property-attribute-list ')'
23285
23286    objc-property-attribute-list:
23287      objc-property-attribute
23288      objc-property-attribute-list, objc-property-attribute
23289
23290    objc-property-attribute
23291      'getter' = identifier
23292      'setter' = identifier
23293      'readonly'
23294      'readwrite'
23295      'assign'
23296      'retain'
23297      'copy'
23298      'nonatomic'
23299
23300   For example:
23301     @property NSString *name;
23302     @property (readonly) id object;
23303     @property (retain, nonatomic, getter=getTheName) id name;
23304     @property int a, b, c;
23305
23306    PS: This function is identical to
23307    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23308 static void 
23309 cp_parser_objc_at_property_declaration (cp_parser *parser)
23310 {
23311   /* The following variables hold the attributes of the properties as
23312      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23313      seen.  When we see an attribute, we set them to 'true' (if they
23314      are boolean properties) or to the identifier (if they have an
23315      argument, ie, for getter and setter).  Note that here we only
23316      parse the list of attributes, check the syntax and accumulate the
23317      attributes that we find.  objc_add_property_declaration() will
23318      then process the information.  */
23319   bool property_assign = false;
23320   bool property_copy = false;
23321   tree property_getter_ident = NULL_TREE;
23322   bool property_nonatomic = false;
23323   bool property_readonly = false;
23324   bool property_readwrite = false;
23325   bool property_retain = false;
23326   tree property_setter_ident = NULL_TREE;
23327
23328   /* 'properties' is the list of properties that we read.  Usually a
23329      single one, but maybe more (eg, in "@property int a, b, c;" there
23330      are three).  */
23331   tree properties;
23332   location_t loc;
23333
23334   loc = cp_lexer_peek_token (parser->lexer)->location;
23335
23336   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23337
23338   /* Parse the optional attribute list...  */
23339   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23340     {
23341       /* Eat the '('.  */
23342       cp_lexer_consume_token (parser->lexer);
23343
23344       while (true)
23345         {
23346           bool syntax_error = false;
23347           cp_token *token = cp_lexer_peek_token (parser->lexer);
23348           enum rid keyword;
23349
23350           if (token->type != CPP_NAME)
23351             {
23352               cp_parser_error (parser, "expected identifier");
23353               break;
23354             }
23355           keyword = C_RID_CODE (token->u.value);
23356           cp_lexer_consume_token (parser->lexer);
23357           switch (keyword)
23358             {
23359             case RID_ASSIGN:    property_assign = true;    break;
23360             case RID_COPY:      property_copy = true;      break;
23361             case RID_NONATOMIC: property_nonatomic = true; break;
23362             case RID_READONLY:  property_readonly = true;  break;
23363             case RID_READWRITE: property_readwrite = true; break;
23364             case RID_RETAIN:    property_retain = true;    break;
23365
23366             case RID_GETTER:
23367             case RID_SETTER:
23368               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23369                 {
23370                   if (keyword == RID_GETTER)
23371                     cp_parser_error (parser,
23372                                      "missing %<=%> (after %<getter%> attribute)");
23373                   else
23374                     cp_parser_error (parser,
23375                                      "missing %<=%> (after %<setter%> attribute)");
23376                   syntax_error = true;
23377                   break;
23378                 }
23379               cp_lexer_consume_token (parser->lexer); /* eat the = */
23380               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23381                 {
23382                   cp_parser_error (parser, "expected identifier");
23383                   syntax_error = true;
23384                   break;
23385                 }
23386               if (keyword == RID_SETTER)
23387                 {
23388                   if (property_setter_ident != NULL_TREE)
23389                     {
23390                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23391                       cp_lexer_consume_token (parser->lexer);
23392                     }
23393                   else
23394                     property_setter_ident = cp_parser_objc_selector (parser);
23395                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23396                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23397                   else
23398                     cp_lexer_consume_token (parser->lexer);
23399                 }
23400               else
23401                 {
23402                   if (property_getter_ident != NULL_TREE)
23403                     {
23404                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23405                       cp_lexer_consume_token (parser->lexer);
23406                     }
23407                   else
23408                     property_getter_ident = cp_parser_objc_selector (parser);
23409                 }
23410               break;
23411             default:
23412               cp_parser_error (parser, "unknown property attribute");
23413               syntax_error = true;
23414               break;
23415             }
23416
23417           if (syntax_error)
23418             break;
23419
23420           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23421             cp_lexer_consume_token (parser->lexer);
23422           else
23423             break;
23424         }
23425
23426       /* FIXME: "@property (setter, assign);" will generate a spurious
23427          "error: expected â€˜)’ before â€˜,’ token".  This is because
23428          cp_parser_require, unlike the C counterpart, will produce an
23429          error even if we are in error recovery.  */
23430       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23431         {
23432           cp_parser_skip_to_closing_parenthesis (parser,
23433                                                  /*recovering=*/true,
23434                                                  /*or_comma=*/false,
23435                                                  /*consume_paren=*/true);
23436         }
23437     }
23438
23439   /* ... and the property declaration(s).  */
23440   properties = cp_parser_objc_struct_declaration (parser);
23441
23442   if (properties == error_mark_node)
23443     {
23444       cp_parser_skip_to_end_of_statement (parser);
23445       /* If the next token is now a `;', consume it.  */
23446       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23447         cp_lexer_consume_token (parser->lexer);
23448       return;
23449     }
23450
23451   if (properties == NULL_TREE)
23452     cp_parser_error (parser, "expected identifier");
23453   else
23454     {
23455       /* Comma-separated properties are chained together in
23456          reverse order; add them one by one.  */
23457       properties = nreverse (properties);
23458       
23459       for (; properties; properties = TREE_CHAIN (properties))
23460         objc_add_property_declaration (loc, copy_node (properties),
23461                                        property_readonly, property_readwrite,
23462                                        property_assign, property_retain,
23463                                        property_copy, property_nonatomic,
23464                                        property_getter_ident, property_setter_ident);
23465     }
23466   
23467   cp_parser_consume_semicolon_at_end_of_statement (parser);
23468 }
23469
23470 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23471
23472    objc-synthesize-declaration:
23473      @synthesize objc-synthesize-identifier-list ;
23474
23475    objc-synthesize-identifier-list:
23476      objc-synthesize-identifier
23477      objc-synthesize-identifier-list, objc-synthesize-identifier
23478
23479    objc-synthesize-identifier
23480      identifier
23481      identifier = identifier
23482
23483   For example:
23484     @synthesize MyProperty;
23485     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23486
23487   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23488   for C.  Keep them in sync.
23489 */
23490 static void 
23491 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23492 {
23493   tree list = NULL_TREE;
23494   location_t loc;
23495   loc = cp_lexer_peek_token (parser->lexer)->location;
23496
23497   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23498   while (true)
23499     {
23500       tree property, ivar;
23501       property = cp_parser_identifier (parser);
23502       if (property == error_mark_node)
23503         {
23504           cp_parser_consume_semicolon_at_end_of_statement (parser);
23505           return;
23506         }
23507       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23508         {
23509           cp_lexer_consume_token (parser->lexer);
23510           ivar = cp_parser_identifier (parser);
23511           if (ivar == error_mark_node)
23512             {
23513               cp_parser_consume_semicolon_at_end_of_statement (parser);
23514               return;
23515             }
23516         }
23517       else
23518         ivar = NULL_TREE;
23519       list = chainon (list, build_tree_list (ivar, property));
23520       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23521         cp_lexer_consume_token (parser->lexer);
23522       else
23523         break;
23524     }
23525   cp_parser_consume_semicolon_at_end_of_statement (parser);
23526   objc_add_synthesize_declaration (loc, list);
23527 }
23528
23529 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23530
23531    objc-dynamic-declaration:
23532      @dynamic identifier-list ;
23533
23534    For example:
23535      @dynamic MyProperty;
23536      @dynamic MyProperty, AnotherProperty;
23537
23538   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23539   for C.  Keep them in sync.
23540 */
23541 static void 
23542 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23543 {
23544   tree list = NULL_TREE;
23545   location_t loc;
23546   loc = cp_lexer_peek_token (parser->lexer)->location;
23547
23548   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23549   while (true)
23550     {
23551       tree property;
23552       property = cp_parser_identifier (parser);
23553       if (property == error_mark_node)
23554         {
23555           cp_parser_consume_semicolon_at_end_of_statement (parser);
23556           return;
23557         }
23558       list = chainon (list, build_tree_list (NULL, property));
23559       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23560         cp_lexer_consume_token (parser->lexer);
23561       else
23562         break;
23563     }
23564   cp_parser_consume_semicolon_at_end_of_statement (parser);
23565   objc_add_dynamic_declaration (loc, list);
23566 }
23567
23568 \f
23569 /* OpenMP 2.5 parsing routines.  */
23570
23571 /* Returns name of the next clause.
23572    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23573    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23574    returned and the token is consumed.  */
23575
23576 static pragma_omp_clause
23577 cp_parser_omp_clause_name (cp_parser *parser)
23578 {
23579   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23580
23581   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23582     result = PRAGMA_OMP_CLAUSE_IF;
23583   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23584     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23585   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23586     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23587   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23588     {
23589       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23590       const char *p = IDENTIFIER_POINTER (id);
23591
23592       switch (p[0])
23593         {
23594         case 'c':
23595           if (!strcmp ("collapse", p))
23596             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23597           else if (!strcmp ("copyin", p))
23598             result = PRAGMA_OMP_CLAUSE_COPYIN;
23599           else if (!strcmp ("copyprivate", p))
23600             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23601           break;
23602         case 'f':
23603           if (!strcmp ("firstprivate", p))
23604             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23605           break;
23606         case 'l':
23607           if (!strcmp ("lastprivate", p))
23608             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23609           break;
23610         case 'n':
23611           if (!strcmp ("nowait", p))
23612             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23613           else if (!strcmp ("num_threads", p))
23614             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23615           break;
23616         case 'o':
23617           if (!strcmp ("ordered", p))
23618             result = PRAGMA_OMP_CLAUSE_ORDERED;
23619           break;
23620         case 'r':
23621           if (!strcmp ("reduction", p))
23622             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23623           break;
23624         case 's':
23625           if (!strcmp ("schedule", p))
23626             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23627           else if (!strcmp ("shared", p))
23628             result = PRAGMA_OMP_CLAUSE_SHARED;
23629           break;
23630         case 'u':
23631           if (!strcmp ("untied", p))
23632             result = PRAGMA_OMP_CLAUSE_UNTIED;
23633           break;
23634         }
23635     }
23636
23637   if (result != PRAGMA_OMP_CLAUSE_NONE)
23638     cp_lexer_consume_token (parser->lexer);
23639
23640   return result;
23641 }
23642
23643 /* Validate that a clause of the given type does not already exist.  */
23644
23645 static void
23646 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23647                            const char *name, location_t location)
23648 {
23649   tree c;
23650
23651   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23652     if (OMP_CLAUSE_CODE (c) == code)
23653       {
23654         error_at (location, "too many %qs clauses", name);
23655         break;
23656       }
23657 }
23658
23659 /* OpenMP 2.5:
23660    variable-list:
23661      identifier
23662      variable-list , identifier
23663
23664    In addition, we match a closing parenthesis.  An opening parenthesis
23665    will have been consumed by the caller.
23666
23667    If KIND is nonzero, create the appropriate node and install the decl
23668    in OMP_CLAUSE_DECL and add the node to the head of the list.
23669
23670    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23671    return the list created.  */
23672
23673 static tree
23674 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23675                                 tree list)
23676 {
23677   cp_token *token;
23678   while (1)
23679     {
23680       tree name, decl;
23681
23682       token = cp_lexer_peek_token (parser->lexer);
23683       name = cp_parser_id_expression (parser, /*template_p=*/false,
23684                                       /*check_dependency_p=*/true,
23685                                       /*template_p=*/NULL,
23686                                       /*declarator_p=*/false,
23687                                       /*optional_p=*/false);
23688       if (name == error_mark_node)
23689         goto skip_comma;
23690
23691       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23692       if (decl == error_mark_node)
23693         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23694                                      token->location);
23695       else if (kind != 0)
23696         {
23697           tree u = build_omp_clause (token->location, kind);
23698           OMP_CLAUSE_DECL (u) = decl;
23699           OMP_CLAUSE_CHAIN (u) = list;
23700           list = u;
23701         }
23702       else
23703         list = tree_cons (decl, NULL_TREE, list);
23704
23705     get_comma:
23706       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23707         break;
23708       cp_lexer_consume_token (parser->lexer);
23709     }
23710
23711   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23712     {
23713       int ending;
23714
23715       /* Try to resync to an unnested comma.  Copied from
23716          cp_parser_parenthesized_expression_list.  */
23717     skip_comma:
23718       ending = cp_parser_skip_to_closing_parenthesis (parser,
23719                                                       /*recovering=*/true,
23720                                                       /*or_comma=*/true,
23721                                                       /*consume_paren=*/true);
23722       if (ending < 0)
23723         goto get_comma;
23724     }
23725
23726   return list;
23727 }
23728
23729 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23730    common case for omp clauses.  */
23731
23732 static tree
23733 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23734 {
23735   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23736     return cp_parser_omp_var_list_no_open (parser, kind, list);
23737   return list;
23738 }
23739
23740 /* OpenMP 3.0:
23741    collapse ( constant-expression ) */
23742
23743 static tree
23744 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23745 {
23746   tree c, num;
23747   location_t loc;
23748   HOST_WIDE_INT n;
23749
23750   loc = cp_lexer_peek_token (parser->lexer)->location;
23751   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23752     return list;
23753
23754   num = cp_parser_constant_expression (parser, false, NULL);
23755
23756   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23757     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23758                                            /*or_comma=*/false,
23759                                            /*consume_paren=*/true);
23760
23761   if (num == error_mark_node)
23762     return list;
23763   num = fold_non_dependent_expr (num);
23764   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23765       || !host_integerp (num, 0)
23766       || (n = tree_low_cst (num, 0)) <= 0
23767       || (int) n != n)
23768     {
23769       error_at (loc, "collapse argument needs positive constant integer expression");
23770       return list;
23771     }
23772
23773   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23774   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23775   OMP_CLAUSE_CHAIN (c) = list;
23776   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23777
23778   return c;
23779 }
23780
23781 /* OpenMP 2.5:
23782    default ( shared | none ) */
23783
23784 static tree
23785 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23786 {
23787   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23788   tree c;
23789
23790   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23791     return list;
23792   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23793     {
23794       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23795       const char *p = IDENTIFIER_POINTER (id);
23796
23797       switch (p[0])
23798         {
23799         case 'n':
23800           if (strcmp ("none", p) != 0)
23801             goto invalid_kind;
23802           kind = OMP_CLAUSE_DEFAULT_NONE;
23803           break;
23804
23805         case 's':
23806           if (strcmp ("shared", p) != 0)
23807             goto invalid_kind;
23808           kind = OMP_CLAUSE_DEFAULT_SHARED;
23809           break;
23810
23811         default:
23812           goto invalid_kind;
23813         }
23814
23815       cp_lexer_consume_token (parser->lexer);
23816     }
23817   else
23818     {
23819     invalid_kind:
23820       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23821     }
23822
23823   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23824     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23825                                            /*or_comma=*/false,
23826                                            /*consume_paren=*/true);
23827
23828   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23829     return list;
23830
23831   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23832   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23833   OMP_CLAUSE_CHAIN (c) = list;
23834   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23835
23836   return c;
23837 }
23838
23839 /* OpenMP 2.5:
23840    if ( expression ) */
23841
23842 static tree
23843 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23844 {
23845   tree t, c;
23846
23847   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23848     return list;
23849
23850   t = cp_parser_condition (parser);
23851
23852   if (t == error_mark_node
23853       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23854     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23855                                            /*or_comma=*/false,
23856                                            /*consume_paren=*/true);
23857
23858   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23859
23860   c = build_omp_clause (location, OMP_CLAUSE_IF);
23861   OMP_CLAUSE_IF_EXPR (c) = t;
23862   OMP_CLAUSE_CHAIN (c) = list;
23863
23864   return c;
23865 }
23866
23867 /* OpenMP 2.5:
23868    nowait */
23869
23870 static tree
23871 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23872                              tree list, location_t location)
23873 {
23874   tree c;
23875
23876   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23877
23878   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23879   OMP_CLAUSE_CHAIN (c) = list;
23880   return c;
23881 }
23882
23883 /* OpenMP 2.5:
23884    num_threads ( expression ) */
23885
23886 static tree
23887 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23888                                   location_t location)
23889 {
23890   tree t, c;
23891
23892   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23893     return list;
23894
23895   t = cp_parser_expression (parser, false, NULL);
23896
23897   if (t == error_mark_node
23898       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23899     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23900                                            /*or_comma=*/false,
23901                                            /*consume_paren=*/true);
23902
23903   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23904                              "num_threads", location);
23905
23906   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23907   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23908   OMP_CLAUSE_CHAIN (c) = list;
23909
23910   return c;
23911 }
23912
23913 /* OpenMP 2.5:
23914    ordered */
23915
23916 static tree
23917 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23918                               tree list, location_t location)
23919 {
23920   tree c;
23921
23922   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23923                              "ordered", location);
23924
23925   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23926   OMP_CLAUSE_CHAIN (c) = list;
23927   return c;
23928 }
23929
23930 /* OpenMP 2.5:
23931    reduction ( reduction-operator : variable-list )
23932
23933    reduction-operator:
23934      One of: + * - & ^ | && || */
23935
23936 static tree
23937 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23938 {
23939   enum tree_code code;
23940   tree nlist, c;
23941
23942   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23943     return list;
23944
23945   switch (cp_lexer_peek_token (parser->lexer)->type)
23946     {
23947     case CPP_PLUS:
23948       code = PLUS_EXPR;
23949       break;
23950     case CPP_MULT:
23951       code = MULT_EXPR;
23952       break;
23953     case CPP_MINUS:
23954       code = MINUS_EXPR;
23955       break;
23956     case CPP_AND:
23957       code = BIT_AND_EXPR;
23958       break;
23959     case CPP_XOR:
23960       code = BIT_XOR_EXPR;
23961       break;
23962     case CPP_OR:
23963       code = BIT_IOR_EXPR;
23964       break;
23965     case CPP_AND_AND:
23966       code = TRUTH_ANDIF_EXPR;
23967       break;
23968     case CPP_OR_OR:
23969       code = TRUTH_ORIF_EXPR;
23970       break;
23971     default:
23972       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23973                                "%<|%>, %<&&%>, or %<||%>");
23974     resync_fail:
23975       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23976                                              /*or_comma=*/false,
23977                                              /*consume_paren=*/true);
23978       return list;
23979     }
23980   cp_lexer_consume_token (parser->lexer);
23981
23982   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23983     goto resync_fail;
23984
23985   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23986   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23987     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23988
23989   return nlist;
23990 }
23991
23992 /* OpenMP 2.5:
23993    schedule ( schedule-kind )
23994    schedule ( schedule-kind , expression )
23995
23996    schedule-kind:
23997      static | dynamic | guided | runtime | auto  */
23998
23999 static tree
24000 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24001 {
24002   tree c, t;
24003
24004   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24005     return list;
24006
24007   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24008
24009   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24010     {
24011       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24012       const char *p = IDENTIFIER_POINTER (id);
24013
24014       switch (p[0])
24015         {
24016         case 'd':
24017           if (strcmp ("dynamic", p) != 0)
24018             goto invalid_kind;
24019           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24020           break;
24021
24022         case 'g':
24023           if (strcmp ("guided", p) != 0)
24024             goto invalid_kind;
24025           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24026           break;
24027
24028         case 'r':
24029           if (strcmp ("runtime", p) != 0)
24030             goto invalid_kind;
24031           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24032           break;
24033
24034         default:
24035           goto invalid_kind;
24036         }
24037     }
24038   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24039     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24040   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24041     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24042   else
24043     goto invalid_kind;
24044   cp_lexer_consume_token (parser->lexer);
24045
24046   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24047     {
24048       cp_token *token;
24049       cp_lexer_consume_token (parser->lexer);
24050
24051       token = cp_lexer_peek_token (parser->lexer);
24052       t = cp_parser_assignment_expression (parser, false, NULL);
24053
24054       if (t == error_mark_node)
24055         goto resync_fail;
24056       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24057         error_at (token->location, "schedule %<runtime%> does not take "
24058                   "a %<chunk_size%> parameter");
24059       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24060         error_at (token->location, "schedule %<auto%> does not take "
24061                   "a %<chunk_size%> parameter");
24062       else
24063         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24064
24065       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24066         goto resync_fail;
24067     }
24068   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
24069     goto resync_fail;
24070
24071   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
24072   OMP_CLAUSE_CHAIN (c) = list;
24073   return c;
24074
24075  invalid_kind:
24076   cp_parser_error (parser, "invalid schedule kind");
24077  resync_fail:
24078   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24079                                          /*or_comma=*/false,
24080                                          /*consume_paren=*/true);
24081   return list;
24082 }
24083
24084 /* OpenMP 3.0:
24085    untied */
24086
24087 static tree
24088 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
24089                              tree list, location_t location)
24090 {
24091   tree c;
24092
24093   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
24094
24095   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
24096   OMP_CLAUSE_CHAIN (c) = list;
24097   return c;
24098 }
24099
24100 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
24101    is a bitmask in MASK.  Return the list of clauses found; the result
24102    of clause default goes in *pdefault.  */
24103
24104 static tree
24105 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
24106                            const char *where, cp_token *pragma_tok)
24107 {
24108   tree clauses = NULL;
24109   bool first = true;
24110   cp_token *token = NULL;
24111
24112   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
24113     {
24114       pragma_omp_clause c_kind;
24115       const char *c_name;
24116       tree prev = clauses;
24117
24118       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24119         cp_lexer_consume_token (parser->lexer);
24120
24121       token = cp_lexer_peek_token (parser->lexer);
24122       c_kind = cp_parser_omp_clause_name (parser);
24123       first = false;
24124
24125       switch (c_kind)
24126         {
24127         case PRAGMA_OMP_CLAUSE_COLLAPSE:
24128           clauses = cp_parser_omp_clause_collapse (parser, clauses,
24129                                                    token->location);
24130           c_name = "collapse";
24131           break;
24132         case PRAGMA_OMP_CLAUSE_COPYIN:
24133           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
24134           c_name = "copyin";
24135           break;
24136         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
24137           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
24138                                             clauses);
24139           c_name = "copyprivate";
24140           break;
24141         case PRAGMA_OMP_CLAUSE_DEFAULT:
24142           clauses = cp_parser_omp_clause_default (parser, clauses,
24143                                                   token->location);
24144           c_name = "default";
24145           break;
24146         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
24147           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
24148                                             clauses);
24149           c_name = "firstprivate";
24150           break;
24151         case PRAGMA_OMP_CLAUSE_IF:
24152           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
24153           c_name = "if";
24154           break;
24155         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24156           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24157                                             clauses);
24158           c_name = "lastprivate";
24159           break;
24160         case PRAGMA_OMP_CLAUSE_NOWAIT:
24161           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24162           c_name = "nowait";
24163           break;
24164         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24165           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24166                                                       token->location);
24167           c_name = "num_threads";
24168           break;
24169         case PRAGMA_OMP_CLAUSE_ORDERED:
24170           clauses = cp_parser_omp_clause_ordered (parser, clauses,
24171                                                   token->location);
24172           c_name = "ordered";
24173           break;
24174         case PRAGMA_OMP_CLAUSE_PRIVATE:
24175           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24176                                             clauses);
24177           c_name = "private";
24178           break;
24179         case PRAGMA_OMP_CLAUSE_REDUCTION:
24180           clauses = cp_parser_omp_clause_reduction (parser, clauses);
24181           c_name = "reduction";
24182           break;
24183         case PRAGMA_OMP_CLAUSE_SCHEDULE:
24184           clauses = cp_parser_omp_clause_schedule (parser, clauses,
24185                                                    token->location);
24186           c_name = "schedule";
24187           break;
24188         case PRAGMA_OMP_CLAUSE_SHARED:
24189           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24190                                             clauses);
24191           c_name = "shared";
24192           break;
24193         case PRAGMA_OMP_CLAUSE_UNTIED:
24194           clauses = cp_parser_omp_clause_untied (parser, clauses,
24195                                                  token->location);
24196           c_name = "nowait";
24197           break;
24198         default:
24199           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24200           goto saw_error;
24201         }
24202
24203       if (((mask >> c_kind) & 1) == 0)
24204         {
24205           /* Remove the invalid clause(s) from the list to avoid
24206              confusing the rest of the compiler.  */
24207           clauses = prev;
24208           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24209         }
24210     }
24211  saw_error:
24212   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24213   return finish_omp_clauses (clauses);
24214 }
24215
24216 /* OpenMP 2.5:
24217    structured-block:
24218      statement
24219
24220    In practice, we're also interested in adding the statement to an
24221    outer node.  So it is convenient if we work around the fact that
24222    cp_parser_statement calls add_stmt.  */
24223
24224 static unsigned
24225 cp_parser_begin_omp_structured_block (cp_parser *parser)
24226 {
24227   unsigned save = parser->in_statement;
24228
24229   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24230      This preserves the "not within loop or switch" style error messages
24231      for nonsense cases like
24232         void foo() {
24233         #pragma omp single
24234           break;
24235         }
24236   */
24237   if (parser->in_statement)
24238     parser->in_statement = IN_OMP_BLOCK;
24239
24240   return save;
24241 }
24242
24243 static void
24244 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24245 {
24246   parser->in_statement = save;
24247 }
24248
24249 static tree
24250 cp_parser_omp_structured_block (cp_parser *parser)
24251 {
24252   tree stmt = begin_omp_structured_block ();
24253   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24254
24255   cp_parser_statement (parser, NULL_TREE, false, NULL);
24256
24257   cp_parser_end_omp_structured_block (parser, save);
24258   return finish_omp_structured_block (stmt);
24259 }
24260
24261 /* OpenMP 2.5:
24262    # pragma omp atomic new-line
24263      expression-stmt
24264
24265    expression-stmt:
24266      x binop= expr | x++ | ++x | x-- | --x
24267    binop:
24268      +, *, -, /, &, ^, |, <<, >>
24269
24270   where x is an lvalue expression with scalar type.  */
24271
24272 static void
24273 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24274 {
24275   tree lhs, rhs;
24276   enum tree_code code;
24277
24278   cp_parser_require_pragma_eol (parser, pragma_tok);
24279
24280   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24281                                     /*cast_p=*/false, NULL);
24282   switch (TREE_CODE (lhs))
24283     {
24284     case ERROR_MARK:
24285       goto saw_error;
24286
24287     case PREINCREMENT_EXPR:
24288     case POSTINCREMENT_EXPR:
24289       lhs = TREE_OPERAND (lhs, 0);
24290       code = PLUS_EXPR;
24291       rhs = integer_one_node;
24292       break;
24293
24294     case PREDECREMENT_EXPR:
24295     case POSTDECREMENT_EXPR:
24296       lhs = TREE_OPERAND (lhs, 0);
24297       code = MINUS_EXPR;
24298       rhs = integer_one_node;
24299       break;
24300
24301     case COMPOUND_EXPR:
24302       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24303          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24304          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24305          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24306          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24307                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24308             == BOOLEAN_TYPE)
24309        /* Undo effects of boolean_increment for post {in,de}crement.  */
24310        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24311       /* FALLTHRU */
24312     case MODIFY_EXPR:
24313       if (TREE_CODE (lhs) == MODIFY_EXPR
24314          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24315        {
24316          /* Undo effects of boolean_increment.  */
24317          if (integer_onep (TREE_OPERAND (lhs, 1)))
24318            {
24319              /* This is pre or post increment.  */
24320              rhs = TREE_OPERAND (lhs, 1);
24321              lhs = TREE_OPERAND (lhs, 0);
24322              code = NOP_EXPR;
24323              break;
24324            }
24325        }
24326       /* FALLTHRU */
24327     default:
24328       switch (cp_lexer_peek_token (parser->lexer)->type)
24329         {
24330         case CPP_MULT_EQ:
24331           code = MULT_EXPR;
24332           break;
24333         case CPP_DIV_EQ:
24334           code = TRUNC_DIV_EXPR;
24335           break;
24336         case CPP_PLUS_EQ:
24337           code = PLUS_EXPR;
24338           break;
24339         case CPP_MINUS_EQ:
24340           code = MINUS_EXPR;
24341           break;
24342         case CPP_LSHIFT_EQ:
24343           code = LSHIFT_EXPR;
24344           break;
24345         case CPP_RSHIFT_EQ:
24346           code = RSHIFT_EXPR;
24347           break;
24348         case CPP_AND_EQ:
24349           code = BIT_AND_EXPR;
24350           break;
24351         case CPP_OR_EQ:
24352           code = BIT_IOR_EXPR;
24353           break;
24354         case CPP_XOR_EQ:
24355           code = BIT_XOR_EXPR;
24356           break;
24357         default:
24358           cp_parser_error (parser,
24359                            "invalid operator for %<#pragma omp atomic%>");
24360           goto saw_error;
24361         }
24362       cp_lexer_consume_token (parser->lexer);
24363
24364       rhs = cp_parser_expression (parser, false, NULL);
24365       if (rhs == error_mark_node)
24366         goto saw_error;
24367       break;
24368     }
24369   finish_omp_atomic (code, lhs, rhs);
24370   cp_parser_consume_semicolon_at_end_of_statement (parser);
24371   return;
24372
24373  saw_error:
24374   cp_parser_skip_to_end_of_block_or_statement (parser);
24375 }
24376
24377
24378 /* OpenMP 2.5:
24379    # pragma omp barrier new-line  */
24380
24381 static void
24382 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24383 {
24384   cp_parser_require_pragma_eol (parser, pragma_tok);
24385   finish_omp_barrier ();
24386 }
24387
24388 /* OpenMP 2.5:
24389    # pragma omp critical [(name)] new-line
24390      structured-block  */
24391
24392 static tree
24393 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24394 {
24395   tree stmt, name = NULL;
24396
24397   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24398     {
24399       cp_lexer_consume_token (parser->lexer);
24400
24401       name = cp_parser_identifier (parser);
24402
24403       if (name == error_mark_node
24404           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24405         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24406                                                /*or_comma=*/false,
24407                                                /*consume_paren=*/true);
24408       if (name == error_mark_node)
24409         name = NULL;
24410     }
24411   cp_parser_require_pragma_eol (parser, pragma_tok);
24412
24413   stmt = cp_parser_omp_structured_block (parser);
24414   return c_finish_omp_critical (input_location, stmt, name);
24415 }
24416
24417 /* OpenMP 2.5:
24418    # pragma omp flush flush-vars[opt] new-line
24419
24420    flush-vars:
24421      ( variable-list ) */
24422
24423 static void
24424 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24425 {
24426   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24427     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24428   cp_parser_require_pragma_eol (parser, pragma_tok);
24429
24430   finish_omp_flush ();
24431 }
24432
24433 /* Helper function, to parse omp for increment expression.  */
24434
24435 static tree
24436 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24437 {
24438   tree cond = cp_parser_binary_expression (parser, false, true,
24439                                            PREC_NOT_OPERATOR, NULL);
24440   if (cond == error_mark_node
24441       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24442     {
24443       cp_parser_skip_to_end_of_statement (parser);
24444       return error_mark_node;
24445     }
24446
24447   switch (TREE_CODE (cond))
24448     {
24449     case GT_EXPR:
24450     case GE_EXPR:
24451     case LT_EXPR:
24452     case LE_EXPR:
24453       break;
24454     default:
24455       return error_mark_node;
24456     }
24457
24458   /* If decl is an iterator, preserve LHS and RHS of the relational
24459      expr until finish_omp_for.  */
24460   if (decl
24461       && (type_dependent_expression_p (decl)
24462           || CLASS_TYPE_P (TREE_TYPE (decl))))
24463     return cond;
24464
24465   return build_x_binary_op (TREE_CODE (cond),
24466                             TREE_OPERAND (cond, 0), ERROR_MARK,
24467                             TREE_OPERAND (cond, 1), ERROR_MARK,
24468                             /*overload=*/NULL, tf_warning_or_error);
24469 }
24470
24471 /* Helper function, to parse omp for increment expression.  */
24472
24473 static tree
24474 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24475 {
24476   cp_token *token = cp_lexer_peek_token (parser->lexer);
24477   enum tree_code op;
24478   tree lhs, rhs;
24479   cp_id_kind idk;
24480   bool decl_first;
24481
24482   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24483     {
24484       op = (token->type == CPP_PLUS_PLUS
24485             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24486       cp_lexer_consume_token (parser->lexer);
24487       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24488       if (lhs != decl)
24489         return error_mark_node;
24490       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24491     }
24492
24493   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24494   if (lhs != decl)
24495     return error_mark_node;
24496
24497   token = cp_lexer_peek_token (parser->lexer);
24498   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24499     {
24500       op = (token->type == CPP_PLUS_PLUS
24501             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24502       cp_lexer_consume_token (parser->lexer);
24503       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24504     }
24505
24506   op = cp_parser_assignment_operator_opt (parser);
24507   if (op == ERROR_MARK)
24508     return error_mark_node;
24509
24510   if (op != NOP_EXPR)
24511     {
24512       rhs = cp_parser_assignment_expression (parser, false, NULL);
24513       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24514       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24515     }
24516
24517   lhs = cp_parser_binary_expression (parser, false, false,
24518                                      PREC_ADDITIVE_EXPRESSION, NULL);
24519   token = cp_lexer_peek_token (parser->lexer);
24520   decl_first = lhs == decl;
24521   if (decl_first)
24522     lhs = NULL_TREE;
24523   if (token->type != CPP_PLUS
24524       && token->type != CPP_MINUS)
24525     return error_mark_node;
24526
24527   do
24528     {
24529       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24530       cp_lexer_consume_token (parser->lexer);
24531       rhs = cp_parser_binary_expression (parser, false, false,
24532                                          PREC_ADDITIVE_EXPRESSION, NULL);
24533       token = cp_lexer_peek_token (parser->lexer);
24534       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24535         {
24536           if (lhs == NULL_TREE)
24537             {
24538               if (op == PLUS_EXPR)
24539                 lhs = rhs;
24540               else
24541                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24542             }
24543           else
24544             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24545                                      NULL, tf_warning_or_error);
24546         }
24547     }
24548   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24549
24550   if (!decl_first)
24551     {
24552       if (rhs != decl || op == MINUS_EXPR)
24553         return error_mark_node;
24554       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24555     }
24556   else
24557     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24558
24559   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24560 }
24561
24562 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24563
24564 static tree
24565 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24566 {
24567   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24568   tree real_decl, initv, condv, incrv, declv;
24569   tree this_pre_body, cl;
24570   location_t loc_first;
24571   bool collapse_err = false;
24572   int i, collapse = 1, nbraces = 0;
24573   VEC(tree,gc) *for_block = make_tree_vector ();
24574
24575   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24576     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24577       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24578
24579   gcc_assert (collapse >= 1);
24580
24581   declv = make_tree_vec (collapse);
24582   initv = make_tree_vec (collapse);
24583   condv = make_tree_vec (collapse);
24584   incrv = make_tree_vec (collapse);
24585
24586   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24587
24588   for (i = 0; i < collapse; i++)
24589     {
24590       int bracecount = 0;
24591       bool add_private_clause = false;
24592       location_t loc;
24593
24594       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24595         {
24596           cp_parser_error (parser, "for statement expected");
24597           return NULL;
24598         }
24599       loc = cp_lexer_consume_token (parser->lexer)->location;
24600
24601       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24602         return NULL;
24603
24604       init = decl = real_decl = NULL;
24605       this_pre_body = push_stmt_list ();
24606       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24607         {
24608           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24609
24610              init-expr:
24611                        var = lb
24612                        integer-type var = lb
24613                        random-access-iterator-type var = lb
24614                        pointer-type var = lb
24615           */
24616           cp_decl_specifier_seq type_specifiers;
24617
24618           /* First, try to parse as an initialized declaration.  See
24619              cp_parser_condition, from whence the bulk of this is copied.  */
24620
24621           cp_parser_parse_tentatively (parser);
24622           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24623                                         /*is_trailing_return=*/false,
24624                                         &type_specifiers);
24625           if (cp_parser_parse_definitely (parser))
24626             {
24627               /* If parsing a type specifier seq succeeded, then this
24628                  MUST be a initialized declaration.  */
24629               tree asm_specification, attributes;
24630               cp_declarator *declarator;
24631
24632               declarator = cp_parser_declarator (parser,
24633                                                  CP_PARSER_DECLARATOR_NAMED,
24634                                                  /*ctor_dtor_or_conv_p=*/NULL,
24635                                                  /*parenthesized_p=*/NULL,
24636                                                  /*member_p=*/false);
24637               attributes = cp_parser_attributes_opt (parser);
24638               asm_specification = cp_parser_asm_specification_opt (parser);
24639
24640               if (declarator == cp_error_declarator) 
24641                 cp_parser_skip_to_end_of_statement (parser);
24642
24643               else 
24644                 {
24645                   tree pushed_scope, auto_node;
24646
24647                   decl = start_decl (declarator, &type_specifiers,
24648                                      SD_INITIALIZED, attributes,
24649                                      /*prefix_attributes=*/NULL_TREE,
24650                                      &pushed_scope);
24651
24652                   auto_node = type_uses_auto (TREE_TYPE (decl));
24653                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24654                     {
24655                       if (cp_lexer_next_token_is (parser->lexer, 
24656                                                   CPP_OPEN_PAREN))
24657                         error ("parenthesized initialization is not allowed in "
24658                                "OpenMP %<for%> loop");
24659                       else
24660                         /* Trigger an error.  */
24661                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24662
24663                       init = error_mark_node;
24664                       cp_parser_skip_to_end_of_statement (parser);
24665                     }
24666                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24667                            || type_dependent_expression_p (decl)
24668                            || auto_node)
24669                     {
24670                       bool is_direct_init, is_non_constant_init;
24671
24672                       init = cp_parser_initializer (parser,
24673                                                     &is_direct_init,
24674                                                     &is_non_constant_init);
24675
24676                       if (auto_node)
24677                         {
24678                           TREE_TYPE (decl)
24679                             = do_auto_deduction (TREE_TYPE (decl), init,
24680                                                  auto_node);
24681
24682                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24683                               && !type_dependent_expression_p (decl))
24684                             goto non_class;
24685                         }
24686                       
24687                       cp_finish_decl (decl, init, !is_non_constant_init,
24688                                       asm_specification,
24689                                       LOOKUP_ONLYCONVERTING);
24690                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24691                         {
24692                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24693                           init = NULL_TREE;
24694                         }
24695                       else
24696                         init = pop_stmt_list (this_pre_body);
24697                       this_pre_body = NULL_TREE;
24698                     }
24699                   else
24700                     {
24701                       /* Consume '='.  */
24702                       cp_lexer_consume_token (parser->lexer);
24703                       init = cp_parser_assignment_expression (parser, false, NULL);
24704
24705                     non_class:
24706                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24707                         init = error_mark_node;
24708                       else
24709                         cp_finish_decl (decl, NULL_TREE,
24710                                         /*init_const_expr_p=*/false,
24711                                         asm_specification,
24712                                         LOOKUP_ONLYCONVERTING);
24713                     }
24714
24715                   if (pushed_scope)
24716                     pop_scope (pushed_scope);
24717                 }
24718             }
24719           else 
24720             {
24721               cp_id_kind idk;
24722               /* If parsing a type specifier sequence failed, then
24723                  this MUST be a simple expression.  */
24724               cp_parser_parse_tentatively (parser);
24725               decl = cp_parser_primary_expression (parser, false, false,
24726                                                    false, &idk);
24727               if (!cp_parser_error_occurred (parser)
24728                   && decl
24729                   && DECL_P (decl)
24730                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24731                 {
24732                   tree rhs;
24733
24734                   cp_parser_parse_definitely (parser);
24735                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24736                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24737                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24738                                                          rhs,
24739                                                          tf_warning_or_error));
24740                   add_private_clause = true;
24741                 }
24742               else
24743                 {
24744                   decl = NULL;
24745                   cp_parser_abort_tentative_parse (parser);
24746                   init = cp_parser_expression (parser, false, NULL);
24747                   if (init)
24748                     {
24749                       if (TREE_CODE (init) == MODIFY_EXPR
24750                           || TREE_CODE (init) == MODOP_EXPR)
24751                         real_decl = TREE_OPERAND (init, 0);
24752                     }
24753                 }
24754             }
24755         }
24756       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24757       if (this_pre_body)
24758         {
24759           this_pre_body = pop_stmt_list (this_pre_body);
24760           if (pre_body)
24761             {
24762               tree t = pre_body;
24763               pre_body = push_stmt_list ();
24764               add_stmt (t);
24765               add_stmt (this_pre_body);
24766               pre_body = pop_stmt_list (pre_body);
24767             }
24768           else
24769             pre_body = this_pre_body;
24770         }
24771
24772       if (decl)
24773         real_decl = decl;
24774       if (par_clauses != NULL && real_decl != NULL_TREE)
24775         {
24776           tree *c;
24777           for (c = par_clauses; *c ; )
24778             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24779                 && OMP_CLAUSE_DECL (*c) == real_decl)
24780               {
24781                 error_at (loc, "iteration variable %qD"
24782                           " should not be firstprivate", real_decl);
24783                 *c = OMP_CLAUSE_CHAIN (*c);
24784               }
24785             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24786                      && OMP_CLAUSE_DECL (*c) == real_decl)
24787               {
24788                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24789                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24790                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24791                 OMP_CLAUSE_DECL (l) = real_decl;
24792                 OMP_CLAUSE_CHAIN (l) = clauses;
24793                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24794                 clauses = l;
24795                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24796                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24797                 add_private_clause = false;
24798               }
24799             else
24800               {
24801                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24802                     && OMP_CLAUSE_DECL (*c) == real_decl)
24803                   add_private_clause = false;
24804                 c = &OMP_CLAUSE_CHAIN (*c);
24805               }
24806         }
24807
24808       if (add_private_clause)
24809         {
24810           tree c;
24811           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24812             {
24813               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24814                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24815                   && OMP_CLAUSE_DECL (c) == decl)
24816                 break;
24817               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24818                        && OMP_CLAUSE_DECL (c) == decl)
24819                 error_at (loc, "iteration variable %qD "
24820                           "should not be firstprivate",
24821                           decl);
24822               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24823                        && OMP_CLAUSE_DECL (c) == decl)
24824                 error_at (loc, "iteration variable %qD should not be reduction",
24825                           decl);
24826             }
24827           if (c == NULL)
24828             {
24829               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24830               OMP_CLAUSE_DECL (c) = decl;
24831               c = finish_omp_clauses (c);
24832               if (c)
24833                 {
24834                   OMP_CLAUSE_CHAIN (c) = clauses;
24835                   clauses = c;
24836                 }
24837             }
24838         }
24839
24840       cond = NULL;
24841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24842         cond = cp_parser_omp_for_cond (parser, decl);
24843       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24844
24845       incr = NULL;
24846       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24847         {
24848           /* If decl is an iterator, preserve the operator on decl
24849              until finish_omp_for.  */
24850           if (decl
24851               && ((type_dependent_expression_p (decl)
24852                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24853                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24854             incr = cp_parser_omp_for_incr (parser, decl);
24855           else
24856             incr = cp_parser_expression (parser, false, NULL);
24857         }
24858
24859       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24860         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24861                                                /*or_comma=*/false,
24862                                                /*consume_paren=*/true);
24863
24864       TREE_VEC_ELT (declv, i) = decl;
24865       TREE_VEC_ELT (initv, i) = init;
24866       TREE_VEC_ELT (condv, i) = cond;
24867       TREE_VEC_ELT (incrv, i) = incr;
24868
24869       if (i == collapse - 1)
24870         break;
24871
24872       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24873          in between the collapsed for loops to be still considered perfectly
24874          nested.  Hopefully the final version clarifies this.
24875          For now handle (multiple) {'s and empty statements.  */
24876       cp_parser_parse_tentatively (parser);
24877       do
24878         {
24879           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24880             break;
24881           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24882             {
24883               cp_lexer_consume_token (parser->lexer);
24884               bracecount++;
24885             }
24886           else if (bracecount
24887                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24888             cp_lexer_consume_token (parser->lexer);
24889           else
24890             {
24891               loc = cp_lexer_peek_token (parser->lexer)->location;
24892               error_at (loc, "not enough collapsed for loops");
24893               collapse_err = true;
24894               cp_parser_abort_tentative_parse (parser);
24895               declv = NULL_TREE;
24896               break;
24897             }
24898         }
24899       while (1);
24900
24901       if (declv)
24902         {
24903           cp_parser_parse_definitely (parser);
24904           nbraces += bracecount;
24905         }
24906     }
24907
24908   /* Note that we saved the original contents of this flag when we entered
24909      the structured block, and so we don't need to re-save it here.  */
24910   parser->in_statement = IN_OMP_FOR;
24911
24912   /* Note that the grammar doesn't call for a structured block here,
24913      though the loop as a whole is a structured block.  */
24914   body = push_stmt_list ();
24915   cp_parser_statement (parser, NULL_TREE, false, NULL);
24916   body = pop_stmt_list (body);
24917
24918   if (declv == NULL_TREE)
24919     ret = NULL_TREE;
24920   else
24921     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24922                           pre_body, clauses);
24923
24924   while (nbraces)
24925     {
24926       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24927         {
24928           cp_lexer_consume_token (parser->lexer);
24929           nbraces--;
24930         }
24931       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24932         cp_lexer_consume_token (parser->lexer);
24933       else
24934         {
24935           if (!collapse_err)
24936             {
24937               error_at (cp_lexer_peek_token (parser->lexer)->location,
24938                         "collapsed loops not perfectly nested");
24939             }
24940           collapse_err = true;
24941           cp_parser_statement_seq_opt (parser, NULL);
24942           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24943             break;
24944         }
24945     }
24946
24947   while (!VEC_empty (tree, for_block))
24948     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24949   release_tree_vector (for_block);
24950
24951   return ret;
24952 }
24953
24954 /* OpenMP 2.5:
24955    #pragma omp for for-clause[optseq] new-line
24956      for-loop  */
24957
24958 #define OMP_FOR_CLAUSE_MASK                             \
24959         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24960         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24961         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24962         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24963         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24964         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24965         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24966         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24967
24968 static tree
24969 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24970 {
24971   tree clauses, sb, ret;
24972   unsigned int save;
24973
24974   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24975                                        "#pragma omp for", pragma_tok);
24976
24977   sb = begin_omp_structured_block ();
24978   save = cp_parser_begin_omp_structured_block (parser);
24979
24980   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24981
24982   cp_parser_end_omp_structured_block (parser, save);
24983   add_stmt (finish_omp_structured_block (sb));
24984
24985   return ret;
24986 }
24987
24988 /* OpenMP 2.5:
24989    # pragma omp master new-line
24990      structured-block  */
24991
24992 static tree
24993 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24994 {
24995   cp_parser_require_pragma_eol (parser, pragma_tok);
24996   return c_finish_omp_master (input_location,
24997                               cp_parser_omp_structured_block (parser));
24998 }
24999
25000 /* OpenMP 2.5:
25001    # pragma omp ordered new-line
25002      structured-block  */
25003
25004 static tree
25005 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
25006 {
25007   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25008   cp_parser_require_pragma_eol (parser, pragma_tok);
25009   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
25010 }
25011
25012 /* OpenMP 2.5:
25013
25014    section-scope:
25015      { section-sequence }
25016
25017    section-sequence:
25018      section-directive[opt] structured-block
25019      section-sequence section-directive structured-block  */
25020
25021 static tree
25022 cp_parser_omp_sections_scope (cp_parser *parser)
25023 {
25024   tree stmt, substmt;
25025   bool error_suppress = false;
25026   cp_token *tok;
25027
25028   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25029     return NULL_TREE;
25030
25031   stmt = push_stmt_list ();
25032
25033   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
25034     {
25035       unsigned save;
25036
25037       substmt = begin_omp_structured_block ();
25038       save = cp_parser_begin_omp_structured_block (parser);
25039
25040       while (1)
25041         {
25042           cp_parser_statement (parser, NULL_TREE, false, NULL);
25043
25044           tok = cp_lexer_peek_token (parser->lexer);
25045           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25046             break;
25047           if (tok->type == CPP_CLOSE_BRACE)
25048             break;
25049           if (tok->type == CPP_EOF)
25050             break;
25051         }
25052
25053       cp_parser_end_omp_structured_block (parser, save);
25054       substmt = finish_omp_structured_block (substmt);
25055       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25056       add_stmt (substmt);
25057     }
25058
25059   while (1)
25060     {
25061       tok = cp_lexer_peek_token (parser->lexer);
25062       if (tok->type == CPP_CLOSE_BRACE)
25063         break;
25064       if (tok->type == CPP_EOF)
25065         break;
25066
25067       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25068         {
25069           cp_lexer_consume_token (parser->lexer);
25070           cp_parser_require_pragma_eol (parser, tok);
25071           error_suppress = false;
25072         }
25073       else if (!error_suppress)
25074         {
25075           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
25076           error_suppress = true;
25077         }
25078
25079       substmt = cp_parser_omp_structured_block (parser);
25080       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25081       add_stmt (substmt);
25082     }
25083   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25084
25085   substmt = pop_stmt_list (stmt);
25086
25087   stmt = make_node (OMP_SECTIONS);
25088   TREE_TYPE (stmt) = void_type_node;
25089   OMP_SECTIONS_BODY (stmt) = substmt;
25090
25091   add_stmt (stmt);
25092   return stmt;
25093 }
25094
25095 /* OpenMP 2.5:
25096    # pragma omp sections sections-clause[optseq] newline
25097      sections-scope  */
25098
25099 #define OMP_SECTIONS_CLAUSE_MASK                        \
25100         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25101         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25102         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25103         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25104         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25105
25106 static tree
25107 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
25108 {
25109   tree clauses, ret;
25110
25111   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
25112                                        "#pragma omp sections", pragma_tok);
25113
25114   ret = cp_parser_omp_sections_scope (parser);
25115   if (ret)
25116     OMP_SECTIONS_CLAUSES (ret) = clauses;
25117
25118   return ret;
25119 }
25120
25121 /* OpenMP 2.5:
25122    # pragma parallel parallel-clause new-line
25123    # pragma parallel for parallel-for-clause new-line
25124    # pragma parallel sections parallel-sections-clause new-line  */
25125
25126 #define OMP_PARALLEL_CLAUSE_MASK                        \
25127         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25128         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25129         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25130         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25131         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25132         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
25133         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25134         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
25135
25136 static tree
25137 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
25138 {
25139   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
25140   const char *p_name = "#pragma omp parallel";
25141   tree stmt, clauses, par_clause, ws_clause, block;
25142   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
25143   unsigned int save;
25144   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25145
25146   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25147     {
25148       cp_lexer_consume_token (parser->lexer);
25149       p_kind = PRAGMA_OMP_PARALLEL_FOR;
25150       p_name = "#pragma omp parallel for";
25151       mask |= OMP_FOR_CLAUSE_MASK;
25152       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25153     }
25154   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25155     {
25156       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25157       const char *p = IDENTIFIER_POINTER (id);
25158       if (strcmp (p, "sections") == 0)
25159         {
25160           cp_lexer_consume_token (parser->lexer);
25161           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25162           p_name = "#pragma omp parallel sections";
25163           mask |= OMP_SECTIONS_CLAUSE_MASK;
25164           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25165         }
25166     }
25167
25168   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25169   block = begin_omp_parallel ();
25170   save = cp_parser_begin_omp_structured_block (parser);
25171
25172   switch (p_kind)
25173     {
25174     case PRAGMA_OMP_PARALLEL:
25175       cp_parser_statement (parser, NULL_TREE, false, NULL);
25176       par_clause = clauses;
25177       break;
25178
25179     case PRAGMA_OMP_PARALLEL_FOR:
25180       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25181       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25182       break;
25183
25184     case PRAGMA_OMP_PARALLEL_SECTIONS:
25185       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25186       stmt = cp_parser_omp_sections_scope (parser);
25187       if (stmt)
25188         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25189       break;
25190
25191     default:
25192       gcc_unreachable ();
25193     }
25194
25195   cp_parser_end_omp_structured_block (parser, save);
25196   stmt = finish_omp_parallel (par_clause, block);
25197   if (p_kind != PRAGMA_OMP_PARALLEL)
25198     OMP_PARALLEL_COMBINED (stmt) = 1;
25199   return stmt;
25200 }
25201
25202 /* OpenMP 2.5:
25203    # pragma omp single single-clause[optseq] new-line
25204      structured-block  */
25205
25206 #define OMP_SINGLE_CLAUSE_MASK                          \
25207         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25208         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25209         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25210         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25211
25212 static tree
25213 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25214 {
25215   tree stmt = make_node (OMP_SINGLE);
25216   TREE_TYPE (stmt) = void_type_node;
25217
25218   OMP_SINGLE_CLAUSES (stmt)
25219     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25220                                  "#pragma omp single", pragma_tok);
25221   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25222
25223   return add_stmt (stmt);
25224 }
25225
25226 /* OpenMP 3.0:
25227    # pragma omp task task-clause[optseq] new-line
25228      structured-block  */
25229
25230 #define OMP_TASK_CLAUSE_MASK                            \
25231         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25232         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25233         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25234         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25235         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25236         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25237
25238 static tree
25239 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25240 {
25241   tree clauses, block;
25242   unsigned int save;
25243
25244   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25245                                        "#pragma omp task", pragma_tok);
25246   block = begin_omp_task ();
25247   save = cp_parser_begin_omp_structured_block (parser);
25248   cp_parser_statement (parser, NULL_TREE, false, NULL);
25249   cp_parser_end_omp_structured_block (parser, save);
25250   return finish_omp_task (clauses, block);
25251 }
25252
25253 /* OpenMP 3.0:
25254    # pragma omp taskwait new-line  */
25255
25256 static void
25257 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25258 {
25259   cp_parser_require_pragma_eol (parser, pragma_tok);
25260   finish_omp_taskwait ();
25261 }
25262
25263 /* OpenMP 2.5:
25264    # pragma omp threadprivate (variable-list) */
25265
25266 static void
25267 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25268 {
25269   tree vars;
25270
25271   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25272   cp_parser_require_pragma_eol (parser, pragma_tok);
25273
25274   finish_omp_threadprivate (vars);
25275 }
25276
25277 /* Main entry point to OpenMP statement pragmas.  */
25278
25279 static void
25280 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25281 {
25282   tree stmt;
25283
25284   switch (pragma_tok->pragma_kind)
25285     {
25286     case PRAGMA_OMP_ATOMIC:
25287       cp_parser_omp_atomic (parser, pragma_tok);
25288       return;
25289     case PRAGMA_OMP_CRITICAL:
25290       stmt = cp_parser_omp_critical (parser, pragma_tok);
25291       break;
25292     case PRAGMA_OMP_FOR:
25293       stmt = cp_parser_omp_for (parser, pragma_tok);
25294       break;
25295     case PRAGMA_OMP_MASTER:
25296       stmt = cp_parser_omp_master (parser, pragma_tok);
25297       break;
25298     case PRAGMA_OMP_ORDERED:
25299       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25300       break;
25301     case PRAGMA_OMP_PARALLEL:
25302       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25303       break;
25304     case PRAGMA_OMP_SECTIONS:
25305       stmt = cp_parser_omp_sections (parser, pragma_tok);
25306       break;
25307     case PRAGMA_OMP_SINGLE:
25308       stmt = cp_parser_omp_single (parser, pragma_tok);
25309       break;
25310     case PRAGMA_OMP_TASK:
25311       stmt = cp_parser_omp_task (parser, pragma_tok);
25312       break;
25313     default:
25314       gcc_unreachable ();
25315     }
25316
25317   if (stmt)
25318     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25319 }
25320 \f
25321 /* The parser.  */
25322
25323 static GTY (()) cp_parser *the_parser;
25324
25325 \f
25326 /* Special handling for the first token or line in the file.  The first
25327    thing in the file might be #pragma GCC pch_preprocess, which loads a
25328    PCH file, which is a GC collection point.  So we need to handle this
25329    first pragma without benefit of an existing lexer structure.
25330
25331    Always returns one token to the caller in *FIRST_TOKEN.  This is
25332    either the true first token of the file, or the first token after
25333    the initial pragma.  */
25334
25335 static void
25336 cp_parser_initial_pragma (cp_token *first_token)
25337 {
25338   tree name = NULL;
25339
25340   cp_lexer_get_preprocessor_token (NULL, first_token);
25341   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25342     return;
25343
25344   cp_lexer_get_preprocessor_token (NULL, first_token);
25345   if (first_token->type == CPP_STRING)
25346     {
25347       name = first_token->u.value;
25348
25349       cp_lexer_get_preprocessor_token (NULL, first_token);
25350       if (first_token->type != CPP_PRAGMA_EOL)
25351         error_at (first_token->location,
25352                   "junk at end of %<#pragma GCC pch_preprocess%>");
25353     }
25354   else
25355     error_at (first_token->location, "expected string literal");
25356
25357   /* Skip to the end of the pragma.  */
25358   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25359     cp_lexer_get_preprocessor_token (NULL, first_token);
25360
25361   /* Now actually load the PCH file.  */
25362   if (name)
25363     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25364
25365   /* Read one more token to return to our caller.  We have to do this
25366      after reading the PCH file in, since its pointers have to be
25367      live.  */
25368   cp_lexer_get_preprocessor_token (NULL, first_token);
25369 }
25370
25371 /* Normal parsing of a pragma token.  Here we can (and must) use the
25372    regular lexer.  */
25373
25374 static bool
25375 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25376 {
25377   cp_token *pragma_tok;
25378   unsigned int id;
25379
25380   pragma_tok = cp_lexer_consume_token (parser->lexer);
25381   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25382   parser->lexer->in_pragma = true;
25383
25384   id = pragma_tok->pragma_kind;
25385   switch (id)
25386     {
25387     case PRAGMA_GCC_PCH_PREPROCESS:
25388       error_at (pragma_tok->location,
25389                 "%<#pragma GCC pch_preprocess%> must be first");
25390       break;
25391
25392     case PRAGMA_OMP_BARRIER:
25393       switch (context)
25394         {
25395         case pragma_compound:
25396           cp_parser_omp_barrier (parser, pragma_tok);
25397           return false;
25398         case pragma_stmt:
25399           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25400                     "used in compound statements");
25401           break;
25402         default:
25403           goto bad_stmt;
25404         }
25405       break;
25406
25407     case PRAGMA_OMP_FLUSH:
25408       switch (context)
25409         {
25410         case pragma_compound:
25411           cp_parser_omp_flush (parser, pragma_tok);
25412           return false;
25413         case pragma_stmt:
25414           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25415                     "used in compound statements");
25416           break;
25417         default:
25418           goto bad_stmt;
25419         }
25420       break;
25421
25422     case PRAGMA_OMP_TASKWAIT:
25423       switch (context)
25424         {
25425         case pragma_compound:
25426           cp_parser_omp_taskwait (parser, pragma_tok);
25427           return false;
25428         case pragma_stmt:
25429           error_at (pragma_tok->location,
25430                     "%<#pragma omp taskwait%> may only be "
25431                     "used in compound statements");
25432           break;
25433         default:
25434           goto bad_stmt;
25435         }
25436       break;
25437
25438     case PRAGMA_OMP_THREADPRIVATE:
25439       cp_parser_omp_threadprivate (parser, pragma_tok);
25440       return false;
25441
25442     case PRAGMA_OMP_ATOMIC:
25443     case PRAGMA_OMP_CRITICAL:
25444     case PRAGMA_OMP_FOR:
25445     case PRAGMA_OMP_MASTER:
25446     case PRAGMA_OMP_ORDERED:
25447     case PRAGMA_OMP_PARALLEL:
25448     case PRAGMA_OMP_SECTIONS:
25449     case PRAGMA_OMP_SINGLE:
25450     case PRAGMA_OMP_TASK:
25451       if (context == pragma_external)
25452         goto bad_stmt;
25453       cp_parser_omp_construct (parser, pragma_tok);
25454       return true;
25455
25456     case PRAGMA_OMP_SECTION:
25457       error_at (pragma_tok->location, 
25458                 "%<#pragma omp section%> may only be used in "
25459                 "%<#pragma omp sections%> construct");
25460       break;
25461
25462     default:
25463       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25464       c_invoke_pragma_handler (id);
25465       break;
25466
25467     bad_stmt:
25468       cp_parser_error (parser, "expected declaration specifiers");
25469       break;
25470     }
25471
25472   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25473   return false;
25474 }
25475
25476 /* The interface the pragma parsers have to the lexer.  */
25477
25478 enum cpp_ttype
25479 pragma_lex (tree *value)
25480 {
25481   cp_token *tok;
25482   enum cpp_ttype ret;
25483
25484   tok = cp_lexer_peek_token (the_parser->lexer);
25485
25486   ret = tok->type;
25487   *value = tok->u.value;
25488
25489   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25490     ret = CPP_EOF;
25491   else if (ret == CPP_STRING)
25492     *value = cp_parser_string_literal (the_parser, false, false);
25493   else
25494     {
25495       cp_lexer_consume_token (the_parser->lexer);
25496       if (ret == CPP_KEYWORD)
25497         ret = CPP_NAME;
25498     }
25499
25500   return ret;
25501 }
25502
25503 \f
25504 /* External interface.  */
25505
25506 /* Parse one entire translation unit.  */
25507
25508 void
25509 c_parse_file (void)
25510 {
25511   static bool already_called = false;
25512
25513   if (already_called)
25514     {
25515       sorry ("inter-module optimizations not implemented for C++");
25516       return;
25517     }
25518   already_called = true;
25519
25520   the_parser = cp_parser_new ();
25521   push_deferring_access_checks (flag_access_control
25522                                 ? dk_no_deferred : dk_no_check);
25523   cp_parser_translation_unit (the_parser);
25524   the_parser = NULL;
25525 }
25526
25527 #include "gt-cp-parser.h"