OSDN Git Service

/cp
[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 #define unparsed_nsdmis \
1490   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1491
1492 static void
1493 push_unparsed_function_queues (cp_parser *parser)
1494 {
1495   VEC_safe_push (cp_unparsed_functions_entry, gc,
1496                  parser->unparsed_queues, NULL);
1497   unparsed_funs_with_default_args = NULL;
1498   unparsed_funs_with_definitions = make_tree_vector ();
1499   unparsed_nsdmis = NULL;
1500 }
1501
1502 static void
1503 pop_unparsed_function_queues (cp_parser *parser)
1504 {
1505   release_tree_vector (unparsed_funs_with_definitions);
1506   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1507 }
1508
1509 /* Prototypes.  */
1510
1511 /* Constructors and destructors.  */
1512
1513 static cp_parser *cp_parser_new
1514   (void);
1515
1516 /* Routines to parse various constructs.
1517
1518    Those that return `tree' will return the error_mark_node (rather
1519    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1520    Sometimes, they will return an ordinary node if error-recovery was
1521    attempted, even though a parse error occurred.  So, to check
1522    whether or not a parse error occurred, you should always use
1523    cp_parser_error_occurred.  If the construct is optional (indicated
1524    either by an `_opt' in the name of the function that does the
1525    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1526    the construct is not present.  */
1527
1528 /* Lexical conventions [gram.lex]  */
1529
1530 static tree cp_parser_identifier
1531   (cp_parser *);
1532 static tree cp_parser_string_literal
1533   (cp_parser *, bool, bool);
1534
1535 /* Basic concepts [gram.basic]  */
1536
1537 static bool cp_parser_translation_unit
1538   (cp_parser *);
1539
1540 /* Expressions [gram.expr]  */
1541
1542 static tree cp_parser_primary_expression
1543   (cp_parser *, bool, bool, bool, cp_id_kind *);
1544 static tree cp_parser_id_expression
1545   (cp_parser *, bool, bool, bool *, bool, bool);
1546 static tree cp_parser_unqualified_id
1547   (cp_parser *, bool, bool, bool, bool);
1548 static tree cp_parser_nested_name_specifier_opt
1549   (cp_parser *, bool, bool, bool, bool);
1550 static tree cp_parser_nested_name_specifier
1551   (cp_parser *, bool, bool, bool, bool);
1552 static tree cp_parser_qualifying_entity
1553   (cp_parser *, bool, bool, bool, bool, bool);
1554 static tree cp_parser_postfix_expression
1555   (cp_parser *, bool, bool, bool, cp_id_kind *);
1556 static tree cp_parser_postfix_open_square_expression
1557   (cp_parser *, tree, bool);
1558 static tree cp_parser_postfix_dot_deref_expression
1559   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1560 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1561   (cp_parser *, int, bool, bool, bool *);
1562 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1563 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1564 static void cp_parser_pseudo_destructor_name
1565   (cp_parser *, tree *, tree *);
1566 static tree cp_parser_unary_expression
1567   (cp_parser *, bool, bool, cp_id_kind *);
1568 static enum tree_code cp_parser_unary_operator
1569   (cp_token *);
1570 static tree cp_parser_new_expression
1571   (cp_parser *);
1572 static VEC(tree,gc) *cp_parser_new_placement
1573   (cp_parser *);
1574 static tree cp_parser_new_type_id
1575   (cp_parser *, tree *);
1576 static cp_declarator *cp_parser_new_declarator_opt
1577   (cp_parser *);
1578 static cp_declarator *cp_parser_direct_new_declarator
1579   (cp_parser *);
1580 static VEC(tree,gc) *cp_parser_new_initializer
1581   (cp_parser *);
1582 static tree cp_parser_delete_expression
1583   (cp_parser *);
1584 static tree cp_parser_cast_expression
1585   (cp_parser *, bool, bool, cp_id_kind *);
1586 static tree cp_parser_binary_expression
1587   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1588 static tree cp_parser_question_colon_clause
1589   (cp_parser *, tree);
1590 static tree cp_parser_assignment_expression
1591   (cp_parser *, bool, cp_id_kind *);
1592 static enum tree_code cp_parser_assignment_operator_opt
1593   (cp_parser *);
1594 static tree cp_parser_expression
1595   (cp_parser *, bool, cp_id_kind *);
1596 static tree cp_parser_constant_expression
1597   (cp_parser *, bool, bool *);
1598 static tree cp_parser_builtin_offsetof
1599   (cp_parser *);
1600 static tree cp_parser_lambda_expression
1601   (cp_parser *);
1602 static void cp_parser_lambda_introducer
1603   (cp_parser *, tree);
1604 static bool cp_parser_lambda_declarator_opt
1605   (cp_parser *, tree);
1606 static void cp_parser_lambda_body
1607   (cp_parser *, tree);
1608
1609 /* Statements [gram.stmt.stmt]  */
1610
1611 static void cp_parser_statement
1612   (cp_parser *, tree, bool, bool *);
1613 static void cp_parser_label_for_labeled_statement
1614   (cp_parser *);
1615 static tree cp_parser_expression_statement
1616   (cp_parser *, tree);
1617 static tree cp_parser_compound_statement
1618   (cp_parser *, tree, bool, bool);
1619 static void cp_parser_statement_seq_opt
1620   (cp_parser *, tree);
1621 static tree cp_parser_selection_statement
1622   (cp_parser *, bool *);
1623 static tree cp_parser_condition
1624   (cp_parser *);
1625 static tree cp_parser_iteration_statement
1626   (cp_parser *);
1627 static bool cp_parser_for_init_statement
1628   (cp_parser *, tree *decl);
1629 static tree cp_parser_for
1630   (cp_parser *);
1631 static tree cp_parser_c_for
1632   (cp_parser *, tree, tree);
1633 static tree cp_parser_range_for
1634   (cp_parser *, tree, tree, tree);
1635 static void do_range_for_auto_deduction
1636   (tree, tree);
1637 static tree cp_parser_perform_range_for_lookup
1638   (tree, tree *, tree *);
1639 static tree cp_parser_range_for_member_function
1640   (tree, tree);
1641 static tree cp_parser_jump_statement
1642   (cp_parser *);
1643 static void cp_parser_declaration_statement
1644   (cp_parser *);
1645
1646 static tree cp_parser_implicitly_scoped_statement
1647   (cp_parser *, bool *);
1648 static void cp_parser_already_scoped_statement
1649   (cp_parser *);
1650
1651 /* Declarations [gram.dcl.dcl] */
1652
1653 static void cp_parser_declaration_seq_opt
1654   (cp_parser *);
1655 static void cp_parser_declaration
1656   (cp_parser *);
1657 static void cp_parser_block_declaration
1658   (cp_parser *, bool);
1659 static void cp_parser_simple_declaration
1660   (cp_parser *, bool, tree *);
1661 static void cp_parser_decl_specifier_seq
1662   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1663 static tree cp_parser_storage_class_specifier_opt
1664   (cp_parser *);
1665 static tree cp_parser_function_specifier_opt
1666   (cp_parser *, cp_decl_specifier_seq *);
1667 static tree cp_parser_type_specifier
1668   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1669    int *, bool *);
1670 static tree cp_parser_simple_type_specifier
1671   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1672 static tree cp_parser_type_name
1673   (cp_parser *);
1674 static tree cp_parser_nonclass_name 
1675   (cp_parser* parser);
1676 static tree cp_parser_elaborated_type_specifier
1677   (cp_parser *, bool, bool);
1678 static tree cp_parser_enum_specifier
1679   (cp_parser *);
1680 static void cp_parser_enumerator_list
1681   (cp_parser *, tree);
1682 static void cp_parser_enumerator_definition
1683   (cp_parser *, tree);
1684 static tree cp_parser_namespace_name
1685   (cp_parser *);
1686 static void cp_parser_namespace_definition
1687   (cp_parser *);
1688 static void cp_parser_namespace_body
1689   (cp_parser *);
1690 static tree cp_parser_qualified_namespace_specifier
1691   (cp_parser *);
1692 static void cp_parser_namespace_alias_definition
1693   (cp_parser *);
1694 static bool cp_parser_using_declaration
1695   (cp_parser *, bool);
1696 static void cp_parser_using_directive
1697   (cp_parser *);
1698 static void cp_parser_asm_definition
1699   (cp_parser *);
1700 static void cp_parser_linkage_specification
1701   (cp_parser *);
1702 static void cp_parser_static_assert
1703   (cp_parser *, bool);
1704 static tree cp_parser_decltype
1705   (cp_parser *);
1706
1707 /* Declarators [gram.dcl.decl] */
1708
1709 static tree cp_parser_init_declarator
1710   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1711 static cp_declarator *cp_parser_declarator
1712   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1713 static cp_declarator *cp_parser_direct_declarator
1714   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1715 static enum tree_code cp_parser_ptr_operator
1716   (cp_parser *, tree *, cp_cv_quals *);
1717 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1718   (cp_parser *);
1719 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1720   (cp_parser *);
1721 static tree cp_parser_late_return_type_opt
1722   (cp_parser *, cp_cv_quals);
1723 static tree cp_parser_declarator_id
1724   (cp_parser *, bool);
1725 static tree cp_parser_type_id
1726   (cp_parser *);
1727 static tree cp_parser_template_type_arg
1728   (cp_parser *);
1729 static tree cp_parser_trailing_type_id (cp_parser *);
1730 static tree cp_parser_type_id_1
1731   (cp_parser *, bool, bool);
1732 static void cp_parser_type_specifier_seq
1733   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1734 static tree cp_parser_parameter_declaration_clause
1735   (cp_parser *);
1736 static tree cp_parser_parameter_declaration_list
1737   (cp_parser *, bool *);
1738 static cp_parameter_declarator *cp_parser_parameter_declaration
1739   (cp_parser *, bool, bool *);
1740 static tree cp_parser_default_argument 
1741   (cp_parser *, bool);
1742 static void cp_parser_function_body
1743   (cp_parser *);
1744 static tree cp_parser_initializer
1745   (cp_parser *, bool *, bool *);
1746 static tree cp_parser_initializer_clause
1747   (cp_parser *, bool *);
1748 static tree cp_parser_braced_list
1749   (cp_parser*, bool*);
1750 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1751   (cp_parser *, bool *);
1752
1753 static bool cp_parser_ctor_initializer_opt_and_function_body
1754   (cp_parser *);
1755
1756 /* Classes [gram.class] */
1757
1758 static tree cp_parser_class_name
1759   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1760 static tree cp_parser_class_specifier
1761   (cp_parser *);
1762 static tree cp_parser_class_head
1763   (cp_parser *, bool *, tree *, tree *);
1764 static enum tag_types cp_parser_class_key
1765   (cp_parser *);
1766 static void cp_parser_member_specification_opt
1767   (cp_parser *);
1768 static void cp_parser_member_declaration
1769   (cp_parser *);
1770 static tree cp_parser_pure_specifier
1771   (cp_parser *);
1772 static tree cp_parser_constant_initializer
1773   (cp_parser *);
1774
1775 /* Derived classes [gram.class.derived] */
1776
1777 static tree cp_parser_base_clause
1778   (cp_parser *);
1779 static tree cp_parser_base_specifier
1780   (cp_parser *);
1781
1782 /* Special member functions [gram.special] */
1783
1784 static tree cp_parser_conversion_function_id
1785   (cp_parser *);
1786 static tree cp_parser_conversion_type_id
1787   (cp_parser *);
1788 static cp_declarator *cp_parser_conversion_declarator_opt
1789   (cp_parser *);
1790 static bool cp_parser_ctor_initializer_opt
1791   (cp_parser *);
1792 static void cp_parser_mem_initializer_list
1793   (cp_parser *);
1794 static tree cp_parser_mem_initializer
1795   (cp_parser *);
1796 static tree cp_parser_mem_initializer_id
1797   (cp_parser *);
1798
1799 /* Overloading [gram.over] */
1800
1801 static tree cp_parser_operator_function_id
1802   (cp_parser *);
1803 static tree cp_parser_operator
1804   (cp_parser *);
1805
1806 /* Templates [gram.temp] */
1807
1808 static void cp_parser_template_declaration
1809   (cp_parser *, bool);
1810 static tree cp_parser_template_parameter_list
1811   (cp_parser *);
1812 static tree cp_parser_template_parameter
1813   (cp_parser *, bool *, bool *);
1814 static tree cp_parser_type_parameter
1815   (cp_parser *, bool *);
1816 static tree cp_parser_template_id
1817   (cp_parser *, bool, bool, bool);
1818 static tree cp_parser_template_name
1819   (cp_parser *, bool, bool, bool, bool *);
1820 static tree cp_parser_template_argument_list
1821   (cp_parser *);
1822 static tree cp_parser_template_argument
1823   (cp_parser *);
1824 static void cp_parser_explicit_instantiation
1825   (cp_parser *);
1826 static void cp_parser_explicit_specialization
1827   (cp_parser *);
1828
1829 /* Exception handling [gram.exception] */
1830
1831 static tree cp_parser_try_block
1832   (cp_parser *);
1833 static bool cp_parser_function_try_block
1834   (cp_parser *);
1835 static void cp_parser_handler_seq
1836   (cp_parser *);
1837 static void cp_parser_handler
1838   (cp_parser *);
1839 static tree cp_parser_exception_declaration
1840   (cp_parser *);
1841 static tree cp_parser_throw_expression
1842   (cp_parser *);
1843 static tree cp_parser_exception_specification_opt
1844   (cp_parser *);
1845 static tree cp_parser_type_id_list
1846   (cp_parser *);
1847
1848 /* GNU Extensions */
1849
1850 static tree cp_parser_asm_specification_opt
1851   (cp_parser *);
1852 static tree cp_parser_asm_operand_list
1853   (cp_parser *);
1854 static tree cp_parser_asm_clobber_list
1855   (cp_parser *);
1856 static tree cp_parser_asm_label_list
1857   (cp_parser *);
1858 static tree cp_parser_attributes_opt
1859   (cp_parser *);
1860 static tree cp_parser_attribute_list
1861   (cp_parser *);
1862 static bool cp_parser_extension_opt
1863   (cp_parser *, int *);
1864 static void cp_parser_label_declaration
1865   (cp_parser *);
1866
1867 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868 static bool cp_parser_pragma
1869   (cp_parser *, enum pragma_context);
1870
1871 /* Objective-C++ Productions */
1872
1873 static tree cp_parser_objc_message_receiver
1874   (cp_parser *);
1875 static tree cp_parser_objc_message_args
1876   (cp_parser *);
1877 static tree cp_parser_objc_message_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_encode_expression
1880   (cp_parser *);
1881 static tree cp_parser_objc_defs_expression
1882   (cp_parser *);
1883 static tree cp_parser_objc_protocol_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_selector_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_expression
1888   (cp_parser *);
1889 static bool cp_parser_objc_selector_p
1890   (enum cpp_ttype);
1891 static tree cp_parser_objc_selector
1892   (cp_parser *);
1893 static tree cp_parser_objc_protocol_refs_opt
1894   (cp_parser *);
1895 static void cp_parser_objc_declaration
1896   (cp_parser *, tree);
1897 static tree cp_parser_objc_statement
1898   (cp_parser *);
1899 static bool cp_parser_objc_valid_prefix_attributes
1900   (cp_parser *, tree *);
1901 static void cp_parser_objc_at_property_declaration 
1902   (cp_parser *) ;
1903 static void cp_parser_objc_at_synthesize_declaration 
1904   (cp_parser *) ;
1905 static void cp_parser_objc_at_dynamic_declaration
1906   (cp_parser *) ;
1907 static tree cp_parser_objc_struct_declaration
1908   (cp_parser *) ;
1909
1910 /* Utility Routines */
1911
1912 static tree cp_parser_lookup_name
1913   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1914 static tree cp_parser_lookup_name_simple
1915   (cp_parser *, tree, location_t);
1916 static tree cp_parser_maybe_treat_template_as_class
1917   (tree, bool);
1918 static bool cp_parser_check_declarator_template_parameters
1919   (cp_parser *, cp_declarator *, location_t);
1920 static bool cp_parser_check_template_parameters
1921   (cp_parser *, unsigned, location_t, cp_declarator *);
1922 static tree cp_parser_simple_cast_expression
1923   (cp_parser *);
1924 static tree cp_parser_global_scope_opt
1925   (cp_parser *, bool);
1926 static bool cp_parser_constructor_declarator_p
1927   (cp_parser *, bool);
1928 static tree cp_parser_function_definition_from_specifiers_and_declarator
1929   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1930 static tree cp_parser_function_definition_after_declarator
1931   (cp_parser *, bool);
1932 static void cp_parser_template_declaration_after_export
1933   (cp_parser *, bool);
1934 static void cp_parser_perform_template_parameter_access_checks
1935   (VEC (deferred_access_check,gc)*);
1936 static tree cp_parser_single_declaration
1937   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1938 static tree cp_parser_functional_cast
1939   (cp_parser *, tree);
1940 static tree cp_parser_save_member_function_body
1941   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1942 static tree cp_parser_save_nsdmi
1943   (cp_parser *);
1944 static tree cp_parser_enclosed_template_argument_list
1945   (cp_parser *);
1946 static void cp_parser_save_default_args
1947   (cp_parser *, tree);
1948 static void cp_parser_late_parsing_for_member
1949   (cp_parser *, tree);
1950 static tree cp_parser_late_parse_one_default_arg
1951   (cp_parser *, tree, tree, tree);
1952 static void cp_parser_late_parsing_nsdmi
1953   (cp_parser *, tree);
1954 static void cp_parser_late_parsing_default_args
1955   (cp_parser *, tree);
1956 static tree cp_parser_sizeof_operand
1957   (cp_parser *, enum rid);
1958 static tree cp_parser_trait_expr
1959   (cp_parser *, enum rid);
1960 static bool cp_parser_declares_only_class_p
1961   (cp_parser *);
1962 static void cp_parser_set_storage_class
1963   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1964 static void cp_parser_set_decl_spec_type
1965   (cp_decl_specifier_seq *, tree, location_t, bool);
1966 static bool cp_parser_friend_p
1967   (const cp_decl_specifier_seq *);
1968 static void cp_parser_required_error
1969   (cp_parser *, required_token, bool);
1970 static cp_token *cp_parser_require
1971   (cp_parser *, enum cpp_ttype, required_token);
1972 static cp_token *cp_parser_require_keyword
1973   (cp_parser *, enum rid, required_token);
1974 static bool cp_parser_token_starts_function_definition_p
1975   (cp_token *);
1976 static bool cp_parser_next_token_starts_class_definition_p
1977   (cp_parser *);
1978 static bool cp_parser_next_token_ends_template_argument_p
1979   (cp_parser *);
1980 static bool cp_parser_nth_token_starts_template_argument_list_p
1981   (cp_parser *, size_t);
1982 static enum tag_types cp_parser_token_is_class_key
1983   (cp_token *);
1984 static void cp_parser_check_class_key
1985   (enum tag_types, tree type);
1986 static void cp_parser_check_access_in_redeclaration
1987   (tree type, location_t location);
1988 static bool cp_parser_optional_template_keyword
1989   (cp_parser *);
1990 static void cp_parser_pre_parsed_nested_name_specifier
1991   (cp_parser *);
1992 static bool cp_parser_cache_group
1993   (cp_parser *, enum cpp_ttype, unsigned);
1994 static void cp_parser_parse_tentatively
1995   (cp_parser *);
1996 static void cp_parser_commit_to_tentative_parse
1997   (cp_parser *);
1998 static void cp_parser_abort_tentative_parse
1999   (cp_parser *);
2000 static bool cp_parser_parse_definitely
2001   (cp_parser *);
2002 static inline bool cp_parser_parsing_tentatively
2003   (cp_parser *);
2004 static bool cp_parser_uncommitted_to_tentative_parse_p
2005   (cp_parser *);
2006 static void cp_parser_error
2007   (cp_parser *, const char *);
2008 static void cp_parser_name_lookup_error
2009   (cp_parser *, tree, tree, name_lookup_error, location_t);
2010 static bool cp_parser_simulate_error
2011   (cp_parser *);
2012 static bool cp_parser_check_type_definition
2013   (cp_parser *);
2014 static void cp_parser_check_for_definition_in_return_type
2015   (cp_declarator *, tree, location_t type_location);
2016 static void cp_parser_check_for_invalid_template_id
2017   (cp_parser *, tree, location_t location);
2018 static bool cp_parser_non_integral_constant_expression
2019   (cp_parser *, non_integral_constant);
2020 static void cp_parser_diagnose_invalid_type_name
2021   (cp_parser *, tree, tree, location_t);
2022 static bool cp_parser_parse_and_diagnose_invalid_type_name
2023   (cp_parser *);
2024 static int cp_parser_skip_to_closing_parenthesis
2025   (cp_parser *, bool, bool, bool);
2026 static void cp_parser_skip_to_end_of_statement
2027   (cp_parser *);
2028 static void cp_parser_consume_semicolon_at_end_of_statement
2029   (cp_parser *);
2030 static void cp_parser_skip_to_end_of_block_or_statement
2031   (cp_parser *);
2032 static bool cp_parser_skip_to_closing_brace
2033   (cp_parser *);
2034 static void cp_parser_skip_to_end_of_template_parameter_list
2035   (cp_parser *);
2036 static void cp_parser_skip_to_pragma_eol
2037   (cp_parser*, cp_token *);
2038 static bool cp_parser_error_occurred
2039   (cp_parser *);
2040 static bool cp_parser_allow_gnu_extensions_p
2041   (cp_parser *);
2042 static bool cp_parser_is_string_literal
2043   (cp_token *);
2044 static bool cp_parser_is_keyword
2045   (cp_token *, enum rid);
2046 static tree cp_parser_make_typename_type
2047   (cp_parser *, tree, tree, location_t location);
2048 static cp_declarator * cp_parser_make_indirect_declarator
2049   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2050
2051 /* Returns nonzero if we are parsing tentatively.  */
2052
2053 static inline bool
2054 cp_parser_parsing_tentatively (cp_parser* parser)
2055 {
2056   return parser->context->next != NULL;
2057 }
2058
2059 /* Returns nonzero if TOKEN is a string literal.  */
2060
2061 static bool
2062 cp_parser_is_string_literal (cp_token* token)
2063 {
2064   return (token->type == CPP_STRING ||
2065           token->type == CPP_STRING16 ||
2066           token->type == CPP_STRING32 ||
2067           token->type == CPP_WSTRING ||
2068           token->type == CPP_UTF8STRING);
2069 }
2070
2071 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2072
2073 static bool
2074 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2075 {
2076   return token->keyword == keyword;
2077 }
2078
2079 /* If not parsing tentatively, issue a diagnostic of the form
2080       FILE:LINE: MESSAGE before TOKEN
2081    where TOKEN is the next token in the input stream.  MESSAGE
2082    (specified by the caller) is usually of the form "expected
2083    OTHER-TOKEN".  */
2084
2085 static void
2086 cp_parser_error (cp_parser* parser, const char* gmsgid)
2087 {
2088   if (!cp_parser_simulate_error (parser))
2089     {
2090       cp_token *token = cp_lexer_peek_token (parser->lexer);
2091       /* This diagnostic makes more sense if it is tagged to the line
2092          of the token we just peeked at.  */
2093       cp_lexer_set_source_position_from_token (token);
2094
2095       if (token->type == CPP_PRAGMA)
2096         {
2097           error_at (token->location,
2098                     "%<#pragma%> is not allowed here");
2099           cp_parser_skip_to_pragma_eol (parser, token);
2100           return;
2101         }
2102
2103       c_parse_error (gmsgid,
2104                      /* Because c_parser_error does not understand
2105                         CPP_KEYWORD, keywords are treated like
2106                         identifiers.  */
2107                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2108                      token->u.value, token->flags);
2109     }
2110 }
2111
2112 /* Issue an error about name-lookup failing.  NAME is the
2113    IDENTIFIER_NODE DECL is the result of
2114    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2115    the thing that we hoped to find.  */
2116
2117 static void
2118 cp_parser_name_lookup_error (cp_parser* parser,
2119                              tree name,
2120                              tree decl,
2121                              name_lookup_error desired,
2122                              location_t location)
2123 {
2124   /* If name lookup completely failed, tell the user that NAME was not
2125      declared.  */
2126   if (decl == error_mark_node)
2127     {
2128       if (parser->scope && parser->scope != global_namespace)
2129         error_at (location, "%<%E::%E%> has not been declared",
2130                   parser->scope, name);
2131       else if (parser->scope == global_namespace)
2132         error_at (location, "%<::%E%> has not been declared", name);
2133       else if (parser->object_scope
2134                && !CLASS_TYPE_P (parser->object_scope))
2135         error_at (location, "request for member %qE in non-class type %qT",
2136                   name, parser->object_scope);
2137       else if (parser->object_scope)
2138         error_at (location, "%<%T::%E%> has not been declared",
2139                   parser->object_scope, name);
2140       else
2141         error_at (location, "%qE has not been declared", name);
2142     }
2143   else if (parser->scope && parser->scope != global_namespace)
2144     {
2145       switch (desired)
2146         {
2147           case NLE_TYPE:
2148             error_at (location, "%<%E::%E%> is not a type",
2149                                 parser->scope, name);
2150             break;
2151           case NLE_CXX98:
2152             error_at (location, "%<%E::%E%> is not a class or namespace",
2153                                 parser->scope, name);
2154             break;
2155           case NLE_NOT_CXX98:
2156             error_at (location,
2157                       "%<%E::%E%> is not a class, namespace, or enumeration",
2158                       parser->scope, name);
2159             break;
2160           default:
2161             gcc_unreachable ();
2162             
2163         }
2164     }
2165   else if (parser->scope == global_namespace)
2166     {
2167       switch (desired)
2168         {
2169           case NLE_TYPE:
2170             error_at (location, "%<::%E%> is not a type", name);
2171             break;
2172           case NLE_CXX98:
2173             error_at (location, "%<::%E%> is not a class or namespace", name);
2174             break;
2175           case NLE_NOT_CXX98:
2176             error_at (location,
2177                       "%<::%E%> is not a class, namespace, or enumeration",
2178                       name);
2179             break;
2180           default:
2181             gcc_unreachable ();
2182         }
2183     }
2184   else
2185     {
2186       switch (desired)
2187         {
2188           case NLE_TYPE:
2189             error_at (location, "%qE is not a type", name);
2190             break;
2191           case NLE_CXX98:
2192             error_at (location, "%qE is not a class or namespace", name);
2193             break;
2194           case NLE_NOT_CXX98:
2195             error_at (location,
2196                       "%qE is not a class, namespace, or enumeration", name);
2197             break;
2198           default:
2199             gcc_unreachable ();
2200         }
2201     }
2202 }
2203
2204 /* If we are parsing tentatively, remember that an error has occurred
2205    during this tentative parse.  Returns true if the error was
2206    simulated; false if a message should be issued by the caller.  */
2207
2208 static bool
2209 cp_parser_simulate_error (cp_parser* parser)
2210 {
2211   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2212     {
2213       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2214       return true;
2215     }
2216   return false;
2217 }
2218
2219 /* Check for repeated decl-specifiers.  */
2220
2221 static void
2222 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2223                            location_t location)
2224 {
2225   int ds;
2226
2227   for (ds = ds_first; ds != ds_last; ++ds)
2228     {
2229       unsigned count = decl_specs->specs[ds];
2230       if (count < 2)
2231         continue;
2232       /* The "long" specifier is a special case because of "long long".  */
2233       if (ds == ds_long)
2234         {
2235           if (count > 2)
2236             error_at (location, "%<long long long%> is too long for GCC");
2237           else 
2238             pedwarn_cxx98 (location, OPT_Wlong_long, 
2239                            "ISO C++ 1998 does not support %<long long%>");
2240         }
2241       else if (count > 1)
2242         {
2243           static const char *const decl_spec_names[] = {
2244             "signed",
2245             "unsigned",
2246             "short",
2247             "long",
2248             "const",
2249             "volatile",
2250             "restrict",
2251             "inline",
2252             "virtual",
2253             "explicit",
2254             "friend",
2255             "typedef",
2256             "constexpr",
2257             "__complex",
2258             "__thread"
2259           };
2260           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2261         }
2262     }
2263 }
2264
2265 /* This function is called when a type is defined.  If type
2266    definitions are forbidden at this point, an error message is
2267    issued.  */
2268
2269 static bool
2270 cp_parser_check_type_definition (cp_parser* parser)
2271 {
2272   /* If types are forbidden here, issue a message.  */
2273   if (parser->type_definition_forbidden_message)
2274     {
2275       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2276          in the message need to be interpreted.  */
2277       error (parser->type_definition_forbidden_message);
2278       return false;
2279     }
2280   return true;
2281 }
2282
2283 /* This function is called when the DECLARATOR is processed.  The TYPE
2284    was a type defined in the decl-specifiers.  If it is invalid to
2285    define a type in the decl-specifiers for DECLARATOR, an error is
2286    issued. TYPE_LOCATION is the location of TYPE and is used
2287    for error reporting.  */
2288
2289 static void
2290 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2291                                                tree type, location_t type_location)
2292 {
2293   /* [dcl.fct] forbids type definitions in return types.
2294      Unfortunately, it's not easy to know whether or not we are
2295      processing a return type until after the fact.  */
2296   while (declarator
2297          && (declarator->kind == cdk_pointer
2298              || declarator->kind == cdk_reference
2299              || declarator->kind == cdk_ptrmem))
2300     declarator = declarator->declarator;
2301   if (declarator
2302       && declarator->kind == cdk_function)
2303     {
2304       error_at (type_location,
2305                 "new types may not be defined in a return type");
2306       inform (type_location, 
2307               "(perhaps a semicolon is missing after the definition of %qT)",
2308               type);
2309     }
2310 }
2311
2312 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2313    "<" in any valid C++ program.  If the next token is indeed "<",
2314    issue a message warning the user about what appears to be an
2315    invalid attempt to form a template-id. LOCATION is the location
2316    of the type-specifier (TYPE) */
2317
2318 static void
2319 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2320                                          tree type, location_t location)
2321 {
2322   cp_token_position start = 0;
2323
2324   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2325     {
2326       if (TYPE_P (type))
2327         error_at (location, "%qT is not a template", type);
2328       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2329         error_at (location, "%qE is not a template", type);
2330       else
2331         error_at (location, "invalid template-id");
2332       /* Remember the location of the invalid "<".  */
2333       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2334         start = cp_lexer_token_position (parser->lexer, true);
2335       /* Consume the "<".  */
2336       cp_lexer_consume_token (parser->lexer);
2337       /* Parse the template arguments.  */
2338       cp_parser_enclosed_template_argument_list (parser);
2339       /* Permanently remove the invalid template arguments so that
2340          this error message is not issued again.  */
2341       if (start)
2342         cp_lexer_purge_tokens_after (parser->lexer, start);
2343     }
2344 }
2345
2346 /* If parsing an integral constant-expression, issue an error message
2347    about the fact that THING appeared and return true.  Otherwise,
2348    return false.  In either case, set
2349    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2350
2351 static bool
2352 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2353                                             non_integral_constant thing)
2354 {
2355   parser->non_integral_constant_expression_p = true;
2356   if (parser->integral_constant_expression_p)
2357     {
2358       if (!parser->allow_non_integral_constant_expression_p)
2359         {
2360           const char *msg = NULL;
2361           switch (thing)
2362             {
2363               case NIC_FLOAT:
2364                 error ("floating-point literal "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_CAST:
2368                 error ("a cast to a type other than an integral or "
2369                        "enumeration type cannot appear in a "
2370                        "constant-expression");
2371                 return true;
2372               case NIC_TYPEID:
2373                 error ("%<typeid%> operator "
2374                        "cannot appear in a constant-expression");
2375                 return true;
2376               case NIC_NCC:
2377                 error ("non-constant compound literals "
2378                        "cannot appear in a constant-expression");
2379                 return true;
2380               case NIC_FUNC_CALL:
2381                 error ("a function call "
2382                        "cannot appear in a constant-expression");
2383                 return true;
2384               case NIC_INC:
2385                 error ("an increment "
2386                        "cannot appear in a constant-expression");
2387                 return true;
2388               case NIC_DEC:
2389                 error ("an decrement "
2390                        "cannot appear in a constant-expression");
2391                 return true;
2392               case NIC_ARRAY_REF:
2393                 error ("an array reference "
2394                        "cannot appear in a constant-expression");
2395                 return true;
2396               case NIC_ADDR_LABEL:
2397                 error ("the address of a label "
2398                        "cannot appear in a constant-expression");
2399                 return true;
2400               case NIC_OVERLOADED:
2401                 error ("calls to overloaded operators "
2402                        "cannot appear in a constant-expression");
2403                 return true;
2404               case NIC_ASSIGNMENT:
2405                 error ("an assignment cannot appear in a constant-expression");
2406                 return true;
2407               case NIC_COMMA:
2408                 error ("a comma operator "
2409                        "cannot appear in a constant-expression");
2410                 return true;
2411               case NIC_CONSTRUCTOR:
2412                 error ("a call to a constructor "
2413                        "cannot appear in a constant-expression");
2414                 return true;
2415               case NIC_THIS:
2416                 msg = "this";
2417                 break;
2418               case NIC_FUNC_NAME:
2419                 msg = "__FUNCTION__";
2420                 break;
2421               case NIC_PRETTY_FUNC:
2422                 msg = "__PRETTY_FUNCTION__";
2423                 break;
2424               case NIC_C99_FUNC:
2425                 msg = "__func__";
2426                 break;
2427               case NIC_VA_ARG:
2428                 msg = "va_arg";
2429                 break;
2430               case NIC_ARROW:
2431                 msg = "->";
2432                 break;
2433               case NIC_POINT:
2434                 msg = ".";
2435                 break;
2436               case NIC_STAR:
2437                 msg = "*";
2438                 break;
2439               case NIC_ADDR:
2440                 msg = "&";
2441                 break;
2442               case NIC_PREINCREMENT:
2443                 msg = "++";
2444                 break;
2445               case NIC_PREDECREMENT:
2446                 msg = "--";
2447                 break;
2448               case NIC_NEW:
2449                 msg = "new";
2450                 break;
2451               case NIC_DEL:
2452                 msg = "delete";
2453                 break;
2454               default:
2455                 gcc_unreachable ();
2456             }
2457           if (msg)
2458             error ("%qs cannot appear in a constant-expression", msg);
2459           return true;
2460         }
2461     }
2462   return false;
2463 }
2464
2465 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2466    qualifying scope (or NULL, if none) for ID.  This function commits
2467    to the current active tentative parse, if any.  (Otherwise, the
2468    problematic construct might be encountered again later, resulting
2469    in duplicate error messages.) LOCATION is the location of ID.  */
2470
2471 static void
2472 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2473                                       tree scope, tree id,
2474                                       location_t location)
2475 {
2476   tree decl, old_scope;
2477   cp_parser_commit_to_tentative_parse (parser);
2478   /* Try to lookup the identifier.  */
2479   old_scope = parser->scope;
2480   parser->scope = scope;
2481   decl = cp_parser_lookup_name_simple (parser, id, location);
2482   parser->scope = old_scope;
2483   /* If the lookup found a template-name, it means that the user forgot
2484   to specify an argument list. Emit a useful error message.  */
2485   if (TREE_CODE (decl) == TEMPLATE_DECL)
2486     error_at (location,
2487               "invalid use of template-name %qE without an argument list",
2488               decl);
2489   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2490     error_at (location, "invalid use of destructor %qD as a type", id);
2491   else if (TREE_CODE (decl) == TYPE_DECL)
2492     /* Something like 'unsigned A a;'  */
2493     error_at (location, "invalid combination of multiple type-specifiers");
2494   else if (!parser->scope)
2495     {
2496       /* Issue an error message.  */
2497       error_at (location, "%qE does not name a type", id);
2498       /* If we're in a template class, it's possible that the user was
2499          referring to a type from a base class.  For example:
2500
2501            template <typename T> struct A { typedef T X; };
2502            template <typename T> struct B : public A<T> { X x; };
2503
2504          The user should have said "typename A<T>::X".  */
2505       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2506         inform (location, "C++0x %<constexpr%> only available with "
2507                 "-std=c++0x or -std=gnu++0x");
2508       else if (processing_template_decl && current_class_type
2509                && TYPE_BINFO (current_class_type))
2510         {
2511           tree b;
2512
2513           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2514                b;
2515                b = TREE_CHAIN (b))
2516             {
2517               tree base_type = BINFO_TYPE (b);
2518               if (CLASS_TYPE_P (base_type)
2519                   && dependent_type_p (base_type))
2520                 {
2521                   tree field;
2522                   /* Go from a particular instantiation of the
2523                      template (which will have an empty TYPE_FIELDs),
2524                      to the main version.  */
2525                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2526                   for (field = TYPE_FIELDS (base_type);
2527                        field;
2528                        field = DECL_CHAIN (field))
2529                     if (TREE_CODE (field) == TYPE_DECL
2530                         && DECL_NAME (field) == id)
2531                       {
2532                         inform (location, 
2533                                 "(perhaps %<typename %T::%E%> was intended)",
2534                                 BINFO_TYPE (b), id);
2535                         break;
2536                       }
2537                   if (field)
2538                     break;
2539                 }
2540             }
2541         }
2542     }
2543   /* Here we diagnose qualified-ids where the scope is actually correct,
2544      but the identifier does not resolve to a valid type name.  */
2545   else if (parser->scope != error_mark_node)
2546     {
2547       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2548         error_at (location, "%qE in namespace %qE does not name a type",
2549                   id, parser->scope);
2550       else if (CLASS_TYPE_P (parser->scope)
2551                && constructor_name_p (id, parser->scope))
2552         {
2553           /* A<T>::A<T>() */
2554           error_at (location, "%<%T::%E%> names the constructor, not"
2555                     " the type", parser->scope, id);
2556           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2557             error_at (location, "and %qT has no template constructors",
2558                       parser->scope);
2559         }
2560       else if (TYPE_P (parser->scope)
2561                && dependent_scope_p (parser->scope))
2562         error_at (location, "need %<typename%> before %<%T::%E%> because "
2563                   "%qT is a dependent scope",
2564                   parser->scope, id, parser->scope);
2565       else if (TYPE_P (parser->scope))
2566         error_at (location, "%qE in %q#T does not name a type",
2567                   id, parser->scope);
2568       else
2569         gcc_unreachable ();
2570     }
2571 }
2572
2573 /* Check for a common situation where a type-name should be present,
2574    but is not, and issue a sensible error message.  Returns true if an
2575    invalid type-name was detected.
2576
2577    The situation handled by this function are variable declarations of the
2578    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2579    Usually, `ID' should name a type, but if we got here it means that it
2580    does not. We try to emit the best possible error message depending on
2581    how exactly the id-expression looks like.  */
2582
2583 static bool
2584 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2585 {
2586   tree id;
2587   cp_token *token = cp_lexer_peek_token (parser->lexer);
2588
2589   /* Avoid duplicate error about ambiguous lookup.  */
2590   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2591     {
2592       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2593       if (next->type == CPP_NAME && next->ambiguous_p)
2594         goto out;
2595     }
2596
2597   cp_parser_parse_tentatively (parser);
2598   id = cp_parser_id_expression (parser,
2599                                 /*template_keyword_p=*/false,
2600                                 /*check_dependency_p=*/true,
2601                                 /*template_p=*/NULL,
2602                                 /*declarator_p=*/true,
2603                                 /*optional_p=*/false);
2604   /* If the next token is a (, this is a function with no explicit return
2605      type, i.e. constructor, destructor or conversion op.  */
2606   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2607       || TREE_CODE (id) == TYPE_DECL)
2608     {
2609       cp_parser_abort_tentative_parse (parser);
2610       return false;
2611     }
2612   if (!cp_parser_parse_definitely (parser))
2613     return false;
2614
2615   /* Emit a diagnostic for the invalid type.  */
2616   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2617                                         id, token->location);
2618  out:
2619   /* If we aren't in the middle of a declarator (i.e. in a
2620      parameter-declaration-clause), skip to the end of the declaration;
2621      there's no point in trying to process it.  */
2622   if (!parser->in_declarator_p)
2623     cp_parser_skip_to_end_of_block_or_statement (parser);
2624   return true;
2625 }
2626
2627 /* Consume tokens up to, and including, the next non-nested closing `)'.
2628    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2629    are doing error recovery. Returns -1 if OR_COMMA is true and we
2630    found an unnested comma.  */
2631
2632 static int
2633 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2634                                        bool recovering,
2635                                        bool or_comma,
2636                                        bool consume_paren)
2637 {
2638   unsigned paren_depth = 0;
2639   unsigned brace_depth = 0;
2640   unsigned square_depth = 0;
2641
2642   if (recovering && !or_comma
2643       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2644     return 0;
2645
2646   while (true)
2647     {
2648       cp_token * token = cp_lexer_peek_token (parser->lexer);
2649
2650       switch (token->type)
2651         {
2652         case CPP_EOF:
2653         case CPP_PRAGMA_EOL:
2654           /* If we've run out of tokens, then there is no closing `)'.  */
2655           return 0;
2656
2657         /* This is good for lambda expression capture-lists.  */
2658         case CPP_OPEN_SQUARE:
2659           ++square_depth;
2660           break;
2661         case CPP_CLOSE_SQUARE:
2662           if (!square_depth--)
2663             return 0;
2664           break;
2665
2666         case CPP_SEMICOLON:
2667           /* This matches the processing in skip_to_end_of_statement.  */
2668           if (!brace_depth)
2669             return 0;
2670           break;
2671
2672         case CPP_OPEN_BRACE:
2673           ++brace_depth;
2674           break;
2675         case CPP_CLOSE_BRACE:
2676           if (!brace_depth--)
2677             return 0;
2678           break;
2679
2680         case CPP_COMMA:
2681           if (recovering && or_comma && !brace_depth && !paren_depth
2682               && !square_depth)
2683             return -1;
2684           break;
2685
2686         case CPP_OPEN_PAREN:
2687           if (!brace_depth)
2688             ++paren_depth;
2689           break;
2690
2691         case CPP_CLOSE_PAREN:
2692           if (!brace_depth && !paren_depth--)
2693             {
2694               if (consume_paren)
2695                 cp_lexer_consume_token (parser->lexer);
2696               return 1;
2697             }
2698           break;
2699
2700         default:
2701           break;
2702         }
2703
2704       /* Consume the token.  */
2705       cp_lexer_consume_token (parser->lexer);
2706     }
2707 }
2708
2709 /* Consume tokens until we reach the end of the current statement.
2710    Normally, that will be just before consuming a `;'.  However, if a
2711    non-nested `}' comes first, then we stop before consuming that.  */
2712
2713 static void
2714 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2715 {
2716   unsigned nesting_depth = 0;
2717
2718   while (true)
2719     {
2720       cp_token *token = cp_lexer_peek_token (parser->lexer);
2721
2722       switch (token->type)
2723         {
2724         case CPP_EOF:
2725         case CPP_PRAGMA_EOL:
2726           /* If we've run out of tokens, stop.  */
2727           return;
2728
2729         case CPP_SEMICOLON:
2730           /* If the next token is a `;', we have reached the end of the
2731              statement.  */
2732           if (!nesting_depth)
2733             return;
2734           break;
2735
2736         case CPP_CLOSE_BRACE:
2737           /* If this is a non-nested '}', stop before consuming it.
2738              That way, when confronted with something like:
2739
2740                { 3 + }
2741
2742              we stop before consuming the closing '}', even though we
2743              have not yet reached a `;'.  */
2744           if (nesting_depth == 0)
2745             return;
2746
2747           /* If it is the closing '}' for a block that we have
2748              scanned, stop -- but only after consuming the token.
2749              That way given:
2750
2751                 void f g () { ... }
2752                 typedef int I;
2753
2754              we will stop after the body of the erroneously declared
2755              function, but before consuming the following `typedef'
2756              declaration.  */
2757           if (--nesting_depth == 0)
2758             {
2759               cp_lexer_consume_token (parser->lexer);
2760               return;
2761             }
2762
2763         case CPP_OPEN_BRACE:
2764           ++nesting_depth;
2765           break;
2766
2767         default:
2768           break;
2769         }
2770
2771       /* Consume the token.  */
2772       cp_lexer_consume_token (parser->lexer);
2773     }
2774 }
2775
2776 /* This function is called at the end of a statement or declaration.
2777    If the next token is a semicolon, it is consumed; otherwise, error
2778    recovery is attempted.  */
2779
2780 static void
2781 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2782 {
2783   /* Look for the trailing `;'.  */
2784   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2785     {
2786       /* If there is additional (erroneous) input, skip to the end of
2787          the statement.  */
2788       cp_parser_skip_to_end_of_statement (parser);
2789       /* If the next token is now a `;', consume it.  */
2790       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2791         cp_lexer_consume_token (parser->lexer);
2792     }
2793 }
2794
2795 /* Skip tokens until we have consumed an entire block, or until we
2796    have consumed a non-nested `;'.  */
2797
2798 static void
2799 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2800 {
2801   int nesting_depth = 0;
2802
2803   while (nesting_depth >= 0)
2804     {
2805       cp_token *token = cp_lexer_peek_token (parser->lexer);
2806
2807       switch (token->type)
2808         {
2809         case CPP_EOF:
2810         case CPP_PRAGMA_EOL:
2811           /* If we've run out of tokens, stop.  */
2812           return;
2813
2814         case CPP_SEMICOLON:
2815           /* Stop if this is an unnested ';'. */
2816           if (!nesting_depth)
2817             nesting_depth = -1;
2818           break;
2819
2820         case CPP_CLOSE_BRACE:
2821           /* Stop if this is an unnested '}', or closes the outermost
2822              nesting level.  */
2823           nesting_depth--;
2824           if (nesting_depth < 0)
2825             return;
2826           if (!nesting_depth)
2827             nesting_depth = -1;
2828           break;
2829
2830         case CPP_OPEN_BRACE:
2831           /* Nest. */
2832           nesting_depth++;
2833           break;
2834
2835         default:
2836           break;
2837         }
2838
2839       /* Consume the token.  */
2840       cp_lexer_consume_token (parser->lexer);
2841     }
2842 }
2843
2844 /* Skip tokens until a non-nested closing curly brace is the next
2845    token, or there are no more tokens. Return true in the first case,
2846    false otherwise.  */
2847
2848 static bool
2849 cp_parser_skip_to_closing_brace (cp_parser *parser)
2850 {
2851   unsigned nesting_depth = 0;
2852
2853   while (true)
2854     {
2855       cp_token *token = cp_lexer_peek_token (parser->lexer);
2856
2857       switch (token->type)
2858         {
2859         case CPP_EOF:
2860         case CPP_PRAGMA_EOL:
2861           /* If we've run out of tokens, stop.  */
2862           return false;
2863
2864         case CPP_CLOSE_BRACE:
2865           /* If the next token is a non-nested `}', then we have reached
2866              the end of the current block.  */
2867           if (nesting_depth-- == 0)
2868             return true;
2869           break;
2870
2871         case CPP_OPEN_BRACE:
2872           /* If it the next token is a `{', then we are entering a new
2873              block.  Consume the entire block.  */
2874           ++nesting_depth;
2875           break;
2876
2877         default:
2878           break;
2879         }
2880
2881       /* Consume the token.  */
2882       cp_lexer_consume_token (parser->lexer);
2883     }
2884 }
2885
2886 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2887    parameter is the PRAGMA token, allowing us to purge the entire pragma
2888    sequence.  */
2889
2890 static void
2891 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2892 {
2893   cp_token *token;
2894
2895   parser->lexer->in_pragma = false;
2896
2897   do
2898     token = cp_lexer_consume_token (parser->lexer);
2899   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2900
2901   /* Ensure that the pragma is not parsed again.  */
2902   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2903 }
2904
2905 /* Require pragma end of line, resyncing with it as necessary.  The
2906    arguments are as for cp_parser_skip_to_pragma_eol.  */
2907
2908 static void
2909 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2910 {
2911   parser->lexer->in_pragma = false;
2912   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2913     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2914 }
2915
2916 /* This is a simple wrapper around make_typename_type. When the id is
2917    an unresolved identifier node, we can provide a superior diagnostic
2918    using cp_parser_diagnose_invalid_type_name.  */
2919
2920 static tree
2921 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2922                               tree id, location_t id_location)
2923 {
2924   tree result;
2925   if (TREE_CODE (id) == IDENTIFIER_NODE)
2926     {
2927       result = make_typename_type (scope, id, typename_type,
2928                                    /*complain=*/tf_none);
2929       if (result == error_mark_node)
2930         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2931       return result;
2932     }
2933   return make_typename_type (scope, id, typename_type, tf_error);
2934 }
2935
2936 /* This is a wrapper around the
2937    make_{pointer,ptrmem,reference}_declarator functions that decides
2938    which one to call based on the CODE and CLASS_TYPE arguments. The
2939    CODE argument should be one of the values returned by
2940    cp_parser_ptr_operator. */
2941 static cp_declarator *
2942 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2943                                     cp_cv_quals cv_qualifiers,
2944                                     cp_declarator *target)
2945 {
2946   if (code == ERROR_MARK)
2947     return cp_error_declarator;
2948
2949   if (code == INDIRECT_REF)
2950     if (class_type == NULL_TREE)
2951       return make_pointer_declarator (cv_qualifiers, target);
2952     else
2953       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2954   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2955     return make_reference_declarator (cv_qualifiers, target, false);
2956   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2957     return make_reference_declarator (cv_qualifiers, target, true);
2958   gcc_unreachable ();
2959 }
2960
2961 /* Create a new C++ parser.  */
2962
2963 static cp_parser *
2964 cp_parser_new (void)
2965 {
2966   cp_parser *parser;
2967   cp_lexer *lexer;
2968   unsigned i;
2969
2970   /* cp_lexer_new_main is called before doing GC allocation because
2971      cp_lexer_new_main might load a PCH file.  */
2972   lexer = cp_lexer_new_main ();
2973
2974   /* Initialize the binops_by_token so that we can get the tree
2975      directly from the token.  */
2976   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2977     binops_by_token[binops[i].token_type] = binops[i];
2978
2979   parser = ggc_alloc_cleared_cp_parser ();
2980   parser->lexer = lexer;
2981   parser->context = cp_parser_context_new (NULL);
2982
2983   /* For now, we always accept GNU extensions.  */
2984   parser->allow_gnu_extensions_p = 1;
2985
2986   /* The `>' token is a greater-than operator, not the end of a
2987      template-id.  */
2988   parser->greater_than_is_operator_p = true;
2989
2990   parser->default_arg_ok_p = true;
2991
2992   /* We are not parsing a constant-expression.  */
2993   parser->integral_constant_expression_p = false;
2994   parser->allow_non_integral_constant_expression_p = false;
2995   parser->non_integral_constant_expression_p = false;
2996
2997   /* Local variable names are not forbidden.  */
2998   parser->local_variables_forbidden_p = false;
2999
3000   /* We are not processing an `extern "C"' declaration.  */
3001   parser->in_unbraced_linkage_specification_p = false;
3002
3003   /* We are not processing a declarator.  */
3004   parser->in_declarator_p = false;
3005
3006   /* We are not processing a template-argument-list.  */
3007   parser->in_template_argument_list_p = false;
3008
3009   /* We are not in an iteration statement.  */
3010   parser->in_statement = 0;
3011
3012   /* We are not in a switch statement.  */
3013   parser->in_switch_statement_p = false;
3014
3015   /* We are not parsing a type-id inside an expression.  */
3016   parser->in_type_id_in_expr_p = false;
3017
3018   /* Declarations aren't implicitly extern "C".  */
3019   parser->implicit_extern_c = false;
3020
3021   /* String literals should be translated to the execution character set.  */
3022   parser->translate_strings_p = true;
3023
3024   /* We are not parsing a function body.  */
3025   parser->in_function_body = false;
3026
3027   /* We can correct until told otherwise.  */
3028   parser->colon_corrects_to_scope_p = true;
3029
3030   /* The unparsed function queue is empty.  */
3031   push_unparsed_function_queues (parser);
3032
3033   /* There are no classes being defined.  */
3034   parser->num_classes_being_defined = 0;
3035
3036   /* No template parameters apply.  */
3037   parser->num_template_parameter_lists = 0;
3038
3039   return parser;
3040 }
3041
3042 /* Create a cp_lexer structure which will emit the tokens in CACHE
3043    and push it onto the parser's lexer stack.  This is used for delayed
3044    parsing of in-class method bodies and default arguments, and should
3045    not be confused with tentative parsing.  */
3046 static void
3047 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3048 {
3049   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3050   lexer->next = parser->lexer;
3051   parser->lexer = lexer;
3052
3053   /* Move the current source position to that of the first token in the
3054      new lexer.  */
3055   cp_lexer_set_source_position_from_token (lexer->next_token);
3056 }
3057
3058 /* Pop the top lexer off the parser stack.  This is never used for the
3059    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3060 static void
3061 cp_parser_pop_lexer (cp_parser *parser)
3062 {
3063   cp_lexer *lexer = parser->lexer;
3064   parser->lexer = lexer->next;
3065   cp_lexer_destroy (lexer);
3066
3067   /* Put the current source position back where it was before this
3068      lexer was pushed.  */
3069   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3070 }
3071
3072 /* Lexical conventions [gram.lex]  */
3073
3074 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3075    identifier.  */
3076
3077 static tree
3078 cp_parser_identifier (cp_parser* parser)
3079 {
3080   cp_token *token;
3081
3082   /* Look for the identifier.  */
3083   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3084   /* Return the value.  */
3085   return token ? token->u.value : error_mark_node;
3086 }
3087
3088 /* Parse a sequence of adjacent string constants.  Returns a
3089    TREE_STRING representing the combined, nul-terminated string
3090    constant.  If TRANSLATE is true, translate the string to the
3091    execution character set.  If WIDE_OK is true, a wide string is
3092    invalid here.
3093
3094    C++98 [lex.string] says that if a narrow string literal token is
3095    adjacent to a wide string literal token, the behavior is undefined.
3096    However, C99 6.4.5p4 says that this results in a wide string literal.
3097    We follow C99 here, for consistency with the C front end.
3098
3099    This code is largely lifted from lex_string() in c-lex.c.
3100
3101    FUTURE: ObjC++ will need to handle @-strings here.  */
3102 static tree
3103 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3104 {
3105   tree value;
3106   size_t count;
3107   struct obstack str_ob;
3108   cpp_string str, istr, *strs;
3109   cp_token *tok;
3110   enum cpp_ttype type;
3111
3112   tok = cp_lexer_peek_token (parser->lexer);
3113   if (!cp_parser_is_string_literal (tok))
3114     {
3115       cp_parser_error (parser, "expected string-literal");
3116       return error_mark_node;
3117     }
3118
3119   type = tok->type;
3120
3121   /* Try to avoid the overhead of creating and destroying an obstack
3122      for the common case of just one string.  */
3123   if (!cp_parser_is_string_literal
3124       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3125     {
3126       cp_lexer_consume_token (parser->lexer);
3127
3128       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3129       str.len = TREE_STRING_LENGTH (tok->u.value);
3130       count = 1;
3131
3132       strs = &str;
3133     }
3134   else
3135     {
3136       gcc_obstack_init (&str_ob);
3137       count = 0;
3138
3139       do
3140         {
3141           cp_lexer_consume_token (parser->lexer);
3142           count++;
3143           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3144           str.len = TREE_STRING_LENGTH (tok->u.value);
3145
3146           if (type != tok->type)
3147             {
3148               if (type == CPP_STRING)
3149                 type = tok->type;
3150               else if (tok->type != CPP_STRING)
3151                 error_at (tok->location,
3152                           "unsupported non-standard concatenation "
3153                           "of string literals");
3154             }
3155
3156           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3157
3158           tok = cp_lexer_peek_token (parser->lexer);
3159         }
3160       while (cp_parser_is_string_literal (tok));
3161
3162       strs = (cpp_string *) obstack_finish (&str_ob);
3163     }
3164
3165   if (type != CPP_STRING && !wide_ok)
3166     {
3167       cp_parser_error (parser, "a wide string is invalid in this context");
3168       type = CPP_STRING;
3169     }
3170
3171   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3172       (parse_in, strs, count, &istr, type))
3173     {
3174       value = build_string (istr.len, (const char *)istr.text);
3175       free (CONST_CAST (unsigned char *, istr.text));
3176
3177       switch (type)
3178         {
3179         default:
3180         case CPP_STRING:
3181         case CPP_UTF8STRING:
3182           TREE_TYPE (value) = char_array_type_node;
3183           break;
3184         case CPP_STRING16:
3185           TREE_TYPE (value) = char16_array_type_node;
3186           break;
3187         case CPP_STRING32:
3188           TREE_TYPE (value) = char32_array_type_node;
3189           break;
3190         case CPP_WSTRING:
3191           TREE_TYPE (value) = wchar_array_type_node;
3192           break;
3193         }
3194
3195       value = fix_string_type (value);
3196     }
3197   else
3198     /* cpp_interpret_string has issued an error.  */
3199     value = error_mark_node;
3200
3201   if (count > 1)
3202     obstack_free (&str_ob, 0);
3203
3204   return value;
3205 }
3206
3207
3208 /* Basic concepts [gram.basic]  */
3209
3210 /* Parse a translation-unit.
3211
3212    translation-unit:
3213      declaration-seq [opt]
3214
3215    Returns TRUE if all went well.  */
3216
3217 static bool
3218 cp_parser_translation_unit (cp_parser* parser)
3219 {
3220   /* The address of the first non-permanent object on the declarator
3221      obstack.  */
3222   static void *declarator_obstack_base;
3223
3224   bool success;
3225
3226   /* Create the declarator obstack, if necessary.  */
3227   if (!cp_error_declarator)
3228     {
3229       gcc_obstack_init (&declarator_obstack);
3230       /* Create the error declarator.  */
3231       cp_error_declarator = make_declarator (cdk_error);
3232       /* Create the empty parameter list.  */
3233       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3234       /* Remember where the base of the declarator obstack lies.  */
3235       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3236     }
3237
3238   cp_parser_declaration_seq_opt (parser);
3239
3240   /* If there are no tokens left then all went well.  */
3241   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3242     {
3243       /* Get rid of the token array; we don't need it any more.  */
3244       cp_lexer_destroy (parser->lexer);
3245       parser->lexer = NULL;
3246
3247       /* This file might have been a context that's implicitly extern
3248          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3249       if (parser->implicit_extern_c)
3250         {
3251           pop_lang_context ();
3252           parser->implicit_extern_c = false;
3253         }
3254
3255       /* Finish up.  */
3256       finish_translation_unit ();
3257
3258       success = true;
3259     }
3260   else
3261     {
3262       cp_parser_error (parser, "expected declaration");
3263       success = false;
3264     }
3265
3266   /* Make sure the declarator obstack was fully cleaned up.  */
3267   gcc_assert (obstack_next_free (&declarator_obstack)
3268               == declarator_obstack_base);
3269
3270   /* All went well.  */
3271   return success;
3272 }
3273
3274 /* Expressions [gram.expr] */
3275
3276 /* Parse a primary-expression.
3277
3278    primary-expression:
3279      literal
3280      this
3281      ( expression )
3282      id-expression
3283
3284    GNU Extensions:
3285
3286    primary-expression:
3287      ( compound-statement )
3288      __builtin_va_arg ( assignment-expression , type-id )
3289      __builtin_offsetof ( type-id , offsetof-expression )
3290
3291    C++ Extensions:
3292      __has_nothrow_assign ( type-id )   
3293      __has_nothrow_constructor ( type-id )
3294      __has_nothrow_copy ( type-id )
3295      __has_trivial_assign ( type-id )   
3296      __has_trivial_constructor ( type-id )
3297      __has_trivial_copy ( type-id )
3298      __has_trivial_destructor ( type-id )
3299      __has_virtual_destructor ( type-id )     
3300      __is_abstract ( type-id )
3301      __is_base_of ( type-id , type-id )
3302      __is_class ( type-id )
3303      __is_convertible_to ( type-id , type-id )     
3304      __is_empty ( type-id )
3305      __is_enum ( type-id )
3306      __is_literal_type ( type-id )
3307      __is_pod ( type-id )
3308      __is_polymorphic ( type-id )
3309      __is_std_layout ( type-id )
3310      __is_trivial ( type-id )
3311      __is_union ( type-id )
3312
3313    Objective-C++ Extension:
3314
3315    primary-expression:
3316      objc-expression
3317
3318    literal:
3319      __null
3320
3321    ADDRESS_P is true iff this expression was immediately preceded by
3322    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3323    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3324    true iff this expression is a template argument.
3325
3326    Returns a representation of the expression.  Upon return, *IDK
3327    indicates what kind of id-expression (if any) was present.  */
3328
3329 static tree
3330 cp_parser_primary_expression (cp_parser *parser,
3331                               bool address_p,
3332                               bool cast_p,
3333                               bool template_arg_p,
3334                               cp_id_kind *idk)
3335 {
3336   cp_token *token = NULL;
3337
3338   /* Assume the primary expression is not an id-expression.  */
3339   *idk = CP_ID_KIND_NONE;
3340
3341   /* Peek at the next token.  */
3342   token = cp_lexer_peek_token (parser->lexer);
3343   switch (token->type)
3344     {
3345       /* literal:
3346            integer-literal
3347            character-literal
3348            floating-literal
3349            string-literal
3350            boolean-literal  */
3351     case CPP_CHAR:
3352     case CPP_CHAR16:
3353     case CPP_CHAR32:
3354     case CPP_WCHAR:
3355     case CPP_NUMBER:
3356       token = cp_lexer_consume_token (parser->lexer);
3357       if (TREE_CODE (token->u.value) == FIXED_CST)
3358         {
3359           error_at (token->location,
3360                     "fixed-point types not supported in C++");
3361           return error_mark_node;
3362         }
3363       /* Floating-point literals are only allowed in an integral
3364          constant expression if they are cast to an integral or
3365          enumeration type.  */
3366       if (TREE_CODE (token->u.value) == REAL_CST
3367           && parser->integral_constant_expression_p
3368           && pedantic)
3369         {
3370           /* CAST_P will be set even in invalid code like "int(2.7 +
3371              ...)".   Therefore, we have to check that the next token
3372              is sure to end the cast.  */
3373           if (cast_p)
3374             {
3375               cp_token *next_token;
3376
3377               next_token = cp_lexer_peek_token (parser->lexer);
3378               if (/* The comma at the end of an
3379                      enumerator-definition.  */
3380                   next_token->type != CPP_COMMA
3381                   /* The curly brace at the end of an enum-specifier.  */
3382                   && next_token->type != CPP_CLOSE_BRACE
3383                   /* The end of a statement.  */
3384                   && next_token->type != CPP_SEMICOLON
3385                   /* The end of the cast-expression.  */
3386                   && next_token->type != CPP_CLOSE_PAREN
3387                   /* The end of an array bound.  */
3388                   && next_token->type != CPP_CLOSE_SQUARE
3389                   /* The closing ">" in a template-argument-list.  */
3390                   && (next_token->type != CPP_GREATER
3391                       || parser->greater_than_is_operator_p)
3392                   /* C++0x only: A ">>" treated like two ">" tokens,
3393                      in a template-argument-list.  */
3394                   && (next_token->type != CPP_RSHIFT
3395                       || (cxx_dialect == cxx98)
3396                       || parser->greater_than_is_operator_p))
3397                 cast_p = false;
3398             }
3399
3400           /* If we are within a cast, then the constraint that the
3401              cast is to an integral or enumeration type will be
3402              checked at that point.  If we are not within a cast, then
3403              this code is invalid.  */
3404           if (!cast_p)
3405             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3406         }
3407       return token->u.value;
3408
3409     case CPP_STRING:
3410     case CPP_STRING16:
3411     case CPP_STRING32:
3412     case CPP_WSTRING:
3413     case CPP_UTF8STRING:
3414       /* ??? Should wide strings be allowed when parser->translate_strings_p
3415          is false (i.e. in attributes)?  If not, we can kill the third
3416          argument to cp_parser_string_literal.  */
3417       return cp_parser_string_literal (parser,
3418                                        parser->translate_strings_p,
3419                                        true);
3420
3421     case CPP_OPEN_PAREN:
3422       {
3423         tree expr;
3424         bool saved_greater_than_is_operator_p;
3425
3426         /* Consume the `('.  */
3427         cp_lexer_consume_token (parser->lexer);
3428         /* Within a parenthesized expression, a `>' token is always
3429            the greater-than operator.  */
3430         saved_greater_than_is_operator_p
3431           = parser->greater_than_is_operator_p;
3432         parser->greater_than_is_operator_p = true;
3433         /* If we see `( { ' then we are looking at the beginning of
3434            a GNU statement-expression.  */
3435         if (cp_parser_allow_gnu_extensions_p (parser)
3436             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3437           {
3438             /* Statement-expressions are not allowed by the standard.  */
3439             pedwarn (token->location, OPT_pedantic, 
3440                      "ISO C++ forbids braced-groups within expressions");
3441
3442             /* And they're not allowed outside of a function-body; you
3443                cannot, for example, write:
3444
3445                  int i = ({ int j = 3; j + 1; });
3446
3447                at class or namespace scope.  */
3448             if (!parser->in_function_body
3449                 || parser->in_template_argument_list_p)
3450               {
3451                 error_at (token->location,
3452                           "statement-expressions are not allowed outside "
3453                           "functions nor in template-argument lists");
3454                 cp_parser_skip_to_end_of_block_or_statement (parser);
3455                 expr = error_mark_node;
3456               }
3457             else
3458               {
3459                 /* Start the statement-expression.  */
3460                 expr = begin_stmt_expr ();
3461                 /* Parse the compound-statement.  */
3462                 cp_parser_compound_statement (parser, expr, false, false);
3463                 /* Finish up.  */
3464                 expr = finish_stmt_expr (expr, false);
3465               }
3466           }
3467         else
3468           {
3469             /* Parse the parenthesized expression.  */
3470             expr = cp_parser_expression (parser, cast_p, idk);
3471             /* Let the front end know that this expression was
3472                enclosed in parentheses. This matters in case, for
3473                example, the expression is of the form `A::B', since
3474                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3475                not.  */
3476             finish_parenthesized_expr (expr);
3477             /* DR 705: Wrapping an unqualified name in parentheses
3478                suppresses arg-dependent lookup.  We want to pass back
3479                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3480                (c++/37862), but none of the others.  */
3481             if (*idk != CP_ID_KIND_QUALIFIED)
3482               *idk = CP_ID_KIND_NONE;
3483           }
3484         /* The `>' token might be the end of a template-id or
3485            template-parameter-list now.  */
3486         parser->greater_than_is_operator_p
3487           = saved_greater_than_is_operator_p;
3488         /* Consume the `)'.  */
3489         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3490           cp_parser_skip_to_end_of_statement (parser);
3491
3492         return expr;
3493       }
3494
3495     case CPP_OPEN_SQUARE:
3496       if (c_dialect_objc ())
3497         /* We have an Objective-C++ message. */
3498         return cp_parser_objc_expression (parser);
3499       {
3500         tree lam = cp_parser_lambda_expression (parser);
3501         /* Don't warn about a failed tentative parse.  */
3502         if (cp_parser_error_occurred (parser))
3503           return error_mark_node;
3504         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3505         return lam;
3506       }
3507
3508     case CPP_OBJC_STRING:
3509       if (c_dialect_objc ())
3510         /* We have an Objective-C++ string literal. */
3511         return cp_parser_objc_expression (parser);
3512       cp_parser_error (parser, "expected primary-expression");
3513       return error_mark_node;
3514
3515     case CPP_KEYWORD:
3516       switch (token->keyword)
3517         {
3518           /* These two are the boolean literals.  */
3519         case RID_TRUE:
3520           cp_lexer_consume_token (parser->lexer);
3521           return boolean_true_node;
3522         case RID_FALSE:
3523           cp_lexer_consume_token (parser->lexer);
3524           return boolean_false_node;
3525
3526           /* The `__null' literal.  */
3527         case RID_NULL:
3528           cp_lexer_consume_token (parser->lexer);
3529           return null_node;
3530
3531           /* The `nullptr' literal.  */
3532         case RID_NULLPTR:
3533           cp_lexer_consume_token (parser->lexer);
3534           return nullptr_node;
3535
3536           /* Recognize the `this' keyword.  */
3537         case RID_THIS:
3538           cp_lexer_consume_token (parser->lexer);
3539           if (parser->local_variables_forbidden_p)
3540             {
3541               error_at (token->location,
3542                         "%<this%> may not be used in this context");
3543               return error_mark_node;
3544             }
3545           /* Pointers cannot appear in constant-expressions.  */
3546           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3547             return error_mark_node;
3548           return finish_this_expr ();
3549
3550           /* The `operator' keyword can be the beginning of an
3551              id-expression.  */
3552         case RID_OPERATOR:
3553           goto id_expression;
3554
3555         case RID_FUNCTION_NAME:
3556         case RID_PRETTY_FUNCTION_NAME:
3557         case RID_C99_FUNCTION_NAME:
3558           {
3559             non_integral_constant name;
3560
3561             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3562                __func__ are the names of variables -- but they are
3563                treated specially.  Therefore, they are handled here,
3564                rather than relying on the generic id-expression logic
3565                below.  Grammatically, these names are id-expressions.
3566
3567                Consume the token.  */
3568             token = cp_lexer_consume_token (parser->lexer);
3569
3570             switch (token->keyword)
3571               {
3572               case RID_FUNCTION_NAME:
3573                 name = NIC_FUNC_NAME;
3574                 break;
3575               case RID_PRETTY_FUNCTION_NAME:
3576                 name = NIC_PRETTY_FUNC;
3577                 break;
3578               case RID_C99_FUNCTION_NAME:
3579                 name = NIC_C99_FUNC;
3580                 break;
3581               default:
3582                 gcc_unreachable ();
3583               }
3584
3585             if (cp_parser_non_integral_constant_expression (parser, name))
3586               return error_mark_node;
3587
3588             /* Look up the name.  */
3589             return finish_fname (token->u.value);
3590           }
3591
3592         case RID_VA_ARG:
3593           {
3594             tree expression;
3595             tree type;
3596
3597             /* The `__builtin_va_arg' construct is used to handle
3598                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3599             cp_lexer_consume_token (parser->lexer);
3600             /* Look for the opening `('.  */
3601             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3602             /* Now, parse the assignment-expression.  */
3603             expression = cp_parser_assignment_expression (parser,
3604                                                           /*cast_p=*/false, NULL);
3605             /* Look for the `,'.  */
3606             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3607             /* Parse the type-id.  */
3608             type = cp_parser_type_id (parser);
3609             /* Look for the closing `)'.  */
3610             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3611             /* Using `va_arg' in a constant-expression is not
3612                allowed.  */
3613             if (cp_parser_non_integral_constant_expression (parser,
3614                                                             NIC_VA_ARG))
3615               return error_mark_node;
3616             return build_x_va_arg (expression, type);
3617           }
3618
3619         case RID_OFFSETOF:
3620           return cp_parser_builtin_offsetof (parser);
3621
3622         case RID_HAS_NOTHROW_ASSIGN:
3623         case RID_HAS_NOTHROW_CONSTRUCTOR:
3624         case RID_HAS_NOTHROW_COPY:        
3625         case RID_HAS_TRIVIAL_ASSIGN:
3626         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3627         case RID_HAS_TRIVIAL_COPY:        
3628         case RID_HAS_TRIVIAL_DESTRUCTOR:
3629         case RID_HAS_VIRTUAL_DESTRUCTOR:
3630         case RID_IS_ABSTRACT:
3631         case RID_IS_BASE_OF:
3632         case RID_IS_CLASS:
3633         case RID_IS_CONVERTIBLE_TO:
3634         case RID_IS_EMPTY:
3635         case RID_IS_ENUM:
3636         case RID_IS_LITERAL_TYPE:
3637         case RID_IS_POD:
3638         case RID_IS_POLYMORPHIC:
3639         case RID_IS_STD_LAYOUT:
3640         case RID_IS_TRIVIAL:
3641         case RID_IS_UNION:
3642           return cp_parser_trait_expr (parser, token->keyword);
3643
3644         /* Objective-C++ expressions.  */
3645         case RID_AT_ENCODE:
3646         case RID_AT_PROTOCOL:
3647         case RID_AT_SELECTOR:
3648           return cp_parser_objc_expression (parser);
3649
3650         case RID_TEMPLATE:
3651           if (parser->in_function_body
3652               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3653                   == CPP_LESS))
3654             {
3655               error_at (token->location,
3656                         "a template declaration cannot appear at block scope");
3657               cp_parser_skip_to_end_of_block_or_statement (parser);
3658               return error_mark_node;
3659             }
3660         default:
3661           cp_parser_error (parser, "expected primary-expression");
3662           return error_mark_node;
3663         }
3664
3665       /* An id-expression can start with either an identifier, a
3666          `::' as the beginning of a qualified-id, or the "operator"
3667          keyword.  */
3668     case CPP_NAME:
3669     case CPP_SCOPE:
3670     case CPP_TEMPLATE_ID:
3671     case CPP_NESTED_NAME_SPECIFIER:
3672       {
3673         tree id_expression;
3674         tree decl;
3675         const char *error_msg;
3676         bool template_p;
3677         bool done;
3678         cp_token *id_expr_token;
3679
3680       id_expression:
3681         /* Parse the id-expression.  */
3682         id_expression
3683           = cp_parser_id_expression (parser,
3684                                      /*template_keyword_p=*/false,
3685                                      /*check_dependency_p=*/true,
3686                                      &template_p,
3687                                      /*declarator_p=*/false,
3688                                      /*optional_p=*/false);
3689         if (id_expression == error_mark_node)
3690           return error_mark_node;
3691         id_expr_token = token;
3692         token = cp_lexer_peek_token (parser->lexer);
3693         done = (token->type != CPP_OPEN_SQUARE
3694                 && token->type != CPP_OPEN_PAREN
3695                 && token->type != CPP_DOT
3696                 && token->type != CPP_DEREF
3697                 && token->type != CPP_PLUS_PLUS
3698                 && token->type != CPP_MINUS_MINUS);
3699         /* If we have a template-id, then no further lookup is
3700            required.  If the template-id was for a template-class, we
3701            will sometimes have a TYPE_DECL at this point.  */
3702         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3703                  || TREE_CODE (id_expression) == TYPE_DECL)
3704           decl = id_expression;
3705         /* Look up the name.  */
3706         else
3707           {
3708             tree ambiguous_decls;
3709
3710             /* If we already know that this lookup is ambiguous, then
3711                we've already issued an error message; there's no reason
3712                to check again.  */
3713             if (id_expr_token->type == CPP_NAME
3714                 && id_expr_token->ambiguous_p)
3715               {
3716                 cp_parser_simulate_error (parser);
3717                 return error_mark_node;
3718               }
3719
3720             decl = cp_parser_lookup_name (parser, id_expression,
3721                                           none_type,
3722                                           template_p,
3723                                           /*is_namespace=*/false,
3724                                           /*check_dependency=*/true,
3725                                           &ambiguous_decls,
3726                                           id_expr_token->location);
3727             /* If the lookup was ambiguous, an error will already have
3728                been issued.  */
3729             if (ambiguous_decls)
3730               return error_mark_node;
3731
3732             /* In Objective-C++, we may have an Objective-C 2.0
3733                dot-syntax for classes here.  */
3734             if (c_dialect_objc ()
3735                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3736                 && TREE_CODE (decl) == TYPE_DECL
3737                 && objc_is_class_name (decl))
3738               {
3739                 tree component;
3740                 cp_lexer_consume_token (parser->lexer);
3741                 component = cp_parser_identifier (parser);
3742                 if (component == error_mark_node)
3743                   return error_mark_node;
3744
3745                 return objc_build_class_component_ref (id_expression, component);
3746               }
3747
3748             /* In Objective-C++, an instance variable (ivar) may be preferred
3749                to whatever cp_parser_lookup_name() found.  */
3750             decl = objc_lookup_ivar (decl, id_expression);
3751
3752             /* If name lookup gives us a SCOPE_REF, then the
3753                qualifying scope was dependent.  */
3754             if (TREE_CODE (decl) == SCOPE_REF)
3755               {
3756                 /* At this point, we do not know if DECL is a valid
3757                    integral constant expression.  We assume that it is
3758                    in fact such an expression, so that code like:
3759
3760                       template <int N> struct A {
3761                         int a[B<N>::i];
3762                       };
3763                      
3764                    is accepted.  At template-instantiation time, we
3765                    will check that B<N>::i is actually a constant.  */
3766                 return decl;
3767               }
3768             /* Check to see if DECL is a local variable in a context
3769                where that is forbidden.  */
3770             if (parser->local_variables_forbidden_p
3771                 && local_variable_p (decl))
3772               {
3773                 /* It might be that we only found DECL because we are
3774                    trying to be generous with pre-ISO scoping rules.
3775                    For example, consider:
3776
3777                      int i;
3778                      void g() {
3779                        for (int i = 0; i < 10; ++i) {}
3780                        extern void f(int j = i);
3781                      }
3782
3783                    Here, name look up will originally find the out
3784                    of scope `i'.  We need to issue a warning message,
3785                    but then use the global `i'.  */
3786                 decl = check_for_out_of_scope_variable (decl);
3787                 if (local_variable_p (decl))
3788                   {
3789                     error_at (id_expr_token->location,
3790                               "local variable %qD may not appear in this context",
3791                               decl);
3792                     return error_mark_node;
3793                   }
3794               }
3795           }
3796
3797         decl = (finish_id_expression
3798                 (id_expression, decl, parser->scope,
3799                  idk,
3800                  parser->integral_constant_expression_p,
3801                  parser->allow_non_integral_constant_expression_p,
3802                  &parser->non_integral_constant_expression_p,
3803                  template_p, done, address_p,
3804                  template_arg_p,
3805                  &error_msg,
3806                  id_expr_token->location));
3807         if (error_msg)
3808           cp_parser_error (parser, error_msg);
3809         return decl;
3810       }
3811
3812       /* Anything else is an error.  */
3813     default:
3814       cp_parser_error (parser, "expected primary-expression");
3815       return error_mark_node;
3816     }
3817 }
3818
3819 /* Parse an id-expression.
3820
3821    id-expression:
3822      unqualified-id
3823      qualified-id
3824
3825    qualified-id:
3826      :: [opt] nested-name-specifier template [opt] unqualified-id
3827      :: identifier
3828      :: operator-function-id
3829      :: template-id
3830
3831    Return a representation of the unqualified portion of the
3832    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3833    a `::' or nested-name-specifier.
3834
3835    Often, if the id-expression was a qualified-id, the caller will
3836    want to make a SCOPE_REF to represent the qualified-id.  This
3837    function does not do this in order to avoid wastefully creating
3838    SCOPE_REFs when they are not required.
3839
3840    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3841    `template' keyword.
3842
3843    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3844    uninstantiated templates.
3845
3846    If *TEMPLATE_P is non-NULL, it is set to true iff the
3847    `template' keyword is used to explicitly indicate that the entity
3848    named is a template.
3849
3850    If DECLARATOR_P is true, the id-expression is appearing as part of
3851    a declarator, rather than as part of an expression.  */
3852
3853 static tree
3854 cp_parser_id_expression (cp_parser *parser,
3855                          bool template_keyword_p,
3856                          bool check_dependency_p,
3857                          bool *template_p,
3858                          bool declarator_p,
3859                          bool optional_p)
3860 {
3861   bool global_scope_p;
3862   bool nested_name_specifier_p;
3863
3864   /* Assume the `template' keyword was not used.  */
3865   if (template_p)
3866     *template_p = template_keyword_p;
3867
3868   /* Look for the optional `::' operator.  */
3869   global_scope_p
3870     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3871        != NULL_TREE);
3872   /* Look for the optional nested-name-specifier.  */
3873   nested_name_specifier_p
3874     = (cp_parser_nested_name_specifier_opt (parser,
3875                                             /*typename_keyword_p=*/false,
3876                                             check_dependency_p,
3877                                             /*type_p=*/false,
3878                                             declarator_p)
3879        != NULL_TREE);
3880   /* If there is a nested-name-specifier, then we are looking at
3881      the first qualified-id production.  */
3882   if (nested_name_specifier_p)
3883     {
3884       tree saved_scope;
3885       tree saved_object_scope;
3886       tree saved_qualifying_scope;
3887       tree unqualified_id;
3888       bool is_template;
3889
3890       /* See if the next token is the `template' keyword.  */
3891       if (!template_p)
3892         template_p = &is_template;
3893       *template_p = cp_parser_optional_template_keyword (parser);
3894       /* Name lookup we do during the processing of the
3895          unqualified-id might obliterate SCOPE.  */
3896       saved_scope = parser->scope;
3897       saved_object_scope = parser->object_scope;
3898       saved_qualifying_scope = parser->qualifying_scope;
3899       /* Process the final unqualified-id.  */
3900       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3901                                                  check_dependency_p,
3902                                                  declarator_p,
3903                                                  /*optional_p=*/false);
3904       /* Restore the SAVED_SCOPE for our caller.  */
3905       parser->scope = saved_scope;
3906       parser->object_scope = saved_object_scope;
3907       parser->qualifying_scope = saved_qualifying_scope;
3908
3909       return unqualified_id;
3910     }
3911   /* Otherwise, if we are in global scope, then we are looking at one
3912      of the other qualified-id productions.  */
3913   else if (global_scope_p)
3914     {
3915       cp_token *token;
3916       tree id;
3917
3918       /* Peek at the next token.  */
3919       token = cp_lexer_peek_token (parser->lexer);
3920
3921       /* If it's an identifier, and the next token is not a "<", then
3922          we can avoid the template-id case.  This is an optimization
3923          for this common case.  */
3924       if (token->type == CPP_NAME
3925           && !cp_parser_nth_token_starts_template_argument_list_p
3926                (parser, 2))
3927         return cp_parser_identifier (parser);
3928
3929       cp_parser_parse_tentatively (parser);
3930       /* Try a template-id.  */
3931       id = cp_parser_template_id (parser,
3932                                   /*template_keyword_p=*/false,
3933                                   /*check_dependency_p=*/true,
3934                                   declarator_p);
3935       /* If that worked, we're done.  */
3936       if (cp_parser_parse_definitely (parser))
3937         return id;
3938
3939       /* Peek at the next token.  (Changes in the token buffer may
3940          have invalidated the pointer obtained above.)  */
3941       token = cp_lexer_peek_token (parser->lexer);
3942
3943       switch (token->type)
3944         {
3945         case CPP_NAME:
3946           return cp_parser_identifier (parser);
3947
3948         case CPP_KEYWORD:
3949           if (token->keyword == RID_OPERATOR)
3950             return cp_parser_operator_function_id (parser);
3951           /* Fall through.  */
3952
3953         default:
3954           cp_parser_error (parser, "expected id-expression");
3955           return error_mark_node;
3956         }
3957     }
3958   else
3959     return cp_parser_unqualified_id (parser, template_keyword_p,
3960                                      /*check_dependency_p=*/true,
3961                                      declarator_p,
3962                                      optional_p);
3963 }
3964
3965 /* Parse an unqualified-id.
3966
3967    unqualified-id:
3968      identifier
3969      operator-function-id
3970      conversion-function-id
3971      ~ class-name
3972      template-id
3973
3974    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3975    keyword, in a construct like `A::template ...'.
3976
3977    Returns a representation of unqualified-id.  For the `identifier'
3978    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3979    production a BIT_NOT_EXPR is returned; the operand of the
3980    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3981    other productions, see the documentation accompanying the
3982    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3983    names are looked up in uninstantiated templates.  If DECLARATOR_P
3984    is true, the unqualified-id is appearing as part of a declarator,
3985    rather than as part of an expression.  */
3986
3987 static tree
3988 cp_parser_unqualified_id (cp_parser* parser,
3989                           bool template_keyword_p,
3990                           bool check_dependency_p,
3991                           bool declarator_p,
3992                           bool optional_p)
3993 {
3994   cp_token *token;
3995
3996   /* Peek at the next token.  */
3997   token = cp_lexer_peek_token (parser->lexer);
3998
3999   switch (token->type)
4000     {
4001     case CPP_NAME:
4002       {
4003         tree id;
4004
4005         /* We don't know yet whether or not this will be a
4006            template-id.  */
4007         cp_parser_parse_tentatively (parser);
4008         /* Try a template-id.  */
4009         id = cp_parser_template_id (parser, template_keyword_p,
4010                                     check_dependency_p,
4011                                     declarator_p);
4012         /* If it worked, we're done.  */
4013         if (cp_parser_parse_definitely (parser))
4014           return id;
4015         /* Otherwise, it's an ordinary identifier.  */
4016         return cp_parser_identifier (parser);
4017       }
4018
4019     case CPP_TEMPLATE_ID:
4020       return cp_parser_template_id (parser, template_keyword_p,
4021                                     check_dependency_p,
4022                                     declarator_p);
4023
4024     case CPP_COMPL:
4025       {
4026         tree type_decl;
4027         tree qualifying_scope;
4028         tree object_scope;
4029         tree scope;
4030         bool done;
4031
4032         /* Consume the `~' token.  */
4033         cp_lexer_consume_token (parser->lexer);
4034         /* Parse the class-name.  The standard, as written, seems to
4035            say that:
4036
4037              template <typename T> struct S { ~S (); };
4038              template <typename T> S<T>::~S() {}
4039
4040            is invalid, since `~' must be followed by a class-name, but
4041            `S<T>' is dependent, and so not known to be a class.
4042            That's not right; we need to look in uninstantiated
4043            templates.  A further complication arises from:
4044
4045              template <typename T> void f(T t) {
4046                t.T::~T();
4047              }
4048
4049            Here, it is not possible to look up `T' in the scope of `T'
4050            itself.  We must look in both the current scope, and the
4051            scope of the containing complete expression.
4052
4053            Yet another issue is:
4054
4055              struct S {
4056                int S;
4057                ~S();
4058              };
4059
4060              S::~S() {}
4061
4062            The standard does not seem to say that the `S' in `~S'
4063            should refer to the type `S' and not the data member
4064            `S::S'.  */
4065
4066         /* DR 244 says that we look up the name after the "~" in the
4067            same scope as we looked up the qualifying name.  That idea
4068            isn't fully worked out; it's more complicated than that.  */
4069         scope = parser->scope;
4070         object_scope = parser->object_scope;
4071         qualifying_scope = parser->qualifying_scope;
4072
4073         /* Check for invalid scopes.  */
4074         if (scope == error_mark_node)
4075           {
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         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4081           {
4082             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4083               error_at (token->location,
4084                         "scope %qT before %<~%> is not a class-name",
4085                         scope);
4086             cp_parser_simulate_error (parser);
4087             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4088               cp_lexer_consume_token (parser->lexer);
4089             return error_mark_node;
4090           }
4091         gcc_assert (!scope || TYPE_P (scope));
4092
4093         /* If the name is of the form "X::~X" it's OK even if X is a
4094            typedef.  */
4095         token = cp_lexer_peek_token (parser->lexer);
4096         if (scope
4097             && token->type == CPP_NAME
4098             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4099                 != CPP_LESS)
4100             && (token->u.value == TYPE_IDENTIFIER (scope)
4101                 || (CLASS_TYPE_P (scope)
4102                     && constructor_name_p (token->u.value, scope))))
4103           {
4104             cp_lexer_consume_token (parser->lexer);
4105             return build_nt (BIT_NOT_EXPR, scope);
4106           }
4107
4108         /* If there was an explicit qualification (S::~T), first look
4109            in the scope given by the qualification (i.e., S).
4110
4111            Note: in the calls to cp_parser_class_name below we pass
4112            typename_type so that lookup finds the injected-class-name
4113            rather than the constructor.  */
4114         done = false;
4115         type_decl = NULL_TREE;
4116         if (scope)
4117           {
4118             cp_parser_parse_tentatively (parser);
4119             type_decl = cp_parser_class_name (parser,
4120                                               /*typename_keyword_p=*/false,
4121                                               /*template_keyword_p=*/false,
4122                                               typename_type,
4123                                               /*check_dependency=*/false,
4124                                               /*class_head_p=*/false,
4125                                               declarator_p);
4126             if (cp_parser_parse_definitely (parser))
4127               done = true;
4128           }
4129         /* In "N::S::~S", look in "N" as well.  */
4130         if (!done && scope && qualifying_scope)
4131           {
4132             cp_parser_parse_tentatively (parser);
4133             parser->scope = qualifying_scope;
4134             parser->object_scope = NULL_TREE;
4135             parser->qualifying_scope = NULL_TREE;
4136             type_decl
4137               = cp_parser_class_name (parser,
4138                                       /*typename_keyword_p=*/false,
4139                                       /*template_keyword_p=*/false,
4140                                       typename_type,
4141                                       /*check_dependency=*/false,
4142                                       /*class_head_p=*/false,
4143                                       declarator_p);
4144             if (cp_parser_parse_definitely (parser))
4145               done = true;
4146           }
4147         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4148         else if (!done && object_scope)
4149           {
4150             cp_parser_parse_tentatively (parser);
4151             parser->scope = object_scope;
4152             parser->object_scope = NULL_TREE;
4153             parser->qualifying_scope = NULL_TREE;
4154             type_decl
4155               = cp_parser_class_name (parser,
4156                                       /*typename_keyword_p=*/false,
4157                                       /*template_keyword_p=*/false,
4158                                       typename_type,
4159                                       /*check_dependency=*/false,
4160                                       /*class_head_p=*/false,
4161                                       declarator_p);
4162             if (cp_parser_parse_definitely (parser))
4163               done = true;
4164           }
4165         /* Look in the surrounding context.  */
4166         if (!done)
4167           {
4168             parser->scope = NULL_TREE;
4169             parser->object_scope = NULL_TREE;
4170             parser->qualifying_scope = NULL_TREE;
4171             if (processing_template_decl)
4172               cp_parser_parse_tentatively (parser);
4173             type_decl
4174               = cp_parser_class_name (parser,
4175                                       /*typename_keyword_p=*/false,
4176                                       /*template_keyword_p=*/false,
4177                                       typename_type,
4178                                       /*check_dependency=*/false,
4179                                       /*class_head_p=*/false,
4180                                       declarator_p);
4181             if (processing_template_decl
4182                 && ! cp_parser_parse_definitely (parser))
4183               {
4184                 /* We couldn't find a type with this name, so just accept
4185                    it and check for a match at instantiation time.  */
4186                 type_decl = cp_parser_identifier (parser);
4187                 if (type_decl != error_mark_node)
4188                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4189                 return type_decl;
4190               }
4191           }
4192         /* If an error occurred, assume that the name of the
4193            destructor is the same as the name of the qualifying
4194            class.  That allows us to keep parsing after running
4195            into ill-formed destructor names.  */
4196         if (type_decl == error_mark_node && scope)
4197           return build_nt (BIT_NOT_EXPR, scope);
4198         else if (type_decl == error_mark_node)
4199           return error_mark_node;
4200
4201         /* Check that destructor name and scope match.  */
4202         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4203           {
4204             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4205               error_at (token->location,
4206                         "declaration of %<~%T%> as member of %qT",
4207                         type_decl, scope);
4208             cp_parser_simulate_error (parser);
4209             return error_mark_node;
4210           }
4211
4212         /* [class.dtor]
4213
4214            A typedef-name that names a class shall not be used as the
4215            identifier in the declarator for a destructor declaration.  */
4216         if (declarator_p
4217             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4218             && !DECL_SELF_REFERENCE_P (type_decl)
4219             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4220           error_at (token->location,
4221                     "typedef-name %qD used as destructor declarator",
4222                     type_decl);
4223
4224         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4225       }
4226
4227     case CPP_KEYWORD:
4228       if (token->keyword == RID_OPERATOR)
4229         {
4230           tree id;
4231
4232           /* This could be a template-id, so we try that first.  */
4233           cp_parser_parse_tentatively (parser);
4234           /* Try a template-id.  */
4235           id = cp_parser_template_id (parser, template_keyword_p,
4236                                       /*check_dependency_p=*/true,
4237                                       declarator_p);
4238           /* If that worked, we're done.  */
4239           if (cp_parser_parse_definitely (parser))
4240             return id;
4241           /* We still don't know whether we're looking at an
4242              operator-function-id or a conversion-function-id.  */
4243           cp_parser_parse_tentatively (parser);
4244           /* Try an operator-function-id.  */
4245           id = cp_parser_operator_function_id (parser);
4246           /* If that didn't work, try a conversion-function-id.  */
4247           if (!cp_parser_parse_definitely (parser))
4248             id = cp_parser_conversion_function_id (parser);
4249
4250           return id;
4251         }
4252       /* Fall through.  */
4253
4254     default:
4255       if (optional_p)
4256         return NULL_TREE;
4257       cp_parser_error (parser, "expected unqualified-id");
4258       return error_mark_node;
4259     }
4260 }
4261
4262 /* Parse an (optional) nested-name-specifier.
4263
4264    nested-name-specifier: [C++98]
4265      class-or-namespace-name :: nested-name-specifier [opt]
4266      class-or-namespace-name :: template nested-name-specifier [opt]
4267
4268    nested-name-specifier: [C++0x]
4269      type-name ::
4270      namespace-name ::
4271      nested-name-specifier identifier ::
4272      nested-name-specifier template [opt] simple-template-id ::
4273
4274    PARSER->SCOPE should be set appropriately before this function is
4275    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4276    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4277    in name lookups.
4278
4279    Sets PARSER->SCOPE to the class (TYPE) or namespace
4280    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4281    it unchanged if there is no nested-name-specifier.  Returns the new
4282    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4283
4284    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4285    part of a declaration and/or decl-specifier.  */
4286
4287 static tree
4288 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4289                                      bool typename_keyword_p,
4290                                      bool check_dependency_p,
4291                                      bool type_p,
4292                                      bool is_declaration)
4293 {
4294   bool success = false;
4295   cp_token_position start = 0;
4296   cp_token *token;
4297
4298   /* Remember where the nested-name-specifier starts.  */
4299   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4300     {
4301       start = cp_lexer_token_position (parser->lexer, false);
4302       push_deferring_access_checks (dk_deferred);
4303     }
4304
4305   while (true)
4306     {
4307       tree new_scope;
4308       tree old_scope;
4309       tree saved_qualifying_scope;
4310       bool template_keyword_p;
4311
4312       /* Spot cases that cannot be the beginning of a
4313          nested-name-specifier.  */
4314       token = cp_lexer_peek_token (parser->lexer);
4315
4316       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4317          the already parsed nested-name-specifier.  */
4318       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4319         {
4320           /* Grab the nested-name-specifier and continue the loop.  */
4321           cp_parser_pre_parsed_nested_name_specifier (parser);
4322           /* If we originally encountered this nested-name-specifier
4323              with IS_DECLARATION set to false, we will not have
4324              resolved TYPENAME_TYPEs, so we must do so here.  */
4325           if (is_declaration
4326               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4327             {
4328               new_scope = resolve_typename_type (parser->scope,
4329                                                  /*only_current_p=*/false);
4330               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4331                 parser->scope = new_scope;
4332             }
4333           success = true;
4334           continue;
4335         }
4336
4337       /* Spot cases that cannot be the beginning of a
4338          nested-name-specifier.  On the second and subsequent times
4339          through the loop, we look for the `template' keyword.  */
4340       if (success && token->keyword == RID_TEMPLATE)
4341         ;
4342       /* A template-id can start a nested-name-specifier.  */
4343       else if (token->type == CPP_TEMPLATE_ID)
4344         ;
4345       /* DR 743: decltype can be used in a nested-name-specifier.  */
4346       else if (token_is_decltype (token))
4347         ;
4348       else
4349         {
4350           /* If the next token is not an identifier, then it is
4351              definitely not a type-name or namespace-name.  */
4352           if (token->type != CPP_NAME)
4353             break;
4354           /* If the following token is neither a `<' (to begin a
4355              template-id), nor a `::', then we are not looking at a
4356              nested-name-specifier.  */
4357           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4358
4359           if (token->type == CPP_COLON
4360               && parser->colon_corrects_to_scope_p
4361               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4362             {
4363               error_at (token->location,
4364                         "found %<:%> in nested-name-specifier, expected %<::%>");
4365               token->type = CPP_SCOPE;
4366             }
4367
4368           if (token->type != CPP_SCOPE
4369               && !cp_parser_nth_token_starts_template_argument_list_p
4370                   (parser, 2))
4371             break;
4372         }
4373
4374       /* The nested-name-specifier is optional, so we parse
4375          tentatively.  */
4376       cp_parser_parse_tentatively (parser);
4377
4378       /* Look for the optional `template' keyword, if this isn't the
4379          first time through the loop.  */
4380       if (success)
4381         template_keyword_p = cp_parser_optional_template_keyword (parser);
4382       else
4383         template_keyword_p = false;
4384
4385       /* Save the old scope since the name lookup we are about to do
4386          might destroy it.  */
4387       old_scope = parser->scope;
4388       saved_qualifying_scope = parser->qualifying_scope;
4389       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4390          look up names in "X<T>::I" in order to determine that "Y" is
4391          a template.  So, if we have a typename at this point, we make
4392          an effort to look through it.  */
4393       if (is_declaration
4394           && !typename_keyword_p
4395           && parser->scope
4396           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4397         parser->scope = resolve_typename_type (parser->scope,
4398                                                /*only_current_p=*/false);
4399       /* Parse the qualifying entity.  */
4400       new_scope
4401         = cp_parser_qualifying_entity (parser,
4402                                        typename_keyword_p,
4403                                        template_keyword_p,
4404                                        check_dependency_p,
4405                                        type_p,
4406                                        is_declaration);
4407       /* Look for the `::' token.  */
4408       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4409
4410       /* If we found what we wanted, we keep going; otherwise, we're
4411          done.  */
4412       if (!cp_parser_parse_definitely (parser))
4413         {
4414           bool error_p = false;
4415
4416           /* Restore the OLD_SCOPE since it was valid before the
4417              failed attempt at finding the last
4418              class-or-namespace-name.  */
4419           parser->scope = old_scope;
4420           parser->qualifying_scope = saved_qualifying_scope;
4421
4422           /* If the next token is a decltype, and the one after that is a
4423              `::', then the decltype has failed to resolve to a class or
4424              enumeration type.  Give this error even when parsing
4425              tentatively since it can't possibly be valid--and we're going
4426              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4427              won't get another chance.*/
4428           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4429               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4430                   == CPP_SCOPE))
4431             {
4432               token = cp_lexer_consume_token (parser->lexer);
4433               error_at (token->location, "decltype evaluates to %qT, "
4434                         "which is not a class or enumeration type",
4435                         token->u.value);
4436               parser->scope = error_mark_node;
4437               error_p = true;
4438               /* As below.  */
4439               success = true;
4440               cp_lexer_consume_token (parser->lexer);
4441             }
4442
4443           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4444             break;
4445           /* If the next token is an identifier, and the one after
4446              that is a `::', then any valid interpretation would have
4447              found a class-or-namespace-name.  */
4448           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4449                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4450                      == CPP_SCOPE)
4451                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4452                      != CPP_COMPL))
4453             {
4454               token = cp_lexer_consume_token (parser->lexer);
4455               if (!error_p)
4456                 {
4457                   if (!token->ambiguous_p)
4458                     {
4459                       tree decl;
4460                       tree ambiguous_decls;
4461
4462                       decl = cp_parser_lookup_name (parser, token->u.value,
4463                                                     none_type,
4464                                                     /*is_template=*/false,
4465                                                     /*is_namespace=*/false,
4466                                                     /*check_dependency=*/true,
4467                                                     &ambiguous_decls,
4468                                                     token->location);
4469                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4470                         error_at (token->location,
4471                                   "%qD used without template parameters",
4472                                   decl);
4473                       else if (ambiguous_decls)
4474                         {
4475                           error_at (token->location,
4476                                     "reference to %qD is ambiguous",
4477                                     token->u.value);
4478                           print_candidates (ambiguous_decls);
4479                           decl = error_mark_node;
4480                         }
4481                       else
4482                         {
4483                           if (cxx_dialect != cxx98)
4484                             cp_parser_name_lookup_error
4485                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4486                              token->location);
4487                           else
4488                             cp_parser_name_lookup_error
4489                             (parser, token->u.value, decl, NLE_CXX98,
4490                              token->location);
4491                         }
4492                     }
4493                   parser->scope = error_mark_node;
4494                   error_p = true;
4495                   /* Treat this as a successful nested-name-specifier
4496                      due to:
4497
4498                      [basic.lookup.qual]
4499
4500                      If the name found is not a class-name (clause
4501                      _class_) or namespace-name (_namespace.def_), the
4502                      program is ill-formed.  */
4503                   success = true;
4504                 }
4505               cp_lexer_consume_token (parser->lexer);
4506             }
4507           break;
4508         }
4509       /* We've found one valid nested-name-specifier.  */
4510       success = true;
4511       /* Name lookup always gives us a DECL.  */
4512       if (TREE_CODE (new_scope) == TYPE_DECL)
4513         new_scope = TREE_TYPE (new_scope);
4514       /* Uses of "template" must be followed by actual templates.  */
4515       if (template_keyword_p
4516           && !(CLASS_TYPE_P (new_scope)
4517                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4518                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4519                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4520           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4521                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4522                    == TEMPLATE_ID_EXPR)))
4523         permerror (input_location, TYPE_P (new_scope)
4524                    ? "%qT is not a template"
4525                    : "%qD is not a template",
4526                    new_scope);
4527       /* If it is a class scope, try to complete it; we are about to
4528          be looking up names inside the class.  */
4529       if (TYPE_P (new_scope)
4530           /* Since checking types for dependency can be expensive,
4531              avoid doing it if the type is already complete.  */
4532           && !COMPLETE_TYPE_P (new_scope)
4533           /* Do not try to complete dependent types.  */
4534           && !dependent_type_p (new_scope))
4535         {
4536           new_scope = complete_type (new_scope);
4537           /* If it is a typedef to current class, use the current
4538              class instead, as the typedef won't have any names inside
4539              it yet.  */
4540           if (!COMPLETE_TYPE_P (new_scope)
4541               && currently_open_class (new_scope))
4542             new_scope = TYPE_MAIN_VARIANT (new_scope);
4543         }
4544       /* Make sure we look in the right scope the next time through
4545          the loop.  */
4546       parser->scope = new_scope;
4547     }
4548
4549   /* If parsing tentatively, replace the sequence of tokens that makes
4550      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4551      token.  That way, should we re-parse the token stream, we will
4552      not have to repeat the effort required to do the parse, nor will
4553      we issue duplicate error messages.  */
4554   if (success && start)
4555     {
4556       cp_token *token;
4557
4558       token = cp_lexer_token_at (parser->lexer, start);
4559       /* Reset the contents of the START token.  */
4560       token->type = CPP_NESTED_NAME_SPECIFIER;
4561       /* Retrieve any deferred checks.  Do not pop this access checks yet
4562          so the memory will not be reclaimed during token replacing below.  */
4563       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4564       token->u.tree_check_value->value = parser->scope;
4565       token->u.tree_check_value->checks = get_deferred_access_checks ();
4566       token->u.tree_check_value->qualifying_scope =
4567         parser->qualifying_scope;
4568       token->keyword = RID_MAX;
4569
4570       /* Purge all subsequent tokens.  */
4571       cp_lexer_purge_tokens_after (parser->lexer, start);
4572     }
4573
4574   if (start)
4575     pop_to_parent_deferring_access_checks ();
4576
4577   return success ? parser->scope : NULL_TREE;
4578 }
4579
4580 /* Parse a nested-name-specifier.  See
4581    cp_parser_nested_name_specifier_opt for details.  This function
4582    behaves identically, except that it will an issue an error if no
4583    nested-name-specifier is present.  */
4584
4585 static tree
4586 cp_parser_nested_name_specifier (cp_parser *parser,
4587                                  bool typename_keyword_p,
4588                                  bool check_dependency_p,
4589                                  bool type_p,
4590                                  bool is_declaration)
4591 {
4592   tree scope;
4593
4594   /* Look for the nested-name-specifier.  */
4595   scope = cp_parser_nested_name_specifier_opt (parser,
4596                                                typename_keyword_p,
4597                                                check_dependency_p,
4598                                                type_p,
4599                                                is_declaration);
4600   /* If it was not present, issue an error message.  */
4601   if (!scope)
4602     {
4603       cp_parser_error (parser, "expected nested-name-specifier");
4604       parser->scope = NULL_TREE;
4605     }
4606
4607   return scope;
4608 }
4609
4610 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4611    this is either a class-name or a namespace-name (which corresponds
4612    to the class-or-namespace-name production in the grammar). For
4613    C++0x, it can also be a type-name that refers to an enumeration
4614    type.
4615
4616    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4617    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4618    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4619    TYPE_P is TRUE iff the next name should be taken as a class-name,
4620    even the same name is declared to be another entity in the same
4621    scope.
4622
4623    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4624    specified by the class-or-namespace-name.  If neither is found the
4625    ERROR_MARK_NODE is returned.  */
4626
4627 static tree
4628 cp_parser_qualifying_entity (cp_parser *parser,
4629                              bool typename_keyword_p,
4630                              bool template_keyword_p,
4631                              bool check_dependency_p,
4632                              bool type_p,
4633                              bool is_declaration)
4634 {
4635   tree saved_scope;
4636   tree saved_qualifying_scope;
4637   tree saved_object_scope;
4638   tree scope;
4639   bool only_class_p;
4640   bool successful_parse_p;
4641
4642   /* DR 743: decltype can appear in a nested-name-specifier.  */
4643   if (cp_lexer_next_token_is_decltype (parser->lexer))
4644     {
4645       scope = cp_parser_decltype (parser);
4646       if (TREE_CODE (scope) != ENUMERAL_TYPE
4647           && !MAYBE_CLASS_TYPE_P (scope))
4648         {
4649           cp_parser_simulate_error (parser);
4650           return error_mark_node;
4651         }
4652       if (TYPE_NAME (scope))
4653         scope = TYPE_NAME (scope);
4654       return scope;
4655     }
4656
4657   /* Before we try to parse the class-name, we must save away the
4658      current PARSER->SCOPE since cp_parser_class_name will destroy
4659      it.  */
4660   saved_scope = parser->scope;
4661   saved_qualifying_scope = parser->qualifying_scope;
4662   saved_object_scope = parser->object_scope;
4663   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4664      there is no need to look for a namespace-name.  */
4665   only_class_p = template_keyword_p 
4666     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4667   if (!only_class_p)
4668     cp_parser_parse_tentatively (parser);
4669   scope = cp_parser_class_name (parser,
4670                                 typename_keyword_p,
4671                                 template_keyword_p,
4672                                 type_p ? class_type : none_type,
4673                                 check_dependency_p,
4674                                 /*class_head_p=*/false,
4675                                 is_declaration);
4676   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4677   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4678   if (!only_class_p 
4679       && cxx_dialect != cxx98
4680       && !successful_parse_p)
4681     {
4682       /* Restore the saved scope.  */
4683       parser->scope = saved_scope;
4684       parser->qualifying_scope = saved_qualifying_scope;
4685       parser->object_scope = saved_object_scope;
4686
4687       /* Parse tentatively.  */
4688       cp_parser_parse_tentatively (parser);
4689      
4690       /* Parse a typedef-name or enum-name.  */
4691       scope = cp_parser_nonclass_name (parser);
4692
4693       /* "If the name found does not designate a namespace or a class,
4694          enumeration, or dependent type, the program is ill-formed."
4695
4696          We cover classes and dependent types above and namespaces below,
4697          so this code is only looking for enums.  */
4698       if (!scope || TREE_CODE (scope) != TYPE_DECL
4699           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4700         cp_parser_simulate_error (parser);
4701
4702       successful_parse_p = cp_parser_parse_definitely (parser);
4703     }
4704   /* If that didn't work, try for a namespace-name.  */
4705   if (!only_class_p && !successful_parse_p)
4706     {
4707       /* Restore the saved scope.  */
4708       parser->scope = saved_scope;
4709       parser->qualifying_scope = saved_qualifying_scope;
4710       parser->object_scope = saved_object_scope;
4711       /* If we are not looking at an identifier followed by the scope
4712          resolution operator, then this is not part of a
4713          nested-name-specifier.  (Note that this function is only used
4714          to parse the components of a nested-name-specifier.)  */
4715       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4716           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4717         return error_mark_node;
4718       scope = cp_parser_namespace_name (parser);
4719     }
4720
4721   return scope;
4722 }
4723
4724 /* Parse a postfix-expression.
4725
4726    postfix-expression:
4727      primary-expression
4728      postfix-expression [ expression ]
4729      postfix-expression ( expression-list [opt] )
4730      simple-type-specifier ( expression-list [opt] )
4731      typename :: [opt] nested-name-specifier identifier
4732        ( expression-list [opt] )
4733      typename :: [opt] nested-name-specifier template [opt] template-id
4734        ( expression-list [opt] )
4735      postfix-expression . template [opt] id-expression
4736      postfix-expression -> template [opt] id-expression
4737      postfix-expression . pseudo-destructor-name
4738      postfix-expression -> pseudo-destructor-name
4739      postfix-expression ++
4740      postfix-expression --
4741      dynamic_cast < type-id > ( expression )
4742      static_cast < type-id > ( expression )
4743      reinterpret_cast < type-id > ( expression )
4744      const_cast < type-id > ( expression )
4745      typeid ( expression )
4746      typeid ( type-id )
4747
4748    GNU Extension:
4749
4750    postfix-expression:
4751      ( type-id ) { initializer-list , [opt] }
4752
4753    This extension is a GNU version of the C99 compound-literal
4754    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4755    but they are essentially the same concept.)
4756
4757    If ADDRESS_P is true, the postfix expression is the operand of the
4758    `&' operator.  CAST_P is true if this expression is the target of a
4759    cast.
4760
4761    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4762    class member access expressions [expr.ref].
4763
4764    Returns a representation of the expression.  */
4765
4766 static tree
4767 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4768                               bool member_access_only_p,
4769                               cp_id_kind * pidk_return)
4770 {
4771   cp_token *token;
4772   enum rid keyword;
4773   cp_id_kind idk = CP_ID_KIND_NONE;
4774   tree postfix_expression = NULL_TREE;
4775   bool is_member_access = false;
4776
4777   /* Peek at the next token.  */
4778   token = cp_lexer_peek_token (parser->lexer);
4779   /* Some of the productions are determined by keywords.  */
4780   keyword = token->keyword;
4781   switch (keyword)
4782     {
4783     case RID_DYNCAST:
4784     case RID_STATCAST:
4785     case RID_REINTCAST:
4786     case RID_CONSTCAST:
4787       {
4788         tree type;
4789         tree expression;
4790         const char *saved_message;
4791
4792         /* All of these can be handled in the same way from the point
4793            of view of parsing.  Begin by consuming the token
4794            identifying the cast.  */
4795         cp_lexer_consume_token (parser->lexer);
4796
4797         /* New types cannot be defined in the cast.  */
4798         saved_message = parser->type_definition_forbidden_message;
4799         parser->type_definition_forbidden_message
4800           = G_("types may not be defined in casts");
4801
4802         /* Look for the opening `<'.  */
4803         cp_parser_require (parser, CPP_LESS, RT_LESS);
4804         /* Parse the type to which we are casting.  */
4805         type = cp_parser_type_id (parser);
4806         /* Look for the closing `>'.  */
4807         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4808         /* Restore the old message.  */
4809         parser->type_definition_forbidden_message = saved_message;
4810
4811         /* And the expression which is being cast.  */
4812         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4813         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4814         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4815
4816         /* Only type conversions to integral or enumeration types
4817            can be used in constant-expressions.  */
4818         if (!cast_valid_in_integral_constant_expression_p (type)
4819             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4820           return error_mark_node;
4821
4822         switch (keyword)
4823           {
4824           case RID_DYNCAST:
4825             postfix_expression
4826               = build_dynamic_cast (type, expression, tf_warning_or_error);
4827             break;
4828           case RID_STATCAST:
4829             postfix_expression
4830               = build_static_cast (type, expression, tf_warning_or_error);
4831             break;
4832           case RID_REINTCAST:
4833             postfix_expression
4834               = build_reinterpret_cast (type, expression, 
4835                                         tf_warning_or_error);
4836             break;
4837           case RID_CONSTCAST:
4838             postfix_expression
4839               = build_const_cast (type, expression, tf_warning_or_error);
4840             break;
4841           default:
4842             gcc_unreachable ();
4843           }
4844       }
4845       break;
4846
4847     case RID_TYPEID:
4848       {
4849         tree type;
4850         const char *saved_message;
4851         bool saved_in_type_id_in_expr_p;
4852
4853         /* Consume the `typeid' token.  */
4854         cp_lexer_consume_token (parser->lexer);
4855         /* Look for the `(' token.  */
4856         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4857         /* Types cannot be defined in a `typeid' expression.  */
4858         saved_message = parser->type_definition_forbidden_message;
4859         parser->type_definition_forbidden_message
4860           = G_("types may not be defined in a %<typeid%> expression");
4861         /* We can't be sure yet whether we're looking at a type-id or an
4862            expression.  */
4863         cp_parser_parse_tentatively (parser);
4864         /* Try a type-id first.  */
4865         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4866         parser->in_type_id_in_expr_p = true;
4867         type = cp_parser_type_id (parser);
4868         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4869         /* Look for the `)' token.  Otherwise, we can't be sure that
4870            we're not looking at an expression: consider `typeid (int
4871            (3))', for example.  */
4872         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4873         /* If all went well, simply lookup the type-id.  */
4874         if (cp_parser_parse_definitely (parser))
4875           postfix_expression = get_typeid (type);
4876         /* Otherwise, fall back to the expression variant.  */
4877         else
4878           {
4879             tree expression;
4880
4881             /* Look for an expression.  */
4882             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4883             /* Compute its typeid.  */
4884             postfix_expression = build_typeid (expression);
4885             /* Look for the `)' token.  */
4886             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4887           }
4888         /* Restore the saved message.  */
4889         parser->type_definition_forbidden_message = saved_message;
4890         /* `typeid' may not appear in an integral constant expression.  */
4891         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4892           return error_mark_node;
4893       }
4894       break;
4895
4896     case RID_TYPENAME:
4897       {
4898         tree type;
4899         /* The syntax permitted here is the same permitted for an
4900            elaborated-type-specifier.  */
4901         type = cp_parser_elaborated_type_specifier (parser,
4902                                                     /*is_friend=*/false,
4903                                                     /*is_declaration=*/false);
4904         postfix_expression = cp_parser_functional_cast (parser, type);
4905       }
4906       break;
4907
4908     default:
4909       {
4910         tree type;
4911
4912         /* If the next thing is a simple-type-specifier, we may be
4913            looking at a functional cast.  We could also be looking at
4914            an id-expression.  So, we try the functional cast, and if
4915            that doesn't work we fall back to the primary-expression.  */
4916         cp_parser_parse_tentatively (parser);
4917         /* Look for the simple-type-specifier.  */
4918         type = cp_parser_simple_type_specifier (parser,
4919                                                 /*decl_specs=*/NULL,
4920                                                 CP_PARSER_FLAGS_NONE);
4921         /* Parse the cast itself.  */
4922         if (!cp_parser_error_occurred (parser))
4923           postfix_expression
4924             = cp_parser_functional_cast (parser, type);
4925         /* If that worked, we're done.  */
4926         if (cp_parser_parse_definitely (parser))
4927           break;
4928
4929         /* If the functional-cast didn't work out, try a
4930            compound-literal.  */
4931         if (cp_parser_allow_gnu_extensions_p (parser)
4932             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4933           {
4934             VEC(constructor_elt,gc) *initializer_list = NULL;
4935             bool saved_in_type_id_in_expr_p;
4936
4937             cp_parser_parse_tentatively (parser);
4938             /* Consume the `('.  */
4939             cp_lexer_consume_token (parser->lexer);
4940             /* Parse the type.  */
4941             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4942             parser->in_type_id_in_expr_p = true;
4943             type = cp_parser_type_id (parser);
4944             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4945             /* Look for the `)'.  */
4946             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4947             /* Look for the `{'.  */
4948             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4949             /* If things aren't going well, there's no need to
4950                keep going.  */
4951             if (!cp_parser_error_occurred (parser))
4952               {
4953                 bool non_constant_p;
4954                 /* Parse the initializer-list.  */
4955                 initializer_list
4956                   = cp_parser_initializer_list (parser, &non_constant_p);
4957                 /* Allow a trailing `,'.  */
4958                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4959                   cp_lexer_consume_token (parser->lexer);
4960                 /* Look for the final `}'.  */
4961                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4962               }
4963             /* If that worked, we're definitely looking at a
4964                compound-literal expression.  */
4965             if (cp_parser_parse_definitely (parser))
4966               {
4967                 /* Warn the user that a compound literal is not
4968                    allowed in standard C++.  */
4969                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4970                 /* For simplicity, we disallow compound literals in
4971                    constant-expressions.  We could
4972                    allow compound literals of integer type, whose
4973                    initializer was a constant, in constant
4974                    expressions.  Permitting that usage, as a further
4975                    extension, would not change the meaning of any
4976                    currently accepted programs.  (Of course, as
4977                    compound literals are not part of ISO C++, the
4978                    standard has nothing to say.)  */
4979                 if (cp_parser_non_integral_constant_expression (parser,
4980                                                                 NIC_NCC))
4981                   {
4982                     postfix_expression = error_mark_node;
4983                     break;
4984                   }
4985                 /* Form the representation of the compound-literal.  */
4986                 postfix_expression
4987                   = (finish_compound_literal
4988                      (type, build_constructor (init_list_type_node,
4989                                                initializer_list),
4990                       tf_warning_or_error));
4991                 break;
4992               }
4993           }
4994
4995         /* It must be a primary-expression.  */
4996         postfix_expression
4997           = cp_parser_primary_expression (parser, address_p, cast_p,
4998                                           /*template_arg_p=*/false,
4999                                           &idk);
5000       }
5001       break;
5002     }
5003
5004   /* Keep looping until the postfix-expression is complete.  */
5005   while (true)
5006     {
5007       if (idk == CP_ID_KIND_UNQUALIFIED
5008           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5009           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5010         /* It is not a Koenig lookup function call.  */
5011         postfix_expression
5012           = unqualified_name_lookup_error (postfix_expression);
5013
5014       /* Peek at the next token.  */
5015       token = cp_lexer_peek_token (parser->lexer);
5016
5017       switch (token->type)
5018         {
5019         case CPP_OPEN_SQUARE:
5020           postfix_expression
5021             = cp_parser_postfix_open_square_expression (parser,
5022                                                         postfix_expression,
5023                                                         false);
5024           idk = CP_ID_KIND_NONE;
5025           is_member_access = false;
5026           break;
5027
5028         case CPP_OPEN_PAREN:
5029           /* postfix-expression ( expression-list [opt] ) */
5030           {
5031             bool koenig_p;
5032             bool is_builtin_constant_p;
5033             bool saved_integral_constant_expression_p = false;
5034             bool saved_non_integral_constant_expression_p = false;
5035             VEC(tree,gc) *args;
5036
5037             is_member_access = false;
5038
5039             is_builtin_constant_p
5040               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5041             if (is_builtin_constant_p)
5042               {
5043                 /* The whole point of __builtin_constant_p is to allow
5044                    non-constant expressions to appear as arguments.  */
5045                 saved_integral_constant_expression_p
5046                   = parser->integral_constant_expression_p;
5047                 saved_non_integral_constant_expression_p
5048                   = parser->non_integral_constant_expression_p;
5049                 parser->integral_constant_expression_p = false;
5050               }
5051             args = (cp_parser_parenthesized_expression_list
5052                     (parser, non_attr,
5053                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5054                      /*non_constant_p=*/NULL));
5055             if (is_builtin_constant_p)
5056               {
5057                 parser->integral_constant_expression_p
5058                   = saved_integral_constant_expression_p;
5059                 parser->non_integral_constant_expression_p
5060                   = saved_non_integral_constant_expression_p;
5061               }
5062
5063             if (args == NULL)
5064               {
5065                 postfix_expression = error_mark_node;
5066                 break;
5067               }
5068
5069             /* Function calls are not permitted in
5070                constant-expressions.  */
5071             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5072                 && cp_parser_non_integral_constant_expression (parser,
5073                                                                NIC_FUNC_CALL))
5074               {
5075                 postfix_expression = error_mark_node;
5076                 release_tree_vector (args);
5077                 break;
5078               }
5079
5080             koenig_p = false;
5081             if (idk == CP_ID_KIND_UNQUALIFIED
5082                 || idk == CP_ID_KIND_TEMPLATE_ID)
5083               {
5084                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5085                   {
5086                     if (!VEC_empty (tree, args))
5087                       {
5088                         koenig_p = true;
5089                         if (!any_type_dependent_arguments_p (args))
5090                           postfix_expression
5091                             = perform_koenig_lookup (postfix_expression, args,
5092                                                      /*include_std=*/false,
5093                                                      tf_warning_or_error);
5094                       }
5095                     else
5096                       postfix_expression
5097                         = unqualified_fn_lookup_error (postfix_expression);
5098                   }
5099                 /* We do not perform argument-dependent lookup if
5100                    normal lookup finds a non-function, in accordance
5101                    with the expected resolution of DR 218.  */
5102                 else if (!VEC_empty (tree, args)
5103                          && is_overloaded_fn (postfix_expression))
5104                   {
5105                     tree fn = get_first_fn (postfix_expression);
5106                     fn = STRIP_TEMPLATE (fn);
5107
5108                     /* Do not do argument dependent lookup if regular
5109                        lookup finds a member function or a block-scope
5110                        function declaration.  [basic.lookup.argdep]/3  */
5111                     if (!DECL_FUNCTION_MEMBER_P (fn)
5112                         && !DECL_LOCAL_FUNCTION_P (fn))
5113                       {
5114                         koenig_p = true;
5115                         if (!any_type_dependent_arguments_p (args))
5116                           postfix_expression
5117                             = perform_koenig_lookup (postfix_expression, args,
5118                                                      /*include_std=*/false,
5119                                                      tf_warning_or_error);
5120                       }
5121                   }
5122               }
5123
5124             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5125               {
5126                 tree instance = TREE_OPERAND (postfix_expression, 0);
5127                 tree fn = TREE_OPERAND (postfix_expression, 1);
5128
5129                 if (processing_template_decl
5130                     && (type_dependent_expression_p (instance)
5131                         || (!BASELINK_P (fn)
5132                             && TREE_CODE (fn) != FIELD_DECL)
5133                         || type_dependent_expression_p (fn)
5134                         || any_type_dependent_arguments_p (args)))
5135                   {
5136                     postfix_expression
5137                       = build_nt_call_vec (postfix_expression, args);
5138                     release_tree_vector (args);
5139                     break;
5140                   }
5141
5142                 if (BASELINK_P (fn))
5143                   {
5144                   postfix_expression
5145                     = (build_new_method_call
5146                        (instance, fn, &args, NULL_TREE,
5147                         (idk == CP_ID_KIND_QUALIFIED
5148                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5149                          : LOOKUP_NORMAL),
5150                         /*fn_p=*/NULL,
5151                         tf_warning_or_error));
5152                   }
5153                 else
5154                   postfix_expression
5155                     = finish_call_expr (postfix_expression, &args,
5156                                         /*disallow_virtual=*/false,
5157                                         /*koenig_p=*/false,
5158                                         tf_warning_or_error);
5159               }
5160             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5161                      || TREE_CODE (postfix_expression) == MEMBER_REF
5162                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5163               postfix_expression = (build_offset_ref_call_from_tree
5164                                     (postfix_expression, &args));
5165             else if (idk == CP_ID_KIND_QUALIFIED)
5166               /* A call to a static class member, or a namespace-scope
5167                  function.  */
5168               postfix_expression
5169                 = finish_call_expr (postfix_expression, &args,
5170                                     /*disallow_virtual=*/true,
5171                                     koenig_p,
5172                                     tf_warning_or_error);
5173             else
5174               /* All other function calls.  */
5175               postfix_expression
5176                 = finish_call_expr (postfix_expression, &args,
5177                                     /*disallow_virtual=*/false,
5178                                     koenig_p,
5179                                     tf_warning_or_error);
5180
5181             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5182             idk = CP_ID_KIND_NONE;
5183
5184             release_tree_vector (args);
5185           }
5186           break;
5187
5188         case CPP_DOT:
5189         case CPP_DEREF:
5190           /* postfix-expression . template [opt] id-expression
5191              postfix-expression . pseudo-destructor-name
5192              postfix-expression -> template [opt] id-expression
5193              postfix-expression -> pseudo-destructor-name */
5194
5195           /* Consume the `.' or `->' operator.  */
5196           cp_lexer_consume_token (parser->lexer);
5197
5198           postfix_expression
5199             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5200                                                       postfix_expression,
5201                                                       false, &idk,
5202                                                       token->location);
5203
5204           is_member_access = true;
5205           break;
5206
5207         case CPP_PLUS_PLUS:
5208           /* postfix-expression ++  */
5209           /* Consume the `++' token.  */
5210           cp_lexer_consume_token (parser->lexer);
5211           /* Generate a representation for the complete expression.  */
5212           postfix_expression
5213             = finish_increment_expr (postfix_expression,
5214                                      POSTINCREMENT_EXPR);
5215           /* Increments may not appear in constant-expressions.  */
5216           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5217             postfix_expression = error_mark_node;
5218           idk = CP_ID_KIND_NONE;
5219           is_member_access = false;
5220           break;
5221
5222         case CPP_MINUS_MINUS:
5223           /* postfix-expression -- */
5224           /* Consume the `--' token.  */
5225           cp_lexer_consume_token (parser->lexer);
5226           /* Generate a representation for the complete expression.  */
5227           postfix_expression
5228             = finish_increment_expr (postfix_expression,
5229                                      POSTDECREMENT_EXPR);
5230           /* Decrements may not appear in constant-expressions.  */
5231           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5232             postfix_expression = error_mark_node;
5233           idk = CP_ID_KIND_NONE;
5234           is_member_access = false;
5235           break;
5236
5237         default:
5238           if (pidk_return != NULL)
5239             * pidk_return = idk;
5240           if (member_access_only_p)
5241             return is_member_access? postfix_expression : error_mark_node;
5242           else
5243             return postfix_expression;
5244         }
5245     }
5246
5247   /* We should never get here.  */
5248   gcc_unreachable ();
5249   return error_mark_node;
5250 }
5251
5252 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5253    by cp_parser_builtin_offsetof.  We're looking for
5254
5255      postfix-expression [ expression ]
5256
5257    FOR_OFFSETOF is set if we're being called in that context, which
5258    changes how we deal with integer constant expressions.  */
5259
5260 static tree
5261 cp_parser_postfix_open_square_expression (cp_parser *parser,
5262                                           tree postfix_expression,
5263                                           bool for_offsetof)
5264 {
5265   tree index;
5266
5267   /* Consume the `[' token.  */
5268   cp_lexer_consume_token (parser->lexer);
5269
5270   /* Parse the index expression.  */
5271   /* ??? For offsetof, there is a question of what to allow here.  If
5272      offsetof is not being used in an integral constant expression context,
5273      then we *could* get the right answer by computing the value at runtime.
5274      If we are in an integral constant expression context, then we might
5275      could accept any constant expression; hard to say without analysis.
5276      Rather than open the barn door too wide right away, allow only integer
5277      constant expressions here.  */
5278   if (for_offsetof)
5279     index = cp_parser_constant_expression (parser, false, NULL);
5280   else
5281     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5282
5283   /* Look for the closing `]'.  */
5284   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5285
5286   /* Build the ARRAY_REF.  */
5287   postfix_expression = grok_array_decl (postfix_expression, index);
5288
5289   /* When not doing offsetof, array references are not permitted in
5290      constant-expressions.  */
5291   if (!for_offsetof
5292       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5293     postfix_expression = error_mark_node;
5294
5295   return postfix_expression;
5296 }
5297
5298 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5299    by cp_parser_builtin_offsetof.  We're looking for
5300
5301      postfix-expression . template [opt] id-expression
5302      postfix-expression . pseudo-destructor-name
5303      postfix-expression -> template [opt] id-expression
5304      postfix-expression -> pseudo-destructor-name
5305
5306    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5307    limits what of the above we'll actually accept, but nevermind.
5308    TOKEN_TYPE is the "." or "->" token, which will already have been
5309    removed from the stream.  */
5310
5311 static tree
5312 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5313                                         enum cpp_ttype token_type,
5314                                         tree postfix_expression,
5315                                         bool for_offsetof, cp_id_kind *idk,
5316                                         location_t location)
5317 {
5318   tree name;
5319   bool dependent_p;
5320   bool pseudo_destructor_p;
5321   tree scope = NULL_TREE;
5322
5323   /* If this is a `->' operator, dereference the pointer.  */
5324   if (token_type == CPP_DEREF)
5325     postfix_expression = build_x_arrow (postfix_expression);
5326   /* Check to see whether or not the expression is type-dependent.  */
5327   dependent_p = type_dependent_expression_p (postfix_expression);
5328   /* The identifier following the `->' or `.' is not qualified.  */
5329   parser->scope = NULL_TREE;
5330   parser->qualifying_scope = NULL_TREE;
5331   parser->object_scope = NULL_TREE;
5332   *idk = CP_ID_KIND_NONE;
5333
5334   /* Enter the scope corresponding to the type of the object
5335      given by the POSTFIX_EXPRESSION.  */
5336   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5337     {
5338       scope = TREE_TYPE (postfix_expression);
5339       /* According to the standard, no expression should ever have
5340          reference type.  Unfortunately, we do not currently match
5341          the standard in this respect in that our internal representation
5342          of an expression may have reference type even when the standard
5343          says it does not.  Therefore, we have to manually obtain the
5344          underlying type here.  */
5345       scope = non_reference (scope);
5346       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5347       if (scope == unknown_type_node)
5348         {
5349           error_at (location, "%qE does not have class type",
5350                     postfix_expression);
5351           scope = NULL_TREE;
5352         }
5353       /* Unlike the object expression in other contexts, *this is not
5354          required to be of complete type for purposes of class member
5355          access (5.2.5) outside the member function body.  */
5356       else if (scope != current_class_ref
5357                && !(processing_template_decl && scope == current_class_type))
5358         scope = complete_type_or_else (scope, NULL_TREE);
5359       /* Let the name lookup machinery know that we are processing a
5360          class member access expression.  */
5361       parser->context->object_type = scope;
5362       /* If something went wrong, we want to be able to discern that case,
5363          as opposed to the case where there was no SCOPE due to the type
5364          of expression being dependent.  */
5365       if (!scope)
5366         scope = error_mark_node;
5367       /* If the SCOPE was erroneous, make the various semantic analysis
5368          functions exit quickly -- and without issuing additional error
5369          messages.  */
5370       if (scope == error_mark_node)
5371         postfix_expression = error_mark_node;
5372     }
5373
5374   /* Assume this expression is not a pseudo-destructor access.  */
5375   pseudo_destructor_p = false;
5376
5377   /* If the SCOPE is a scalar type, then, if this is a valid program,
5378      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5379      is type dependent, it can be pseudo-destructor-name or something else.
5380      Try to parse it as pseudo-destructor-name first.  */
5381   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5382     {
5383       tree s;
5384       tree type;
5385
5386       cp_parser_parse_tentatively (parser);
5387       /* Parse the pseudo-destructor-name.  */
5388       s = NULL_TREE;
5389       cp_parser_pseudo_destructor_name (parser, &s, &type);
5390       if (dependent_p
5391           && (cp_parser_error_occurred (parser)
5392               || TREE_CODE (type) != TYPE_DECL
5393               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5394         cp_parser_abort_tentative_parse (parser);
5395       else if (cp_parser_parse_definitely (parser))
5396         {
5397           pseudo_destructor_p = true;
5398           postfix_expression
5399             = finish_pseudo_destructor_expr (postfix_expression,
5400                                              s, TREE_TYPE (type));
5401         }
5402     }
5403
5404   if (!pseudo_destructor_p)
5405     {
5406       /* If the SCOPE is not a scalar type, we are looking at an
5407          ordinary class member access expression, rather than a
5408          pseudo-destructor-name.  */
5409       bool template_p;
5410       cp_token *token = cp_lexer_peek_token (parser->lexer);
5411       /* Parse the id-expression.  */
5412       name = (cp_parser_id_expression
5413               (parser,
5414                cp_parser_optional_template_keyword (parser),
5415                /*check_dependency_p=*/true,
5416                &template_p,
5417                /*declarator_p=*/false,
5418                /*optional_p=*/false));
5419       /* In general, build a SCOPE_REF if the member name is qualified.
5420          However, if the name was not dependent and has already been
5421          resolved; there is no need to build the SCOPE_REF.  For example;
5422
5423              struct X { void f(); };
5424              template <typename T> void f(T* t) { t->X::f(); }
5425
5426          Even though "t" is dependent, "X::f" is not and has been resolved
5427          to a BASELINK; there is no need to include scope information.  */
5428
5429       /* But we do need to remember that there was an explicit scope for
5430          virtual function calls.  */
5431       if (parser->scope)
5432         *idk = CP_ID_KIND_QUALIFIED;
5433
5434       /* If the name is a template-id that names a type, we will get a
5435          TYPE_DECL here.  That is invalid code.  */
5436       if (TREE_CODE (name) == TYPE_DECL)
5437         {
5438           error_at (token->location, "invalid use of %qD", name);
5439           postfix_expression = error_mark_node;
5440         }
5441       else
5442         {
5443           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5444             {
5445               name = build_qualified_name (/*type=*/NULL_TREE,
5446                                            parser->scope,
5447                                            name,
5448                                            template_p);
5449               parser->scope = NULL_TREE;
5450               parser->qualifying_scope = NULL_TREE;
5451               parser->object_scope = NULL_TREE;
5452             }
5453           if (scope && name && BASELINK_P (name))
5454             adjust_result_of_qualified_name_lookup
5455               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5456           postfix_expression
5457             = finish_class_member_access_expr (postfix_expression, name,
5458                                                template_p, 
5459                                                tf_warning_or_error);
5460         }
5461     }
5462
5463   /* We no longer need to look up names in the scope of the object on
5464      the left-hand side of the `.' or `->' operator.  */
5465   parser->context->object_type = NULL_TREE;
5466
5467   /* Outside of offsetof, these operators may not appear in
5468      constant-expressions.  */
5469   if (!for_offsetof
5470       && (cp_parser_non_integral_constant_expression
5471           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5472     postfix_expression = error_mark_node;
5473
5474   return postfix_expression;
5475 }
5476
5477 /* Parse a parenthesized expression-list.
5478
5479    expression-list:
5480      assignment-expression
5481      expression-list, assignment-expression
5482
5483    attribute-list:
5484      expression-list
5485      identifier
5486      identifier, expression-list
5487
5488    CAST_P is true if this expression is the target of a cast.
5489
5490    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5491    argument pack.
5492
5493    Returns a vector of trees.  Each element is a representation of an
5494    assignment-expression.  NULL is returned if the ( and or ) are
5495    missing.  An empty, but allocated, vector is returned on no
5496    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5497    if we are parsing an attribute list for an attribute that wants a
5498    plain identifier argument, normal_attr for an attribute that wants
5499    an expression, or non_attr if we aren't parsing an attribute list.  If
5500    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5501    not all of the expressions in the list were constant.  */
5502
5503 static VEC(tree,gc) *
5504 cp_parser_parenthesized_expression_list (cp_parser* parser,
5505                                          int is_attribute_list,
5506                                          bool cast_p,
5507                                          bool allow_expansion_p,
5508                                          bool *non_constant_p)
5509 {
5510   VEC(tree,gc) *expression_list;
5511   bool fold_expr_p = is_attribute_list != non_attr;
5512   tree identifier = NULL_TREE;
5513   bool saved_greater_than_is_operator_p;
5514
5515   /* Assume all the expressions will be constant.  */
5516   if (non_constant_p)
5517     *non_constant_p = false;
5518
5519   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5520     return NULL;
5521
5522   expression_list = make_tree_vector ();
5523
5524   /* Within a parenthesized expression, a `>' token is always
5525      the greater-than operator.  */
5526   saved_greater_than_is_operator_p
5527     = parser->greater_than_is_operator_p;
5528   parser->greater_than_is_operator_p = true;
5529
5530   /* Consume expressions until there are no more.  */
5531   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5532     while (true)
5533       {
5534         tree expr;
5535
5536         /* At the beginning of attribute lists, check to see if the
5537            next token is an identifier.  */
5538         if (is_attribute_list == id_attr
5539             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5540           {
5541             cp_token *token;
5542
5543             /* Consume the identifier.  */
5544             token = cp_lexer_consume_token (parser->lexer);
5545             /* Save the identifier.  */
5546             identifier = token->u.value;
5547           }
5548         else
5549           {
5550             bool expr_non_constant_p;
5551
5552             /* Parse the next assignment-expression.  */
5553             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5554               {
5555                 /* A braced-init-list.  */
5556                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5557                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5558                 if (non_constant_p && expr_non_constant_p)
5559                   *non_constant_p = true;
5560               }
5561             else if (non_constant_p)
5562               {
5563                 expr = (cp_parser_constant_expression
5564                         (parser, /*allow_non_constant_p=*/true,
5565                          &expr_non_constant_p));
5566                 if (expr_non_constant_p)
5567                   *non_constant_p = true;
5568               }
5569             else
5570               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5571
5572             if (fold_expr_p)
5573               expr = fold_non_dependent_expr (expr);
5574
5575             /* If we have an ellipsis, then this is an expression
5576                expansion.  */
5577             if (allow_expansion_p
5578                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5579               {
5580                 /* Consume the `...'.  */
5581                 cp_lexer_consume_token (parser->lexer);
5582
5583                 /* Build the argument pack.  */
5584                 expr = make_pack_expansion (expr);
5585               }
5586
5587              /* Add it to the list.  We add error_mark_node
5588                 expressions to the list, so that we can still tell if
5589                 the correct form for a parenthesized expression-list
5590                 is found. That gives better errors.  */
5591             VEC_safe_push (tree, gc, expression_list, expr);
5592
5593             if (expr == error_mark_node)
5594               goto skip_comma;
5595           }
5596
5597         /* After the first item, attribute lists look the same as
5598            expression lists.  */
5599         is_attribute_list = non_attr;
5600
5601       get_comma:;
5602         /* If the next token isn't a `,', then we are done.  */
5603         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5604           break;
5605
5606         /* Otherwise, consume the `,' and keep going.  */
5607         cp_lexer_consume_token (parser->lexer);
5608       }
5609
5610   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5611     {
5612       int ending;
5613
5614     skip_comma:;
5615       /* We try and resync to an unnested comma, as that will give the
5616          user better diagnostics.  */
5617       ending = cp_parser_skip_to_closing_parenthesis (parser,
5618                                                       /*recovering=*/true,
5619                                                       /*or_comma=*/true,
5620                                                       /*consume_paren=*/true);
5621       if (ending < 0)
5622         goto get_comma;
5623       if (!ending)
5624         {
5625           parser->greater_than_is_operator_p
5626             = saved_greater_than_is_operator_p;
5627           return NULL;
5628         }
5629     }
5630
5631   parser->greater_than_is_operator_p
5632     = saved_greater_than_is_operator_p;
5633
5634   if (identifier)
5635     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5636
5637   return expression_list;
5638 }
5639
5640 /* Parse a pseudo-destructor-name.
5641
5642    pseudo-destructor-name:
5643      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5644      :: [opt] nested-name-specifier template template-id :: ~ type-name
5645      :: [opt] nested-name-specifier [opt] ~ type-name
5646
5647    If either of the first two productions is used, sets *SCOPE to the
5648    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5649    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5650    or ERROR_MARK_NODE if the parse fails.  */
5651
5652 static void
5653 cp_parser_pseudo_destructor_name (cp_parser* parser,
5654                                   tree* scope,
5655                                   tree* type)
5656 {
5657   bool nested_name_specifier_p;
5658
5659   /* Assume that things will not work out.  */
5660   *type = error_mark_node;
5661
5662   /* Look for the optional `::' operator.  */
5663   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5664   /* Look for the optional nested-name-specifier.  */
5665   nested_name_specifier_p
5666     = (cp_parser_nested_name_specifier_opt (parser,
5667                                             /*typename_keyword_p=*/false,
5668                                             /*check_dependency_p=*/true,
5669                                             /*type_p=*/false,
5670                                             /*is_declaration=*/false)
5671        != NULL_TREE);
5672   /* Now, if we saw a nested-name-specifier, we might be doing the
5673      second production.  */
5674   if (nested_name_specifier_p
5675       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5676     {
5677       /* Consume the `template' keyword.  */
5678       cp_lexer_consume_token (parser->lexer);
5679       /* Parse the template-id.  */
5680       cp_parser_template_id (parser,
5681                              /*template_keyword_p=*/true,
5682                              /*check_dependency_p=*/false,
5683                              /*is_declaration=*/true);
5684       /* Look for the `::' token.  */
5685       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5686     }
5687   /* If the next token is not a `~', then there might be some
5688      additional qualification.  */
5689   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5690     {
5691       /* At this point, we're looking for "type-name :: ~".  The type-name
5692          must not be a class-name, since this is a pseudo-destructor.  So,
5693          it must be either an enum-name, or a typedef-name -- both of which
5694          are just identifiers.  So, we peek ahead to check that the "::"
5695          and "~" tokens are present; if they are not, then we can avoid
5696          calling type_name.  */
5697       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5698           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5699           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5700         {
5701           cp_parser_error (parser, "non-scalar type");
5702           return;
5703         }
5704
5705       /* Look for the type-name.  */
5706       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5707       if (*scope == error_mark_node)
5708         return;
5709
5710       /* Look for the `::' token.  */
5711       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5712     }
5713   else
5714     *scope = NULL_TREE;
5715
5716   /* Look for the `~'.  */
5717   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5718
5719   /* Once we see the ~, this has to be a pseudo-destructor.  */
5720   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5721     cp_parser_commit_to_tentative_parse (parser);
5722
5723   /* Look for the type-name again.  We are not responsible for
5724      checking that it matches the first type-name.  */
5725   *type = cp_parser_nonclass_name (parser);
5726 }
5727
5728 /* Parse a unary-expression.
5729
5730    unary-expression:
5731      postfix-expression
5732      ++ cast-expression
5733      -- cast-expression
5734      unary-operator cast-expression
5735      sizeof unary-expression
5736      sizeof ( type-id )
5737      alignof ( type-id )  [C++0x]
5738      new-expression
5739      delete-expression
5740
5741    GNU Extensions:
5742
5743    unary-expression:
5744      __extension__ cast-expression
5745      __alignof__ unary-expression
5746      __alignof__ ( type-id )
5747      alignof unary-expression  [C++0x]
5748      __real__ cast-expression
5749      __imag__ cast-expression
5750      && identifier
5751
5752    ADDRESS_P is true iff the unary-expression is appearing as the
5753    operand of the `&' operator.   CAST_P is true if this expression is
5754    the target of a cast.
5755
5756    Returns a representation of the expression.  */
5757
5758 static tree
5759 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5760                             cp_id_kind * pidk)
5761 {
5762   cp_token *token;
5763   enum tree_code unary_operator;
5764
5765   /* Peek at the next token.  */
5766   token = cp_lexer_peek_token (parser->lexer);
5767   /* Some keywords give away the kind of expression.  */
5768   if (token->type == CPP_KEYWORD)
5769     {
5770       enum rid keyword = token->keyword;
5771
5772       switch (keyword)
5773         {
5774         case RID_ALIGNOF:
5775         case RID_SIZEOF:
5776           {
5777             tree operand;
5778             enum tree_code op;
5779
5780             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5781             /* Consume the token.  */
5782             cp_lexer_consume_token (parser->lexer);
5783             /* Parse the operand.  */
5784             operand = cp_parser_sizeof_operand (parser, keyword);
5785
5786             if (TYPE_P (operand))
5787               return cxx_sizeof_or_alignof_type (operand, op, true);
5788             else
5789               {
5790                 /* ISO C++ defines alignof only with types, not with
5791                    expressions. So pedwarn if alignof is used with a non-
5792                    type expression. However, __alignof__ is ok.  */
5793                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5794                   pedwarn (token->location, OPT_pedantic,
5795                            "ISO C++ does not allow %<alignof%> "
5796                            "with a non-type");
5797
5798                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5799               }
5800           }
5801
5802         case RID_NEW:
5803           return cp_parser_new_expression (parser);
5804
5805         case RID_DELETE:
5806           return cp_parser_delete_expression (parser);
5807
5808         case RID_EXTENSION:
5809           {
5810             /* The saved value of the PEDANTIC flag.  */
5811             int saved_pedantic;
5812             tree expr;
5813
5814             /* Save away the PEDANTIC flag.  */
5815             cp_parser_extension_opt (parser, &saved_pedantic);
5816             /* Parse the cast-expression.  */
5817             expr = cp_parser_simple_cast_expression (parser);
5818             /* Restore the PEDANTIC flag.  */
5819             pedantic = saved_pedantic;
5820
5821             return expr;
5822           }
5823
5824         case RID_REALPART:
5825         case RID_IMAGPART:
5826           {
5827             tree expression;
5828
5829             /* Consume the `__real__' or `__imag__' token.  */
5830             cp_lexer_consume_token (parser->lexer);
5831             /* Parse the cast-expression.  */
5832             expression = cp_parser_simple_cast_expression (parser);
5833             /* Create the complete representation.  */
5834             return build_x_unary_op ((keyword == RID_REALPART
5835                                       ? REALPART_EXPR : IMAGPART_EXPR),
5836                                      expression,
5837                                      tf_warning_or_error);
5838           }
5839           break;
5840
5841         case RID_NOEXCEPT:
5842           {
5843             tree expr;
5844             const char *saved_message;
5845             bool saved_integral_constant_expression_p;
5846             bool saved_non_integral_constant_expression_p;
5847             bool saved_greater_than_is_operator_p;
5848
5849             cp_lexer_consume_token (parser->lexer);
5850             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5851
5852             saved_message = parser->type_definition_forbidden_message;
5853             parser->type_definition_forbidden_message
5854               = G_("types may not be defined in %<noexcept%> expressions");
5855
5856             saved_integral_constant_expression_p
5857               = parser->integral_constant_expression_p;
5858             saved_non_integral_constant_expression_p
5859               = parser->non_integral_constant_expression_p;
5860             parser->integral_constant_expression_p = false;
5861
5862             saved_greater_than_is_operator_p
5863               = parser->greater_than_is_operator_p;
5864             parser->greater_than_is_operator_p = true;
5865
5866             ++cp_unevaluated_operand;
5867             ++c_inhibit_evaluation_warnings;
5868             expr = cp_parser_expression (parser, false, NULL);
5869             --c_inhibit_evaluation_warnings;
5870             --cp_unevaluated_operand;
5871
5872             parser->greater_than_is_operator_p
5873               = saved_greater_than_is_operator_p;
5874
5875             parser->integral_constant_expression_p
5876               = saved_integral_constant_expression_p;
5877             parser->non_integral_constant_expression_p
5878               = saved_non_integral_constant_expression_p;
5879
5880             parser->type_definition_forbidden_message = saved_message;
5881
5882             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5883             return finish_noexcept_expr (expr, tf_warning_or_error);
5884           }
5885
5886         default:
5887           break;
5888         }
5889     }
5890
5891   /* Look for the `:: new' and `:: delete', which also signal the
5892      beginning of a new-expression, or delete-expression,
5893      respectively.  If the next token is `::', then it might be one of
5894      these.  */
5895   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5896     {
5897       enum rid keyword;
5898
5899       /* See if the token after the `::' is one of the keywords in
5900          which we're interested.  */
5901       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5902       /* If it's `new', we have a new-expression.  */
5903       if (keyword == RID_NEW)
5904         return cp_parser_new_expression (parser);
5905       /* Similarly, for `delete'.  */
5906       else if (keyword == RID_DELETE)
5907         return cp_parser_delete_expression (parser);
5908     }
5909
5910   /* Look for a unary operator.  */
5911   unary_operator = cp_parser_unary_operator (token);
5912   /* The `++' and `--' operators can be handled similarly, even though
5913      they are not technically unary-operators in the grammar.  */
5914   if (unary_operator == ERROR_MARK)
5915     {
5916       if (token->type == CPP_PLUS_PLUS)
5917         unary_operator = PREINCREMENT_EXPR;
5918       else if (token->type == CPP_MINUS_MINUS)
5919         unary_operator = PREDECREMENT_EXPR;
5920       /* Handle the GNU address-of-label extension.  */
5921       else if (cp_parser_allow_gnu_extensions_p (parser)
5922                && token->type == CPP_AND_AND)
5923         {
5924           tree identifier;
5925           tree expression;
5926           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5927
5928           /* Consume the '&&' token.  */
5929           cp_lexer_consume_token (parser->lexer);
5930           /* Look for the identifier.  */
5931           identifier = cp_parser_identifier (parser);
5932           /* Create an expression representing the address.  */
5933           expression = finish_label_address_expr (identifier, loc);
5934           if (cp_parser_non_integral_constant_expression (parser,
5935                                                           NIC_ADDR_LABEL))
5936             expression = error_mark_node;
5937           return expression;
5938         }
5939     }
5940   if (unary_operator != ERROR_MARK)
5941     {
5942       tree cast_expression;
5943       tree expression = error_mark_node;
5944       non_integral_constant non_constant_p = NIC_NONE;
5945
5946       /* Consume the operator token.  */
5947       token = cp_lexer_consume_token (parser->lexer);
5948       /* Parse the cast-expression.  */
5949       cast_expression
5950         = cp_parser_cast_expression (parser,
5951                                      unary_operator == ADDR_EXPR,
5952                                      /*cast_p=*/false, pidk);
5953       /* Now, build an appropriate representation.  */
5954       switch (unary_operator)
5955         {
5956         case INDIRECT_REF:
5957           non_constant_p = NIC_STAR;
5958           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5959                                              tf_warning_or_error);
5960           break;
5961
5962         case ADDR_EXPR:
5963            non_constant_p = NIC_ADDR;
5964           /* Fall through.  */
5965         case BIT_NOT_EXPR:
5966           expression = build_x_unary_op (unary_operator, cast_expression,
5967                                          tf_warning_or_error);
5968           break;
5969
5970         case PREINCREMENT_EXPR:
5971         case PREDECREMENT_EXPR:
5972           non_constant_p = unary_operator == PREINCREMENT_EXPR
5973                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5974           /* Fall through.  */
5975         case UNARY_PLUS_EXPR:
5976         case NEGATE_EXPR:
5977         case TRUTH_NOT_EXPR:
5978           expression = finish_unary_op_expr (unary_operator, cast_expression);
5979           break;
5980
5981         default:
5982           gcc_unreachable ();
5983         }
5984
5985       if (non_constant_p != NIC_NONE
5986           && cp_parser_non_integral_constant_expression (parser,
5987                                                          non_constant_p))
5988         expression = error_mark_node;
5989
5990       return expression;
5991     }
5992
5993   return cp_parser_postfix_expression (parser, address_p, cast_p,
5994                                        /*member_access_only_p=*/false,
5995                                        pidk);
5996 }
5997
5998 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5999    unary-operator, the corresponding tree code is returned.  */
6000
6001 static enum tree_code
6002 cp_parser_unary_operator (cp_token* token)
6003 {
6004   switch (token->type)
6005     {
6006     case CPP_MULT:
6007       return INDIRECT_REF;
6008
6009     case CPP_AND:
6010       return ADDR_EXPR;
6011
6012     case CPP_PLUS:
6013       return UNARY_PLUS_EXPR;
6014
6015     case CPP_MINUS:
6016       return NEGATE_EXPR;
6017
6018     case CPP_NOT:
6019       return TRUTH_NOT_EXPR;
6020
6021     case CPP_COMPL:
6022       return BIT_NOT_EXPR;
6023
6024     default:
6025       return ERROR_MARK;
6026     }
6027 }
6028
6029 /* Parse a new-expression.
6030
6031    new-expression:
6032      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6033      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6034
6035    Returns a representation of the expression.  */
6036
6037 static tree
6038 cp_parser_new_expression (cp_parser* parser)
6039 {
6040   bool global_scope_p;
6041   VEC(tree,gc) *placement;
6042   tree type;
6043   VEC(tree,gc) *initializer;
6044   tree nelts;
6045   tree ret;
6046
6047   /* Look for the optional `::' operator.  */
6048   global_scope_p
6049     = (cp_parser_global_scope_opt (parser,
6050                                    /*current_scope_valid_p=*/false)
6051        != NULL_TREE);
6052   /* Look for the `new' operator.  */
6053   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6054   /* There's no easy way to tell a new-placement from the
6055      `( type-id )' construct.  */
6056   cp_parser_parse_tentatively (parser);
6057   /* Look for a new-placement.  */
6058   placement = cp_parser_new_placement (parser);
6059   /* If that didn't work out, there's no new-placement.  */
6060   if (!cp_parser_parse_definitely (parser))
6061     {
6062       if (placement != NULL)
6063         release_tree_vector (placement);
6064       placement = NULL;
6065     }
6066
6067   /* If the next token is a `(', then we have a parenthesized
6068      type-id.  */
6069   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6070     {
6071       cp_token *token;
6072       /* Consume the `('.  */
6073       cp_lexer_consume_token (parser->lexer);
6074       /* Parse the type-id.  */
6075       type = cp_parser_type_id (parser);
6076       /* Look for the closing `)'.  */
6077       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6078       token = cp_lexer_peek_token (parser->lexer);
6079       /* There should not be a direct-new-declarator in this production,
6080          but GCC used to allowed this, so we check and emit a sensible error
6081          message for this case.  */
6082       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6083         {
6084           error_at (token->location,
6085                     "array bound forbidden after parenthesized type-id");
6086           inform (token->location, 
6087                   "try removing the parentheses around the type-id");
6088           cp_parser_direct_new_declarator (parser);
6089         }
6090       nelts = NULL_TREE;
6091     }
6092   /* Otherwise, there must be a new-type-id.  */
6093   else
6094     type = cp_parser_new_type_id (parser, &nelts);
6095
6096   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6097   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6098       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6099     initializer = cp_parser_new_initializer (parser);
6100   else
6101     initializer = NULL;
6102
6103   /* A new-expression may not appear in an integral constant
6104      expression.  */
6105   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6106     ret = error_mark_node;
6107   else
6108     {
6109       /* Create a representation of the new-expression.  */
6110       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6111                        tf_warning_or_error);
6112     }
6113
6114   if (placement != NULL)
6115     release_tree_vector (placement);
6116   if (initializer != NULL)
6117     release_tree_vector (initializer);
6118
6119   return ret;
6120 }
6121
6122 /* Parse a new-placement.
6123
6124    new-placement:
6125      ( expression-list )
6126
6127    Returns the same representation as for an expression-list.  */
6128
6129 static VEC(tree,gc) *
6130 cp_parser_new_placement (cp_parser* parser)
6131 {
6132   VEC(tree,gc) *expression_list;
6133
6134   /* Parse the expression-list.  */
6135   expression_list = (cp_parser_parenthesized_expression_list
6136                      (parser, non_attr, /*cast_p=*/false,
6137                       /*allow_expansion_p=*/true,
6138                       /*non_constant_p=*/NULL));
6139
6140   return expression_list;
6141 }
6142
6143 /* Parse a new-type-id.
6144
6145    new-type-id:
6146      type-specifier-seq new-declarator [opt]
6147
6148    Returns the TYPE allocated.  If the new-type-id indicates an array
6149    type, *NELTS is set to the number of elements in the last array
6150    bound; the TYPE will not include the last array bound.  */
6151
6152 static tree
6153 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6154 {
6155   cp_decl_specifier_seq type_specifier_seq;
6156   cp_declarator *new_declarator;
6157   cp_declarator *declarator;
6158   cp_declarator *outer_declarator;
6159   const char *saved_message;
6160   tree type;
6161
6162   /* The type-specifier sequence must not contain type definitions.
6163      (It cannot contain declarations of new types either, but if they
6164      are not definitions we will catch that because they are not
6165      complete.)  */
6166   saved_message = parser->type_definition_forbidden_message;
6167   parser->type_definition_forbidden_message
6168     = G_("types may not be defined in a new-type-id");
6169   /* Parse the type-specifier-seq.  */
6170   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6171                                 /*is_trailing_return=*/false,
6172                                 &type_specifier_seq);
6173   /* Restore the old message.  */
6174   parser->type_definition_forbidden_message = saved_message;
6175   /* Parse the new-declarator.  */
6176   new_declarator = cp_parser_new_declarator_opt (parser);
6177
6178   /* Determine the number of elements in the last array dimension, if
6179      any.  */
6180   *nelts = NULL_TREE;
6181   /* Skip down to the last array dimension.  */
6182   declarator = new_declarator;
6183   outer_declarator = NULL;
6184   while (declarator && (declarator->kind == cdk_pointer
6185                         || declarator->kind == cdk_ptrmem))
6186     {
6187       outer_declarator = declarator;
6188       declarator = declarator->declarator;
6189     }
6190   while (declarator
6191          && declarator->kind == cdk_array
6192          && declarator->declarator
6193          && declarator->declarator->kind == cdk_array)
6194     {
6195       outer_declarator = declarator;
6196       declarator = declarator->declarator;
6197     }
6198
6199   if (declarator && declarator->kind == cdk_array)
6200     {
6201       *nelts = declarator->u.array.bounds;
6202       if (*nelts == error_mark_node)
6203         *nelts = integer_one_node;
6204
6205       if (outer_declarator)
6206         outer_declarator->declarator = declarator->declarator;
6207       else
6208         new_declarator = NULL;
6209     }
6210
6211   type = groktypename (&type_specifier_seq, new_declarator, false);
6212   return type;
6213 }
6214
6215 /* Parse an (optional) new-declarator.
6216
6217    new-declarator:
6218      ptr-operator new-declarator [opt]
6219      direct-new-declarator
6220
6221    Returns the declarator.  */
6222
6223 static cp_declarator *
6224 cp_parser_new_declarator_opt (cp_parser* parser)
6225 {
6226   enum tree_code code;
6227   tree type;
6228   cp_cv_quals cv_quals;
6229
6230   /* We don't know if there's a ptr-operator next, or not.  */
6231   cp_parser_parse_tentatively (parser);
6232   /* Look for a ptr-operator.  */
6233   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6234   /* If that worked, look for more new-declarators.  */
6235   if (cp_parser_parse_definitely (parser))
6236     {
6237       cp_declarator *declarator;
6238
6239       /* Parse another optional declarator.  */
6240       declarator = cp_parser_new_declarator_opt (parser);
6241
6242       return cp_parser_make_indirect_declarator
6243         (code, type, cv_quals, declarator);
6244     }
6245
6246   /* If the next token is a `[', there is a direct-new-declarator.  */
6247   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6248     return cp_parser_direct_new_declarator (parser);
6249
6250   return NULL;
6251 }
6252
6253 /* Parse a direct-new-declarator.
6254
6255    direct-new-declarator:
6256      [ expression ]
6257      direct-new-declarator [constant-expression]
6258
6259    */
6260
6261 static cp_declarator *
6262 cp_parser_direct_new_declarator (cp_parser* parser)
6263 {
6264   cp_declarator *declarator = NULL;
6265
6266   while (true)
6267     {
6268       tree expression;
6269
6270       /* Look for the opening `['.  */
6271       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6272       /* The first expression is not required to be constant.  */
6273       if (!declarator)
6274         {
6275           cp_token *token = cp_lexer_peek_token (parser->lexer);
6276           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6277           /* The standard requires that the expression have integral
6278              type.  DR 74 adds enumeration types.  We believe that the
6279              real intent is that these expressions be handled like the
6280              expression in a `switch' condition, which also allows
6281              classes with a single conversion to integral or
6282              enumeration type.  */
6283           if (!processing_template_decl)
6284             {
6285               expression
6286                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6287                                               expression,
6288                                               /*complain=*/true);
6289               if (!expression)
6290                 {
6291                   error_at (token->location,
6292                             "expression in new-declarator must have integral "
6293                             "or enumeration type");
6294                   expression = error_mark_node;
6295                 }
6296             }
6297         }
6298       /* But all the other expressions must be.  */
6299       else
6300         expression
6301           = cp_parser_constant_expression (parser,
6302                                            /*allow_non_constant=*/false,
6303                                            NULL);
6304       /* Look for the closing `]'.  */
6305       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6306
6307       /* Add this bound to the declarator.  */
6308       declarator = make_array_declarator (declarator, expression);
6309
6310       /* If the next token is not a `[', then there are no more
6311          bounds.  */
6312       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6313         break;
6314     }
6315
6316   return declarator;
6317 }
6318
6319 /* Parse a new-initializer.
6320
6321    new-initializer:
6322      ( expression-list [opt] )
6323      braced-init-list
6324
6325    Returns a representation of the expression-list.  */
6326
6327 static VEC(tree,gc) *
6328 cp_parser_new_initializer (cp_parser* parser)
6329 {
6330   VEC(tree,gc) *expression_list;
6331
6332   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6333     {
6334       tree t;
6335       bool expr_non_constant_p;
6336       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6337       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6338       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6339       expression_list = make_tree_vector_single (t);
6340     }
6341   else
6342     expression_list = (cp_parser_parenthesized_expression_list
6343                        (parser, non_attr, /*cast_p=*/false,
6344                         /*allow_expansion_p=*/true,
6345                         /*non_constant_p=*/NULL));
6346
6347   return expression_list;
6348 }
6349
6350 /* Parse a delete-expression.
6351
6352    delete-expression:
6353      :: [opt] delete cast-expression
6354      :: [opt] delete [ ] cast-expression
6355
6356    Returns a representation of the expression.  */
6357
6358 static tree
6359 cp_parser_delete_expression (cp_parser* parser)
6360 {
6361   bool global_scope_p;
6362   bool array_p;
6363   tree expression;
6364
6365   /* Look for the optional `::' operator.  */
6366   global_scope_p
6367     = (cp_parser_global_scope_opt (parser,
6368                                    /*current_scope_valid_p=*/false)
6369        != NULL_TREE);
6370   /* Look for the `delete' keyword.  */
6371   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6372   /* See if the array syntax is in use.  */
6373   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6374     {
6375       /* Consume the `[' token.  */
6376       cp_lexer_consume_token (parser->lexer);
6377       /* Look for the `]' token.  */
6378       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6379       /* Remember that this is the `[]' construct.  */
6380       array_p = true;
6381     }
6382   else
6383     array_p = false;
6384
6385   /* Parse the cast-expression.  */
6386   expression = cp_parser_simple_cast_expression (parser);
6387
6388   /* A delete-expression may not appear in an integral constant
6389      expression.  */
6390   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6391     return error_mark_node;
6392
6393   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6394                         tf_warning_or_error);
6395 }
6396
6397 /* Returns true if TOKEN may start a cast-expression and false
6398    otherwise.  */
6399
6400 static bool
6401 cp_parser_token_starts_cast_expression (cp_token *token)
6402 {
6403   switch (token->type)
6404     {
6405     case CPP_COMMA:
6406     case CPP_SEMICOLON:
6407     case CPP_QUERY:
6408     case CPP_COLON:
6409     case CPP_CLOSE_SQUARE:
6410     case CPP_CLOSE_PAREN:
6411     case CPP_CLOSE_BRACE:
6412     case CPP_DOT:
6413     case CPP_DOT_STAR:
6414     case CPP_DEREF:
6415     case CPP_DEREF_STAR:
6416     case CPP_DIV:
6417     case CPP_MOD:
6418     case CPP_LSHIFT:
6419     case CPP_RSHIFT:
6420     case CPP_LESS:
6421     case CPP_GREATER:
6422     case CPP_LESS_EQ:
6423     case CPP_GREATER_EQ:
6424     case CPP_EQ_EQ:
6425     case CPP_NOT_EQ:
6426     case CPP_EQ:
6427     case CPP_MULT_EQ:
6428     case CPP_DIV_EQ:
6429     case CPP_MOD_EQ:
6430     case CPP_PLUS_EQ:
6431     case CPP_MINUS_EQ:
6432     case CPP_RSHIFT_EQ:
6433     case CPP_LSHIFT_EQ:
6434     case CPP_AND_EQ:
6435     case CPP_XOR_EQ:
6436     case CPP_OR_EQ:
6437     case CPP_XOR:
6438     case CPP_OR:
6439     case CPP_OR_OR:
6440     case CPP_EOF:
6441       return false;
6442
6443       /* '[' may start a primary-expression in obj-c++.  */
6444     case CPP_OPEN_SQUARE:
6445       return c_dialect_objc ();
6446
6447     default:
6448       return true;
6449     }
6450 }
6451
6452 /* Parse a cast-expression.
6453
6454    cast-expression:
6455      unary-expression
6456      ( type-id ) cast-expression
6457
6458    ADDRESS_P is true iff the unary-expression is appearing as the
6459    operand of the `&' operator.   CAST_P is true if this expression is
6460    the target of a cast.
6461
6462    Returns a representation of the expression.  */
6463
6464 static tree
6465 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6466                            cp_id_kind * pidk)
6467 {
6468   /* If it's a `(', then we might be looking at a cast.  */
6469   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6470     {
6471       tree type = NULL_TREE;
6472       tree expr = NULL_TREE;
6473       bool compound_literal_p;
6474       const char *saved_message;
6475
6476       /* There's no way to know yet whether or not this is a cast.
6477          For example, `(int (3))' is a unary-expression, while `(int)
6478          3' is a cast.  So, we resort to parsing tentatively.  */
6479       cp_parser_parse_tentatively (parser);
6480       /* Types may not be defined in a cast.  */
6481       saved_message = parser->type_definition_forbidden_message;
6482       parser->type_definition_forbidden_message
6483         = G_("types may not be defined in casts");
6484       /* Consume the `('.  */
6485       cp_lexer_consume_token (parser->lexer);
6486       /* A very tricky bit is that `(struct S) { 3 }' is a
6487          compound-literal (which we permit in C++ as an extension).
6488          But, that construct is not a cast-expression -- it is a
6489          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6490          is legal; if the compound-literal were a cast-expression,
6491          you'd need an extra set of parentheses.)  But, if we parse
6492          the type-id, and it happens to be a class-specifier, then we
6493          will commit to the parse at that point, because we cannot
6494          undo the action that is done when creating a new class.  So,
6495          then we cannot back up and do a postfix-expression.
6496
6497          Therefore, we scan ahead to the closing `)', and check to see
6498          if the token after the `)' is a `{'.  If so, we are not
6499          looking at a cast-expression.
6500
6501          Save tokens so that we can put them back.  */
6502       cp_lexer_save_tokens (parser->lexer);
6503       /* Skip tokens until the next token is a closing parenthesis.
6504          If we find the closing `)', and the next token is a `{', then
6505          we are looking at a compound-literal.  */
6506       compound_literal_p
6507         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6508                                                   /*consume_paren=*/true)
6509            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6510       /* Roll back the tokens we skipped.  */
6511       cp_lexer_rollback_tokens (parser->lexer);
6512       /* If we were looking at a compound-literal, simulate an error
6513          so that the call to cp_parser_parse_definitely below will
6514          fail.  */
6515       if (compound_literal_p)
6516         cp_parser_simulate_error (parser);
6517       else
6518         {
6519           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6520           parser->in_type_id_in_expr_p = true;
6521           /* Look for the type-id.  */
6522           type = cp_parser_type_id (parser);
6523           /* Look for the closing `)'.  */
6524           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6525           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6526         }
6527
6528       /* Restore the saved message.  */
6529       parser->type_definition_forbidden_message = saved_message;
6530
6531       /* At this point this can only be either a cast or a
6532          parenthesized ctor such as `(T ())' that looks like a cast to
6533          function returning T.  */
6534       if (!cp_parser_error_occurred (parser)
6535           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6536                                                      (parser->lexer)))
6537         {
6538           cp_parser_parse_definitely (parser);
6539           expr = cp_parser_cast_expression (parser,
6540                                             /*address_p=*/false,
6541                                             /*cast_p=*/true, pidk);
6542
6543           /* Warn about old-style casts, if so requested.  */
6544           if (warn_old_style_cast
6545               && !in_system_header
6546               && !VOID_TYPE_P (type)
6547               && current_lang_name != lang_name_c)
6548             warning (OPT_Wold_style_cast, "use of old-style cast");
6549
6550           /* Only type conversions to integral or enumeration types
6551              can be used in constant-expressions.  */
6552           if (!cast_valid_in_integral_constant_expression_p (type)
6553               && cp_parser_non_integral_constant_expression (parser,
6554                                                              NIC_CAST))
6555             return error_mark_node;
6556
6557           /* Perform the cast.  */
6558           expr = build_c_cast (input_location, type, expr);
6559           return expr;
6560         }
6561       else 
6562         cp_parser_abort_tentative_parse (parser);
6563     }
6564
6565   /* If we get here, then it's not a cast, so it must be a
6566      unary-expression.  */
6567   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6568 }
6569
6570 /* Parse a binary expression of the general form:
6571
6572    pm-expression:
6573      cast-expression
6574      pm-expression .* cast-expression
6575      pm-expression ->* cast-expression
6576
6577    multiplicative-expression:
6578      pm-expression
6579      multiplicative-expression * pm-expression
6580      multiplicative-expression / pm-expression
6581      multiplicative-expression % pm-expression
6582
6583    additive-expression:
6584      multiplicative-expression
6585      additive-expression + multiplicative-expression
6586      additive-expression - multiplicative-expression
6587
6588    shift-expression:
6589      additive-expression
6590      shift-expression << additive-expression
6591      shift-expression >> additive-expression
6592
6593    relational-expression:
6594      shift-expression
6595      relational-expression < shift-expression
6596      relational-expression > shift-expression
6597      relational-expression <= shift-expression
6598      relational-expression >= shift-expression
6599
6600   GNU Extension:
6601
6602    relational-expression:
6603      relational-expression <? shift-expression
6604      relational-expression >? shift-expression
6605
6606    equality-expression:
6607      relational-expression
6608      equality-expression == relational-expression
6609      equality-expression != relational-expression
6610
6611    and-expression:
6612      equality-expression
6613      and-expression & equality-expression
6614
6615    exclusive-or-expression:
6616      and-expression
6617      exclusive-or-expression ^ and-expression
6618
6619    inclusive-or-expression:
6620      exclusive-or-expression
6621      inclusive-or-expression | exclusive-or-expression
6622
6623    logical-and-expression:
6624      inclusive-or-expression
6625      logical-and-expression && inclusive-or-expression
6626
6627    logical-or-expression:
6628      logical-and-expression
6629      logical-or-expression || logical-and-expression
6630
6631    All these are implemented with a single function like:
6632
6633    binary-expression:
6634      simple-cast-expression
6635      binary-expression <token> binary-expression
6636
6637    CAST_P is true if this expression is the target of a cast.
6638
6639    The binops_by_token map is used to get the tree codes for each <token> type.
6640    binary-expressions are associated according to a precedence table.  */
6641
6642 #define TOKEN_PRECEDENCE(token)                              \
6643 (((token->type == CPP_GREATER                                \
6644    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6645   && !parser->greater_than_is_operator_p)                    \
6646  ? PREC_NOT_OPERATOR                                         \
6647  : binops_by_token[token->type].prec)
6648
6649 static tree
6650 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6651                              bool no_toplevel_fold_p,
6652                              enum cp_parser_prec prec,
6653                              cp_id_kind * pidk)
6654 {
6655   cp_parser_expression_stack stack;
6656   cp_parser_expression_stack_entry *sp = &stack[0];
6657   tree lhs, rhs;
6658   cp_token *token;
6659   enum tree_code tree_type, lhs_type, rhs_type;
6660   enum cp_parser_prec new_prec, lookahead_prec;
6661   tree overload;
6662
6663   /* Parse the first expression.  */
6664   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6665   lhs_type = ERROR_MARK;
6666
6667   for (;;)
6668     {
6669       /* Get an operator token.  */
6670       token = cp_lexer_peek_token (parser->lexer);
6671
6672       if (warn_cxx0x_compat
6673           && token->type == CPP_RSHIFT
6674           && !parser->greater_than_is_operator_p)
6675         {
6676           if (warning_at (token->location, OPT_Wc__0x_compat, 
6677                           "%<>>%> operator will be treated as"
6678                           " two right angle brackets in C++0x"))
6679             inform (token->location,
6680                     "suggest parentheses around %<>>%> expression");
6681         }
6682
6683       new_prec = TOKEN_PRECEDENCE (token);
6684
6685       /* Popping an entry off the stack means we completed a subexpression:
6686          - either we found a token which is not an operator (`>' where it is not
6687            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6688            will happen repeatedly;
6689          - or, we found an operator which has lower priority.  This is the case
6690            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6691            parsing `3 * 4'.  */
6692       if (new_prec <= prec)
6693         {
6694           if (sp == stack)
6695             break;
6696           else
6697             goto pop;
6698         }
6699
6700      get_rhs:
6701       tree_type = binops_by_token[token->type].tree_type;
6702
6703       /* We used the operator token.  */
6704       cp_lexer_consume_token (parser->lexer);
6705
6706       /* For "false && x" or "true || x", x will never be executed;
6707          disable warnings while evaluating it.  */
6708       if (tree_type == TRUTH_ANDIF_EXPR)
6709         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6710       else if (tree_type == TRUTH_ORIF_EXPR)
6711         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6712
6713       /* Extract another operand.  It may be the RHS of this expression
6714          or the LHS of a new, higher priority expression.  */
6715       rhs = cp_parser_simple_cast_expression (parser);
6716       rhs_type = ERROR_MARK;
6717
6718       /* Get another operator token.  Look up its precedence to avoid
6719          building a useless (immediately popped) stack entry for common
6720          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6721       token = cp_lexer_peek_token (parser->lexer);
6722       lookahead_prec = TOKEN_PRECEDENCE (token);
6723       if (lookahead_prec > new_prec)
6724         {
6725           /* ... and prepare to parse the RHS of the new, higher priority
6726              expression.  Since precedence levels on the stack are
6727              monotonically increasing, we do not have to care about
6728              stack overflows.  */
6729           sp->prec = prec;
6730           sp->tree_type = tree_type;
6731           sp->lhs = lhs;
6732           sp->lhs_type = lhs_type;
6733           sp++;
6734           lhs = rhs;
6735           lhs_type = rhs_type;
6736           prec = new_prec;
6737           new_prec = lookahead_prec;
6738           goto get_rhs;
6739
6740          pop:
6741           lookahead_prec = new_prec;
6742           /* If the stack is not empty, we have parsed into LHS the right side
6743              (`4' in the example above) of an expression we had suspended.
6744              We can use the information on the stack to recover the LHS (`3')
6745              from the stack together with the tree code (`MULT_EXPR'), and
6746              the precedence of the higher level subexpression
6747              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6748              which will be used to actually build the additive expression.  */
6749           --sp;
6750           prec = sp->prec;
6751           tree_type = sp->tree_type;
6752           rhs = lhs;
6753           rhs_type = lhs_type;
6754           lhs = sp->lhs;
6755           lhs_type = sp->lhs_type;
6756         }
6757
6758       /* Undo the disabling of warnings done above.  */
6759       if (tree_type == TRUTH_ANDIF_EXPR)
6760         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6761       else if (tree_type == TRUTH_ORIF_EXPR)
6762         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6763
6764       overload = NULL;
6765       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6766          ERROR_MARK for everything that is not a binary expression.
6767          This makes warn_about_parentheses miss some warnings that
6768          involve unary operators.  For unary expressions we should
6769          pass the correct tree_code unless the unary expression was
6770          surrounded by parentheses.
6771       */
6772       if (no_toplevel_fold_p
6773           && lookahead_prec <= prec
6774           && sp == stack
6775           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6776         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6777       else
6778         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6779                                  &overload, tf_warning_or_error);
6780       lhs_type = tree_type;
6781
6782       /* If the binary operator required the use of an overloaded operator,
6783          then this expression cannot be an integral constant-expression.
6784          An overloaded operator can be used even if both operands are
6785          otherwise permissible in an integral constant-expression if at
6786          least one of the operands is of enumeration type.  */
6787
6788       if (overload
6789           && cp_parser_non_integral_constant_expression (parser,
6790                                                          NIC_OVERLOADED))
6791         return error_mark_node;
6792     }
6793
6794   return lhs;
6795 }
6796
6797
6798 /* Parse the `? expression : assignment-expression' part of a
6799    conditional-expression.  The LOGICAL_OR_EXPR is the
6800    logical-or-expression that started the conditional-expression.
6801    Returns a representation of the entire conditional-expression.
6802
6803    This routine is used by cp_parser_assignment_expression.
6804
6805      ? expression : assignment-expression
6806
6807    GNU Extensions:
6808
6809      ? : assignment-expression */
6810
6811 static tree
6812 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6813 {
6814   tree expr;
6815   tree assignment_expr;
6816   struct cp_token *token;
6817
6818   /* Consume the `?' token.  */
6819   cp_lexer_consume_token (parser->lexer);
6820   token = cp_lexer_peek_token (parser->lexer);
6821   if (cp_parser_allow_gnu_extensions_p (parser)
6822       && token->type == CPP_COLON)
6823     {
6824       pedwarn (token->location, OPT_pedantic, 
6825                "ISO C++ does not allow ?: with omitted middle operand");
6826       /* Implicit true clause.  */
6827       expr = NULL_TREE;
6828       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6829       warn_for_omitted_condop (token->location, logical_or_expr);
6830     }
6831   else
6832     {
6833       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6834       parser->colon_corrects_to_scope_p = false;
6835       /* Parse the expression.  */
6836       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6837       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6838       c_inhibit_evaluation_warnings +=
6839         ((logical_or_expr == truthvalue_true_node)
6840          - (logical_or_expr == truthvalue_false_node));
6841       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6842     }
6843
6844   /* The next token should be a `:'.  */
6845   cp_parser_require (parser, CPP_COLON, RT_COLON);
6846   /* Parse the assignment-expression.  */
6847   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6848   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6849
6850   /* Build the conditional-expression.  */
6851   return build_x_conditional_expr (logical_or_expr,
6852                                    expr,
6853                                    assignment_expr,
6854                                    tf_warning_or_error);
6855 }
6856
6857 /* Parse an assignment-expression.
6858
6859    assignment-expression:
6860      conditional-expression
6861      logical-or-expression assignment-operator assignment_expression
6862      throw-expression
6863
6864    CAST_P is true if this expression is the target of a cast.
6865
6866    Returns a representation for the expression.  */
6867
6868 static tree
6869 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6870                                  cp_id_kind * pidk)
6871 {
6872   tree expr;
6873
6874   /* If the next token is the `throw' keyword, then we're looking at
6875      a throw-expression.  */
6876   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6877     expr = cp_parser_throw_expression (parser);
6878   /* Otherwise, it must be that we are looking at a
6879      logical-or-expression.  */
6880   else
6881     {
6882       /* Parse the binary expressions (logical-or-expression).  */
6883       expr = cp_parser_binary_expression (parser, cast_p, false,
6884                                           PREC_NOT_OPERATOR, pidk);
6885       /* If the next token is a `?' then we're actually looking at a
6886          conditional-expression.  */
6887       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6888         return cp_parser_question_colon_clause (parser, expr);
6889       else
6890         {
6891           enum tree_code assignment_operator;
6892
6893           /* If it's an assignment-operator, we're using the second
6894              production.  */
6895           assignment_operator
6896             = cp_parser_assignment_operator_opt (parser);
6897           if (assignment_operator != ERROR_MARK)
6898             {
6899               bool non_constant_p;
6900
6901               /* Parse the right-hand side of the assignment.  */
6902               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6903
6904               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6905                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6906
6907               /* An assignment may not appear in a
6908                  constant-expression.  */
6909               if (cp_parser_non_integral_constant_expression (parser,
6910                                                               NIC_ASSIGNMENT))
6911                 return error_mark_node;
6912               /* Build the assignment expression.  */
6913               expr = build_x_modify_expr (expr,
6914                                           assignment_operator,
6915                                           rhs,
6916                                           tf_warning_or_error);
6917             }
6918         }
6919     }
6920
6921   return expr;
6922 }
6923
6924 /* Parse an (optional) assignment-operator.
6925
6926    assignment-operator: one of
6927      = *= /= %= += -= >>= <<= &= ^= |=
6928
6929    GNU Extension:
6930
6931    assignment-operator: one of
6932      <?= >?=
6933
6934    If the next token is an assignment operator, the corresponding tree
6935    code is returned, and the token is consumed.  For example, for
6936    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6937    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6938    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6939    operator, ERROR_MARK is returned.  */
6940
6941 static enum tree_code
6942 cp_parser_assignment_operator_opt (cp_parser* parser)
6943 {
6944   enum tree_code op;
6945   cp_token *token;
6946
6947   /* Peek at the next token.  */
6948   token = cp_lexer_peek_token (parser->lexer);
6949
6950   switch (token->type)
6951     {
6952     case CPP_EQ:
6953       op = NOP_EXPR;
6954       break;
6955
6956     case CPP_MULT_EQ:
6957       op = MULT_EXPR;
6958       break;
6959
6960     case CPP_DIV_EQ:
6961       op = TRUNC_DIV_EXPR;
6962       break;
6963
6964     case CPP_MOD_EQ:
6965       op = TRUNC_MOD_EXPR;
6966       break;
6967
6968     case CPP_PLUS_EQ:
6969       op = PLUS_EXPR;
6970       break;
6971
6972     case CPP_MINUS_EQ:
6973       op = MINUS_EXPR;
6974       break;
6975
6976     case CPP_RSHIFT_EQ:
6977       op = RSHIFT_EXPR;
6978       break;
6979
6980     case CPP_LSHIFT_EQ:
6981       op = LSHIFT_EXPR;
6982       break;
6983
6984     case CPP_AND_EQ:
6985       op = BIT_AND_EXPR;
6986       break;
6987
6988     case CPP_XOR_EQ:
6989       op = BIT_XOR_EXPR;
6990       break;
6991
6992     case CPP_OR_EQ:
6993       op = BIT_IOR_EXPR;
6994       break;
6995
6996     default:
6997       /* Nothing else is an assignment operator.  */
6998       op = ERROR_MARK;
6999     }
7000
7001   /* If it was an assignment operator, consume it.  */
7002   if (op != ERROR_MARK)
7003     cp_lexer_consume_token (parser->lexer);
7004
7005   return op;
7006 }
7007
7008 /* Parse an expression.
7009
7010    expression:
7011      assignment-expression
7012      expression , assignment-expression
7013
7014    CAST_P is true if this expression is the target of a cast.
7015
7016    Returns a representation of the expression.  */
7017
7018 static tree
7019 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7020 {
7021   tree expression = NULL_TREE;
7022
7023   while (true)
7024     {
7025       tree assignment_expression;
7026
7027       /* Parse the next assignment-expression.  */
7028       assignment_expression
7029         = cp_parser_assignment_expression (parser, cast_p, pidk);
7030       /* If this is the first assignment-expression, we can just
7031          save it away.  */
7032       if (!expression)
7033         expression = assignment_expression;
7034       else
7035         expression = build_x_compound_expr (expression,
7036                                             assignment_expression,
7037                                             tf_warning_or_error);
7038       /* If the next token is not a comma, then we are done with the
7039          expression.  */
7040       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7041         break;
7042       /* Consume the `,'.  */
7043       cp_lexer_consume_token (parser->lexer);
7044       /* A comma operator cannot appear in a constant-expression.  */
7045       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7046         expression = error_mark_node;
7047     }
7048
7049   return expression;
7050 }
7051
7052 /* Parse a constant-expression.
7053
7054    constant-expression:
7055      conditional-expression
7056
7057   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7058   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7059   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7060   is false, NON_CONSTANT_P should be NULL.  */
7061
7062 static tree
7063 cp_parser_constant_expression (cp_parser* parser,
7064                                bool allow_non_constant_p,
7065                                bool *non_constant_p)
7066 {
7067   bool saved_integral_constant_expression_p;
7068   bool saved_allow_non_integral_constant_expression_p;
7069   bool saved_non_integral_constant_expression_p;
7070   tree expression;
7071
7072   /* It might seem that we could simply parse the
7073      conditional-expression, and then check to see if it were
7074      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7075      one that the compiler can figure out is constant, possibly after
7076      doing some simplifications or optimizations.  The standard has a
7077      precise definition of constant-expression, and we must honor
7078      that, even though it is somewhat more restrictive.
7079
7080      For example:
7081
7082        int i[(2, 3)];
7083
7084      is not a legal declaration, because `(2, 3)' is not a
7085      constant-expression.  The `,' operator is forbidden in a
7086      constant-expression.  However, GCC's constant-folding machinery
7087      will fold this operation to an INTEGER_CST for `3'.  */
7088
7089   /* Save the old settings.  */
7090   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7091   saved_allow_non_integral_constant_expression_p
7092     = parser->allow_non_integral_constant_expression_p;
7093   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7094   /* We are now parsing a constant-expression.  */
7095   parser->integral_constant_expression_p = true;
7096   parser->allow_non_integral_constant_expression_p
7097     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7098   parser->non_integral_constant_expression_p = false;
7099   /* Although the grammar says "conditional-expression", we parse an
7100      "assignment-expression", which also permits "throw-expression"
7101      and the use of assignment operators.  In the case that
7102      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7103      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7104      actually essential that we look for an assignment-expression.
7105      For example, cp_parser_initializer_clauses uses this function to
7106      determine whether a particular assignment-expression is in fact
7107      constant.  */
7108   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7109   /* Restore the old settings.  */
7110   parser->integral_constant_expression_p
7111     = saved_integral_constant_expression_p;
7112   parser->allow_non_integral_constant_expression_p
7113     = saved_allow_non_integral_constant_expression_p;
7114   if (cxx_dialect >= cxx0x)
7115     {
7116       /* Require an rvalue constant expression here; that's what our
7117          callers expect.  Reference constant expressions are handled
7118          separately in e.g. cp_parser_template_argument.  */
7119       bool is_const = potential_rvalue_constant_expression (expression);
7120       parser->non_integral_constant_expression_p = !is_const;
7121       if (!is_const && !allow_non_constant_p)
7122         require_potential_rvalue_constant_expression (expression);
7123     }
7124   if (allow_non_constant_p)
7125     *non_constant_p = parser->non_integral_constant_expression_p;
7126   parser->non_integral_constant_expression_p
7127     = saved_non_integral_constant_expression_p;
7128
7129   return expression;
7130 }
7131
7132 /* Parse __builtin_offsetof.
7133
7134    offsetof-expression:
7135      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7136
7137    offsetof-member-designator:
7138      id-expression
7139      | offsetof-member-designator "." id-expression
7140      | offsetof-member-designator "[" expression "]"
7141      | offsetof-member-designator "->" id-expression  */
7142
7143 static tree
7144 cp_parser_builtin_offsetof (cp_parser *parser)
7145 {
7146   int save_ice_p, save_non_ice_p;
7147   tree type, expr;
7148   cp_id_kind dummy;
7149   cp_token *token;
7150
7151   /* We're about to accept non-integral-constant things, but will
7152      definitely yield an integral constant expression.  Save and
7153      restore these values around our local parsing.  */
7154   save_ice_p = parser->integral_constant_expression_p;
7155   save_non_ice_p = parser->non_integral_constant_expression_p;
7156
7157   /* Consume the "__builtin_offsetof" token.  */
7158   cp_lexer_consume_token (parser->lexer);
7159   /* Consume the opening `('.  */
7160   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7161   /* Parse the type-id.  */
7162   type = cp_parser_type_id (parser);
7163   /* Look for the `,'.  */
7164   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7165   token = cp_lexer_peek_token (parser->lexer);
7166
7167   /* Build the (type *)null that begins the traditional offsetof macro.  */
7168   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7169                             tf_warning_or_error);
7170
7171   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7172   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7173                                                  true, &dummy, token->location);
7174   while (true)
7175     {
7176       token = cp_lexer_peek_token (parser->lexer);
7177       switch (token->type)
7178         {
7179         case CPP_OPEN_SQUARE:
7180           /* offsetof-member-designator "[" expression "]" */
7181           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7182           break;
7183
7184         case CPP_DEREF:
7185           /* offsetof-member-designator "->" identifier */
7186           expr = grok_array_decl (expr, integer_zero_node);
7187           /* FALLTHRU */
7188
7189         case CPP_DOT:
7190           /* offsetof-member-designator "." identifier */
7191           cp_lexer_consume_token (parser->lexer);
7192           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7193                                                          expr, true, &dummy,
7194                                                          token->location);
7195           break;
7196
7197         case CPP_CLOSE_PAREN:
7198           /* Consume the ")" token.  */
7199           cp_lexer_consume_token (parser->lexer);
7200           goto success;
7201
7202         default:
7203           /* Error.  We know the following require will fail, but
7204              that gives the proper error message.  */
7205           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7206           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7207           expr = error_mark_node;
7208           goto failure;
7209         }
7210     }
7211
7212  success:
7213   /* If we're processing a template, we can't finish the semantics yet.
7214      Otherwise we can fold the entire expression now.  */
7215   if (processing_template_decl)
7216     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7217   else
7218     expr = finish_offsetof (expr);
7219
7220  failure:
7221   parser->integral_constant_expression_p = save_ice_p;
7222   parser->non_integral_constant_expression_p = save_non_ice_p;
7223
7224   return expr;
7225 }
7226
7227 /* Parse a trait expression.
7228
7229    Returns a representation of the expression, the underlying type
7230    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7231
7232 static tree
7233 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7234 {
7235   cp_trait_kind kind;
7236   tree type1, type2 = NULL_TREE;
7237   bool binary = false;
7238   cp_decl_specifier_seq decl_specs;
7239
7240   switch (keyword)
7241     {
7242     case RID_HAS_NOTHROW_ASSIGN:
7243       kind = CPTK_HAS_NOTHROW_ASSIGN;
7244       break;
7245     case RID_HAS_NOTHROW_CONSTRUCTOR:
7246       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7247       break;
7248     case RID_HAS_NOTHROW_COPY:
7249       kind = CPTK_HAS_NOTHROW_COPY;
7250       break;
7251     case RID_HAS_TRIVIAL_ASSIGN:
7252       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7253       break;
7254     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7255       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7256       break;
7257     case RID_HAS_TRIVIAL_COPY:
7258       kind = CPTK_HAS_TRIVIAL_COPY;
7259       break;
7260     case RID_HAS_TRIVIAL_DESTRUCTOR:
7261       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7262       break;
7263     case RID_HAS_VIRTUAL_DESTRUCTOR:
7264       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7265       break;
7266     case RID_IS_ABSTRACT:
7267       kind = CPTK_IS_ABSTRACT;
7268       break;
7269     case RID_IS_BASE_OF:
7270       kind = CPTK_IS_BASE_OF;
7271       binary = true;
7272       break;
7273     case RID_IS_CLASS:
7274       kind = CPTK_IS_CLASS;
7275       break;
7276     case RID_IS_CONVERTIBLE_TO:
7277       kind = CPTK_IS_CONVERTIBLE_TO;
7278       binary = true;
7279       break;
7280     case RID_IS_EMPTY:
7281       kind = CPTK_IS_EMPTY;
7282       break;
7283     case RID_IS_ENUM:
7284       kind = CPTK_IS_ENUM;
7285       break;
7286     case RID_IS_LITERAL_TYPE:
7287       kind = CPTK_IS_LITERAL_TYPE;
7288       break;
7289     case RID_IS_POD:
7290       kind = CPTK_IS_POD;
7291       break;
7292     case RID_IS_POLYMORPHIC:
7293       kind = CPTK_IS_POLYMORPHIC;
7294       break;
7295     case RID_IS_STD_LAYOUT:
7296       kind = CPTK_IS_STD_LAYOUT;
7297       break;
7298     case RID_IS_TRIVIAL:
7299       kind = CPTK_IS_TRIVIAL;
7300       break;
7301     case RID_IS_UNION:
7302       kind = CPTK_IS_UNION;
7303       break;
7304     case RID_UNDERLYING_TYPE:
7305       kind = CPTK_UNDERLYING_TYPE;
7306       break;
7307     default:
7308       gcc_unreachable ();
7309     }
7310
7311   /* Consume the token.  */
7312   cp_lexer_consume_token (parser->lexer);
7313
7314   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7315
7316   type1 = cp_parser_type_id (parser);
7317
7318   if (type1 == error_mark_node)
7319     return error_mark_node;
7320
7321   /* Build a trivial decl-specifier-seq.  */
7322   clear_decl_specs (&decl_specs);
7323   decl_specs.type = type1;
7324
7325   /* Call grokdeclarator to figure out what type this is.  */
7326   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7327                           /*initialized=*/0, /*attrlist=*/NULL);
7328
7329   if (binary)
7330     {
7331       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7332  
7333       type2 = cp_parser_type_id (parser);
7334
7335       if (type2 == error_mark_node)
7336         return error_mark_node;
7337
7338       /* Build a trivial decl-specifier-seq.  */
7339       clear_decl_specs (&decl_specs);
7340       decl_specs.type = type2;
7341
7342       /* Call grokdeclarator to figure out what type this is.  */
7343       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7344                               /*initialized=*/0, /*attrlist=*/NULL);
7345     }
7346
7347   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7348
7349   /* Complete the trait expression, which may mean either processing
7350      the trait expr now or saving it for template instantiation.  */
7351   return kind != CPTK_UNDERLYING_TYPE
7352     ? finish_trait_expr (kind, type1, type2)
7353     : finish_underlying_type (type1);
7354 }
7355
7356 /* Lambdas that appear in variable initializer or default argument scope
7357    get that in their mangling, so we need to record it.  We might as well
7358    use the count for function and namespace scopes as well.  */
7359 static GTY(()) tree lambda_scope;
7360 static GTY(()) int lambda_count;
7361 typedef struct GTY(()) tree_int
7362 {
7363   tree t;
7364   int i;
7365 } tree_int;
7366 DEF_VEC_O(tree_int);
7367 DEF_VEC_ALLOC_O(tree_int,gc);
7368 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7369
7370 static void
7371 start_lambda_scope (tree decl)
7372 {
7373   tree_int ti;
7374   gcc_assert (decl);
7375   /* Once we're inside a function, we ignore other scopes and just push
7376      the function again so that popping works properly.  */
7377   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7378     decl = current_function_decl;
7379   ti.t = lambda_scope;
7380   ti.i = lambda_count;
7381   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7382   if (lambda_scope != decl)
7383     {
7384       /* Don't reset the count if we're still in the same function.  */
7385       lambda_scope = decl;
7386       lambda_count = 0;
7387     }
7388 }
7389
7390 static void
7391 record_lambda_scope (tree lambda)
7392 {
7393   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7394   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7395 }
7396
7397 static void
7398 finish_lambda_scope (void)
7399 {
7400   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7401   if (lambda_scope != p->t)
7402     {
7403       lambda_scope = p->t;
7404       lambda_count = p->i;
7405     }
7406   VEC_pop (tree_int, lambda_scope_stack);
7407 }
7408
7409 /* Parse a lambda expression.
7410
7411    lambda-expression:
7412      lambda-introducer lambda-declarator [opt] compound-statement
7413
7414    Returns a representation of the expression.  */
7415
7416 static tree
7417 cp_parser_lambda_expression (cp_parser* parser)
7418 {
7419   tree lambda_expr = build_lambda_expr ();
7420   tree type;
7421   bool ok;
7422
7423   LAMBDA_EXPR_LOCATION (lambda_expr)
7424     = cp_lexer_peek_token (parser->lexer)->location;
7425
7426   if (cp_unevaluated_operand)
7427     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7428               "lambda-expression in unevaluated context");
7429
7430   /* We may be in the middle of deferred access check.  Disable
7431      it now.  */
7432   push_deferring_access_checks (dk_no_deferred);
7433
7434   cp_parser_lambda_introducer (parser, lambda_expr);
7435
7436   type = begin_lambda_type (lambda_expr);
7437
7438   record_lambda_scope (lambda_expr);
7439
7440   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7441   determine_visibility (TYPE_NAME (type));
7442
7443   /* Now that we've started the type, add the capture fields for any
7444      explicit captures.  */
7445   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7446
7447   {
7448     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7449     unsigned int saved_num_template_parameter_lists
7450         = parser->num_template_parameter_lists;
7451     unsigned char in_statement = parser->in_statement;
7452     bool in_switch_statement_p = parser->in_switch_statement_p;
7453
7454     parser->num_template_parameter_lists = 0;
7455     parser->in_statement = 0;
7456     parser->in_switch_statement_p = false;
7457
7458     /* By virtue of defining a local class, a lambda expression has access to
7459        the private variables of enclosing classes.  */
7460
7461     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7462
7463     if (ok)
7464       cp_parser_lambda_body (parser, lambda_expr);
7465     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7466       cp_parser_skip_to_end_of_block_or_statement (parser);
7467
7468     /* The capture list was built up in reverse order; fix that now.  */
7469     {
7470       tree newlist = NULL_TREE;
7471       tree elt, next;
7472
7473       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7474            elt; elt = next)
7475         {
7476           next = TREE_CHAIN (elt);
7477           TREE_CHAIN (elt) = newlist;
7478           newlist = elt;
7479         }
7480       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7481     }
7482
7483     if (ok)
7484       maybe_add_lambda_conv_op (type);
7485
7486     type = finish_struct (type, /*attributes=*/NULL_TREE);
7487
7488     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7489     parser->in_statement = in_statement;
7490     parser->in_switch_statement_p = in_switch_statement_p;
7491   }
7492
7493   pop_deferring_access_checks ();
7494
7495   /* This field is only used during parsing of the lambda.  */
7496   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7497
7498   /* This lambda shouldn't have any proxies left at this point.  */
7499   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7500   /* And now that we're done, push proxies for an enclosing lambda.  */
7501   insert_pending_capture_proxies ();
7502
7503   if (ok)
7504     return build_lambda_object (lambda_expr);
7505   else
7506     return error_mark_node;
7507 }
7508
7509 /* Parse the beginning of a lambda expression.
7510
7511    lambda-introducer:
7512      [ lambda-capture [opt] ]
7513
7514    LAMBDA_EXPR is the current representation of the lambda expression.  */
7515
7516 static void
7517 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7518 {
7519   /* Need commas after the first capture.  */
7520   bool first = true;
7521
7522   /* Eat the leading `['.  */
7523   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7524
7525   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7526   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7527       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7528     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7529   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7530     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7531
7532   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7533     {
7534       cp_lexer_consume_token (parser->lexer);
7535       first = false;
7536     }
7537
7538   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7539     {
7540       cp_token* capture_token;
7541       tree capture_id;
7542       tree capture_init_expr;
7543       cp_id_kind idk = CP_ID_KIND_NONE;
7544       bool explicit_init_p = false;
7545
7546       enum capture_kind_type
7547       {
7548         BY_COPY,
7549         BY_REFERENCE
7550       };
7551       enum capture_kind_type capture_kind = BY_COPY;
7552
7553       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7554         {
7555           error ("expected end of capture-list");
7556           return;
7557         }
7558
7559       if (first)
7560         first = false;
7561       else
7562         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7563
7564       /* Possibly capture `this'.  */
7565       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7566         {
7567           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7568           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7569             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7570                      "with by-copy capture default");
7571           cp_lexer_consume_token (parser->lexer);
7572           add_capture (lambda_expr,
7573                        /*id=*/this_identifier,
7574                        /*initializer=*/finish_this_expr(),
7575                        /*by_reference_p=*/false,
7576                        explicit_init_p);
7577           continue;
7578         }
7579
7580       /* Remember whether we want to capture as a reference or not.  */
7581       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7582         {
7583           capture_kind = BY_REFERENCE;
7584           cp_lexer_consume_token (parser->lexer);
7585         }
7586
7587       /* Get the identifier.  */
7588       capture_token = cp_lexer_peek_token (parser->lexer);
7589       capture_id = cp_parser_identifier (parser);
7590
7591       if (capture_id == error_mark_node)
7592         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7593            delimiters, but I modified this to stop on unnested ']' as well.  It
7594            was already changed to stop on unnested '}', so the
7595            "closing_parenthesis" name is no more misleading with my change.  */
7596         {
7597           cp_parser_skip_to_closing_parenthesis (parser,
7598                                                  /*recovering=*/true,
7599                                                  /*or_comma=*/true,
7600                                                  /*consume_paren=*/true);
7601           break;
7602         }
7603
7604       /* Find the initializer for this capture.  */
7605       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7606         {
7607           /* An explicit expression exists.  */
7608           cp_lexer_consume_token (parser->lexer);
7609           pedwarn (input_location, OPT_pedantic,
7610                    "ISO C++ does not allow initializers "
7611                    "in lambda expression capture lists");
7612           capture_init_expr = cp_parser_assignment_expression (parser,
7613                                                                /*cast_p=*/true,
7614                                                                &idk);
7615           explicit_init_p = true;
7616         }
7617       else
7618         {
7619           const char* error_msg;
7620
7621           /* Turn the identifier into an id-expression.  */
7622           capture_init_expr
7623             = cp_parser_lookup_name
7624                 (parser,
7625                  capture_id,
7626                  none_type,
7627                  /*is_template=*/false,
7628                  /*is_namespace=*/false,
7629                  /*check_dependency=*/true,
7630                  /*ambiguous_decls=*/NULL,
7631                  capture_token->location);
7632
7633           capture_init_expr
7634             = finish_id_expression
7635                 (capture_id,
7636                  capture_init_expr,
7637                  parser->scope,
7638                  &idk,
7639                  /*integral_constant_expression_p=*/false,
7640                  /*allow_non_integral_constant_expression_p=*/false,
7641                  /*non_integral_constant_expression_p=*/NULL,
7642                  /*template_p=*/false,
7643                  /*done=*/true,
7644                  /*address_p=*/false,
7645                  /*template_arg_p=*/false,
7646                  &error_msg,
7647                  capture_token->location);
7648         }
7649
7650       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7651         capture_init_expr
7652           = unqualified_name_lookup_error (capture_init_expr);
7653
7654       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7655           && !explicit_init_p)
7656         {
7657           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7658               && capture_kind == BY_COPY)
7659             pedwarn (capture_token->location, 0, "explicit by-copy capture "
7660                      "of %qD redundant with by-copy capture default",
7661                      capture_id);
7662           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7663               && capture_kind == BY_REFERENCE)
7664             pedwarn (capture_token->location, 0, "explicit by-reference "
7665                      "capture of %qD redundant with by-reference capture "
7666                      "default", capture_id);
7667         }
7668
7669       add_capture (lambda_expr,
7670                    capture_id,
7671                    capture_init_expr,
7672                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7673                    explicit_init_p);
7674     }
7675
7676   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7677 }
7678
7679 /* Parse the (optional) middle of a lambda expression.
7680
7681    lambda-declarator:
7682      ( parameter-declaration-clause [opt] )
7683        attribute-specifier [opt]
7684        mutable [opt]
7685        exception-specification [opt]
7686        lambda-return-type-clause [opt]
7687
7688    LAMBDA_EXPR is the current representation of the lambda expression.  */
7689
7690 static bool
7691 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7692 {
7693   /* 5.1.1.4 of the standard says:
7694        If a lambda-expression does not include a lambda-declarator, it is as if
7695        the lambda-declarator were ().
7696      This means an empty parameter list, no attributes, and no exception
7697      specification.  */
7698   tree param_list = void_list_node;
7699   tree attributes = NULL_TREE;
7700   tree exception_spec = NULL_TREE;
7701   tree t;
7702
7703   /* The lambda-declarator is optional, but must begin with an opening
7704      parenthesis if present.  */
7705   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7706     {
7707       cp_lexer_consume_token (parser->lexer);
7708
7709       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7710
7711       /* Parse parameters.  */
7712       param_list = cp_parser_parameter_declaration_clause (parser);
7713
7714       /* Default arguments shall not be specified in the
7715          parameter-declaration-clause of a lambda-declarator.  */
7716       for (t = param_list; t; t = TREE_CHAIN (t))
7717         if (TREE_PURPOSE (t))
7718           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7719                    "default argument specified for lambda parameter");
7720
7721       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7722
7723       attributes = cp_parser_attributes_opt (parser);
7724
7725       /* Parse optional `mutable' keyword.  */
7726       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7727         {
7728           cp_lexer_consume_token (parser->lexer);
7729           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7730         }
7731
7732       /* Parse optional exception specification.  */
7733       exception_spec = cp_parser_exception_specification_opt (parser);
7734
7735       /* Parse optional trailing return type.  */
7736       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7737         {
7738           cp_lexer_consume_token (parser->lexer);
7739           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7740         }
7741
7742       /* The function parameters must be in scope all the way until after the
7743          trailing-return-type in case of decltype.  */
7744       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7745         pop_binding (DECL_NAME (t), t);
7746
7747       leave_scope ();
7748     }
7749
7750   /* Create the function call operator.
7751
7752      Messing with declarators like this is no uglier than building up the
7753      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7754      other code.  */
7755   {
7756     cp_decl_specifier_seq return_type_specs;
7757     cp_declarator* declarator;
7758     tree fco;
7759     int quals;
7760     void *p;
7761
7762     clear_decl_specs (&return_type_specs);
7763     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7764       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7765     else
7766       /* Maybe we will deduce the return type later, but we can use void
7767          as a placeholder return type anyways.  */
7768       return_type_specs.type = void_type_node;
7769
7770     p = obstack_alloc (&declarator_obstack, 0);
7771
7772     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7773                                      sfk_none);
7774
7775     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7776              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7777     declarator = make_call_declarator (declarator, param_list, quals,
7778                                        VIRT_SPEC_UNSPECIFIED,
7779                                        exception_spec,
7780                                        /*late_return_type=*/NULL_TREE);
7781     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7782
7783     fco = grokmethod (&return_type_specs,
7784                       declarator,
7785                       attributes);
7786     if (fco != error_mark_node)
7787       {
7788         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7789         DECL_ARTIFICIAL (fco) = 1;
7790         /* Give the object parameter a different name.  */
7791         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7792       }
7793
7794     finish_member_declaration (fco);
7795
7796     obstack_free (&declarator_obstack, p);
7797
7798     return (fco != error_mark_node);
7799   }
7800 }
7801
7802 /* Parse the body of a lambda expression, which is simply
7803
7804    compound-statement
7805
7806    but which requires special handling.
7807    LAMBDA_EXPR is the current representation of the lambda expression.  */
7808
7809 static void
7810 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7811 {
7812   bool nested = (current_function_decl != NULL_TREE);
7813   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
7814   if (nested)
7815     push_function_context ();
7816   else
7817     /* Still increment function_depth so that we don't GC in the
7818        middle of an expression.  */
7819     ++function_depth;
7820   /* Clear this in case we're in the middle of a default argument.  */
7821   parser->local_variables_forbidden_p = false;
7822
7823   /* Finish the function call operator
7824      - class_specifier
7825      + late_parsing_for_member
7826      + function_definition_after_declarator
7827      + ctor_initializer_opt_and_function_body  */
7828   {
7829     tree fco = lambda_function (lambda_expr);
7830     tree body;
7831     bool done = false;
7832     tree compound_stmt;
7833     tree cap;
7834
7835     /* Let the front end know that we are going to be defining this
7836        function.  */
7837     start_preparsed_function (fco,
7838                               NULL_TREE,
7839                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7840
7841     start_lambda_scope (fco);
7842     body = begin_function_body ();
7843
7844     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7845       goto out;
7846
7847     /* Push the proxies for any explicit captures.  */
7848     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7849          cap = TREE_CHAIN (cap))
7850       build_capture_proxy (TREE_PURPOSE (cap));
7851
7852     compound_stmt = begin_compound_stmt (0);
7853
7854     /* 5.1.1.4 of the standard says:
7855          If a lambda-expression does not include a trailing-return-type, it
7856          is as if the trailing-return-type denotes the following type:
7857           * if the compound-statement is of the form
7858                { return attribute-specifier [opt] expression ; }
7859              the type of the returned expression after lvalue-to-rvalue
7860              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7861              (_conv.array_ 4.2), and function-to-pointer conversion
7862              (_conv.func_ 4.3);
7863           * otherwise, void.  */
7864
7865     /* In a lambda that has neither a lambda-return-type-clause
7866        nor a deducible form, errors should be reported for return statements
7867        in the body.  Since we used void as the placeholder return type, parsing
7868        the body as usual will give such desired behavior.  */
7869     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7870         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7871         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7872       {
7873         tree expr = NULL_TREE;
7874         cp_id_kind idk = CP_ID_KIND_NONE;
7875
7876         /* Parse tentatively in case there's more after the initial return
7877            statement.  */
7878         cp_parser_parse_tentatively (parser);
7879
7880         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7881
7882         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7883
7884         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7885         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7886
7887         if (cp_parser_parse_definitely (parser))
7888           {
7889             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7890
7891             /* Will get error here if type not deduced yet.  */
7892             finish_return_stmt (expr);
7893
7894             done = true;
7895           }
7896       }
7897
7898     if (!done)
7899       {
7900         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7901           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7902         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7903           cp_parser_label_declaration (parser);
7904         cp_parser_statement_seq_opt (parser, NULL_TREE);
7905         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7906         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7907       }
7908
7909     finish_compound_stmt (compound_stmt);
7910
7911   out:
7912     finish_function_body (body);
7913     finish_lambda_scope ();
7914
7915     /* Finish the function and generate code for it if necessary.  */
7916     expand_or_defer_fn (finish_function (/*inline*/2));
7917   }
7918
7919   parser->local_variables_forbidden_p = local_variables_forbidden_p;
7920   if (nested)
7921     pop_function_context();
7922   else
7923     --function_depth;
7924 }
7925
7926 /* Statements [gram.stmt.stmt]  */
7927
7928 /* Parse a statement.
7929
7930    statement:
7931      labeled-statement
7932      expression-statement
7933      compound-statement
7934      selection-statement
7935      iteration-statement
7936      jump-statement
7937      declaration-statement
7938      try-block
7939
7940   IN_COMPOUND is true when the statement is nested inside a
7941   cp_parser_compound_statement; this matters for certain pragmas.
7942
7943   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7944   is a (possibly labeled) if statement which is not enclosed in braces
7945   and has an else clause.  This is used to implement -Wparentheses.  */
7946
7947 static void
7948 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7949                      bool in_compound, bool *if_p)
7950 {
7951   tree statement;
7952   cp_token *token;
7953   location_t statement_location;
7954
7955  restart:
7956   if (if_p != NULL)
7957     *if_p = false;
7958   /* There is no statement yet.  */
7959   statement = NULL_TREE;
7960   /* Peek at the next token.  */
7961   token = cp_lexer_peek_token (parser->lexer);
7962   /* Remember the location of the first token in the statement.  */
7963   statement_location = token->location;
7964   /* If this is a keyword, then that will often determine what kind of
7965      statement we have.  */
7966   if (token->type == CPP_KEYWORD)
7967     {
7968       enum rid keyword = token->keyword;
7969
7970       switch (keyword)
7971         {
7972         case RID_CASE:
7973         case RID_DEFAULT:
7974           /* Looks like a labeled-statement with a case label.
7975              Parse the label, and then use tail recursion to parse
7976              the statement.  */
7977           cp_parser_label_for_labeled_statement (parser);
7978           goto restart;
7979
7980         case RID_IF:
7981         case RID_SWITCH:
7982           statement = cp_parser_selection_statement (parser, if_p);
7983           break;
7984
7985         case RID_WHILE:
7986         case RID_DO:
7987         case RID_FOR:
7988           statement = cp_parser_iteration_statement (parser);
7989           break;
7990
7991         case RID_BREAK:
7992         case RID_CONTINUE:
7993         case RID_RETURN:
7994         case RID_GOTO:
7995           statement = cp_parser_jump_statement (parser);
7996           break;
7997
7998           /* Objective-C++ exception-handling constructs.  */
7999         case RID_AT_TRY:
8000         case RID_AT_CATCH:
8001         case RID_AT_FINALLY:
8002         case RID_AT_SYNCHRONIZED:
8003         case RID_AT_THROW:
8004           statement = cp_parser_objc_statement (parser);
8005           break;
8006
8007         case RID_TRY:
8008           statement = cp_parser_try_block (parser);
8009           break;
8010
8011         case RID_NAMESPACE:
8012           /* This must be a namespace alias definition.  */
8013           cp_parser_declaration_statement (parser);
8014           return;
8015           
8016         default:
8017           /* It might be a keyword like `int' that can start a
8018              declaration-statement.  */
8019           break;
8020         }
8021     }
8022   else if (token->type == CPP_NAME)
8023     {
8024       /* If the next token is a `:', then we are looking at a
8025          labeled-statement.  */
8026       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8027       if (token->type == CPP_COLON)
8028         {
8029           /* Looks like a labeled-statement with an ordinary label.
8030              Parse the label, and then use tail recursion to parse
8031              the statement.  */
8032           cp_parser_label_for_labeled_statement (parser);
8033           goto restart;
8034         }
8035     }
8036   /* Anything that starts with a `{' must be a compound-statement.  */
8037   else if (token->type == CPP_OPEN_BRACE)
8038     statement = cp_parser_compound_statement (parser, NULL, false, false);
8039   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8040      a statement all its own.  */
8041   else if (token->type == CPP_PRAGMA)
8042     {
8043       /* Only certain OpenMP pragmas are attached to statements, and thus
8044          are considered statements themselves.  All others are not.  In
8045          the context of a compound, accept the pragma as a "statement" and
8046          return so that we can check for a close brace.  Otherwise we
8047          require a real statement and must go back and read one.  */
8048       if (in_compound)
8049         cp_parser_pragma (parser, pragma_compound);
8050       else if (!cp_parser_pragma (parser, pragma_stmt))
8051         goto restart;
8052       return;
8053     }
8054   else if (token->type == CPP_EOF)
8055     {
8056       cp_parser_error (parser, "expected statement");
8057       return;
8058     }
8059
8060   /* Everything else must be a declaration-statement or an
8061      expression-statement.  Try for the declaration-statement
8062      first, unless we are looking at a `;', in which case we know that
8063      we have an expression-statement.  */
8064   if (!statement)
8065     {
8066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8067         {
8068           cp_parser_parse_tentatively (parser);
8069           /* Try to parse the declaration-statement.  */
8070           cp_parser_declaration_statement (parser);
8071           /* If that worked, we're done.  */
8072           if (cp_parser_parse_definitely (parser))
8073             return;
8074         }
8075       /* Look for an expression-statement instead.  */
8076       statement = cp_parser_expression_statement (parser, in_statement_expr);
8077     }
8078
8079   /* Set the line number for the statement.  */
8080   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8081     SET_EXPR_LOCATION (statement, statement_location);
8082 }
8083
8084 /* Parse the label for a labeled-statement, i.e.
8085
8086    identifier :
8087    case constant-expression :
8088    default :
8089
8090    GNU Extension:
8091    case constant-expression ... constant-expression : statement
8092
8093    When a label is parsed without errors, the label is added to the
8094    parse tree by the finish_* functions, so this function doesn't
8095    have to return the label.  */
8096
8097 static void
8098 cp_parser_label_for_labeled_statement (cp_parser* parser)
8099 {
8100   cp_token *token;
8101   tree label = NULL_TREE;
8102   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8103
8104   /* The next token should be an identifier.  */
8105   token = cp_lexer_peek_token (parser->lexer);
8106   if (token->type != CPP_NAME
8107       && token->type != CPP_KEYWORD)
8108     {
8109       cp_parser_error (parser, "expected labeled-statement");
8110       return;
8111     }
8112
8113   parser->colon_corrects_to_scope_p = false;
8114   switch (token->keyword)
8115     {
8116     case RID_CASE:
8117       {
8118         tree expr, expr_hi;
8119         cp_token *ellipsis;
8120
8121         /* Consume the `case' token.  */
8122         cp_lexer_consume_token (parser->lexer);
8123         /* Parse the constant-expression.  */
8124         expr = cp_parser_constant_expression (parser,
8125                                               /*allow_non_constant_p=*/false,
8126                                               NULL);
8127
8128         ellipsis = cp_lexer_peek_token (parser->lexer);
8129         if (ellipsis->type == CPP_ELLIPSIS)
8130           {
8131             /* Consume the `...' token.  */
8132             cp_lexer_consume_token (parser->lexer);
8133             expr_hi =
8134               cp_parser_constant_expression (parser,
8135                                              /*allow_non_constant_p=*/false,
8136                                              NULL);
8137             /* We don't need to emit warnings here, as the common code
8138                will do this for us.  */
8139           }
8140         else
8141           expr_hi = NULL_TREE;
8142
8143         if (parser->in_switch_statement_p)
8144           finish_case_label (token->location, expr, expr_hi);
8145         else
8146           error_at (token->location,
8147                     "case label %qE not within a switch statement",
8148                     expr);
8149       }
8150       break;
8151
8152     case RID_DEFAULT:
8153       /* Consume the `default' token.  */
8154       cp_lexer_consume_token (parser->lexer);
8155
8156       if (parser->in_switch_statement_p)
8157         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8158       else
8159         error_at (token->location, "case label not within a switch statement");
8160       break;
8161
8162     default:
8163       /* Anything else must be an ordinary label.  */
8164       label = finish_label_stmt (cp_parser_identifier (parser));
8165       break;
8166     }
8167
8168   /* Require the `:' token.  */
8169   cp_parser_require (parser, CPP_COLON, RT_COLON);
8170
8171   /* An ordinary label may optionally be followed by attributes.
8172      However, this is only permitted if the attributes are then
8173      followed by a semicolon.  This is because, for backward
8174      compatibility, when parsing
8175        lab: __attribute__ ((unused)) int i;
8176      we want the attribute to attach to "i", not "lab".  */
8177   if (label != NULL_TREE
8178       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8179     {
8180       tree attrs;
8181
8182       cp_parser_parse_tentatively (parser);
8183       attrs = cp_parser_attributes_opt (parser);
8184       if (attrs == NULL_TREE
8185           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8186         cp_parser_abort_tentative_parse (parser);
8187       else if (!cp_parser_parse_definitely (parser))
8188         ;
8189       else
8190         cplus_decl_attributes (&label, attrs, 0);
8191     }
8192
8193   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8194 }
8195
8196 /* Parse an expression-statement.
8197
8198    expression-statement:
8199      expression [opt] ;
8200
8201    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8202    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8203    indicates whether this expression-statement is part of an
8204    expression statement.  */
8205
8206 static tree
8207 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8208 {
8209   tree statement = NULL_TREE;
8210   cp_token *token = cp_lexer_peek_token (parser->lexer);
8211
8212   /* If the next token is a ';', then there is no expression
8213      statement.  */
8214   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8215     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8216
8217   /* Give a helpful message for "A<T>::type t;" and the like.  */
8218   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8219       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8220     {
8221       if (TREE_CODE (statement) == SCOPE_REF)
8222         error_at (token->location, "need %<typename%> before %qE because "
8223                   "%qT is a dependent scope",
8224                   statement, TREE_OPERAND (statement, 0));
8225       else if (is_overloaded_fn (statement)
8226                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8227         {
8228           /* A::A a; */
8229           tree fn = get_first_fn (statement);
8230           error_at (token->location,
8231                     "%<%T::%D%> names the constructor, not the type",
8232                     DECL_CONTEXT (fn), DECL_NAME (fn));
8233         }
8234     }
8235
8236   /* Consume the final `;'.  */
8237   cp_parser_consume_semicolon_at_end_of_statement (parser);
8238
8239   if (in_statement_expr
8240       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8241     /* This is the final expression statement of a statement
8242        expression.  */
8243     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8244   else if (statement)
8245     statement = finish_expr_stmt (statement);
8246   else
8247     finish_stmt ();
8248
8249   return statement;
8250 }
8251
8252 /* Parse a compound-statement.
8253
8254    compound-statement:
8255      { statement-seq [opt] }
8256
8257    GNU extension:
8258
8259    compound-statement:
8260      { label-declaration-seq [opt] statement-seq [opt] }
8261
8262    label-declaration-seq:
8263      label-declaration
8264      label-declaration-seq label-declaration
8265
8266    Returns a tree representing the statement.  */
8267
8268 static tree
8269 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8270                               bool in_try, bool function_body)
8271 {
8272   tree compound_stmt;
8273
8274   /* Consume the `{'.  */
8275   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8276     return error_mark_node;
8277   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8278       && !function_body)
8279     pedwarn (input_location, OPT_pedantic,
8280              "compound-statement in constexpr function");
8281   /* Begin the compound-statement.  */
8282   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8283   /* If the next keyword is `__label__' we have a label declaration.  */
8284   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8285     cp_parser_label_declaration (parser);
8286   /* Parse an (optional) statement-seq.  */
8287   cp_parser_statement_seq_opt (parser, in_statement_expr);
8288   /* Finish the compound-statement.  */
8289   finish_compound_stmt (compound_stmt);
8290   /* Consume the `}'.  */
8291   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8292
8293   return compound_stmt;
8294 }
8295
8296 /* Parse an (optional) statement-seq.
8297
8298    statement-seq:
8299      statement
8300      statement-seq [opt] statement  */
8301
8302 static void
8303 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8304 {
8305   /* Scan statements until there aren't any more.  */
8306   while (true)
8307     {
8308       cp_token *token = cp_lexer_peek_token (parser->lexer);
8309
8310       /* If we are looking at a `}', then we have run out of
8311          statements; the same is true if we have reached the end
8312          of file, or have stumbled upon a stray '@end'.  */
8313       if (token->type == CPP_CLOSE_BRACE
8314           || token->type == CPP_EOF
8315           || token->type == CPP_PRAGMA_EOL
8316           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8317         break;
8318       
8319       /* If we are in a compound statement and find 'else' then
8320          something went wrong.  */
8321       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8322         {
8323           if (parser->in_statement & IN_IF_STMT) 
8324             break;
8325           else
8326             {
8327               token = cp_lexer_consume_token (parser->lexer);
8328               error_at (token->location, "%<else%> without a previous %<if%>");
8329             }
8330         }
8331
8332       /* Parse the statement.  */
8333       cp_parser_statement (parser, in_statement_expr, true, NULL);
8334     }
8335 }
8336
8337 /* Parse a selection-statement.
8338
8339    selection-statement:
8340      if ( condition ) statement
8341      if ( condition ) statement else statement
8342      switch ( condition ) statement
8343
8344    Returns the new IF_STMT or SWITCH_STMT.
8345
8346    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8347    is a (possibly labeled) if statement which is not enclosed in
8348    braces and has an else clause.  This is used to implement
8349    -Wparentheses.  */
8350
8351 static tree
8352 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8353 {
8354   cp_token *token;
8355   enum rid keyword;
8356
8357   if (if_p != NULL)
8358     *if_p = false;
8359
8360   /* Peek at the next token.  */
8361   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8362
8363   /* See what kind of keyword it is.  */
8364   keyword = token->keyword;
8365   switch (keyword)
8366     {
8367     case RID_IF:
8368     case RID_SWITCH:
8369       {
8370         tree statement;
8371         tree condition;
8372
8373         /* Look for the `('.  */
8374         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8375           {
8376             cp_parser_skip_to_end_of_statement (parser);
8377             return error_mark_node;
8378           }
8379
8380         /* Begin the selection-statement.  */
8381         if (keyword == RID_IF)
8382           statement = begin_if_stmt ();
8383         else
8384           statement = begin_switch_stmt ();
8385
8386         /* Parse the condition.  */
8387         condition = cp_parser_condition (parser);
8388         /* Look for the `)'.  */
8389         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8390           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8391                                                  /*consume_paren=*/true);
8392
8393         if (keyword == RID_IF)
8394           {
8395             bool nested_if;
8396             unsigned char in_statement;
8397
8398             /* Add the condition.  */
8399             finish_if_stmt_cond (condition, statement);
8400
8401             /* Parse the then-clause.  */
8402             in_statement = parser->in_statement;
8403             parser->in_statement |= IN_IF_STMT;
8404             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8405               {
8406                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8407                 add_stmt (build_empty_stmt (loc));
8408                 cp_lexer_consume_token (parser->lexer);
8409                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8410                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8411                               "empty body in an %<if%> statement");
8412                 nested_if = false;
8413               }
8414             else
8415               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8416             parser->in_statement = in_statement;
8417
8418             finish_then_clause (statement);
8419
8420             /* If the next token is `else', parse the else-clause.  */
8421             if (cp_lexer_next_token_is_keyword (parser->lexer,
8422                                                 RID_ELSE))
8423               {
8424                 /* Consume the `else' keyword.  */
8425                 cp_lexer_consume_token (parser->lexer);
8426                 begin_else_clause (statement);
8427                 /* Parse the else-clause.  */
8428                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8429                   {
8430                     location_t loc;
8431                     loc = cp_lexer_peek_token (parser->lexer)->location;
8432                     warning_at (loc,
8433                                 OPT_Wempty_body, "suggest braces around "
8434                                 "empty body in an %<else%> statement");
8435                     add_stmt (build_empty_stmt (loc));
8436                     cp_lexer_consume_token (parser->lexer);
8437                   }
8438                 else
8439                   cp_parser_implicitly_scoped_statement (parser, NULL);
8440
8441                 finish_else_clause (statement);
8442
8443                 /* If we are currently parsing a then-clause, then
8444                    IF_P will not be NULL.  We set it to true to
8445                    indicate that this if statement has an else clause.
8446                    This may trigger the Wparentheses warning below
8447                    when we get back up to the parent if statement.  */
8448                 if (if_p != NULL)
8449                   *if_p = true;
8450               }
8451             else
8452               {
8453                 /* This if statement does not have an else clause.  If
8454                    NESTED_IF is true, then the then-clause is an if
8455                    statement which does have an else clause.  We warn
8456                    about the potential ambiguity.  */
8457                 if (nested_if)
8458                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8459                               "suggest explicit braces to avoid ambiguous"
8460                               " %<else%>");
8461               }
8462
8463             /* Now we're all done with the if-statement.  */
8464             finish_if_stmt (statement);
8465           }
8466         else
8467           {
8468             bool in_switch_statement_p;
8469             unsigned char in_statement;
8470
8471             /* Add the condition.  */
8472             finish_switch_cond (condition, statement);
8473
8474             /* Parse the body of the switch-statement.  */
8475             in_switch_statement_p = parser->in_switch_statement_p;
8476             in_statement = parser->in_statement;
8477             parser->in_switch_statement_p = true;
8478             parser->in_statement |= IN_SWITCH_STMT;
8479             cp_parser_implicitly_scoped_statement (parser, NULL);
8480             parser->in_switch_statement_p = in_switch_statement_p;
8481             parser->in_statement = in_statement;
8482
8483             /* Now we're all done with the switch-statement.  */
8484             finish_switch_stmt (statement);
8485           }
8486
8487         return statement;
8488       }
8489       break;
8490
8491     default:
8492       cp_parser_error (parser, "expected selection-statement");
8493       return error_mark_node;
8494     }
8495 }
8496
8497 /* Parse a condition.
8498
8499    condition:
8500      expression
8501      type-specifier-seq declarator = initializer-clause
8502      type-specifier-seq declarator braced-init-list
8503
8504    GNU Extension:
8505
8506    condition:
8507      type-specifier-seq declarator asm-specification [opt]
8508        attributes [opt] = assignment-expression
8509
8510    Returns the expression that should be tested.  */
8511
8512 static tree
8513 cp_parser_condition (cp_parser* parser)
8514 {
8515   cp_decl_specifier_seq type_specifiers;
8516   const char *saved_message;
8517   int declares_class_or_enum;
8518
8519   /* Try the declaration first.  */
8520   cp_parser_parse_tentatively (parser);
8521   /* New types are not allowed in the type-specifier-seq for a
8522      condition.  */
8523   saved_message = parser->type_definition_forbidden_message;
8524   parser->type_definition_forbidden_message
8525     = G_("types may not be defined in conditions");
8526   /* Parse the type-specifier-seq.  */
8527   cp_parser_decl_specifier_seq (parser,
8528                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8529                                 &type_specifiers,
8530                                 &declares_class_or_enum);
8531   /* Restore the saved message.  */
8532   parser->type_definition_forbidden_message = saved_message;
8533   /* If all is well, we might be looking at a declaration.  */
8534   if (!cp_parser_error_occurred (parser))
8535     {
8536       tree decl;
8537       tree asm_specification;
8538       tree attributes;
8539       cp_declarator *declarator;
8540       tree initializer = NULL_TREE;
8541
8542       /* Parse the declarator.  */
8543       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8544                                          /*ctor_dtor_or_conv_p=*/NULL,
8545                                          /*parenthesized_p=*/NULL,
8546                                          /*member_p=*/false);
8547       /* Parse the attributes.  */
8548       attributes = cp_parser_attributes_opt (parser);
8549       /* Parse the asm-specification.  */
8550       asm_specification = cp_parser_asm_specification_opt (parser);
8551       /* If the next token is not an `=' or '{', then we might still be
8552          looking at an expression.  For example:
8553
8554            if (A(a).x)
8555
8556          looks like a decl-specifier-seq and a declarator -- but then
8557          there is no `=', so this is an expression.  */
8558       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8559           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8560         cp_parser_simulate_error (parser);
8561         
8562       /* If we did see an `=' or '{', then we are looking at a declaration
8563          for sure.  */
8564       if (cp_parser_parse_definitely (parser))
8565         {
8566           tree pushed_scope;
8567           bool non_constant_p;
8568           bool flags = LOOKUP_ONLYCONVERTING;
8569
8570           /* Create the declaration.  */
8571           decl = start_decl (declarator, &type_specifiers,
8572                              /*initialized_p=*/true,
8573                              attributes, /*prefix_attributes=*/NULL_TREE,
8574                              &pushed_scope);
8575
8576           /* Parse the initializer.  */
8577           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8578             {
8579               initializer = cp_parser_braced_list (parser, &non_constant_p);
8580               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8581               flags = 0;
8582             }
8583           else
8584             {
8585               /* Consume the `='.  */
8586               cp_parser_require (parser, CPP_EQ, RT_EQ);
8587               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8588             }
8589           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8590             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8591
8592           /* Process the initializer.  */
8593           cp_finish_decl (decl,
8594                           initializer, !non_constant_p,
8595                           asm_specification,
8596                           flags);
8597
8598           if (pushed_scope)
8599             pop_scope (pushed_scope);
8600
8601           return convert_from_reference (decl);
8602         }
8603     }
8604   /* If we didn't even get past the declarator successfully, we are
8605      definitely not looking at a declaration.  */
8606   else
8607     cp_parser_abort_tentative_parse (parser);
8608
8609   /* Otherwise, we are looking at an expression.  */
8610   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8611 }
8612
8613 /* Parses a for-statement or range-for-statement until the closing ')',
8614    not included. */
8615
8616 static tree
8617 cp_parser_for (cp_parser *parser)
8618 {
8619   tree init, scope, decl;
8620   bool is_range_for;
8621
8622   /* Begin the for-statement.  */
8623   scope = begin_for_scope (&init);
8624
8625   /* Parse the initialization.  */
8626   is_range_for = cp_parser_for_init_statement (parser, &decl);
8627
8628   if (is_range_for)
8629     return cp_parser_range_for (parser, scope, init, decl);
8630   else
8631     return cp_parser_c_for (parser, scope, init);
8632 }
8633
8634 static tree
8635 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8636 {
8637   /* Normal for loop */
8638   tree condition = NULL_TREE;
8639   tree expression = NULL_TREE;
8640   tree stmt;
8641
8642   stmt = begin_for_stmt (scope, init);
8643   /* The for-init-statement has already been parsed in
8644      cp_parser_for_init_statement, so no work is needed here.  */
8645   finish_for_init_stmt (stmt);
8646
8647   /* If there's a condition, process it.  */
8648   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8649     condition = cp_parser_condition (parser);
8650   finish_for_cond (condition, stmt);
8651   /* Look for the `;'.  */
8652   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8653
8654   /* If there's an expression, process it.  */
8655   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8656     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8657   finish_for_expr (expression, stmt);
8658
8659   return stmt;
8660 }
8661
8662 /* Tries to parse a range-based for-statement:
8663
8664   range-based-for:
8665     decl-specifier-seq declarator : expression
8666
8667   The decl-specifier-seq declarator and the `:' are already parsed by
8668   cp_parser_for_init_statement. If processing_template_decl it returns a
8669   newly created RANGE_FOR_STMT; if not, it is converted to a
8670   regular FOR_STMT.  */
8671
8672 static tree
8673 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8674 {
8675   tree stmt, range_expr;
8676
8677   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8678     {
8679       bool expr_non_constant_p;
8680       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8681     }
8682   else
8683     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8684
8685   /* If in template, STMT is converted to a normal for-statement
8686      at instantiation. If not, it is done just ahead. */
8687   if (processing_template_decl)
8688     {
8689       stmt = begin_range_for_stmt (scope, init);
8690       finish_range_for_decl (stmt, range_decl, range_expr);
8691       if (!type_dependent_expression_p (range_expr))
8692         do_range_for_auto_deduction (range_decl, range_expr);
8693     }
8694   else
8695     {
8696       stmt = begin_for_stmt (scope, init);
8697       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8698     }
8699   return stmt;
8700 }
8701
8702 /* Subroutine of cp_convert_range_for: given the initializer expression,
8703    builds up the range temporary.  */
8704
8705 static tree
8706 build_range_temp (tree range_expr)
8707 {
8708   tree range_type, range_temp;
8709
8710   /* Find out the type deduced by the declaration
8711      `auto &&__range = range_expr'.  */
8712   range_type = cp_build_reference_type (make_auto (), true);
8713   range_type = do_auto_deduction (range_type, range_expr,
8714                                   type_uses_auto (range_type));
8715
8716   /* Create the __range variable.  */
8717   range_temp = build_decl (input_location, VAR_DECL,
8718                            get_identifier ("__for_range"), range_type);
8719   TREE_USED (range_temp) = 1;
8720   DECL_ARTIFICIAL (range_temp) = 1;
8721
8722   return range_temp;
8723 }
8724
8725 /* Used by cp_parser_range_for in template context: we aren't going to
8726    do a full conversion yet, but we still need to resolve auto in the
8727    type of the for-range-declaration if present.  This is basically
8728    a shortcut version of cp_convert_range_for.  */
8729
8730 static void
8731 do_range_for_auto_deduction (tree decl, tree range_expr)
8732 {
8733   tree auto_node = type_uses_auto (TREE_TYPE (decl));
8734   if (auto_node)
8735     {
8736       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
8737       range_temp = convert_from_reference (build_range_temp (range_expr));
8738       iter_type = (cp_parser_perform_range_for_lookup
8739                    (range_temp, &begin_dummy, &end_dummy));
8740       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
8741       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
8742                                         tf_warning_or_error);
8743       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
8744                                             iter_decl, auto_node);
8745     }
8746 }
8747
8748 /* Converts a range-based for-statement into a normal
8749    for-statement, as per the definition.
8750
8751       for (RANGE_DECL : RANGE_EXPR)
8752         BLOCK
8753
8754    should be equivalent to:
8755
8756       {
8757         auto &&__range = RANGE_EXPR;
8758         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8759               __begin != __end;
8760               ++__begin)
8761           {
8762               RANGE_DECL = *__begin;
8763               BLOCK
8764           }
8765       }
8766
8767    If RANGE_EXPR is an array:
8768         BEGIN_EXPR = __range
8769         END_EXPR = __range + ARRAY_SIZE(__range)
8770    Else if RANGE_EXPR has a member 'begin' or 'end':
8771         BEGIN_EXPR = __range.begin()
8772         END_EXPR = __range.end()
8773    Else:
8774         BEGIN_EXPR = begin(__range)
8775         END_EXPR = end(__range);
8776
8777    If __range has a member 'begin' but not 'end', or vice versa, we must
8778    still use the second alternative (it will surely fail, however).
8779    When calling begin()/end() in the third alternative we must use
8780    argument dependent lookup, but always considering 'std' as an associated
8781    namespace.  */
8782
8783 tree
8784 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8785 {
8786   tree begin, end;
8787   tree iter_type, begin_expr, end_expr;
8788   tree condition, expression;
8789
8790   if (range_decl == error_mark_node || range_expr == error_mark_node)
8791     /* If an error happened previously do nothing or else a lot of
8792        unhelpful errors would be issued.  */
8793     begin_expr = end_expr = iter_type = error_mark_node;
8794   else
8795     {
8796       tree range_temp = build_range_temp (range_expr);
8797       pushdecl (range_temp);
8798       cp_finish_decl (range_temp, range_expr,
8799                       /*is_constant_init*/false, NULL_TREE,
8800                       LOOKUP_ONLYCONVERTING);
8801
8802       range_temp = convert_from_reference (range_temp);
8803       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8804                                                       &begin_expr, &end_expr);
8805     }
8806
8807   /* The new for initialization statement.  */
8808   begin = build_decl (input_location, VAR_DECL,
8809                       get_identifier ("__for_begin"), iter_type);
8810   TREE_USED (begin) = 1;
8811   DECL_ARTIFICIAL (begin) = 1;
8812   pushdecl (begin);
8813   cp_finish_decl (begin, begin_expr,
8814                   /*is_constant_init*/false, NULL_TREE,
8815                   LOOKUP_ONLYCONVERTING);
8816
8817   end = build_decl (input_location, VAR_DECL,
8818                     get_identifier ("__for_end"), iter_type);
8819   TREE_USED (end) = 1;
8820   DECL_ARTIFICIAL (end) = 1;
8821   pushdecl (end);
8822   cp_finish_decl (end, end_expr,
8823                   /*is_constant_init*/false, NULL_TREE,
8824                   LOOKUP_ONLYCONVERTING);
8825
8826   finish_for_init_stmt (statement);
8827
8828   /* The new for condition.  */
8829   condition = build_x_binary_op (NE_EXPR,
8830                                  begin, ERROR_MARK,
8831                                  end, ERROR_MARK,
8832                                  NULL, tf_warning_or_error);
8833   finish_for_cond (condition, statement);
8834
8835   /* The new increment expression.  */
8836   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8837   finish_for_expr (expression, statement);
8838
8839   /* The declaration is initialized with *__begin inside the loop body.  */
8840   cp_finish_decl (range_decl,
8841                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8842                   /*is_constant_init*/false, NULL_TREE,
8843                   LOOKUP_ONLYCONVERTING);
8844
8845   return statement;
8846 }
8847
8848 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8849    We need to solve both at the same time because the method used
8850    depends on the existence of members begin or end.
8851    Returns the type deduced for the iterator expression.  */
8852
8853 static tree
8854 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8855 {
8856   if (error_operand_p (range))
8857     {
8858       *begin = *end = error_mark_node;
8859       return error_mark_node;
8860     }
8861
8862   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8863     {
8864       error ("range-based %<for%> expression of type %qT "
8865              "has incomplete type", TREE_TYPE (range));
8866       *begin = *end = error_mark_node;
8867       return error_mark_node;
8868     }
8869   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8870     {
8871       /* If RANGE is an array, we will use pointer arithmetic.  */
8872       *begin = range;
8873       *end = build_binary_op (input_location, PLUS_EXPR,
8874                               range,
8875                               array_type_nelts_top (TREE_TYPE (range)),
8876                               0);
8877       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8878     }
8879   else
8880     {
8881       /* If it is not an array, we must do a bit of magic.  */
8882       tree id_begin, id_end;
8883       tree member_begin, member_end;
8884
8885       *begin = *end = error_mark_node;
8886
8887       id_begin = get_identifier ("begin");
8888       id_end = get_identifier ("end");
8889       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8890                                     /*protect=*/2, /*want_type=*/false);
8891       member_end = lookup_member (TREE_TYPE (range), id_end,
8892                                   /*protect=*/2, /*want_type=*/false);
8893
8894       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8895         {
8896           /* Use the member functions.  */
8897           if (member_begin != NULL_TREE)
8898             *begin = cp_parser_range_for_member_function (range, id_begin);
8899           else
8900             error ("range-based %<for%> expression of type %qT has an "
8901                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8902
8903           if (member_end != NULL_TREE)
8904             *end = cp_parser_range_for_member_function (range, id_end);
8905           else
8906             error ("range-based %<for%> expression of type %qT has a "
8907                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8908         }
8909       else
8910         {
8911           /* Use global functions with ADL.  */
8912           VEC(tree,gc) *vec;
8913           vec = make_tree_vector ();
8914
8915           VEC_safe_push (tree, gc, vec, range);
8916
8917           member_begin = perform_koenig_lookup (id_begin, vec,
8918                                                 /*include_std=*/true,
8919                                                 tf_warning_or_error);
8920           *begin = finish_call_expr (member_begin, &vec, false, true,
8921                                      tf_warning_or_error);
8922           member_end = perform_koenig_lookup (id_end, vec,
8923                                               /*include_std=*/true,
8924                                               tf_warning_or_error);
8925           *end = finish_call_expr (member_end, &vec, false, true,
8926                                    tf_warning_or_error);
8927
8928           release_tree_vector (vec);
8929         }
8930
8931       /* Last common checks.  */
8932       if (*begin == error_mark_node || *end == error_mark_node)
8933         {
8934           /* If one of the expressions is an error do no more checks.  */
8935           *begin = *end = error_mark_node;
8936           return error_mark_node;
8937         }
8938       else
8939         {
8940           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8941           /* The unqualified type of the __begin and __end temporaries should
8942              be the same, as required by the multiple auto declaration.  */
8943           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8944             error ("inconsistent begin/end types in range-based %<for%> "
8945                    "statement: %qT and %qT",
8946                    TREE_TYPE (*begin), TREE_TYPE (*end));
8947           return iter_type;
8948         }
8949     }
8950 }
8951
8952 /* Helper function for cp_parser_perform_range_for_lookup.
8953    Builds a tree for RANGE.IDENTIFIER().  */
8954
8955 static tree
8956 cp_parser_range_for_member_function (tree range, tree identifier)
8957 {
8958   tree member, res;
8959   VEC(tree,gc) *vec;
8960
8961   member = finish_class_member_access_expr (range, identifier,
8962                                             false, tf_warning_or_error);
8963   if (member == error_mark_node)
8964     return error_mark_node;
8965
8966   vec = make_tree_vector ();
8967   res = finish_call_expr (member, &vec,
8968                           /*disallow_virtual=*/false,
8969                           /*koenig_p=*/false,
8970                           tf_warning_or_error);
8971   release_tree_vector (vec);
8972   return res;
8973 }
8974
8975 /* Parse an iteration-statement.
8976
8977    iteration-statement:
8978      while ( condition ) statement
8979      do statement while ( expression ) ;
8980      for ( for-init-statement condition [opt] ; expression [opt] )
8981        statement
8982
8983    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8984
8985 static tree
8986 cp_parser_iteration_statement (cp_parser* parser)
8987 {
8988   cp_token *token;
8989   enum rid keyword;
8990   tree statement;
8991   unsigned char in_statement;
8992
8993   /* Peek at the next token.  */
8994   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8995   if (!token)
8996     return error_mark_node;
8997
8998   /* Remember whether or not we are already within an iteration
8999      statement.  */
9000   in_statement = parser->in_statement;
9001
9002   /* See what kind of keyword it is.  */
9003   keyword = token->keyword;
9004   switch (keyword)
9005     {
9006     case RID_WHILE:
9007       {
9008         tree condition;
9009
9010         /* Begin the while-statement.  */
9011         statement = begin_while_stmt ();
9012         /* Look for the `('.  */
9013         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9014         /* Parse the condition.  */
9015         condition = cp_parser_condition (parser);
9016         finish_while_stmt_cond (condition, statement);
9017         /* Look for the `)'.  */
9018         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9019         /* Parse the dependent statement.  */
9020         parser->in_statement = IN_ITERATION_STMT;
9021         cp_parser_already_scoped_statement (parser);
9022         parser->in_statement = in_statement;
9023         /* We're done with the while-statement.  */
9024         finish_while_stmt (statement);
9025       }
9026       break;
9027
9028     case RID_DO:
9029       {
9030         tree expression;
9031
9032         /* Begin the do-statement.  */
9033         statement = begin_do_stmt ();
9034         /* Parse the body of the do-statement.  */
9035         parser->in_statement = IN_ITERATION_STMT;
9036         cp_parser_implicitly_scoped_statement (parser, NULL);
9037         parser->in_statement = in_statement;
9038         finish_do_body (statement);
9039         /* Look for the `while' keyword.  */
9040         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9041         /* Look for the `('.  */
9042         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9043         /* Parse the expression.  */
9044         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9045         /* We're done with the do-statement.  */
9046         finish_do_stmt (expression, statement);
9047         /* Look for the `)'.  */
9048         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9049         /* Look for the `;'.  */
9050         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9051       }
9052       break;
9053
9054     case RID_FOR:
9055       {
9056         /* Look for the `('.  */
9057         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9058
9059         statement = cp_parser_for (parser);
9060
9061         /* Look for the `)'.  */
9062         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9063
9064         /* Parse the body of the for-statement.  */
9065         parser->in_statement = IN_ITERATION_STMT;
9066         cp_parser_already_scoped_statement (parser);
9067         parser->in_statement = in_statement;
9068
9069         /* We're done with the for-statement.  */
9070         finish_for_stmt (statement);
9071       }
9072       break;
9073
9074     default:
9075       cp_parser_error (parser, "expected iteration-statement");
9076       statement = error_mark_node;
9077       break;
9078     }
9079
9080   return statement;
9081 }
9082
9083 /* Parse a for-init-statement or the declarator of a range-based-for.
9084    Returns true if a range-based-for declaration is seen.
9085
9086    for-init-statement:
9087      expression-statement
9088      simple-declaration  */
9089
9090 static bool
9091 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9092 {
9093   /* If the next token is a `;', then we have an empty
9094      expression-statement.  Grammatically, this is also a
9095      simple-declaration, but an invalid one, because it does not
9096      declare anything.  Therefore, if we did not handle this case
9097      specially, we would issue an error message about an invalid
9098      declaration.  */
9099   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9100     {
9101       bool is_range_for = false;
9102       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9103
9104       parser->colon_corrects_to_scope_p = false;
9105
9106       /* We're going to speculatively look for a declaration, falling back
9107          to an expression, if necessary.  */
9108       cp_parser_parse_tentatively (parser);
9109       /* Parse the declaration.  */
9110       cp_parser_simple_declaration (parser,
9111                                     /*function_definition_allowed_p=*/false,
9112                                     decl);
9113       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9114       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9115         {
9116           /* It is a range-for, consume the ':' */
9117           cp_lexer_consume_token (parser->lexer);
9118           is_range_for = true;
9119           if (cxx_dialect < cxx0x)
9120             {
9121               error_at (cp_lexer_peek_token (parser->lexer)->location,
9122                         "range-based %<for%> loops are not allowed "
9123                         "in C++98 mode");
9124               *decl = error_mark_node;
9125             }
9126         }
9127       else
9128           /* The ';' is not consumed yet because we told
9129              cp_parser_simple_declaration not to.  */
9130           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9131
9132       if (cp_parser_parse_definitely (parser))
9133         return is_range_for;
9134       /* If the tentative parse failed, then we shall need to look for an
9135          expression-statement.  */
9136     }
9137   /* If we are here, it is an expression-statement.  */
9138   cp_parser_expression_statement (parser, NULL_TREE);
9139   return false;
9140 }
9141
9142 /* Parse a jump-statement.
9143
9144    jump-statement:
9145      break ;
9146      continue ;
9147      return expression [opt] ;
9148      return braced-init-list ;
9149      goto identifier ;
9150
9151    GNU extension:
9152
9153    jump-statement:
9154      goto * expression ;
9155
9156    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9157
9158 static tree
9159 cp_parser_jump_statement (cp_parser* parser)
9160 {
9161   tree statement = error_mark_node;
9162   cp_token *token;
9163   enum rid keyword;
9164   unsigned char in_statement;
9165
9166   /* Peek at the next token.  */
9167   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9168   if (!token)
9169     return error_mark_node;
9170
9171   /* See what kind of keyword it is.  */
9172   keyword = token->keyword;
9173   switch (keyword)
9174     {
9175     case RID_BREAK:
9176       in_statement = parser->in_statement & ~IN_IF_STMT;      
9177       switch (in_statement)
9178         {
9179         case 0:
9180           error_at (token->location, "break statement not within loop or switch");
9181           break;
9182         default:
9183           gcc_assert ((in_statement & IN_SWITCH_STMT)
9184                       || in_statement == IN_ITERATION_STMT);
9185           statement = finish_break_stmt ();
9186           break;
9187         case IN_OMP_BLOCK:
9188           error_at (token->location, "invalid exit from OpenMP structured block");
9189           break;
9190         case IN_OMP_FOR:
9191           error_at (token->location, "break statement used with OpenMP for loop");
9192           break;
9193         }
9194       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9195       break;
9196
9197     case RID_CONTINUE:
9198       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9199         {
9200         case 0:
9201           error_at (token->location, "continue statement not within a loop");
9202           break;
9203         case IN_ITERATION_STMT:
9204         case IN_OMP_FOR:
9205           statement = finish_continue_stmt ();
9206           break;
9207         case IN_OMP_BLOCK:
9208           error_at (token->location, "invalid exit from OpenMP structured block");
9209           break;
9210         default:
9211           gcc_unreachable ();
9212         }
9213       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9214       break;
9215
9216     case RID_RETURN:
9217       {
9218         tree expr;
9219         bool expr_non_constant_p;
9220
9221         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9222           {
9223             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9224             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9225           }
9226         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9227           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9228         else
9229           /* If the next token is a `;', then there is no
9230              expression.  */
9231           expr = NULL_TREE;
9232         /* Build the return-statement.  */
9233         statement = finish_return_stmt (expr);
9234         /* Look for the final `;'.  */
9235         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9236       }
9237       break;
9238
9239     case RID_GOTO:
9240       /* Create the goto-statement.  */
9241       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9242         {
9243           /* Issue a warning about this use of a GNU extension.  */
9244           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9245           /* Consume the '*' token.  */
9246           cp_lexer_consume_token (parser->lexer);
9247           /* Parse the dependent expression.  */
9248           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9249         }
9250       else
9251         finish_goto_stmt (cp_parser_identifier (parser));
9252       /* Look for the final `;'.  */
9253       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9254       break;
9255
9256     default:
9257       cp_parser_error (parser, "expected jump-statement");
9258       break;
9259     }
9260
9261   return statement;
9262 }
9263
9264 /* Parse a declaration-statement.
9265
9266    declaration-statement:
9267      block-declaration  */
9268
9269 static void
9270 cp_parser_declaration_statement (cp_parser* parser)
9271 {
9272   void *p;
9273
9274   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9275   p = obstack_alloc (&declarator_obstack, 0);
9276
9277  /* Parse the block-declaration.  */
9278   cp_parser_block_declaration (parser, /*statement_p=*/true);
9279
9280   /* Free any declarators allocated.  */
9281   obstack_free (&declarator_obstack, p);
9282
9283   /* Finish off the statement.  */
9284   finish_stmt ();
9285 }
9286
9287 /* Some dependent statements (like `if (cond) statement'), are
9288    implicitly in their own scope.  In other words, if the statement is
9289    a single statement (as opposed to a compound-statement), it is
9290    none-the-less treated as if it were enclosed in braces.  Any
9291    declarations appearing in the dependent statement are out of scope
9292    after control passes that point.  This function parses a statement,
9293    but ensures that is in its own scope, even if it is not a
9294    compound-statement.
9295
9296    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9297    is a (possibly labeled) if statement which is not enclosed in
9298    braces and has an else clause.  This is used to implement
9299    -Wparentheses.
9300
9301    Returns the new statement.  */
9302
9303 static tree
9304 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9305 {
9306   tree statement;
9307
9308   if (if_p != NULL)
9309     *if_p = false;
9310
9311   /* Mark if () ; with a special NOP_EXPR.  */
9312   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9313     {
9314       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9315       cp_lexer_consume_token (parser->lexer);
9316       statement = add_stmt (build_empty_stmt (loc));
9317     }
9318   /* if a compound is opened, we simply parse the statement directly.  */
9319   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9320     statement = cp_parser_compound_statement (parser, NULL, false, false);
9321   /* If the token is not a `{', then we must take special action.  */
9322   else
9323     {
9324       /* Create a compound-statement.  */
9325       statement = begin_compound_stmt (0);
9326       /* Parse the dependent-statement.  */
9327       cp_parser_statement (parser, NULL_TREE, false, if_p);
9328       /* Finish the dummy compound-statement.  */
9329       finish_compound_stmt (statement);
9330     }
9331
9332   /* Return the statement.  */
9333   return statement;
9334 }
9335
9336 /* For some dependent statements (like `while (cond) statement'), we
9337    have already created a scope.  Therefore, even if the dependent
9338    statement is a compound-statement, we do not want to create another
9339    scope.  */
9340
9341 static void
9342 cp_parser_already_scoped_statement (cp_parser* parser)
9343 {
9344   /* If the token is a `{', then we must take special action.  */
9345   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9346     cp_parser_statement (parser, NULL_TREE, false, NULL);
9347   else
9348     {
9349       /* Avoid calling cp_parser_compound_statement, so that we
9350          don't create a new scope.  Do everything else by hand.  */
9351       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9352       /* If the next keyword is `__label__' we have a label declaration.  */
9353       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9354         cp_parser_label_declaration (parser);
9355       /* Parse an (optional) statement-seq.  */
9356       cp_parser_statement_seq_opt (parser, NULL_TREE);
9357       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9358     }
9359 }
9360
9361 /* Declarations [gram.dcl.dcl] */
9362
9363 /* Parse an optional declaration-sequence.
9364
9365    declaration-seq:
9366      declaration
9367      declaration-seq declaration  */
9368
9369 static void
9370 cp_parser_declaration_seq_opt (cp_parser* parser)
9371 {
9372   while (true)
9373     {
9374       cp_token *token;
9375
9376       token = cp_lexer_peek_token (parser->lexer);
9377
9378       if (token->type == CPP_CLOSE_BRACE
9379           || token->type == CPP_EOF
9380           || token->type == CPP_PRAGMA_EOL)
9381         break;
9382
9383       if (token->type == CPP_SEMICOLON)
9384         {
9385           /* A declaration consisting of a single semicolon is
9386              invalid.  Allow it unless we're being pedantic.  */
9387           cp_lexer_consume_token (parser->lexer);
9388           if (!in_system_header)
9389             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9390           continue;
9391         }
9392
9393       /* If we're entering or exiting a region that's implicitly
9394          extern "C", modify the lang context appropriately.  */
9395       if (!parser->implicit_extern_c && token->implicit_extern_c)
9396         {
9397           push_lang_context (lang_name_c);
9398           parser->implicit_extern_c = true;
9399         }
9400       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9401         {
9402           pop_lang_context ();
9403           parser->implicit_extern_c = false;
9404         }
9405
9406       if (token->type == CPP_PRAGMA)
9407         {
9408           /* A top-level declaration can consist solely of a #pragma.
9409              A nested declaration cannot, so this is done here and not
9410              in cp_parser_declaration.  (A #pragma at block scope is
9411              handled in cp_parser_statement.)  */
9412           cp_parser_pragma (parser, pragma_external);
9413           continue;
9414         }
9415
9416       /* Parse the declaration itself.  */
9417       cp_parser_declaration (parser);
9418     }
9419 }
9420
9421 /* Parse a declaration.
9422
9423    declaration:
9424      block-declaration
9425      function-definition
9426      template-declaration
9427      explicit-instantiation
9428      explicit-specialization
9429      linkage-specification
9430      namespace-definition
9431
9432    GNU extension:
9433
9434    declaration:
9435       __extension__ declaration */
9436
9437 static void
9438 cp_parser_declaration (cp_parser* parser)
9439 {
9440   cp_token token1;
9441   cp_token token2;
9442   int saved_pedantic;
9443   void *p;
9444   tree attributes = NULL_TREE;
9445
9446   /* Check for the `__extension__' keyword.  */
9447   if (cp_parser_extension_opt (parser, &saved_pedantic))
9448     {
9449       /* Parse the qualified declaration.  */
9450       cp_parser_declaration (parser);
9451       /* Restore the PEDANTIC flag.  */
9452       pedantic = saved_pedantic;
9453
9454       return;
9455     }
9456
9457   /* Try to figure out what kind of declaration is present.  */
9458   token1 = *cp_lexer_peek_token (parser->lexer);
9459
9460   if (token1.type != CPP_EOF)
9461     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9462   else
9463     {
9464       token2.type = CPP_EOF;
9465       token2.keyword = RID_MAX;
9466     }
9467
9468   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9469   p = obstack_alloc (&declarator_obstack, 0);
9470
9471   /* If the next token is `extern' and the following token is a string
9472      literal, then we have a linkage specification.  */
9473   if (token1.keyword == RID_EXTERN
9474       && cp_parser_is_string_literal (&token2))
9475     cp_parser_linkage_specification (parser);
9476   /* If the next token is `template', then we have either a template
9477      declaration, an explicit instantiation, or an explicit
9478      specialization.  */
9479   else if (token1.keyword == RID_TEMPLATE)
9480     {
9481       /* `template <>' indicates a template specialization.  */
9482       if (token2.type == CPP_LESS
9483           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9484         cp_parser_explicit_specialization (parser);
9485       /* `template <' indicates a template declaration.  */
9486       else if (token2.type == CPP_LESS)
9487         cp_parser_template_declaration (parser, /*member_p=*/false);
9488       /* Anything else must be an explicit instantiation.  */
9489       else
9490         cp_parser_explicit_instantiation (parser);
9491     }
9492   /* If the next token is `export', then we have a template
9493      declaration.  */
9494   else if (token1.keyword == RID_EXPORT)
9495     cp_parser_template_declaration (parser, /*member_p=*/false);
9496   /* If the next token is `extern', 'static' or 'inline' and the one
9497      after that is `template', we have a GNU extended explicit
9498      instantiation directive.  */
9499   else if (cp_parser_allow_gnu_extensions_p (parser)
9500            && (token1.keyword == RID_EXTERN
9501                || token1.keyword == RID_STATIC
9502                || token1.keyword == RID_INLINE)
9503            && token2.keyword == RID_TEMPLATE)
9504     cp_parser_explicit_instantiation (parser);
9505   /* If the next token is `namespace', check for a named or unnamed
9506      namespace definition.  */
9507   else if (token1.keyword == RID_NAMESPACE
9508            && (/* A named namespace definition.  */
9509                (token2.type == CPP_NAME
9510                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9511                     != CPP_EQ))
9512                /* An unnamed namespace definition.  */
9513                || token2.type == CPP_OPEN_BRACE
9514                || token2.keyword == RID_ATTRIBUTE))
9515     cp_parser_namespace_definition (parser);
9516   /* An inline (associated) namespace definition.  */
9517   else if (token1.keyword == RID_INLINE
9518            && token2.keyword == RID_NAMESPACE)
9519     cp_parser_namespace_definition (parser);
9520   /* Objective-C++ declaration/definition.  */
9521   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9522     cp_parser_objc_declaration (parser, NULL_TREE);
9523   else if (c_dialect_objc ()
9524            && token1.keyword == RID_ATTRIBUTE
9525            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9526     cp_parser_objc_declaration (parser, attributes);
9527   /* We must have either a block declaration or a function
9528      definition.  */
9529   else
9530     /* Try to parse a block-declaration, or a function-definition.  */
9531     cp_parser_block_declaration (parser, /*statement_p=*/false);
9532
9533   /* Free any declarators allocated.  */
9534   obstack_free (&declarator_obstack, p);
9535 }
9536
9537 /* Parse a block-declaration.
9538
9539    block-declaration:
9540      simple-declaration
9541      asm-definition
9542      namespace-alias-definition
9543      using-declaration
9544      using-directive
9545
9546    GNU Extension:
9547
9548    block-declaration:
9549      __extension__ block-declaration
9550
9551    C++0x Extension:
9552
9553    block-declaration:
9554      static_assert-declaration
9555
9556    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9557    part of a declaration-statement.  */
9558
9559 static void
9560 cp_parser_block_declaration (cp_parser *parser,
9561                              bool      statement_p)
9562 {
9563   cp_token *token1;
9564   int saved_pedantic;
9565
9566   /* Check for the `__extension__' keyword.  */
9567   if (cp_parser_extension_opt (parser, &saved_pedantic))
9568     {
9569       /* Parse the qualified declaration.  */
9570       cp_parser_block_declaration (parser, statement_p);
9571       /* Restore the PEDANTIC flag.  */
9572       pedantic = saved_pedantic;
9573
9574       return;
9575     }
9576
9577   /* Peek at the next token to figure out which kind of declaration is
9578      present.  */
9579   token1 = cp_lexer_peek_token (parser->lexer);
9580
9581   /* If the next keyword is `asm', we have an asm-definition.  */
9582   if (token1->keyword == RID_ASM)
9583     {
9584       if (statement_p)
9585         cp_parser_commit_to_tentative_parse (parser);
9586       cp_parser_asm_definition (parser);
9587     }
9588   /* If the next keyword is `namespace', we have a
9589      namespace-alias-definition.  */
9590   else if (token1->keyword == RID_NAMESPACE)
9591     cp_parser_namespace_alias_definition (parser);
9592   /* If the next keyword is `using', we have either a
9593      using-declaration or a using-directive.  */
9594   else if (token1->keyword == RID_USING)
9595     {
9596       cp_token *token2;
9597
9598       if (statement_p)
9599         cp_parser_commit_to_tentative_parse (parser);
9600       /* If the token after `using' is `namespace', then we have a
9601          using-directive.  */
9602       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9603       if (token2->keyword == RID_NAMESPACE)
9604         cp_parser_using_directive (parser);
9605       /* Otherwise, it's a using-declaration.  */
9606       else
9607         cp_parser_using_declaration (parser,
9608                                      /*access_declaration_p=*/false);
9609     }
9610   /* If the next keyword is `__label__' we have a misplaced label
9611      declaration.  */
9612   else if (token1->keyword == RID_LABEL)
9613     {
9614       cp_lexer_consume_token (parser->lexer);
9615       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9616       cp_parser_skip_to_end_of_statement (parser);
9617       /* If the next token is now a `;', consume it.  */
9618       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9619         cp_lexer_consume_token (parser->lexer);
9620     }
9621   /* If the next token is `static_assert' we have a static assertion.  */
9622   else if (token1->keyword == RID_STATIC_ASSERT)
9623     cp_parser_static_assert (parser, /*member_p=*/false);
9624   /* Anything else must be a simple-declaration.  */
9625   else
9626     cp_parser_simple_declaration (parser, !statement_p,
9627                                   /*maybe_range_for_decl*/NULL);
9628 }
9629
9630 /* Parse a simple-declaration.
9631
9632    simple-declaration:
9633      decl-specifier-seq [opt] init-declarator-list [opt] ;
9634
9635    init-declarator-list:
9636      init-declarator
9637      init-declarator-list , init-declarator
9638
9639    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9640    function-definition as a simple-declaration.
9641
9642    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9643    parsed declaration if it is an uninitialized single declarator not followed
9644    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9645    if present, will not be consumed.  */
9646
9647 static void
9648 cp_parser_simple_declaration (cp_parser* parser,
9649                               bool function_definition_allowed_p,
9650                               tree *maybe_range_for_decl)
9651 {
9652   cp_decl_specifier_seq decl_specifiers;
9653   int declares_class_or_enum;
9654   bool saw_declarator;
9655
9656   if (maybe_range_for_decl)
9657     *maybe_range_for_decl = NULL_TREE;
9658
9659   /* Defer access checks until we know what is being declared; the
9660      checks for names appearing in the decl-specifier-seq should be
9661      done as if we were in the scope of the thing being declared.  */
9662   push_deferring_access_checks (dk_deferred);
9663
9664   /* Parse the decl-specifier-seq.  We have to keep track of whether
9665      or not the decl-specifier-seq declares a named class or
9666      enumeration type, since that is the only case in which the
9667      init-declarator-list is allowed to be empty.
9668
9669      [dcl.dcl]
9670
9671      In a simple-declaration, the optional init-declarator-list can be
9672      omitted only when declaring a class or enumeration, that is when
9673      the decl-specifier-seq contains either a class-specifier, an
9674      elaborated-type-specifier, or an enum-specifier.  */
9675   cp_parser_decl_specifier_seq (parser,
9676                                 CP_PARSER_FLAGS_OPTIONAL,
9677                                 &decl_specifiers,
9678                                 &declares_class_or_enum);
9679   /* We no longer need to defer access checks.  */
9680   stop_deferring_access_checks ();
9681
9682   /* In a block scope, a valid declaration must always have a
9683      decl-specifier-seq.  By not trying to parse declarators, we can
9684      resolve the declaration/expression ambiguity more quickly.  */
9685   if (!function_definition_allowed_p
9686       && !decl_specifiers.any_specifiers_p)
9687     {
9688       cp_parser_error (parser, "expected declaration");
9689       goto done;
9690     }
9691
9692   /* If the next two tokens are both identifiers, the code is
9693      erroneous. The usual cause of this situation is code like:
9694
9695        T t;
9696
9697      where "T" should name a type -- but does not.  */
9698   if (!decl_specifiers.any_type_specifiers_p
9699       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9700     {
9701       /* If parsing tentatively, we should commit; we really are
9702          looking at a declaration.  */
9703       cp_parser_commit_to_tentative_parse (parser);
9704       /* Give up.  */
9705       goto done;
9706     }
9707
9708   /* If we have seen at least one decl-specifier, and the next token
9709      is not a parenthesis, then we must be looking at a declaration.
9710      (After "int (" we might be looking at a functional cast.)  */
9711   if (decl_specifiers.any_specifiers_p
9712       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9713       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9714       && !cp_parser_error_occurred (parser))
9715     cp_parser_commit_to_tentative_parse (parser);
9716
9717   /* Keep going until we hit the `;' at the end of the simple
9718      declaration.  */
9719   saw_declarator = false;
9720   while (cp_lexer_next_token_is_not (parser->lexer,
9721                                      CPP_SEMICOLON))
9722     {
9723       cp_token *token;
9724       bool function_definition_p;
9725       tree decl;
9726
9727       if (saw_declarator)
9728         {
9729           /* If we are processing next declarator, coma is expected */
9730           token = cp_lexer_peek_token (parser->lexer);
9731           gcc_assert (token->type == CPP_COMMA);
9732           cp_lexer_consume_token (parser->lexer);
9733           if (maybe_range_for_decl)
9734             *maybe_range_for_decl = error_mark_node;
9735         }
9736       else
9737         saw_declarator = true;
9738
9739       /* Parse the init-declarator.  */
9740       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9741                                         /*checks=*/NULL,
9742                                         function_definition_allowed_p,
9743                                         /*member_p=*/false,
9744                                         declares_class_or_enum,
9745                                         &function_definition_p,
9746                                         maybe_range_for_decl);
9747       /* If an error occurred while parsing tentatively, exit quickly.
9748          (That usually happens when in the body of a function; each
9749          statement is treated as a declaration-statement until proven
9750          otherwise.)  */
9751       if (cp_parser_error_occurred (parser))
9752         goto done;
9753       /* Handle function definitions specially.  */
9754       if (function_definition_p)
9755         {
9756           /* If the next token is a `,', then we are probably
9757              processing something like:
9758
9759                void f() {}, *p;
9760
9761              which is erroneous.  */
9762           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9763             {
9764               cp_token *token = cp_lexer_peek_token (parser->lexer);
9765               error_at (token->location,
9766                         "mixing"
9767                         " declarations and function-definitions is forbidden");
9768             }
9769           /* Otherwise, we're done with the list of declarators.  */
9770           else
9771             {
9772               pop_deferring_access_checks ();
9773               return;
9774             }
9775         }
9776       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9777         *maybe_range_for_decl = decl;
9778       /* The next token should be either a `,' or a `;'.  */
9779       token = cp_lexer_peek_token (parser->lexer);
9780       /* If it's a `,', there are more declarators to come.  */
9781       if (token->type == CPP_COMMA)
9782         /* will be consumed next time around */;
9783       /* If it's a `;', we are done.  */
9784       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9785         break;
9786       /* Anything else is an error.  */
9787       else
9788         {
9789           /* If we have already issued an error message we don't need
9790              to issue another one.  */
9791           if (decl != error_mark_node
9792               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9793             cp_parser_error (parser, "expected %<,%> or %<;%>");
9794           /* Skip tokens until we reach the end of the statement.  */
9795           cp_parser_skip_to_end_of_statement (parser);
9796           /* If the next token is now a `;', consume it.  */
9797           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9798             cp_lexer_consume_token (parser->lexer);
9799           goto done;
9800         }
9801       /* After the first time around, a function-definition is not
9802          allowed -- even if it was OK at first.  For example:
9803
9804            int i, f() {}
9805
9806          is not valid.  */
9807       function_definition_allowed_p = false;
9808     }
9809
9810   /* Issue an error message if no declarators are present, and the
9811      decl-specifier-seq does not itself declare a class or
9812      enumeration.  */
9813   if (!saw_declarator)
9814     {
9815       if (cp_parser_declares_only_class_p (parser))
9816         shadow_tag (&decl_specifiers);
9817       /* Perform any deferred access checks.  */
9818       perform_deferred_access_checks ();
9819     }
9820
9821   /* Consume the `;'.  */
9822   if (!maybe_range_for_decl)
9823       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9824
9825  done:
9826   pop_deferring_access_checks ();
9827 }
9828
9829 /* Parse a decl-specifier-seq.
9830
9831    decl-specifier-seq:
9832      decl-specifier-seq [opt] decl-specifier
9833
9834    decl-specifier:
9835      storage-class-specifier
9836      type-specifier
9837      function-specifier
9838      friend
9839      typedef
9840
9841    GNU Extension:
9842
9843    decl-specifier:
9844      attributes
9845
9846    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9847
9848    The parser flags FLAGS is used to control type-specifier parsing.
9849
9850    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9851    flags:
9852
9853      1: one of the decl-specifiers is an elaborated-type-specifier
9854         (i.e., a type declaration)
9855      2: one of the decl-specifiers is an enum-specifier or a
9856         class-specifier (i.e., a type definition)
9857
9858    */
9859
9860 static void
9861 cp_parser_decl_specifier_seq (cp_parser* parser,
9862                               cp_parser_flags flags,
9863                               cp_decl_specifier_seq *decl_specs,
9864                               int* declares_class_or_enum)
9865 {
9866   bool constructor_possible_p = !parser->in_declarator_p;
9867   cp_token *start_token = NULL;
9868
9869   /* Clear DECL_SPECS.  */
9870   clear_decl_specs (decl_specs);
9871
9872   /* Assume no class or enumeration type is declared.  */
9873   *declares_class_or_enum = 0;
9874
9875   /* Keep reading specifiers until there are no more to read.  */
9876   while (true)
9877     {
9878       bool constructor_p;
9879       bool found_decl_spec;
9880       cp_token *token;
9881
9882       /* Peek at the next token.  */
9883       token = cp_lexer_peek_token (parser->lexer);
9884
9885       /* Save the first token of the decl spec list for error
9886          reporting.  */
9887       if (!start_token)
9888         start_token = token;
9889       /* Handle attributes.  */
9890       if (token->keyword == RID_ATTRIBUTE)
9891         {
9892           /* Parse the attributes.  */
9893           decl_specs->attributes
9894             = chainon (decl_specs->attributes,
9895                        cp_parser_attributes_opt (parser));
9896           continue;
9897         }
9898       /* Assume we will find a decl-specifier keyword.  */
9899       found_decl_spec = true;
9900       /* If the next token is an appropriate keyword, we can simply
9901          add it to the list.  */
9902       switch (token->keyword)
9903         {
9904           /* decl-specifier:
9905                friend
9906                constexpr */
9907         case RID_FRIEND:
9908           if (!at_class_scope_p ())
9909             {
9910               error_at (token->location, "%<friend%> used outside of class");
9911               cp_lexer_purge_token (parser->lexer);
9912             }
9913           else
9914             {
9915               ++decl_specs->specs[(int) ds_friend];
9916               /* Consume the token.  */
9917               cp_lexer_consume_token (parser->lexer);
9918             }
9919           break;
9920
9921         case RID_CONSTEXPR:
9922           ++decl_specs->specs[(int) ds_constexpr];
9923           cp_lexer_consume_token (parser->lexer);
9924           break;
9925
9926           /* function-specifier:
9927                inline
9928                virtual
9929                explicit  */
9930         case RID_INLINE:
9931         case RID_VIRTUAL:
9932         case RID_EXPLICIT:
9933           cp_parser_function_specifier_opt (parser, decl_specs);
9934           break;
9935
9936           /* decl-specifier:
9937                typedef  */
9938         case RID_TYPEDEF:
9939           ++decl_specs->specs[(int) ds_typedef];
9940           /* Consume the token.  */
9941           cp_lexer_consume_token (parser->lexer);
9942           /* A constructor declarator cannot appear in a typedef.  */
9943           constructor_possible_p = false;
9944           /* The "typedef" keyword can only occur in a declaration; we
9945              may as well commit at this point.  */
9946           cp_parser_commit_to_tentative_parse (parser);
9947
9948           if (decl_specs->storage_class != sc_none)
9949             decl_specs->conflicting_specifiers_p = true;
9950           break;
9951
9952           /* storage-class-specifier:
9953                auto
9954                register
9955                static
9956                extern
9957                mutable
9958
9959              GNU Extension:
9960                thread  */
9961         case RID_AUTO:
9962           if (cxx_dialect == cxx98) 
9963             {
9964               /* Consume the token.  */
9965               cp_lexer_consume_token (parser->lexer);
9966
9967               /* Complain about `auto' as a storage specifier, if
9968                  we're complaining about C++0x compatibility.  */
9969               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9970                           " will change meaning in C++0x; please remove it");
9971
9972               /* Set the storage class anyway.  */
9973               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9974                                            token->location);
9975             }
9976           else
9977             /* C++0x auto type-specifier.  */
9978             found_decl_spec = false;
9979           break;
9980
9981         case RID_REGISTER:
9982         case RID_STATIC:
9983         case RID_EXTERN:
9984         case RID_MUTABLE:
9985           /* Consume the token.  */
9986           cp_lexer_consume_token (parser->lexer);
9987           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9988                                        token->location);
9989           break;
9990         case RID_THREAD:
9991           /* Consume the token.  */
9992           cp_lexer_consume_token (parser->lexer);
9993           ++decl_specs->specs[(int) ds_thread];
9994           break;
9995
9996         default:
9997           /* We did not yet find a decl-specifier yet.  */
9998           found_decl_spec = false;
9999           break;
10000         }
10001
10002       if (found_decl_spec
10003           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10004           && token->keyword != RID_CONSTEXPR)
10005         error ("decl-specifier invalid in condition");
10006
10007       /* Constructors are a special case.  The `S' in `S()' is not a
10008          decl-specifier; it is the beginning of the declarator.  */
10009       constructor_p
10010         = (!found_decl_spec
10011            && constructor_possible_p
10012            && (cp_parser_constructor_declarator_p
10013                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10014
10015       /* If we don't have a DECL_SPEC yet, then we must be looking at
10016          a type-specifier.  */
10017       if (!found_decl_spec && !constructor_p)
10018         {
10019           int decl_spec_declares_class_or_enum;
10020           bool is_cv_qualifier;
10021           tree type_spec;
10022
10023           type_spec
10024             = cp_parser_type_specifier (parser, flags,
10025                                         decl_specs,
10026                                         /*is_declaration=*/true,
10027                                         &decl_spec_declares_class_or_enum,
10028                                         &is_cv_qualifier);
10029           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10030
10031           /* If this type-specifier referenced a user-defined type
10032              (a typedef, class-name, etc.), then we can't allow any
10033              more such type-specifiers henceforth.
10034
10035              [dcl.spec]
10036
10037              The longest sequence of decl-specifiers that could
10038              possibly be a type name is taken as the
10039              decl-specifier-seq of a declaration.  The sequence shall
10040              be self-consistent as described below.
10041
10042              [dcl.type]
10043
10044              As a general rule, at most one type-specifier is allowed
10045              in the complete decl-specifier-seq of a declaration.  The
10046              only exceptions are the following:
10047
10048              -- const or volatile can be combined with any other
10049                 type-specifier.
10050
10051              -- signed or unsigned can be combined with char, long,
10052                 short, or int.
10053
10054              -- ..
10055
10056              Example:
10057
10058                typedef char* Pc;
10059                void g (const int Pc);
10060
10061              Here, Pc is *not* part of the decl-specifier seq; it's
10062              the declarator.  Therefore, once we see a type-specifier
10063              (other than a cv-qualifier), we forbid any additional
10064              user-defined types.  We *do* still allow things like `int
10065              int' to be considered a decl-specifier-seq, and issue the
10066              error message later.  */
10067           if (type_spec && !is_cv_qualifier)
10068             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10069           /* A constructor declarator cannot follow a type-specifier.  */
10070           if (type_spec)
10071             {
10072               constructor_possible_p = false;
10073               found_decl_spec = true;
10074               if (!is_cv_qualifier)
10075                 decl_specs->any_type_specifiers_p = true;
10076             }
10077         }
10078
10079       /* If we still do not have a DECL_SPEC, then there are no more
10080          decl-specifiers.  */
10081       if (!found_decl_spec)
10082         break;
10083
10084       decl_specs->any_specifiers_p = true;
10085       /* After we see one decl-specifier, further decl-specifiers are
10086          always optional.  */
10087       flags |= CP_PARSER_FLAGS_OPTIONAL;
10088     }
10089
10090   cp_parser_check_decl_spec (decl_specs, start_token->location);
10091
10092   /* Don't allow a friend specifier with a class definition.  */
10093   if (decl_specs->specs[(int) ds_friend] != 0
10094       && (*declares_class_or_enum & 2))
10095     error_at (start_token->location,
10096               "class definition may not be declared a friend");
10097 }
10098
10099 /* Parse an (optional) storage-class-specifier.
10100
10101    storage-class-specifier:
10102      auto
10103      register
10104      static
10105      extern
10106      mutable
10107
10108    GNU Extension:
10109
10110    storage-class-specifier:
10111      thread
10112
10113    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10114
10115 static tree
10116 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10117 {
10118   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10119     {
10120     case RID_AUTO:
10121       if (cxx_dialect != cxx98)
10122         return NULL_TREE;
10123       /* Fall through for C++98.  */
10124
10125     case RID_REGISTER:
10126     case RID_STATIC:
10127     case RID_EXTERN:
10128     case RID_MUTABLE:
10129     case RID_THREAD:
10130       /* Consume the token.  */
10131       return cp_lexer_consume_token (parser->lexer)->u.value;
10132
10133     default:
10134       return NULL_TREE;
10135     }
10136 }
10137
10138 /* Parse an (optional) function-specifier.
10139
10140    function-specifier:
10141      inline
10142      virtual
10143      explicit
10144
10145    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10146    Updates DECL_SPECS, if it is non-NULL.  */
10147
10148 static tree
10149 cp_parser_function_specifier_opt (cp_parser* parser,
10150                                   cp_decl_specifier_seq *decl_specs)
10151 {
10152   cp_token *token = cp_lexer_peek_token (parser->lexer);
10153   switch (token->keyword)
10154     {
10155     case RID_INLINE:
10156       if (decl_specs)
10157         ++decl_specs->specs[(int) ds_inline];
10158       break;
10159
10160     case RID_VIRTUAL:
10161       /* 14.5.2.3 [temp.mem]
10162
10163          A member function template shall not be virtual.  */
10164       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10165         error_at (token->location, "templates may not be %<virtual%>");
10166       else if (decl_specs)
10167         ++decl_specs->specs[(int) ds_virtual];
10168       break;
10169
10170     case RID_EXPLICIT:
10171       if (decl_specs)
10172         ++decl_specs->specs[(int) ds_explicit];
10173       break;
10174
10175     default:
10176       return NULL_TREE;
10177     }
10178
10179   /* Consume the token.  */
10180   return cp_lexer_consume_token (parser->lexer)->u.value;
10181 }
10182
10183 /* Parse a linkage-specification.
10184
10185    linkage-specification:
10186      extern string-literal { declaration-seq [opt] }
10187      extern string-literal declaration  */
10188
10189 static void
10190 cp_parser_linkage_specification (cp_parser* parser)
10191 {
10192   tree linkage;
10193
10194   /* Look for the `extern' keyword.  */
10195   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10196
10197   /* Look for the string-literal.  */
10198   linkage = cp_parser_string_literal (parser, false, false);
10199
10200   /* Transform the literal into an identifier.  If the literal is a
10201      wide-character string, or contains embedded NULs, then we can't
10202      handle it as the user wants.  */
10203   if (strlen (TREE_STRING_POINTER (linkage))
10204       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10205     {
10206       cp_parser_error (parser, "invalid linkage-specification");
10207       /* Assume C++ linkage.  */
10208       linkage = lang_name_cplusplus;
10209     }
10210   else
10211     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10212
10213   /* We're now using the new linkage.  */
10214   push_lang_context (linkage);
10215
10216   /* If the next token is a `{', then we're using the first
10217      production.  */
10218   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10219     {
10220       /* Consume the `{' token.  */
10221       cp_lexer_consume_token (parser->lexer);
10222       /* Parse the declarations.  */
10223       cp_parser_declaration_seq_opt (parser);
10224       /* Look for the closing `}'.  */
10225       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10226     }
10227   /* Otherwise, there's just one declaration.  */
10228   else
10229     {
10230       bool saved_in_unbraced_linkage_specification_p;
10231
10232       saved_in_unbraced_linkage_specification_p
10233         = parser->in_unbraced_linkage_specification_p;
10234       parser->in_unbraced_linkage_specification_p = true;
10235       cp_parser_declaration (parser);
10236       parser->in_unbraced_linkage_specification_p
10237         = saved_in_unbraced_linkage_specification_p;
10238     }
10239
10240   /* We're done with the linkage-specification.  */
10241   pop_lang_context ();
10242 }
10243
10244 /* Parse a static_assert-declaration.
10245
10246    static_assert-declaration:
10247      static_assert ( constant-expression , string-literal ) ; 
10248
10249    If MEMBER_P, this static_assert is a class member.  */
10250
10251 static void 
10252 cp_parser_static_assert(cp_parser *parser, bool member_p)
10253 {
10254   tree condition;
10255   tree message;
10256   cp_token *token;
10257   location_t saved_loc;
10258   bool dummy;
10259
10260   /* Peek at the `static_assert' token so we can keep track of exactly
10261      where the static assertion started.  */
10262   token = cp_lexer_peek_token (parser->lexer);
10263   saved_loc = token->location;
10264
10265   /* Look for the `static_assert' keyword.  */
10266   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10267                                   RT_STATIC_ASSERT))
10268     return;
10269
10270   /*  We know we are in a static assertion; commit to any tentative
10271       parse.  */
10272   if (cp_parser_parsing_tentatively (parser))
10273     cp_parser_commit_to_tentative_parse (parser);
10274
10275   /* Parse the `(' starting the static assertion condition.  */
10276   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10277
10278   /* Parse the constant-expression.  Allow a non-constant expression
10279      here in order to give better diagnostics in finish_static_assert.  */
10280   condition = 
10281     cp_parser_constant_expression (parser,
10282                                    /*allow_non_constant_p=*/true,
10283                                    /*non_constant_p=*/&dummy);
10284
10285   /* Parse the separating `,'.  */
10286   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10287
10288   /* Parse the string-literal message.  */
10289   message = cp_parser_string_literal (parser, 
10290                                       /*translate=*/false,
10291                                       /*wide_ok=*/true);
10292
10293   /* A `)' completes the static assertion.  */
10294   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10295     cp_parser_skip_to_closing_parenthesis (parser, 
10296                                            /*recovering=*/true, 
10297                                            /*or_comma=*/false,
10298                                            /*consume_paren=*/true);
10299
10300   /* A semicolon terminates the declaration.  */
10301   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10302
10303   /* Complete the static assertion, which may mean either processing 
10304      the static assert now or saving it for template instantiation.  */
10305   finish_static_assert (condition, message, saved_loc, member_p);
10306 }
10307
10308 /* Parse a `decltype' type. Returns the type. 
10309
10310    simple-type-specifier:
10311      decltype ( expression )  */
10312
10313 static tree
10314 cp_parser_decltype (cp_parser *parser)
10315 {
10316   tree expr;
10317   bool id_expression_or_member_access_p = false;
10318   const char *saved_message;
10319   bool saved_integral_constant_expression_p;
10320   bool saved_non_integral_constant_expression_p;
10321   cp_token *id_expr_start_token;
10322   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10323
10324   if (start_token->type == CPP_DECLTYPE)
10325     {
10326       /* Already parsed.  */
10327       cp_lexer_consume_token (parser->lexer);
10328       return start_token->u.value;
10329     }
10330
10331   /* Look for the `decltype' token.  */
10332   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10333     return error_mark_node;
10334
10335   /* Types cannot be defined in a `decltype' expression.  Save away the
10336      old message.  */
10337   saved_message = parser->type_definition_forbidden_message;
10338
10339   /* And create the new one.  */
10340   parser->type_definition_forbidden_message
10341     = G_("types may not be defined in %<decltype%> expressions");
10342
10343   /* The restrictions on constant-expressions do not apply inside
10344      decltype expressions.  */
10345   saved_integral_constant_expression_p
10346     = parser->integral_constant_expression_p;
10347   saved_non_integral_constant_expression_p
10348     = parser->non_integral_constant_expression_p;
10349   parser->integral_constant_expression_p = false;
10350
10351   /* Do not actually evaluate the expression.  */
10352   ++cp_unevaluated_operand;
10353
10354   /* Do not warn about problems with the expression.  */
10355   ++c_inhibit_evaluation_warnings;
10356
10357   /* Parse the opening `('.  */
10358   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10359     return error_mark_node;
10360   
10361   /* First, try parsing an id-expression.  */
10362   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10363   cp_parser_parse_tentatively (parser);
10364   expr = cp_parser_id_expression (parser,
10365                                   /*template_keyword_p=*/false,
10366                                   /*check_dependency_p=*/true,
10367                                   /*template_p=*/NULL,
10368                                   /*declarator_p=*/false,
10369                                   /*optional_p=*/false);
10370
10371   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10372     {
10373       bool non_integral_constant_expression_p = false;
10374       tree id_expression = expr;
10375       cp_id_kind idk;
10376       const char *error_msg;
10377
10378       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10379         /* Lookup the name we got back from the id-expression.  */
10380         expr = cp_parser_lookup_name (parser, expr,
10381                                       none_type,
10382                                       /*is_template=*/false,
10383                                       /*is_namespace=*/false,
10384                                       /*check_dependency=*/true,
10385                                       /*ambiguous_decls=*/NULL,
10386                                       id_expr_start_token->location);
10387
10388       if (expr
10389           && expr != error_mark_node
10390           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10391           && TREE_CODE (expr) != TYPE_DECL
10392           && (TREE_CODE (expr) != BIT_NOT_EXPR
10393               || !TYPE_P (TREE_OPERAND (expr, 0)))
10394           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10395         {
10396           /* Complete lookup of the id-expression.  */
10397           expr = (finish_id_expression
10398                   (id_expression, expr, parser->scope, &idk,
10399                    /*integral_constant_expression_p=*/false,
10400                    /*allow_non_integral_constant_expression_p=*/true,
10401                    &non_integral_constant_expression_p,
10402                    /*template_p=*/false,
10403                    /*done=*/true,
10404                    /*address_p=*/false,
10405                    /*template_arg_p=*/false,
10406                    &error_msg,
10407                    id_expr_start_token->location));
10408
10409           if (expr == error_mark_node)
10410             /* We found an id-expression, but it was something that we
10411                should not have found. This is an error, not something
10412                we can recover from, so note that we found an
10413                id-expression and we'll recover as gracefully as
10414                possible.  */
10415             id_expression_or_member_access_p = true;
10416         }
10417
10418       if (expr 
10419           && expr != error_mark_node
10420           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10421         /* We have an id-expression.  */
10422         id_expression_or_member_access_p = true;
10423     }
10424
10425   if (!id_expression_or_member_access_p)
10426     {
10427       /* Abort the id-expression parse.  */
10428       cp_parser_abort_tentative_parse (parser);
10429
10430       /* Parsing tentatively, again.  */
10431       cp_parser_parse_tentatively (parser);
10432
10433       /* Parse a class member access.  */
10434       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10435                                            /*cast_p=*/false,
10436                                            /*member_access_only_p=*/true, NULL);
10437
10438       if (expr 
10439           && expr != error_mark_node
10440           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10441         /* We have an id-expression.  */
10442         id_expression_or_member_access_p = true;
10443     }
10444
10445   if (id_expression_or_member_access_p)
10446     /* We have parsed the complete id-expression or member access.  */
10447     cp_parser_parse_definitely (parser);
10448   else
10449     {
10450       bool saved_greater_than_is_operator_p;
10451
10452       /* Abort our attempt to parse an id-expression or member access
10453          expression.  */
10454       cp_parser_abort_tentative_parse (parser);
10455
10456       /* Within a parenthesized expression, a `>' token is always
10457          the greater-than operator.  */
10458       saved_greater_than_is_operator_p
10459         = parser->greater_than_is_operator_p;
10460       parser->greater_than_is_operator_p = true;
10461
10462       /* Parse a full expression.  */
10463       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10464
10465       /* The `>' token might be the end of a template-id or
10466          template-parameter-list now.  */
10467       parser->greater_than_is_operator_p
10468         = saved_greater_than_is_operator_p;
10469     }
10470
10471   /* Go back to evaluating expressions.  */
10472   --cp_unevaluated_operand;
10473   --c_inhibit_evaluation_warnings;
10474
10475   /* Restore the old message and the integral constant expression
10476      flags.  */
10477   parser->type_definition_forbidden_message = saved_message;
10478   parser->integral_constant_expression_p
10479     = saved_integral_constant_expression_p;
10480   parser->non_integral_constant_expression_p
10481     = saved_non_integral_constant_expression_p;
10482
10483   /* Parse to the closing `)'.  */
10484   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10485     {
10486       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10487                                              /*consume_paren=*/true);
10488       return error_mark_node;
10489     }
10490
10491   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
10492                                tf_warning_or_error);
10493
10494   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
10495      it again.  */
10496   start_token->type = CPP_DECLTYPE;
10497   start_token->u.value = expr;
10498   start_token->keyword = RID_MAX;
10499   cp_lexer_purge_tokens_after (parser->lexer, start_token);
10500
10501   return expr;
10502 }
10503
10504 /* Special member functions [gram.special] */
10505
10506 /* Parse a conversion-function-id.
10507
10508    conversion-function-id:
10509      operator conversion-type-id
10510
10511    Returns an IDENTIFIER_NODE representing the operator.  */
10512
10513 static tree
10514 cp_parser_conversion_function_id (cp_parser* parser)
10515 {
10516   tree type;
10517   tree saved_scope;
10518   tree saved_qualifying_scope;
10519   tree saved_object_scope;
10520   tree pushed_scope = NULL_TREE;
10521
10522   /* Look for the `operator' token.  */
10523   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10524     return error_mark_node;
10525   /* When we parse the conversion-type-id, the current scope will be
10526      reset.  However, we need that information in able to look up the
10527      conversion function later, so we save it here.  */
10528   saved_scope = parser->scope;
10529   saved_qualifying_scope = parser->qualifying_scope;
10530   saved_object_scope = parser->object_scope;
10531   /* We must enter the scope of the class so that the names of
10532      entities declared within the class are available in the
10533      conversion-type-id.  For example, consider:
10534
10535        struct S {
10536          typedef int I;
10537          operator I();
10538        };
10539
10540        S::operator I() { ... }
10541
10542      In order to see that `I' is a type-name in the definition, we
10543      must be in the scope of `S'.  */
10544   if (saved_scope)
10545     pushed_scope = push_scope (saved_scope);
10546   /* Parse the conversion-type-id.  */
10547   type = cp_parser_conversion_type_id (parser);
10548   /* Leave the scope of the class, if any.  */
10549   if (pushed_scope)
10550     pop_scope (pushed_scope);
10551   /* Restore the saved scope.  */
10552   parser->scope = saved_scope;
10553   parser->qualifying_scope = saved_qualifying_scope;
10554   parser->object_scope = saved_object_scope;
10555   /* If the TYPE is invalid, indicate failure.  */
10556   if (type == error_mark_node)
10557     return error_mark_node;
10558   return mangle_conv_op_name_for_type (type);
10559 }
10560
10561 /* Parse a conversion-type-id:
10562
10563    conversion-type-id:
10564      type-specifier-seq conversion-declarator [opt]
10565
10566    Returns the TYPE specified.  */
10567
10568 static tree
10569 cp_parser_conversion_type_id (cp_parser* parser)
10570 {
10571   tree attributes;
10572   cp_decl_specifier_seq type_specifiers;
10573   cp_declarator *declarator;
10574   tree type_specified;
10575
10576   /* Parse the attributes.  */
10577   attributes = cp_parser_attributes_opt (parser);
10578   /* Parse the type-specifiers.  */
10579   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10580                                 /*is_trailing_return=*/false,
10581                                 &type_specifiers);
10582   /* If that didn't work, stop.  */
10583   if (type_specifiers.type == error_mark_node)
10584     return error_mark_node;
10585   /* Parse the conversion-declarator.  */
10586   declarator = cp_parser_conversion_declarator_opt (parser);
10587
10588   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10589                                     /*initialized=*/0, &attributes);
10590   if (attributes)
10591     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10592
10593   /* Don't give this error when parsing tentatively.  This happens to
10594      work because we always parse this definitively once.  */
10595   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10596       && type_uses_auto (type_specified))
10597     {
10598       error ("invalid use of %<auto%> in conversion operator");
10599       return error_mark_node;
10600     }
10601
10602   return type_specified;
10603 }
10604
10605 /* Parse an (optional) conversion-declarator.
10606
10607    conversion-declarator:
10608      ptr-operator conversion-declarator [opt]
10609
10610    */
10611
10612 static cp_declarator *
10613 cp_parser_conversion_declarator_opt (cp_parser* parser)
10614 {
10615   enum tree_code code;
10616   tree class_type;
10617   cp_cv_quals cv_quals;
10618
10619   /* We don't know if there's a ptr-operator next, or not.  */
10620   cp_parser_parse_tentatively (parser);
10621   /* Try the ptr-operator.  */
10622   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10623   /* If it worked, look for more conversion-declarators.  */
10624   if (cp_parser_parse_definitely (parser))
10625     {
10626       cp_declarator *declarator;
10627
10628       /* Parse another optional declarator.  */
10629       declarator = cp_parser_conversion_declarator_opt (parser);
10630
10631       return cp_parser_make_indirect_declarator
10632         (code, class_type, cv_quals, declarator);
10633    }
10634
10635   return NULL;
10636 }
10637
10638 /* Parse an (optional) ctor-initializer.
10639
10640    ctor-initializer:
10641      : mem-initializer-list
10642
10643    Returns TRUE iff the ctor-initializer was actually present.  */
10644
10645 static bool
10646 cp_parser_ctor_initializer_opt (cp_parser* parser)
10647 {
10648   /* If the next token is not a `:', then there is no
10649      ctor-initializer.  */
10650   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10651     {
10652       /* Do default initialization of any bases and members.  */
10653       if (DECL_CONSTRUCTOR_P (current_function_decl))
10654         finish_mem_initializers (NULL_TREE);
10655
10656       return false;
10657     }
10658
10659   /* Consume the `:' token.  */
10660   cp_lexer_consume_token (parser->lexer);
10661   /* And the mem-initializer-list.  */
10662   cp_parser_mem_initializer_list (parser);
10663
10664   return true;
10665 }
10666
10667 /* Parse a mem-initializer-list.
10668
10669    mem-initializer-list:
10670      mem-initializer ... [opt]
10671      mem-initializer ... [opt] , mem-initializer-list  */
10672
10673 static void
10674 cp_parser_mem_initializer_list (cp_parser* parser)
10675 {
10676   tree mem_initializer_list = NULL_TREE;
10677   cp_token *token = cp_lexer_peek_token (parser->lexer);
10678
10679   /* Let the semantic analysis code know that we are starting the
10680      mem-initializer-list.  */
10681   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10682     error_at (token->location,
10683               "only constructors take member initializers");
10684
10685   /* Loop through the list.  */
10686   while (true)
10687     {
10688       tree mem_initializer;
10689
10690       token = cp_lexer_peek_token (parser->lexer);
10691       /* Parse the mem-initializer.  */
10692       mem_initializer = cp_parser_mem_initializer (parser);
10693       /* If the next token is a `...', we're expanding member initializers. */
10694       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10695         {
10696           /* Consume the `...'. */
10697           cp_lexer_consume_token (parser->lexer);
10698
10699           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10700              can be expanded but members cannot. */
10701           if (mem_initializer != error_mark_node
10702               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10703             {
10704               error_at (token->location,
10705                         "cannot expand initializer for member %<%D%>",
10706                         TREE_PURPOSE (mem_initializer));
10707               mem_initializer = error_mark_node;
10708             }
10709
10710           /* Construct the pack expansion type. */
10711           if (mem_initializer != error_mark_node)
10712             mem_initializer = make_pack_expansion (mem_initializer);
10713         }
10714       /* Add it to the list, unless it was erroneous.  */
10715       if (mem_initializer != error_mark_node)
10716         {
10717           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10718           mem_initializer_list = mem_initializer;
10719         }
10720       /* If the next token is not a `,', we're done.  */
10721       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10722         break;
10723       /* Consume the `,' token.  */
10724       cp_lexer_consume_token (parser->lexer);
10725     }
10726
10727   /* Perform semantic analysis.  */
10728   if (DECL_CONSTRUCTOR_P (current_function_decl))
10729     finish_mem_initializers (mem_initializer_list);
10730 }
10731
10732 /* Parse a mem-initializer.
10733
10734    mem-initializer:
10735      mem-initializer-id ( expression-list [opt] )
10736      mem-initializer-id braced-init-list
10737
10738    GNU extension:
10739
10740    mem-initializer:
10741      ( expression-list [opt] )
10742
10743    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10744    class) or FIELD_DECL (for a non-static data member) to initialize;
10745    the TREE_VALUE is the expression-list.  An empty initialization
10746    list is represented by void_list_node.  */
10747
10748 static tree
10749 cp_parser_mem_initializer (cp_parser* parser)
10750 {
10751   tree mem_initializer_id;
10752   tree expression_list;
10753   tree member;
10754   cp_token *token = cp_lexer_peek_token (parser->lexer);
10755
10756   /* Find out what is being initialized.  */
10757   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10758     {
10759       permerror (token->location,
10760                  "anachronistic old-style base class initializer");
10761       mem_initializer_id = NULL_TREE;
10762     }
10763   else
10764     {
10765       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10766       if (mem_initializer_id == error_mark_node)
10767         return mem_initializer_id;
10768     }
10769   member = expand_member_init (mem_initializer_id);
10770   if (member && !DECL_P (member))
10771     in_base_initializer = 1;
10772
10773   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10774     {
10775       bool expr_non_constant_p;
10776       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10777       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10778       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10779       expression_list = build_tree_list (NULL_TREE, expression_list);
10780     }
10781   else
10782     {
10783       VEC(tree,gc)* vec;
10784       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10785                                                      /*cast_p=*/false,
10786                                                      /*allow_expansion_p=*/true,
10787                                                      /*non_constant_p=*/NULL);
10788       if (vec == NULL)
10789         return error_mark_node;
10790       expression_list = build_tree_list_vec (vec);
10791       release_tree_vector (vec);
10792     }
10793
10794   if (expression_list == error_mark_node)
10795     return error_mark_node;
10796   if (!expression_list)
10797     expression_list = void_type_node;
10798
10799   in_base_initializer = 0;
10800
10801   return member ? build_tree_list (member, expression_list) : error_mark_node;
10802 }
10803
10804 /* Parse a mem-initializer-id.
10805
10806    mem-initializer-id:
10807      :: [opt] nested-name-specifier [opt] class-name
10808      identifier
10809
10810    Returns a TYPE indicating the class to be initializer for the first
10811    production.  Returns an IDENTIFIER_NODE indicating the data member
10812    to be initialized for the second production.  */
10813
10814 static tree
10815 cp_parser_mem_initializer_id (cp_parser* parser)
10816 {
10817   bool global_scope_p;
10818   bool nested_name_specifier_p;
10819   bool template_p = false;
10820   tree id;
10821
10822   cp_token *token = cp_lexer_peek_token (parser->lexer);
10823
10824   /* `typename' is not allowed in this context ([temp.res]).  */
10825   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10826     {
10827       error_at (token->location, 
10828                 "keyword %<typename%> not allowed in this context (a qualified "
10829                 "member initializer is implicitly a type)");
10830       cp_lexer_consume_token (parser->lexer);
10831     }
10832   /* Look for the optional `::' operator.  */
10833   global_scope_p
10834     = (cp_parser_global_scope_opt (parser,
10835                                    /*current_scope_valid_p=*/false)
10836        != NULL_TREE);
10837   /* Look for the optional nested-name-specifier.  The simplest way to
10838      implement:
10839
10840        [temp.res]
10841
10842        The keyword `typename' is not permitted in a base-specifier or
10843        mem-initializer; in these contexts a qualified name that
10844        depends on a template-parameter is implicitly assumed to be a
10845        type name.
10846
10847      is to assume that we have seen the `typename' keyword at this
10848      point.  */
10849   nested_name_specifier_p
10850     = (cp_parser_nested_name_specifier_opt (parser,
10851                                             /*typename_keyword_p=*/true,
10852                                             /*check_dependency_p=*/true,
10853                                             /*type_p=*/true,
10854                                             /*is_declaration=*/true)
10855        != NULL_TREE);
10856   if (nested_name_specifier_p)
10857     template_p = cp_parser_optional_template_keyword (parser);
10858   /* If there is a `::' operator or a nested-name-specifier, then we
10859      are definitely looking for a class-name.  */
10860   if (global_scope_p || nested_name_specifier_p)
10861     return cp_parser_class_name (parser,
10862                                  /*typename_keyword_p=*/true,
10863                                  /*template_keyword_p=*/template_p,
10864                                  typename_type,
10865                                  /*check_dependency_p=*/true,
10866                                  /*class_head_p=*/false,
10867                                  /*is_declaration=*/true);
10868   /* Otherwise, we could also be looking for an ordinary identifier.  */
10869   cp_parser_parse_tentatively (parser);
10870   /* Try a class-name.  */
10871   id = cp_parser_class_name (parser,
10872                              /*typename_keyword_p=*/true,
10873                              /*template_keyword_p=*/false,
10874                              none_type,
10875                              /*check_dependency_p=*/true,
10876                              /*class_head_p=*/false,
10877                              /*is_declaration=*/true);
10878   /* If we found one, we're done.  */
10879   if (cp_parser_parse_definitely (parser))
10880     return id;
10881   /* Otherwise, look for an ordinary identifier.  */
10882   return cp_parser_identifier (parser);
10883 }
10884
10885 /* Overloading [gram.over] */
10886
10887 /* Parse an operator-function-id.
10888
10889    operator-function-id:
10890      operator operator
10891
10892    Returns an IDENTIFIER_NODE for the operator which is a
10893    human-readable spelling of the identifier, e.g., `operator +'.  */
10894
10895 static tree
10896 cp_parser_operator_function_id (cp_parser* parser)
10897 {
10898   /* Look for the `operator' keyword.  */
10899   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10900     return error_mark_node;
10901   /* And then the name of the operator itself.  */
10902   return cp_parser_operator (parser);
10903 }
10904
10905 /* Parse an operator.
10906
10907    operator:
10908      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10909      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10910      || ++ -- , ->* -> () []
10911
10912    GNU Extensions:
10913
10914    operator:
10915      <? >? <?= >?=
10916
10917    Returns an IDENTIFIER_NODE for the operator which is a
10918    human-readable spelling of the identifier, e.g., `operator +'.  */
10919
10920 static tree
10921 cp_parser_operator (cp_parser* parser)
10922 {
10923   tree id = NULL_TREE;
10924   cp_token *token;
10925
10926   /* Peek at the next token.  */
10927   token = cp_lexer_peek_token (parser->lexer);
10928   /* Figure out which operator we have.  */
10929   switch (token->type)
10930     {
10931     case CPP_KEYWORD:
10932       {
10933         enum tree_code op;
10934
10935         /* The keyword should be either `new' or `delete'.  */
10936         if (token->keyword == RID_NEW)
10937           op = NEW_EXPR;
10938         else if (token->keyword == RID_DELETE)
10939           op = DELETE_EXPR;
10940         else
10941           break;
10942
10943         /* Consume the `new' or `delete' token.  */
10944         cp_lexer_consume_token (parser->lexer);
10945
10946         /* Peek at the next token.  */
10947         token = cp_lexer_peek_token (parser->lexer);
10948         /* If it's a `[' token then this is the array variant of the
10949            operator.  */
10950         if (token->type == CPP_OPEN_SQUARE)
10951           {
10952             /* Consume the `[' token.  */
10953             cp_lexer_consume_token (parser->lexer);
10954             /* Look for the `]' token.  */
10955             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10956             id = ansi_opname (op == NEW_EXPR
10957                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10958           }
10959         /* Otherwise, we have the non-array variant.  */
10960         else
10961           id = ansi_opname (op);
10962
10963         return id;
10964       }
10965
10966     case CPP_PLUS:
10967       id = ansi_opname (PLUS_EXPR);
10968       break;
10969
10970     case CPP_MINUS:
10971       id = ansi_opname (MINUS_EXPR);
10972       break;
10973
10974     case CPP_MULT:
10975       id = ansi_opname (MULT_EXPR);
10976       break;
10977
10978     case CPP_DIV:
10979       id = ansi_opname (TRUNC_DIV_EXPR);
10980       break;
10981
10982     case CPP_MOD:
10983       id = ansi_opname (TRUNC_MOD_EXPR);
10984       break;
10985
10986     case CPP_XOR:
10987       id = ansi_opname (BIT_XOR_EXPR);
10988       break;
10989
10990     case CPP_AND:
10991       id = ansi_opname (BIT_AND_EXPR);
10992       break;
10993
10994     case CPP_OR:
10995       id = ansi_opname (BIT_IOR_EXPR);
10996       break;
10997
10998     case CPP_COMPL:
10999       id = ansi_opname (BIT_NOT_EXPR);
11000       break;
11001
11002     case CPP_NOT:
11003       id = ansi_opname (TRUTH_NOT_EXPR);
11004       break;
11005
11006     case CPP_EQ:
11007       id = ansi_assopname (NOP_EXPR);
11008       break;
11009
11010     case CPP_LESS:
11011       id = ansi_opname (LT_EXPR);
11012       break;
11013
11014     case CPP_GREATER:
11015       id = ansi_opname (GT_EXPR);
11016       break;
11017
11018     case CPP_PLUS_EQ:
11019       id = ansi_assopname (PLUS_EXPR);
11020       break;
11021
11022     case CPP_MINUS_EQ:
11023       id = ansi_assopname (MINUS_EXPR);
11024       break;
11025
11026     case CPP_MULT_EQ:
11027       id = ansi_assopname (MULT_EXPR);
11028       break;
11029
11030     case CPP_DIV_EQ:
11031       id = ansi_assopname (TRUNC_DIV_EXPR);
11032       break;
11033
11034     case CPP_MOD_EQ:
11035       id = ansi_assopname (TRUNC_MOD_EXPR);
11036       break;
11037
11038     case CPP_XOR_EQ:
11039       id = ansi_assopname (BIT_XOR_EXPR);
11040       break;
11041
11042     case CPP_AND_EQ:
11043       id = ansi_assopname (BIT_AND_EXPR);
11044       break;
11045
11046     case CPP_OR_EQ:
11047       id = ansi_assopname (BIT_IOR_EXPR);
11048       break;
11049
11050     case CPP_LSHIFT:
11051       id = ansi_opname (LSHIFT_EXPR);
11052       break;
11053
11054     case CPP_RSHIFT:
11055       id = ansi_opname (RSHIFT_EXPR);
11056       break;
11057
11058     case CPP_LSHIFT_EQ:
11059       id = ansi_assopname (LSHIFT_EXPR);
11060       break;
11061
11062     case CPP_RSHIFT_EQ:
11063       id = ansi_assopname (RSHIFT_EXPR);
11064       break;
11065
11066     case CPP_EQ_EQ:
11067       id = ansi_opname (EQ_EXPR);
11068       break;
11069
11070     case CPP_NOT_EQ:
11071       id = ansi_opname (NE_EXPR);
11072       break;
11073
11074     case CPP_LESS_EQ:
11075       id = ansi_opname (LE_EXPR);
11076       break;
11077
11078     case CPP_GREATER_EQ:
11079       id = ansi_opname (GE_EXPR);
11080       break;
11081
11082     case CPP_AND_AND:
11083       id = ansi_opname (TRUTH_ANDIF_EXPR);
11084       break;
11085
11086     case CPP_OR_OR:
11087       id = ansi_opname (TRUTH_ORIF_EXPR);
11088       break;
11089
11090     case CPP_PLUS_PLUS:
11091       id = ansi_opname (POSTINCREMENT_EXPR);
11092       break;
11093
11094     case CPP_MINUS_MINUS:
11095       id = ansi_opname (PREDECREMENT_EXPR);
11096       break;
11097
11098     case CPP_COMMA:
11099       id = ansi_opname (COMPOUND_EXPR);
11100       break;
11101
11102     case CPP_DEREF_STAR:
11103       id = ansi_opname (MEMBER_REF);
11104       break;
11105
11106     case CPP_DEREF:
11107       id = ansi_opname (COMPONENT_REF);
11108       break;
11109
11110     case CPP_OPEN_PAREN:
11111       /* Consume the `('.  */
11112       cp_lexer_consume_token (parser->lexer);
11113       /* Look for the matching `)'.  */
11114       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11115       return ansi_opname (CALL_EXPR);
11116
11117     case CPP_OPEN_SQUARE:
11118       /* Consume the `['.  */
11119       cp_lexer_consume_token (parser->lexer);
11120       /* Look for the matching `]'.  */
11121       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11122       return ansi_opname (ARRAY_REF);
11123
11124     default:
11125       /* Anything else is an error.  */
11126       break;
11127     }
11128
11129   /* If we have selected an identifier, we need to consume the
11130      operator token.  */
11131   if (id)
11132     cp_lexer_consume_token (parser->lexer);
11133   /* Otherwise, no valid operator name was present.  */
11134   else
11135     {
11136       cp_parser_error (parser, "expected operator");
11137       id = error_mark_node;
11138     }
11139
11140   return id;
11141 }
11142
11143 /* Parse a template-declaration.
11144
11145    template-declaration:
11146      export [opt] template < template-parameter-list > declaration
11147
11148    If MEMBER_P is TRUE, this template-declaration occurs within a
11149    class-specifier.
11150
11151    The grammar rule given by the standard isn't correct.  What
11152    is really meant is:
11153
11154    template-declaration:
11155      export [opt] template-parameter-list-seq
11156        decl-specifier-seq [opt] init-declarator [opt] ;
11157      export [opt] template-parameter-list-seq
11158        function-definition
11159
11160    template-parameter-list-seq:
11161      template-parameter-list-seq [opt]
11162      template < template-parameter-list >  */
11163
11164 static void
11165 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11166 {
11167   /* Check for `export'.  */
11168   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11169     {
11170       /* Consume the `export' token.  */
11171       cp_lexer_consume_token (parser->lexer);
11172       /* Warn that we do not support `export'.  */
11173       warning (0, "keyword %<export%> not implemented, and will be ignored");
11174     }
11175
11176   cp_parser_template_declaration_after_export (parser, member_p);
11177 }
11178
11179 /* Parse a template-parameter-list.
11180
11181    template-parameter-list:
11182      template-parameter
11183      template-parameter-list , template-parameter
11184
11185    Returns a TREE_LIST.  Each node represents a template parameter.
11186    The nodes are connected via their TREE_CHAINs.  */
11187
11188 static tree
11189 cp_parser_template_parameter_list (cp_parser* parser)
11190 {
11191   tree parameter_list = NULL_TREE;
11192
11193   begin_template_parm_list ();
11194
11195   /* The loop below parses the template parms.  We first need to know
11196      the total number of template parms to be able to compute proper
11197      canonical types of each dependent type. So after the loop, when
11198      we know the total number of template parms,
11199      end_template_parm_list computes the proper canonical types and
11200      fixes up the dependent types accordingly.  */
11201   while (true)
11202     {
11203       tree parameter;
11204       bool is_non_type;
11205       bool is_parameter_pack;
11206       location_t parm_loc;
11207
11208       /* Parse the template-parameter.  */
11209       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11210       parameter = cp_parser_template_parameter (parser, 
11211                                                 &is_non_type,
11212                                                 &is_parameter_pack);
11213       /* Add it to the list.  */
11214       if (parameter != error_mark_node)
11215         parameter_list = process_template_parm (parameter_list,
11216                                                 parm_loc,
11217                                                 parameter,
11218                                                 is_non_type,
11219                                                 is_parameter_pack,
11220                                                 0);
11221       else
11222        {
11223          tree err_parm = build_tree_list (parameter, parameter);
11224          parameter_list = chainon (parameter_list, err_parm);
11225        }
11226
11227       /* If the next token is not a `,', we're done.  */
11228       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11229         break;
11230       /* Otherwise, consume the `,' token.  */
11231       cp_lexer_consume_token (parser->lexer);
11232     }
11233
11234   return end_template_parm_list (parameter_list);
11235 }
11236
11237 /* Parse a template-parameter.
11238
11239    template-parameter:
11240      type-parameter
11241      parameter-declaration
11242
11243    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11244    the parameter.  The TREE_PURPOSE is the default value, if any.
11245    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11246    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11247    set to true iff this parameter is a parameter pack. */
11248
11249 static tree
11250 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11251                               bool *is_parameter_pack)
11252 {
11253   cp_token *token;
11254   cp_parameter_declarator *parameter_declarator;
11255   cp_declarator *id_declarator;
11256   tree parm;
11257
11258   /* Assume it is a type parameter or a template parameter.  */
11259   *is_non_type = false;
11260   /* Assume it not a parameter pack. */
11261   *is_parameter_pack = false;
11262   /* Peek at the next token.  */
11263   token = cp_lexer_peek_token (parser->lexer);
11264   /* If it is `class' or `template', we have a type-parameter.  */
11265   if (token->keyword == RID_TEMPLATE)
11266     return cp_parser_type_parameter (parser, is_parameter_pack);
11267   /* If it is `class' or `typename' we do not know yet whether it is a
11268      type parameter or a non-type parameter.  Consider:
11269
11270        template <typename T, typename T::X X> ...
11271
11272      or:
11273
11274        template <class C, class D*> ...
11275
11276      Here, the first parameter is a type parameter, and the second is
11277      a non-type parameter.  We can tell by looking at the token after
11278      the identifier -- if it is a `,', `=', or `>' then we have a type
11279      parameter.  */
11280   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11281     {
11282       /* Peek at the token after `class' or `typename'.  */
11283       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11284       /* If it's an ellipsis, we have a template type parameter
11285          pack. */
11286       if (token->type == CPP_ELLIPSIS)
11287         return cp_parser_type_parameter (parser, is_parameter_pack);
11288       /* If it's an identifier, skip it.  */
11289       if (token->type == CPP_NAME)
11290         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11291       /* Now, see if the token looks like the end of a template
11292          parameter.  */
11293       if (token->type == CPP_COMMA
11294           || token->type == CPP_EQ
11295           || token->type == CPP_GREATER)
11296         return cp_parser_type_parameter (parser, is_parameter_pack);
11297     }
11298
11299   /* Otherwise, it is a non-type parameter.
11300
11301      [temp.param]
11302
11303      When parsing a default template-argument for a non-type
11304      template-parameter, the first non-nested `>' is taken as the end
11305      of the template parameter-list rather than a greater-than
11306      operator.  */
11307   *is_non_type = true;
11308   parameter_declarator
11309      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11310                                         /*parenthesized_p=*/NULL);
11311
11312   /* If the parameter declaration is marked as a parameter pack, set
11313      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11314      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11315      grokdeclarator. */
11316   if (parameter_declarator
11317       && parameter_declarator->declarator
11318       && parameter_declarator->declarator->parameter_pack_p)
11319     {
11320       *is_parameter_pack = true;
11321       parameter_declarator->declarator->parameter_pack_p = false;
11322     }
11323
11324   /* If the next token is an ellipsis, and we don't already have it
11325      marked as a parameter pack, then we have a parameter pack (that
11326      has no declarator).  */
11327   if (!*is_parameter_pack
11328       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11329       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11330     {
11331       /* Consume the `...'.  */
11332       cp_lexer_consume_token (parser->lexer);
11333       maybe_warn_variadic_templates ();
11334       
11335       *is_parameter_pack = true;
11336     }
11337   /* We might end up with a pack expansion as the type of the non-type
11338      template parameter, in which case this is a non-type template
11339      parameter pack.  */
11340   else if (parameter_declarator
11341            && parameter_declarator->decl_specifiers.type
11342            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11343     {
11344       *is_parameter_pack = true;
11345       parameter_declarator->decl_specifiers.type = 
11346         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11347     }
11348
11349   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11350     {
11351       /* Parameter packs cannot have default arguments.  However, a
11352          user may try to do so, so we'll parse them and give an
11353          appropriate diagnostic here.  */
11354
11355       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11356       
11357       /* Find the name of the parameter pack.  */     
11358       id_declarator = parameter_declarator->declarator;
11359       while (id_declarator && id_declarator->kind != cdk_id)
11360         id_declarator = id_declarator->declarator;
11361       
11362       if (id_declarator && id_declarator->kind == cdk_id)
11363         error_at (start_token->location,
11364                   "template parameter pack %qD cannot have a default argument",
11365                   id_declarator->u.id.unqualified_name);
11366       else
11367         error_at (start_token->location,
11368                   "template parameter pack cannot have a default argument");
11369       
11370       /* Parse the default argument, but throw away the result.  */
11371       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11372     }
11373
11374   parm = grokdeclarator (parameter_declarator->declarator,
11375                          &parameter_declarator->decl_specifiers,
11376                          TPARM, /*initialized=*/0,
11377                          /*attrlist=*/NULL);
11378   if (parm == error_mark_node)
11379     return error_mark_node;
11380
11381   return build_tree_list (parameter_declarator->default_argument, parm);
11382 }
11383
11384 /* Parse a type-parameter.
11385
11386    type-parameter:
11387      class identifier [opt]
11388      class identifier [opt] = type-id
11389      typename identifier [opt]
11390      typename identifier [opt] = type-id
11391      template < template-parameter-list > class identifier [opt]
11392      template < template-parameter-list > class identifier [opt]
11393        = id-expression
11394
11395    GNU Extension (variadic templates):
11396
11397    type-parameter:
11398      class ... identifier [opt]
11399      typename ... identifier [opt]
11400
11401    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11402    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11403    the declaration of the parameter.
11404
11405    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11406
11407 static tree
11408 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11409 {
11410   cp_token *token;
11411   tree parameter;
11412
11413   /* Look for a keyword to tell us what kind of parameter this is.  */
11414   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11415   if (!token)
11416     return error_mark_node;
11417
11418   switch (token->keyword)
11419     {
11420     case RID_CLASS:
11421     case RID_TYPENAME:
11422       {
11423         tree identifier;
11424         tree default_argument;
11425
11426         /* If the next token is an ellipsis, we have a template
11427            argument pack. */
11428         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11429           {
11430             /* Consume the `...' token. */
11431             cp_lexer_consume_token (parser->lexer);
11432             maybe_warn_variadic_templates ();
11433
11434             *is_parameter_pack = true;
11435           }
11436
11437         /* If the next token is an identifier, then it names the
11438            parameter.  */
11439         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11440           identifier = cp_parser_identifier (parser);
11441         else
11442           identifier = NULL_TREE;
11443
11444         /* Create the parameter.  */
11445         parameter = finish_template_type_parm (class_type_node, identifier);
11446
11447         /* If the next token is an `=', we have a default argument.  */
11448         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11449           {
11450             /* Consume the `=' token.  */
11451             cp_lexer_consume_token (parser->lexer);
11452             /* Parse the default-argument.  */
11453             push_deferring_access_checks (dk_no_deferred);
11454             default_argument = cp_parser_type_id (parser);
11455
11456             /* Template parameter packs cannot have default
11457                arguments. */
11458             if (*is_parameter_pack)
11459               {
11460                 if (identifier)
11461                   error_at (token->location,
11462                             "template parameter pack %qD cannot have a "
11463                             "default argument", identifier);
11464                 else
11465                   error_at (token->location,
11466                             "template parameter packs cannot have "
11467                             "default arguments");
11468                 default_argument = NULL_TREE;
11469               }
11470             pop_deferring_access_checks ();
11471           }
11472         else
11473           default_argument = NULL_TREE;
11474
11475         /* Create the combined representation of the parameter and the
11476            default argument.  */
11477         parameter = build_tree_list (default_argument, parameter);
11478       }
11479       break;
11480
11481     case RID_TEMPLATE:
11482       {
11483         tree identifier;
11484         tree default_argument;
11485
11486         /* Look for the `<'.  */
11487         cp_parser_require (parser, CPP_LESS, RT_LESS);
11488         /* Parse the template-parameter-list.  */
11489         cp_parser_template_parameter_list (parser);
11490         /* Look for the `>'.  */
11491         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11492         /* Look for the `class' keyword.  */
11493         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11494         /* If the next token is an ellipsis, we have a template
11495            argument pack. */
11496         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11497           {
11498             /* Consume the `...' token. */
11499             cp_lexer_consume_token (parser->lexer);
11500             maybe_warn_variadic_templates ();
11501
11502             *is_parameter_pack = true;
11503           }
11504         /* If the next token is an `=', then there is a
11505            default-argument.  If the next token is a `>', we are at
11506            the end of the parameter-list.  If the next token is a `,',
11507            then we are at the end of this parameter.  */
11508         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11509             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11510             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11511           {
11512             identifier = cp_parser_identifier (parser);
11513             /* Treat invalid names as if the parameter were nameless.  */
11514             if (identifier == error_mark_node)
11515               identifier = NULL_TREE;
11516           }
11517         else
11518           identifier = NULL_TREE;
11519
11520         /* Create the template parameter.  */
11521         parameter = finish_template_template_parm (class_type_node,
11522                                                    identifier);
11523
11524         /* If the next token is an `=', then there is a
11525            default-argument.  */
11526         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11527           {
11528             bool is_template;
11529
11530             /* Consume the `='.  */
11531             cp_lexer_consume_token (parser->lexer);
11532             /* Parse the id-expression.  */
11533             push_deferring_access_checks (dk_no_deferred);
11534             /* save token before parsing the id-expression, for error
11535                reporting */
11536             token = cp_lexer_peek_token (parser->lexer);
11537             default_argument
11538               = cp_parser_id_expression (parser,
11539                                          /*template_keyword_p=*/false,
11540                                          /*check_dependency_p=*/true,
11541                                          /*template_p=*/&is_template,
11542                                          /*declarator_p=*/false,
11543                                          /*optional_p=*/false);
11544             if (TREE_CODE (default_argument) == TYPE_DECL)
11545               /* If the id-expression was a template-id that refers to
11546                  a template-class, we already have the declaration here,
11547                  so no further lookup is needed.  */
11548                  ;
11549             else
11550               /* Look up the name.  */
11551               default_argument
11552                 = cp_parser_lookup_name (parser, default_argument,
11553                                          none_type,
11554                                          /*is_template=*/is_template,
11555                                          /*is_namespace=*/false,
11556                                          /*check_dependency=*/true,
11557                                          /*ambiguous_decls=*/NULL,
11558                                          token->location);
11559             /* See if the default argument is valid.  */
11560             default_argument
11561               = check_template_template_default_arg (default_argument);
11562
11563             /* Template parameter packs cannot have default
11564                arguments. */
11565             if (*is_parameter_pack)
11566               {
11567                 if (identifier)
11568                   error_at (token->location,
11569                             "template parameter pack %qD cannot "
11570                             "have a default argument",
11571                             identifier);
11572                 else
11573                   error_at (token->location, "template parameter packs cannot "
11574                             "have default arguments");
11575                 default_argument = NULL_TREE;
11576               }
11577             pop_deferring_access_checks ();
11578           }
11579         else
11580           default_argument = NULL_TREE;
11581
11582         /* Create the combined representation of the parameter and the
11583            default argument.  */
11584         parameter = build_tree_list (default_argument, parameter);
11585       }
11586       break;
11587
11588     default:
11589       gcc_unreachable ();
11590       break;
11591     }
11592
11593   return parameter;
11594 }
11595
11596 /* Parse a template-id.
11597
11598    template-id:
11599      template-name < template-argument-list [opt] >
11600
11601    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11602    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11603    returned.  Otherwise, if the template-name names a function, or set
11604    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11605    names a class, returns a TYPE_DECL for the specialization.
11606
11607    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11608    uninstantiated templates.  */
11609
11610 static tree
11611 cp_parser_template_id (cp_parser *parser,
11612                        bool template_keyword_p,
11613                        bool check_dependency_p,
11614                        bool is_declaration)
11615 {
11616   int i;
11617   tree templ;
11618   tree arguments;
11619   tree template_id;
11620   cp_token_position start_of_id = 0;
11621   deferred_access_check *chk;
11622   VEC (deferred_access_check,gc) *access_check;
11623   cp_token *next_token = NULL, *next_token_2 = NULL;
11624   bool is_identifier;
11625
11626   /* If the next token corresponds to a template-id, there is no need
11627      to reparse it.  */
11628   next_token = cp_lexer_peek_token (parser->lexer);
11629   if (next_token->type == CPP_TEMPLATE_ID)
11630     {
11631       struct tree_check *check_value;
11632
11633       /* Get the stored value.  */
11634       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11635       /* Perform any access checks that were deferred.  */
11636       access_check = check_value->checks;
11637       if (access_check)
11638         {
11639           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11640             perform_or_defer_access_check (chk->binfo,
11641                                            chk->decl,
11642                                            chk->diag_decl);
11643         }
11644       /* Return the stored value.  */
11645       return check_value->value;
11646     }
11647
11648   /* Avoid performing name lookup if there is no possibility of
11649      finding a template-id.  */
11650   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11651       || (next_token->type == CPP_NAME
11652           && !cp_parser_nth_token_starts_template_argument_list_p
11653                (parser, 2)))
11654     {
11655       cp_parser_error (parser, "expected template-id");
11656       return error_mark_node;
11657     }
11658
11659   /* Remember where the template-id starts.  */
11660   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11661     start_of_id = cp_lexer_token_position (parser->lexer, false);
11662
11663   push_deferring_access_checks (dk_deferred);
11664
11665   /* Parse the template-name.  */
11666   is_identifier = false;
11667   templ = cp_parser_template_name (parser, template_keyword_p,
11668                                    check_dependency_p,
11669                                    is_declaration,
11670                                    &is_identifier);
11671   if (templ == error_mark_node || is_identifier)
11672     {
11673       pop_deferring_access_checks ();
11674       return templ;
11675     }
11676
11677   /* If we find the sequence `[:' after a template-name, it's probably
11678      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11679      parse correctly the argument list.  */
11680   next_token = cp_lexer_peek_token (parser->lexer);
11681   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11682   if (next_token->type == CPP_OPEN_SQUARE
11683       && next_token->flags & DIGRAPH
11684       && next_token_2->type == CPP_COLON
11685       && !(next_token_2->flags & PREV_WHITE))
11686     {
11687       cp_parser_parse_tentatively (parser);
11688       /* Change `:' into `::'.  */
11689       next_token_2->type = CPP_SCOPE;
11690       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11691          CPP_LESS.  */
11692       cp_lexer_consume_token (parser->lexer);
11693
11694       /* Parse the arguments.  */
11695       arguments = cp_parser_enclosed_template_argument_list (parser);
11696       if (!cp_parser_parse_definitely (parser))
11697         {
11698           /* If we couldn't parse an argument list, then we revert our changes
11699              and return simply an error. Maybe this is not a template-id
11700              after all.  */
11701           next_token_2->type = CPP_COLON;
11702           cp_parser_error (parser, "expected %<<%>");
11703           pop_deferring_access_checks ();
11704           return error_mark_node;
11705         }
11706       /* Otherwise, emit an error about the invalid digraph, but continue
11707          parsing because we got our argument list.  */
11708       if (permerror (next_token->location,
11709                      "%<<::%> cannot begin a template-argument list"))
11710         {
11711           static bool hint = false;
11712           inform (next_token->location,
11713                   "%<<:%> is an alternate spelling for %<[%>."
11714                   " Insert whitespace between %<<%> and %<::%>");
11715           if (!hint && !flag_permissive)
11716             {
11717               inform (next_token->location, "(if you use %<-fpermissive%>"
11718                       " G++ will accept your code)");
11719               hint = true;
11720             }
11721         }
11722     }
11723   else
11724     {
11725       /* Look for the `<' that starts the template-argument-list.  */
11726       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11727         {
11728           pop_deferring_access_checks ();
11729           return error_mark_node;
11730         }
11731       /* Parse the arguments.  */
11732       arguments = cp_parser_enclosed_template_argument_list (parser);
11733     }
11734
11735   /* Build a representation of the specialization.  */
11736   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11737     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11738   else if (DECL_CLASS_TEMPLATE_P (templ)
11739            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11740     {
11741       bool entering_scope;
11742       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11743          template (rather than some instantiation thereof) only if
11744          is not nested within some other construct.  For example, in
11745          "template <typename T> void f(T) { A<T>::", A<T> is just an
11746          instantiation of A.  */
11747       entering_scope = (template_parm_scope_p ()
11748                         && cp_lexer_next_token_is (parser->lexer,
11749                                                    CPP_SCOPE));
11750       template_id
11751         = finish_template_type (templ, arguments, entering_scope);
11752     }
11753   else
11754     {
11755       /* If it's not a class-template or a template-template, it should be
11756          a function-template.  */
11757       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11758                    || TREE_CODE (templ) == OVERLOAD
11759                    || BASELINK_P (templ)));
11760
11761       template_id = lookup_template_function (templ, arguments);
11762     }
11763
11764   /* If parsing tentatively, replace the sequence of tokens that makes
11765      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11766      should we re-parse the token stream, we will not have to repeat
11767      the effort required to do the parse, nor will we issue duplicate
11768      error messages about problems during instantiation of the
11769      template.  */
11770   if (start_of_id)
11771     {
11772       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11773
11774       /* Reset the contents of the START_OF_ID token.  */
11775       token->type = CPP_TEMPLATE_ID;
11776       /* Retrieve any deferred checks.  Do not pop this access checks yet
11777          so the memory will not be reclaimed during token replacing below.  */
11778       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11779       token->u.tree_check_value->value = template_id;
11780       token->u.tree_check_value->checks = get_deferred_access_checks ();
11781       token->keyword = RID_MAX;
11782
11783       /* Purge all subsequent tokens.  */
11784       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11785
11786       /* ??? Can we actually assume that, if template_id ==
11787          error_mark_node, we will have issued a diagnostic to the
11788          user, as opposed to simply marking the tentative parse as
11789          failed?  */
11790       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11791         error_at (token->location, "parse error in template argument list");
11792     }
11793
11794   pop_deferring_access_checks ();
11795   return template_id;
11796 }
11797
11798 /* Parse a template-name.
11799
11800    template-name:
11801      identifier
11802
11803    The standard should actually say:
11804
11805    template-name:
11806      identifier
11807      operator-function-id
11808
11809    A defect report has been filed about this issue.
11810
11811    A conversion-function-id cannot be a template name because they cannot
11812    be part of a template-id. In fact, looking at this code:
11813
11814    a.operator K<int>()
11815
11816    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11817    It is impossible to call a templated conversion-function-id with an
11818    explicit argument list, since the only allowed template parameter is
11819    the type to which it is converting.
11820
11821    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11822    `template' keyword, in a construction like:
11823
11824      T::template f<3>()
11825
11826    In that case `f' is taken to be a template-name, even though there
11827    is no way of knowing for sure.
11828
11829    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11830    name refers to a set of overloaded functions, at least one of which
11831    is a template, or an IDENTIFIER_NODE with the name of the template,
11832    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11833    names are looked up inside uninstantiated templates.  */
11834
11835 static tree
11836 cp_parser_template_name (cp_parser* parser,
11837                          bool template_keyword_p,
11838                          bool check_dependency_p,
11839                          bool is_declaration,
11840                          bool *is_identifier)
11841 {
11842   tree identifier;
11843   tree decl;
11844   tree fns;
11845   cp_token *token = cp_lexer_peek_token (parser->lexer);
11846
11847   /* If the next token is `operator', then we have either an
11848      operator-function-id or a conversion-function-id.  */
11849   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11850     {
11851       /* We don't know whether we're looking at an
11852          operator-function-id or a conversion-function-id.  */
11853       cp_parser_parse_tentatively (parser);
11854       /* Try an operator-function-id.  */
11855       identifier = cp_parser_operator_function_id (parser);
11856       /* If that didn't work, try a conversion-function-id.  */
11857       if (!cp_parser_parse_definitely (parser))
11858         {
11859           cp_parser_error (parser, "expected template-name");
11860           return error_mark_node;
11861         }
11862     }
11863   /* Look for the identifier.  */
11864   else
11865     identifier = cp_parser_identifier (parser);
11866
11867   /* If we didn't find an identifier, we don't have a template-id.  */
11868   if (identifier == error_mark_node)
11869     return error_mark_node;
11870
11871   /* If the name immediately followed the `template' keyword, then it
11872      is a template-name.  However, if the next token is not `<', then
11873      we do not treat it as a template-name, since it is not being used
11874      as part of a template-id.  This enables us to handle constructs
11875      like:
11876
11877        template <typename T> struct S { S(); };
11878        template <typename T> S<T>::S();
11879
11880      correctly.  We would treat `S' as a template -- if it were `S<T>'
11881      -- but we do not if there is no `<'.  */
11882
11883   if (processing_template_decl
11884       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11885     {
11886       /* In a declaration, in a dependent context, we pretend that the
11887          "template" keyword was present in order to improve error
11888          recovery.  For example, given:
11889
11890            template <typename T> void f(T::X<int>);
11891
11892          we want to treat "X<int>" as a template-id.  */
11893       if (is_declaration
11894           && !template_keyword_p
11895           && parser->scope && TYPE_P (parser->scope)
11896           && check_dependency_p
11897           && dependent_scope_p (parser->scope)
11898           /* Do not do this for dtors (or ctors), since they never
11899              need the template keyword before their name.  */
11900           && !constructor_name_p (identifier, parser->scope))
11901         {
11902           cp_token_position start = 0;
11903
11904           /* Explain what went wrong.  */
11905           error_at (token->location, "non-template %qD used as template",
11906                     identifier);
11907           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11908                   parser->scope, identifier);
11909           /* If parsing tentatively, find the location of the "<" token.  */
11910           if (cp_parser_simulate_error (parser))
11911             start = cp_lexer_token_position (parser->lexer, true);
11912           /* Parse the template arguments so that we can issue error
11913              messages about them.  */
11914           cp_lexer_consume_token (parser->lexer);
11915           cp_parser_enclosed_template_argument_list (parser);
11916           /* Skip tokens until we find a good place from which to
11917              continue parsing.  */
11918           cp_parser_skip_to_closing_parenthesis (parser,
11919                                                  /*recovering=*/true,
11920                                                  /*or_comma=*/true,
11921                                                  /*consume_paren=*/false);
11922           /* If parsing tentatively, permanently remove the
11923              template argument list.  That will prevent duplicate
11924              error messages from being issued about the missing
11925              "template" keyword.  */
11926           if (start)
11927             cp_lexer_purge_tokens_after (parser->lexer, start);
11928           if (is_identifier)
11929             *is_identifier = true;
11930           return identifier;
11931         }
11932
11933       /* If the "template" keyword is present, then there is generally
11934          no point in doing name-lookup, so we just return IDENTIFIER.
11935          But, if the qualifying scope is non-dependent then we can
11936          (and must) do name-lookup normally.  */
11937       if (template_keyword_p
11938           && (!parser->scope
11939               || (TYPE_P (parser->scope)
11940                   && dependent_type_p (parser->scope))))
11941         return identifier;
11942     }
11943
11944   /* Look up the name.  */
11945   decl = cp_parser_lookup_name (parser, identifier,
11946                                 none_type,
11947                                 /*is_template=*/true,
11948                                 /*is_namespace=*/false,
11949                                 check_dependency_p,
11950                                 /*ambiguous_decls=*/NULL,
11951                                 token->location);
11952
11953   /* If DECL is a template, then the name was a template-name.  */
11954   if (TREE_CODE (decl) == TEMPLATE_DECL)
11955     ;
11956   else
11957     {
11958       tree fn = NULL_TREE;
11959
11960       /* The standard does not explicitly indicate whether a name that
11961          names a set of overloaded declarations, some of which are
11962          templates, is a template-name.  However, such a name should
11963          be a template-name; otherwise, there is no way to form a
11964          template-id for the overloaded templates.  */
11965       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11966       if (TREE_CODE (fns) == OVERLOAD)
11967         for (fn = fns; fn; fn = OVL_NEXT (fn))
11968           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11969             break;
11970
11971       if (!fn)
11972         {
11973           /* The name does not name a template.  */
11974           cp_parser_error (parser, "expected template-name");
11975           return error_mark_node;
11976         }
11977     }
11978
11979   /* If DECL is dependent, and refers to a function, then just return
11980      its name; we will look it up again during template instantiation.  */
11981   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11982     {
11983       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11984       if (TYPE_P (scope) && dependent_type_p (scope))
11985         return identifier;
11986     }
11987
11988   return decl;
11989 }
11990
11991 /* Parse a template-argument-list.
11992
11993    template-argument-list:
11994      template-argument ... [opt]
11995      template-argument-list , template-argument ... [opt]
11996
11997    Returns a TREE_VEC containing the arguments.  */
11998
11999 static tree
12000 cp_parser_template_argument_list (cp_parser* parser)
12001 {
12002   tree fixed_args[10];
12003   unsigned n_args = 0;
12004   unsigned alloced = 10;
12005   tree *arg_ary = fixed_args;
12006   tree vec;
12007   bool saved_in_template_argument_list_p;
12008   bool saved_ice_p;
12009   bool saved_non_ice_p;
12010
12011   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12012   parser->in_template_argument_list_p = true;
12013   /* Even if the template-id appears in an integral
12014      constant-expression, the contents of the argument list do
12015      not.  */
12016   saved_ice_p = parser->integral_constant_expression_p;
12017   parser->integral_constant_expression_p = false;
12018   saved_non_ice_p = parser->non_integral_constant_expression_p;
12019   parser->non_integral_constant_expression_p = false;
12020   /* Parse the arguments.  */
12021   do
12022     {
12023       tree argument;
12024
12025       if (n_args)
12026         /* Consume the comma.  */
12027         cp_lexer_consume_token (parser->lexer);
12028
12029       /* Parse the template-argument.  */
12030       argument = cp_parser_template_argument (parser);
12031
12032       /* If the next token is an ellipsis, we're expanding a template
12033          argument pack. */
12034       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12035         {
12036           if (argument == error_mark_node)
12037             {
12038               cp_token *token = cp_lexer_peek_token (parser->lexer);
12039               error_at (token->location,
12040                         "expected parameter pack before %<...%>");
12041             }
12042           /* Consume the `...' token. */
12043           cp_lexer_consume_token (parser->lexer);
12044
12045           /* Make the argument into a TYPE_PACK_EXPANSION or
12046              EXPR_PACK_EXPANSION. */
12047           argument = make_pack_expansion (argument);
12048         }
12049
12050       if (n_args == alloced)
12051         {
12052           alloced *= 2;
12053
12054           if (arg_ary == fixed_args)
12055             {
12056               arg_ary = XNEWVEC (tree, alloced);
12057               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12058             }
12059           else
12060             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12061         }
12062       arg_ary[n_args++] = argument;
12063     }
12064   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12065
12066   vec = make_tree_vec (n_args);
12067
12068   while (n_args--)
12069     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12070
12071   if (arg_ary != fixed_args)
12072     free (arg_ary);
12073   parser->non_integral_constant_expression_p = saved_non_ice_p;
12074   parser->integral_constant_expression_p = saved_ice_p;
12075   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12076 #ifdef ENABLE_CHECKING
12077   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12078 #endif
12079   return vec;
12080 }
12081
12082 /* Parse a template-argument.
12083
12084    template-argument:
12085      assignment-expression
12086      type-id
12087      id-expression
12088
12089    The representation is that of an assignment-expression, type-id, or
12090    id-expression -- except that the qualified id-expression is
12091    evaluated, so that the value returned is either a DECL or an
12092    OVERLOAD.
12093
12094    Although the standard says "assignment-expression", it forbids
12095    throw-expressions or assignments in the template argument.
12096    Therefore, we use "conditional-expression" instead.  */
12097
12098 static tree
12099 cp_parser_template_argument (cp_parser* parser)
12100 {
12101   tree argument;
12102   bool template_p;
12103   bool address_p;
12104   bool maybe_type_id = false;
12105   cp_token *token = NULL, *argument_start_token = NULL;
12106   cp_id_kind idk;
12107
12108   /* There's really no way to know what we're looking at, so we just
12109      try each alternative in order.
12110
12111        [temp.arg]
12112
12113        In a template-argument, an ambiguity between a type-id and an
12114        expression is resolved to a type-id, regardless of the form of
12115        the corresponding template-parameter.
12116
12117      Therefore, we try a type-id first.  */
12118   cp_parser_parse_tentatively (parser);
12119   argument = cp_parser_template_type_arg (parser);
12120   /* If there was no error parsing the type-id but the next token is a
12121      '>>', our behavior depends on which dialect of C++ we're
12122      parsing. In C++98, we probably found a typo for '> >'. But there
12123      are type-id which are also valid expressions. For instance:
12124
12125      struct X { int operator >> (int); };
12126      template <int V> struct Foo {};
12127      Foo<X () >> 5> r;
12128
12129      Here 'X()' is a valid type-id of a function type, but the user just
12130      wanted to write the expression "X() >> 5". Thus, we remember that we
12131      found a valid type-id, but we still try to parse the argument as an
12132      expression to see what happens. 
12133
12134      In C++0x, the '>>' will be considered two separate '>'
12135      tokens.  */
12136   if (!cp_parser_error_occurred (parser)
12137       && cxx_dialect == cxx98
12138       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12139     {
12140       maybe_type_id = true;
12141       cp_parser_abort_tentative_parse (parser);
12142     }
12143   else
12144     {
12145       /* If the next token isn't a `,' or a `>', then this argument wasn't
12146       really finished. This means that the argument is not a valid
12147       type-id.  */
12148       if (!cp_parser_next_token_ends_template_argument_p (parser))
12149         cp_parser_error (parser, "expected template-argument");
12150       /* If that worked, we're done.  */
12151       if (cp_parser_parse_definitely (parser))
12152         return argument;
12153     }
12154   /* We're still not sure what the argument will be.  */
12155   cp_parser_parse_tentatively (parser);
12156   /* Try a template.  */
12157   argument_start_token = cp_lexer_peek_token (parser->lexer);
12158   argument = cp_parser_id_expression (parser,
12159                                       /*template_keyword_p=*/false,
12160                                       /*check_dependency_p=*/true,
12161                                       &template_p,
12162                                       /*declarator_p=*/false,
12163                                       /*optional_p=*/false);
12164   /* If the next token isn't a `,' or a `>', then this argument wasn't
12165      really finished.  */
12166   if (!cp_parser_next_token_ends_template_argument_p (parser))
12167     cp_parser_error (parser, "expected template-argument");
12168   if (!cp_parser_error_occurred (parser))
12169     {
12170       /* Figure out what is being referred to.  If the id-expression
12171          was for a class template specialization, then we will have a
12172          TYPE_DECL at this point.  There is no need to do name lookup
12173          at this point in that case.  */
12174       if (TREE_CODE (argument) != TYPE_DECL)
12175         argument = cp_parser_lookup_name (parser, argument,
12176                                           none_type,
12177                                           /*is_template=*/template_p,
12178                                           /*is_namespace=*/false,
12179                                           /*check_dependency=*/true,
12180                                           /*ambiguous_decls=*/NULL,
12181                                           argument_start_token->location);
12182       if (TREE_CODE (argument) != TEMPLATE_DECL
12183           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12184         cp_parser_error (parser, "expected template-name");
12185     }
12186   if (cp_parser_parse_definitely (parser))
12187     return argument;
12188   /* It must be a non-type argument.  There permitted cases are given
12189      in [temp.arg.nontype]:
12190
12191      -- an integral constant-expression of integral or enumeration
12192         type; or
12193
12194      -- the name of a non-type template-parameter; or
12195
12196      -- the name of an object or function with external linkage...
12197
12198      -- the address of an object or function with external linkage...
12199
12200      -- a pointer to member...  */
12201   /* Look for a non-type template parameter.  */
12202   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12203     {
12204       cp_parser_parse_tentatively (parser);
12205       argument = cp_parser_primary_expression (parser,
12206                                                /*address_p=*/false,
12207                                                /*cast_p=*/false,
12208                                                /*template_arg_p=*/true,
12209                                                &idk);
12210       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12211           || !cp_parser_next_token_ends_template_argument_p (parser))
12212         cp_parser_simulate_error (parser);
12213       if (cp_parser_parse_definitely (parser))
12214         return argument;
12215     }
12216
12217   /* If the next token is "&", the argument must be the address of an
12218      object or function with external linkage.  */
12219   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12220   if (address_p)
12221     cp_lexer_consume_token (parser->lexer);
12222   /* See if we might have an id-expression.  */
12223   token = cp_lexer_peek_token (parser->lexer);
12224   if (token->type == CPP_NAME
12225       || token->keyword == RID_OPERATOR
12226       || token->type == CPP_SCOPE
12227       || token->type == CPP_TEMPLATE_ID
12228       || token->type == CPP_NESTED_NAME_SPECIFIER)
12229     {
12230       cp_parser_parse_tentatively (parser);
12231       argument = cp_parser_primary_expression (parser,
12232                                                address_p,
12233                                                /*cast_p=*/false,
12234                                                /*template_arg_p=*/true,
12235                                                &idk);
12236       if (cp_parser_error_occurred (parser)
12237           || !cp_parser_next_token_ends_template_argument_p (parser))
12238         cp_parser_abort_tentative_parse (parser);
12239       else
12240         {
12241           tree probe;
12242
12243           if (TREE_CODE (argument) == INDIRECT_REF)
12244             {
12245               gcc_assert (REFERENCE_REF_P (argument));
12246               argument = TREE_OPERAND (argument, 0);
12247             }
12248
12249           /* If we're in a template, we represent a qualified-id referring
12250              to a static data member as a SCOPE_REF even if the scope isn't
12251              dependent so that we can check access control later.  */
12252           probe = argument;
12253           if (TREE_CODE (probe) == SCOPE_REF)
12254             probe = TREE_OPERAND (probe, 1);
12255           if (TREE_CODE (probe) == VAR_DECL)
12256             {
12257               /* A variable without external linkage might still be a
12258                  valid constant-expression, so no error is issued here
12259                  if the external-linkage check fails.  */
12260               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12261                 cp_parser_simulate_error (parser);
12262             }
12263           else if (is_overloaded_fn (argument))
12264             /* All overloaded functions are allowed; if the external
12265                linkage test does not pass, an error will be issued
12266                later.  */
12267             ;
12268           else if (address_p
12269                    && (TREE_CODE (argument) == OFFSET_REF
12270                        || TREE_CODE (argument) == SCOPE_REF))
12271             /* A pointer-to-member.  */
12272             ;
12273           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12274             ;
12275           else
12276             cp_parser_simulate_error (parser);
12277
12278           if (cp_parser_parse_definitely (parser))
12279             {
12280               if (address_p)
12281                 argument = build_x_unary_op (ADDR_EXPR, argument,
12282                                              tf_warning_or_error);
12283               return argument;
12284             }
12285         }
12286     }
12287   /* If the argument started with "&", there are no other valid
12288      alternatives at this point.  */
12289   if (address_p)
12290     {
12291       cp_parser_error (parser, "invalid non-type template argument");
12292       return error_mark_node;
12293     }
12294
12295   /* If the argument wasn't successfully parsed as a type-id followed
12296      by '>>', the argument can only be a constant expression now.
12297      Otherwise, we try parsing the constant-expression tentatively,
12298      because the argument could really be a type-id.  */
12299   if (maybe_type_id)
12300     cp_parser_parse_tentatively (parser);
12301   argument = cp_parser_constant_expression (parser,
12302                                             /*allow_non_constant_p=*/false,
12303                                             /*non_constant_p=*/NULL);
12304   argument = fold_non_dependent_expr (argument);
12305   if (!maybe_type_id)
12306     return argument;
12307   if (!cp_parser_next_token_ends_template_argument_p (parser))
12308     cp_parser_error (parser, "expected template-argument");
12309   if (cp_parser_parse_definitely (parser))
12310     return argument;
12311   /* We did our best to parse the argument as a non type-id, but that
12312      was the only alternative that matched (albeit with a '>' after
12313      it). We can assume it's just a typo from the user, and a
12314      diagnostic will then be issued.  */
12315   return cp_parser_template_type_arg (parser);
12316 }
12317
12318 /* Parse an explicit-instantiation.
12319
12320    explicit-instantiation:
12321      template declaration
12322
12323    Although the standard says `declaration', what it really means is:
12324
12325    explicit-instantiation:
12326      template decl-specifier-seq [opt] declarator [opt] ;
12327
12328    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12329    supposed to be allowed.  A defect report has been filed about this
12330    issue.
12331
12332    GNU Extension:
12333
12334    explicit-instantiation:
12335      storage-class-specifier template
12336        decl-specifier-seq [opt] declarator [opt] ;
12337      function-specifier template
12338        decl-specifier-seq [opt] declarator [opt] ;  */
12339
12340 static void
12341 cp_parser_explicit_instantiation (cp_parser* parser)
12342 {
12343   int declares_class_or_enum;
12344   cp_decl_specifier_seq decl_specifiers;
12345   tree extension_specifier = NULL_TREE;
12346
12347   timevar_push (TV_TEMPLATE_INST);
12348
12349   /* Look for an (optional) storage-class-specifier or
12350      function-specifier.  */
12351   if (cp_parser_allow_gnu_extensions_p (parser))
12352     {
12353       extension_specifier
12354         = cp_parser_storage_class_specifier_opt (parser);
12355       if (!extension_specifier)
12356         extension_specifier
12357           = cp_parser_function_specifier_opt (parser,
12358                                               /*decl_specs=*/NULL);
12359     }
12360
12361   /* Look for the `template' keyword.  */
12362   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12363   /* Let the front end know that we are processing an explicit
12364      instantiation.  */
12365   begin_explicit_instantiation ();
12366   /* [temp.explicit] says that we are supposed to ignore access
12367      control while processing explicit instantiation directives.  */
12368   push_deferring_access_checks (dk_no_check);
12369   /* Parse a decl-specifier-seq.  */
12370   cp_parser_decl_specifier_seq (parser,
12371                                 CP_PARSER_FLAGS_OPTIONAL,
12372                                 &decl_specifiers,
12373                                 &declares_class_or_enum);
12374   /* If there was exactly one decl-specifier, and it declared a class,
12375      and there's no declarator, then we have an explicit type
12376      instantiation.  */
12377   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12378     {
12379       tree type;
12380
12381       type = check_tag_decl (&decl_specifiers);
12382       /* Turn access control back on for names used during
12383          template instantiation.  */
12384       pop_deferring_access_checks ();
12385       if (type)
12386         do_type_instantiation (type, extension_specifier,
12387                                /*complain=*/tf_error);
12388     }
12389   else
12390     {
12391       cp_declarator *declarator;
12392       tree decl;
12393
12394       /* Parse the declarator.  */
12395       declarator
12396         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12397                                 /*ctor_dtor_or_conv_p=*/NULL,
12398                                 /*parenthesized_p=*/NULL,
12399                                 /*member_p=*/false);
12400       if (declares_class_or_enum & 2)
12401         cp_parser_check_for_definition_in_return_type (declarator,
12402                                                        decl_specifiers.type,
12403                                                        decl_specifiers.type_location);
12404       if (declarator != cp_error_declarator)
12405         {
12406           if (decl_specifiers.specs[(int)ds_inline])
12407             permerror (input_location, "explicit instantiation shall not use"
12408                        " %<inline%> specifier");
12409           if (decl_specifiers.specs[(int)ds_constexpr])
12410             permerror (input_location, "explicit instantiation shall not use"
12411                        " %<constexpr%> specifier");
12412
12413           decl = grokdeclarator (declarator, &decl_specifiers,
12414                                  NORMAL, 0, &decl_specifiers.attributes);
12415           /* Turn access control back on for names used during
12416              template instantiation.  */
12417           pop_deferring_access_checks ();
12418           /* Do the explicit instantiation.  */
12419           do_decl_instantiation (decl, extension_specifier);
12420         }
12421       else
12422         {
12423           pop_deferring_access_checks ();
12424           /* Skip the body of the explicit instantiation.  */
12425           cp_parser_skip_to_end_of_statement (parser);
12426         }
12427     }
12428   /* We're done with the instantiation.  */
12429   end_explicit_instantiation ();
12430
12431   cp_parser_consume_semicolon_at_end_of_statement (parser);
12432
12433   timevar_pop (TV_TEMPLATE_INST);
12434 }
12435
12436 /* Parse an explicit-specialization.
12437
12438    explicit-specialization:
12439      template < > declaration
12440
12441    Although the standard says `declaration', what it really means is:
12442
12443    explicit-specialization:
12444      template <> decl-specifier [opt] init-declarator [opt] ;
12445      template <> function-definition
12446      template <> explicit-specialization
12447      template <> template-declaration  */
12448
12449 static void
12450 cp_parser_explicit_specialization (cp_parser* parser)
12451 {
12452   bool need_lang_pop;
12453   cp_token *token = cp_lexer_peek_token (parser->lexer);
12454
12455   /* Look for the `template' keyword.  */
12456   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12457   /* Look for the `<'.  */
12458   cp_parser_require (parser, CPP_LESS, RT_LESS);
12459   /* Look for the `>'.  */
12460   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12461   /* We have processed another parameter list.  */
12462   ++parser->num_template_parameter_lists;
12463   /* [temp]
12464
12465      A template ... explicit specialization ... shall not have C
12466      linkage.  */
12467   if (current_lang_name == lang_name_c)
12468     {
12469       error_at (token->location, "template specialization with C linkage");
12470       /* Give it C++ linkage to avoid confusing other parts of the
12471          front end.  */
12472       push_lang_context (lang_name_cplusplus);
12473       need_lang_pop = true;
12474     }
12475   else
12476     need_lang_pop = false;
12477   /* Let the front end know that we are beginning a specialization.  */
12478   if (!begin_specialization ())
12479     {
12480       end_specialization ();
12481       return;
12482     }
12483
12484   /* If the next keyword is `template', we need to figure out whether
12485      or not we're looking a template-declaration.  */
12486   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12487     {
12488       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12489           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12490         cp_parser_template_declaration_after_export (parser,
12491                                                      /*member_p=*/false);
12492       else
12493         cp_parser_explicit_specialization (parser);
12494     }
12495   else
12496     /* Parse the dependent declaration.  */
12497     cp_parser_single_declaration (parser,
12498                                   /*checks=*/NULL,
12499                                   /*member_p=*/false,
12500                                   /*explicit_specialization_p=*/true,
12501                                   /*friend_p=*/NULL);
12502   /* We're done with the specialization.  */
12503   end_specialization ();
12504   /* For the erroneous case of a template with C linkage, we pushed an
12505      implicit C++ linkage scope; exit that scope now.  */
12506   if (need_lang_pop)
12507     pop_lang_context ();
12508   /* We're done with this parameter list.  */
12509   --parser->num_template_parameter_lists;
12510 }
12511
12512 /* Parse a type-specifier.
12513
12514    type-specifier:
12515      simple-type-specifier
12516      class-specifier
12517      enum-specifier
12518      elaborated-type-specifier
12519      cv-qualifier
12520
12521    GNU Extension:
12522
12523    type-specifier:
12524      __complex__
12525
12526    Returns a representation of the type-specifier.  For a
12527    class-specifier, enum-specifier, or elaborated-type-specifier, a
12528    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12529
12530    The parser flags FLAGS is used to control type-specifier parsing.
12531
12532    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12533    in a decl-specifier-seq.
12534
12535    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12536    class-specifier, enum-specifier, or elaborated-type-specifier, then
12537    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12538    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12539    zero.
12540
12541    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12542    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12543    is set to FALSE.  */
12544
12545 static tree
12546 cp_parser_type_specifier (cp_parser* parser,
12547                           cp_parser_flags flags,
12548                           cp_decl_specifier_seq *decl_specs,
12549                           bool is_declaration,
12550                           int* declares_class_or_enum,
12551                           bool* is_cv_qualifier)
12552 {
12553   tree type_spec = NULL_TREE;
12554   cp_token *token;
12555   enum rid keyword;
12556   cp_decl_spec ds = ds_last;
12557
12558   /* Assume this type-specifier does not declare a new type.  */
12559   if (declares_class_or_enum)
12560     *declares_class_or_enum = 0;
12561   /* And that it does not specify a cv-qualifier.  */
12562   if (is_cv_qualifier)
12563     *is_cv_qualifier = false;
12564   /* Peek at the next token.  */
12565   token = cp_lexer_peek_token (parser->lexer);
12566
12567   /* If we're looking at a keyword, we can use that to guide the
12568      production we choose.  */
12569   keyword = token->keyword;
12570   switch (keyword)
12571     {
12572     case RID_ENUM:
12573       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12574         goto elaborated_type_specifier;
12575
12576       /* Look for the enum-specifier.  */
12577       type_spec = cp_parser_enum_specifier (parser);
12578       /* If that worked, we're done.  */
12579       if (type_spec)
12580         {
12581           if (declares_class_or_enum)
12582             *declares_class_or_enum = 2;
12583           if (decl_specs)
12584             cp_parser_set_decl_spec_type (decl_specs,
12585                                           type_spec,
12586                                           token->location,
12587                                           /*type_definition_p=*/true);
12588           return type_spec;
12589         }
12590       else
12591         goto elaborated_type_specifier;
12592
12593       /* Any of these indicate either a class-specifier, or an
12594          elaborated-type-specifier.  */
12595     case RID_CLASS:
12596     case RID_STRUCT:
12597     case RID_UNION:
12598       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12599         goto elaborated_type_specifier;
12600
12601       /* Parse tentatively so that we can back up if we don't find a
12602          class-specifier.  */
12603       cp_parser_parse_tentatively (parser);
12604       /* Look for the class-specifier.  */
12605       type_spec = cp_parser_class_specifier (parser);
12606       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12607       /* If that worked, we're done.  */
12608       if (cp_parser_parse_definitely (parser))
12609         {
12610           if (declares_class_or_enum)
12611             *declares_class_or_enum = 2;
12612           if (decl_specs)
12613             cp_parser_set_decl_spec_type (decl_specs,
12614                                           type_spec,
12615                                           token->location,
12616                                           /*type_definition_p=*/true);
12617           return type_spec;
12618         }
12619
12620       /* Fall through.  */
12621     elaborated_type_specifier:
12622       /* We're declaring (not defining) a class or enum.  */
12623       if (declares_class_or_enum)
12624         *declares_class_or_enum = 1;
12625
12626       /* Fall through.  */
12627     case RID_TYPENAME:
12628       /* Look for an elaborated-type-specifier.  */
12629       type_spec
12630         = (cp_parser_elaborated_type_specifier
12631            (parser,
12632             decl_specs && decl_specs->specs[(int) ds_friend],
12633             is_declaration));
12634       if (decl_specs)
12635         cp_parser_set_decl_spec_type (decl_specs,
12636                                       type_spec,
12637                                       token->location,
12638                                       /*type_definition_p=*/false);
12639       return type_spec;
12640
12641     case RID_CONST:
12642       ds = ds_const;
12643       if (is_cv_qualifier)
12644         *is_cv_qualifier = true;
12645       break;
12646
12647     case RID_VOLATILE:
12648       ds = ds_volatile;
12649       if (is_cv_qualifier)
12650         *is_cv_qualifier = true;
12651       break;
12652
12653     case RID_RESTRICT:
12654       ds = ds_restrict;
12655       if (is_cv_qualifier)
12656         *is_cv_qualifier = true;
12657       break;
12658
12659     case RID_COMPLEX:
12660       /* The `__complex__' keyword is a GNU extension.  */
12661       ds = ds_complex;
12662       break;
12663
12664     default:
12665       break;
12666     }
12667
12668   /* Handle simple keywords.  */
12669   if (ds != ds_last)
12670     {
12671       if (decl_specs)
12672         {
12673           ++decl_specs->specs[(int)ds];
12674           decl_specs->any_specifiers_p = true;
12675         }
12676       return cp_lexer_consume_token (parser->lexer)->u.value;
12677     }
12678
12679   /* If we do not already have a type-specifier, assume we are looking
12680      at a simple-type-specifier.  */
12681   type_spec = cp_parser_simple_type_specifier (parser,
12682                                                decl_specs,
12683                                                flags);
12684
12685   /* If we didn't find a type-specifier, and a type-specifier was not
12686      optional in this context, issue an error message.  */
12687   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12688     {
12689       cp_parser_error (parser, "expected type specifier");
12690       return error_mark_node;
12691     }
12692
12693   return type_spec;
12694 }
12695
12696 /* Parse a simple-type-specifier.
12697
12698    simple-type-specifier:
12699      :: [opt] nested-name-specifier [opt] type-name
12700      :: [opt] nested-name-specifier template template-id
12701      char
12702      wchar_t
12703      bool
12704      short
12705      int
12706      long
12707      signed
12708      unsigned
12709      float
12710      double
12711      void
12712
12713    C++0x Extension:
12714
12715    simple-type-specifier:
12716      auto
12717      decltype ( expression )   
12718      char16_t
12719      char32_t
12720      __underlying_type ( type-id )
12721
12722    GNU Extension:
12723
12724    simple-type-specifier:
12725      __int128
12726      __typeof__ unary-expression
12727      __typeof__ ( type-id )
12728
12729    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12730    appropriately updated.  */
12731
12732 static tree
12733 cp_parser_simple_type_specifier (cp_parser* parser,
12734                                  cp_decl_specifier_seq *decl_specs,
12735                                  cp_parser_flags flags)
12736 {
12737   tree type = NULL_TREE;
12738   cp_token *token;
12739
12740   /* Peek at the next token.  */
12741   token = cp_lexer_peek_token (parser->lexer);
12742
12743   /* If we're looking at a keyword, things are easy.  */
12744   switch (token->keyword)
12745     {
12746     case RID_CHAR:
12747       if (decl_specs)
12748         decl_specs->explicit_char_p = true;
12749       type = char_type_node;
12750       break;
12751     case RID_CHAR16:
12752       type = char16_type_node;
12753       break;
12754     case RID_CHAR32:
12755       type = char32_type_node;
12756       break;
12757     case RID_WCHAR:
12758       type = wchar_type_node;
12759       break;
12760     case RID_BOOL:
12761       type = boolean_type_node;
12762       break;
12763     case RID_SHORT:
12764       if (decl_specs)
12765         ++decl_specs->specs[(int) ds_short];
12766       type = short_integer_type_node;
12767       break;
12768     case RID_INT:
12769       if (decl_specs)
12770         decl_specs->explicit_int_p = true;
12771       type = integer_type_node;
12772       break;
12773     case RID_INT128:
12774       if (!int128_integer_type_node)
12775         break;
12776       if (decl_specs)
12777         decl_specs->explicit_int128_p = true;
12778       type = int128_integer_type_node;
12779       break;
12780     case RID_LONG:
12781       if (decl_specs)
12782         ++decl_specs->specs[(int) ds_long];
12783       type = long_integer_type_node;
12784       break;
12785     case RID_SIGNED:
12786       if (decl_specs)
12787         ++decl_specs->specs[(int) ds_signed];
12788       type = integer_type_node;
12789       break;
12790     case RID_UNSIGNED:
12791       if (decl_specs)
12792         ++decl_specs->specs[(int) ds_unsigned];
12793       type = unsigned_type_node;
12794       break;
12795     case RID_FLOAT:
12796       type = float_type_node;
12797       break;
12798     case RID_DOUBLE:
12799       type = double_type_node;
12800       break;
12801     case RID_VOID:
12802       type = void_type_node;
12803       break;
12804       
12805     case RID_AUTO:
12806       maybe_warn_cpp0x (CPP0X_AUTO);
12807       type = make_auto ();
12808       break;
12809
12810     case RID_DECLTYPE:
12811       /* Since DR 743, decltype can either be a simple-type-specifier by
12812          itself or begin a nested-name-specifier.  Parsing it will replace
12813          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
12814          handling below decide what to do.  */
12815       cp_parser_decltype (parser);
12816       cp_lexer_set_token_position (parser->lexer, token);
12817       break;
12818
12819     case RID_TYPEOF:
12820       /* Consume the `typeof' token.  */
12821       cp_lexer_consume_token (parser->lexer);
12822       /* Parse the operand to `typeof'.  */
12823       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12824       /* If it is not already a TYPE, take its type.  */
12825       if (!TYPE_P (type))
12826         type = finish_typeof (type);
12827
12828       if (decl_specs)
12829         cp_parser_set_decl_spec_type (decl_specs, type,
12830                                       token->location,
12831                                       /*type_definition_p=*/false);
12832
12833       return type;
12834
12835     case RID_UNDERLYING_TYPE:
12836       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12837
12838       if (decl_specs)
12839         cp_parser_set_decl_spec_type (decl_specs, type,
12840                                       token->location,
12841                                       /*type_definition_p=*/false);
12842
12843       return type;
12844
12845     default:
12846       break;
12847     }
12848
12849   /* If token is an already-parsed decltype not followed by ::,
12850      it's a simple-type-specifier.  */
12851   if (token->type == CPP_DECLTYPE
12852       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
12853     {
12854       type = token->u.value;
12855       if (decl_specs)
12856         cp_parser_set_decl_spec_type (decl_specs, type,
12857                                       token->location,
12858                                       /*type_definition_p=*/false);
12859       cp_lexer_consume_token (parser->lexer);
12860       return type;
12861     }
12862
12863   /* If the type-specifier was for a built-in type, we're done.  */
12864   if (type)
12865     {
12866       /* Record the type.  */
12867       if (decl_specs
12868           && (token->keyword != RID_SIGNED
12869               && token->keyword != RID_UNSIGNED
12870               && token->keyword != RID_SHORT
12871               && token->keyword != RID_LONG))
12872         cp_parser_set_decl_spec_type (decl_specs,
12873                                       type,
12874                                       token->location,
12875                                       /*type_definition_p=*/false);
12876       if (decl_specs)
12877         decl_specs->any_specifiers_p = true;
12878
12879       /* Consume the token.  */
12880       cp_lexer_consume_token (parser->lexer);
12881
12882       /* There is no valid C++ program where a non-template type is
12883          followed by a "<".  That usually indicates that the user thought
12884          that the type was a template.  */
12885       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12886
12887       return TYPE_NAME (type);
12888     }
12889
12890   /* The type-specifier must be a user-defined type.  */
12891   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12892     {
12893       bool qualified_p;
12894       bool global_p;
12895
12896       /* Don't gobble tokens or issue error messages if this is an
12897          optional type-specifier.  */
12898       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12899         cp_parser_parse_tentatively (parser);
12900
12901       /* Look for the optional `::' operator.  */
12902       global_p
12903         = (cp_parser_global_scope_opt (parser,
12904                                        /*current_scope_valid_p=*/false)
12905            != NULL_TREE);
12906       /* Look for the nested-name specifier.  */
12907       qualified_p
12908         = (cp_parser_nested_name_specifier_opt (parser,
12909                                                 /*typename_keyword_p=*/false,
12910                                                 /*check_dependency_p=*/true,
12911                                                 /*type_p=*/false,
12912                                                 /*is_declaration=*/false)
12913            != NULL_TREE);
12914       token = cp_lexer_peek_token (parser->lexer);
12915       /* If we have seen a nested-name-specifier, and the next token
12916          is `template', then we are using the template-id production.  */
12917       if (parser->scope
12918           && cp_parser_optional_template_keyword (parser))
12919         {
12920           /* Look for the template-id.  */
12921           type = cp_parser_template_id (parser,
12922                                         /*template_keyword_p=*/true,
12923                                         /*check_dependency_p=*/true,
12924                                         /*is_declaration=*/false);
12925           /* If the template-id did not name a type, we are out of
12926              luck.  */
12927           if (TREE_CODE (type) != TYPE_DECL)
12928             {
12929               cp_parser_error (parser, "expected template-id for type");
12930               type = NULL_TREE;
12931             }
12932         }
12933       /* Otherwise, look for a type-name.  */
12934       else
12935         type = cp_parser_type_name (parser);
12936       /* Keep track of all name-lookups performed in class scopes.  */
12937       if (type
12938           && !global_p
12939           && !qualified_p
12940           && TREE_CODE (type) == TYPE_DECL
12941           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12942         maybe_note_name_used_in_class (DECL_NAME (type), type);
12943       /* If it didn't work out, we don't have a TYPE.  */
12944       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12945           && !cp_parser_parse_definitely (parser))
12946         type = NULL_TREE;
12947       if (type && decl_specs)
12948         cp_parser_set_decl_spec_type (decl_specs, type,
12949                                       token->location,
12950                                       /*type_definition_p=*/false);
12951     }
12952
12953   /* If we didn't get a type-name, issue an error message.  */
12954   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12955     {
12956       cp_parser_error (parser, "expected type-name");
12957       return error_mark_node;
12958     }
12959
12960   if (type && type != error_mark_node)
12961     {
12962       /* See if TYPE is an Objective-C type, and if so, parse and
12963          accept any protocol references following it.  Do this before
12964          the cp_parser_check_for_invalid_template_id() call, because
12965          Objective-C types can be followed by '<...>' which would
12966          enclose protocol names rather than template arguments, and so
12967          everything is fine.  */
12968       if (c_dialect_objc () && !parser->scope
12969           && (objc_is_id (type) || objc_is_class_name (type)))
12970         {
12971           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12972           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12973
12974           /* Clobber the "unqualified" type previously entered into
12975              DECL_SPECS with the new, improved protocol-qualified version.  */
12976           if (decl_specs)
12977             decl_specs->type = qual_type;
12978
12979           return qual_type;
12980         }
12981
12982       /* There is no valid C++ program where a non-template type is
12983          followed by a "<".  That usually indicates that the user
12984          thought that the type was a template.  */
12985       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12986                                                token->location);
12987     }
12988
12989   return type;
12990 }
12991
12992 /* Parse a type-name.
12993
12994    type-name:
12995      class-name
12996      enum-name
12997      typedef-name
12998
12999    enum-name:
13000      identifier
13001
13002    typedef-name:
13003      identifier
13004
13005    Returns a TYPE_DECL for the type.  */
13006
13007 static tree
13008 cp_parser_type_name (cp_parser* parser)
13009 {
13010   tree type_decl;
13011
13012   /* We can't know yet whether it is a class-name or not.  */
13013   cp_parser_parse_tentatively (parser);
13014   /* Try a class-name.  */
13015   type_decl = cp_parser_class_name (parser,
13016                                     /*typename_keyword_p=*/false,
13017                                     /*template_keyword_p=*/false,
13018                                     none_type,
13019                                     /*check_dependency_p=*/true,
13020                                     /*class_head_p=*/false,
13021                                     /*is_declaration=*/false);
13022   /* If it's not a class-name, keep looking.  */
13023   if (!cp_parser_parse_definitely (parser))
13024     {
13025       /* It must be a typedef-name or an enum-name.  */
13026       return cp_parser_nonclass_name (parser);
13027     }
13028
13029   return type_decl;
13030 }
13031
13032 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13033
13034    enum-name:
13035      identifier
13036
13037    typedef-name:
13038      identifier
13039
13040    Returns a TYPE_DECL for the type.  */
13041
13042 static tree
13043 cp_parser_nonclass_name (cp_parser* parser)
13044 {
13045   tree type_decl;
13046   tree identifier;
13047
13048   cp_token *token = cp_lexer_peek_token (parser->lexer);
13049   identifier = cp_parser_identifier (parser);
13050   if (identifier == error_mark_node)
13051     return error_mark_node;
13052
13053   /* Look up the type-name.  */
13054   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13055
13056   if (TREE_CODE (type_decl) != TYPE_DECL
13057       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13058     {
13059       /* See if this is an Objective-C type.  */
13060       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13061       tree type = objc_get_protocol_qualified_type (identifier, protos);
13062       if (type)
13063         type_decl = TYPE_NAME (type);
13064     }
13065
13066   /* Issue an error if we did not find a type-name.  */
13067   if (TREE_CODE (type_decl) != TYPE_DECL
13068       /* In Objective-C, we have the complication that class names are
13069          normally type names and start declarations (eg, the
13070          "NSObject" in "NSObject *object;"), but can be used in an
13071          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13072          is an expression.  So, a classname followed by a dot is not a
13073          valid type-name.  */
13074       || (objc_is_class_name (TREE_TYPE (type_decl))
13075           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13076     {
13077       if (!cp_parser_simulate_error (parser))
13078         cp_parser_name_lookup_error (parser, identifier, type_decl,
13079                                      NLE_TYPE, token->location);
13080       return error_mark_node;
13081     }
13082   /* Remember that the name was used in the definition of the
13083      current class so that we can check later to see if the
13084      meaning would have been different after the class was
13085      entirely defined.  */
13086   else if (type_decl != error_mark_node
13087            && !parser->scope)
13088     maybe_note_name_used_in_class (identifier, type_decl);
13089   
13090   return type_decl;
13091 }
13092
13093 /* Parse an elaborated-type-specifier.  Note that the grammar given
13094    here incorporates the resolution to DR68.
13095
13096    elaborated-type-specifier:
13097      class-key :: [opt] nested-name-specifier [opt] identifier
13098      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13099      enum-key :: [opt] nested-name-specifier [opt] identifier
13100      typename :: [opt] nested-name-specifier identifier
13101      typename :: [opt] nested-name-specifier template [opt]
13102        template-id
13103
13104    GNU extension:
13105
13106    elaborated-type-specifier:
13107      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13108      class-key attributes :: [opt] nested-name-specifier [opt]
13109                template [opt] template-id
13110      enum attributes :: [opt] nested-name-specifier [opt] identifier
13111
13112    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13113    declared `friend'.  If IS_DECLARATION is TRUE, then this
13114    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13115    something is being declared.
13116
13117    Returns the TYPE specified.  */
13118
13119 static tree
13120 cp_parser_elaborated_type_specifier (cp_parser* parser,
13121                                      bool is_friend,
13122                                      bool is_declaration)
13123 {
13124   enum tag_types tag_type;
13125   tree identifier;
13126   tree type = NULL_TREE;
13127   tree attributes = NULL_TREE;
13128   tree globalscope;
13129   cp_token *token = NULL;
13130
13131   /* See if we're looking at the `enum' keyword.  */
13132   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13133     {
13134       /* Consume the `enum' token.  */
13135       cp_lexer_consume_token (parser->lexer);
13136       /* Remember that it's an enumeration type.  */
13137       tag_type = enum_type;
13138       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13139          enums) is used here.  */
13140       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13141           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13142         {
13143             pedwarn (input_location, 0, "elaborated-type-specifier "
13144                       "for a scoped enum must not use the %<%D%> keyword",
13145                       cp_lexer_peek_token (parser->lexer)->u.value);
13146           /* Consume the `struct' or `class' and parse it anyway.  */
13147           cp_lexer_consume_token (parser->lexer);
13148         }
13149       /* Parse the attributes.  */
13150       attributes = cp_parser_attributes_opt (parser);
13151     }
13152   /* Or, it might be `typename'.  */
13153   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13154                                            RID_TYPENAME))
13155     {
13156       /* Consume the `typename' token.  */
13157       cp_lexer_consume_token (parser->lexer);
13158       /* Remember that it's a `typename' type.  */
13159       tag_type = typename_type;
13160     }
13161   /* Otherwise it must be a class-key.  */
13162   else
13163     {
13164       tag_type = cp_parser_class_key (parser);
13165       if (tag_type == none_type)
13166         return error_mark_node;
13167       /* Parse the attributes.  */
13168       attributes = cp_parser_attributes_opt (parser);
13169     }
13170
13171   /* Look for the `::' operator.  */
13172   globalscope =  cp_parser_global_scope_opt (parser,
13173                                              /*current_scope_valid_p=*/false);
13174   /* Look for the nested-name-specifier.  */
13175   if (tag_type == typename_type && !globalscope)
13176     {
13177       if (!cp_parser_nested_name_specifier (parser,
13178                                            /*typename_keyword_p=*/true,
13179                                            /*check_dependency_p=*/true,
13180                                            /*type_p=*/true,
13181                                             is_declaration))
13182         return error_mark_node;
13183     }
13184   else
13185     /* Even though `typename' is not present, the proposed resolution
13186        to Core Issue 180 says that in `class A<T>::B', `B' should be
13187        considered a type-name, even if `A<T>' is dependent.  */
13188     cp_parser_nested_name_specifier_opt (parser,
13189                                          /*typename_keyword_p=*/true,
13190                                          /*check_dependency_p=*/true,
13191                                          /*type_p=*/true,
13192                                          is_declaration);
13193  /* For everything but enumeration types, consider a template-id.
13194     For an enumeration type, consider only a plain identifier.  */
13195   if (tag_type != enum_type)
13196     {
13197       bool template_p = false;
13198       tree decl;
13199
13200       /* Allow the `template' keyword.  */
13201       template_p = cp_parser_optional_template_keyword (parser);
13202       /* If we didn't see `template', we don't know if there's a
13203          template-id or not.  */
13204       if (!template_p)
13205         cp_parser_parse_tentatively (parser);
13206       /* Parse the template-id.  */
13207       token = cp_lexer_peek_token (parser->lexer);
13208       decl = cp_parser_template_id (parser, template_p,
13209                                     /*check_dependency_p=*/true,
13210                                     is_declaration);
13211       /* If we didn't find a template-id, look for an ordinary
13212          identifier.  */
13213       if (!template_p && !cp_parser_parse_definitely (parser))
13214         ;
13215       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13216          in effect, then we must assume that, upon instantiation, the
13217          template will correspond to a class.  */
13218       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13219                && tag_type == typename_type)
13220         type = make_typename_type (parser->scope, decl,
13221                                    typename_type,
13222                                    /*complain=*/tf_error);
13223       /* If the `typename' keyword is in effect and DECL is not a type
13224          decl. Then type is non existant.   */
13225       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13226         type = NULL_TREE; 
13227       else 
13228         type = TREE_TYPE (decl);
13229     }
13230
13231   if (!type)
13232     {
13233       token = cp_lexer_peek_token (parser->lexer);
13234       identifier = cp_parser_identifier (parser);
13235
13236       if (identifier == error_mark_node)
13237         {
13238           parser->scope = NULL_TREE;
13239           return error_mark_node;
13240         }
13241
13242       /* For a `typename', we needn't call xref_tag.  */
13243       if (tag_type == typename_type
13244           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13245         return cp_parser_make_typename_type (parser, parser->scope,
13246                                              identifier,
13247                                              token->location);
13248       /* Look up a qualified name in the usual way.  */
13249       if (parser->scope)
13250         {
13251           tree decl;
13252           tree ambiguous_decls;
13253
13254           decl = cp_parser_lookup_name (parser, identifier,
13255                                         tag_type,
13256                                         /*is_template=*/false,
13257                                         /*is_namespace=*/false,
13258                                         /*check_dependency=*/true,
13259                                         &ambiguous_decls,
13260                                         token->location);
13261
13262           /* If the lookup was ambiguous, an error will already have been
13263              issued.  */
13264           if (ambiguous_decls)
13265             return error_mark_node;
13266
13267           /* If we are parsing friend declaration, DECL may be a
13268              TEMPLATE_DECL tree node here.  However, we need to check
13269              whether this TEMPLATE_DECL results in valid code.  Consider
13270              the following example:
13271
13272                namespace N {
13273                  template <class T> class C {};
13274                }
13275                class X {
13276                  template <class T> friend class N::C; // #1, valid code
13277                };
13278                template <class T> class Y {
13279                  friend class N::C;                    // #2, invalid code
13280                };
13281
13282              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13283              name lookup of `N::C'.  We see that friend declaration must
13284              be template for the code to be valid.  Note that
13285              processing_template_decl does not work here since it is
13286              always 1 for the above two cases.  */
13287
13288           decl = (cp_parser_maybe_treat_template_as_class
13289                   (decl, /*tag_name_p=*/is_friend
13290                          && parser->num_template_parameter_lists));
13291
13292           if (TREE_CODE (decl) != TYPE_DECL)
13293             {
13294               cp_parser_diagnose_invalid_type_name (parser,
13295                                                     parser->scope,
13296                                                     identifier,
13297                                                     token->location);
13298               return error_mark_node;
13299             }
13300
13301           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13302             {
13303               bool allow_template = (parser->num_template_parameter_lists
13304                                       || DECL_SELF_REFERENCE_P (decl));
13305               type = check_elaborated_type_specifier (tag_type, decl, 
13306                                                       allow_template);
13307
13308               if (type == error_mark_node)
13309                 return error_mark_node;
13310             }
13311
13312           /* Forward declarations of nested types, such as
13313
13314                class C1::C2;
13315                class C1::C2::C3;
13316
13317              are invalid unless all components preceding the final '::'
13318              are complete.  If all enclosing types are complete, these
13319              declarations become merely pointless.
13320
13321              Invalid forward declarations of nested types are errors
13322              caught elsewhere in parsing.  Those that are pointless arrive
13323              here.  */
13324
13325           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13326               && !is_friend && !processing_explicit_instantiation)
13327             warning (0, "declaration %qD does not declare anything", decl);
13328
13329           type = TREE_TYPE (decl);
13330         }
13331       else
13332         {
13333           /* An elaborated-type-specifier sometimes introduces a new type and
13334              sometimes names an existing type.  Normally, the rule is that it
13335              introduces a new type only if there is not an existing type of
13336              the same name already in scope.  For example, given:
13337
13338                struct S {};
13339                void f() { struct S s; }
13340
13341              the `struct S' in the body of `f' is the same `struct S' as in
13342              the global scope; the existing definition is used.  However, if
13343              there were no global declaration, this would introduce a new
13344              local class named `S'.
13345
13346              An exception to this rule applies to the following code:
13347
13348                namespace N { struct S; }
13349
13350              Here, the elaborated-type-specifier names a new type
13351              unconditionally; even if there is already an `S' in the
13352              containing scope this declaration names a new type.
13353              This exception only applies if the elaborated-type-specifier
13354              forms the complete declaration:
13355
13356                [class.name]
13357
13358                A declaration consisting solely of `class-key identifier ;' is
13359                either a redeclaration of the name in the current scope or a
13360                forward declaration of the identifier as a class name.  It
13361                introduces the name into the current scope.
13362
13363              We are in this situation precisely when the next token is a `;'.
13364
13365              An exception to the exception is that a `friend' declaration does
13366              *not* name a new type; i.e., given:
13367
13368                struct S { friend struct T; };
13369
13370              `T' is not a new type in the scope of `S'.
13371
13372              Also, `new struct S' or `sizeof (struct S)' never results in the
13373              definition of a new type; a new type can only be declared in a
13374              declaration context.  */
13375
13376           tag_scope ts;
13377           bool template_p;
13378
13379           if (is_friend)
13380             /* Friends have special name lookup rules.  */
13381             ts = ts_within_enclosing_non_class;
13382           else if (is_declaration
13383                    && cp_lexer_next_token_is (parser->lexer,
13384                                               CPP_SEMICOLON))
13385             /* This is a `class-key identifier ;' */
13386             ts = ts_current;
13387           else
13388             ts = ts_global;
13389
13390           template_p =
13391             (parser->num_template_parameter_lists
13392              && (cp_parser_next_token_starts_class_definition_p (parser)
13393                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13394           /* An unqualified name was used to reference this type, so
13395              there were no qualifying templates.  */
13396           if (!cp_parser_check_template_parameters (parser,
13397                                                     /*num_templates=*/0,
13398                                                     token->location,
13399                                                     /*declarator=*/NULL))
13400             return error_mark_node;
13401           type = xref_tag (tag_type, identifier, ts, template_p);
13402         }
13403     }
13404
13405   if (type == error_mark_node)
13406     return error_mark_node;
13407
13408   /* Allow attributes on forward declarations of classes.  */
13409   if (attributes)
13410     {
13411       if (TREE_CODE (type) == TYPENAME_TYPE)
13412         warning (OPT_Wattributes,
13413                  "attributes ignored on uninstantiated type");
13414       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13415                && ! processing_explicit_instantiation)
13416         warning (OPT_Wattributes,
13417                  "attributes ignored on template instantiation");
13418       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13419         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13420       else
13421         warning (OPT_Wattributes,
13422                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13423     }
13424
13425   if (tag_type != enum_type)
13426     {
13427       /* Indicate whether this class was declared as a `class' or as a
13428          `struct'.  */
13429       if (TREE_CODE (type) == RECORD_TYPE)
13430         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
13431       cp_parser_check_class_key (tag_type, type);
13432     }
13433
13434   /* A "<" cannot follow an elaborated type specifier.  If that
13435      happens, the user was probably trying to form a template-id.  */
13436   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13437
13438   return type;
13439 }
13440
13441 /* Parse an enum-specifier.
13442
13443    enum-specifier:
13444      enum-head { enumerator-list [opt] }
13445
13446    enum-head:
13447      enum-key identifier [opt] enum-base [opt]
13448      enum-key nested-name-specifier identifier enum-base [opt]
13449
13450    enum-key:
13451      enum
13452      enum class   [C++0x]
13453      enum struct  [C++0x]
13454
13455    enum-base:   [C++0x]
13456      : type-specifier-seq
13457
13458    opaque-enum-specifier:
13459      enum-key identifier enum-base [opt] ;
13460
13461    GNU Extensions:
13462      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13463        { enumerator-list [opt] }attributes[opt]
13464
13465    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13466    if the token stream isn't an enum-specifier after all.  */
13467
13468 static tree
13469 cp_parser_enum_specifier (cp_parser* parser)
13470 {
13471   tree identifier;
13472   tree type = NULL_TREE;
13473   tree prev_scope;
13474   tree nested_name_specifier = NULL_TREE;
13475   tree attributes;
13476   bool scoped_enum_p = false;
13477   bool has_underlying_type = false;
13478   bool nested_being_defined = false;
13479   bool new_value_list = false;
13480   bool is_new_type = false;
13481   bool is_anonymous = false;
13482   tree underlying_type = NULL_TREE;
13483   cp_token *type_start_token = NULL;
13484   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13485
13486   parser->colon_corrects_to_scope_p = false;
13487
13488   /* Parse tentatively so that we can back up if we don't find a
13489      enum-specifier.  */
13490   cp_parser_parse_tentatively (parser);
13491
13492   /* Caller guarantees that the current token is 'enum', an identifier
13493      possibly follows, and the token after that is an opening brace.
13494      If we don't have an identifier, fabricate an anonymous name for
13495      the enumeration being defined.  */
13496   cp_lexer_consume_token (parser->lexer);
13497
13498   /* Parse the "class" or "struct", which indicates a scoped
13499      enumeration type in C++0x.  */
13500   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13501       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13502     {
13503       if (cxx_dialect < cxx0x)
13504         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13505
13506       /* Consume the `struct' or `class' token.  */
13507       cp_lexer_consume_token (parser->lexer);
13508
13509       scoped_enum_p = true;
13510     }
13511
13512   attributes = cp_parser_attributes_opt (parser);
13513
13514   /* Clear the qualification.  */
13515   parser->scope = NULL_TREE;
13516   parser->qualifying_scope = NULL_TREE;
13517   parser->object_scope = NULL_TREE;
13518
13519   /* Figure out in what scope the declaration is being placed.  */
13520   prev_scope = current_scope ();
13521
13522   type_start_token = cp_lexer_peek_token (parser->lexer);
13523
13524   push_deferring_access_checks (dk_no_check);
13525   nested_name_specifier
13526       = cp_parser_nested_name_specifier_opt (parser,
13527                                              /*typename_keyword_p=*/true,
13528                                              /*check_dependency_p=*/false,
13529                                              /*type_p=*/false,
13530                                              /*is_declaration=*/false);
13531
13532   if (nested_name_specifier)
13533     {
13534       tree name;
13535
13536       identifier = cp_parser_identifier (parser);
13537       name =  cp_parser_lookup_name (parser, identifier,
13538                                      enum_type,
13539                                      /*is_template=*/false,
13540                                      /*is_namespace=*/false,
13541                                      /*check_dependency=*/true,
13542                                      /*ambiguous_decls=*/NULL,
13543                                      input_location);
13544       if (name)
13545         {
13546           type = TREE_TYPE (name);
13547           if (TREE_CODE (type) == TYPENAME_TYPE)
13548             {
13549               /* Are template enums allowed in ISO? */
13550               if (template_parm_scope_p ())
13551                 pedwarn (type_start_token->location, OPT_pedantic,
13552                          "%qD is an enumeration template", name);
13553               /* ignore a typename reference, for it will be solved by name
13554                  in start_enum.  */
13555               type = NULL_TREE;
13556             }
13557         }
13558       else
13559         error_at (type_start_token->location,
13560                   "%qD is not an enumerator-name", identifier);
13561     }
13562   else
13563     {
13564       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13565         identifier = cp_parser_identifier (parser);
13566       else
13567         {
13568           identifier = make_anon_name ();
13569           is_anonymous = true;
13570         }
13571     }
13572   pop_deferring_access_checks ();
13573
13574   /* Check for the `:' that denotes a specified underlying type in C++0x.
13575      Note that a ':' could also indicate a bitfield width, however.  */
13576   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13577     {
13578       cp_decl_specifier_seq type_specifiers;
13579
13580       /* Consume the `:'.  */
13581       cp_lexer_consume_token (parser->lexer);
13582
13583       /* Parse the type-specifier-seq.  */
13584       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13585                                     /*is_trailing_return=*/false,
13586                                     &type_specifiers);
13587
13588       /* At this point this is surely not elaborated type specifier.  */
13589       if (!cp_parser_parse_definitely (parser))
13590         return NULL_TREE;
13591
13592       if (cxx_dialect < cxx0x)
13593         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13594
13595       has_underlying_type = true;
13596
13597       /* If that didn't work, stop.  */
13598       if (type_specifiers.type != error_mark_node)
13599         {
13600           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13601                                             /*initialized=*/0, NULL);
13602           if (underlying_type == error_mark_node)
13603             underlying_type = NULL_TREE;
13604         }
13605     }
13606
13607   /* Look for the `{' but don't consume it yet.  */
13608   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13609     {
13610       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13611         {
13612           cp_parser_error (parser, "expected %<{%>");
13613           if (has_underlying_type)
13614             {
13615               type = NULL_TREE;
13616               goto out;
13617             }
13618         }
13619       /* An opaque-enum-specifier must have a ';' here.  */
13620       if ((scoped_enum_p || underlying_type)
13621           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13622         {
13623           cp_parser_error (parser, "expected %<;%> or %<{%>");
13624           if (has_underlying_type)
13625             {
13626               type = NULL_TREE;
13627               goto out;
13628             }
13629         }
13630     }
13631
13632   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13633     return NULL_TREE;
13634
13635   if (nested_name_specifier)
13636     {
13637       if (CLASS_TYPE_P (nested_name_specifier))
13638         {
13639           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13640           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13641           push_scope (nested_name_specifier);
13642         }
13643       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13644         {
13645           push_nested_namespace (nested_name_specifier);
13646         }
13647     }
13648
13649   /* Issue an error message if type-definitions are forbidden here.  */
13650   if (!cp_parser_check_type_definition (parser))
13651     type = error_mark_node;
13652   else
13653     /* Create the new type.  We do this before consuming the opening
13654        brace so the enum will be recorded as being on the line of its
13655        tag (or the 'enum' keyword, if there is no tag).  */
13656     type = start_enum (identifier, type, underlying_type,
13657                        scoped_enum_p, &is_new_type);
13658
13659   /* If the next token is not '{' it is an opaque-enum-specifier or an
13660      elaborated-type-specifier.  */
13661   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13662     {
13663       timevar_push (TV_PARSE_ENUM);
13664       if (nested_name_specifier)
13665         {
13666           /* The following catches invalid code such as:
13667              enum class S<int>::E { A, B, C }; */
13668           if (!processing_specialization
13669               && CLASS_TYPE_P (nested_name_specifier)
13670               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13671             error_at (type_start_token->location, "cannot add an enumerator "
13672                       "list to a template instantiation");
13673
13674           /* If that scope does not contain the scope in which the
13675              class was originally declared, the program is invalid.  */
13676           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13677             {
13678               if (at_namespace_scope_p ())
13679                 error_at (type_start_token->location,
13680                           "declaration of %qD in namespace %qD which does not "
13681                           "enclose %qD",
13682                           type, prev_scope, nested_name_specifier);
13683               else
13684                 error_at (type_start_token->location,
13685                           "declaration of %qD in %qD which does not enclose %qD",
13686                           type, prev_scope, nested_name_specifier);
13687               type = error_mark_node;
13688             }
13689         }
13690
13691       if (scoped_enum_p)
13692         begin_scope (sk_scoped_enum, type);
13693
13694       /* Consume the opening brace.  */
13695       cp_lexer_consume_token (parser->lexer);
13696
13697       if (type == error_mark_node)
13698         ; /* Nothing to add */
13699       else if (OPAQUE_ENUM_P (type)
13700                || (cxx_dialect > cxx98 && processing_specialization))
13701         {
13702           new_value_list = true;
13703           SET_OPAQUE_ENUM_P (type, false);
13704           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13705         }
13706       else
13707         {
13708           error_at (type_start_token->location, "multiple definition of %q#T", type);
13709           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13710                     "previous definition here");
13711           type = error_mark_node;
13712         }
13713
13714       if (type == error_mark_node)
13715         cp_parser_skip_to_end_of_block_or_statement (parser);
13716       /* If the next token is not '}', then there are some enumerators.  */
13717       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13718         cp_parser_enumerator_list (parser, type);
13719
13720       /* Consume the final '}'.  */
13721       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13722
13723       if (scoped_enum_p)
13724         finish_scope ();
13725       timevar_pop (TV_PARSE_ENUM);
13726     }
13727   else
13728     {
13729       /* If a ';' follows, then it is an opaque-enum-specifier
13730         and additional restrictions apply.  */
13731       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13732         {
13733           if (is_anonymous)
13734             error_at (type_start_token->location,
13735                       "opaque-enum-specifier without name");
13736           else if (nested_name_specifier)
13737             error_at (type_start_token->location,
13738                       "opaque-enum-specifier must use a simple identifier");
13739         }
13740     }
13741
13742   /* Look for trailing attributes to apply to this enumeration, and
13743      apply them if appropriate.  */
13744   if (cp_parser_allow_gnu_extensions_p (parser))
13745     {
13746       tree trailing_attr = cp_parser_attributes_opt (parser);
13747       trailing_attr = chainon (trailing_attr, attributes);
13748       cplus_decl_attributes (&type,
13749                              trailing_attr,
13750                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13751     }
13752
13753   /* Finish up the enumeration.  */
13754   if (type != error_mark_node)
13755     {
13756       if (new_value_list)
13757         finish_enum_value_list (type);
13758       if (is_new_type)
13759         finish_enum (type);
13760     }
13761
13762   if (nested_name_specifier)
13763     {
13764       if (CLASS_TYPE_P (nested_name_specifier))
13765         {
13766           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13767           pop_scope (nested_name_specifier);
13768         }
13769       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13770         {
13771           pop_nested_namespace (nested_name_specifier);
13772         }
13773     }
13774  out:
13775   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13776   return type;
13777 }
13778
13779 /* Parse an enumerator-list.  The enumerators all have the indicated
13780    TYPE.
13781
13782    enumerator-list:
13783      enumerator-definition
13784      enumerator-list , enumerator-definition  */
13785
13786 static void
13787 cp_parser_enumerator_list (cp_parser* parser, tree type)
13788 {
13789   while (true)
13790     {
13791       /* Parse an enumerator-definition.  */
13792       cp_parser_enumerator_definition (parser, type);
13793
13794       /* If the next token is not a ',', we've reached the end of
13795          the list.  */
13796       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13797         break;
13798       /* Otherwise, consume the `,' and keep going.  */
13799       cp_lexer_consume_token (parser->lexer);
13800       /* If the next token is a `}', there is a trailing comma.  */
13801       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13802         {
13803           if (!in_system_header)
13804             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13805           break;
13806         }
13807     }
13808 }
13809
13810 /* Parse an enumerator-definition.  The enumerator has the indicated
13811    TYPE.
13812
13813    enumerator-definition:
13814      enumerator
13815      enumerator = constant-expression
13816
13817    enumerator:
13818      identifier  */
13819
13820 static void
13821 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13822 {
13823   tree identifier;
13824   tree value;
13825   location_t loc;
13826
13827   /* Save the input location because we are interested in the location
13828      of the identifier and not the location of the explicit value.  */
13829   loc = cp_lexer_peek_token (parser->lexer)->location;
13830
13831   /* Look for the identifier.  */
13832   identifier = cp_parser_identifier (parser);
13833   if (identifier == error_mark_node)
13834     return;
13835
13836   /* If the next token is an '=', then there is an explicit value.  */
13837   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13838     {
13839       /* Consume the `=' token.  */
13840       cp_lexer_consume_token (parser->lexer);
13841       /* Parse the value.  */
13842       value = cp_parser_constant_expression (parser,
13843                                              /*allow_non_constant_p=*/false,
13844                                              NULL);
13845     }
13846   else
13847     value = NULL_TREE;
13848
13849   /* If we are processing a template, make sure the initializer of the
13850      enumerator doesn't contain any bare template parameter pack.  */
13851   if (check_for_bare_parameter_packs (value))
13852     value = error_mark_node;
13853
13854   /* integral_constant_value will pull out this expression, so make sure
13855      it's folded as appropriate.  */
13856   value = fold_non_dependent_expr (value);
13857
13858   /* Create the enumerator.  */
13859   build_enumerator (identifier, value, type, loc);
13860 }
13861
13862 /* Parse a namespace-name.
13863
13864    namespace-name:
13865      original-namespace-name
13866      namespace-alias
13867
13868    Returns the NAMESPACE_DECL for the namespace.  */
13869
13870 static tree
13871 cp_parser_namespace_name (cp_parser* parser)
13872 {
13873   tree identifier;
13874   tree namespace_decl;
13875
13876   cp_token *token = cp_lexer_peek_token (parser->lexer);
13877
13878   /* Get the name of the namespace.  */
13879   identifier = cp_parser_identifier (parser);
13880   if (identifier == error_mark_node)
13881     return error_mark_node;
13882
13883   /* Look up the identifier in the currently active scope.  Look only
13884      for namespaces, due to:
13885
13886        [basic.lookup.udir]
13887
13888        When looking up a namespace-name in a using-directive or alias
13889        definition, only namespace names are considered.
13890
13891      And:
13892
13893        [basic.lookup.qual]
13894
13895        During the lookup of a name preceding the :: scope resolution
13896        operator, object, function, and enumerator names are ignored.
13897
13898      (Note that cp_parser_qualifying_entity only calls this
13899      function if the token after the name is the scope resolution
13900      operator.)  */
13901   namespace_decl = cp_parser_lookup_name (parser, identifier,
13902                                           none_type,
13903                                           /*is_template=*/false,
13904                                           /*is_namespace=*/true,
13905                                           /*check_dependency=*/true,
13906                                           /*ambiguous_decls=*/NULL,
13907                                           token->location);
13908   /* If it's not a namespace, issue an error.  */
13909   if (namespace_decl == error_mark_node
13910       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13911     {
13912       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13913         error_at (token->location, "%qD is not a namespace-name", identifier);
13914       cp_parser_error (parser, "expected namespace-name");
13915       namespace_decl = error_mark_node;
13916     }
13917
13918   return namespace_decl;
13919 }
13920
13921 /* Parse a namespace-definition.
13922
13923    namespace-definition:
13924      named-namespace-definition
13925      unnamed-namespace-definition
13926
13927    named-namespace-definition:
13928      original-namespace-definition
13929      extension-namespace-definition
13930
13931    original-namespace-definition:
13932      namespace identifier { namespace-body }
13933
13934    extension-namespace-definition:
13935      namespace original-namespace-name { namespace-body }
13936
13937    unnamed-namespace-definition:
13938      namespace { namespace-body } */
13939
13940 static void
13941 cp_parser_namespace_definition (cp_parser* parser)
13942 {
13943   tree identifier, attribs;
13944   bool has_visibility;
13945   bool is_inline;
13946
13947   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13948     {
13949       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13950       is_inline = true;
13951       cp_lexer_consume_token (parser->lexer);
13952     }
13953   else
13954     is_inline = false;
13955
13956   /* Look for the `namespace' keyword.  */
13957   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13958
13959   /* Get the name of the namespace.  We do not attempt to distinguish
13960      between an original-namespace-definition and an
13961      extension-namespace-definition at this point.  The semantic
13962      analysis routines are responsible for that.  */
13963   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13964     identifier = cp_parser_identifier (parser);
13965   else
13966     identifier = NULL_TREE;
13967
13968   /* Parse any specified attributes.  */
13969   attribs = cp_parser_attributes_opt (parser);
13970
13971   /* Look for the `{' to start the namespace.  */
13972   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13973   /* Start the namespace.  */
13974   push_namespace (identifier);
13975
13976   /* "inline namespace" is equivalent to a stub namespace definition
13977      followed by a strong using directive.  */
13978   if (is_inline)
13979     {
13980       tree name_space = current_namespace;
13981       /* Set up namespace association.  */
13982       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13983         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13984                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13985       /* Import the contents of the inline namespace.  */
13986       pop_namespace ();
13987       do_using_directive (name_space);
13988       push_namespace (identifier);
13989     }
13990
13991   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13992
13993   /* Parse the body of the namespace.  */
13994   cp_parser_namespace_body (parser);
13995
13996   if (has_visibility)
13997     pop_visibility (1);
13998
13999   /* Finish the namespace.  */
14000   pop_namespace ();
14001   /* Look for the final `}'.  */
14002   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14003 }
14004
14005 /* Parse a namespace-body.
14006
14007    namespace-body:
14008      declaration-seq [opt]  */
14009
14010 static void
14011 cp_parser_namespace_body (cp_parser* parser)
14012 {
14013   cp_parser_declaration_seq_opt (parser);
14014 }
14015
14016 /* Parse a namespace-alias-definition.
14017
14018    namespace-alias-definition:
14019      namespace identifier = qualified-namespace-specifier ;  */
14020
14021 static void
14022 cp_parser_namespace_alias_definition (cp_parser* parser)
14023 {
14024   tree identifier;
14025   tree namespace_specifier;
14026
14027   cp_token *token = cp_lexer_peek_token (parser->lexer);
14028
14029   /* Look for the `namespace' keyword.  */
14030   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14031   /* Look for the identifier.  */
14032   identifier = cp_parser_identifier (parser);
14033   if (identifier == error_mark_node)
14034     return;
14035   /* Look for the `=' token.  */
14036   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14037       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14038     {
14039       error_at (token->location, "%<namespace%> definition is not allowed here");
14040       /* Skip the definition.  */
14041       cp_lexer_consume_token (parser->lexer);
14042       if (cp_parser_skip_to_closing_brace (parser))
14043         cp_lexer_consume_token (parser->lexer);
14044       return;
14045     }
14046   cp_parser_require (parser, CPP_EQ, RT_EQ);
14047   /* Look for the qualified-namespace-specifier.  */
14048   namespace_specifier
14049     = cp_parser_qualified_namespace_specifier (parser);
14050   /* Look for the `;' token.  */
14051   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14052
14053   /* Register the alias in the symbol table.  */
14054   do_namespace_alias (identifier, namespace_specifier);
14055 }
14056
14057 /* Parse a qualified-namespace-specifier.
14058
14059    qualified-namespace-specifier:
14060      :: [opt] nested-name-specifier [opt] namespace-name
14061
14062    Returns a NAMESPACE_DECL corresponding to the specified
14063    namespace.  */
14064
14065 static tree
14066 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14067 {
14068   /* Look for the optional `::'.  */
14069   cp_parser_global_scope_opt (parser,
14070                               /*current_scope_valid_p=*/false);
14071
14072   /* Look for the optional nested-name-specifier.  */
14073   cp_parser_nested_name_specifier_opt (parser,
14074                                        /*typename_keyword_p=*/false,
14075                                        /*check_dependency_p=*/true,
14076                                        /*type_p=*/false,
14077                                        /*is_declaration=*/true);
14078
14079   return cp_parser_namespace_name (parser);
14080 }
14081
14082 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14083    access declaration.
14084
14085    using-declaration:
14086      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14087      using :: unqualified-id ;  
14088
14089    access-declaration:
14090      qualified-id ;  
14091
14092    */
14093
14094 static bool
14095 cp_parser_using_declaration (cp_parser* parser, 
14096                              bool access_declaration_p)
14097 {
14098   cp_token *token;
14099   bool typename_p = false;
14100   bool global_scope_p;
14101   tree decl;
14102   tree identifier;
14103   tree qscope;
14104
14105   if (access_declaration_p)
14106     cp_parser_parse_tentatively (parser);
14107   else
14108     {
14109       /* Look for the `using' keyword.  */
14110       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14111       
14112       /* Peek at the next token.  */
14113       token = cp_lexer_peek_token (parser->lexer);
14114       /* See if it's `typename'.  */
14115       if (token->keyword == RID_TYPENAME)
14116         {
14117           /* Remember that we've seen it.  */
14118           typename_p = true;
14119           /* Consume the `typename' token.  */
14120           cp_lexer_consume_token (parser->lexer);
14121         }
14122     }
14123
14124   /* Look for the optional global scope qualification.  */
14125   global_scope_p
14126     = (cp_parser_global_scope_opt (parser,
14127                                    /*current_scope_valid_p=*/false)
14128        != NULL_TREE);
14129
14130   /* If we saw `typename', or didn't see `::', then there must be a
14131      nested-name-specifier present.  */
14132   if (typename_p || !global_scope_p)
14133     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14134                                               /*check_dependency_p=*/true,
14135                                               /*type_p=*/false,
14136                                               /*is_declaration=*/true);
14137   /* Otherwise, we could be in either of the two productions.  In that
14138      case, treat the nested-name-specifier as optional.  */
14139   else
14140     qscope = cp_parser_nested_name_specifier_opt (parser,
14141                                                   /*typename_keyword_p=*/false,
14142                                                   /*check_dependency_p=*/true,
14143                                                   /*type_p=*/false,
14144                                                   /*is_declaration=*/true);
14145   if (!qscope)
14146     qscope = global_namespace;
14147
14148   if (access_declaration_p && cp_parser_error_occurred (parser))
14149     /* Something has already gone wrong; there's no need to parse
14150        further.  Since an error has occurred, the return value of
14151        cp_parser_parse_definitely will be false, as required.  */
14152     return cp_parser_parse_definitely (parser);
14153
14154   token = cp_lexer_peek_token (parser->lexer);
14155   /* Parse the unqualified-id.  */
14156   identifier = cp_parser_unqualified_id (parser,
14157                                          /*template_keyword_p=*/false,
14158                                          /*check_dependency_p=*/true,
14159                                          /*declarator_p=*/true,
14160                                          /*optional_p=*/false);
14161
14162   if (access_declaration_p)
14163     {
14164       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14165         cp_parser_simulate_error (parser);
14166       if (!cp_parser_parse_definitely (parser))
14167         return false;
14168     }
14169
14170   /* The function we call to handle a using-declaration is different
14171      depending on what scope we are in.  */
14172   if (qscope == error_mark_node || identifier == error_mark_node)
14173     ;
14174   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14175            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14176     /* [namespace.udecl]
14177
14178        A using declaration shall not name a template-id.  */
14179     error_at (token->location,
14180               "a template-id may not appear in a using-declaration");
14181   else
14182     {
14183       if (at_class_scope_p ())
14184         {
14185           /* Create the USING_DECL.  */
14186           decl = do_class_using_decl (parser->scope, identifier);
14187
14188           if (check_for_bare_parameter_packs (decl))
14189             return false;
14190           else
14191             /* Add it to the list of members in this class.  */
14192             finish_member_declaration (decl);
14193         }
14194       else
14195         {
14196           decl = cp_parser_lookup_name_simple (parser,
14197                                                identifier,
14198                                                token->location);
14199           if (decl == error_mark_node)
14200             cp_parser_name_lookup_error (parser, identifier,
14201                                          decl, NLE_NULL,
14202                                          token->location);
14203           else if (check_for_bare_parameter_packs (decl))
14204             return false;
14205           else if (!at_namespace_scope_p ())
14206             do_local_using_decl (decl, qscope, identifier);
14207           else
14208             do_toplevel_using_decl (decl, qscope, identifier);
14209         }
14210     }
14211
14212   /* Look for the final `;'.  */
14213   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14214   
14215   return true;
14216 }
14217
14218 /* Parse a using-directive.
14219
14220    using-directive:
14221      using namespace :: [opt] nested-name-specifier [opt]
14222        namespace-name ;  */
14223
14224 static void
14225 cp_parser_using_directive (cp_parser* parser)
14226 {
14227   tree namespace_decl;
14228   tree attribs;
14229
14230   /* Look for the `using' keyword.  */
14231   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14232   /* And the `namespace' keyword.  */
14233   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14234   /* Look for the optional `::' operator.  */
14235   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14236   /* And the optional nested-name-specifier.  */
14237   cp_parser_nested_name_specifier_opt (parser,
14238                                        /*typename_keyword_p=*/false,
14239                                        /*check_dependency_p=*/true,
14240                                        /*type_p=*/false,
14241                                        /*is_declaration=*/true);
14242   /* Get the namespace being used.  */
14243   namespace_decl = cp_parser_namespace_name (parser);
14244   /* And any specified attributes.  */
14245   attribs = cp_parser_attributes_opt (parser);
14246   /* Update the symbol table.  */
14247   parse_using_directive (namespace_decl, attribs);
14248   /* Look for the final `;'.  */
14249   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14250 }
14251
14252 /* Parse an asm-definition.
14253
14254    asm-definition:
14255      asm ( string-literal ) ;
14256
14257    GNU Extension:
14258
14259    asm-definition:
14260      asm volatile [opt] ( string-literal ) ;
14261      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14262      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14263                           : asm-operand-list [opt] ) ;
14264      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14265                           : asm-operand-list [opt]
14266                           : asm-clobber-list [opt] ) ;
14267      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14268                                : asm-clobber-list [opt]
14269                                : asm-goto-list ) ;  */
14270
14271 static void
14272 cp_parser_asm_definition (cp_parser* parser)
14273 {
14274   tree string;
14275   tree outputs = NULL_TREE;
14276   tree inputs = NULL_TREE;
14277   tree clobbers = NULL_TREE;
14278   tree labels = NULL_TREE;
14279   tree asm_stmt;
14280   bool volatile_p = false;
14281   bool extended_p = false;
14282   bool invalid_inputs_p = false;
14283   bool invalid_outputs_p = false;
14284   bool goto_p = false;
14285   required_token missing = RT_NONE;
14286
14287   /* Look for the `asm' keyword.  */
14288   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14289   /* See if the next token is `volatile'.  */
14290   if (cp_parser_allow_gnu_extensions_p (parser)
14291       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14292     {
14293       /* Remember that we saw the `volatile' keyword.  */
14294       volatile_p = true;
14295       /* Consume the token.  */
14296       cp_lexer_consume_token (parser->lexer);
14297     }
14298   if (cp_parser_allow_gnu_extensions_p (parser)
14299       && parser->in_function_body
14300       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14301     {
14302       /* Remember that we saw the `goto' keyword.  */
14303       goto_p = true;
14304       /* Consume the token.  */
14305       cp_lexer_consume_token (parser->lexer);
14306     }
14307   /* Look for the opening `('.  */
14308   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14309     return;
14310   /* Look for the string.  */
14311   string = cp_parser_string_literal (parser, false, false);
14312   if (string == error_mark_node)
14313     {
14314       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14315                                              /*consume_paren=*/true);
14316       return;
14317     }
14318
14319   /* If we're allowing GNU extensions, check for the extended assembly
14320      syntax.  Unfortunately, the `:' tokens need not be separated by
14321      a space in C, and so, for compatibility, we tolerate that here
14322      too.  Doing that means that we have to treat the `::' operator as
14323      two `:' tokens.  */
14324   if (cp_parser_allow_gnu_extensions_p (parser)
14325       && parser->in_function_body
14326       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14327           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14328     {
14329       bool inputs_p = false;
14330       bool clobbers_p = false;
14331       bool labels_p = false;
14332
14333       /* The extended syntax was used.  */
14334       extended_p = true;
14335
14336       /* Look for outputs.  */
14337       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14338         {
14339           /* Consume the `:'.  */
14340           cp_lexer_consume_token (parser->lexer);
14341           /* Parse the output-operands.  */
14342           if (cp_lexer_next_token_is_not (parser->lexer,
14343                                           CPP_COLON)
14344               && cp_lexer_next_token_is_not (parser->lexer,
14345                                              CPP_SCOPE)
14346               && cp_lexer_next_token_is_not (parser->lexer,
14347                                              CPP_CLOSE_PAREN)
14348               && !goto_p)
14349             outputs = cp_parser_asm_operand_list (parser);
14350
14351             if (outputs == error_mark_node)
14352               invalid_outputs_p = true;
14353         }
14354       /* If the next token is `::', there are no outputs, and the
14355          next token is the beginning of the inputs.  */
14356       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14357         /* The inputs are coming next.  */
14358         inputs_p = true;
14359
14360       /* Look for inputs.  */
14361       if (inputs_p
14362           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14363         {
14364           /* Consume the `:' or `::'.  */
14365           cp_lexer_consume_token (parser->lexer);
14366           /* Parse the output-operands.  */
14367           if (cp_lexer_next_token_is_not (parser->lexer,
14368                                           CPP_COLON)
14369               && cp_lexer_next_token_is_not (parser->lexer,
14370                                              CPP_SCOPE)
14371               && cp_lexer_next_token_is_not (parser->lexer,
14372                                              CPP_CLOSE_PAREN))
14373             inputs = cp_parser_asm_operand_list (parser);
14374
14375             if (inputs == error_mark_node)
14376               invalid_inputs_p = true;
14377         }
14378       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14379         /* The clobbers are coming next.  */
14380         clobbers_p = true;
14381
14382       /* Look for clobbers.  */
14383       if (clobbers_p
14384           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14385         {
14386           clobbers_p = true;
14387           /* Consume the `:' or `::'.  */
14388           cp_lexer_consume_token (parser->lexer);
14389           /* Parse the clobbers.  */
14390           if (cp_lexer_next_token_is_not (parser->lexer,
14391                                           CPP_COLON)
14392               && cp_lexer_next_token_is_not (parser->lexer,
14393                                              CPP_CLOSE_PAREN))
14394             clobbers = cp_parser_asm_clobber_list (parser);
14395         }
14396       else if (goto_p
14397                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14398         /* The labels are coming next.  */
14399         labels_p = true;
14400
14401       /* Look for labels.  */
14402       if (labels_p
14403           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14404         {
14405           labels_p = true;
14406           /* Consume the `:' or `::'.  */
14407           cp_lexer_consume_token (parser->lexer);
14408           /* Parse the labels.  */
14409           labels = cp_parser_asm_label_list (parser);
14410         }
14411
14412       if (goto_p && !labels_p)
14413         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14414     }
14415   else if (goto_p)
14416     missing = RT_COLON_SCOPE;
14417
14418   /* Look for the closing `)'.  */
14419   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14420                           missing ? missing : RT_CLOSE_PAREN))
14421     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14422                                            /*consume_paren=*/true);
14423   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14424
14425   if (!invalid_inputs_p && !invalid_outputs_p)
14426     {
14427       /* Create the ASM_EXPR.  */
14428       if (parser->in_function_body)
14429         {
14430           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14431                                       inputs, clobbers, labels);
14432           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14433           if (!extended_p)
14434             {
14435               tree temp = asm_stmt;
14436               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14437                 temp = TREE_OPERAND (temp, 0);
14438
14439               ASM_INPUT_P (temp) = 1;
14440             }
14441         }
14442       else
14443         cgraph_add_asm_node (string);
14444     }
14445 }
14446
14447 /* Declarators [gram.dcl.decl] */
14448
14449 /* Parse an init-declarator.
14450
14451    init-declarator:
14452      declarator initializer [opt]
14453
14454    GNU Extension:
14455
14456    init-declarator:
14457      declarator asm-specification [opt] attributes [opt] initializer [opt]
14458
14459    function-definition:
14460      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14461        function-body
14462      decl-specifier-seq [opt] declarator function-try-block
14463
14464    GNU Extension:
14465
14466    function-definition:
14467      __extension__ function-definition
14468
14469    The DECL_SPECIFIERS apply to this declarator.  Returns a
14470    representation of the entity declared.  If MEMBER_P is TRUE, then
14471    this declarator appears in a class scope.  The new DECL created by
14472    this declarator is returned.
14473
14474    The CHECKS are access checks that should be performed once we know
14475    what entity is being declared (and, therefore, what classes have
14476    befriended it).
14477
14478    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14479    for a function-definition here as well.  If the declarator is a
14480    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14481    be TRUE upon return.  By that point, the function-definition will
14482    have been completely parsed.
14483
14484    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14485    is FALSE.
14486
14487    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14488    parsed declaration if it is an uninitialized single declarator not followed
14489    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14490    if present, will not be consumed.  If returned, this declarator will be
14491    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14492
14493 static tree
14494 cp_parser_init_declarator (cp_parser* parser,
14495                            cp_decl_specifier_seq *decl_specifiers,
14496                            VEC (deferred_access_check,gc)* checks,
14497                            bool function_definition_allowed_p,
14498                            bool member_p,
14499                            int declares_class_or_enum,
14500                            bool* function_definition_p,
14501                            tree* maybe_range_for_decl)
14502 {
14503   cp_token *token = NULL, *asm_spec_start_token = NULL,
14504            *attributes_start_token = NULL;
14505   cp_declarator *declarator;
14506   tree prefix_attributes;
14507   tree attributes;
14508   tree asm_specification;
14509   tree initializer;
14510   tree decl = NULL_TREE;
14511   tree scope;
14512   int is_initialized;
14513   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14514      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14515      "(...)".  */
14516   enum cpp_ttype initialization_kind;
14517   bool is_direct_init = false;
14518   bool is_non_constant_init;
14519   int ctor_dtor_or_conv_p;
14520   bool friend_p;
14521   tree pushed_scope = NULL_TREE;
14522   bool range_for_decl_p = false;
14523
14524   /* Gather the attributes that were provided with the
14525      decl-specifiers.  */
14526   prefix_attributes = decl_specifiers->attributes;
14527
14528   /* Assume that this is not the declarator for a function
14529      definition.  */
14530   if (function_definition_p)
14531     *function_definition_p = false;
14532
14533   /* Defer access checks while parsing the declarator; we cannot know
14534      what names are accessible until we know what is being
14535      declared.  */
14536   resume_deferring_access_checks ();
14537
14538   /* Parse the declarator.  */
14539   token = cp_lexer_peek_token (parser->lexer);
14540   declarator
14541     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14542                             &ctor_dtor_or_conv_p,
14543                             /*parenthesized_p=*/NULL,
14544                             member_p);
14545   /* Gather up the deferred checks.  */
14546   stop_deferring_access_checks ();
14547
14548   /* If the DECLARATOR was erroneous, there's no need to go
14549      further.  */
14550   if (declarator == cp_error_declarator)
14551     return error_mark_node;
14552
14553   /* Check that the number of template-parameter-lists is OK.  */
14554   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14555                                                        token->location))
14556     return error_mark_node;
14557
14558   if (declares_class_or_enum & 2)
14559     cp_parser_check_for_definition_in_return_type (declarator,
14560                                                    decl_specifiers->type,
14561                                                    decl_specifiers->type_location);
14562
14563   /* Figure out what scope the entity declared by the DECLARATOR is
14564      located in.  `grokdeclarator' sometimes changes the scope, so
14565      we compute it now.  */
14566   scope = get_scope_of_declarator (declarator);
14567
14568   /* Perform any lookups in the declared type which were thought to be
14569      dependent, but are not in the scope of the declarator.  */
14570   decl_specifiers->type
14571     = maybe_update_decl_type (decl_specifiers->type, scope);
14572
14573   /* If we're allowing GNU extensions, look for an asm-specification
14574      and attributes.  */
14575   if (cp_parser_allow_gnu_extensions_p (parser))
14576     {
14577       /* Look for an asm-specification.  */
14578       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14579       asm_specification = cp_parser_asm_specification_opt (parser);
14580       /* And attributes.  */
14581       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14582       attributes = cp_parser_attributes_opt (parser);
14583     }
14584   else
14585     {
14586       asm_specification = NULL_TREE;
14587       attributes = NULL_TREE;
14588     }
14589
14590   /* Peek at the next token.  */
14591   token = cp_lexer_peek_token (parser->lexer);
14592   /* Check to see if the token indicates the start of a
14593      function-definition.  */
14594   if (function_declarator_p (declarator)
14595       && cp_parser_token_starts_function_definition_p (token))
14596     {
14597       if (!function_definition_allowed_p)
14598         {
14599           /* If a function-definition should not appear here, issue an
14600              error message.  */
14601           cp_parser_error (parser,
14602                            "a function-definition is not allowed here");
14603           return error_mark_node;
14604         }
14605       else
14606         {
14607           location_t func_brace_location
14608             = cp_lexer_peek_token (parser->lexer)->location;
14609
14610           /* Neither attributes nor an asm-specification are allowed
14611              on a function-definition.  */
14612           if (asm_specification)
14613             error_at (asm_spec_start_token->location,
14614                       "an asm-specification is not allowed "
14615                       "on a function-definition");
14616           if (attributes)
14617             error_at (attributes_start_token->location,
14618                       "attributes are not allowed on a function-definition");
14619           /* This is a function-definition.  */
14620           *function_definition_p = true;
14621
14622           /* Parse the function definition.  */
14623           if (member_p)
14624             decl = cp_parser_save_member_function_body (parser,
14625                                                         decl_specifiers,
14626                                                         declarator,
14627                                                         prefix_attributes);
14628           else
14629             decl
14630               = (cp_parser_function_definition_from_specifiers_and_declarator
14631                  (parser, decl_specifiers, prefix_attributes, declarator));
14632
14633           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14634             {
14635               /* This is where the prologue starts...  */
14636               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14637                 = func_brace_location;
14638             }
14639
14640           return decl;
14641         }
14642     }
14643
14644   /* [dcl.dcl]
14645
14646      Only in function declarations for constructors, destructors, and
14647      type conversions can the decl-specifier-seq be omitted.
14648
14649      We explicitly postpone this check past the point where we handle
14650      function-definitions because we tolerate function-definitions
14651      that are missing their return types in some modes.  */
14652   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14653     {
14654       cp_parser_error (parser,
14655                        "expected constructor, destructor, or type conversion");
14656       return error_mark_node;
14657     }
14658
14659   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14660   if (token->type == CPP_EQ
14661       || token->type == CPP_OPEN_PAREN
14662       || token->type == CPP_OPEN_BRACE)
14663     {
14664       is_initialized = SD_INITIALIZED;
14665       initialization_kind = token->type;
14666       if (maybe_range_for_decl)
14667         *maybe_range_for_decl = error_mark_node;
14668
14669       if (token->type == CPP_EQ
14670           && function_declarator_p (declarator))
14671         {
14672           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14673           if (t2->keyword == RID_DEFAULT)
14674             is_initialized = SD_DEFAULTED;
14675           else if (t2->keyword == RID_DELETE)
14676             is_initialized = SD_DELETED;
14677         }
14678     }
14679   else
14680     {
14681       /* If the init-declarator isn't initialized and isn't followed by a
14682          `,' or `;', it's not a valid init-declarator.  */
14683       if (token->type != CPP_COMMA
14684           && token->type != CPP_SEMICOLON)
14685         {
14686           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14687             range_for_decl_p = true;
14688           else
14689             {
14690               cp_parser_error (parser, "expected initializer");
14691               return error_mark_node;
14692             }
14693         }
14694       is_initialized = SD_UNINITIALIZED;
14695       initialization_kind = CPP_EOF;
14696     }
14697
14698   /* Because start_decl has side-effects, we should only call it if we
14699      know we're going ahead.  By this point, we know that we cannot
14700      possibly be looking at any other construct.  */
14701   cp_parser_commit_to_tentative_parse (parser);
14702
14703   /* If the decl specifiers were bad, issue an error now that we're
14704      sure this was intended to be a declarator.  Then continue
14705      declaring the variable(s), as int, to try to cut down on further
14706      errors.  */
14707   if (decl_specifiers->any_specifiers_p
14708       && decl_specifiers->type == error_mark_node)
14709     {
14710       cp_parser_error (parser, "invalid type in declaration");
14711       decl_specifiers->type = integer_type_node;
14712     }
14713
14714   /* Check to see whether or not this declaration is a friend.  */
14715   friend_p = cp_parser_friend_p (decl_specifiers);
14716
14717   /* Enter the newly declared entry in the symbol table.  If we're
14718      processing a declaration in a class-specifier, we wait until
14719      after processing the initializer.  */
14720   if (!member_p)
14721     {
14722       if (parser->in_unbraced_linkage_specification_p)
14723         decl_specifiers->storage_class = sc_extern;
14724       decl = start_decl (declarator, decl_specifiers,
14725                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14726                          attributes, prefix_attributes,
14727                          &pushed_scope);
14728       /* Adjust location of decl if declarator->id_loc is more appropriate:
14729          set, and decl wasn't merged with another decl, in which case its
14730          location would be different from input_location, and more accurate.  */
14731       if (DECL_P (decl)
14732           && declarator->id_loc != UNKNOWN_LOCATION
14733           && DECL_SOURCE_LOCATION (decl) == input_location)
14734         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14735     }
14736   else if (scope)
14737     /* Enter the SCOPE.  That way unqualified names appearing in the
14738        initializer will be looked up in SCOPE.  */
14739     pushed_scope = push_scope (scope);
14740
14741   /* Perform deferred access control checks, now that we know in which
14742      SCOPE the declared entity resides.  */
14743   if (!member_p && decl)
14744     {
14745       tree saved_current_function_decl = NULL_TREE;
14746
14747       /* If the entity being declared is a function, pretend that we
14748          are in its scope.  If it is a `friend', it may have access to
14749          things that would not otherwise be accessible.  */
14750       if (TREE_CODE (decl) == FUNCTION_DECL)
14751         {
14752           saved_current_function_decl = current_function_decl;
14753           current_function_decl = decl;
14754         }
14755
14756       /* Perform access checks for template parameters.  */
14757       cp_parser_perform_template_parameter_access_checks (checks);
14758
14759       /* Perform the access control checks for the declarator and the
14760          decl-specifiers.  */
14761       perform_deferred_access_checks ();
14762
14763       /* Restore the saved value.  */
14764       if (TREE_CODE (decl) == FUNCTION_DECL)
14765         current_function_decl = saved_current_function_decl;
14766     }
14767
14768   /* Parse the initializer.  */
14769   initializer = NULL_TREE;
14770   is_direct_init = false;
14771   is_non_constant_init = true;
14772   if (is_initialized)
14773     {
14774       if (function_declarator_p (declarator))
14775         {
14776           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14777            if (initialization_kind == CPP_EQ)
14778              initializer = cp_parser_pure_specifier (parser);
14779            else
14780              {
14781                /* If the declaration was erroneous, we don't really
14782                   know what the user intended, so just silently
14783                   consume the initializer.  */
14784                if (decl != error_mark_node)
14785                  error_at (initializer_start_token->location,
14786                            "initializer provided for function");
14787                cp_parser_skip_to_closing_parenthesis (parser,
14788                                                       /*recovering=*/true,
14789                                                       /*or_comma=*/false,
14790                                                       /*consume_paren=*/true);
14791              }
14792         }
14793       else
14794         {
14795           /* We want to record the extra mangling scope for in-class
14796              initializers of class members and initializers of static data
14797              member templates.  The former is a C++0x feature which isn't
14798              implemented yet, and I expect it will involve deferring
14799              parsing of the initializer until end of class as with default
14800              arguments.  So right here we only handle the latter.  */
14801           if (!member_p && processing_template_decl)
14802             start_lambda_scope (decl);
14803           initializer = cp_parser_initializer (parser,
14804                                                &is_direct_init,
14805                                                &is_non_constant_init);
14806           if (!member_p && processing_template_decl)
14807             finish_lambda_scope ();
14808         }
14809     }
14810
14811   /* The old parser allows attributes to appear after a parenthesized
14812      initializer.  Mark Mitchell proposed removing this functionality
14813      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14814      attributes -- but ignores them.  */
14815   if (cp_parser_allow_gnu_extensions_p (parser)
14816       && initialization_kind == CPP_OPEN_PAREN)
14817     if (cp_parser_attributes_opt (parser))
14818       warning (OPT_Wattributes,
14819                "attributes after parenthesized initializer ignored");
14820
14821   /* For an in-class declaration, use `grokfield' to create the
14822      declaration.  */
14823   if (member_p)
14824     {
14825       if (pushed_scope)
14826         {
14827           pop_scope (pushed_scope);
14828           pushed_scope = NULL_TREE;
14829         }
14830       decl = grokfield (declarator, decl_specifiers,
14831                         initializer, !is_non_constant_init,
14832                         /*asmspec=*/NULL_TREE,
14833                         prefix_attributes);
14834       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14835         cp_parser_save_default_args (parser, decl);
14836     }
14837
14838   /* Finish processing the declaration.  But, skip member
14839      declarations.  */
14840   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14841     {
14842       cp_finish_decl (decl,
14843                       initializer, !is_non_constant_init,
14844                       asm_specification,
14845                       /* If the initializer is in parentheses, then this is
14846                          a direct-initialization, which means that an
14847                          `explicit' constructor is OK.  Otherwise, an
14848                          `explicit' constructor cannot be used.  */
14849                       ((is_direct_init || !is_initialized)
14850                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14851     }
14852   else if ((cxx_dialect != cxx98) && friend_p
14853            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14854     /* Core issue #226 (C++0x only): A default template-argument
14855        shall not be specified in a friend class template
14856        declaration. */
14857     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14858                              /*is_partial=*/0, /*is_friend_decl=*/1);
14859
14860   if (!friend_p && pushed_scope)
14861     pop_scope (pushed_scope);
14862
14863   return decl;
14864 }
14865
14866 /* Parse a declarator.
14867
14868    declarator:
14869      direct-declarator
14870      ptr-operator declarator
14871
14872    abstract-declarator:
14873      ptr-operator abstract-declarator [opt]
14874      direct-abstract-declarator
14875
14876    GNU Extensions:
14877
14878    declarator:
14879      attributes [opt] direct-declarator
14880      attributes [opt] ptr-operator declarator
14881
14882    abstract-declarator:
14883      attributes [opt] ptr-operator abstract-declarator [opt]
14884      attributes [opt] direct-abstract-declarator
14885
14886    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14887    detect constructor, destructor or conversion operators. It is set
14888    to -1 if the declarator is a name, and +1 if it is a
14889    function. Otherwise it is set to zero. Usually you just want to
14890    test for >0, but internally the negative value is used.
14891
14892    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14893    a decl-specifier-seq unless it declares a constructor, destructor,
14894    or conversion.  It might seem that we could check this condition in
14895    semantic analysis, rather than parsing, but that makes it difficult
14896    to handle something like `f()'.  We want to notice that there are
14897    no decl-specifiers, and therefore realize that this is an
14898    expression, not a declaration.)
14899
14900    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14901    the declarator is a direct-declarator of the form "(...)".
14902
14903    MEMBER_P is true iff this declarator is a member-declarator.  */
14904
14905 static cp_declarator *
14906 cp_parser_declarator (cp_parser* parser,
14907                       cp_parser_declarator_kind dcl_kind,
14908                       int* ctor_dtor_or_conv_p,
14909                       bool* parenthesized_p,
14910                       bool member_p)
14911 {
14912   cp_declarator *declarator;
14913   enum tree_code code;
14914   cp_cv_quals cv_quals;
14915   tree class_type;
14916   tree attributes = NULL_TREE;
14917
14918   /* Assume this is not a constructor, destructor, or type-conversion
14919      operator.  */
14920   if (ctor_dtor_or_conv_p)
14921     *ctor_dtor_or_conv_p = 0;
14922
14923   if (cp_parser_allow_gnu_extensions_p (parser))
14924     attributes = cp_parser_attributes_opt (parser);
14925
14926   /* Check for the ptr-operator production.  */
14927   cp_parser_parse_tentatively (parser);
14928   /* Parse the ptr-operator.  */
14929   code = cp_parser_ptr_operator (parser,
14930                                  &class_type,
14931                                  &cv_quals);
14932   /* If that worked, then we have a ptr-operator.  */
14933   if (cp_parser_parse_definitely (parser))
14934     {
14935       /* If a ptr-operator was found, then this declarator was not
14936          parenthesized.  */
14937       if (parenthesized_p)
14938         *parenthesized_p = true;
14939       /* The dependent declarator is optional if we are parsing an
14940          abstract-declarator.  */
14941       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14942         cp_parser_parse_tentatively (parser);
14943
14944       /* Parse the dependent declarator.  */
14945       declarator = cp_parser_declarator (parser, dcl_kind,
14946                                          /*ctor_dtor_or_conv_p=*/NULL,
14947                                          /*parenthesized_p=*/NULL,
14948                                          /*member_p=*/false);
14949
14950       /* If we are parsing an abstract-declarator, we must handle the
14951          case where the dependent declarator is absent.  */
14952       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14953           && !cp_parser_parse_definitely (parser))
14954         declarator = NULL;
14955
14956       declarator = cp_parser_make_indirect_declarator
14957         (code, class_type, cv_quals, declarator);
14958     }
14959   /* Everything else is a direct-declarator.  */
14960   else
14961     {
14962       if (parenthesized_p)
14963         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14964                                                    CPP_OPEN_PAREN);
14965       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14966                                                 ctor_dtor_or_conv_p,
14967                                                 member_p);
14968     }
14969
14970   if (attributes && declarator && declarator != cp_error_declarator)
14971     declarator->attributes = attributes;
14972
14973   return declarator;
14974 }
14975
14976 /* Parse a direct-declarator or direct-abstract-declarator.
14977
14978    direct-declarator:
14979      declarator-id
14980      direct-declarator ( parameter-declaration-clause )
14981        cv-qualifier-seq [opt]
14982        exception-specification [opt]
14983      direct-declarator [ constant-expression [opt] ]
14984      ( declarator )
14985
14986    direct-abstract-declarator:
14987      direct-abstract-declarator [opt]
14988        ( parameter-declaration-clause )
14989        cv-qualifier-seq [opt]
14990        exception-specification [opt]
14991      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14992      ( abstract-declarator )
14993
14994    Returns a representation of the declarator.  DCL_KIND is
14995    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14996    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14997    we are parsing a direct-declarator.  It is
14998    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14999    of ambiguity we prefer an abstract declarator, as per
15000    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15001    cp_parser_declarator.  */
15002
15003 static cp_declarator *
15004 cp_parser_direct_declarator (cp_parser* parser,
15005                              cp_parser_declarator_kind dcl_kind,
15006                              int* ctor_dtor_or_conv_p,
15007                              bool member_p)
15008 {
15009   cp_token *token;
15010   cp_declarator *declarator = NULL;
15011   tree scope = NULL_TREE;
15012   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15013   bool saved_in_declarator_p = parser->in_declarator_p;
15014   bool first = true;
15015   tree pushed_scope = NULL_TREE;
15016
15017   while (true)
15018     {
15019       /* Peek at the next token.  */
15020       token = cp_lexer_peek_token (parser->lexer);
15021       if (token->type == CPP_OPEN_PAREN)
15022         {
15023           /* This is either a parameter-declaration-clause, or a
15024              parenthesized declarator. When we know we are parsing a
15025              named declarator, it must be a parenthesized declarator
15026              if FIRST is true. For instance, `(int)' is a
15027              parameter-declaration-clause, with an omitted
15028              direct-abstract-declarator. But `((*))', is a
15029              parenthesized abstract declarator. Finally, when T is a
15030              template parameter `(T)' is a
15031              parameter-declaration-clause, and not a parenthesized
15032              named declarator.
15033
15034              We first try and parse a parameter-declaration-clause,
15035              and then try a nested declarator (if FIRST is true).
15036
15037              It is not an error for it not to be a
15038              parameter-declaration-clause, even when FIRST is
15039              false. Consider,
15040
15041                int i (int);
15042                int i (3);
15043
15044              The first is the declaration of a function while the
15045              second is the definition of a variable, including its
15046              initializer.
15047
15048              Having seen only the parenthesis, we cannot know which of
15049              these two alternatives should be selected.  Even more
15050              complex are examples like:
15051
15052                int i (int (a));
15053                int i (int (3));
15054
15055              The former is a function-declaration; the latter is a
15056              variable initialization.
15057
15058              Thus again, we try a parameter-declaration-clause, and if
15059              that fails, we back out and return.  */
15060
15061           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15062             {
15063               tree params;
15064               unsigned saved_num_template_parameter_lists;
15065               bool is_declarator = false;
15066               tree t;
15067
15068               /* In a member-declarator, the only valid interpretation
15069                  of a parenthesis is the start of a
15070                  parameter-declaration-clause.  (It is invalid to
15071                  initialize a static data member with a parenthesized
15072                  initializer; only the "=" form of initialization is
15073                  permitted.)  */
15074               if (!member_p)
15075                 cp_parser_parse_tentatively (parser);
15076
15077               /* Consume the `('.  */
15078               cp_lexer_consume_token (parser->lexer);
15079               if (first)
15080                 {
15081                   /* If this is going to be an abstract declarator, we're
15082                      in a declarator and we can't have default args.  */
15083                   parser->default_arg_ok_p = false;
15084                   parser->in_declarator_p = true;
15085                 }
15086
15087               /* Inside the function parameter list, surrounding
15088                  template-parameter-lists do not apply.  */
15089               saved_num_template_parameter_lists
15090                 = parser->num_template_parameter_lists;
15091               parser->num_template_parameter_lists = 0;
15092
15093               begin_scope (sk_function_parms, NULL_TREE);
15094
15095               /* Parse the parameter-declaration-clause.  */
15096               params = cp_parser_parameter_declaration_clause (parser);
15097
15098               parser->num_template_parameter_lists
15099                 = saved_num_template_parameter_lists;
15100
15101               /* Consume the `)'.  */
15102               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15103
15104               /* If all went well, parse the cv-qualifier-seq and the
15105                  exception-specification.  */
15106               if (member_p || cp_parser_parse_definitely (parser))
15107                 {
15108                   cp_cv_quals cv_quals;
15109                   cp_virt_specifiers virt_specifiers;
15110                   tree exception_specification;
15111                   tree late_return;
15112
15113                   is_declarator = true;
15114
15115                   if (ctor_dtor_or_conv_p)
15116                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15117                   first = false;
15118
15119                   /* Parse the cv-qualifier-seq.  */
15120                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15121                   /* And the exception-specification.  */
15122                   exception_specification
15123                     = cp_parser_exception_specification_opt (parser);
15124                   /* Parse the virt-specifier-seq.  */
15125                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15126
15127                   late_return = (cp_parser_late_return_type_opt
15128                                  (parser, member_p ? cv_quals : -1));
15129
15130                   /* Create the function-declarator.  */
15131                   declarator = make_call_declarator (declarator,
15132                                                      params,
15133                                                      cv_quals,
15134                                                      virt_specifiers,
15135                                                      exception_specification,
15136                                                      late_return);
15137                   /* Any subsequent parameter lists are to do with
15138                      return type, so are not those of the declared
15139                      function.  */
15140                   parser->default_arg_ok_p = false;
15141                 }
15142
15143               /* Remove the function parms from scope.  */
15144               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15145                 pop_binding (DECL_NAME (t), t);
15146               leave_scope();
15147
15148               if (is_declarator)
15149                 /* Repeat the main loop.  */
15150                 continue;
15151             }
15152
15153           /* If this is the first, we can try a parenthesized
15154              declarator.  */
15155           if (first)
15156             {
15157               bool saved_in_type_id_in_expr_p;
15158
15159               parser->default_arg_ok_p = saved_default_arg_ok_p;
15160               parser->in_declarator_p = saved_in_declarator_p;
15161
15162               /* Consume the `('.  */
15163               cp_lexer_consume_token (parser->lexer);
15164               /* Parse the nested declarator.  */
15165               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15166               parser->in_type_id_in_expr_p = true;
15167               declarator
15168                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15169                                         /*parenthesized_p=*/NULL,
15170                                         member_p);
15171               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15172               first = false;
15173               /* Expect a `)'.  */
15174               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15175                 declarator = cp_error_declarator;
15176               if (declarator == cp_error_declarator)
15177                 break;
15178
15179               goto handle_declarator;
15180             }
15181           /* Otherwise, we must be done.  */
15182           else
15183             break;
15184         }
15185       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15186                && token->type == CPP_OPEN_SQUARE)
15187         {
15188           /* Parse an array-declarator.  */
15189           tree bounds;
15190
15191           if (ctor_dtor_or_conv_p)
15192             *ctor_dtor_or_conv_p = 0;
15193
15194           first = false;
15195           parser->default_arg_ok_p = false;
15196           parser->in_declarator_p = true;
15197           /* Consume the `['.  */
15198           cp_lexer_consume_token (parser->lexer);
15199           /* Peek at the next token.  */
15200           token = cp_lexer_peek_token (parser->lexer);
15201           /* If the next token is `]', then there is no
15202              constant-expression.  */
15203           if (token->type != CPP_CLOSE_SQUARE)
15204             {
15205               bool non_constant_p;
15206
15207               bounds
15208                 = cp_parser_constant_expression (parser,
15209                                                  /*allow_non_constant=*/true,
15210                                                  &non_constant_p);
15211               if (!non_constant_p)
15212                 /* OK */;
15213               /* Normally, the array bound must be an integral constant
15214                  expression.  However, as an extension, we allow VLAs
15215                  in function scopes as long as they aren't part of a
15216                  parameter declaration.  */
15217               else if (!parser->in_function_body
15218                        || current_binding_level->kind == sk_function_parms)
15219                 {
15220                   cp_parser_error (parser,
15221                                    "array bound is not an integer constant");
15222                   bounds = error_mark_node;
15223                 }
15224               else if (processing_template_decl && !error_operand_p (bounds))
15225                 {
15226                   /* Remember this wasn't a constant-expression.  */
15227                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15228                   TREE_SIDE_EFFECTS (bounds) = 1;
15229                 }
15230             }
15231           else
15232             bounds = NULL_TREE;
15233           /* Look for the closing `]'.  */
15234           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15235             {
15236               declarator = cp_error_declarator;
15237               break;
15238             }
15239
15240           declarator = make_array_declarator (declarator, bounds);
15241         }
15242       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15243         {
15244           {
15245             tree qualifying_scope;
15246             tree unqualified_name;
15247             special_function_kind sfk;
15248             bool abstract_ok;
15249             bool pack_expansion_p = false;
15250             cp_token *declarator_id_start_token;
15251
15252             /* Parse a declarator-id */
15253             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15254             if (abstract_ok)
15255               {
15256                 cp_parser_parse_tentatively (parser);
15257
15258                 /* If we see an ellipsis, we should be looking at a
15259                    parameter pack. */
15260                 if (token->type == CPP_ELLIPSIS)
15261                   {
15262                     /* Consume the `...' */
15263                     cp_lexer_consume_token (parser->lexer);
15264
15265                     pack_expansion_p = true;
15266                   }
15267               }
15268
15269             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15270             unqualified_name
15271               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15272             qualifying_scope = parser->scope;
15273             if (abstract_ok)
15274               {
15275                 bool okay = false;
15276
15277                 if (!unqualified_name && pack_expansion_p)
15278                   {
15279                     /* Check whether an error occurred. */
15280                     okay = !cp_parser_error_occurred (parser);
15281
15282                     /* We already consumed the ellipsis to mark a
15283                        parameter pack, but we have no way to report it,
15284                        so abort the tentative parse. We will be exiting
15285                        immediately anyway. */
15286                     cp_parser_abort_tentative_parse (parser);
15287                   }
15288                 else
15289                   okay = cp_parser_parse_definitely (parser);
15290
15291                 if (!okay)
15292                   unqualified_name = error_mark_node;
15293                 else if (unqualified_name
15294                          && (qualifying_scope
15295                              || (TREE_CODE (unqualified_name)
15296                                  != IDENTIFIER_NODE)))
15297                   {
15298                     cp_parser_error (parser, "expected unqualified-id");
15299                     unqualified_name = error_mark_node;
15300                   }
15301               }
15302
15303             if (!unqualified_name)
15304               return NULL;
15305             if (unqualified_name == error_mark_node)
15306               {
15307                 declarator = cp_error_declarator;
15308                 pack_expansion_p = false;
15309                 declarator->parameter_pack_p = false;
15310                 break;
15311               }
15312
15313             if (qualifying_scope && at_namespace_scope_p ()
15314                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15315               {
15316                 /* In the declaration of a member of a template class
15317                    outside of the class itself, the SCOPE will sometimes
15318                    be a TYPENAME_TYPE.  For example, given:
15319
15320                    template <typename T>
15321                    int S<T>::R::i = 3;
15322
15323                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15324                    this context, we must resolve S<T>::R to an ordinary
15325                    type, rather than a typename type.
15326
15327                    The reason we normally avoid resolving TYPENAME_TYPEs
15328                    is that a specialization of `S' might render
15329                    `S<T>::R' not a type.  However, if `S' is
15330                    specialized, then this `i' will not be used, so there
15331                    is no harm in resolving the types here.  */
15332                 tree type;
15333
15334                 /* Resolve the TYPENAME_TYPE.  */
15335                 type = resolve_typename_type (qualifying_scope,
15336                                               /*only_current_p=*/false);
15337                 /* If that failed, the declarator is invalid.  */
15338                 if (TREE_CODE (type) == TYPENAME_TYPE)
15339                   {
15340                     if (typedef_variant_p (type))
15341                       error_at (declarator_id_start_token->location,
15342                                 "cannot define member of dependent typedef "
15343                                 "%qT", type);
15344                     else
15345                       error_at (declarator_id_start_token->location,
15346                                 "%<%T::%E%> is not a type",
15347                                 TYPE_CONTEXT (qualifying_scope),
15348                                 TYPE_IDENTIFIER (qualifying_scope));
15349                   }
15350                 qualifying_scope = type;
15351               }
15352
15353             sfk = sfk_none;
15354
15355             if (unqualified_name)
15356               {
15357                 tree class_type;
15358
15359                 if (qualifying_scope
15360                     && CLASS_TYPE_P (qualifying_scope))
15361                   class_type = qualifying_scope;
15362                 else
15363                   class_type = current_class_type;
15364
15365                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15366                   {
15367                     tree name_type = TREE_TYPE (unqualified_name);
15368                     if (class_type && same_type_p (name_type, class_type))
15369                       {
15370                         if (qualifying_scope
15371                             && CLASSTYPE_USE_TEMPLATE (name_type))
15372                           {
15373                             error_at (declarator_id_start_token->location,
15374                                       "invalid use of constructor as a template");
15375                             inform (declarator_id_start_token->location,
15376                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15377                                     "name the constructor in a qualified name",
15378                                     class_type,
15379                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15380                                     class_type, name_type);
15381                             declarator = cp_error_declarator;
15382                             break;
15383                           }
15384                         else
15385                           unqualified_name = constructor_name (class_type);
15386                       }
15387                     else
15388                       {
15389                         /* We do not attempt to print the declarator
15390                            here because we do not have enough
15391                            information about its original syntactic
15392                            form.  */
15393                         cp_parser_error (parser, "invalid declarator");
15394                         declarator = cp_error_declarator;
15395                         break;
15396                       }
15397                   }
15398
15399                 if (class_type)
15400                   {
15401                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15402                       sfk = sfk_destructor;
15403                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15404                       sfk = sfk_conversion;
15405                     else if (/* There's no way to declare a constructor
15406                                 for an anonymous type, even if the type
15407                                 got a name for linkage purposes.  */
15408                              !TYPE_WAS_ANONYMOUS (class_type)
15409                              && constructor_name_p (unqualified_name,
15410                                                     class_type))
15411                       {
15412                         unqualified_name = constructor_name (class_type);
15413                         sfk = sfk_constructor;
15414                       }
15415                     else if (is_overloaded_fn (unqualified_name)
15416                              && DECL_CONSTRUCTOR_P (get_first_fn
15417                                                     (unqualified_name)))
15418                       sfk = sfk_constructor;
15419
15420                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15421                       *ctor_dtor_or_conv_p = -1;
15422                   }
15423               }
15424             declarator = make_id_declarator (qualifying_scope,
15425                                              unqualified_name,
15426                                              sfk);
15427             declarator->id_loc = token->location;
15428             declarator->parameter_pack_p = pack_expansion_p;
15429
15430             if (pack_expansion_p)
15431               maybe_warn_variadic_templates ();
15432           }
15433
15434         handle_declarator:;
15435           scope = get_scope_of_declarator (declarator);
15436           if (scope)
15437             /* Any names that appear after the declarator-id for a
15438                member are looked up in the containing scope.  */
15439             pushed_scope = push_scope (scope);
15440           parser->in_declarator_p = true;
15441           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15442               || (declarator && declarator->kind == cdk_id))
15443             /* Default args are only allowed on function
15444                declarations.  */
15445             parser->default_arg_ok_p = saved_default_arg_ok_p;
15446           else
15447             parser->default_arg_ok_p = false;
15448
15449           first = false;
15450         }
15451       /* We're done.  */
15452       else
15453         break;
15454     }
15455
15456   /* For an abstract declarator, we might wind up with nothing at this
15457      point.  That's an error; the declarator is not optional.  */
15458   if (!declarator)
15459     cp_parser_error (parser, "expected declarator");
15460
15461   /* If we entered a scope, we must exit it now.  */
15462   if (pushed_scope)
15463     pop_scope (pushed_scope);
15464
15465   parser->default_arg_ok_p = saved_default_arg_ok_p;
15466   parser->in_declarator_p = saved_in_declarator_p;
15467
15468   return declarator;
15469 }
15470
15471 /* Parse a ptr-operator.
15472
15473    ptr-operator:
15474      * cv-qualifier-seq [opt]
15475      &
15476      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15477
15478    GNU Extension:
15479
15480    ptr-operator:
15481      & cv-qualifier-seq [opt]
15482
15483    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15484    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15485    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15486    filled in with the TYPE containing the member.  *CV_QUALS is
15487    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15488    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15489    Note that the tree codes returned by this function have nothing
15490    to do with the types of trees that will be eventually be created
15491    to represent the pointer or reference type being parsed. They are
15492    just constants with suggestive names. */
15493 static enum tree_code
15494 cp_parser_ptr_operator (cp_parser* parser,
15495                         tree* type,
15496                         cp_cv_quals *cv_quals)
15497 {
15498   enum tree_code code = ERROR_MARK;
15499   cp_token *token;
15500
15501   /* Assume that it's not a pointer-to-member.  */
15502   *type = NULL_TREE;
15503   /* And that there are no cv-qualifiers.  */
15504   *cv_quals = TYPE_UNQUALIFIED;
15505
15506   /* Peek at the next token.  */
15507   token = cp_lexer_peek_token (parser->lexer);
15508
15509   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15510   if (token->type == CPP_MULT)
15511     code = INDIRECT_REF;
15512   else if (token->type == CPP_AND)
15513     code = ADDR_EXPR;
15514   else if ((cxx_dialect != cxx98) &&
15515            token->type == CPP_AND_AND) /* C++0x only */
15516     code = NON_LVALUE_EXPR;
15517
15518   if (code != ERROR_MARK)
15519     {
15520       /* Consume the `*', `&' or `&&'.  */
15521       cp_lexer_consume_token (parser->lexer);
15522
15523       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15524          `&', if we are allowing GNU extensions.  (The only qualifier
15525          that can legally appear after `&' is `restrict', but that is
15526          enforced during semantic analysis.  */
15527       if (code == INDIRECT_REF
15528           || cp_parser_allow_gnu_extensions_p (parser))
15529         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15530     }
15531   else
15532     {
15533       /* Try the pointer-to-member case.  */
15534       cp_parser_parse_tentatively (parser);
15535       /* Look for the optional `::' operator.  */
15536       cp_parser_global_scope_opt (parser,
15537                                   /*current_scope_valid_p=*/false);
15538       /* Look for the nested-name specifier.  */
15539       token = cp_lexer_peek_token (parser->lexer);
15540       cp_parser_nested_name_specifier (parser,
15541                                        /*typename_keyword_p=*/false,
15542                                        /*check_dependency_p=*/true,
15543                                        /*type_p=*/false,
15544                                        /*is_declaration=*/false);
15545       /* If we found it, and the next token is a `*', then we are
15546          indeed looking at a pointer-to-member operator.  */
15547       if (!cp_parser_error_occurred (parser)
15548           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15549         {
15550           /* Indicate that the `*' operator was used.  */
15551           code = INDIRECT_REF;
15552
15553           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15554             error_at (token->location, "%qD is a namespace", parser->scope);
15555           else
15556             {
15557               /* The type of which the member is a member is given by the
15558                  current SCOPE.  */
15559               *type = parser->scope;
15560               /* The next name will not be qualified.  */
15561               parser->scope = NULL_TREE;
15562               parser->qualifying_scope = NULL_TREE;
15563               parser->object_scope = NULL_TREE;
15564               /* Look for the optional cv-qualifier-seq.  */
15565               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15566             }
15567         }
15568       /* If that didn't work we don't have a ptr-operator.  */
15569       if (!cp_parser_parse_definitely (parser))
15570         cp_parser_error (parser, "expected ptr-operator");
15571     }
15572
15573   return code;
15574 }
15575
15576 /* Parse an (optional) cv-qualifier-seq.
15577
15578    cv-qualifier-seq:
15579      cv-qualifier cv-qualifier-seq [opt]
15580
15581    cv-qualifier:
15582      const
15583      volatile
15584
15585    GNU Extension:
15586
15587    cv-qualifier:
15588      __restrict__
15589
15590    Returns a bitmask representing the cv-qualifiers.  */
15591
15592 static cp_cv_quals
15593 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15594 {
15595   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15596
15597   while (true)
15598     {
15599       cp_token *token;
15600       cp_cv_quals cv_qualifier;
15601
15602       /* Peek at the next token.  */
15603       token = cp_lexer_peek_token (parser->lexer);
15604       /* See if it's a cv-qualifier.  */
15605       switch (token->keyword)
15606         {
15607         case RID_CONST:
15608           cv_qualifier = TYPE_QUAL_CONST;
15609           break;
15610
15611         case RID_VOLATILE:
15612           cv_qualifier = TYPE_QUAL_VOLATILE;
15613           break;
15614
15615         case RID_RESTRICT:
15616           cv_qualifier = TYPE_QUAL_RESTRICT;
15617           break;
15618
15619         default:
15620           cv_qualifier = TYPE_UNQUALIFIED;
15621           break;
15622         }
15623
15624       if (!cv_qualifier)
15625         break;
15626
15627       if (cv_quals & cv_qualifier)
15628         {
15629           error_at (token->location, "duplicate cv-qualifier");
15630           cp_lexer_purge_token (parser->lexer);
15631         }
15632       else
15633         {
15634           cp_lexer_consume_token (parser->lexer);
15635           cv_quals |= cv_qualifier;
15636         }
15637     }
15638
15639   return cv_quals;
15640 }
15641
15642 /* Parse an (optional) virt-specifier-seq.
15643
15644    virt-specifier-seq:
15645      virt-specifier virt-specifier-seq [opt]
15646
15647    virt-specifier:
15648      override
15649      final
15650
15651    Returns a bitmask representing the virt-specifiers.  */
15652
15653 static cp_virt_specifiers
15654 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15655 {
15656   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15657
15658   while (true)
15659     {
15660       cp_token *token;
15661       cp_virt_specifiers virt_specifier;
15662
15663       /* Peek at the next token.  */
15664       token = cp_lexer_peek_token (parser->lexer);
15665       /* See if it's a virt-specifier-qualifier.  */
15666       if (token->type != CPP_NAME)
15667         break;
15668       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15669         {
15670           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15671           virt_specifier = VIRT_SPEC_OVERRIDE;
15672         }
15673       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15674         {
15675           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15676           virt_specifier = VIRT_SPEC_FINAL;
15677         }
15678       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
15679         {
15680           virt_specifier = VIRT_SPEC_FINAL;
15681         }
15682       else
15683         break;
15684
15685       if (virt_specifiers & virt_specifier)
15686         {
15687           error_at (token->location, "duplicate virt-specifier");
15688           cp_lexer_purge_token (parser->lexer);
15689         }
15690       else
15691         {
15692           cp_lexer_consume_token (parser->lexer);
15693           virt_specifiers |= virt_specifier;
15694         }
15695     }
15696   return virt_specifiers;
15697 }
15698
15699 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
15700    is in scope even though it isn't real.  */
15701
15702 static void
15703 inject_this_parameter (tree ctype, cp_cv_quals quals)
15704 {
15705   tree this_parm;
15706
15707   if (current_class_ptr)
15708     {
15709       /* We don't clear this between NSDMIs.  Is it already what we want?  */
15710       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
15711       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
15712           && cp_type_quals (type) == quals)
15713         return;
15714     }
15715
15716   this_parm = build_this_parm (ctype, quals);
15717   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
15718   current_class_ptr = NULL_TREE;
15719   current_class_ref
15720     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15721   current_class_ptr = this_parm;
15722 }
15723
15724 /* Parse a late-specified return type, if any.  This is not a separate
15725    non-terminal, but part of a function declarator, which looks like
15726
15727    -> trailing-type-specifier-seq abstract-declarator(opt)
15728
15729    Returns the type indicated by the type-id.
15730
15731    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15732    function.  */
15733
15734 static tree
15735 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15736 {
15737   cp_token *token;
15738   tree type;
15739
15740   /* Peek at the next token.  */
15741   token = cp_lexer_peek_token (parser->lexer);
15742   /* A late-specified return type is indicated by an initial '->'. */
15743   if (token->type != CPP_DEREF)
15744     return NULL_TREE;
15745
15746   /* Consume the ->.  */
15747   cp_lexer_consume_token (parser->lexer);
15748
15749   if (quals >= 0)
15750     {
15751       /* DR 1207: 'this' is in scope in the trailing return type.  */
15752       gcc_assert (current_class_ptr == NULL_TREE);
15753       inject_this_parameter (current_class_type, quals);
15754     }
15755
15756   type = cp_parser_trailing_type_id (parser);
15757
15758   if (quals >= 0)
15759     current_class_ptr = current_class_ref = NULL_TREE;
15760
15761   return type;
15762 }
15763
15764 /* Parse a declarator-id.
15765
15766    declarator-id:
15767      id-expression
15768      :: [opt] nested-name-specifier [opt] type-name
15769
15770    In the `id-expression' case, the value returned is as for
15771    cp_parser_id_expression if the id-expression was an unqualified-id.
15772    If the id-expression was a qualified-id, then a SCOPE_REF is
15773    returned.  The first operand is the scope (either a NAMESPACE_DECL
15774    or TREE_TYPE), but the second is still just a representation of an
15775    unqualified-id.  */
15776
15777 static tree
15778 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15779 {
15780   tree id;
15781   /* The expression must be an id-expression.  Assume that qualified
15782      names are the names of types so that:
15783
15784        template <class T>
15785        int S<T>::R::i = 3;
15786
15787      will work; we must treat `S<T>::R' as the name of a type.
15788      Similarly, assume that qualified names are templates, where
15789      required, so that:
15790
15791        template <class T>
15792        int S<T>::R<T>::i = 3;
15793
15794      will work, too.  */
15795   id = cp_parser_id_expression (parser,
15796                                 /*template_keyword_p=*/false,
15797                                 /*check_dependency_p=*/false,
15798                                 /*template_p=*/NULL,
15799                                 /*declarator_p=*/true,
15800                                 optional_p);
15801   if (id && BASELINK_P (id))
15802     id = BASELINK_FUNCTIONS (id);
15803   return id;
15804 }
15805
15806 /* Parse a type-id.
15807
15808    type-id:
15809      type-specifier-seq abstract-declarator [opt]
15810
15811    Returns the TYPE specified.  */
15812
15813 static tree
15814 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15815                      bool is_trailing_return)
15816 {
15817   cp_decl_specifier_seq type_specifier_seq;
15818   cp_declarator *abstract_declarator;
15819
15820   /* Parse the type-specifier-seq.  */
15821   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15822                                 is_trailing_return,
15823                                 &type_specifier_seq);
15824   if (type_specifier_seq.type == error_mark_node)
15825     return error_mark_node;
15826
15827   /* There might or might not be an abstract declarator.  */
15828   cp_parser_parse_tentatively (parser);
15829   /* Look for the declarator.  */
15830   abstract_declarator
15831     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15832                             /*parenthesized_p=*/NULL,
15833                             /*member_p=*/false);
15834   /* Check to see if there really was a declarator.  */
15835   if (!cp_parser_parse_definitely (parser))
15836     abstract_declarator = NULL;
15837
15838   if (type_specifier_seq.type
15839       && type_uses_auto (type_specifier_seq.type))
15840     {
15841       /* A type-id with type 'auto' is only ok if the abstract declarator
15842          is a function declarator with a late-specified return type.  */
15843       if (abstract_declarator
15844           && abstract_declarator->kind == cdk_function
15845           && abstract_declarator->u.function.late_return_type)
15846         /* OK */;
15847       else
15848         {
15849           error ("invalid use of %<auto%>");
15850           return error_mark_node;
15851         }
15852     }
15853   
15854   return groktypename (&type_specifier_seq, abstract_declarator,
15855                        is_template_arg);
15856 }
15857
15858 static tree cp_parser_type_id (cp_parser *parser)
15859 {
15860   return cp_parser_type_id_1 (parser, false, false);
15861 }
15862
15863 static tree cp_parser_template_type_arg (cp_parser *parser)
15864 {
15865   tree r;
15866   const char *saved_message = parser->type_definition_forbidden_message;
15867   parser->type_definition_forbidden_message
15868     = G_("types may not be defined in template arguments");
15869   r = cp_parser_type_id_1 (parser, true, false);
15870   parser->type_definition_forbidden_message = saved_message;
15871   return r;
15872 }
15873
15874 static tree cp_parser_trailing_type_id (cp_parser *parser)
15875 {
15876   return cp_parser_type_id_1 (parser, false, true);
15877 }
15878
15879 /* Parse a type-specifier-seq.
15880
15881    type-specifier-seq:
15882      type-specifier type-specifier-seq [opt]
15883
15884    GNU extension:
15885
15886    type-specifier-seq:
15887      attributes type-specifier-seq [opt]
15888
15889    If IS_DECLARATION is true, we are at the start of a "condition" or
15890    exception-declaration, so we might be followed by a declarator-id.
15891
15892    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15893    i.e. we've just seen "->".
15894
15895    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15896
15897 static void
15898 cp_parser_type_specifier_seq (cp_parser* parser,
15899                               bool is_declaration,
15900                               bool is_trailing_return,
15901                               cp_decl_specifier_seq *type_specifier_seq)
15902 {
15903   bool seen_type_specifier = false;
15904   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15905   cp_token *start_token = NULL;
15906
15907   /* Clear the TYPE_SPECIFIER_SEQ.  */
15908   clear_decl_specs (type_specifier_seq);
15909
15910   /* In the context of a trailing return type, enum E { } is an
15911      elaborated-type-specifier followed by a function-body, not an
15912      enum-specifier.  */
15913   if (is_trailing_return)
15914     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15915
15916   /* Parse the type-specifiers and attributes.  */
15917   while (true)
15918     {
15919       tree type_specifier;
15920       bool is_cv_qualifier;
15921
15922       /* Check for attributes first.  */
15923       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15924         {
15925           type_specifier_seq->attributes =
15926             chainon (type_specifier_seq->attributes,
15927                      cp_parser_attributes_opt (parser));
15928           continue;
15929         }
15930
15931       /* record the token of the beginning of the type specifier seq,
15932          for error reporting purposes*/
15933      if (!start_token)
15934        start_token = cp_lexer_peek_token (parser->lexer);
15935
15936       /* Look for the type-specifier.  */
15937       type_specifier = cp_parser_type_specifier (parser,
15938                                                  flags,
15939                                                  type_specifier_seq,
15940                                                  /*is_declaration=*/false,
15941                                                  NULL,
15942                                                  &is_cv_qualifier);
15943       if (!type_specifier)
15944         {
15945           /* If the first type-specifier could not be found, this is not a
15946              type-specifier-seq at all.  */
15947           if (!seen_type_specifier)
15948             {
15949               cp_parser_error (parser, "expected type-specifier");
15950               type_specifier_seq->type = error_mark_node;
15951               return;
15952             }
15953           /* If subsequent type-specifiers could not be found, the
15954              type-specifier-seq is complete.  */
15955           break;
15956         }
15957
15958       seen_type_specifier = true;
15959       /* The standard says that a condition can be:
15960
15961             type-specifier-seq declarator = assignment-expression
15962
15963          However, given:
15964
15965            struct S {};
15966            if (int S = ...)
15967
15968          we should treat the "S" as a declarator, not as a
15969          type-specifier.  The standard doesn't say that explicitly for
15970          type-specifier-seq, but it does say that for
15971          decl-specifier-seq in an ordinary declaration.  Perhaps it
15972          would be clearer just to allow a decl-specifier-seq here, and
15973          then add a semantic restriction that if any decl-specifiers
15974          that are not type-specifiers appear, the program is invalid.  */
15975       if (is_declaration && !is_cv_qualifier)
15976         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15977     }
15978
15979   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15980 }
15981
15982 /* Parse a parameter-declaration-clause.
15983
15984    parameter-declaration-clause:
15985      parameter-declaration-list [opt] ... [opt]
15986      parameter-declaration-list , ...
15987
15988    Returns a representation for the parameter declarations.  A return
15989    value of NULL indicates a parameter-declaration-clause consisting
15990    only of an ellipsis.  */
15991
15992 static tree
15993 cp_parser_parameter_declaration_clause (cp_parser* parser)
15994 {
15995   tree parameters;
15996   cp_token *token;
15997   bool ellipsis_p;
15998   bool is_error;
15999
16000   /* Peek at the next token.  */
16001   token = cp_lexer_peek_token (parser->lexer);
16002   /* Check for trivial parameter-declaration-clauses.  */
16003   if (token->type == CPP_ELLIPSIS)
16004     {
16005       /* Consume the `...' token.  */
16006       cp_lexer_consume_token (parser->lexer);
16007       return NULL_TREE;
16008     }
16009   else if (token->type == CPP_CLOSE_PAREN)
16010     /* There are no parameters.  */
16011     {
16012 #ifndef NO_IMPLICIT_EXTERN_C
16013       if (in_system_header && current_class_type == NULL
16014           && current_lang_name == lang_name_c)
16015         return NULL_TREE;
16016       else
16017 #endif
16018         return void_list_node;
16019     }
16020   /* Check for `(void)', too, which is a special case.  */
16021   else if (token->keyword == RID_VOID
16022            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16023                == CPP_CLOSE_PAREN))
16024     {
16025       /* Consume the `void' token.  */
16026       cp_lexer_consume_token (parser->lexer);
16027       /* There are no parameters.  */
16028       return void_list_node;
16029     }
16030
16031   /* Parse the parameter-declaration-list.  */
16032   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16033   /* If a parse error occurred while parsing the
16034      parameter-declaration-list, then the entire
16035      parameter-declaration-clause is erroneous.  */
16036   if (is_error)
16037     return NULL;
16038
16039   /* Peek at the next token.  */
16040   token = cp_lexer_peek_token (parser->lexer);
16041   /* If it's a `,', the clause should terminate with an ellipsis.  */
16042   if (token->type == CPP_COMMA)
16043     {
16044       /* Consume the `,'.  */
16045       cp_lexer_consume_token (parser->lexer);
16046       /* Expect an ellipsis.  */
16047       ellipsis_p
16048         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16049     }
16050   /* It might also be `...' if the optional trailing `,' was
16051      omitted.  */
16052   else if (token->type == CPP_ELLIPSIS)
16053     {
16054       /* Consume the `...' token.  */
16055       cp_lexer_consume_token (parser->lexer);
16056       /* And remember that we saw it.  */
16057       ellipsis_p = true;
16058     }
16059   else
16060     ellipsis_p = false;
16061
16062   /* Finish the parameter list.  */
16063   if (!ellipsis_p)
16064     parameters = chainon (parameters, void_list_node);
16065
16066   return parameters;
16067 }
16068
16069 /* Parse a parameter-declaration-list.
16070
16071    parameter-declaration-list:
16072      parameter-declaration
16073      parameter-declaration-list , parameter-declaration
16074
16075    Returns a representation of the parameter-declaration-list, as for
16076    cp_parser_parameter_declaration_clause.  However, the
16077    `void_list_node' is never appended to the list.  Upon return,
16078    *IS_ERROR will be true iff an error occurred.  */
16079
16080 static tree
16081 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16082 {
16083   tree parameters = NULL_TREE;
16084   tree *tail = &parameters; 
16085   bool saved_in_unbraced_linkage_specification_p;
16086   int index = 0;
16087
16088   /* Assume all will go well.  */
16089   *is_error = false;
16090   /* The special considerations that apply to a function within an
16091      unbraced linkage specifications do not apply to the parameters
16092      to the function.  */
16093   saved_in_unbraced_linkage_specification_p 
16094     = parser->in_unbraced_linkage_specification_p;
16095   parser->in_unbraced_linkage_specification_p = false;
16096
16097   /* Look for more parameters.  */
16098   while (true)
16099     {
16100       cp_parameter_declarator *parameter;
16101       tree decl = error_mark_node;
16102       bool parenthesized_p = false;
16103       /* Parse the parameter.  */
16104       parameter
16105         = cp_parser_parameter_declaration (parser,
16106                                            /*template_parm_p=*/false,
16107                                            &parenthesized_p);
16108
16109       /* We don't know yet if the enclosing context is deprecated, so wait
16110          and warn in grokparms if appropriate.  */
16111       deprecated_state = DEPRECATED_SUPPRESS;
16112
16113       if (parameter)
16114         decl = grokdeclarator (parameter->declarator,
16115                                &parameter->decl_specifiers,
16116                                PARM,
16117                                parameter->default_argument != NULL_TREE,
16118                                &parameter->decl_specifiers.attributes);
16119
16120       deprecated_state = DEPRECATED_NORMAL;
16121
16122       /* If a parse error occurred parsing the parameter declaration,
16123          then the entire parameter-declaration-list is erroneous.  */
16124       if (decl == error_mark_node)
16125         {
16126           *is_error = true;
16127           parameters = error_mark_node;
16128           break;
16129         }
16130
16131       if (parameter->decl_specifiers.attributes)
16132         cplus_decl_attributes (&decl,
16133                                parameter->decl_specifiers.attributes,
16134                                0);
16135       if (DECL_NAME (decl))
16136         decl = pushdecl (decl);
16137
16138       if (decl != error_mark_node)
16139         {
16140           retrofit_lang_decl (decl);
16141           DECL_PARM_INDEX (decl) = ++index;
16142           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16143         }
16144
16145       /* Add the new parameter to the list.  */
16146       *tail = build_tree_list (parameter->default_argument, decl);
16147       tail = &TREE_CHAIN (*tail);
16148
16149       /* Peek at the next token.  */
16150       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16151           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16152           /* These are for Objective-C++ */
16153           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16154           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16155         /* The parameter-declaration-list is complete.  */
16156         break;
16157       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16158         {
16159           cp_token *token;
16160
16161           /* Peek at the next token.  */
16162           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16163           /* If it's an ellipsis, then the list is complete.  */
16164           if (token->type == CPP_ELLIPSIS)
16165             break;
16166           /* Otherwise, there must be more parameters.  Consume the
16167              `,'.  */
16168           cp_lexer_consume_token (parser->lexer);
16169           /* When parsing something like:
16170
16171                 int i(float f, double d)
16172
16173              we can tell after seeing the declaration for "f" that we
16174              are not looking at an initialization of a variable "i",
16175              but rather at the declaration of a function "i".
16176
16177              Due to the fact that the parsing of template arguments
16178              (as specified to a template-id) requires backtracking we
16179              cannot use this technique when inside a template argument
16180              list.  */
16181           if (!parser->in_template_argument_list_p
16182               && !parser->in_type_id_in_expr_p
16183               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16184               /* However, a parameter-declaration of the form
16185                  "foat(f)" (which is a valid declaration of a
16186                  parameter "f") can also be interpreted as an
16187                  expression (the conversion of "f" to "float").  */
16188               && !parenthesized_p)
16189             cp_parser_commit_to_tentative_parse (parser);
16190         }
16191       else
16192         {
16193           cp_parser_error (parser, "expected %<,%> or %<...%>");
16194           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16195             cp_parser_skip_to_closing_parenthesis (parser,
16196                                                    /*recovering=*/true,
16197                                                    /*or_comma=*/false,
16198                                                    /*consume_paren=*/false);
16199           break;
16200         }
16201     }
16202
16203   parser->in_unbraced_linkage_specification_p
16204     = saved_in_unbraced_linkage_specification_p;
16205
16206   return parameters;
16207 }
16208
16209 /* Parse a parameter declaration.
16210
16211    parameter-declaration:
16212      decl-specifier-seq ... [opt] declarator
16213      decl-specifier-seq declarator = assignment-expression
16214      decl-specifier-seq ... [opt] abstract-declarator [opt]
16215      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16216
16217    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16218    declares a template parameter.  (In that case, a non-nested `>'
16219    token encountered during the parsing of the assignment-expression
16220    is not interpreted as a greater-than operator.)
16221
16222    Returns a representation of the parameter, or NULL if an error
16223    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16224    true iff the declarator is of the form "(p)".  */
16225
16226 static cp_parameter_declarator *
16227 cp_parser_parameter_declaration (cp_parser *parser,
16228                                  bool template_parm_p,
16229                                  bool *parenthesized_p)
16230 {
16231   int declares_class_or_enum;
16232   cp_decl_specifier_seq decl_specifiers;
16233   cp_declarator *declarator;
16234   tree default_argument;
16235   cp_token *token = NULL, *declarator_token_start = NULL;
16236   const char *saved_message;
16237
16238   /* In a template parameter, `>' is not an operator.
16239
16240      [temp.param]
16241
16242      When parsing a default template-argument for a non-type
16243      template-parameter, the first non-nested `>' is taken as the end
16244      of the template parameter-list rather than a greater-than
16245      operator.  */
16246
16247   /* Type definitions may not appear in parameter types.  */
16248   saved_message = parser->type_definition_forbidden_message;
16249   parser->type_definition_forbidden_message
16250     = G_("types may not be defined in parameter types");
16251
16252   /* Parse the declaration-specifiers.  */
16253   cp_parser_decl_specifier_seq (parser,
16254                                 CP_PARSER_FLAGS_NONE,
16255                                 &decl_specifiers,
16256                                 &declares_class_or_enum);
16257
16258   /* Complain about missing 'typename' or other invalid type names.  */
16259   if (!decl_specifiers.any_type_specifiers_p)
16260     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16261
16262   /* If an error occurred, there's no reason to attempt to parse the
16263      rest of the declaration.  */
16264   if (cp_parser_error_occurred (parser))
16265     {
16266       parser->type_definition_forbidden_message = saved_message;
16267       return NULL;
16268     }
16269
16270   /* Peek at the next token.  */
16271   token = cp_lexer_peek_token (parser->lexer);
16272
16273   /* If the next token is a `)', `,', `=', `>', or `...', then there
16274      is no declarator. However, when variadic templates are enabled,
16275      there may be a declarator following `...'.  */
16276   if (token->type == CPP_CLOSE_PAREN
16277       || token->type == CPP_COMMA
16278       || token->type == CPP_EQ
16279       || token->type == CPP_GREATER)
16280     {
16281       declarator = NULL;
16282       if (parenthesized_p)
16283         *parenthesized_p = false;
16284     }
16285   /* Otherwise, there should be a declarator.  */
16286   else
16287     {
16288       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16289       parser->default_arg_ok_p = false;
16290
16291       /* After seeing a decl-specifier-seq, if the next token is not a
16292          "(", there is no possibility that the code is a valid
16293          expression.  Therefore, if parsing tentatively, we commit at
16294          this point.  */
16295       if (!parser->in_template_argument_list_p
16296           /* In an expression context, having seen:
16297
16298                (int((char ...
16299
16300              we cannot be sure whether we are looking at a
16301              function-type (taking a "char" as a parameter) or a cast
16302              of some object of type "char" to "int".  */
16303           && !parser->in_type_id_in_expr_p
16304           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16305           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16306           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16307         cp_parser_commit_to_tentative_parse (parser);
16308       /* Parse the declarator.  */
16309       declarator_token_start = token;
16310       declarator = cp_parser_declarator (parser,
16311                                          CP_PARSER_DECLARATOR_EITHER,
16312                                          /*ctor_dtor_or_conv_p=*/NULL,
16313                                          parenthesized_p,
16314                                          /*member_p=*/false);
16315       parser->default_arg_ok_p = saved_default_arg_ok_p;
16316       /* After the declarator, allow more attributes.  */
16317       decl_specifiers.attributes
16318         = chainon (decl_specifiers.attributes,
16319                    cp_parser_attributes_opt (parser));
16320     }
16321
16322   /* If the next token is an ellipsis, and we have not seen a
16323      declarator name, and the type of the declarator contains parameter
16324      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16325      a parameter pack expansion expression. Otherwise, leave the
16326      ellipsis for a C-style variadic function. */
16327   token = cp_lexer_peek_token (parser->lexer);
16328   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16329     {
16330       tree type = decl_specifiers.type;
16331
16332       if (type && DECL_P (type))
16333         type = TREE_TYPE (type);
16334
16335       if (type
16336           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16337           && declarator_can_be_parameter_pack (declarator)
16338           && (!declarator || !declarator->parameter_pack_p)
16339           && uses_parameter_packs (type))
16340         {
16341           /* Consume the `...'. */
16342           cp_lexer_consume_token (parser->lexer);
16343           maybe_warn_variadic_templates ();
16344           
16345           /* Build a pack expansion type */
16346           if (declarator)
16347             declarator->parameter_pack_p = true;
16348           else
16349             decl_specifiers.type = make_pack_expansion (type);
16350         }
16351     }
16352
16353   /* The restriction on defining new types applies only to the type
16354      of the parameter, not to the default argument.  */
16355   parser->type_definition_forbidden_message = saved_message;
16356
16357   /* If the next token is `=', then process a default argument.  */
16358   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16359     {
16360       /* If we are defining a class, then the tokens that make up the
16361          default argument must be saved and processed later.  */
16362       if (!template_parm_p && at_class_scope_p ()
16363           && TYPE_BEING_DEFINED (current_class_type)
16364           && !LAMBDA_TYPE_P (current_class_type))
16365         {
16366           unsigned depth = 0;
16367           int maybe_template_id = 0;
16368           cp_token *first_token;
16369           cp_token *token;
16370
16371           /* Add tokens until we have processed the entire default
16372              argument.  We add the range [first_token, token).  */
16373           first_token = cp_lexer_peek_token (parser->lexer);
16374           while (true)
16375             {
16376               bool done = false;
16377
16378               /* Peek at the next token.  */
16379               token = cp_lexer_peek_token (parser->lexer);
16380               /* What we do depends on what token we have.  */
16381               switch (token->type)
16382                 {
16383                   /* In valid code, a default argument must be
16384                      immediately followed by a `,' `)', or `...'.  */
16385                 case CPP_COMMA:
16386                   if (depth == 0 && maybe_template_id)
16387                     {
16388                       /* If we've seen a '<', we might be in a
16389                          template-argument-list.  Until Core issue 325 is
16390                          resolved, we don't know how this situation ought
16391                          to be handled, so try to DTRT.  We check whether
16392                          what comes after the comma is a valid parameter
16393                          declaration list.  If it is, then the comma ends
16394                          the default argument; otherwise the default
16395                          argument continues.  */
16396                       bool error = false;
16397                       tree t;
16398
16399                       /* Set ITALP so cp_parser_parameter_declaration_list
16400                          doesn't decide to commit to this parse.  */
16401                       bool saved_italp = parser->in_template_argument_list_p;
16402                       parser->in_template_argument_list_p = true;
16403
16404                       cp_parser_parse_tentatively (parser);
16405                       cp_lexer_consume_token (parser->lexer);
16406                       begin_scope (sk_function_parms, NULL_TREE);
16407                       cp_parser_parameter_declaration_list (parser, &error);
16408                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16409                         pop_binding (DECL_NAME (t), t);
16410                       leave_scope ();
16411                       if (!cp_parser_error_occurred (parser) && !error)
16412                         done = true;
16413                       cp_parser_abort_tentative_parse (parser);
16414
16415                       parser->in_template_argument_list_p = saved_italp;
16416                       break;
16417                     }
16418                 case CPP_CLOSE_PAREN:
16419                 case CPP_ELLIPSIS:
16420                   /* If we run into a non-nested `;', `}', or `]',
16421                      then the code is invalid -- but the default
16422                      argument is certainly over.  */
16423                 case CPP_SEMICOLON:
16424                 case CPP_CLOSE_BRACE:
16425                 case CPP_CLOSE_SQUARE:
16426                   if (depth == 0)
16427                     done = true;
16428                   /* Update DEPTH, if necessary.  */
16429                   else if (token->type == CPP_CLOSE_PAREN
16430                            || token->type == CPP_CLOSE_BRACE
16431                            || token->type == CPP_CLOSE_SQUARE)
16432                     --depth;
16433                   break;
16434
16435                 case CPP_OPEN_PAREN:
16436                 case CPP_OPEN_SQUARE:
16437                 case CPP_OPEN_BRACE:
16438                   ++depth;
16439                   break;
16440
16441                 case CPP_LESS:
16442                   if (depth == 0)
16443                     /* This might be the comparison operator, or it might
16444                        start a template argument list.  */
16445                     ++maybe_template_id;
16446                   break;
16447
16448                 case CPP_RSHIFT:
16449                   if (cxx_dialect == cxx98)
16450                     break;
16451                   /* Fall through for C++0x, which treats the `>>'
16452                      operator like two `>' tokens in certain
16453                      cases.  */
16454
16455                 case CPP_GREATER:
16456                   if (depth == 0)
16457                     {
16458                       /* This might be an operator, or it might close a
16459                          template argument list.  But if a previous '<'
16460                          started a template argument list, this will have
16461                          closed it, so we can't be in one anymore.  */
16462                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16463                       if (maybe_template_id < 0)
16464                         maybe_template_id = 0;
16465                     }
16466                   break;
16467
16468                   /* If we run out of tokens, issue an error message.  */
16469                 case CPP_EOF:
16470                 case CPP_PRAGMA_EOL:
16471                   error_at (token->location, "file ends in default argument");
16472                   done = true;
16473                   break;
16474
16475                 case CPP_NAME:
16476                 case CPP_SCOPE:
16477                   /* In these cases, we should look for template-ids.
16478                      For example, if the default argument is
16479                      `X<int, double>()', we need to do name lookup to
16480                      figure out whether or not `X' is a template; if
16481                      so, the `,' does not end the default argument.
16482
16483                      That is not yet done.  */
16484                   break;
16485
16486                 default:
16487                   break;
16488                 }
16489
16490               /* If we've reached the end, stop.  */
16491               if (done)
16492                 break;
16493
16494               /* Add the token to the token block.  */
16495               token = cp_lexer_consume_token (parser->lexer);
16496             }
16497
16498           /* Create a DEFAULT_ARG to represent the unparsed default
16499              argument.  */
16500           default_argument = make_node (DEFAULT_ARG);
16501           DEFARG_TOKENS (default_argument)
16502             = cp_token_cache_new (first_token, token);
16503           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16504         }
16505       /* Outside of a class definition, we can just parse the
16506          assignment-expression.  */
16507       else
16508         {
16509           token = cp_lexer_peek_token (parser->lexer);
16510           default_argument 
16511             = cp_parser_default_argument (parser, template_parm_p);
16512         }
16513
16514       if (!parser->default_arg_ok_p)
16515         {
16516           if (flag_permissive)
16517             warning (0, "deprecated use of default argument for parameter of non-function");
16518           else
16519             {
16520               error_at (token->location,
16521                         "default arguments are only "
16522                         "permitted for function parameters");
16523               default_argument = NULL_TREE;
16524             }
16525         }
16526       else if ((declarator && declarator->parameter_pack_p)
16527                || (decl_specifiers.type
16528                    && PACK_EXPANSION_P (decl_specifiers.type)))
16529         {
16530           /* Find the name of the parameter pack.  */     
16531           cp_declarator *id_declarator = declarator;
16532           while (id_declarator && id_declarator->kind != cdk_id)
16533             id_declarator = id_declarator->declarator;
16534           
16535           if (id_declarator && id_declarator->kind == cdk_id)
16536             error_at (declarator_token_start->location,
16537                       template_parm_p 
16538                       ? "template parameter pack %qD"
16539                       " cannot have a default argument"
16540                       : "parameter pack %qD cannot have a default argument",
16541                       id_declarator->u.id.unqualified_name);
16542           else
16543             error_at (declarator_token_start->location,
16544                       template_parm_p 
16545                       ? "template parameter pack cannot have a default argument"
16546                       : "parameter pack cannot have a default argument");
16547           
16548           default_argument = NULL_TREE;
16549         }
16550     }
16551   else
16552     default_argument = NULL_TREE;
16553
16554   return make_parameter_declarator (&decl_specifiers,
16555                                     declarator,
16556                                     default_argument);
16557 }
16558
16559 /* Parse a default argument and return it.
16560
16561    TEMPLATE_PARM_P is true if this is a default argument for a
16562    non-type template parameter.  */
16563 static tree
16564 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16565 {
16566   tree default_argument = NULL_TREE;
16567   bool saved_greater_than_is_operator_p;
16568   bool saved_local_variables_forbidden_p;
16569   bool non_constant_p, is_direct_init;
16570
16571   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16572      set correctly.  */
16573   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16574   parser->greater_than_is_operator_p = !template_parm_p;
16575   /* Local variable names (and the `this' keyword) may not
16576      appear in a default argument.  */
16577   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16578   parser->local_variables_forbidden_p = true;
16579   /* Parse the assignment-expression.  */
16580   if (template_parm_p)
16581     push_deferring_access_checks (dk_no_deferred);
16582   default_argument
16583     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
16584   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
16585     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16586   if (template_parm_p)
16587     pop_deferring_access_checks ();
16588   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16589   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16590
16591   return default_argument;
16592 }
16593
16594 /* Parse a function-body.
16595
16596    function-body:
16597      compound_statement  */
16598
16599 static void
16600 cp_parser_function_body (cp_parser *parser)
16601 {
16602   cp_parser_compound_statement (parser, NULL, false, true);
16603 }
16604
16605 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16606    true if a ctor-initializer was present.  */
16607
16608 static bool
16609 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16610 {
16611   tree body, list;
16612   bool ctor_initializer_p;
16613   const bool check_body_p =
16614      DECL_CONSTRUCTOR_P (current_function_decl)
16615      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16616   tree last = NULL;
16617
16618   /* Begin the function body.  */
16619   body = begin_function_body ();
16620   /* Parse the optional ctor-initializer.  */
16621   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16622
16623   /* If we're parsing a constexpr constructor definition, we need
16624      to check that the constructor body is indeed empty.  However,
16625      before we get to cp_parser_function_body lot of junk has been
16626      generated, so we can't just check that we have an empty block.
16627      Rather we take a snapshot of the outermost block, and check whether
16628      cp_parser_function_body changed its state.  */
16629   if (check_body_p)
16630     {
16631       list = body;
16632       if (TREE_CODE (list) == BIND_EXPR)
16633         list = BIND_EXPR_BODY (list);
16634       if (TREE_CODE (list) == STATEMENT_LIST
16635           && STATEMENT_LIST_TAIL (list) != NULL)
16636         last = STATEMENT_LIST_TAIL (list)->stmt;
16637     }
16638   /* Parse the function-body.  */
16639   cp_parser_function_body (parser);
16640   if (check_body_p)
16641     check_constexpr_ctor_body (last, list);
16642   /* Finish the function body.  */
16643   finish_function_body (body);
16644
16645   return ctor_initializer_p;
16646 }
16647
16648 /* Parse an initializer.
16649
16650    initializer:
16651      = initializer-clause
16652      ( expression-list )
16653
16654    Returns an expression representing the initializer.  If no
16655    initializer is present, NULL_TREE is returned.
16656
16657    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16658    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16659    set to TRUE if there is no initializer present.  If there is an
16660    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16661    is set to true; otherwise it is set to false.  */
16662
16663 static tree
16664 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16665                        bool* non_constant_p)
16666 {
16667   cp_token *token;
16668   tree init;
16669
16670   /* Peek at the next token.  */
16671   token = cp_lexer_peek_token (parser->lexer);
16672
16673   /* Let our caller know whether or not this initializer was
16674      parenthesized.  */
16675   *is_direct_init = (token->type != CPP_EQ);
16676   /* Assume that the initializer is constant.  */
16677   *non_constant_p = false;
16678
16679   if (token->type == CPP_EQ)
16680     {
16681       /* Consume the `='.  */
16682       cp_lexer_consume_token (parser->lexer);
16683       /* Parse the initializer-clause.  */
16684       init = cp_parser_initializer_clause (parser, non_constant_p);
16685     }
16686   else if (token->type == CPP_OPEN_PAREN)
16687     {
16688       VEC(tree,gc) *vec;
16689       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16690                                                      /*cast_p=*/false,
16691                                                      /*allow_expansion_p=*/true,
16692                                                      non_constant_p);
16693       if (vec == NULL)
16694         return error_mark_node;
16695       init = build_tree_list_vec (vec);
16696       release_tree_vector (vec);
16697     }
16698   else if (token->type == CPP_OPEN_BRACE)
16699     {
16700       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16701       init = cp_parser_braced_list (parser, non_constant_p);
16702       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16703     }
16704   else
16705     {
16706       /* Anything else is an error.  */
16707       cp_parser_error (parser, "expected initializer");
16708       init = error_mark_node;
16709     }
16710
16711   return init;
16712 }
16713
16714 /* Parse an initializer-clause.
16715
16716    initializer-clause:
16717      assignment-expression
16718      braced-init-list
16719
16720    Returns an expression representing the initializer.
16721
16722    If the `assignment-expression' production is used the value
16723    returned is simply a representation for the expression.
16724
16725    Otherwise, calls cp_parser_braced_list.  */
16726
16727 static tree
16728 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16729 {
16730   tree initializer;
16731
16732   /* Assume the expression is constant.  */
16733   *non_constant_p = false;
16734
16735   /* If it is not a `{', then we are looking at an
16736      assignment-expression.  */
16737   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16738     {
16739       initializer
16740         = cp_parser_constant_expression (parser,
16741                                         /*allow_non_constant_p=*/true,
16742                                         non_constant_p);
16743     }
16744   else
16745     initializer = cp_parser_braced_list (parser, non_constant_p);
16746
16747   return initializer;
16748 }
16749
16750 /* Parse a brace-enclosed initializer list.
16751
16752    braced-init-list:
16753      { initializer-list , [opt] }
16754      { }
16755
16756    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16757    the elements of the initializer-list (or NULL, if the last
16758    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16759    NULL_TREE.  There is no way to detect whether or not the optional
16760    trailing `,' was provided.  NON_CONSTANT_P is as for
16761    cp_parser_initializer.  */     
16762
16763 static tree
16764 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16765 {
16766   tree initializer;
16767
16768   /* Consume the `{' token.  */
16769   cp_lexer_consume_token (parser->lexer);
16770   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16771   initializer = make_node (CONSTRUCTOR);
16772   /* If it's not a `}', then there is a non-trivial initializer.  */
16773   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16774     {
16775       /* Parse the initializer list.  */
16776       CONSTRUCTOR_ELTS (initializer)
16777         = cp_parser_initializer_list (parser, non_constant_p);
16778       /* A trailing `,' token is allowed.  */
16779       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16780         cp_lexer_consume_token (parser->lexer);
16781     }
16782   /* Now, there should be a trailing `}'.  */
16783   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16784   TREE_TYPE (initializer) = init_list_type_node;
16785   return initializer;
16786 }
16787
16788 /* Parse an initializer-list.
16789
16790    initializer-list:
16791      initializer-clause ... [opt]
16792      initializer-list , initializer-clause ... [opt]
16793
16794    GNU Extension:
16795
16796    initializer-list:
16797      designation initializer-clause ...[opt]
16798      initializer-list , designation initializer-clause ...[opt]
16799
16800    designation:
16801      . identifier =
16802      identifier :
16803      [ constant-expression ] =
16804
16805    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16806    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16807    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16808    as for cp_parser_initializer.  */
16809
16810 static VEC(constructor_elt,gc) *
16811 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16812 {
16813   VEC(constructor_elt,gc) *v = NULL;
16814
16815   /* Assume all of the expressions are constant.  */
16816   *non_constant_p = false;
16817
16818   /* Parse the rest of the list.  */
16819   while (true)
16820     {
16821       cp_token *token;
16822       tree designator;
16823       tree initializer;
16824       bool clause_non_constant_p;
16825
16826       /* If the next token is an identifier and the following one is a
16827          colon, we are looking at the GNU designated-initializer
16828          syntax.  */
16829       if (cp_parser_allow_gnu_extensions_p (parser)
16830           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16831           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16832         {
16833           /* Warn the user that they are using an extension.  */
16834           pedwarn (input_location, OPT_pedantic, 
16835                    "ISO C++ does not allow designated initializers");
16836           /* Consume the identifier.  */
16837           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16838           /* Consume the `:'.  */
16839           cp_lexer_consume_token (parser->lexer);
16840         }
16841       /* Also handle the C99 syntax, '. id ='.  */
16842       else if (cp_parser_allow_gnu_extensions_p (parser)
16843                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
16844                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
16845                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
16846         {
16847           /* Warn the user that they are using an extension.  */
16848           pedwarn (input_location, OPT_pedantic,
16849                    "ISO C++ does not allow C99 designated initializers");
16850           /* Consume the `.'.  */
16851           cp_lexer_consume_token (parser->lexer);
16852           /* Consume the identifier.  */
16853           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16854           /* Consume the `='.  */
16855           cp_lexer_consume_token (parser->lexer);
16856         }
16857       /* Also handle C99 array designators, '[ const ] ='.  */
16858       else if (cp_parser_allow_gnu_extensions_p (parser)
16859                && !c_dialect_objc ()
16860                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16861         {
16862           cp_lexer_consume_token (parser->lexer);
16863           designator = cp_parser_constant_expression (parser, false, NULL);
16864           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16865           cp_parser_require (parser, CPP_EQ, RT_EQ);
16866         }
16867       else
16868         designator = NULL_TREE;
16869
16870       /* Parse the initializer.  */
16871       initializer = cp_parser_initializer_clause (parser,
16872                                                   &clause_non_constant_p);
16873       /* If any clause is non-constant, so is the entire initializer.  */
16874       if (clause_non_constant_p)
16875         *non_constant_p = true;
16876
16877       /* If we have an ellipsis, this is an initializer pack
16878          expansion.  */
16879       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16880         {
16881           /* Consume the `...'.  */
16882           cp_lexer_consume_token (parser->lexer);
16883
16884           /* Turn the initializer into an initializer expansion.  */
16885           initializer = make_pack_expansion (initializer);
16886         }
16887
16888       /* Add it to the vector.  */
16889       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
16890
16891       /* If the next token is not a comma, we have reached the end of
16892          the list.  */
16893       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16894         break;
16895
16896       /* Peek at the next token.  */
16897       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16898       /* If the next token is a `}', then we're still done.  An
16899          initializer-clause can have a trailing `,' after the
16900          initializer-list and before the closing `}'.  */
16901       if (token->type == CPP_CLOSE_BRACE)
16902         break;
16903
16904       /* Consume the `,' token.  */
16905       cp_lexer_consume_token (parser->lexer);
16906     }
16907
16908   return v;
16909 }
16910
16911 /* Classes [gram.class] */
16912
16913 /* Parse a class-name.
16914
16915    class-name:
16916      identifier
16917      template-id
16918
16919    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16920    to indicate that names looked up in dependent types should be
16921    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16922    keyword has been used to indicate that the name that appears next
16923    is a template.  TAG_TYPE indicates the explicit tag given before
16924    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16925    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16926    is the class being defined in a class-head.
16927
16928    Returns the TYPE_DECL representing the class.  */
16929
16930 static tree
16931 cp_parser_class_name (cp_parser *parser,
16932                       bool typename_keyword_p,
16933                       bool template_keyword_p,
16934                       enum tag_types tag_type,
16935                       bool check_dependency_p,
16936                       bool class_head_p,
16937                       bool is_declaration)
16938 {
16939   tree decl;
16940   tree scope;
16941   bool typename_p;
16942   cp_token *token;
16943   tree identifier = NULL_TREE;
16944
16945   /* All class-names start with an identifier.  */
16946   token = cp_lexer_peek_token (parser->lexer);
16947   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16948     {
16949       cp_parser_error (parser, "expected class-name");
16950       return error_mark_node;
16951     }
16952
16953   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16954      to a template-id, so we save it here.  */
16955   scope = parser->scope;
16956   if (scope == error_mark_node)
16957     return error_mark_node;
16958
16959   /* Any name names a type if we're following the `typename' keyword
16960      in a qualified name where the enclosing scope is type-dependent.  */
16961   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16962                 && dependent_type_p (scope));
16963   /* Handle the common case (an identifier, but not a template-id)
16964      efficiently.  */
16965   if (token->type == CPP_NAME
16966       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16967     {
16968       cp_token *identifier_token;
16969       bool ambiguous_p;
16970
16971       /* Look for the identifier.  */
16972       identifier_token = cp_lexer_peek_token (parser->lexer);
16973       ambiguous_p = identifier_token->ambiguous_p;
16974       identifier = cp_parser_identifier (parser);
16975       /* If the next token isn't an identifier, we are certainly not
16976          looking at a class-name.  */
16977       if (identifier == error_mark_node)
16978         decl = error_mark_node;
16979       /* If we know this is a type-name, there's no need to look it
16980          up.  */
16981       else if (typename_p)
16982         decl = identifier;
16983       else
16984         {
16985           tree ambiguous_decls;
16986           /* If we already know that this lookup is ambiguous, then
16987              we've already issued an error message; there's no reason
16988              to check again.  */
16989           if (ambiguous_p)
16990             {
16991               cp_parser_simulate_error (parser);
16992               return error_mark_node;
16993             }
16994           /* If the next token is a `::', then the name must be a type
16995              name.
16996
16997              [basic.lookup.qual]
16998
16999              During the lookup for a name preceding the :: scope
17000              resolution operator, object, function, and enumerator
17001              names are ignored.  */
17002           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17003             tag_type = typename_type;
17004           /* Look up the name.  */
17005           decl = cp_parser_lookup_name (parser, identifier,
17006                                         tag_type,
17007                                         /*is_template=*/false,
17008                                         /*is_namespace=*/false,
17009                                         check_dependency_p,
17010                                         &ambiguous_decls,
17011                                         identifier_token->location);
17012           if (ambiguous_decls)
17013             {
17014               if (cp_parser_parsing_tentatively (parser))
17015                 cp_parser_simulate_error (parser);
17016               return error_mark_node;
17017             }
17018         }
17019     }
17020   else
17021     {
17022       /* Try a template-id.  */
17023       decl = cp_parser_template_id (parser, template_keyword_p,
17024                                     check_dependency_p,
17025                                     is_declaration);
17026       if (decl == error_mark_node)
17027         return error_mark_node;
17028     }
17029
17030   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17031
17032   /* If this is a typename, create a TYPENAME_TYPE.  */
17033   if (typename_p && decl != error_mark_node)
17034     {
17035       decl = make_typename_type (scope, decl, typename_type,
17036                                  /*complain=*/tf_error);
17037       if (decl != error_mark_node)
17038         decl = TYPE_NAME (decl);
17039     }
17040
17041   /* Check to see that it is really the name of a class.  */
17042   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17043       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17044       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17045     /* Situations like this:
17046
17047          template <typename T> struct A {
17048            typename T::template X<int>::I i;
17049          };
17050
17051        are problematic.  Is `T::template X<int>' a class-name?  The
17052        standard does not seem to be definitive, but there is no other
17053        valid interpretation of the following `::'.  Therefore, those
17054        names are considered class-names.  */
17055     {
17056       decl = make_typename_type (scope, decl, tag_type, tf_error);
17057       if (decl != error_mark_node)
17058         decl = TYPE_NAME (decl);
17059     }
17060   else if (TREE_CODE (decl) != TYPE_DECL
17061            || TREE_TYPE (decl) == error_mark_node
17062            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17063            /* In Objective-C 2.0, a classname followed by '.' starts a
17064               dot-syntax expression, and it's not a type-name.  */
17065            || (c_dialect_objc ()
17066                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17067                && objc_is_class_name (decl)))
17068     decl = error_mark_node;
17069
17070   if (decl == error_mark_node)
17071     cp_parser_error (parser, "expected class-name");
17072   else if (identifier && !parser->scope)
17073     maybe_note_name_used_in_class (identifier, decl);
17074
17075   return decl;
17076 }
17077
17078 /* Parse a class-specifier.
17079
17080    class-specifier:
17081      class-head { member-specification [opt] }
17082
17083    Returns the TREE_TYPE representing the class.  */
17084
17085 static tree
17086 cp_parser_class_specifier_1 (cp_parser* parser)
17087 {
17088   tree type;
17089   tree attributes = NULL_TREE;
17090   bool nested_name_specifier_p;
17091   unsigned saved_num_template_parameter_lists;
17092   bool saved_in_function_body;
17093   unsigned char in_statement;
17094   bool in_switch_statement_p;
17095   bool saved_in_unbraced_linkage_specification_p;
17096   tree old_scope = NULL_TREE;
17097   tree scope = NULL_TREE;
17098   tree bases;
17099   cp_token *closing_brace;
17100
17101   push_deferring_access_checks (dk_no_deferred);
17102
17103   /* Parse the class-head.  */
17104   type = cp_parser_class_head (parser,
17105                                &nested_name_specifier_p,
17106                                &attributes,
17107                                &bases);
17108   /* If the class-head was a semantic disaster, skip the entire body
17109      of the class.  */
17110   if (!type)
17111     {
17112       cp_parser_skip_to_end_of_block_or_statement (parser);
17113       pop_deferring_access_checks ();
17114       return error_mark_node;
17115     }
17116
17117   /* Look for the `{'.  */
17118   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17119     {
17120       pop_deferring_access_checks ();
17121       return error_mark_node;
17122     }
17123
17124   /* Process the base classes. If they're invalid, skip the 
17125      entire class body.  */
17126   if (!xref_basetypes (type, bases))
17127     {
17128       /* Consuming the closing brace yields better error messages
17129          later on.  */
17130       if (cp_parser_skip_to_closing_brace (parser))
17131         cp_lexer_consume_token (parser->lexer);
17132       pop_deferring_access_checks ();
17133       return error_mark_node;
17134     }
17135
17136   /* Issue an error message if type-definitions are forbidden here.  */
17137   cp_parser_check_type_definition (parser);
17138   /* Remember that we are defining one more class.  */
17139   ++parser->num_classes_being_defined;
17140   /* Inside the class, surrounding template-parameter-lists do not
17141      apply.  */
17142   saved_num_template_parameter_lists
17143     = parser->num_template_parameter_lists;
17144   parser->num_template_parameter_lists = 0;
17145   /* We are not in a function body.  */
17146   saved_in_function_body = parser->in_function_body;
17147   parser->in_function_body = false;
17148   /* Or in a loop.  */
17149   in_statement = parser->in_statement;
17150   parser->in_statement = 0;
17151   /* Or in a switch.  */
17152   in_switch_statement_p = parser->in_switch_statement_p;
17153   parser->in_switch_statement_p = false;
17154   /* We are not immediately inside an extern "lang" block.  */
17155   saved_in_unbraced_linkage_specification_p
17156     = parser->in_unbraced_linkage_specification_p;
17157   parser->in_unbraced_linkage_specification_p = false;
17158
17159   /* Start the class.  */
17160   if (nested_name_specifier_p)
17161     {
17162       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17163       old_scope = push_inner_scope (scope);
17164     }
17165   type = begin_class_definition (type, attributes);
17166
17167   if (type == error_mark_node)
17168     /* If the type is erroneous, skip the entire body of the class.  */
17169     cp_parser_skip_to_closing_brace (parser);
17170   else
17171     /* Parse the member-specification.  */
17172     cp_parser_member_specification_opt (parser);
17173
17174   /* Look for the trailing `}'.  */
17175   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17176   /* Look for trailing attributes to apply to this class.  */
17177   if (cp_parser_allow_gnu_extensions_p (parser))
17178     attributes = cp_parser_attributes_opt (parser);
17179   if (type != error_mark_node)
17180     type = finish_struct (type, attributes);
17181   if (nested_name_specifier_p)
17182     pop_inner_scope (old_scope, scope);
17183
17184   /* We've finished a type definition.  Check for the common syntax
17185      error of forgetting a semicolon after the definition.  We need to
17186      be careful, as we can't just check for not-a-semicolon and be done
17187      with it; the user might have typed:
17188
17189      class X { } c = ...;
17190      class X { } *p = ...;
17191
17192      and so forth.  Instead, enumerate all the possible tokens that
17193      might follow this production; if we don't see one of them, then
17194      complain and silently insert the semicolon.  */
17195   {
17196     cp_token *token = cp_lexer_peek_token (parser->lexer);
17197     bool want_semicolon = true;
17198
17199     switch (token->type)
17200       {
17201       case CPP_NAME:
17202       case CPP_SEMICOLON:
17203       case CPP_MULT:
17204       case CPP_AND:
17205       case CPP_OPEN_PAREN:
17206       case CPP_CLOSE_PAREN:
17207       case CPP_COMMA:
17208         want_semicolon = false;
17209         break;
17210
17211         /* While it's legal for type qualifiers and storage class
17212            specifiers to follow type definitions in the grammar, only
17213            compiler testsuites contain code like that.  Assume that if
17214            we see such code, then what we're really seeing is a case
17215            like:
17216
17217            class X { }
17218            const <type> var = ...;
17219
17220            or
17221
17222            class Y { }
17223            static <type> func (...) ...
17224
17225            i.e. the qualifier or specifier applies to the next
17226            declaration.  To do so, however, we need to look ahead one
17227            more token to see if *that* token is a type specifier.
17228
17229            This code could be improved to handle:
17230
17231            class Z { }
17232            static const <type> var = ...;  */
17233       case CPP_KEYWORD:
17234         if (keyword_is_decl_specifier (token->keyword))
17235           {
17236             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17237
17238             /* Handling user-defined types here would be nice, but very
17239                tricky.  */
17240             want_semicolon
17241               = (lookahead->type == CPP_KEYWORD
17242                  && keyword_begins_type_specifier (lookahead->keyword));
17243           }
17244         break;
17245       default:
17246         break;
17247       }
17248
17249     /* If we don't have a type, then something is very wrong and we
17250        shouldn't try to do anything clever.  Likewise for not seeing the
17251        closing brace.  */
17252     if (closing_brace && TYPE_P (type) && want_semicolon)
17253       {
17254         cp_token_position prev
17255           = cp_lexer_previous_token_position (parser->lexer);
17256         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17257         location_t loc = prev_token->location;
17258
17259         if (CLASSTYPE_DECLARED_CLASS (type))
17260           error_at (loc, "expected %<;%> after class definition");
17261         else if (TREE_CODE (type) == RECORD_TYPE)
17262           error_at (loc, "expected %<;%> after struct definition");
17263         else if (TREE_CODE (type) == UNION_TYPE)
17264           error_at (loc, "expected %<;%> after union definition");
17265         else
17266           gcc_unreachable ();
17267
17268         /* Unget one token and smash it to look as though we encountered
17269            a semicolon in the input stream.  */
17270         cp_lexer_set_token_position (parser->lexer, prev);
17271         token = cp_lexer_peek_token (parser->lexer);
17272         token->type = CPP_SEMICOLON;
17273         token->keyword = RID_MAX;
17274       }
17275   }
17276
17277   /* If this class is not itself within the scope of another class,
17278      then we need to parse the bodies of all of the queued function
17279      definitions.  Note that the queued functions defined in a class
17280      are not always processed immediately following the
17281      class-specifier for that class.  Consider:
17282
17283        struct A {
17284          struct B { void f() { sizeof (A); } };
17285        };
17286
17287      If `f' were processed before the processing of `A' were
17288      completed, there would be no way to compute the size of `A'.
17289      Note that the nesting we are interested in here is lexical --
17290      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17291      for:
17292
17293        struct A { struct B; };
17294        struct A::B { void f() { } };
17295
17296      there is no need to delay the parsing of `A::B::f'.  */
17297   if (--parser->num_classes_being_defined == 0)
17298     {
17299       tree decl;
17300       tree class_type = NULL_TREE;
17301       tree pushed_scope = NULL_TREE;
17302       unsigned ix;
17303       cp_default_arg_entry *e;
17304       tree save_ccp, save_ccr;
17305
17306       /* In a first pass, parse default arguments to the functions.
17307          Then, in a second pass, parse the bodies of the functions.
17308          This two-phased approach handles cases like:
17309
17310             struct S {
17311               void f() { g(); }
17312               void g(int i = 3);
17313             };
17314
17315          */
17316       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17317                         ix, e)
17318         {
17319           decl = e->decl;
17320           /* If there are default arguments that have not yet been processed,
17321              take care of them now.  */
17322           if (class_type != e->class_type)
17323             {
17324               if (pushed_scope)
17325                 pop_scope (pushed_scope);
17326               class_type = e->class_type;
17327               pushed_scope = push_scope (class_type);
17328             }
17329           /* Make sure that any template parameters are in scope.  */
17330           maybe_begin_member_template_processing (decl);
17331           /* Parse the default argument expressions.  */
17332           cp_parser_late_parsing_default_args (parser, decl);
17333           /* Remove any template parameters from the symbol table.  */
17334           maybe_end_member_template_processing ();
17335         }
17336       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17337       /* Now parse any NSDMIs.  */
17338       save_ccp = current_class_ptr;
17339       save_ccr = current_class_ref;
17340       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
17341         {
17342           if (class_type != DECL_CONTEXT (decl))
17343             {
17344               if (pushed_scope)
17345                 pop_scope (pushed_scope);
17346               class_type = DECL_CONTEXT (decl);
17347               pushed_scope = push_scope (class_type);
17348             }
17349           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
17350           cp_parser_late_parsing_nsdmi (parser, decl);
17351         }
17352       VEC_truncate (tree, unparsed_nsdmis, 0);
17353       current_class_ptr = save_ccp;
17354       current_class_ref = save_ccr;
17355       if (pushed_scope)
17356         pop_scope (pushed_scope);
17357       /* Now parse the body of the functions.  */
17358       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
17359         cp_parser_late_parsing_for_member (parser, decl);
17360       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17361     }
17362
17363   /* Put back any saved access checks.  */
17364   pop_deferring_access_checks ();
17365
17366   /* Restore saved state.  */
17367   parser->in_switch_statement_p = in_switch_statement_p;
17368   parser->in_statement = in_statement;
17369   parser->in_function_body = saved_in_function_body;
17370   parser->num_template_parameter_lists
17371     = saved_num_template_parameter_lists;
17372   parser->in_unbraced_linkage_specification_p
17373     = saved_in_unbraced_linkage_specification_p;
17374
17375   return type;
17376 }
17377
17378 static tree
17379 cp_parser_class_specifier (cp_parser* parser)
17380 {
17381   tree ret;
17382   timevar_push (TV_PARSE_STRUCT);
17383   ret = cp_parser_class_specifier_1 (parser);
17384   timevar_pop (TV_PARSE_STRUCT);
17385   return ret;
17386 }
17387
17388 /* Parse a class-head.
17389
17390    class-head:
17391      class-key identifier [opt] base-clause [opt]
17392      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17393      class-key nested-name-specifier [opt] template-id
17394        base-clause [opt]
17395
17396    class-virt-specifier:
17397      final
17398
17399    GNU Extensions:
17400      class-key attributes identifier [opt] base-clause [opt]
17401      class-key attributes nested-name-specifier identifier base-clause [opt]
17402      class-key attributes nested-name-specifier [opt] template-id
17403        base-clause [opt]
17404
17405    Upon return BASES is initialized to the list of base classes (or
17406    NULL, if there are none) in the same form returned by
17407    cp_parser_base_clause.
17408
17409    Returns the TYPE of the indicated class.  Sets
17410    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17411    involving a nested-name-specifier was used, and FALSE otherwise.
17412
17413    Returns error_mark_node if this is not a class-head.
17414
17415    Returns NULL_TREE if the class-head is syntactically valid, but
17416    semantically invalid in a way that means we should skip the entire
17417    body of the class.  */
17418
17419 static tree
17420 cp_parser_class_head (cp_parser* parser,
17421                       bool* nested_name_specifier_p,
17422                       tree *attributes_p,
17423                       tree *bases)
17424 {
17425   tree nested_name_specifier;
17426   enum tag_types class_key;
17427   tree id = NULL_TREE;
17428   tree type = NULL_TREE;
17429   tree attributes;
17430   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17431   bool template_id_p = false;
17432   bool qualified_p = false;
17433   bool invalid_nested_name_p = false;
17434   bool invalid_explicit_specialization_p = false;
17435   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17436   tree pushed_scope = NULL_TREE;
17437   unsigned num_templates;
17438   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17439   /* Assume no nested-name-specifier will be present.  */
17440   *nested_name_specifier_p = false;
17441   /* Assume no template parameter lists will be used in defining the
17442      type.  */
17443   num_templates = 0;
17444   parser->colon_corrects_to_scope_p = false;
17445
17446   *bases = NULL_TREE;
17447
17448   /* Look for the class-key.  */
17449   class_key = cp_parser_class_key (parser);
17450   if (class_key == none_type)
17451     return error_mark_node;
17452
17453   /* Parse the attributes.  */
17454   attributes = cp_parser_attributes_opt (parser);
17455
17456   /* If the next token is `::', that is invalid -- but sometimes
17457      people do try to write:
17458
17459        struct ::S {};
17460
17461      Handle this gracefully by accepting the extra qualifier, and then
17462      issuing an error about it later if this really is a
17463      class-head.  If it turns out just to be an elaborated type
17464      specifier, remain silent.  */
17465   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17466     qualified_p = true;
17467
17468   push_deferring_access_checks (dk_no_check);
17469
17470   /* Determine the name of the class.  Begin by looking for an
17471      optional nested-name-specifier.  */
17472   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17473   nested_name_specifier
17474     = cp_parser_nested_name_specifier_opt (parser,
17475                                            /*typename_keyword_p=*/false,
17476                                            /*check_dependency_p=*/false,
17477                                            /*type_p=*/false,
17478                                            /*is_declaration=*/false);
17479   /* If there was a nested-name-specifier, then there *must* be an
17480      identifier.  */
17481   if (nested_name_specifier)
17482     {
17483       type_start_token = cp_lexer_peek_token (parser->lexer);
17484       /* Although the grammar says `identifier', it really means
17485          `class-name' or `template-name'.  You are only allowed to
17486          define a class that has already been declared with this
17487          syntax.
17488
17489          The proposed resolution for Core Issue 180 says that wherever
17490          you see `class T::X' you should treat `X' as a type-name.
17491
17492          It is OK to define an inaccessible class; for example:
17493
17494            class A { class B; };
17495            class A::B {};
17496
17497          We do not know if we will see a class-name, or a
17498          template-name.  We look for a class-name first, in case the
17499          class-name is a template-id; if we looked for the
17500          template-name first we would stop after the template-name.  */
17501       cp_parser_parse_tentatively (parser);
17502       type = cp_parser_class_name (parser,
17503                                    /*typename_keyword_p=*/false,
17504                                    /*template_keyword_p=*/false,
17505                                    class_type,
17506                                    /*check_dependency_p=*/false,
17507                                    /*class_head_p=*/true,
17508                                    /*is_declaration=*/false);
17509       /* If that didn't work, ignore the nested-name-specifier.  */
17510       if (!cp_parser_parse_definitely (parser))
17511         {
17512           invalid_nested_name_p = true;
17513           type_start_token = cp_lexer_peek_token (parser->lexer);
17514           id = cp_parser_identifier (parser);
17515           if (id == error_mark_node)
17516             id = NULL_TREE;
17517         }
17518       /* If we could not find a corresponding TYPE, treat this
17519          declaration like an unqualified declaration.  */
17520       if (type == error_mark_node)
17521         nested_name_specifier = NULL_TREE;
17522       /* Otherwise, count the number of templates used in TYPE and its
17523          containing scopes.  */
17524       else
17525         {
17526           tree scope;
17527
17528           for (scope = TREE_TYPE (type);
17529                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17530                scope = (TYPE_P (scope)
17531                         ? TYPE_CONTEXT (scope)
17532                         : DECL_CONTEXT (scope)))
17533             if (TYPE_P (scope)
17534                 && CLASS_TYPE_P (scope)
17535                 && CLASSTYPE_TEMPLATE_INFO (scope)
17536                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17537                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17538               ++num_templates;
17539         }
17540     }
17541   /* Otherwise, the identifier is optional.  */
17542   else
17543     {
17544       /* We don't know whether what comes next is a template-id,
17545          an identifier, or nothing at all.  */
17546       cp_parser_parse_tentatively (parser);
17547       /* Check for a template-id.  */
17548       type_start_token = cp_lexer_peek_token (parser->lexer);
17549       id = cp_parser_template_id (parser,
17550                                   /*template_keyword_p=*/false,
17551                                   /*check_dependency_p=*/true,
17552                                   /*is_declaration=*/true);
17553       /* If that didn't work, it could still be an identifier.  */
17554       if (!cp_parser_parse_definitely (parser))
17555         {
17556           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17557             {
17558               type_start_token = cp_lexer_peek_token (parser->lexer);
17559               id = cp_parser_identifier (parser);
17560             }
17561           else
17562             id = NULL_TREE;
17563         }
17564       else
17565         {
17566           template_id_p = true;
17567           ++num_templates;
17568         }
17569     }
17570
17571   pop_deferring_access_checks ();
17572
17573   if (id)
17574     {
17575       cp_parser_check_for_invalid_template_id (parser, id,
17576                                                type_start_token->location);
17577       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17578     }
17579
17580   /* If it's not a `:' or a `{' then we can't really be looking at a
17581      class-head, since a class-head only appears as part of a
17582      class-specifier.  We have to detect this situation before calling
17583      xref_tag, since that has irreversible side-effects.  */
17584   if (!cp_parser_next_token_starts_class_definition_p (parser))
17585     {
17586       cp_parser_error (parser, "expected %<{%> or %<:%>");
17587       type = error_mark_node;
17588       goto out;
17589     }
17590
17591   /* At this point, we're going ahead with the class-specifier, even
17592      if some other problem occurs.  */
17593   cp_parser_commit_to_tentative_parse (parser);
17594   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17595     {
17596       cp_parser_error (parser,
17597                        "cannot specify %<override%> for a class");
17598       type = error_mark_node;
17599       goto out;
17600     }
17601   /* Issue the error about the overly-qualified name now.  */
17602   if (qualified_p)
17603     {
17604       cp_parser_error (parser,
17605                        "global qualification of class name is invalid");
17606       type = error_mark_node;
17607       goto out;
17608     }
17609   else if (invalid_nested_name_p)
17610     {
17611       cp_parser_error (parser,
17612                        "qualified name does not name a class");
17613       type = error_mark_node;
17614       goto out;
17615     }
17616   else if (nested_name_specifier)
17617     {
17618       tree scope;
17619
17620       /* Reject typedef-names in class heads.  */
17621       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17622         {
17623           error_at (type_start_token->location,
17624                     "invalid class name in declaration of %qD",
17625                     type);
17626           type = NULL_TREE;
17627           goto done;
17628         }
17629
17630       /* Figure out in what scope the declaration is being placed.  */
17631       scope = current_scope ();
17632       /* If that scope does not contain the scope in which the
17633          class was originally declared, the program is invalid.  */
17634       if (scope && !is_ancestor (scope, nested_name_specifier))
17635         {
17636           if (at_namespace_scope_p ())
17637             error_at (type_start_token->location,
17638                       "declaration of %qD in namespace %qD which does not "
17639                       "enclose %qD",
17640                       type, scope, nested_name_specifier);
17641           else
17642             error_at (type_start_token->location,
17643                       "declaration of %qD in %qD which does not enclose %qD",
17644                       type, scope, nested_name_specifier);
17645           type = NULL_TREE;
17646           goto done;
17647         }
17648       /* [dcl.meaning]
17649
17650          A declarator-id shall not be qualified except for the
17651          definition of a ... nested class outside of its class
17652          ... [or] the definition or explicit instantiation of a
17653          class member of a namespace outside of its namespace.  */
17654       if (scope == nested_name_specifier)
17655         {
17656           permerror (nested_name_specifier_token_start->location,
17657                      "extra qualification not allowed");
17658           nested_name_specifier = NULL_TREE;
17659           num_templates = 0;
17660         }
17661     }
17662   /* An explicit-specialization must be preceded by "template <>".  If
17663      it is not, try to recover gracefully.  */
17664   if (at_namespace_scope_p ()
17665       && parser->num_template_parameter_lists == 0
17666       && template_id_p)
17667     {
17668       error_at (type_start_token->location,
17669                 "an explicit specialization must be preceded by %<template <>%>");
17670       invalid_explicit_specialization_p = true;
17671       /* Take the same action that would have been taken by
17672          cp_parser_explicit_specialization.  */
17673       ++parser->num_template_parameter_lists;
17674       begin_specialization ();
17675     }
17676   /* There must be no "return" statements between this point and the
17677      end of this function; set "type "to the correct return value and
17678      use "goto done;" to return.  */
17679   /* Make sure that the right number of template parameters were
17680      present.  */
17681   if (!cp_parser_check_template_parameters (parser, num_templates,
17682                                             type_start_token->location,
17683                                             /*declarator=*/NULL))
17684     {
17685       /* If something went wrong, there is no point in even trying to
17686          process the class-definition.  */
17687       type = NULL_TREE;
17688       goto done;
17689     }
17690
17691   /* Look up the type.  */
17692   if (template_id_p)
17693     {
17694       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17695           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17696               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17697         {
17698           error_at (type_start_token->location,
17699                     "function template %qD redeclared as a class template", id);
17700           type = error_mark_node;
17701         }
17702       else
17703         {
17704           type = TREE_TYPE (id);
17705           type = maybe_process_partial_specialization (type);
17706         }
17707       if (nested_name_specifier)
17708         pushed_scope = push_scope (nested_name_specifier);
17709     }
17710   else if (nested_name_specifier)
17711     {
17712       tree class_type;
17713
17714       /* Given:
17715
17716             template <typename T> struct S { struct T };
17717             template <typename T> struct S<T>::T { };
17718
17719          we will get a TYPENAME_TYPE when processing the definition of
17720          `S::T'.  We need to resolve it to the actual type before we
17721          try to define it.  */
17722       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17723         {
17724           class_type = resolve_typename_type (TREE_TYPE (type),
17725                                               /*only_current_p=*/false);
17726           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17727             type = TYPE_NAME (class_type);
17728           else
17729             {
17730               cp_parser_error (parser, "could not resolve typename type");
17731               type = error_mark_node;
17732             }
17733         }
17734
17735       if (maybe_process_partial_specialization (TREE_TYPE (type))
17736           == error_mark_node)
17737         {
17738           type = NULL_TREE;
17739           goto done;
17740         }
17741
17742       class_type = current_class_type;
17743       /* Enter the scope indicated by the nested-name-specifier.  */
17744       pushed_scope = push_scope (nested_name_specifier);
17745       /* Get the canonical version of this type.  */
17746       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17747       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17748           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17749         {
17750           type = push_template_decl (type);
17751           if (type == error_mark_node)
17752             {
17753               type = NULL_TREE;
17754               goto done;
17755             }
17756         }
17757
17758       type = TREE_TYPE (type);
17759       *nested_name_specifier_p = true;
17760     }
17761   else      /* The name is not a nested name.  */
17762     {
17763       /* If the class was unnamed, create a dummy name.  */
17764       if (!id)
17765         id = make_anon_name ();
17766       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17767                        parser->num_template_parameter_lists);
17768     }
17769
17770   /* Indicate whether this class was declared as a `class' or as a
17771      `struct'.  */
17772   if (TREE_CODE (type) == RECORD_TYPE)
17773     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17774   cp_parser_check_class_key (class_key, type);
17775
17776   /* If this type was already complete, and we see another definition,
17777      that's an error.  */
17778   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17779     {
17780       error_at (type_start_token->location, "redefinition of %q#T",
17781                 type);
17782       error_at (type_start_token->location, "previous definition of %q+#T",
17783                 type);
17784       type = NULL_TREE;
17785       goto done;
17786     }
17787   else if (type == error_mark_node)
17788     type = NULL_TREE;
17789
17790   /* We will have entered the scope containing the class; the names of
17791      base classes should be looked up in that context.  For example:
17792
17793        struct A { struct B {}; struct C; };
17794        struct A::C : B {};
17795
17796      is valid.  */
17797
17798   /* Get the list of base-classes, if there is one.  */
17799   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17800     *bases = cp_parser_base_clause (parser);
17801
17802  done:
17803   /* Leave the scope given by the nested-name-specifier.  We will
17804      enter the class scope itself while processing the members.  */
17805   if (pushed_scope)
17806     pop_scope (pushed_scope);
17807
17808   if (invalid_explicit_specialization_p)
17809     {
17810       end_specialization ();
17811       --parser->num_template_parameter_lists;
17812     }
17813
17814   if (type)
17815     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17816   *attributes_p = attributes;
17817   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17818     CLASSTYPE_FINAL (type) = 1;
17819  out:
17820   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17821   return type;
17822 }
17823
17824 /* Parse a class-key.
17825
17826    class-key:
17827      class
17828      struct
17829      union
17830
17831    Returns the kind of class-key specified, or none_type to indicate
17832    error.  */
17833
17834 static enum tag_types
17835 cp_parser_class_key (cp_parser* parser)
17836 {
17837   cp_token *token;
17838   enum tag_types tag_type;
17839
17840   /* Look for the class-key.  */
17841   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17842   if (!token)
17843     return none_type;
17844
17845   /* Check to see if the TOKEN is a class-key.  */
17846   tag_type = cp_parser_token_is_class_key (token);
17847   if (!tag_type)
17848     cp_parser_error (parser, "expected class-key");
17849   return tag_type;
17850 }
17851
17852 /* Parse an (optional) member-specification.
17853
17854    member-specification:
17855      member-declaration member-specification [opt]
17856      access-specifier : member-specification [opt]  */
17857
17858 static void
17859 cp_parser_member_specification_opt (cp_parser* parser)
17860 {
17861   while (true)
17862     {
17863       cp_token *token;
17864       enum rid keyword;
17865
17866       /* Peek at the next token.  */
17867       token = cp_lexer_peek_token (parser->lexer);
17868       /* If it's a `}', or EOF then we've seen all the members.  */
17869       if (token->type == CPP_CLOSE_BRACE
17870           || token->type == CPP_EOF
17871           || token->type == CPP_PRAGMA_EOL)
17872         break;
17873
17874       /* See if this token is a keyword.  */
17875       keyword = token->keyword;
17876       switch (keyword)
17877         {
17878         case RID_PUBLIC:
17879         case RID_PROTECTED:
17880         case RID_PRIVATE:
17881           /* Consume the access-specifier.  */
17882           cp_lexer_consume_token (parser->lexer);
17883           /* Remember which access-specifier is active.  */
17884           current_access_specifier = token->u.value;
17885           /* Look for the `:'.  */
17886           cp_parser_require (parser, CPP_COLON, RT_COLON);
17887           break;
17888
17889         default:
17890           /* Accept #pragmas at class scope.  */
17891           if (token->type == CPP_PRAGMA)
17892             {
17893               cp_parser_pragma (parser, pragma_external);
17894               break;
17895             }
17896
17897           /* Otherwise, the next construction must be a
17898              member-declaration.  */
17899           cp_parser_member_declaration (parser);
17900         }
17901     }
17902 }
17903
17904 /* Parse a member-declaration.
17905
17906    member-declaration:
17907      decl-specifier-seq [opt] member-declarator-list [opt] ;
17908      function-definition ; [opt]
17909      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17910      using-declaration
17911      template-declaration
17912
17913    member-declarator-list:
17914      member-declarator
17915      member-declarator-list , member-declarator
17916
17917    member-declarator:
17918      declarator pure-specifier [opt]
17919      declarator constant-initializer [opt]
17920      identifier [opt] : constant-expression
17921
17922    GNU Extensions:
17923
17924    member-declaration:
17925      __extension__ member-declaration
17926
17927    member-declarator:
17928      declarator attributes [opt] pure-specifier [opt]
17929      declarator attributes [opt] constant-initializer [opt]
17930      identifier [opt] attributes [opt] : constant-expression  
17931
17932    C++0x Extensions:
17933
17934    member-declaration:
17935      static_assert-declaration  */
17936
17937 static void
17938 cp_parser_member_declaration (cp_parser* parser)
17939 {
17940   cp_decl_specifier_seq decl_specifiers;
17941   tree prefix_attributes;
17942   tree decl;
17943   int declares_class_or_enum;
17944   bool friend_p;
17945   cp_token *token = NULL;
17946   cp_token *decl_spec_token_start = NULL;
17947   cp_token *initializer_token_start = NULL;
17948   int saved_pedantic;
17949   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17950
17951   /* Check for the `__extension__' keyword.  */
17952   if (cp_parser_extension_opt (parser, &saved_pedantic))
17953     {
17954       /* Recurse.  */
17955       cp_parser_member_declaration (parser);
17956       /* Restore the old value of the PEDANTIC flag.  */
17957       pedantic = saved_pedantic;
17958
17959       return;
17960     }
17961
17962   /* Check for a template-declaration.  */
17963   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17964     {
17965       /* An explicit specialization here is an error condition, and we
17966          expect the specialization handler to detect and report this.  */
17967       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17968           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17969         cp_parser_explicit_specialization (parser);
17970       else
17971         cp_parser_template_declaration (parser, /*member_p=*/true);
17972
17973       return;
17974     }
17975
17976   /* Check for a using-declaration.  */
17977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17978     {
17979       /* Parse the using-declaration.  */
17980       cp_parser_using_declaration (parser,
17981                                    /*access_declaration_p=*/false);
17982       return;
17983     }
17984
17985   /* Check for @defs.  */
17986   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17987     {
17988       tree ivar, member;
17989       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17990       ivar = ivar_chains;
17991       while (ivar)
17992         {
17993           member = ivar;
17994           ivar = TREE_CHAIN (member);
17995           TREE_CHAIN (member) = NULL_TREE;
17996           finish_member_declaration (member);
17997         }
17998       return;
17999     }
18000
18001   /* If the next token is `static_assert' we have a static assertion.  */
18002   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18003     {
18004       cp_parser_static_assert (parser, /*member_p=*/true);
18005       return;
18006     }
18007
18008   parser->colon_corrects_to_scope_p = false;
18009
18010   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18011     goto out;
18012
18013   /* Parse the decl-specifier-seq.  */
18014   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18015   cp_parser_decl_specifier_seq (parser,
18016                                 CP_PARSER_FLAGS_OPTIONAL,
18017                                 &decl_specifiers,
18018                                 &declares_class_or_enum);
18019   prefix_attributes = decl_specifiers.attributes;
18020   decl_specifiers.attributes = NULL_TREE;
18021   /* Check for an invalid type-name.  */
18022   if (!decl_specifiers.any_type_specifiers_p
18023       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18024     goto out;
18025   /* If there is no declarator, then the decl-specifier-seq should
18026      specify a type.  */
18027   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18028     {
18029       /* If there was no decl-specifier-seq, and the next token is a
18030          `;', then we have something like:
18031
18032            struct S { ; };
18033
18034          [class.mem]
18035
18036          Each member-declaration shall declare at least one member
18037          name of the class.  */
18038       if (!decl_specifiers.any_specifiers_p)
18039         {
18040           cp_token *token = cp_lexer_peek_token (parser->lexer);
18041           if (!in_system_header_at (token->location))
18042             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18043         }
18044       else
18045         {
18046           tree type;
18047
18048           /* See if this declaration is a friend.  */
18049           friend_p = cp_parser_friend_p (&decl_specifiers);
18050           /* If there were decl-specifiers, check to see if there was
18051              a class-declaration.  */
18052           type = check_tag_decl (&decl_specifiers);
18053           /* Nested classes have already been added to the class, but
18054              a `friend' needs to be explicitly registered.  */
18055           if (friend_p)
18056             {
18057               /* If the `friend' keyword was present, the friend must
18058                  be introduced with a class-key.  */
18059                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18060                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18061                           "in C++03 a class-key must be used "
18062                           "when declaring a friend");
18063                /* In this case:
18064
18065                     template <typename T> struct A {
18066                       friend struct A<T>::B;
18067                     };
18068
18069                   A<T>::B will be represented by a TYPENAME_TYPE, and
18070                   therefore not recognized by check_tag_decl.  */
18071                if (!type)
18072                  {
18073                    type = decl_specifiers.type;
18074                    if (type && TREE_CODE (type) == TYPE_DECL)
18075                      type = TREE_TYPE (type);
18076                  }
18077                if (!type || !TYPE_P (type))
18078                  error_at (decl_spec_token_start->location,
18079                            "friend declaration does not name a class or "
18080                            "function");
18081                else
18082                  make_friend_class (current_class_type, type,
18083                                     /*complain=*/true);
18084             }
18085           /* If there is no TYPE, an error message will already have
18086              been issued.  */
18087           else if (!type || type == error_mark_node)
18088             ;
18089           /* An anonymous aggregate has to be handled specially; such
18090              a declaration really declares a data member (with a
18091              particular type), as opposed to a nested class.  */
18092           else if (ANON_AGGR_TYPE_P (type))
18093             {
18094               /* Remove constructors and such from TYPE, now that we
18095                  know it is an anonymous aggregate.  */
18096               fixup_anonymous_aggr (type);
18097               /* And make the corresponding data member.  */
18098               decl = build_decl (decl_spec_token_start->location,
18099                                  FIELD_DECL, NULL_TREE, type);
18100               /* Add it to the class.  */
18101               finish_member_declaration (decl);
18102             }
18103           else
18104             cp_parser_check_access_in_redeclaration
18105                                               (TYPE_NAME (type),
18106                                                decl_spec_token_start->location);
18107         }
18108     }
18109   else
18110     {
18111       bool assume_semicolon = false;
18112
18113       /* See if these declarations will be friends.  */
18114       friend_p = cp_parser_friend_p (&decl_specifiers);
18115
18116       /* Keep going until we hit the `;' at the end of the
18117          declaration.  */
18118       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18119         {
18120           tree attributes = NULL_TREE;
18121           tree first_attribute;
18122
18123           /* Peek at the next token.  */
18124           token = cp_lexer_peek_token (parser->lexer);
18125
18126           /* Check for a bitfield declaration.  */
18127           if (token->type == CPP_COLON
18128               || (token->type == CPP_NAME
18129                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18130                   == CPP_COLON))
18131             {
18132               tree identifier;
18133               tree width;
18134
18135               /* Get the name of the bitfield.  Note that we cannot just
18136                  check TOKEN here because it may have been invalidated by
18137                  the call to cp_lexer_peek_nth_token above.  */
18138               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18139                 identifier = cp_parser_identifier (parser);
18140               else
18141                 identifier = NULL_TREE;
18142
18143               /* Consume the `:' token.  */
18144               cp_lexer_consume_token (parser->lexer);
18145               /* Get the width of the bitfield.  */
18146               width
18147                 = cp_parser_constant_expression (parser,
18148                                                  /*allow_non_constant=*/false,
18149                                                  NULL);
18150
18151               /* Look for attributes that apply to the bitfield.  */
18152               attributes = cp_parser_attributes_opt (parser);
18153               /* Remember which attributes are prefix attributes and
18154                  which are not.  */
18155               first_attribute = attributes;
18156               /* Combine the attributes.  */
18157               attributes = chainon (prefix_attributes, attributes);
18158
18159               /* Create the bitfield declaration.  */
18160               decl = grokbitfield (identifier
18161                                    ? make_id_declarator (NULL_TREE,
18162                                                          identifier,
18163                                                          sfk_none)
18164                                    : NULL,
18165                                    &decl_specifiers,
18166                                    width,
18167                                    attributes);
18168             }
18169           else
18170             {
18171               cp_declarator *declarator;
18172               tree initializer;
18173               tree asm_specification;
18174               int ctor_dtor_or_conv_p;
18175
18176               /* Parse the declarator.  */
18177               declarator
18178                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18179                                         &ctor_dtor_or_conv_p,
18180                                         /*parenthesized_p=*/NULL,
18181                                         /*member_p=*/true);
18182
18183               /* If something went wrong parsing the declarator, make sure
18184                  that we at least consume some tokens.  */
18185               if (declarator == cp_error_declarator)
18186                 {
18187                   /* Skip to the end of the statement.  */
18188                   cp_parser_skip_to_end_of_statement (parser);
18189                   /* If the next token is not a semicolon, that is
18190                      probably because we just skipped over the body of
18191                      a function.  So, we consume a semicolon if
18192                      present, but do not issue an error message if it
18193                      is not present.  */
18194                   if (cp_lexer_next_token_is (parser->lexer,
18195                                               CPP_SEMICOLON))
18196                     cp_lexer_consume_token (parser->lexer);
18197                   goto out;
18198                 }
18199
18200               if (declares_class_or_enum & 2)
18201                 cp_parser_check_for_definition_in_return_type
18202                                             (declarator, decl_specifiers.type,
18203                                              decl_specifiers.type_location);
18204
18205               /* Look for an asm-specification.  */
18206               asm_specification = cp_parser_asm_specification_opt (parser);
18207               /* Look for attributes that apply to the declaration.  */
18208               attributes = cp_parser_attributes_opt (parser);
18209               /* Remember which attributes are prefix attributes and
18210                  which are not.  */
18211               first_attribute = attributes;
18212               /* Combine the attributes.  */
18213               attributes = chainon (prefix_attributes, attributes);
18214
18215               /* If it's an `=', then we have a constant-initializer or a
18216                  pure-specifier.  It is not correct to parse the
18217                  initializer before registering the member declaration
18218                  since the member declaration should be in scope while
18219                  its initializer is processed.  However, the rest of the
18220                  front end does not yet provide an interface that allows
18221                  us to handle this correctly.  */
18222               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18223                 {
18224                   /* In [class.mem]:
18225
18226                      A pure-specifier shall be used only in the declaration of
18227                      a virtual function.
18228
18229                      A member-declarator can contain a constant-initializer
18230                      only if it declares a static member of integral or
18231                      enumeration type.
18232
18233                      Therefore, if the DECLARATOR is for a function, we look
18234                      for a pure-specifier; otherwise, we look for a
18235                      constant-initializer.  When we call `grokfield', it will
18236                      perform more stringent semantics checks.  */
18237                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18238                   if (function_declarator_p (declarator)
18239                       || (decl_specifiers.type
18240                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18241                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18242                               == FUNCTION_TYPE)))
18243                     initializer = cp_parser_pure_specifier (parser);
18244                   else if (decl_specifiers.storage_class != sc_static)
18245                     initializer = cp_parser_save_nsdmi (parser);
18246                   else if (cxx_dialect >= cxx0x)
18247                     {
18248                       bool nonconst;
18249                       /* Don't require a constant rvalue in C++11, since we
18250                          might want a reference constant.  We'll enforce
18251                          constancy later.  */
18252                       cp_lexer_consume_token (parser->lexer);
18253                       /* Parse the initializer.  */
18254                       initializer = cp_parser_initializer_clause (parser,
18255                                                                   &nonconst);
18256                     }
18257                   else
18258                     /* Parse the initializer.  */
18259                     initializer = cp_parser_constant_initializer (parser);
18260                 }
18261               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18262                        && !function_declarator_p (declarator))
18263                 {
18264                   bool x;
18265                   if (decl_specifiers.storage_class != sc_static)
18266                     initializer = cp_parser_save_nsdmi (parser);
18267                   else
18268                     initializer = cp_parser_initializer (parser, &x, &x);
18269                 }
18270               /* Otherwise, there is no initializer.  */
18271               else
18272                 initializer = NULL_TREE;
18273
18274               /* See if we are probably looking at a function
18275                  definition.  We are certainly not looking at a
18276                  member-declarator.  Calling `grokfield' has
18277                  side-effects, so we must not do it unless we are sure
18278                  that we are looking at a member-declarator.  */
18279               if (cp_parser_token_starts_function_definition_p
18280                   (cp_lexer_peek_token (parser->lexer)))
18281                 {
18282                   /* The grammar does not allow a pure-specifier to be
18283                      used when a member function is defined.  (It is
18284                      possible that this fact is an oversight in the
18285                      standard, since a pure function may be defined
18286                      outside of the class-specifier.  */
18287                   if (initializer)
18288                     error_at (initializer_token_start->location,
18289                               "pure-specifier on function-definition");
18290                   decl = cp_parser_save_member_function_body (parser,
18291                                                               &decl_specifiers,
18292                                                               declarator,
18293                                                               attributes);
18294                   /* If the member was not a friend, declare it here.  */
18295                   if (!friend_p)
18296                     finish_member_declaration (decl);
18297                   /* Peek at the next token.  */
18298                   token = cp_lexer_peek_token (parser->lexer);
18299                   /* If the next token is a semicolon, consume it.  */
18300                   if (token->type == CPP_SEMICOLON)
18301                     cp_lexer_consume_token (parser->lexer);
18302                   goto out;
18303                 }
18304               else
18305                 if (declarator->kind == cdk_function)
18306                   declarator->id_loc = token->location;
18307                 /* Create the declaration.  */
18308                 decl = grokfield (declarator, &decl_specifiers,
18309                                   initializer, /*init_const_expr_p=*/true,
18310                                   asm_specification,
18311                                   attributes);
18312             }
18313
18314           /* Reset PREFIX_ATTRIBUTES.  */
18315           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18316             attributes = TREE_CHAIN (attributes);
18317           if (attributes)
18318             TREE_CHAIN (attributes) = NULL_TREE;
18319
18320           /* If there is any qualification still in effect, clear it
18321              now; we will be starting fresh with the next declarator.  */
18322           parser->scope = NULL_TREE;
18323           parser->qualifying_scope = NULL_TREE;
18324           parser->object_scope = NULL_TREE;
18325           /* If it's a `,', then there are more declarators.  */
18326           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18327             cp_lexer_consume_token (parser->lexer);
18328           /* If the next token isn't a `;', then we have a parse error.  */
18329           else if (cp_lexer_next_token_is_not (parser->lexer,
18330                                                CPP_SEMICOLON))
18331             {
18332               /* The next token might be a ways away from where the
18333                  actual semicolon is missing.  Find the previous token
18334                  and use that for our error position.  */
18335               cp_token *token = cp_lexer_previous_token (parser->lexer);
18336               error_at (token->location,
18337                         "expected %<;%> at end of member declaration");
18338
18339               /* Assume that the user meant to provide a semicolon.  If
18340                  we were to cp_parser_skip_to_end_of_statement, we might
18341                  skip to a semicolon inside a member function definition
18342                  and issue nonsensical error messages.  */
18343               assume_semicolon = true;
18344             }
18345
18346           if (decl)
18347             {
18348               /* Add DECL to the list of members.  */
18349               if (!friend_p)
18350                 finish_member_declaration (decl);
18351
18352               if (TREE_CODE (decl) == FUNCTION_DECL)
18353                 cp_parser_save_default_args (parser, decl);
18354               else if (TREE_CODE (decl) == FIELD_DECL
18355                        && !DECL_C_BIT_FIELD (decl)
18356                        && DECL_INITIAL (decl))
18357                 /* Add DECL to the queue of NSDMI to be parsed later.  */
18358                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
18359             }
18360
18361           if (assume_semicolon)
18362             goto out;
18363         }
18364     }
18365
18366   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18367  out:
18368   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18369 }
18370
18371 /* Parse a pure-specifier.
18372
18373    pure-specifier:
18374      = 0
18375
18376    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18377    Otherwise, ERROR_MARK_NODE is returned.  */
18378
18379 static tree
18380 cp_parser_pure_specifier (cp_parser* parser)
18381 {
18382   cp_token *token;
18383
18384   /* Look for the `=' token.  */
18385   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18386     return error_mark_node;
18387   /* Look for the `0' token.  */
18388   token = cp_lexer_peek_token (parser->lexer);
18389
18390   if (token->type == CPP_EOF
18391       || token->type == CPP_PRAGMA_EOL)
18392     return error_mark_node;
18393
18394   cp_lexer_consume_token (parser->lexer);
18395
18396   /* Accept = default or = delete in c++0x mode.  */
18397   if (token->keyword == RID_DEFAULT
18398       || token->keyword == RID_DELETE)
18399     {
18400       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18401       return token->u.value;
18402     }
18403
18404   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18405   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18406     {
18407       cp_parser_error (parser,
18408                        "invalid pure specifier (only %<= 0%> is allowed)");
18409       cp_parser_skip_to_end_of_statement (parser);
18410       return error_mark_node;
18411     }
18412   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18413     {
18414       error_at (token->location, "templates may not be %<virtual%>");
18415       return error_mark_node;
18416     }
18417
18418   return integer_zero_node;
18419 }
18420
18421 /* Parse a constant-initializer.
18422
18423    constant-initializer:
18424      = constant-expression
18425
18426    Returns a representation of the constant-expression.  */
18427
18428 static tree
18429 cp_parser_constant_initializer (cp_parser* parser)
18430 {
18431   /* Look for the `=' token.  */
18432   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18433     return error_mark_node;
18434
18435   /* It is invalid to write:
18436
18437        struct S { static const int i = { 7 }; };
18438
18439      */
18440   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18441     {
18442       cp_parser_error (parser,
18443                        "a brace-enclosed initializer is not allowed here");
18444       /* Consume the opening brace.  */
18445       cp_lexer_consume_token (parser->lexer);
18446       /* Skip the initializer.  */
18447       cp_parser_skip_to_closing_brace (parser);
18448       /* Look for the trailing `}'.  */
18449       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18450
18451       return error_mark_node;
18452     }
18453
18454   return cp_parser_constant_expression (parser,
18455                                         /*allow_non_constant=*/false,
18456                                         NULL);
18457 }
18458
18459 /* Derived classes [gram.class.derived] */
18460
18461 /* Parse a base-clause.
18462
18463    base-clause:
18464      : base-specifier-list
18465
18466    base-specifier-list:
18467      base-specifier ... [opt]
18468      base-specifier-list , base-specifier ... [opt]
18469
18470    Returns a TREE_LIST representing the base-classes, in the order in
18471    which they were declared.  The representation of each node is as
18472    described by cp_parser_base_specifier.
18473
18474    In the case that no bases are specified, this function will return
18475    NULL_TREE, not ERROR_MARK_NODE.  */
18476
18477 static tree
18478 cp_parser_base_clause (cp_parser* parser)
18479 {
18480   tree bases = NULL_TREE;
18481
18482   /* Look for the `:' that begins the list.  */
18483   cp_parser_require (parser, CPP_COLON, RT_COLON);
18484
18485   /* Scan the base-specifier-list.  */
18486   while (true)
18487     {
18488       cp_token *token;
18489       tree base;
18490       bool pack_expansion_p = false;
18491
18492       /* Look for the base-specifier.  */
18493       base = cp_parser_base_specifier (parser);
18494       /* Look for the (optional) ellipsis. */
18495       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18496         {
18497           /* Consume the `...'. */
18498           cp_lexer_consume_token (parser->lexer);
18499
18500           pack_expansion_p = true;
18501         }
18502
18503       /* Add BASE to the front of the list.  */
18504       if (base && base != error_mark_node)
18505         {
18506           if (pack_expansion_p)
18507             /* Make this a pack expansion type. */
18508             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18509
18510           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18511             {
18512               TREE_CHAIN (base) = bases;
18513               bases = base;
18514             }
18515         }
18516       /* Peek at the next token.  */
18517       token = cp_lexer_peek_token (parser->lexer);
18518       /* If it's not a comma, then the list is complete.  */
18519       if (token->type != CPP_COMMA)
18520         break;
18521       /* Consume the `,'.  */
18522       cp_lexer_consume_token (parser->lexer);
18523     }
18524
18525   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18526      base class had a qualified name.  However, the next name that
18527      appears is certainly not qualified.  */
18528   parser->scope = NULL_TREE;
18529   parser->qualifying_scope = NULL_TREE;
18530   parser->object_scope = NULL_TREE;
18531
18532   return nreverse (bases);
18533 }
18534
18535 /* Parse a base-specifier.
18536
18537    base-specifier:
18538      :: [opt] nested-name-specifier [opt] class-name
18539      virtual access-specifier [opt] :: [opt] nested-name-specifier
18540        [opt] class-name
18541      access-specifier virtual [opt] :: [opt] nested-name-specifier
18542        [opt] class-name
18543
18544    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18545    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18546    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18547    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18548
18549 static tree
18550 cp_parser_base_specifier (cp_parser* parser)
18551 {
18552   cp_token *token;
18553   bool done = false;
18554   bool virtual_p = false;
18555   bool duplicate_virtual_error_issued_p = false;
18556   bool duplicate_access_error_issued_p = false;
18557   bool class_scope_p, template_p;
18558   tree access = access_default_node;
18559   tree type;
18560
18561   /* Process the optional `virtual' and `access-specifier'.  */
18562   while (!done)
18563     {
18564       /* Peek at the next token.  */
18565       token = cp_lexer_peek_token (parser->lexer);
18566       /* Process `virtual'.  */
18567       switch (token->keyword)
18568         {
18569         case RID_VIRTUAL:
18570           /* If `virtual' appears more than once, issue an error.  */
18571           if (virtual_p && !duplicate_virtual_error_issued_p)
18572             {
18573               cp_parser_error (parser,
18574                                "%<virtual%> specified more than once in base-specified");
18575               duplicate_virtual_error_issued_p = true;
18576             }
18577
18578           virtual_p = true;
18579
18580           /* Consume the `virtual' token.  */
18581           cp_lexer_consume_token (parser->lexer);
18582
18583           break;
18584
18585         case RID_PUBLIC:
18586         case RID_PROTECTED:
18587         case RID_PRIVATE:
18588           /* If more than one access specifier appears, issue an
18589              error.  */
18590           if (access != access_default_node
18591               && !duplicate_access_error_issued_p)
18592             {
18593               cp_parser_error (parser,
18594                                "more than one access specifier in base-specified");
18595               duplicate_access_error_issued_p = true;
18596             }
18597
18598           access = ridpointers[(int) token->keyword];
18599
18600           /* Consume the access-specifier.  */
18601           cp_lexer_consume_token (parser->lexer);
18602
18603           break;
18604
18605         default:
18606           done = true;
18607           break;
18608         }
18609     }
18610   /* It is not uncommon to see programs mechanically, erroneously, use
18611      the 'typename' keyword to denote (dependent) qualified types
18612      as base classes.  */
18613   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18614     {
18615       token = cp_lexer_peek_token (parser->lexer);
18616       if (!processing_template_decl)
18617         error_at (token->location,
18618                   "keyword %<typename%> not allowed outside of templates");
18619       else
18620         error_at (token->location,
18621                   "keyword %<typename%> not allowed in this context "
18622                   "(the base class is implicitly a type)");
18623       cp_lexer_consume_token (parser->lexer);
18624     }
18625
18626   /* Look for the optional `::' operator.  */
18627   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18628   /* Look for the nested-name-specifier.  The simplest way to
18629      implement:
18630
18631        [temp.res]
18632
18633        The keyword `typename' is not permitted in a base-specifier or
18634        mem-initializer; in these contexts a qualified name that
18635        depends on a template-parameter is implicitly assumed to be a
18636        type name.
18637
18638      is to pretend that we have seen the `typename' keyword at this
18639      point.  */
18640   cp_parser_nested_name_specifier_opt (parser,
18641                                        /*typename_keyword_p=*/true,
18642                                        /*check_dependency_p=*/true,
18643                                        typename_type,
18644                                        /*is_declaration=*/true);
18645   /* If the base class is given by a qualified name, assume that names
18646      we see are type names or templates, as appropriate.  */
18647   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18648   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18649
18650   if (!parser->scope
18651       && cp_lexer_next_token_is_decltype (parser->lexer))
18652     /* DR 950 allows decltype as a base-specifier.  */
18653     type = cp_parser_decltype (parser);
18654   else
18655     {
18656       /* Otherwise, look for the class-name.  */
18657       type = cp_parser_class_name (parser,
18658                                    class_scope_p,
18659                                    template_p,
18660                                    typename_type,
18661                                    /*check_dependency_p=*/true,
18662                                    /*class_head_p=*/false,
18663                                    /*is_declaration=*/true);
18664       type = TREE_TYPE (type);
18665     }
18666
18667   if (type == error_mark_node)
18668     return error_mark_node;
18669
18670   return finish_base_specifier (type, access, virtual_p);
18671 }
18672
18673 /* Exception handling [gram.exception] */
18674
18675 /* Parse an (optional) exception-specification.
18676
18677    exception-specification:
18678      throw ( type-id-list [opt] )
18679
18680    Returns a TREE_LIST representing the exception-specification.  The
18681    TREE_VALUE of each node is a type.  */
18682
18683 static tree
18684 cp_parser_exception_specification_opt (cp_parser* parser)
18685 {
18686   cp_token *token;
18687   tree type_id_list;
18688   const char *saved_message;
18689
18690   /* Peek at the next token.  */
18691   token = cp_lexer_peek_token (parser->lexer);
18692
18693   /* Is it a noexcept-specification?  */
18694   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18695     {
18696       tree expr;
18697       cp_lexer_consume_token (parser->lexer);
18698
18699       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18700         {
18701           cp_lexer_consume_token (parser->lexer);
18702
18703           /* Types may not be defined in an exception-specification.  */
18704           saved_message = parser->type_definition_forbidden_message;
18705           parser->type_definition_forbidden_message
18706             = G_("types may not be defined in an exception-specification");
18707
18708           expr = cp_parser_constant_expression (parser, false, NULL);
18709
18710           /* Restore the saved message.  */
18711           parser->type_definition_forbidden_message = saved_message;
18712
18713           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18714         }
18715       else
18716         expr = boolean_true_node;
18717
18718       return build_noexcept_spec (expr, tf_warning_or_error);
18719     }
18720
18721   /* If it's not `throw', then there's no exception-specification.  */
18722   if (!cp_parser_is_keyword (token, RID_THROW))
18723     return NULL_TREE;
18724
18725 #if 0
18726   /* Enable this once a lot of code has transitioned to noexcept?  */
18727   if (cxx_dialect == cxx0x && !in_system_header)
18728     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18729              "deprecated in C++0x; use %<noexcept%> instead");
18730 #endif
18731
18732   /* Consume the `throw'.  */
18733   cp_lexer_consume_token (parser->lexer);
18734
18735   /* Look for the `('.  */
18736   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18737
18738   /* Peek at the next token.  */
18739   token = cp_lexer_peek_token (parser->lexer);
18740   /* If it's not a `)', then there is a type-id-list.  */
18741   if (token->type != CPP_CLOSE_PAREN)
18742     {
18743       /* Types may not be defined in an exception-specification.  */
18744       saved_message = parser->type_definition_forbidden_message;
18745       parser->type_definition_forbidden_message
18746         = G_("types may not be defined in an exception-specification");
18747       /* Parse the type-id-list.  */
18748       type_id_list = cp_parser_type_id_list (parser);
18749       /* Restore the saved message.  */
18750       parser->type_definition_forbidden_message = saved_message;
18751     }
18752   else
18753     type_id_list = empty_except_spec;
18754
18755   /* Look for the `)'.  */
18756   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18757
18758   return type_id_list;
18759 }
18760
18761 /* Parse an (optional) type-id-list.
18762
18763    type-id-list:
18764      type-id ... [opt]
18765      type-id-list , type-id ... [opt]
18766
18767    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18768    in the order that the types were presented.  */
18769
18770 static tree
18771 cp_parser_type_id_list (cp_parser* parser)
18772 {
18773   tree types = NULL_TREE;
18774
18775   while (true)
18776     {
18777       cp_token *token;
18778       tree type;
18779
18780       /* Get the next type-id.  */
18781       type = cp_parser_type_id (parser);
18782       /* Parse the optional ellipsis. */
18783       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18784         {
18785           /* Consume the `...'. */
18786           cp_lexer_consume_token (parser->lexer);
18787
18788           /* Turn the type into a pack expansion expression. */
18789           type = make_pack_expansion (type);
18790         }
18791       /* Add it to the list.  */
18792       types = add_exception_specifier (types, type, /*complain=*/1);
18793       /* Peek at the next token.  */
18794       token = cp_lexer_peek_token (parser->lexer);
18795       /* If it is not a `,', we are done.  */
18796       if (token->type != CPP_COMMA)
18797         break;
18798       /* Consume the `,'.  */
18799       cp_lexer_consume_token (parser->lexer);
18800     }
18801
18802   return nreverse (types);
18803 }
18804
18805 /* Parse a try-block.
18806
18807    try-block:
18808      try compound-statement handler-seq  */
18809
18810 static tree
18811 cp_parser_try_block (cp_parser* parser)
18812 {
18813   tree try_block;
18814
18815   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18816   try_block = begin_try_block ();
18817   cp_parser_compound_statement (parser, NULL, true, false);
18818   finish_try_block (try_block);
18819   cp_parser_handler_seq (parser);
18820   finish_handler_sequence (try_block);
18821
18822   return try_block;
18823 }
18824
18825 /* Parse a function-try-block.
18826
18827    function-try-block:
18828      try ctor-initializer [opt] function-body handler-seq  */
18829
18830 static bool
18831 cp_parser_function_try_block (cp_parser* parser)
18832 {
18833   tree compound_stmt;
18834   tree try_block;
18835   bool ctor_initializer_p;
18836
18837   /* Look for the `try' keyword.  */
18838   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18839     return false;
18840   /* Let the rest of the front end know where we are.  */
18841   try_block = begin_function_try_block (&compound_stmt);
18842   /* Parse the function-body.  */
18843   ctor_initializer_p
18844     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18845   /* We're done with the `try' part.  */
18846   finish_function_try_block (try_block);
18847   /* Parse the handlers.  */
18848   cp_parser_handler_seq (parser);
18849   /* We're done with the handlers.  */
18850   finish_function_handler_sequence (try_block, compound_stmt);
18851
18852   return ctor_initializer_p;
18853 }
18854
18855 /* Parse a handler-seq.
18856
18857    handler-seq:
18858      handler handler-seq [opt]  */
18859
18860 static void
18861 cp_parser_handler_seq (cp_parser* parser)
18862 {
18863   while (true)
18864     {
18865       cp_token *token;
18866
18867       /* Parse the handler.  */
18868       cp_parser_handler (parser);
18869       /* Peek at the next token.  */
18870       token = cp_lexer_peek_token (parser->lexer);
18871       /* If it's not `catch' then there are no more handlers.  */
18872       if (!cp_parser_is_keyword (token, RID_CATCH))
18873         break;
18874     }
18875 }
18876
18877 /* Parse a handler.
18878
18879    handler:
18880      catch ( exception-declaration ) compound-statement  */
18881
18882 static void
18883 cp_parser_handler (cp_parser* parser)
18884 {
18885   tree handler;
18886   tree declaration;
18887
18888   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18889   handler = begin_handler ();
18890   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18891   declaration = cp_parser_exception_declaration (parser);
18892   finish_handler_parms (declaration, handler);
18893   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18894   cp_parser_compound_statement (parser, NULL, false, false);
18895   finish_handler (handler);
18896 }
18897
18898 /* Parse an exception-declaration.
18899
18900    exception-declaration:
18901      type-specifier-seq declarator
18902      type-specifier-seq abstract-declarator
18903      type-specifier-seq
18904      ...
18905
18906    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18907    ellipsis variant is used.  */
18908
18909 static tree
18910 cp_parser_exception_declaration (cp_parser* parser)
18911 {
18912   cp_decl_specifier_seq type_specifiers;
18913   cp_declarator *declarator;
18914   const char *saved_message;
18915
18916   /* If it's an ellipsis, it's easy to handle.  */
18917   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18918     {
18919       /* Consume the `...' token.  */
18920       cp_lexer_consume_token (parser->lexer);
18921       return NULL_TREE;
18922     }
18923
18924   /* Types may not be defined in exception-declarations.  */
18925   saved_message = parser->type_definition_forbidden_message;
18926   parser->type_definition_forbidden_message
18927     = G_("types may not be defined in exception-declarations");
18928
18929   /* Parse the type-specifier-seq.  */
18930   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18931                                 /*is_trailing_return=*/false,
18932                                 &type_specifiers);
18933   /* If it's a `)', then there is no declarator.  */
18934   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18935     declarator = NULL;
18936   else
18937     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18938                                        /*ctor_dtor_or_conv_p=*/NULL,
18939                                        /*parenthesized_p=*/NULL,
18940                                        /*member_p=*/false);
18941
18942   /* Restore the saved message.  */
18943   parser->type_definition_forbidden_message = saved_message;
18944
18945   if (!type_specifiers.any_specifiers_p)
18946     return error_mark_node;
18947
18948   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18949 }
18950
18951 /* Parse a throw-expression.
18952
18953    throw-expression:
18954      throw assignment-expression [opt]
18955
18956    Returns a THROW_EXPR representing the throw-expression.  */
18957
18958 static tree
18959 cp_parser_throw_expression (cp_parser* parser)
18960 {
18961   tree expression;
18962   cp_token* token;
18963
18964   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18965   token = cp_lexer_peek_token (parser->lexer);
18966   /* Figure out whether or not there is an assignment-expression
18967      following the "throw" keyword.  */
18968   if (token->type == CPP_COMMA
18969       || token->type == CPP_SEMICOLON
18970       || token->type == CPP_CLOSE_PAREN
18971       || token->type == CPP_CLOSE_SQUARE
18972       || token->type == CPP_CLOSE_BRACE
18973       || token->type == CPP_COLON)
18974     expression = NULL_TREE;
18975   else
18976     expression = cp_parser_assignment_expression (parser,
18977                                                   /*cast_p=*/false, NULL);
18978
18979   return build_throw (expression);
18980 }
18981
18982 /* GNU Extensions */
18983
18984 /* Parse an (optional) asm-specification.
18985
18986    asm-specification:
18987      asm ( string-literal )
18988
18989    If the asm-specification is present, returns a STRING_CST
18990    corresponding to the string-literal.  Otherwise, returns
18991    NULL_TREE.  */
18992
18993 static tree
18994 cp_parser_asm_specification_opt (cp_parser* parser)
18995 {
18996   cp_token *token;
18997   tree asm_specification;
18998
18999   /* Peek at the next token.  */
19000   token = cp_lexer_peek_token (parser->lexer);
19001   /* If the next token isn't the `asm' keyword, then there's no
19002      asm-specification.  */
19003   if (!cp_parser_is_keyword (token, RID_ASM))
19004     return NULL_TREE;
19005
19006   /* Consume the `asm' token.  */
19007   cp_lexer_consume_token (parser->lexer);
19008   /* Look for the `('.  */
19009   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19010
19011   /* Look for the string-literal.  */
19012   asm_specification = cp_parser_string_literal (parser, false, false);
19013
19014   /* Look for the `)'.  */
19015   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19016
19017   return asm_specification;
19018 }
19019
19020 /* Parse an asm-operand-list.
19021
19022    asm-operand-list:
19023      asm-operand
19024      asm-operand-list , asm-operand
19025
19026    asm-operand:
19027      string-literal ( expression )
19028      [ string-literal ] string-literal ( expression )
19029
19030    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19031    each node is the expression.  The TREE_PURPOSE is itself a
19032    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19033    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19034    is a STRING_CST for the string literal before the parenthesis. Returns
19035    ERROR_MARK_NODE if any of the operands are invalid.  */
19036
19037 static tree
19038 cp_parser_asm_operand_list (cp_parser* parser)
19039 {
19040   tree asm_operands = NULL_TREE;
19041   bool invalid_operands = false;
19042
19043   while (true)
19044     {
19045       tree string_literal;
19046       tree expression;
19047       tree name;
19048
19049       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19050         {
19051           /* Consume the `[' token.  */
19052           cp_lexer_consume_token (parser->lexer);
19053           /* Read the operand name.  */
19054           name = cp_parser_identifier (parser);
19055           if (name != error_mark_node)
19056             name = build_string (IDENTIFIER_LENGTH (name),
19057                                  IDENTIFIER_POINTER (name));
19058           /* Look for the closing `]'.  */
19059           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19060         }
19061       else
19062         name = NULL_TREE;
19063       /* Look for the string-literal.  */
19064       string_literal = cp_parser_string_literal (parser, false, false);
19065
19066       /* Look for the `('.  */
19067       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19068       /* Parse the expression.  */
19069       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19070       /* Look for the `)'.  */
19071       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19072
19073       if (name == error_mark_node 
19074           || string_literal == error_mark_node 
19075           || expression == error_mark_node)
19076         invalid_operands = true;
19077
19078       /* Add this operand to the list.  */
19079       asm_operands = tree_cons (build_tree_list (name, string_literal),
19080                                 expression,
19081                                 asm_operands);
19082       /* If the next token is not a `,', there are no more
19083          operands.  */
19084       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19085         break;
19086       /* Consume the `,'.  */
19087       cp_lexer_consume_token (parser->lexer);
19088     }
19089
19090   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19091 }
19092
19093 /* Parse an asm-clobber-list.
19094
19095    asm-clobber-list:
19096      string-literal
19097      asm-clobber-list , string-literal
19098
19099    Returns a TREE_LIST, indicating the clobbers in the order that they
19100    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19101
19102 static tree
19103 cp_parser_asm_clobber_list (cp_parser* parser)
19104 {
19105   tree clobbers = NULL_TREE;
19106
19107   while (true)
19108     {
19109       tree string_literal;
19110
19111       /* Look for the string literal.  */
19112       string_literal = cp_parser_string_literal (parser, false, false);
19113       /* Add it to the list.  */
19114       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19115       /* If the next token is not a `,', then the list is
19116          complete.  */
19117       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19118         break;
19119       /* Consume the `,' token.  */
19120       cp_lexer_consume_token (parser->lexer);
19121     }
19122
19123   return clobbers;
19124 }
19125
19126 /* Parse an asm-label-list.
19127
19128    asm-label-list:
19129      identifier
19130      asm-label-list , identifier
19131
19132    Returns a TREE_LIST, indicating the labels in the order that they
19133    appeared.  The TREE_VALUE of each node is a label.  */
19134
19135 static tree
19136 cp_parser_asm_label_list (cp_parser* parser)
19137 {
19138   tree labels = NULL_TREE;
19139
19140   while (true)
19141     {
19142       tree identifier, label, name;
19143
19144       /* Look for the identifier.  */
19145       identifier = cp_parser_identifier (parser);
19146       if (!error_operand_p (identifier))
19147         {
19148           label = lookup_label (identifier);
19149           if (TREE_CODE (label) == LABEL_DECL)
19150             {
19151               TREE_USED (label) = 1;
19152               check_goto (label);
19153               name = build_string (IDENTIFIER_LENGTH (identifier),
19154                                    IDENTIFIER_POINTER (identifier));
19155               labels = tree_cons (name, label, labels);
19156             }
19157         }
19158       /* If the next token is not a `,', then the list is
19159          complete.  */
19160       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19161         break;
19162       /* Consume the `,' token.  */
19163       cp_lexer_consume_token (parser->lexer);
19164     }
19165
19166   return nreverse (labels);
19167 }
19168
19169 /* Parse an (optional) series of attributes.
19170
19171    attributes:
19172      attributes attribute
19173
19174    attribute:
19175      __attribute__ (( attribute-list [opt] ))
19176
19177    The return value is as for cp_parser_attribute_list.  */
19178
19179 static tree
19180 cp_parser_attributes_opt (cp_parser* parser)
19181 {
19182   tree attributes = NULL_TREE;
19183
19184   while (true)
19185     {
19186       cp_token *token;
19187       tree attribute_list;
19188
19189       /* Peek at the next token.  */
19190       token = cp_lexer_peek_token (parser->lexer);
19191       /* If it's not `__attribute__', then we're done.  */
19192       if (token->keyword != RID_ATTRIBUTE)
19193         break;
19194
19195       /* Consume the `__attribute__' keyword.  */
19196       cp_lexer_consume_token (parser->lexer);
19197       /* Look for the two `(' tokens.  */
19198       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19199       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19200
19201       /* Peek at the next token.  */
19202       token = cp_lexer_peek_token (parser->lexer);
19203       if (token->type != CPP_CLOSE_PAREN)
19204         /* Parse the attribute-list.  */
19205         attribute_list = cp_parser_attribute_list (parser);
19206       else
19207         /* If the next token is a `)', then there is no attribute
19208            list.  */
19209         attribute_list = NULL;
19210
19211       /* Look for the two `)' tokens.  */
19212       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19213       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19214
19215       /* Add these new attributes to the list.  */
19216       attributes = chainon (attributes, attribute_list);
19217     }
19218
19219   return attributes;
19220 }
19221
19222 /* Parse an attribute-list.
19223
19224    attribute-list:
19225      attribute
19226      attribute-list , attribute
19227
19228    attribute:
19229      identifier
19230      identifier ( identifier )
19231      identifier ( identifier , expression-list )
19232      identifier ( expression-list )
19233
19234    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19235    to an attribute.  The TREE_PURPOSE of each node is the identifier
19236    indicating which attribute is in use.  The TREE_VALUE represents
19237    the arguments, if any.  */
19238
19239 static tree
19240 cp_parser_attribute_list (cp_parser* parser)
19241 {
19242   tree attribute_list = NULL_TREE;
19243   bool save_translate_strings_p = parser->translate_strings_p;
19244
19245   parser->translate_strings_p = false;
19246   while (true)
19247     {
19248       cp_token *token;
19249       tree identifier;
19250       tree attribute;
19251
19252       /* Look for the identifier.  We also allow keywords here; for
19253          example `__attribute__ ((const))' is legal.  */
19254       token = cp_lexer_peek_token (parser->lexer);
19255       if (token->type == CPP_NAME
19256           || token->type == CPP_KEYWORD)
19257         {
19258           tree arguments = NULL_TREE;
19259
19260           /* Consume the token.  */
19261           token = cp_lexer_consume_token (parser->lexer);
19262
19263           /* Save away the identifier that indicates which attribute
19264              this is.  */
19265           identifier = (token->type == CPP_KEYWORD) 
19266             /* For keywords, use the canonical spelling, not the
19267                parsed identifier.  */
19268             ? ridpointers[(int) token->keyword]
19269             : token->u.value;
19270           
19271           attribute = build_tree_list (identifier, NULL_TREE);
19272
19273           /* Peek at the next token.  */
19274           token = cp_lexer_peek_token (parser->lexer);
19275           /* If it's an `(', then parse the attribute arguments.  */
19276           if (token->type == CPP_OPEN_PAREN)
19277             {
19278               VEC(tree,gc) *vec;
19279               int attr_flag = (attribute_takes_identifier_p (identifier)
19280                                ? id_attr : normal_attr);
19281               vec = cp_parser_parenthesized_expression_list
19282                     (parser, attr_flag, /*cast_p=*/false,
19283                      /*allow_expansion_p=*/false,
19284                      /*non_constant_p=*/NULL);
19285               if (vec == NULL)
19286                 arguments = error_mark_node;
19287               else
19288                 {
19289                   arguments = build_tree_list_vec (vec);
19290                   release_tree_vector (vec);
19291                 }
19292               /* Save the arguments away.  */
19293               TREE_VALUE (attribute) = arguments;
19294             }
19295
19296           if (arguments != error_mark_node)
19297             {
19298               /* Add this attribute to the list.  */
19299               TREE_CHAIN (attribute) = attribute_list;
19300               attribute_list = attribute;
19301             }
19302
19303           token = cp_lexer_peek_token (parser->lexer);
19304         }
19305       /* Now, look for more attributes.  If the next token isn't a
19306          `,', we're done.  */
19307       if (token->type != CPP_COMMA)
19308         break;
19309
19310       /* Consume the comma and keep going.  */
19311       cp_lexer_consume_token (parser->lexer);
19312     }
19313   parser->translate_strings_p = save_translate_strings_p;
19314
19315   /* We built up the list in reverse order.  */
19316   return nreverse (attribute_list);
19317 }
19318
19319 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19320    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19321    current value of the PEDANTIC flag, regardless of whether or not
19322    the `__extension__' keyword is present.  The caller is responsible
19323    for restoring the value of the PEDANTIC flag.  */
19324
19325 static bool
19326 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19327 {
19328   /* Save the old value of the PEDANTIC flag.  */
19329   *saved_pedantic = pedantic;
19330
19331   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19332     {
19333       /* Consume the `__extension__' token.  */
19334       cp_lexer_consume_token (parser->lexer);
19335       /* We're not being pedantic while the `__extension__' keyword is
19336          in effect.  */
19337       pedantic = 0;
19338
19339       return true;
19340     }
19341
19342   return false;
19343 }
19344
19345 /* Parse a label declaration.
19346
19347    label-declaration:
19348      __label__ label-declarator-seq ;
19349
19350    label-declarator-seq:
19351      identifier , label-declarator-seq
19352      identifier  */
19353
19354 static void
19355 cp_parser_label_declaration (cp_parser* parser)
19356 {
19357   /* Look for the `__label__' keyword.  */
19358   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19359
19360   while (true)
19361     {
19362       tree identifier;
19363
19364       /* Look for an identifier.  */
19365       identifier = cp_parser_identifier (parser);
19366       /* If we failed, stop.  */
19367       if (identifier == error_mark_node)
19368         break;
19369       /* Declare it as a label.  */
19370       finish_label_decl (identifier);
19371       /* If the next token is a `;', stop.  */
19372       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19373         break;
19374       /* Look for the `,' separating the label declarations.  */
19375       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19376     }
19377
19378   /* Look for the final `;'.  */
19379   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19380 }
19381
19382 /* Support Functions */
19383
19384 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19385    NAME should have one of the representations used for an
19386    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19387    is returned.  If PARSER->SCOPE is a dependent type, then a
19388    SCOPE_REF is returned.
19389
19390    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19391    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19392    was formed.  Abstractly, such entities should not be passed to this
19393    function, because they do not need to be looked up, but it is
19394    simpler to check for this special case here, rather than at the
19395    call-sites.
19396
19397    In cases not explicitly covered above, this function returns a
19398    DECL, OVERLOAD, or baselink representing the result of the lookup.
19399    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19400    is returned.
19401
19402    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19403    (e.g., "struct") that was used.  In that case bindings that do not
19404    refer to types are ignored.
19405
19406    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19407    ignored.
19408
19409    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19410    are ignored.
19411
19412    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19413    types.
19414
19415    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19416    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19417    NULL_TREE otherwise.  */
19418
19419 static tree
19420 cp_parser_lookup_name (cp_parser *parser, tree name,
19421                        enum tag_types tag_type,
19422                        bool is_template,
19423                        bool is_namespace,
19424                        bool check_dependency,
19425                        tree *ambiguous_decls,
19426                        location_t name_location)
19427 {
19428   int flags = 0;
19429   tree decl;
19430   tree object_type = parser->context->object_type;
19431
19432   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19433     flags |= LOOKUP_COMPLAIN;
19434
19435   /* Assume that the lookup will be unambiguous.  */
19436   if (ambiguous_decls)
19437     *ambiguous_decls = NULL_TREE;
19438
19439   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19440      no longer valid.  Note that if we are parsing tentatively, and
19441      the parse fails, OBJECT_TYPE will be automatically restored.  */
19442   parser->context->object_type = NULL_TREE;
19443
19444   if (name == error_mark_node)
19445     return error_mark_node;
19446
19447   /* A template-id has already been resolved; there is no lookup to
19448      do.  */
19449   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19450     return name;
19451   if (BASELINK_P (name))
19452     {
19453       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19454                   == TEMPLATE_ID_EXPR);
19455       return name;
19456     }
19457
19458   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19459      it should already have been checked to make sure that the name
19460      used matches the type being destroyed.  */
19461   if (TREE_CODE (name) == BIT_NOT_EXPR)
19462     {
19463       tree type;
19464
19465       /* Figure out to which type this destructor applies.  */
19466       if (parser->scope)
19467         type = parser->scope;
19468       else if (object_type)
19469         type = object_type;
19470       else
19471         type = current_class_type;
19472       /* If that's not a class type, there is no destructor.  */
19473       if (!type || !CLASS_TYPE_P (type))
19474         return error_mark_node;
19475       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19476         lazily_declare_fn (sfk_destructor, type);
19477       if (!CLASSTYPE_DESTRUCTORS (type))
19478           return error_mark_node;
19479       /* If it was a class type, return the destructor.  */
19480       return CLASSTYPE_DESTRUCTORS (type);
19481     }
19482
19483   /* By this point, the NAME should be an ordinary identifier.  If
19484      the id-expression was a qualified name, the qualifying scope is
19485      stored in PARSER->SCOPE at this point.  */
19486   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19487
19488   /* Perform the lookup.  */
19489   if (parser->scope)
19490     {
19491       bool dependent_p;
19492
19493       if (parser->scope == error_mark_node)
19494         return error_mark_node;
19495
19496       /* If the SCOPE is dependent, the lookup must be deferred until
19497          the template is instantiated -- unless we are explicitly
19498          looking up names in uninstantiated templates.  Even then, we
19499          cannot look up the name if the scope is not a class type; it
19500          might, for example, be a template type parameter.  */
19501       dependent_p = (TYPE_P (parser->scope)
19502                      && dependent_scope_p (parser->scope));
19503       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19504           && dependent_p)
19505         /* Defer lookup.  */
19506         decl = error_mark_node;
19507       else
19508         {
19509           tree pushed_scope = NULL_TREE;
19510
19511           /* If PARSER->SCOPE is a dependent type, then it must be a
19512              class type, and we must not be checking dependencies;
19513              otherwise, we would have processed this lookup above.  So
19514              that PARSER->SCOPE is not considered a dependent base by
19515              lookup_member, we must enter the scope here.  */
19516           if (dependent_p)
19517             pushed_scope = push_scope (parser->scope);
19518
19519           /* If the PARSER->SCOPE is a template specialization, it
19520              may be instantiated during name lookup.  In that case,
19521              errors may be issued.  Even if we rollback the current
19522              tentative parse, those errors are valid.  */
19523           decl = lookup_qualified_name (parser->scope, name,
19524                                         tag_type != none_type,
19525                                         /*complain=*/true);
19526
19527           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19528              lookup result and the nested-name-specifier nominates a class C:
19529                * if the name specified after the nested-name-specifier, when
19530                looked up in C, is the injected-class-name of C (Clause 9), or
19531                * if the name specified after the nested-name-specifier is the
19532                same as the identifier or the simple-template-id's template-
19533                name in the last component of the nested-name-specifier,
19534              the name is instead considered to name the constructor of
19535              class C. [ Note: for example, the constructor is not an
19536              acceptable lookup result in an elaborated-type-specifier so
19537              the constructor would not be used in place of the
19538              injected-class-name. --end note ] Such a constructor name
19539              shall be used only in the declarator-id of a declaration that
19540              names a constructor or in a using-declaration.  */
19541           if (tag_type == none_type
19542               && DECL_SELF_REFERENCE_P (decl)
19543               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19544             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19545                                           tag_type != none_type,
19546                                           /*complain=*/true);
19547
19548           /* If we have a single function from a using decl, pull it out.  */
19549           if (TREE_CODE (decl) == OVERLOAD
19550               && !really_overloaded_fn (decl))
19551             decl = OVL_FUNCTION (decl);
19552
19553           if (pushed_scope)
19554             pop_scope (pushed_scope);
19555         }
19556
19557       /* If the scope is a dependent type and either we deferred lookup or
19558          we did lookup but didn't find the name, rememeber the name.  */
19559       if (decl == error_mark_node && TYPE_P (parser->scope)
19560           && dependent_type_p (parser->scope))
19561         {
19562           if (tag_type)
19563             {
19564               tree type;
19565
19566               /* The resolution to Core Issue 180 says that `struct
19567                  A::B' should be considered a type-name, even if `A'
19568                  is dependent.  */
19569               type = make_typename_type (parser->scope, name, tag_type,
19570                                          /*complain=*/tf_error);
19571               decl = TYPE_NAME (type);
19572             }
19573           else if (is_template
19574                    && (cp_parser_next_token_ends_template_argument_p (parser)
19575                        || cp_lexer_next_token_is (parser->lexer,
19576                                                   CPP_CLOSE_PAREN)))
19577             decl = make_unbound_class_template (parser->scope,
19578                                                 name, NULL_TREE,
19579                                                 /*complain=*/tf_error);
19580           else
19581             decl = build_qualified_name (/*type=*/NULL_TREE,
19582                                          parser->scope, name,
19583                                          is_template);
19584         }
19585       parser->qualifying_scope = parser->scope;
19586       parser->object_scope = NULL_TREE;
19587     }
19588   else if (object_type)
19589     {
19590       tree object_decl = NULL_TREE;
19591       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19592          OBJECT_TYPE is not a class.  */
19593       if (CLASS_TYPE_P (object_type))
19594         /* If the OBJECT_TYPE is a template specialization, it may
19595            be instantiated during name lookup.  In that case, errors
19596            may be issued.  Even if we rollback the current tentative
19597            parse, those errors are valid.  */
19598         object_decl = lookup_member (object_type,
19599                                      name,
19600                                      /*protect=*/0,
19601                                      tag_type != none_type);
19602       /* Look it up in the enclosing context, too.  */
19603       decl = lookup_name_real (name, tag_type != none_type,
19604                                /*nonclass=*/0,
19605                                /*block_p=*/true, is_namespace, flags);
19606       parser->object_scope = object_type;
19607       parser->qualifying_scope = NULL_TREE;
19608       if (object_decl)
19609         decl = object_decl;
19610     }
19611   else
19612     {
19613       decl = lookup_name_real (name, tag_type != none_type,
19614                                /*nonclass=*/0,
19615                                /*block_p=*/true, is_namespace, flags);
19616       parser->qualifying_scope = NULL_TREE;
19617       parser->object_scope = NULL_TREE;
19618     }
19619
19620   /* If the lookup failed, let our caller know.  */
19621   if (!decl || decl == error_mark_node)
19622     return error_mark_node;
19623
19624   /* Pull out the template from an injected-class-name (or multiple).  */
19625   if (is_template)
19626     decl = maybe_get_template_decl_from_type_decl (decl);
19627
19628   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19629   if (TREE_CODE (decl) == TREE_LIST)
19630     {
19631       if (ambiguous_decls)
19632         *ambiguous_decls = decl;
19633       /* The error message we have to print is too complicated for
19634          cp_parser_error, so we incorporate its actions directly.  */
19635       if (!cp_parser_simulate_error (parser))
19636         {
19637           error_at (name_location, "reference to %qD is ambiguous",
19638                     name);
19639           print_candidates (decl);
19640         }
19641       return error_mark_node;
19642     }
19643
19644   gcc_assert (DECL_P (decl)
19645               || TREE_CODE (decl) == OVERLOAD
19646               || TREE_CODE (decl) == SCOPE_REF
19647               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19648               || BASELINK_P (decl));
19649
19650   /* If we have resolved the name of a member declaration, check to
19651      see if the declaration is accessible.  When the name resolves to
19652      set of overloaded functions, accessibility is checked when
19653      overload resolution is done.
19654
19655      During an explicit instantiation, access is not checked at all,
19656      as per [temp.explicit].  */
19657   if (DECL_P (decl))
19658     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19659
19660   maybe_record_typedef_use (decl);
19661
19662   return decl;
19663 }
19664
19665 /* Like cp_parser_lookup_name, but for use in the typical case where
19666    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19667    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19668
19669 static tree
19670 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19671 {
19672   return cp_parser_lookup_name (parser, name,
19673                                 none_type,
19674                                 /*is_template=*/false,
19675                                 /*is_namespace=*/false,
19676                                 /*check_dependency=*/true,
19677                                 /*ambiguous_decls=*/NULL,
19678                                 location);
19679 }
19680
19681 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19682    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19683    true, the DECL indicates the class being defined in a class-head,
19684    or declared in an elaborated-type-specifier.
19685
19686    Otherwise, return DECL.  */
19687
19688 static tree
19689 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19690 {
19691   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19692      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19693
19694        struct A {
19695          template <typename T> struct B;
19696        };
19697
19698        template <typename T> struct A::B {};
19699
19700      Similarly, in an elaborated-type-specifier:
19701
19702        namespace N { struct X{}; }
19703
19704        struct A {
19705          template <typename T> friend struct N::X;
19706        };
19707
19708      However, if the DECL refers to a class type, and we are in
19709      the scope of the class, then the name lookup automatically
19710      finds the TYPE_DECL created by build_self_reference rather
19711      than a TEMPLATE_DECL.  For example, in:
19712
19713        template <class T> struct S {
19714          S s;
19715        };
19716
19717      there is no need to handle such case.  */
19718
19719   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19720     return DECL_TEMPLATE_RESULT (decl);
19721
19722   return decl;
19723 }
19724
19725 /* If too many, or too few, template-parameter lists apply to the
19726    declarator, issue an error message.  Returns TRUE if all went well,
19727    and FALSE otherwise.  */
19728
19729 static bool
19730 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19731                                                 cp_declarator *declarator,
19732                                                 location_t declarator_location)
19733 {
19734   unsigned num_templates;
19735
19736   /* We haven't seen any classes that involve template parameters yet.  */
19737   num_templates = 0;
19738
19739   switch (declarator->kind)
19740     {
19741     case cdk_id:
19742       if (declarator->u.id.qualifying_scope)
19743         {
19744           tree scope;
19745
19746           scope = declarator->u.id.qualifying_scope;
19747
19748           while (scope && CLASS_TYPE_P (scope))
19749             {
19750               /* You're supposed to have one `template <...>'
19751                  for every template class, but you don't need one
19752                  for a full specialization.  For example:
19753
19754                  template <class T> struct S{};
19755                  template <> struct S<int> { void f(); };
19756                  void S<int>::f () {}
19757
19758                  is correct; there shouldn't be a `template <>' for
19759                  the definition of `S<int>::f'.  */
19760               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19761                 /* If SCOPE does not have template information of any
19762                    kind, then it is not a template, nor is it nested
19763                    within a template.  */
19764                 break;
19765               if (explicit_class_specialization_p (scope))
19766                 break;
19767               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19768                 ++num_templates;
19769
19770               scope = TYPE_CONTEXT (scope);
19771             }
19772         }
19773       else if (TREE_CODE (declarator->u.id.unqualified_name)
19774                == TEMPLATE_ID_EXPR)
19775         /* If the DECLARATOR has the form `X<y>' then it uses one
19776            additional level of template parameters.  */
19777         ++num_templates;
19778
19779       return cp_parser_check_template_parameters 
19780         (parser, num_templates, declarator_location, declarator);
19781
19782
19783     case cdk_function:
19784     case cdk_array:
19785     case cdk_pointer:
19786     case cdk_reference:
19787     case cdk_ptrmem:
19788       return (cp_parser_check_declarator_template_parameters
19789               (parser, declarator->declarator, declarator_location));
19790
19791     case cdk_error:
19792       return true;
19793
19794     default:
19795       gcc_unreachable ();
19796     }
19797   return false;
19798 }
19799
19800 /* NUM_TEMPLATES were used in the current declaration.  If that is
19801    invalid, return FALSE and issue an error messages.  Otherwise,
19802    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19803    declarator and we can print more accurate diagnostics.  */
19804
19805 static bool
19806 cp_parser_check_template_parameters (cp_parser* parser,
19807                                      unsigned num_templates,
19808                                      location_t location,
19809                                      cp_declarator *declarator)
19810 {
19811   /* If there are the same number of template classes and parameter
19812      lists, that's OK.  */
19813   if (parser->num_template_parameter_lists == num_templates)
19814     return true;
19815   /* If there are more, but only one more, then we are referring to a
19816      member template.  That's OK too.  */
19817   if (parser->num_template_parameter_lists == num_templates + 1)
19818     return true;
19819   /* If there are more template classes than parameter lists, we have
19820      something like:
19821
19822        template <class T> void S<T>::R<T>::f ();  */
19823   if (parser->num_template_parameter_lists < num_templates)
19824     {
19825       if (declarator && !current_function_decl)
19826         error_at (location, "specializing member %<%T::%E%> "
19827                   "requires %<template<>%> syntax", 
19828                   declarator->u.id.qualifying_scope,
19829                   declarator->u.id.unqualified_name);
19830       else if (declarator)
19831         error_at (location, "invalid declaration of %<%T::%E%>",
19832                   declarator->u.id.qualifying_scope,
19833                   declarator->u.id.unqualified_name);
19834       else 
19835         error_at (location, "too few template-parameter-lists");
19836       return false;
19837     }
19838   /* Otherwise, there are too many template parameter lists.  We have
19839      something like:
19840
19841      template <class T> template <class U> void S::f();  */
19842   error_at (location, "too many template-parameter-lists");
19843   return false;
19844 }
19845
19846 /* Parse an optional `::' token indicating that the following name is
19847    from the global namespace.  If so, PARSER->SCOPE is set to the
19848    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19849    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19850    Returns the new value of PARSER->SCOPE, if the `::' token is
19851    present, and NULL_TREE otherwise.  */
19852
19853 static tree
19854 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19855 {
19856   cp_token *token;
19857
19858   /* Peek at the next token.  */
19859   token = cp_lexer_peek_token (parser->lexer);
19860   /* If we're looking at a `::' token then we're starting from the
19861      global namespace, not our current location.  */
19862   if (token->type == CPP_SCOPE)
19863     {
19864       /* Consume the `::' token.  */
19865       cp_lexer_consume_token (parser->lexer);
19866       /* Set the SCOPE so that we know where to start the lookup.  */
19867       parser->scope = global_namespace;
19868       parser->qualifying_scope = global_namespace;
19869       parser->object_scope = NULL_TREE;
19870
19871       return parser->scope;
19872     }
19873   else if (!current_scope_valid_p)
19874     {
19875       parser->scope = NULL_TREE;
19876       parser->qualifying_scope = NULL_TREE;
19877       parser->object_scope = NULL_TREE;
19878     }
19879
19880   return NULL_TREE;
19881 }
19882
19883 /* Returns TRUE if the upcoming token sequence is the start of a
19884    constructor declarator.  If FRIEND_P is true, the declarator is
19885    preceded by the `friend' specifier.  */
19886
19887 static bool
19888 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19889 {
19890   bool constructor_p;
19891   tree nested_name_specifier;
19892   cp_token *next_token;
19893
19894   /* The common case is that this is not a constructor declarator, so
19895      try to avoid doing lots of work if at all possible.  It's not
19896      valid declare a constructor at function scope.  */
19897   if (parser->in_function_body)
19898     return false;
19899   /* And only certain tokens can begin a constructor declarator.  */
19900   next_token = cp_lexer_peek_token (parser->lexer);
19901   if (next_token->type != CPP_NAME
19902       && next_token->type != CPP_SCOPE
19903       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19904       && next_token->type != CPP_TEMPLATE_ID)
19905     return false;
19906
19907   /* Parse tentatively; we are going to roll back all of the tokens
19908      consumed here.  */
19909   cp_parser_parse_tentatively (parser);
19910   /* Assume that we are looking at a constructor declarator.  */
19911   constructor_p = true;
19912
19913   /* Look for the optional `::' operator.  */
19914   cp_parser_global_scope_opt (parser,
19915                               /*current_scope_valid_p=*/false);
19916   /* Look for the nested-name-specifier.  */
19917   nested_name_specifier
19918     = (cp_parser_nested_name_specifier_opt (parser,
19919                                             /*typename_keyword_p=*/false,
19920                                             /*check_dependency_p=*/false,
19921                                             /*type_p=*/false,
19922                                             /*is_declaration=*/false));
19923   /* Outside of a class-specifier, there must be a
19924      nested-name-specifier.  */
19925   if (!nested_name_specifier &&
19926       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19927        || friend_p))
19928     constructor_p = false;
19929   else if (nested_name_specifier == error_mark_node)
19930     constructor_p = false;
19931
19932   /* If we have a class scope, this is easy; DR 147 says that S::S always
19933      names the constructor, and no other qualified name could.  */
19934   if (constructor_p && nested_name_specifier
19935       && CLASS_TYPE_P (nested_name_specifier))
19936     {
19937       tree id = cp_parser_unqualified_id (parser,
19938                                           /*template_keyword_p=*/false,
19939                                           /*check_dependency_p=*/false,
19940                                           /*declarator_p=*/true,
19941                                           /*optional_p=*/false);
19942       if (is_overloaded_fn (id))
19943         id = DECL_NAME (get_first_fn (id));
19944       if (!constructor_name_p (id, nested_name_specifier))
19945         constructor_p = false;
19946     }
19947   /* If we still think that this might be a constructor-declarator,
19948      look for a class-name.  */
19949   else if (constructor_p)
19950     {
19951       /* If we have:
19952
19953            template <typename T> struct S {
19954              S();
19955            };
19956
19957          we must recognize that the nested `S' names a class.  */
19958       tree type_decl;
19959       type_decl = cp_parser_class_name (parser,
19960                                         /*typename_keyword_p=*/false,
19961                                         /*template_keyword_p=*/false,
19962                                         none_type,
19963                                         /*check_dependency_p=*/false,
19964                                         /*class_head_p=*/false,
19965                                         /*is_declaration=*/false);
19966       /* If there was no class-name, then this is not a constructor.  */
19967       constructor_p = !cp_parser_error_occurred (parser);
19968
19969       /* If we're still considering a constructor, we have to see a `(',
19970          to begin the parameter-declaration-clause, followed by either a
19971          `)', an `...', or a decl-specifier.  We need to check for a
19972          type-specifier to avoid being fooled into thinking that:
19973
19974            S (f) (int);
19975
19976          is a constructor.  (It is actually a function named `f' that
19977          takes one parameter (of type `int') and returns a value of type
19978          `S'.  */
19979       if (constructor_p
19980           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19981         constructor_p = false;
19982
19983       if (constructor_p
19984           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19985           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19986           /* A parameter declaration begins with a decl-specifier,
19987              which is either the "attribute" keyword, a storage class
19988              specifier, or (usually) a type-specifier.  */
19989           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19990         {
19991           tree type;
19992           tree pushed_scope = NULL_TREE;
19993           unsigned saved_num_template_parameter_lists;
19994
19995           /* Names appearing in the type-specifier should be looked up
19996              in the scope of the class.  */
19997           if (current_class_type)
19998             type = NULL_TREE;
19999           else
20000             {
20001               type = TREE_TYPE (type_decl);
20002               if (TREE_CODE (type) == TYPENAME_TYPE)
20003                 {
20004                   type = resolve_typename_type (type,
20005                                                 /*only_current_p=*/false);
20006                   if (TREE_CODE (type) == TYPENAME_TYPE)
20007                     {
20008                       cp_parser_abort_tentative_parse (parser);
20009                       return false;
20010                     }
20011                 }
20012               pushed_scope = push_scope (type);
20013             }
20014
20015           /* Inside the constructor parameter list, surrounding
20016              template-parameter-lists do not apply.  */
20017           saved_num_template_parameter_lists
20018             = parser->num_template_parameter_lists;
20019           parser->num_template_parameter_lists = 0;
20020
20021           /* Look for the type-specifier.  */
20022           cp_parser_type_specifier (parser,
20023                                     CP_PARSER_FLAGS_NONE,
20024                                     /*decl_specs=*/NULL,
20025                                     /*is_declarator=*/true,
20026                                     /*declares_class_or_enum=*/NULL,
20027                                     /*is_cv_qualifier=*/NULL);
20028
20029           parser->num_template_parameter_lists
20030             = saved_num_template_parameter_lists;
20031
20032           /* Leave the scope of the class.  */
20033           if (pushed_scope)
20034             pop_scope (pushed_scope);
20035
20036           constructor_p = !cp_parser_error_occurred (parser);
20037         }
20038     }
20039
20040   /* We did not really want to consume any tokens.  */
20041   cp_parser_abort_tentative_parse (parser);
20042
20043   return constructor_p;
20044 }
20045
20046 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20047    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20048    they must be performed once we are in the scope of the function.
20049
20050    Returns the function defined.  */
20051
20052 static tree
20053 cp_parser_function_definition_from_specifiers_and_declarator
20054   (cp_parser* parser,
20055    cp_decl_specifier_seq *decl_specifiers,
20056    tree attributes,
20057    const cp_declarator *declarator)
20058 {
20059   tree fn;
20060   bool success_p;
20061
20062   /* Begin the function-definition.  */
20063   success_p = start_function (decl_specifiers, declarator, attributes);
20064
20065   /* The things we're about to see are not directly qualified by any
20066      template headers we've seen thus far.  */
20067   reset_specialization ();
20068
20069   /* If there were names looked up in the decl-specifier-seq that we
20070      did not check, check them now.  We must wait until we are in the
20071      scope of the function to perform the checks, since the function
20072      might be a friend.  */
20073   perform_deferred_access_checks ();
20074
20075   if (!success_p)
20076     {
20077       /* Skip the entire function.  */
20078       cp_parser_skip_to_end_of_block_or_statement (parser);
20079       fn = error_mark_node;
20080     }
20081   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20082     {
20083       /* Seen already, skip it.  An error message has already been output.  */
20084       cp_parser_skip_to_end_of_block_or_statement (parser);
20085       fn = current_function_decl;
20086       current_function_decl = NULL_TREE;
20087       /* If this is a function from a class, pop the nested class.  */
20088       if (current_class_name)
20089         pop_nested_class ();
20090     }
20091   else
20092     {
20093       timevar_id_t tv;
20094       if (DECL_DECLARED_INLINE_P (current_function_decl))
20095         tv = TV_PARSE_INLINE;
20096       else
20097         tv = TV_PARSE_FUNC;
20098       timevar_push (tv);
20099       fn = cp_parser_function_definition_after_declarator (parser,
20100                                                          /*inline_p=*/false);
20101       timevar_pop (tv);
20102     }
20103
20104   return fn;
20105 }
20106
20107 /* Parse the part of a function-definition that follows the
20108    declarator.  INLINE_P is TRUE iff this function is an inline
20109    function defined within a class-specifier.
20110
20111    Returns the function defined.  */
20112
20113 static tree
20114 cp_parser_function_definition_after_declarator (cp_parser* parser,
20115                                                 bool inline_p)
20116 {
20117   tree fn;
20118   bool ctor_initializer_p = false;
20119   bool saved_in_unbraced_linkage_specification_p;
20120   bool saved_in_function_body;
20121   unsigned saved_num_template_parameter_lists;
20122   cp_token *token;
20123
20124   saved_in_function_body = parser->in_function_body;
20125   parser->in_function_body = true;
20126   /* If the next token is `return', then the code may be trying to
20127      make use of the "named return value" extension that G++ used to
20128      support.  */
20129   token = cp_lexer_peek_token (parser->lexer);
20130   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20131     {
20132       /* Consume the `return' keyword.  */
20133       cp_lexer_consume_token (parser->lexer);
20134       /* Look for the identifier that indicates what value is to be
20135          returned.  */
20136       cp_parser_identifier (parser);
20137       /* Issue an error message.  */
20138       error_at (token->location,
20139                 "named return values are no longer supported");
20140       /* Skip tokens until we reach the start of the function body.  */
20141       while (true)
20142         {
20143           cp_token *token = cp_lexer_peek_token (parser->lexer);
20144           if (token->type == CPP_OPEN_BRACE
20145               || token->type == CPP_EOF
20146               || token->type == CPP_PRAGMA_EOL)
20147             break;
20148           cp_lexer_consume_token (parser->lexer);
20149         }
20150     }
20151   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20152      anything declared inside `f'.  */
20153   saved_in_unbraced_linkage_specification_p
20154     = parser->in_unbraced_linkage_specification_p;
20155   parser->in_unbraced_linkage_specification_p = false;
20156   /* Inside the function, surrounding template-parameter-lists do not
20157      apply.  */
20158   saved_num_template_parameter_lists
20159     = parser->num_template_parameter_lists;
20160   parser->num_template_parameter_lists = 0;
20161
20162   start_lambda_scope (current_function_decl);
20163
20164   /* If the next token is `try', then we are looking at a
20165      function-try-block.  */
20166   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20167     ctor_initializer_p = cp_parser_function_try_block (parser);
20168   /* A function-try-block includes the function-body, so we only do
20169      this next part if we're not processing a function-try-block.  */
20170   else
20171     ctor_initializer_p
20172       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20173
20174   finish_lambda_scope ();
20175
20176   /* Finish the function.  */
20177   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20178                         (inline_p ? 2 : 0));
20179   /* Generate code for it, if necessary.  */
20180   expand_or_defer_fn (fn);
20181   /* Restore the saved values.  */
20182   parser->in_unbraced_linkage_specification_p
20183     = saved_in_unbraced_linkage_specification_p;
20184   parser->num_template_parameter_lists
20185     = saved_num_template_parameter_lists;
20186   parser->in_function_body = saved_in_function_body;
20187
20188   return fn;
20189 }
20190
20191 /* Parse a template-declaration, assuming that the `export' (and
20192    `extern') keywords, if present, has already been scanned.  MEMBER_P
20193    is as for cp_parser_template_declaration.  */
20194
20195 static void
20196 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20197 {
20198   tree decl = NULL_TREE;
20199   VEC (deferred_access_check,gc) *checks;
20200   tree parameter_list;
20201   bool friend_p = false;
20202   bool need_lang_pop;
20203   cp_token *token;
20204
20205   /* Look for the `template' keyword.  */
20206   token = cp_lexer_peek_token (parser->lexer);
20207   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20208     return;
20209
20210   /* And the `<'.  */
20211   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20212     return;
20213   if (at_class_scope_p () && current_function_decl)
20214     {
20215       /* 14.5.2.2 [temp.mem]
20216
20217          A local class shall not have member templates.  */
20218       error_at (token->location,
20219                 "invalid declaration of member template in local class");
20220       cp_parser_skip_to_end_of_block_or_statement (parser);
20221       return;
20222     }
20223   /* [temp]
20224
20225      A template ... shall not have C linkage.  */
20226   if (current_lang_name == lang_name_c)
20227     {
20228       error_at (token->location, "template with C linkage");
20229       /* Give it C++ linkage to avoid confusing other parts of the
20230          front end.  */
20231       push_lang_context (lang_name_cplusplus);
20232       need_lang_pop = true;
20233     }
20234   else
20235     need_lang_pop = false;
20236
20237   /* We cannot perform access checks on the template parameter
20238      declarations until we know what is being declared, just as we
20239      cannot check the decl-specifier list.  */
20240   push_deferring_access_checks (dk_deferred);
20241
20242   /* If the next token is `>', then we have an invalid
20243      specialization.  Rather than complain about an invalid template
20244      parameter, issue an error message here.  */
20245   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20246     {
20247       cp_parser_error (parser, "invalid explicit specialization");
20248       begin_specialization ();
20249       parameter_list = NULL_TREE;
20250     }
20251   else
20252     {
20253       /* Parse the template parameters.  */
20254       parameter_list = cp_parser_template_parameter_list (parser);
20255       fixup_template_parms ();
20256     }
20257
20258   /* Get the deferred access checks from the parameter list.  These
20259      will be checked once we know what is being declared, as for a
20260      member template the checks must be performed in the scope of the
20261      class containing the member.  */
20262   checks = get_deferred_access_checks ();
20263
20264   /* Look for the `>'.  */
20265   cp_parser_skip_to_end_of_template_parameter_list (parser);
20266   /* We just processed one more parameter list.  */
20267   ++parser->num_template_parameter_lists;
20268   /* If the next token is `template', there are more template
20269      parameters.  */
20270   if (cp_lexer_next_token_is_keyword (parser->lexer,
20271                                       RID_TEMPLATE))
20272     cp_parser_template_declaration_after_export (parser, member_p);
20273   else
20274     {
20275       /* There are no access checks when parsing a template, as we do not
20276          know if a specialization will be a friend.  */
20277       push_deferring_access_checks (dk_no_check);
20278       token = cp_lexer_peek_token (parser->lexer);
20279       decl = cp_parser_single_declaration (parser,
20280                                            checks,
20281                                            member_p,
20282                                            /*explicit_specialization_p=*/false,
20283                                            &friend_p);
20284       pop_deferring_access_checks ();
20285
20286       /* If this is a member template declaration, let the front
20287          end know.  */
20288       if (member_p && !friend_p && decl)
20289         {
20290           if (TREE_CODE (decl) == TYPE_DECL)
20291             cp_parser_check_access_in_redeclaration (decl, token->location);
20292
20293           decl = finish_member_template_decl (decl);
20294         }
20295       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20296         make_friend_class (current_class_type, TREE_TYPE (decl),
20297                            /*complain=*/true);
20298     }
20299   /* We are done with the current parameter list.  */
20300   --parser->num_template_parameter_lists;
20301
20302   pop_deferring_access_checks ();
20303
20304   /* Finish up.  */
20305   finish_template_decl (parameter_list);
20306
20307   /* Register member declarations.  */
20308   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20309     finish_member_declaration (decl);
20310   /* For the erroneous case of a template with C linkage, we pushed an
20311      implicit C++ linkage scope; exit that scope now.  */
20312   if (need_lang_pop)
20313     pop_lang_context ();
20314   /* If DECL is a function template, we must return to parse it later.
20315      (Even though there is no definition, there might be default
20316      arguments that need handling.)  */
20317   if (member_p && decl
20318       && (TREE_CODE (decl) == FUNCTION_DECL
20319           || DECL_FUNCTION_TEMPLATE_P (decl)))
20320     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20321 }
20322
20323 /* Perform the deferred access checks from a template-parameter-list.
20324    CHECKS is a TREE_LIST of access checks, as returned by
20325    get_deferred_access_checks.  */
20326
20327 static void
20328 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20329 {
20330   ++processing_template_parmlist;
20331   perform_access_checks (checks);
20332   --processing_template_parmlist;
20333 }
20334
20335 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20336    `function-definition' sequence.  MEMBER_P is true, this declaration
20337    appears in a class scope.
20338
20339    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20340    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20341
20342 static tree
20343 cp_parser_single_declaration (cp_parser* parser,
20344                               VEC (deferred_access_check,gc)* checks,
20345                               bool member_p,
20346                               bool explicit_specialization_p,
20347                               bool* friend_p)
20348 {
20349   int declares_class_or_enum;
20350   tree decl = NULL_TREE;
20351   cp_decl_specifier_seq decl_specifiers;
20352   bool function_definition_p = false;
20353   cp_token *decl_spec_token_start;
20354
20355   /* This function is only used when processing a template
20356      declaration.  */
20357   gcc_assert (innermost_scope_kind () == sk_template_parms
20358               || innermost_scope_kind () == sk_template_spec);
20359
20360   /* Defer access checks until we know what is being declared.  */
20361   push_deferring_access_checks (dk_deferred);
20362
20363   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20364      alternative.  */
20365   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20366   cp_parser_decl_specifier_seq (parser,
20367                                 CP_PARSER_FLAGS_OPTIONAL,
20368                                 &decl_specifiers,
20369                                 &declares_class_or_enum);
20370   if (friend_p)
20371     *friend_p = cp_parser_friend_p (&decl_specifiers);
20372
20373   /* There are no template typedefs.  */
20374   if (decl_specifiers.specs[(int) ds_typedef])
20375     {
20376       error_at (decl_spec_token_start->location,
20377                 "template declaration of %<typedef%>");
20378       decl = error_mark_node;
20379     }
20380
20381   /* Gather up the access checks that occurred the
20382      decl-specifier-seq.  */
20383   stop_deferring_access_checks ();
20384
20385   /* Check for the declaration of a template class.  */
20386   if (declares_class_or_enum)
20387     {
20388       if (cp_parser_declares_only_class_p (parser))
20389         {
20390           decl = shadow_tag (&decl_specifiers);
20391
20392           /* In this case:
20393
20394                struct C {
20395                  friend template <typename T> struct A<T>::B;
20396                };
20397
20398              A<T>::B will be represented by a TYPENAME_TYPE, and
20399              therefore not recognized by shadow_tag.  */
20400           if (friend_p && *friend_p
20401               && !decl
20402               && decl_specifiers.type
20403               && TYPE_P (decl_specifiers.type))
20404             decl = decl_specifiers.type;
20405
20406           if (decl && decl != error_mark_node)
20407             decl = TYPE_NAME (decl);
20408           else
20409             decl = error_mark_node;
20410
20411           /* Perform access checks for template parameters.  */
20412           cp_parser_perform_template_parameter_access_checks (checks);
20413         }
20414     }
20415
20416   /* Complain about missing 'typename' or other invalid type names.  */
20417   if (!decl_specifiers.any_type_specifiers_p
20418       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20419     {
20420       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20421          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20422          the rest of this declaration.  */
20423       decl = error_mark_node;
20424       goto out;
20425     }
20426
20427   /* If it's not a template class, try for a template function.  If
20428      the next token is a `;', then this declaration does not declare
20429      anything.  But, if there were errors in the decl-specifiers, then
20430      the error might well have come from an attempted class-specifier.
20431      In that case, there's no need to warn about a missing declarator.  */
20432   if (!decl
20433       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20434           || decl_specifiers.type != error_mark_node))
20435     {
20436       decl = cp_parser_init_declarator (parser,
20437                                         &decl_specifiers,
20438                                         checks,
20439                                         /*function_definition_allowed_p=*/true,
20440                                         member_p,
20441                                         declares_class_or_enum,
20442                                         &function_definition_p,
20443                                         NULL);
20444
20445     /* 7.1.1-1 [dcl.stc]
20446
20447        A storage-class-specifier shall not be specified in an explicit
20448        specialization...  */
20449     if (decl
20450         && explicit_specialization_p
20451         && decl_specifiers.storage_class != sc_none)
20452       {
20453         error_at (decl_spec_token_start->location,
20454                   "explicit template specialization cannot have a storage class");
20455         decl = error_mark_node;
20456       }
20457     }
20458
20459   /* Look for a trailing `;' after the declaration.  */
20460   if (!function_definition_p
20461       && (decl == error_mark_node
20462           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20463     cp_parser_skip_to_end_of_block_or_statement (parser);
20464
20465  out:
20466   pop_deferring_access_checks ();
20467
20468   /* Clear any current qualification; whatever comes next is the start
20469      of something new.  */
20470   parser->scope = NULL_TREE;
20471   parser->qualifying_scope = NULL_TREE;
20472   parser->object_scope = NULL_TREE;
20473
20474   return decl;
20475 }
20476
20477 /* Parse a cast-expression that is not the operand of a unary "&".  */
20478
20479 static tree
20480 cp_parser_simple_cast_expression (cp_parser *parser)
20481 {
20482   return cp_parser_cast_expression (parser, /*address_p=*/false,
20483                                     /*cast_p=*/false, NULL);
20484 }
20485
20486 /* Parse a functional cast to TYPE.  Returns an expression
20487    representing the cast.  */
20488
20489 static tree
20490 cp_parser_functional_cast (cp_parser* parser, tree type)
20491 {
20492   VEC(tree,gc) *vec;
20493   tree expression_list;
20494   tree cast;
20495   bool nonconst_p;
20496
20497   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20498     {
20499       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20500       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20501       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20502       if (TREE_CODE (type) == TYPE_DECL)
20503         type = TREE_TYPE (type);
20504       return finish_compound_literal (type, expression_list,
20505                                       tf_warning_or_error);
20506     }
20507
20508
20509   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20510                                                  /*cast_p=*/true,
20511                                                  /*allow_expansion_p=*/true,
20512                                                  /*non_constant_p=*/NULL);
20513   if (vec == NULL)
20514     expression_list = error_mark_node;
20515   else
20516     {
20517       expression_list = build_tree_list_vec (vec);
20518       release_tree_vector (vec);
20519     }
20520
20521   cast = build_functional_cast (type, expression_list,
20522                                 tf_warning_or_error);
20523   /* [expr.const]/1: In an integral constant expression "only type
20524      conversions to integral or enumeration type can be used".  */
20525   if (TREE_CODE (type) == TYPE_DECL)
20526     type = TREE_TYPE (type);
20527   if (cast != error_mark_node
20528       && !cast_valid_in_integral_constant_expression_p (type)
20529       && cp_parser_non_integral_constant_expression (parser,
20530                                                      NIC_CONSTRUCTOR))
20531     return error_mark_node;
20532   return cast;
20533 }
20534
20535 /* Save the tokens that make up the body of a member function defined
20536    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20537    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20538    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20539    for the member function.  */
20540
20541 static tree
20542 cp_parser_save_member_function_body (cp_parser* parser,
20543                                      cp_decl_specifier_seq *decl_specifiers,
20544                                      cp_declarator *declarator,
20545                                      tree attributes)
20546 {
20547   cp_token *first;
20548   cp_token *last;
20549   tree fn;
20550
20551   /* Create the FUNCTION_DECL.  */
20552   fn = grokmethod (decl_specifiers, declarator, attributes);
20553   /* If something went badly wrong, bail out now.  */
20554   if (fn == error_mark_node)
20555     {
20556       /* If there's a function-body, skip it.  */
20557       if (cp_parser_token_starts_function_definition_p
20558           (cp_lexer_peek_token (parser->lexer)))
20559         cp_parser_skip_to_end_of_block_or_statement (parser);
20560       return error_mark_node;
20561     }
20562
20563   /* Remember it, if there default args to post process.  */
20564   cp_parser_save_default_args (parser, fn);
20565
20566   /* Save away the tokens that make up the body of the
20567      function.  */
20568   first = parser->lexer->next_token;
20569   /* We can have braced-init-list mem-initializers before the fn body.  */
20570   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20571     {
20572       cp_lexer_consume_token (parser->lexer);
20573       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20574              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20575         {
20576           /* cache_group will stop after an un-nested { } pair, too.  */
20577           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20578             break;
20579
20580           /* variadic mem-inits have ... after the ')'.  */
20581           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20582             cp_lexer_consume_token (parser->lexer);
20583         }
20584     }
20585   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20586   /* Handle function try blocks.  */
20587   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20588     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20589   last = parser->lexer->next_token;
20590
20591   /* Save away the inline definition; we will process it when the
20592      class is complete.  */
20593   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20594   DECL_PENDING_INLINE_P (fn) = 1;
20595
20596   /* We need to know that this was defined in the class, so that
20597      friend templates are handled correctly.  */
20598   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20599
20600   /* Add FN to the queue of functions to be parsed later.  */
20601   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20602
20603   return fn;
20604 }
20605
20606 /* Save the tokens that make up the in-class initializer for a non-static
20607    data member.  Returns a DEFAULT_ARG.  */
20608
20609 static tree
20610 cp_parser_save_nsdmi (cp_parser* parser)
20611 {
20612   /* Save away the tokens that make up the body of the
20613      function.  */
20614   cp_token *first = parser->lexer->next_token;
20615   cp_token *last;
20616   tree node;
20617
20618   cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
20619
20620   last = parser->lexer->next_token;
20621
20622   node = make_node (DEFAULT_ARG);
20623   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
20624   DEFARG_INSTANTIATIONS (node) = NULL;
20625
20626   return node;
20627 }
20628
20629
20630 /* Parse a template-argument-list, as well as the trailing ">" (but
20631    not the opening ">").  See cp_parser_template_argument_list for the
20632    return value.  */
20633
20634 static tree
20635 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20636 {
20637   tree arguments;
20638   tree saved_scope;
20639   tree saved_qualifying_scope;
20640   tree saved_object_scope;
20641   bool saved_greater_than_is_operator_p;
20642   int saved_unevaluated_operand;
20643   int saved_inhibit_evaluation_warnings;
20644
20645   /* [temp.names]
20646
20647      When parsing a template-id, the first non-nested `>' is taken as
20648      the end of the template-argument-list rather than a greater-than
20649      operator.  */
20650   saved_greater_than_is_operator_p
20651     = parser->greater_than_is_operator_p;
20652   parser->greater_than_is_operator_p = false;
20653   /* Parsing the argument list may modify SCOPE, so we save it
20654      here.  */
20655   saved_scope = parser->scope;
20656   saved_qualifying_scope = parser->qualifying_scope;
20657   saved_object_scope = parser->object_scope;
20658   /* We need to evaluate the template arguments, even though this
20659      template-id may be nested within a "sizeof".  */
20660   saved_unevaluated_operand = cp_unevaluated_operand;
20661   cp_unevaluated_operand = 0;
20662   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20663   c_inhibit_evaluation_warnings = 0;
20664   /* Parse the template-argument-list itself.  */
20665   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20666       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20667     arguments = NULL_TREE;
20668   else
20669     arguments = cp_parser_template_argument_list (parser);
20670   /* Look for the `>' that ends the template-argument-list. If we find
20671      a '>>' instead, it's probably just a typo.  */
20672   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20673     {
20674       if (cxx_dialect != cxx98)
20675         {
20676           /* In C++0x, a `>>' in a template argument list or cast
20677              expression is considered to be two separate `>'
20678              tokens. So, change the current token to a `>', but don't
20679              consume it: it will be consumed later when the outer
20680              template argument list (or cast expression) is parsed.
20681              Note that this replacement of `>' for `>>' is necessary
20682              even if we are parsing tentatively: in the tentative
20683              case, after calling
20684              cp_parser_enclosed_template_argument_list we will always
20685              throw away all of the template arguments and the first
20686              closing `>', either because the template argument list
20687              was erroneous or because we are replacing those tokens
20688              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20689              not have been thrown away) is needed either to close an
20690              outer template argument list or to complete a new-style
20691              cast.  */
20692           cp_token *token = cp_lexer_peek_token (parser->lexer);
20693           token->type = CPP_GREATER;
20694         }
20695       else if (!saved_greater_than_is_operator_p)
20696         {
20697           /* If we're in a nested template argument list, the '>>' has
20698             to be a typo for '> >'. We emit the error message, but we
20699             continue parsing and we push a '>' as next token, so that
20700             the argument list will be parsed correctly.  Note that the
20701             global source location is still on the token before the
20702             '>>', so we need to say explicitly where we want it.  */
20703           cp_token *token = cp_lexer_peek_token (parser->lexer);
20704           error_at (token->location, "%<>>%> should be %<> >%> "
20705                     "within a nested template argument list");
20706
20707           token->type = CPP_GREATER;
20708         }
20709       else
20710         {
20711           /* If this is not a nested template argument list, the '>>'
20712             is a typo for '>'. Emit an error message and continue.
20713             Same deal about the token location, but here we can get it
20714             right by consuming the '>>' before issuing the diagnostic.  */
20715           cp_token *token = cp_lexer_consume_token (parser->lexer);
20716           error_at (token->location,
20717                     "spurious %<>>%>, use %<>%> to terminate "
20718                     "a template argument list");
20719         }
20720     }
20721   else
20722     cp_parser_skip_to_end_of_template_parameter_list (parser);
20723   /* The `>' token might be a greater-than operator again now.  */
20724   parser->greater_than_is_operator_p
20725     = saved_greater_than_is_operator_p;
20726   /* Restore the SAVED_SCOPE.  */
20727   parser->scope = saved_scope;
20728   parser->qualifying_scope = saved_qualifying_scope;
20729   parser->object_scope = saved_object_scope;
20730   cp_unevaluated_operand = saved_unevaluated_operand;
20731   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20732
20733   return arguments;
20734 }
20735
20736 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20737    arguments, or the body of the function have not yet been parsed,
20738    parse them now.  */
20739
20740 static void
20741 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20742 {
20743   timevar_push (TV_PARSE_INMETH);
20744   /* If this member is a template, get the underlying
20745      FUNCTION_DECL.  */
20746   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20747     member_function = DECL_TEMPLATE_RESULT (member_function);
20748
20749   /* There should not be any class definitions in progress at this
20750      point; the bodies of members are only parsed outside of all class
20751      definitions.  */
20752   gcc_assert (parser->num_classes_being_defined == 0);
20753   /* While we're parsing the member functions we might encounter more
20754      classes.  We want to handle them right away, but we don't want
20755      them getting mixed up with functions that are currently in the
20756      queue.  */
20757   push_unparsed_function_queues (parser);
20758
20759   /* Make sure that any template parameters are in scope.  */
20760   maybe_begin_member_template_processing (member_function);
20761
20762   /* If the body of the function has not yet been parsed, parse it
20763      now.  */
20764   if (DECL_PENDING_INLINE_P (member_function))
20765     {
20766       tree function_scope;
20767       cp_token_cache *tokens;
20768
20769       /* The function is no longer pending; we are processing it.  */
20770       tokens = DECL_PENDING_INLINE_INFO (member_function);
20771       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20772       DECL_PENDING_INLINE_P (member_function) = 0;
20773
20774       /* If this is a local class, enter the scope of the containing
20775          function.  */
20776       function_scope = current_function_decl;
20777       if (function_scope)
20778         push_function_context ();
20779
20780       /* Push the body of the function onto the lexer stack.  */
20781       cp_parser_push_lexer_for_tokens (parser, tokens);
20782
20783       /* Let the front end know that we going to be defining this
20784          function.  */
20785       start_preparsed_function (member_function, NULL_TREE,
20786                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20787
20788       /* Don't do access checking if it is a templated function.  */
20789       if (processing_template_decl)
20790         push_deferring_access_checks (dk_no_check);
20791
20792       /* Now, parse the body of the function.  */
20793       cp_parser_function_definition_after_declarator (parser,
20794                                                       /*inline_p=*/true);
20795
20796       if (processing_template_decl)
20797         pop_deferring_access_checks ();
20798
20799       /* Leave the scope of the containing function.  */
20800       if (function_scope)
20801         pop_function_context ();
20802       cp_parser_pop_lexer (parser);
20803     }
20804
20805   /* Remove any template parameters from the symbol table.  */
20806   maybe_end_member_template_processing ();
20807
20808   /* Restore the queue.  */
20809   pop_unparsed_function_queues (parser);
20810   timevar_pop (TV_PARSE_INMETH);
20811 }
20812
20813 /* If DECL contains any default args, remember it on the unparsed
20814    functions queue.  */
20815
20816 static void
20817 cp_parser_save_default_args (cp_parser* parser, tree decl)
20818 {
20819   tree probe;
20820
20821   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20822        probe;
20823        probe = TREE_CHAIN (probe))
20824     if (TREE_PURPOSE (probe))
20825       {
20826         cp_default_arg_entry *entry
20827           = VEC_safe_push (cp_default_arg_entry, gc,
20828                            unparsed_funs_with_default_args, NULL);
20829         entry->class_type = current_class_type;
20830         entry->decl = decl;
20831         break;
20832       }
20833 }
20834
20835 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
20836    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
20837    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
20838    from the parameter-type-list.  */
20839
20840 static tree
20841 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
20842                                       tree default_arg, tree parmtype)
20843 {
20844   cp_token_cache *tokens;
20845   tree parsed_arg;
20846   bool dummy;
20847
20848   /* Push the saved tokens for the default argument onto the parser's
20849      lexer stack.  */
20850   tokens = DEFARG_TOKENS (default_arg);
20851   cp_parser_push_lexer_for_tokens (parser, tokens);
20852
20853   start_lambda_scope (decl);
20854
20855   /* Parse the default argument.  */
20856   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
20857   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
20858     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20859
20860   finish_lambda_scope ();
20861
20862   if (!processing_template_decl)
20863     {
20864       /* In a non-template class, check conversions now.  In a template,
20865          we'll wait and instantiate these as needed.  */
20866       if (TREE_CODE (decl) == PARM_DECL)
20867         parsed_arg = check_default_argument (parmtype, parsed_arg);
20868       else
20869         {
20870           int flags = LOOKUP_IMPLICIT;
20871           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
20872               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
20873             flags = LOOKUP_NORMAL;
20874           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
20875         }
20876     }
20877
20878   /* If the token stream has not been completely used up, then
20879      there was extra junk after the end of the default
20880      argument.  */
20881   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20882     {
20883       if (TREE_CODE (decl) == PARM_DECL)
20884         cp_parser_error (parser, "expected %<,%>");
20885       else
20886         cp_parser_error (parser, "expected %<;%>");
20887     }
20888
20889   /* Revert to the main lexer.  */
20890   cp_parser_pop_lexer (parser);
20891
20892   return parsed_arg;
20893 }
20894
20895 /* FIELD is a non-static data member with an initializer which we saved for
20896    later; parse it now.  */
20897
20898 static void
20899 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
20900 {
20901   tree def;
20902
20903   push_unparsed_function_queues (parser);
20904   def = cp_parser_late_parse_one_default_arg (parser, field,
20905                                               DECL_INITIAL (field),
20906                                               NULL_TREE);
20907   pop_unparsed_function_queues (parser);
20908
20909   DECL_INITIAL (field) = def;
20910 }
20911
20912 /* FN is a FUNCTION_DECL which may contains a parameter with an
20913    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20914    assumes that the current scope is the scope in which the default
20915    argument should be processed.  */
20916
20917 static void
20918 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20919 {
20920   bool saved_local_variables_forbidden_p;
20921   tree parm, parmdecl;
20922
20923   /* While we're parsing the default args, we might (due to the
20924      statement expression extension) encounter more classes.  We want
20925      to handle them right away, but we don't want them getting mixed
20926      up with default args that are currently in the queue.  */
20927   push_unparsed_function_queues (parser);
20928
20929   /* Local variable names (and the `this' keyword) may not appear
20930      in a default argument.  */
20931   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20932   parser->local_variables_forbidden_p = true;
20933
20934   push_defarg_context (fn);
20935
20936   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20937          parmdecl = DECL_ARGUMENTS (fn);
20938        parm && parm != void_list_node;
20939        parm = TREE_CHAIN (parm),
20940          parmdecl = DECL_CHAIN (parmdecl))
20941     {
20942       tree default_arg = TREE_PURPOSE (parm);
20943       tree parsed_arg;
20944       VEC(tree,gc) *insts;
20945       tree copy;
20946       unsigned ix;
20947
20948       if (!default_arg)
20949         continue;
20950
20951       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20952         /* This can happen for a friend declaration for a function
20953            already declared with default arguments.  */
20954         continue;
20955
20956       parsed_arg
20957         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
20958                                                 default_arg,
20959                                                 TREE_VALUE (parm));
20960       if (parsed_arg == error_mark_node)
20961         {
20962           continue;
20963         }
20964
20965       TREE_PURPOSE (parm) = parsed_arg;
20966
20967       /* Update any instantiations we've already created.  */
20968       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20969            VEC_iterate (tree, insts, ix, copy); ix++)
20970         TREE_PURPOSE (copy) = parsed_arg;
20971     }
20972
20973   pop_defarg_context ();
20974
20975   /* Make sure no default arg is missing.  */
20976   check_default_args (fn);
20977
20978   /* Restore the state of local_variables_forbidden_p.  */
20979   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20980
20981   /* Restore the queue.  */
20982   pop_unparsed_function_queues (parser);
20983 }
20984
20985 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20986    either a TYPE or an expression, depending on the form of the
20987    input.  The KEYWORD indicates which kind of expression we have
20988    encountered.  */
20989
20990 static tree
20991 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20992 {
20993   tree expr = NULL_TREE;
20994   const char *saved_message;
20995   char *tmp;
20996   bool saved_integral_constant_expression_p;
20997   bool saved_non_integral_constant_expression_p;
20998   bool pack_expansion_p = false;
20999
21000   /* Types cannot be defined in a `sizeof' expression.  Save away the
21001      old message.  */
21002   saved_message = parser->type_definition_forbidden_message;
21003   /* And create the new one.  */
21004   tmp = concat ("types may not be defined in %<",
21005                 IDENTIFIER_POINTER (ridpointers[keyword]),
21006                 "%> expressions", NULL);
21007   parser->type_definition_forbidden_message = tmp;
21008
21009   /* The restrictions on constant-expressions do not apply inside
21010      sizeof expressions.  */
21011   saved_integral_constant_expression_p
21012     = parser->integral_constant_expression_p;
21013   saved_non_integral_constant_expression_p
21014     = parser->non_integral_constant_expression_p;
21015   parser->integral_constant_expression_p = false;
21016
21017   /* If it's a `...', then we are computing the length of a parameter
21018      pack.  */
21019   if (keyword == RID_SIZEOF
21020       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21021     {
21022       /* Consume the `...'.  */
21023       cp_lexer_consume_token (parser->lexer);
21024       maybe_warn_variadic_templates ();
21025
21026       /* Note that this is an expansion.  */
21027       pack_expansion_p = true;
21028     }
21029
21030   /* Do not actually evaluate the expression.  */
21031   ++cp_unevaluated_operand;
21032   ++c_inhibit_evaluation_warnings;
21033   /* If it's a `(', then we might be looking at the type-id
21034      construction.  */
21035   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21036     {
21037       tree type;
21038       bool saved_in_type_id_in_expr_p;
21039
21040       /* We can't be sure yet whether we're looking at a type-id or an
21041          expression.  */
21042       cp_parser_parse_tentatively (parser);
21043       /* Consume the `('.  */
21044       cp_lexer_consume_token (parser->lexer);
21045       /* Parse the type-id.  */
21046       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21047       parser->in_type_id_in_expr_p = true;
21048       type = cp_parser_type_id (parser);
21049       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21050       /* Now, look for the trailing `)'.  */
21051       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21052       /* If all went well, then we're done.  */
21053       if (cp_parser_parse_definitely (parser))
21054         {
21055           cp_decl_specifier_seq decl_specs;
21056
21057           /* Build a trivial decl-specifier-seq.  */
21058           clear_decl_specs (&decl_specs);
21059           decl_specs.type = type;
21060
21061           /* Call grokdeclarator to figure out what type this is.  */
21062           expr = grokdeclarator (NULL,
21063                                  &decl_specs,
21064                                  TYPENAME,
21065                                  /*initialized=*/0,
21066                                  /*attrlist=*/NULL);
21067         }
21068     }
21069
21070   /* If the type-id production did not work out, then we must be
21071      looking at the unary-expression production.  */
21072   if (!expr)
21073     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21074                                        /*cast_p=*/false, NULL);
21075
21076   if (pack_expansion_p)
21077     /* Build a pack expansion. */
21078     expr = make_pack_expansion (expr);
21079
21080   /* Go back to evaluating expressions.  */
21081   --cp_unevaluated_operand;
21082   --c_inhibit_evaluation_warnings;
21083
21084   /* Free the message we created.  */
21085   free (tmp);
21086   /* And restore the old one.  */
21087   parser->type_definition_forbidden_message = saved_message;
21088   parser->integral_constant_expression_p
21089     = saved_integral_constant_expression_p;
21090   parser->non_integral_constant_expression_p
21091     = saved_non_integral_constant_expression_p;
21092
21093   return expr;
21094 }
21095
21096 /* If the current declaration has no declarator, return true.  */
21097
21098 static bool
21099 cp_parser_declares_only_class_p (cp_parser *parser)
21100 {
21101   /* If the next token is a `;' or a `,' then there is no
21102      declarator.  */
21103   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21104           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21105 }
21106
21107 /* Update the DECL_SPECS to reflect the storage class indicated by
21108    KEYWORD.  */
21109
21110 static void
21111 cp_parser_set_storage_class (cp_parser *parser,
21112                              cp_decl_specifier_seq *decl_specs,
21113                              enum rid keyword,
21114                              location_t location)
21115 {
21116   cp_storage_class storage_class;
21117
21118   if (parser->in_unbraced_linkage_specification_p)
21119     {
21120       error_at (location, "invalid use of %qD in linkage specification",
21121                 ridpointers[keyword]);
21122       return;
21123     }
21124   else if (decl_specs->storage_class != sc_none)
21125     {
21126       decl_specs->conflicting_specifiers_p = true;
21127       return;
21128     }
21129
21130   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21131       && decl_specs->specs[(int) ds_thread])
21132     {
21133       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21134       decl_specs->specs[(int) ds_thread] = 0;
21135     }
21136
21137   switch (keyword)
21138     {
21139     case RID_AUTO:
21140       storage_class = sc_auto;
21141       break;
21142     case RID_REGISTER:
21143       storage_class = sc_register;
21144       break;
21145     case RID_STATIC:
21146       storage_class = sc_static;
21147       break;
21148     case RID_EXTERN:
21149       storage_class = sc_extern;
21150       break;
21151     case RID_MUTABLE:
21152       storage_class = sc_mutable;
21153       break;
21154     default:
21155       gcc_unreachable ();
21156     }
21157   decl_specs->storage_class = storage_class;
21158
21159   /* A storage class specifier cannot be applied alongside a typedef 
21160      specifier. If there is a typedef specifier present then set 
21161      conflicting_specifiers_p which will trigger an error later
21162      on in grokdeclarator. */
21163   if (decl_specs->specs[(int)ds_typedef])
21164     decl_specs->conflicting_specifiers_p = true;
21165 }
21166
21167 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
21168    is true, the type is a class or enum definition.  */
21169
21170 static void
21171 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21172                               tree type_spec,
21173                               location_t location,
21174                               bool type_definition_p)
21175 {
21176   decl_specs->any_specifiers_p = true;
21177
21178   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21179      (with, for example, in "typedef int wchar_t;") we remember that
21180      this is what happened.  In system headers, we ignore these
21181      declarations so that G++ can work with system headers that are not
21182      C++-safe.  */
21183   if (decl_specs->specs[(int) ds_typedef]
21184       && !type_definition_p
21185       && (type_spec == boolean_type_node
21186           || type_spec == char16_type_node
21187           || type_spec == char32_type_node
21188           || type_spec == wchar_type_node)
21189       && (decl_specs->type
21190           || decl_specs->specs[(int) ds_long]
21191           || decl_specs->specs[(int) ds_short]
21192           || decl_specs->specs[(int) ds_unsigned]
21193           || decl_specs->specs[(int) ds_signed]))
21194     {
21195       decl_specs->redefined_builtin_type = type_spec;
21196       if (!decl_specs->type)
21197         {
21198           decl_specs->type = type_spec;
21199           decl_specs->type_definition_p = false;
21200           decl_specs->type_location = location;
21201         }
21202     }
21203   else if (decl_specs->type)
21204     decl_specs->multiple_types_p = true;
21205   else
21206     {
21207       decl_specs->type = type_spec;
21208       decl_specs->type_definition_p = type_definition_p;
21209       decl_specs->redefined_builtin_type = NULL_TREE;
21210       decl_specs->type_location = location;
21211     }
21212 }
21213
21214 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21215    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
21216
21217 static bool
21218 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21219 {
21220   return decl_specifiers->specs[(int) ds_friend] != 0;
21221 }
21222
21223 /* Issue an error message indicating that TOKEN_DESC was expected.
21224    If KEYWORD is true, it indicated this function is called by
21225    cp_parser_require_keword and the required token can only be
21226    a indicated keyword. */
21227
21228 static void
21229 cp_parser_required_error (cp_parser *parser,
21230                           required_token token_desc,
21231                           bool keyword)
21232 {
21233   switch (token_desc)
21234     {
21235       case RT_NEW:
21236         cp_parser_error (parser, "expected %<new%>");
21237         return;
21238       case RT_DELETE:
21239         cp_parser_error (parser, "expected %<delete%>");
21240         return;
21241       case RT_RETURN:
21242         cp_parser_error (parser, "expected %<return%>");
21243         return;
21244       case RT_WHILE:
21245         cp_parser_error (parser, "expected %<while%>");
21246         return;
21247       case RT_EXTERN:
21248         cp_parser_error (parser, "expected %<extern%>");
21249         return;
21250       case RT_STATIC_ASSERT:
21251         cp_parser_error (parser, "expected %<static_assert%>");
21252         return;
21253       case RT_DECLTYPE:
21254         cp_parser_error (parser, "expected %<decltype%>");
21255         return;
21256       case RT_OPERATOR:
21257         cp_parser_error (parser, "expected %<operator%>");
21258         return;
21259       case RT_CLASS:
21260         cp_parser_error (parser, "expected %<class%>");
21261         return;
21262       case RT_TEMPLATE:
21263         cp_parser_error (parser, "expected %<template%>");
21264         return;
21265       case RT_NAMESPACE:
21266         cp_parser_error (parser, "expected %<namespace%>");
21267         return;
21268       case RT_USING:
21269         cp_parser_error (parser, "expected %<using%>");
21270         return;
21271       case RT_ASM:
21272         cp_parser_error (parser, "expected %<asm%>");
21273         return;
21274       case RT_TRY:
21275         cp_parser_error (parser, "expected %<try%>");
21276         return;
21277       case RT_CATCH:
21278         cp_parser_error (parser, "expected %<catch%>");
21279         return;
21280       case RT_THROW:
21281         cp_parser_error (parser, "expected %<throw%>");
21282         return;
21283       case RT_LABEL:
21284         cp_parser_error (parser, "expected %<__label__%>");
21285         return;
21286       case RT_AT_TRY:
21287         cp_parser_error (parser, "expected %<@try%>");
21288         return;
21289       case RT_AT_SYNCHRONIZED:
21290         cp_parser_error (parser, "expected %<@synchronized%>");
21291         return;
21292       case RT_AT_THROW:
21293         cp_parser_error (parser, "expected %<@throw%>");
21294         return;
21295       default:
21296         break;
21297     }
21298   if (!keyword)
21299     {
21300       switch (token_desc)
21301         {
21302           case RT_SEMICOLON:
21303             cp_parser_error (parser, "expected %<;%>");
21304             return;
21305           case RT_OPEN_PAREN:
21306             cp_parser_error (parser, "expected %<(%>");
21307             return;
21308           case RT_CLOSE_BRACE:
21309             cp_parser_error (parser, "expected %<}%>");
21310             return;
21311           case RT_OPEN_BRACE:
21312             cp_parser_error (parser, "expected %<{%>");
21313             return;
21314           case RT_CLOSE_SQUARE:
21315             cp_parser_error (parser, "expected %<]%>");
21316             return;
21317           case RT_OPEN_SQUARE:
21318             cp_parser_error (parser, "expected %<[%>");
21319             return;
21320           case RT_COMMA:
21321             cp_parser_error (parser, "expected %<,%>");
21322             return;
21323           case RT_SCOPE:
21324             cp_parser_error (parser, "expected %<::%>");
21325             return;
21326           case RT_LESS:
21327             cp_parser_error (parser, "expected %<<%>");
21328             return;
21329           case RT_GREATER:
21330             cp_parser_error (parser, "expected %<>%>");
21331             return;
21332           case RT_EQ:
21333             cp_parser_error (parser, "expected %<=%>");
21334             return;
21335           case RT_ELLIPSIS:
21336             cp_parser_error (parser, "expected %<...%>");
21337             return;
21338           case RT_MULT:
21339             cp_parser_error (parser, "expected %<*%>");
21340             return;
21341           case RT_COMPL:
21342             cp_parser_error (parser, "expected %<~%>");
21343             return;
21344           case RT_COLON:
21345             cp_parser_error (parser, "expected %<:%>");
21346             return;
21347           case RT_COLON_SCOPE:
21348             cp_parser_error (parser, "expected %<:%> or %<::%>");
21349             return;
21350           case RT_CLOSE_PAREN:
21351             cp_parser_error (parser, "expected %<)%>");
21352             return;
21353           case RT_COMMA_CLOSE_PAREN:
21354             cp_parser_error (parser, "expected %<,%> or %<)%>");
21355             return;
21356           case RT_PRAGMA_EOL:
21357             cp_parser_error (parser, "expected end of line");
21358             return;
21359           case RT_NAME:
21360             cp_parser_error (parser, "expected identifier");
21361             return;
21362           case RT_SELECT:
21363             cp_parser_error (parser, "expected selection-statement");
21364             return;
21365           case RT_INTERATION:
21366             cp_parser_error (parser, "expected iteration-statement");
21367             return;
21368           case RT_JUMP:
21369             cp_parser_error (parser, "expected jump-statement");
21370             return;
21371           case RT_CLASS_KEY:
21372             cp_parser_error (parser, "expected class-key");
21373             return;
21374           case RT_CLASS_TYPENAME_TEMPLATE:
21375             cp_parser_error (parser,
21376                  "expected %<class%>, %<typename%>, or %<template%>");
21377             return;
21378           default:
21379             gcc_unreachable ();
21380         }
21381     }
21382   else
21383     gcc_unreachable ();
21384 }
21385
21386
21387
21388 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
21389    issue an error message indicating that TOKEN_DESC was expected.
21390
21391    Returns the token consumed, if the token had the appropriate type.
21392    Otherwise, returns NULL.  */
21393
21394 static cp_token *
21395 cp_parser_require (cp_parser* parser,
21396                    enum cpp_ttype type,
21397                    required_token token_desc)
21398 {
21399   if (cp_lexer_next_token_is (parser->lexer, type))
21400     return cp_lexer_consume_token (parser->lexer);
21401   else
21402     {
21403       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21404       if (!cp_parser_simulate_error (parser))
21405         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21406       return NULL;
21407     }
21408 }
21409
21410 /* An error message is produced if the next token is not '>'.
21411    All further tokens are skipped until the desired token is
21412    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21413
21414 static void
21415 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21416 {
21417   /* Current level of '< ... >'.  */
21418   unsigned level = 0;
21419   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21420   unsigned nesting_depth = 0;
21421
21422   /* Are we ready, yet?  If not, issue error message.  */
21423   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21424     return;
21425
21426   /* Skip tokens until the desired token is found.  */
21427   while (true)
21428     {
21429       /* Peek at the next token.  */
21430       switch (cp_lexer_peek_token (parser->lexer)->type)
21431         {
21432         case CPP_LESS:
21433           if (!nesting_depth)
21434             ++level;
21435           break;
21436
21437         case CPP_RSHIFT:
21438           if (cxx_dialect == cxx98)
21439             /* C++0x views the `>>' operator as two `>' tokens, but
21440                C++98 does not. */
21441             break;
21442           else if (!nesting_depth && level-- == 0)
21443             {
21444               /* We've hit a `>>' where the first `>' closes the
21445                  template argument list, and the second `>' is
21446                  spurious.  Just consume the `>>' and stop; we've
21447                  already produced at least one error.  */
21448               cp_lexer_consume_token (parser->lexer);
21449               return;
21450             }
21451           /* Fall through for C++0x, so we handle the second `>' in
21452              the `>>'.  */
21453
21454         case CPP_GREATER:
21455           if (!nesting_depth && level-- == 0)
21456             {
21457               /* We've reached the token we want, consume it and stop.  */
21458               cp_lexer_consume_token (parser->lexer);
21459               return;
21460             }
21461           break;
21462
21463         case CPP_OPEN_PAREN:
21464         case CPP_OPEN_SQUARE:
21465           ++nesting_depth;
21466           break;
21467
21468         case CPP_CLOSE_PAREN:
21469         case CPP_CLOSE_SQUARE:
21470           if (nesting_depth-- == 0)
21471             return;
21472           break;
21473
21474         case CPP_EOF:
21475         case CPP_PRAGMA_EOL:
21476         case CPP_SEMICOLON:
21477         case CPP_OPEN_BRACE:
21478         case CPP_CLOSE_BRACE:
21479           /* The '>' was probably forgotten, don't look further.  */
21480           return;
21481
21482         default:
21483           break;
21484         }
21485
21486       /* Consume this token.  */
21487       cp_lexer_consume_token (parser->lexer);
21488     }
21489 }
21490
21491 /* If the next token is the indicated keyword, consume it.  Otherwise,
21492    issue an error message indicating that TOKEN_DESC was expected.
21493
21494    Returns the token consumed, if the token had the appropriate type.
21495    Otherwise, returns NULL.  */
21496
21497 static cp_token *
21498 cp_parser_require_keyword (cp_parser* parser,
21499                            enum rid keyword,
21500                            required_token token_desc)
21501 {
21502   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21503
21504   if (token && token->keyword != keyword)
21505     {
21506       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21507       return NULL;
21508     }
21509
21510   return token;
21511 }
21512
21513 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21514    function-definition.  */
21515
21516 static bool
21517 cp_parser_token_starts_function_definition_p (cp_token* token)
21518 {
21519   return (/* An ordinary function-body begins with an `{'.  */
21520           token->type == CPP_OPEN_BRACE
21521           /* A ctor-initializer begins with a `:'.  */
21522           || token->type == CPP_COLON
21523           /* A function-try-block begins with `try'.  */
21524           || token->keyword == RID_TRY
21525           /* The named return value extension begins with `return'.  */
21526           || token->keyword == RID_RETURN);
21527 }
21528
21529 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21530    definition.  */
21531
21532 static bool
21533 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21534 {
21535   cp_token *token;
21536
21537   token = cp_lexer_peek_token (parser->lexer);
21538   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21539 }
21540
21541 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21542    C++0x) ending a template-argument.  */
21543
21544 static bool
21545 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21546 {
21547   cp_token *token;
21548
21549   token = cp_lexer_peek_token (parser->lexer);
21550   return (token->type == CPP_COMMA 
21551           || token->type == CPP_GREATER
21552           || token->type == CPP_ELLIPSIS
21553           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21554 }
21555
21556 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21557    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21558
21559 static bool
21560 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21561                                                      size_t n)
21562 {
21563   cp_token *token;
21564
21565   token = cp_lexer_peek_nth_token (parser->lexer, n);
21566   if (token->type == CPP_LESS)
21567     return true;
21568   /* Check for the sequence `<::' in the original code. It would be lexed as
21569      `[:', where `[' is a digraph, and there is no whitespace before
21570      `:'.  */
21571   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21572     {
21573       cp_token *token2;
21574       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21575       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21576         return true;
21577     }
21578   return false;
21579 }
21580
21581 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21582    or none_type otherwise.  */
21583
21584 static enum tag_types
21585 cp_parser_token_is_class_key (cp_token* token)
21586 {
21587   switch (token->keyword)
21588     {
21589     case RID_CLASS:
21590       return class_type;
21591     case RID_STRUCT:
21592       return record_type;
21593     case RID_UNION:
21594       return union_type;
21595
21596     default:
21597       return none_type;
21598     }
21599 }
21600
21601 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21602
21603 static void
21604 cp_parser_check_class_key (enum tag_types class_key, tree type)
21605 {
21606   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21607     permerror (input_location, "%qs tag used in naming %q#T",
21608             class_key == union_type ? "union"
21609              : class_key == record_type ? "struct" : "class",
21610              type);
21611 }
21612
21613 /* Issue an error message if DECL is redeclared with different
21614    access than its original declaration [class.access.spec/3].
21615    This applies to nested classes and nested class templates.
21616    [class.mem/1].  */
21617
21618 static void
21619 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21620 {
21621   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21622     return;
21623
21624   if ((TREE_PRIVATE (decl)
21625        != (current_access_specifier == access_private_node))
21626       || (TREE_PROTECTED (decl)
21627           != (current_access_specifier == access_protected_node)))
21628     error_at (location, "%qD redeclared with different access", decl);
21629 }
21630
21631 /* Look for the `template' keyword, as a syntactic disambiguator.
21632    Return TRUE iff it is present, in which case it will be
21633    consumed.  */
21634
21635 static bool
21636 cp_parser_optional_template_keyword (cp_parser *parser)
21637 {
21638   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21639     {
21640       /* The `template' keyword can only be used within templates;
21641          outside templates the parser can always figure out what is a
21642          template and what is not.  */
21643       if (!processing_template_decl)
21644         {
21645           cp_token *token = cp_lexer_peek_token (parser->lexer);
21646           error_at (token->location,
21647                     "%<template%> (as a disambiguator) is only allowed "
21648                     "within templates");
21649           /* If this part of the token stream is rescanned, the same
21650              error message would be generated.  So, we purge the token
21651              from the stream.  */
21652           cp_lexer_purge_token (parser->lexer);
21653           return false;
21654         }
21655       else
21656         {
21657           /* Consume the `template' keyword.  */
21658           cp_lexer_consume_token (parser->lexer);
21659           return true;
21660         }
21661     }
21662
21663   return false;
21664 }
21665
21666 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21667    set PARSER->SCOPE, and perform other related actions.  */
21668
21669 static void
21670 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21671 {
21672   int i;
21673   struct tree_check *check_value;
21674   deferred_access_check *chk;
21675   VEC (deferred_access_check,gc) *checks;
21676
21677   /* Get the stored value.  */
21678   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21679   /* Perform any access checks that were deferred.  */
21680   checks = check_value->checks;
21681   if (checks)
21682     {
21683       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21684         perform_or_defer_access_check (chk->binfo,
21685                                        chk->decl,
21686                                        chk->diag_decl);
21687     }
21688   /* Set the scope from the stored value.  */
21689   parser->scope = check_value->value;
21690   parser->qualifying_scope = check_value->qualifying_scope;
21691   parser->object_scope = NULL_TREE;
21692 }
21693
21694 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21695    encounter the end of a block before what we were looking for.  */
21696
21697 static bool
21698 cp_parser_cache_group (cp_parser *parser,
21699                        enum cpp_ttype end,
21700                        unsigned depth)
21701 {
21702   while (true)
21703     {
21704       cp_token *token = cp_lexer_peek_token (parser->lexer);
21705
21706       /* Abort a parenthesized expression if we encounter a semicolon.  */
21707       if ((end == CPP_CLOSE_PAREN || depth == 0)
21708           && token->type == CPP_SEMICOLON)
21709         return true;
21710       /* If we've reached the end of the file, stop.  */
21711       if (token->type == CPP_EOF
21712           || (end != CPP_PRAGMA_EOL
21713               && token->type == CPP_PRAGMA_EOL))
21714         return true;
21715       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21716         /* We've hit the end of an enclosing block, so there's been some
21717            kind of syntax error.  */
21718         return true;
21719
21720       /* Consume the token.  */
21721       cp_lexer_consume_token (parser->lexer);
21722       /* See if it starts a new group.  */
21723       if (token->type == CPP_OPEN_BRACE)
21724         {
21725           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21726           /* In theory this should probably check end == '}', but
21727              cp_parser_save_member_function_body needs it to exit
21728              after either '}' or ')' when called with ')'.  */
21729           if (depth == 0)
21730             return false;
21731         }
21732       else if (token->type == CPP_OPEN_PAREN)
21733         {
21734           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21735           if (depth == 0 && end == CPP_CLOSE_PAREN)
21736             return false;
21737         }
21738       else if (token->type == CPP_PRAGMA)
21739         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21740       else if (token->type == end)
21741         return false;
21742     }
21743 }
21744
21745 /* Begin parsing tentatively.  We always save tokens while parsing
21746    tentatively so that if the tentative parsing fails we can restore the
21747    tokens.  */
21748
21749 static void
21750 cp_parser_parse_tentatively (cp_parser* parser)
21751 {
21752   /* Enter a new parsing context.  */
21753   parser->context = cp_parser_context_new (parser->context);
21754   /* Begin saving tokens.  */
21755   cp_lexer_save_tokens (parser->lexer);
21756   /* In order to avoid repetitive access control error messages,
21757      access checks are queued up until we are no longer parsing
21758      tentatively.  */
21759   push_deferring_access_checks (dk_deferred);
21760 }
21761
21762 /* Commit to the currently active tentative parse.  */
21763
21764 static void
21765 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21766 {
21767   cp_parser_context *context;
21768   cp_lexer *lexer;
21769
21770   /* Mark all of the levels as committed.  */
21771   lexer = parser->lexer;
21772   for (context = parser->context; context->next; context = context->next)
21773     {
21774       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21775         break;
21776       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21777       while (!cp_lexer_saving_tokens (lexer))
21778         lexer = lexer->next;
21779       cp_lexer_commit_tokens (lexer);
21780     }
21781 }
21782
21783 /* Abort the currently active tentative parse.  All consumed tokens
21784    will be rolled back, and no diagnostics will be issued.  */
21785
21786 static void
21787 cp_parser_abort_tentative_parse (cp_parser* parser)
21788 {
21789   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21790               || errorcount > 0);
21791   cp_parser_simulate_error (parser);
21792   /* Now, pretend that we want to see if the construct was
21793      successfully parsed.  */
21794   cp_parser_parse_definitely (parser);
21795 }
21796
21797 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21798    token stream.  Otherwise, commit to the tokens we have consumed.
21799    Returns true if no error occurred; false otherwise.  */
21800
21801 static bool
21802 cp_parser_parse_definitely (cp_parser* parser)
21803 {
21804   bool error_occurred;
21805   cp_parser_context *context;
21806
21807   /* Remember whether or not an error occurred, since we are about to
21808      destroy that information.  */
21809   error_occurred = cp_parser_error_occurred (parser);
21810   /* Remove the topmost context from the stack.  */
21811   context = parser->context;
21812   parser->context = context->next;
21813   /* If no parse errors occurred, commit to the tentative parse.  */
21814   if (!error_occurred)
21815     {
21816       /* Commit to the tokens read tentatively, unless that was
21817          already done.  */
21818       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21819         cp_lexer_commit_tokens (parser->lexer);
21820
21821       pop_to_parent_deferring_access_checks ();
21822     }
21823   /* Otherwise, if errors occurred, roll back our state so that things
21824      are just as they were before we began the tentative parse.  */
21825   else
21826     {
21827       cp_lexer_rollback_tokens (parser->lexer);
21828       pop_deferring_access_checks ();
21829     }
21830   /* Add the context to the front of the free list.  */
21831   context->next = cp_parser_context_free_list;
21832   cp_parser_context_free_list = context;
21833
21834   return !error_occurred;
21835 }
21836
21837 /* Returns true if we are parsing tentatively and are not committed to
21838    this tentative parse.  */
21839
21840 static bool
21841 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21842 {
21843   return (cp_parser_parsing_tentatively (parser)
21844           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21845 }
21846
21847 /* Returns nonzero iff an error has occurred during the most recent
21848    tentative parse.  */
21849
21850 static bool
21851 cp_parser_error_occurred (cp_parser* parser)
21852 {
21853   return (cp_parser_parsing_tentatively (parser)
21854           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21855 }
21856
21857 /* Returns nonzero if GNU extensions are allowed.  */
21858
21859 static bool
21860 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21861 {
21862   return parser->allow_gnu_extensions_p;
21863 }
21864 \f
21865 /* Objective-C++ Productions */
21866
21867
21868 /* Parse an Objective-C expression, which feeds into a primary-expression
21869    above.
21870
21871    objc-expression:
21872      objc-message-expression
21873      objc-string-literal
21874      objc-encode-expression
21875      objc-protocol-expression
21876      objc-selector-expression
21877
21878   Returns a tree representation of the expression.  */
21879
21880 static tree
21881 cp_parser_objc_expression (cp_parser* parser)
21882 {
21883   /* Try to figure out what kind of declaration is present.  */
21884   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21885
21886   switch (kwd->type)
21887     {
21888     case CPP_OPEN_SQUARE:
21889       return cp_parser_objc_message_expression (parser);
21890
21891     case CPP_OBJC_STRING:
21892       kwd = cp_lexer_consume_token (parser->lexer);
21893       return objc_build_string_object (kwd->u.value);
21894
21895     case CPP_KEYWORD:
21896       switch (kwd->keyword)
21897         {
21898         case RID_AT_ENCODE:
21899           return cp_parser_objc_encode_expression (parser);
21900
21901         case RID_AT_PROTOCOL:
21902           return cp_parser_objc_protocol_expression (parser);
21903
21904         case RID_AT_SELECTOR:
21905           return cp_parser_objc_selector_expression (parser);
21906
21907         default:
21908           break;
21909         }
21910     default:
21911       error_at (kwd->location,
21912                 "misplaced %<@%D%> Objective-C++ construct",
21913                 kwd->u.value);
21914       cp_parser_skip_to_end_of_block_or_statement (parser);
21915     }
21916
21917   return error_mark_node;
21918 }
21919
21920 /* Parse an Objective-C message expression.
21921
21922    objc-message-expression:
21923      [ objc-message-receiver objc-message-args ]
21924
21925    Returns a representation of an Objective-C message.  */
21926
21927 static tree
21928 cp_parser_objc_message_expression (cp_parser* parser)
21929 {
21930   tree receiver, messageargs;
21931
21932   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21933   receiver = cp_parser_objc_message_receiver (parser);
21934   messageargs = cp_parser_objc_message_args (parser);
21935   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21936
21937   return objc_build_message_expr (receiver, messageargs);
21938 }
21939
21940 /* Parse an objc-message-receiver.
21941
21942    objc-message-receiver:
21943      expression
21944      simple-type-specifier
21945
21946   Returns a representation of the type or expression.  */
21947
21948 static tree
21949 cp_parser_objc_message_receiver (cp_parser* parser)
21950 {
21951   tree rcv;
21952
21953   /* An Objective-C message receiver may be either (1) a type
21954      or (2) an expression.  */
21955   cp_parser_parse_tentatively (parser);
21956   rcv = cp_parser_expression (parser, false, NULL);
21957
21958   if (cp_parser_parse_definitely (parser))
21959     return rcv;
21960
21961   rcv = cp_parser_simple_type_specifier (parser,
21962                                          /*decl_specs=*/NULL,
21963                                          CP_PARSER_FLAGS_NONE);
21964
21965   return objc_get_class_reference (rcv);
21966 }
21967
21968 /* Parse the arguments and selectors comprising an Objective-C message.
21969
21970    objc-message-args:
21971      objc-selector
21972      objc-selector-args
21973      objc-selector-args , objc-comma-args
21974
21975    objc-selector-args:
21976      objc-selector [opt] : assignment-expression
21977      objc-selector-args objc-selector [opt] : assignment-expression
21978
21979    objc-comma-args:
21980      assignment-expression
21981      objc-comma-args , assignment-expression
21982
21983    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21984    selector arguments and TREE_VALUE containing a list of comma
21985    arguments.  */
21986
21987 static tree
21988 cp_parser_objc_message_args (cp_parser* parser)
21989 {
21990   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21991   bool maybe_unary_selector_p = true;
21992   cp_token *token = cp_lexer_peek_token (parser->lexer);
21993
21994   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21995     {
21996       tree selector = NULL_TREE, arg;
21997
21998       if (token->type != CPP_COLON)
21999         selector = cp_parser_objc_selector (parser);
22000
22001       /* Detect if we have a unary selector.  */
22002       if (maybe_unary_selector_p
22003           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22004         return build_tree_list (selector, NULL_TREE);
22005
22006       maybe_unary_selector_p = false;
22007       cp_parser_require (parser, CPP_COLON, RT_COLON);
22008       arg = cp_parser_assignment_expression (parser, false, NULL);
22009
22010       sel_args
22011         = chainon (sel_args,
22012                    build_tree_list (selector, arg));
22013
22014       token = cp_lexer_peek_token (parser->lexer);
22015     }
22016
22017   /* Handle non-selector arguments, if any. */
22018   while (token->type == CPP_COMMA)
22019     {
22020       tree arg;
22021
22022       cp_lexer_consume_token (parser->lexer);
22023       arg = cp_parser_assignment_expression (parser, false, NULL);
22024
22025       addl_args
22026         = chainon (addl_args,
22027                    build_tree_list (NULL_TREE, arg));
22028
22029       token = cp_lexer_peek_token (parser->lexer);
22030     }
22031
22032   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22033     {
22034       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22035       return build_tree_list (error_mark_node, error_mark_node);
22036     }
22037
22038   return build_tree_list (sel_args, addl_args);
22039 }
22040
22041 /* Parse an Objective-C encode expression.
22042
22043    objc-encode-expression:
22044      @encode objc-typename
22045
22046    Returns an encoded representation of the type argument.  */
22047
22048 static tree
22049 cp_parser_objc_encode_expression (cp_parser* parser)
22050 {
22051   tree type;
22052   cp_token *token;
22053
22054   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
22055   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22056   token = cp_lexer_peek_token (parser->lexer);
22057   type = complete_type (cp_parser_type_id (parser));
22058   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22059
22060   if (!type)
22061     {
22062       error_at (token->location, 
22063                 "%<@encode%> must specify a type as an argument");
22064       return error_mark_node;
22065     }
22066
22067   /* This happens if we find @encode(T) (where T is a template
22068      typename or something dependent on a template typename) when
22069      parsing a template.  In that case, we can't compile it
22070      immediately, but we rather create an AT_ENCODE_EXPR which will
22071      need to be instantiated when the template is used.
22072   */
22073   if (dependent_type_p (type))
22074     {
22075       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22076       TREE_READONLY (value) = 1;
22077       return value;
22078     }
22079
22080   return objc_build_encode_expr (type);
22081 }
22082
22083 /* Parse an Objective-C @defs expression.  */
22084
22085 static tree
22086 cp_parser_objc_defs_expression (cp_parser *parser)
22087 {
22088   tree name;
22089
22090   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
22091   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22092   name = cp_parser_identifier (parser);
22093   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22094
22095   return objc_get_class_ivars (name);
22096 }
22097
22098 /* Parse an Objective-C protocol expression.
22099
22100   objc-protocol-expression:
22101     @protocol ( identifier )
22102
22103   Returns a representation of the protocol expression.  */
22104
22105 static tree
22106 cp_parser_objc_protocol_expression (cp_parser* parser)
22107 {
22108   tree proto;
22109
22110   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22111   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22112   proto = cp_parser_identifier (parser);
22113   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22114
22115   return objc_build_protocol_expr (proto);
22116 }
22117
22118 /* Parse an Objective-C selector expression.
22119
22120    objc-selector-expression:
22121      @selector ( objc-method-signature )
22122
22123    objc-method-signature:
22124      objc-selector
22125      objc-selector-seq
22126
22127    objc-selector-seq:
22128      objc-selector :
22129      objc-selector-seq objc-selector :
22130
22131   Returns a representation of the method selector.  */
22132
22133 static tree
22134 cp_parser_objc_selector_expression (cp_parser* parser)
22135 {
22136   tree sel_seq = NULL_TREE;
22137   bool maybe_unary_selector_p = true;
22138   cp_token *token;
22139   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22140
22141   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
22142   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22143   token = cp_lexer_peek_token (parser->lexer);
22144
22145   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22146          || token->type == CPP_SCOPE)
22147     {
22148       tree selector = NULL_TREE;
22149
22150       if (token->type != CPP_COLON
22151           || token->type == CPP_SCOPE)
22152         selector = cp_parser_objc_selector (parser);
22153
22154       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22155           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22156         {
22157           /* Detect if we have a unary selector.  */
22158           if (maybe_unary_selector_p)
22159             {
22160               sel_seq = selector;
22161               goto finish_selector;
22162             }
22163           else
22164             {
22165               cp_parser_error (parser, "expected %<:%>");
22166             }
22167         }
22168       maybe_unary_selector_p = false;
22169       token = cp_lexer_consume_token (parser->lexer);
22170
22171       if (token->type == CPP_SCOPE)
22172         {
22173           sel_seq
22174             = chainon (sel_seq,
22175                        build_tree_list (selector, NULL_TREE));
22176           sel_seq
22177             = chainon (sel_seq,
22178                        build_tree_list (NULL_TREE, NULL_TREE));
22179         }
22180       else
22181         sel_seq
22182           = chainon (sel_seq,
22183                      build_tree_list (selector, NULL_TREE));
22184
22185       token = cp_lexer_peek_token (parser->lexer);
22186     }
22187
22188  finish_selector:
22189   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22190
22191   return objc_build_selector_expr (loc, sel_seq);
22192 }
22193
22194 /* Parse a list of identifiers.
22195
22196    objc-identifier-list:
22197      identifier
22198      objc-identifier-list , identifier
22199
22200    Returns a TREE_LIST of identifier nodes.  */
22201
22202 static tree
22203 cp_parser_objc_identifier_list (cp_parser* parser)
22204 {
22205   tree identifier;
22206   tree list;
22207   cp_token *sep;
22208
22209   identifier = cp_parser_identifier (parser);
22210   if (identifier == error_mark_node)
22211     return error_mark_node;      
22212
22213   list = build_tree_list (NULL_TREE, identifier);
22214   sep = cp_lexer_peek_token (parser->lexer);
22215
22216   while (sep->type == CPP_COMMA)
22217     {
22218       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22219       identifier = cp_parser_identifier (parser);
22220       if (identifier == error_mark_node)
22221         return list;
22222
22223       list = chainon (list, build_tree_list (NULL_TREE,
22224                                              identifier));
22225       sep = cp_lexer_peek_token (parser->lexer);
22226     }
22227   
22228   return list;
22229 }
22230
22231 /* Parse an Objective-C alias declaration.
22232
22233    objc-alias-declaration:
22234      @compatibility_alias identifier identifier ;
22235
22236    This function registers the alias mapping with the Objective-C front end.
22237    It returns nothing.  */
22238
22239 static void
22240 cp_parser_objc_alias_declaration (cp_parser* parser)
22241 {
22242   tree alias, orig;
22243
22244   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22245   alias = cp_parser_identifier (parser);
22246   orig = cp_parser_identifier (parser);
22247   objc_declare_alias (alias, orig);
22248   cp_parser_consume_semicolon_at_end_of_statement (parser);
22249 }
22250
22251 /* Parse an Objective-C class forward-declaration.
22252
22253    objc-class-declaration:
22254      @class objc-identifier-list ;
22255
22256    The function registers the forward declarations with the Objective-C
22257    front end.  It returns nothing.  */
22258
22259 static void
22260 cp_parser_objc_class_declaration (cp_parser* parser)
22261 {
22262   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22263   while (true)
22264     {
22265       tree id;
22266       
22267       id = cp_parser_identifier (parser);
22268       if (id == error_mark_node)
22269         break;
22270       
22271       objc_declare_class (id);
22272
22273       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22274         cp_lexer_consume_token (parser->lexer);
22275       else
22276         break;
22277     }
22278   cp_parser_consume_semicolon_at_end_of_statement (parser);
22279 }
22280
22281 /* Parse a list of Objective-C protocol references.
22282
22283    objc-protocol-refs-opt:
22284      objc-protocol-refs [opt]
22285
22286    objc-protocol-refs:
22287      < objc-identifier-list >
22288
22289    Returns a TREE_LIST of identifiers, if any.  */
22290
22291 static tree
22292 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22293 {
22294   tree protorefs = NULL_TREE;
22295
22296   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22297     {
22298       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22299       protorefs = cp_parser_objc_identifier_list (parser);
22300       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22301     }
22302
22303   return protorefs;
22304 }
22305
22306 /* Parse a Objective-C visibility specification.  */
22307
22308 static void
22309 cp_parser_objc_visibility_spec (cp_parser* parser)
22310 {
22311   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22312
22313   switch (vis->keyword)
22314     {
22315     case RID_AT_PRIVATE:
22316       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22317       break;
22318     case RID_AT_PROTECTED:
22319       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22320       break;
22321     case RID_AT_PUBLIC:
22322       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22323       break;
22324     case RID_AT_PACKAGE:
22325       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22326       break;
22327     default:
22328       return;
22329     }
22330
22331   /* Eat '@private'/'@protected'/'@public'.  */
22332   cp_lexer_consume_token (parser->lexer);
22333 }
22334
22335 /* Parse an Objective-C method type.  Return 'true' if it is a class
22336    (+) method, and 'false' if it is an instance (-) method.  */
22337
22338 static inline bool
22339 cp_parser_objc_method_type (cp_parser* parser)
22340 {
22341   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22342     return true;
22343   else
22344     return false;
22345 }
22346
22347 /* Parse an Objective-C protocol qualifier.  */
22348
22349 static tree
22350 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
22351 {
22352   tree quals = NULL_TREE, node;
22353   cp_token *token = cp_lexer_peek_token (parser->lexer);
22354
22355   node = token->u.value;
22356
22357   while (node && TREE_CODE (node) == IDENTIFIER_NODE
22358          && (node == ridpointers [(int) RID_IN]
22359              || node == ridpointers [(int) RID_OUT]
22360              || node == ridpointers [(int) RID_INOUT]
22361              || node == ridpointers [(int) RID_BYCOPY]
22362              || node == ridpointers [(int) RID_BYREF]
22363              || node == ridpointers [(int) RID_ONEWAY]))
22364     {
22365       quals = tree_cons (NULL_TREE, node, quals);
22366       cp_lexer_consume_token (parser->lexer);
22367       token = cp_lexer_peek_token (parser->lexer);
22368       node = token->u.value;
22369     }
22370
22371   return quals;
22372 }
22373
22374 /* Parse an Objective-C typename.  */
22375
22376 static tree
22377 cp_parser_objc_typename (cp_parser* parser)
22378 {
22379   tree type_name = NULL_TREE;
22380
22381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22382     {
22383       tree proto_quals, cp_type = NULL_TREE;
22384
22385       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22386       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22387
22388       /* An ObjC type name may consist of just protocol qualifiers, in which
22389          case the type shall default to 'id'.  */
22390       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22391         {
22392           cp_type = cp_parser_type_id (parser);
22393           
22394           /* If the type could not be parsed, an error has already
22395              been produced.  For error recovery, behave as if it had
22396              not been specified, which will use the default type
22397              'id'.  */
22398           if (cp_type == error_mark_node)
22399             {
22400               cp_type = NULL_TREE;
22401               /* We need to skip to the closing parenthesis as
22402                  cp_parser_type_id() does not seem to do it for
22403                  us.  */
22404               cp_parser_skip_to_closing_parenthesis (parser,
22405                                                      /*recovering=*/true,
22406                                                      /*or_comma=*/false,
22407                                                      /*consume_paren=*/false);
22408             }
22409         }
22410
22411       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22412       type_name = build_tree_list (proto_quals, cp_type);
22413     }
22414
22415   return type_name;
22416 }
22417
22418 /* Check to see if TYPE refers to an Objective-C selector name.  */
22419
22420 static bool
22421 cp_parser_objc_selector_p (enum cpp_ttype type)
22422 {
22423   return (type == CPP_NAME || type == CPP_KEYWORD
22424           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22425           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22426           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22427           || type == CPP_XOR || type == CPP_XOR_EQ);
22428 }
22429
22430 /* Parse an Objective-C selector.  */
22431
22432 static tree
22433 cp_parser_objc_selector (cp_parser* parser)
22434 {
22435   cp_token *token = cp_lexer_consume_token (parser->lexer);
22436
22437   if (!cp_parser_objc_selector_p (token->type))
22438     {
22439       error_at (token->location, "invalid Objective-C++ selector name");
22440       return error_mark_node;
22441     }
22442
22443   /* C++ operator names are allowed to appear in ObjC selectors.  */
22444   switch (token->type)
22445     {
22446     case CPP_AND_AND: return get_identifier ("and");
22447     case CPP_AND_EQ: return get_identifier ("and_eq");
22448     case CPP_AND: return get_identifier ("bitand");
22449     case CPP_OR: return get_identifier ("bitor");
22450     case CPP_COMPL: return get_identifier ("compl");
22451     case CPP_NOT: return get_identifier ("not");
22452     case CPP_NOT_EQ: return get_identifier ("not_eq");
22453     case CPP_OR_OR: return get_identifier ("or");
22454     case CPP_OR_EQ: return get_identifier ("or_eq");
22455     case CPP_XOR: return get_identifier ("xor");
22456     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22457     default: return token->u.value;
22458     }
22459 }
22460
22461 /* Parse an Objective-C params list.  */
22462
22463 static tree
22464 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22465 {
22466   tree params = NULL_TREE;
22467   bool maybe_unary_selector_p = true;
22468   cp_token *token = cp_lexer_peek_token (parser->lexer);
22469
22470   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22471     {
22472       tree selector = NULL_TREE, type_name, identifier;
22473       tree parm_attr = NULL_TREE;
22474
22475       if (token->keyword == RID_ATTRIBUTE)
22476         break;
22477
22478       if (token->type != CPP_COLON)
22479         selector = cp_parser_objc_selector (parser);
22480
22481       /* Detect if we have a unary selector.  */
22482       if (maybe_unary_selector_p
22483           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22484         {
22485           params = selector; /* Might be followed by attributes.  */
22486           break;
22487         }
22488
22489       maybe_unary_selector_p = false;
22490       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22491         {
22492           /* Something went quite wrong.  There should be a colon
22493              here, but there is not.  Stop parsing parameters.  */
22494           break;
22495         }
22496       type_name = cp_parser_objc_typename (parser);
22497       /* New ObjC allows attributes on parameters too.  */
22498       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22499         parm_attr = cp_parser_attributes_opt (parser);
22500       identifier = cp_parser_identifier (parser);
22501
22502       params
22503         = chainon (params,
22504                    objc_build_keyword_decl (selector,
22505                                             type_name,
22506                                             identifier,
22507                                             parm_attr));
22508
22509       token = cp_lexer_peek_token (parser->lexer);
22510     }
22511
22512   if (params == NULL_TREE)
22513     {
22514       cp_parser_error (parser, "objective-c++ method declaration is expected");
22515       return error_mark_node;
22516     }
22517
22518   /* We allow tail attributes for the method.  */
22519   if (token->keyword == RID_ATTRIBUTE)
22520     {
22521       *attributes = cp_parser_attributes_opt (parser);
22522       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22523           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22524         return params;
22525       cp_parser_error (parser, 
22526                        "method attributes must be specified at the end");
22527       return error_mark_node;
22528     }
22529
22530   if (params == NULL_TREE)
22531     {
22532       cp_parser_error (parser, "objective-c++ method declaration is expected");
22533       return error_mark_node;
22534     }
22535   return params;
22536 }
22537
22538 /* Parse the non-keyword Objective-C params.  */
22539
22540 static tree
22541 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22542                                        tree* attributes)
22543 {
22544   tree params = make_node (TREE_LIST);
22545   cp_token *token = cp_lexer_peek_token (parser->lexer);
22546   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22547
22548   while (token->type == CPP_COMMA)
22549     {
22550       cp_parameter_declarator *parmdecl;
22551       tree parm;
22552
22553       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22554       token = cp_lexer_peek_token (parser->lexer);
22555
22556       if (token->type == CPP_ELLIPSIS)
22557         {
22558           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22559           *ellipsisp = true;
22560           token = cp_lexer_peek_token (parser->lexer);
22561           break;
22562         }
22563
22564       /* TODO: parse attributes for tail parameters.  */
22565       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22566       parm = grokdeclarator (parmdecl->declarator,
22567                              &parmdecl->decl_specifiers,
22568                              PARM, /*initialized=*/0,
22569                              /*attrlist=*/NULL);
22570
22571       chainon (params, build_tree_list (NULL_TREE, parm));
22572       token = cp_lexer_peek_token (parser->lexer);
22573     }
22574
22575   /* We allow tail attributes for the method.  */
22576   if (token->keyword == RID_ATTRIBUTE)
22577     {
22578       if (*attributes == NULL_TREE)
22579         {
22580           *attributes = cp_parser_attributes_opt (parser);
22581           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22582               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22583             return params;
22584         }
22585       else        
22586         /* We have an error, but parse the attributes, so that we can 
22587            carry on.  */
22588         *attributes = cp_parser_attributes_opt (parser);
22589
22590       cp_parser_error (parser, 
22591                        "method attributes must be specified at the end");
22592       return error_mark_node;
22593     }
22594
22595   return params;
22596 }
22597
22598 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22599
22600 static void
22601 cp_parser_objc_interstitial_code (cp_parser* parser)
22602 {
22603   cp_token *token = cp_lexer_peek_token (parser->lexer);
22604
22605   /* If the next token is `extern' and the following token is a string
22606      literal, then we have a linkage specification.  */
22607   if (token->keyword == RID_EXTERN
22608       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22609     cp_parser_linkage_specification (parser);
22610   /* Handle #pragma, if any.  */
22611   else if (token->type == CPP_PRAGMA)
22612     cp_parser_pragma (parser, pragma_external);
22613   /* Allow stray semicolons.  */
22614   else if (token->type == CPP_SEMICOLON)
22615     cp_lexer_consume_token (parser->lexer);
22616   /* Mark methods as optional or required, when building protocols.  */
22617   else if (token->keyword == RID_AT_OPTIONAL)
22618     {
22619       cp_lexer_consume_token (parser->lexer);
22620       objc_set_method_opt (true);
22621     }
22622   else if (token->keyword == RID_AT_REQUIRED)
22623     {
22624       cp_lexer_consume_token (parser->lexer);
22625       objc_set_method_opt (false);
22626     }
22627   else if (token->keyword == RID_NAMESPACE)
22628     cp_parser_namespace_definition (parser);
22629   /* Other stray characters must generate errors.  */
22630   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22631     {
22632       cp_lexer_consume_token (parser->lexer);
22633       error ("stray %qs between Objective-C++ methods",
22634              token->type == CPP_OPEN_BRACE ? "{" : "}");
22635     }
22636   /* Finally, try to parse a block-declaration, or a function-definition.  */
22637   else
22638     cp_parser_block_declaration (parser, /*statement_p=*/false);
22639 }
22640
22641 /* Parse a method signature.  */
22642
22643 static tree
22644 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22645 {
22646   tree rettype, kwdparms, optparms;
22647   bool ellipsis = false;
22648   bool is_class_method;
22649
22650   is_class_method = cp_parser_objc_method_type (parser);
22651   rettype = cp_parser_objc_typename (parser);
22652   *attributes = NULL_TREE;
22653   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22654   if (kwdparms == error_mark_node)
22655     return error_mark_node;
22656   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22657   if (optparms == error_mark_node)
22658     return error_mark_node;
22659
22660   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22661 }
22662
22663 static bool
22664 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22665 {
22666   tree tattr;  
22667   cp_lexer_save_tokens (parser->lexer);
22668   tattr = cp_parser_attributes_opt (parser);
22669   gcc_assert (tattr) ;
22670   
22671   /* If the attributes are followed by a method introducer, this is not allowed.
22672      Dump the attributes and flag the situation.  */
22673   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22674       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22675     return true;
22676
22677   /* Otherwise, the attributes introduce some interstitial code, possibly so
22678      rewind to allow that check.  */
22679   cp_lexer_rollback_tokens (parser->lexer);
22680   return false;  
22681 }
22682
22683 /* Parse an Objective-C method prototype list.  */
22684
22685 static void
22686 cp_parser_objc_method_prototype_list (cp_parser* parser)
22687 {
22688   cp_token *token = cp_lexer_peek_token (parser->lexer);
22689
22690   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22691     {
22692       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22693         {
22694           tree attributes, sig;
22695           bool is_class_method;
22696           if (token->type == CPP_PLUS)
22697             is_class_method = true;
22698           else
22699             is_class_method = false;
22700           sig = cp_parser_objc_method_signature (parser, &attributes);
22701           if (sig == error_mark_node)
22702             {
22703               cp_parser_skip_to_end_of_block_or_statement (parser);
22704               token = cp_lexer_peek_token (parser->lexer);
22705               continue;
22706             }
22707           objc_add_method_declaration (is_class_method, sig, attributes);
22708           cp_parser_consume_semicolon_at_end_of_statement (parser);
22709         }
22710       else if (token->keyword == RID_AT_PROPERTY)
22711         cp_parser_objc_at_property_declaration (parser);
22712       else if (token->keyword == RID_ATTRIBUTE 
22713                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22714         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22715                     OPT_Wattributes, 
22716                     "prefix attributes are ignored for methods");
22717       else
22718         /* Allow for interspersed non-ObjC++ code.  */
22719         cp_parser_objc_interstitial_code (parser);
22720
22721       token = cp_lexer_peek_token (parser->lexer);
22722     }
22723
22724   if (token->type != CPP_EOF)
22725     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22726   else
22727     cp_parser_error (parser, "expected %<@end%>");
22728
22729   objc_finish_interface ();
22730 }
22731
22732 /* Parse an Objective-C method definition list.  */
22733
22734 static void
22735 cp_parser_objc_method_definition_list (cp_parser* parser)
22736 {
22737   cp_token *token = cp_lexer_peek_token (parser->lexer);
22738
22739   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22740     {
22741       tree meth;
22742
22743       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22744         {
22745           cp_token *ptk;
22746           tree sig, attribute;
22747           bool is_class_method;
22748           if (token->type == CPP_PLUS)
22749             is_class_method = true;
22750           else
22751             is_class_method = false;
22752           push_deferring_access_checks (dk_deferred);
22753           sig = cp_parser_objc_method_signature (parser, &attribute);
22754           if (sig == error_mark_node)
22755             {
22756               cp_parser_skip_to_end_of_block_or_statement (parser);
22757               token = cp_lexer_peek_token (parser->lexer);
22758               continue;
22759             }
22760           objc_start_method_definition (is_class_method, sig, attribute,
22761                                         NULL_TREE);
22762
22763           /* For historical reasons, we accept an optional semicolon.  */
22764           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22765             cp_lexer_consume_token (parser->lexer);
22766
22767           ptk = cp_lexer_peek_token (parser->lexer);
22768           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22769                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22770             {
22771               perform_deferred_access_checks ();
22772               stop_deferring_access_checks ();
22773               meth = cp_parser_function_definition_after_declarator (parser,
22774                                                                      false);
22775               pop_deferring_access_checks ();
22776               objc_finish_method_definition (meth);
22777             }
22778         }
22779       /* The following case will be removed once @synthesize is
22780          completely implemented.  */
22781       else if (token->keyword == RID_AT_PROPERTY)
22782         cp_parser_objc_at_property_declaration (parser);
22783       else if (token->keyword == RID_AT_SYNTHESIZE)
22784         cp_parser_objc_at_synthesize_declaration (parser);
22785       else if (token->keyword == RID_AT_DYNAMIC)
22786         cp_parser_objc_at_dynamic_declaration (parser);
22787       else if (token->keyword == RID_ATTRIBUTE 
22788                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22789         warning_at (token->location, OPT_Wattributes,
22790                     "prefix attributes are ignored for methods");
22791       else
22792         /* Allow for interspersed non-ObjC++ code.  */
22793         cp_parser_objc_interstitial_code (parser);
22794
22795       token = cp_lexer_peek_token (parser->lexer);
22796     }
22797
22798   if (token->type != CPP_EOF)
22799     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22800   else
22801     cp_parser_error (parser, "expected %<@end%>");
22802
22803   objc_finish_implementation ();
22804 }
22805
22806 /* Parse Objective-C ivars.  */
22807
22808 static void
22809 cp_parser_objc_class_ivars (cp_parser* parser)
22810 {
22811   cp_token *token = cp_lexer_peek_token (parser->lexer);
22812
22813   if (token->type != CPP_OPEN_BRACE)
22814     return;     /* No ivars specified.  */
22815
22816   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22817   token = cp_lexer_peek_token (parser->lexer);
22818
22819   while (token->type != CPP_CLOSE_BRACE 
22820         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22821     {
22822       cp_decl_specifier_seq declspecs;
22823       int decl_class_or_enum_p;
22824       tree prefix_attributes;
22825
22826       cp_parser_objc_visibility_spec (parser);
22827
22828       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22829         break;
22830
22831       cp_parser_decl_specifier_seq (parser,
22832                                     CP_PARSER_FLAGS_OPTIONAL,
22833                                     &declspecs,
22834                                     &decl_class_or_enum_p);
22835
22836       /* auto, register, static, extern, mutable.  */
22837       if (declspecs.storage_class != sc_none)
22838         {
22839           cp_parser_error (parser, "invalid type for instance variable");         
22840           declspecs.storage_class = sc_none;
22841         }
22842
22843       /* __thread.  */
22844       if (declspecs.specs[(int) ds_thread])
22845         {
22846           cp_parser_error (parser, "invalid type for instance variable");
22847           declspecs.specs[(int) ds_thread] = 0;
22848         }
22849       
22850       /* typedef.  */
22851       if (declspecs.specs[(int) ds_typedef])
22852         {
22853           cp_parser_error (parser, "invalid type for instance variable");
22854           declspecs.specs[(int) ds_typedef] = 0;
22855         }
22856
22857       prefix_attributes = declspecs.attributes;
22858       declspecs.attributes = NULL_TREE;
22859
22860       /* Keep going until we hit the `;' at the end of the
22861          declaration.  */
22862       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22863         {
22864           tree width = NULL_TREE, attributes, first_attribute, decl;
22865           cp_declarator *declarator = NULL;
22866           int ctor_dtor_or_conv_p;
22867
22868           /* Check for a (possibly unnamed) bitfield declaration.  */
22869           token = cp_lexer_peek_token (parser->lexer);
22870           if (token->type == CPP_COLON)
22871             goto eat_colon;
22872
22873           if (token->type == CPP_NAME
22874               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22875                   == CPP_COLON))
22876             {
22877               /* Get the name of the bitfield.  */
22878               declarator = make_id_declarator (NULL_TREE,
22879                                                cp_parser_identifier (parser),
22880                                                sfk_none);
22881
22882              eat_colon:
22883               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22884               /* Get the width of the bitfield.  */
22885               width
22886                 = cp_parser_constant_expression (parser,
22887                                                  /*allow_non_constant=*/false,
22888                                                  NULL);
22889             }
22890           else
22891             {
22892               /* Parse the declarator.  */
22893               declarator
22894                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22895                                         &ctor_dtor_or_conv_p,
22896                                         /*parenthesized_p=*/NULL,
22897                                         /*member_p=*/false);
22898             }
22899
22900           /* Look for attributes that apply to the ivar.  */
22901           attributes = cp_parser_attributes_opt (parser);
22902           /* Remember which attributes are prefix attributes and
22903              which are not.  */
22904           first_attribute = attributes;
22905           /* Combine the attributes.  */
22906           attributes = chainon (prefix_attributes, attributes);
22907
22908           if (width)
22909               /* Create the bitfield declaration.  */
22910               decl = grokbitfield (declarator, &declspecs,
22911                                    width,
22912                                    attributes);
22913           else
22914             decl = grokfield (declarator, &declspecs,
22915                               NULL_TREE, /*init_const_expr_p=*/false,
22916                               NULL_TREE, attributes);
22917
22918           /* Add the instance variable.  */
22919           if (decl != error_mark_node && decl != NULL_TREE)
22920             objc_add_instance_variable (decl);
22921
22922           /* Reset PREFIX_ATTRIBUTES.  */
22923           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22924             attributes = TREE_CHAIN (attributes);
22925           if (attributes)
22926             TREE_CHAIN (attributes) = NULL_TREE;
22927
22928           token = cp_lexer_peek_token (parser->lexer);
22929
22930           if (token->type == CPP_COMMA)
22931             {
22932               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22933               continue;
22934             }
22935           break;
22936         }
22937
22938       cp_parser_consume_semicolon_at_end_of_statement (parser);
22939       token = cp_lexer_peek_token (parser->lexer);
22940     }
22941
22942   if (token->keyword == RID_AT_END)
22943     cp_parser_error (parser, "expected %<}%>");
22944
22945   /* Do not consume the RID_AT_END, so it will be read again as terminating
22946      the @interface of @implementation.  */ 
22947   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22948     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22949     
22950   /* For historical reasons, we accept an optional semicolon.  */
22951   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22952     cp_lexer_consume_token (parser->lexer);
22953 }
22954
22955 /* Parse an Objective-C protocol declaration.  */
22956
22957 static void
22958 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22959 {
22960   tree proto, protorefs;
22961   cp_token *tok;
22962
22963   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22964   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22965     {
22966       tok = cp_lexer_peek_token (parser->lexer);
22967       error_at (tok->location, "identifier expected after %<@protocol%>");
22968       cp_parser_consume_semicolon_at_end_of_statement (parser);
22969       return;
22970     }
22971
22972   /* See if we have a forward declaration or a definition.  */
22973   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22974
22975   /* Try a forward declaration first.  */
22976   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22977     {
22978       while (true)
22979         {
22980           tree id;
22981           
22982           id = cp_parser_identifier (parser);
22983           if (id == error_mark_node)
22984             break;
22985           
22986           objc_declare_protocol (id, attributes);
22987           
22988           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22989             cp_lexer_consume_token (parser->lexer);
22990           else
22991             break;
22992         }
22993       cp_parser_consume_semicolon_at_end_of_statement (parser);
22994     }
22995
22996   /* Ok, we got a full-fledged definition (or at least should).  */
22997   else
22998     {
22999       proto = cp_parser_identifier (parser);
23000       protorefs = cp_parser_objc_protocol_refs_opt (parser);
23001       objc_start_protocol (proto, protorefs, attributes);
23002       cp_parser_objc_method_prototype_list (parser);
23003     }
23004 }
23005
23006 /* Parse an Objective-C superclass or category.  */
23007
23008 static void
23009 cp_parser_objc_superclass_or_category (cp_parser *parser, 
23010                                        bool iface_p,
23011                                        tree *super,
23012                                        tree *categ, bool *is_class_extension)
23013 {
23014   cp_token *next = cp_lexer_peek_token (parser->lexer);
23015
23016   *super = *categ = NULL_TREE;
23017   *is_class_extension = false;
23018   if (next->type == CPP_COLON)
23019     {
23020       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23021       *super = cp_parser_identifier (parser);
23022     }
23023   else if (next->type == CPP_OPEN_PAREN)
23024     {
23025       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23026
23027       /* If there is no category name, and this is an @interface, we
23028          have a class extension.  */
23029       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23030         {
23031           *categ = NULL_TREE;
23032           *is_class_extension = true;
23033         }
23034       else
23035         *categ = cp_parser_identifier (parser);
23036
23037       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23038     }
23039 }
23040
23041 /* Parse an Objective-C class interface.  */
23042
23043 static void
23044 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23045 {
23046   tree name, super, categ, protos;
23047   bool is_class_extension;
23048
23049   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
23050   name = cp_parser_identifier (parser);
23051   if (name == error_mark_node)
23052     {
23053       /* It's hard to recover because even if valid @interface stuff
23054          is to follow, we can't compile it (or validate it) if we
23055          don't even know which class it refers to.  Let's assume this
23056          was a stray '@interface' token in the stream and skip it.
23057       */
23058       return;
23059     }
23060   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23061                                          &is_class_extension);
23062   protos = cp_parser_objc_protocol_refs_opt (parser);
23063
23064   /* We have either a class or a category on our hands.  */
23065   if (categ || is_class_extension)
23066     objc_start_category_interface (name, categ, protos, attributes);
23067   else
23068     {
23069       objc_start_class_interface (name, super, protos, attributes);
23070       /* Handle instance variable declarations, if any.  */
23071       cp_parser_objc_class_ivars (parser);
23072       objc_continue_interface ();
23073     }
23074
23075   cp_parser_objc_method_prototype_list (parser);
23076 }
23077
23078 /* Parse an Objective-C class implementation.  */
23079
23080 static void
23081 cp_parser_objc_class_implementation (cp_parser* parser)
23082 {
23083   tree name, super, categ;
23084   bool is_class_extension;
23085
23086   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
23087   name = cp_parser_identifier (parser);
23088   if (name == error_mark_node)
23089     {
23090       /* It's hard to recover because even if valid @implementation
23091          stuff is to follow, we can't compile it (or validate it) if
23092          we don't even know which class it refers to.  Let's assume
23093          this was a stray '@implementation' token in the stream and
23094          skip it.
23095       */
23096       return;
23097     }
23098   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23099                                          &is_class_extension);
23100
23101   /* We have either a class or a category on our hands.  */
23102   if (categ)
23103     objc_start_category_implementation (name, categ);
23104   else
23105     {
23106       objc_start_class_implementation (name, super);
23107       /* Handle instance variable declarations, if any.  */
23108       cp_parser_objc_class_ivars (parser);
23109       objc_continue_implementation ();
23110     }
23111
23112   cp_parser_objc_method_definition_list (parser);
23113 }
23114
23115 /* Consume the @end token and finish off the implementation.  */
23116
23117 static void
23118 cp_parser_objc_end_implementation (cp_parser* parser)
23119 {
23120   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23121   objc_finish_implementation ();
23122 }
23123
23124 /* Parse an Objective-C declaration.  */
23125
23126 static void
23127 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23128 {
23129   /* Try to figure out what kind of declaration is present.  */
23130   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23131
23132   if (attributes)
23133     switch (kwd->keyword)
23134       {
23135         case RID_AT_ALIAS:
23136         case RID_AT_CLASS:
23137         case RID_AT_END:
23138           error_at (kwd->location, "attributes may not be specified before"
23139                     " the %<@%D%> Objective-C++ keyword",
23140                     kwd->u.value);
23141           attributes = NULL;
23142           break;
23143         case RID_AT_IMPLEMENTATION:
23144           warning_at (kwd->location, OPT_Wattributes,
23145                       "prefix attributes are ignored before %<@%D%>",
23146                       kwd->u.value);
23147           attributes = NULL;
23148         default:
23149           break;
23150       }
23151
23152   switch (kwd->keyword)
23153     {
23154     case RID_AT_ALIAS:
23155       cp_parser_objc_alias_declaration (parser);
23156       break;
23157     case RID_AT_CLASS:
23158       cp_parser_objc_class_declaration (parser);
23159       break;
23160     case RID_AT_PROTOCOL:
23161       cp_parser_objc_protocol_declaration (parser, attributes);
23162       break;
23163     case RID_AT_INTERFACE:
23164       cp_parser_objc_class_interface (parser, attributes);
23165       break;
23166     case RID_AT_IMPLEMENTATION:
23167       cp_parser_objc_class_implementation (parser);
23168       break;
23169     case RID_AT_END:
23170       cp_parser_objc_end_implementation (parser);
23171       break;
23172     default:
23173       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23174                 kwd->u.value);
23175       cp_parser_skip_to_end_of_block_or_statement (parser);
23176     }
23177 }
23178
23179 /* Parse an Objective-C try-catch-finally statement.
23180
23181    objc-try-catch-finally-stmt:
23182      @try compound-statement objc-catch-clause-seq [opt]
23183        objc-finally-clause [opt]
23184
23185    objc-catch-clause-seq:
23186      objc-catch-clause objc-catch-clause-seq [opt]
23187
23188    objc-catch-clause:
23189      @catch ( objc-exception-declaration ) compound-statement
23190
23191    objc-finally-clause:
23192      @finally compound-statement
23193
23194    objc-exception-declaration:
23195      parameter-declaration
23196      '...'
23197
23198    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23199
23200    Returns NULL_TREE.
23201
23202    PS: This function is identical to c_parser_objc_try_catch_finally_statement
23203    for C.  Keep them in sync.  */   
23204
23205 static tree
23206 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23207 {
23208   location_t location;
23209   tree stmt;
23210
23211   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23212   location = cp_lexer_peek_token (parser->lexer)->location;
23213   objc_maybe_warn_exceptions (location);
23214   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23215      node, lest it get absorbed into the surrounding block.  */
23216   stmt = push_stmt_list ();
23217   cp_parser_compound_statement (parser, NULL, false, false);
23218   objc_begin_try_stmt (location, pop_stmt_list (stmt));
23219
23220   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23221     {
23222       cp_parameter_declarator *parm;
23223       tree parameter_declaration = error_mark_node;
23224       bool seen_open_paren = false;
23225
23226       cp_lexer_consume_token (parser->lexer);
23227       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23228         seen_open_paren = true;
23229       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23230         {
23231           /* We have "@catch (...)" (where the '...' are literally
23232              what is in the code).  Skip the '...'.
23233              parameter_declaration is set to NULL_TREE, and
23234              objc_being_catch_clauses() knows that that means
23235              '...'.  */
23236           cp_lexer_consume_token (parser->lexer);
23237           parameter_declaration = NULL_TREE;
23238         }
23239       else
23240         {
23241           /* We have "@catch (NSException *exception)" or something
23242              like that.  Parse the parameter declaration.  */
23243           parm = cp_parser_parameter_declaration (parser, false, NULL);
23244           if (parm == NULL)
23245             parameter_declaration = error_mark_node;
23246           else
23247             parameter_declaration = grokdeclarator (parm->declarator,
23248                                                     &parm->decl_specifiers,
23249                                                     PARM, /*initialized=*/0,
23250                                                     /*attrlist=*/NULL);
23251         }
23252       if (seen_open_paren)
23253         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23254       else
23255         {
23256           /* If there was no open parenthesis, we are recovering from
23257              an error, and we are trying to figure out what mistake
23258              the user has made.  */
23259
23260           /* If there is an immediate closing parenthesis, the user
23261              probably forgot the opening one (ie, they typed "@catch
23262              NSException *e)".  Parse the closing parenthesis and keep
23263              going.  */
23264           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23265             cp_lexer_consume_token (parser->lexer);
23266           
23267           /* If these is no immediate closing parenthesis, the user
23268              probably doesn't know that parenthesis are required at
23269              all (ie, they typed "@catch NSException *e").  So, just
23270              forget about the closing parenthesis and keep going.  */
23271         }
23272       objc_begin_catch_clause (parameter_declaration);
23273       cp_parser_compound_statement (parser, NULL, false, false);
23274       objc_finish_catch_clause ();
23275     }
23276   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23277     {
23278       cp_lexer_consume_token (parser->lexer);
23279       location = cp_lexer_peek_token (parser->lexer)->location;
23280       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23281          node, lest it get absorbed into the surrounding block.  */
23282       stmt = push_stmt_list ();
23283       cp_parser_compound_statement (parser, NULL, false, false);
23284       objc_build_finally_clause (location, pop_stmt_list (stmt));
23285     }
23286
23287   return objc_finish_try_stmt ();
23288 }
23289
23290 /* Parse an Objective-C synchronized statement.
23291
23292    objc-synchronized-stmt:
23293      @synchronized ( expression ) compound-statement
23294
23295    Returns NULL_TREE.  */
23296
23297 static tree
23298 cp_parser_objc_synchronized_statement (cp_parser *parser)
23299 {
23300   location_t location;
23301   tree lock, stmt;
23302
23303   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23304
23305   location = cp_lexer_peek_token (parser->lexer)->location;
23306   objc_maybe_warn_exceptions (location);
23307   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23308   lock = cp_parser_expression (parser, false, NULL);
23309   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23310
23311   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23312      node, lest it get absorbed into the surrounding block.  */
23313   stmt = push_stmt_list ();
23314   cp_parser_compound_statement (parser, NULL, false, false);
23315
23316   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23317 }
23318
23319 /* Parse an Objective-C throw statement.
23320
23321    objc-throw-stmt:
23322      @throw assignment-expression [opt] ;
23323
23324    Returns a constructed '@throw' statement.  */
23325
23326 static tree
23327 cp_parser_objc_throw_statement (cp_parser *parser)
23328 {
23329   tree expr = NULL_TREE;
23330   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23331
23332   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23333
23334   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23335     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23336
23337   cp_parser_consume_semicolon_at_end_of_statement (parser);
23338
23339   return objc_build_throw_stmt (loc, expr);
23340 }
23341
23342 /* Parse an Objective-C statement.  */
23343
23344 static tree
23345 cp_parser_objc_statement (cp_parser * parser)
23346 {
23347   /* Try to figure out what kind of declaration is present.  */
23348   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23349
23350   switch (kwd->keyword)
23351     {
23352     case RID_AT_TRY:
23353       return cp_parser_objc_try_catch_finally_statement (parser);
23354     case RID_AT_SYNCHRONIZED:
23355       return cp_parser_objc_synchronized_statement (parser);
23356     case RID_AT_THROW:
23357       return cp_parser_objc_throw_statement (parser);
23358     default:
23359       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23360                kwd->u.value);
23361       cp_parser_skip_to_end_of_block_or_statement (parser);
23362     }
23363
23364   return error_mark_node;
23365 }
23366
23367 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
23368    look ahead to see if an objc keyword follows the attributes.  This
23369    is to detect the use of prefix attributes on ObjC @interface and 
23370    @protocol.  */
23371
23372 static bool
23373 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23374 {
23375   cp_lexer_save_tokens (parser->lexer);
23376   *attrib = cp_parser_attributes_opt (parser);
23377   gcc_assert (*attrib);
23378   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23379     {
23380       cp_lexer_commit_tokens (parser->lexer);
23381       return true;
23382     }
23383   cp_lexer_rollback_tokens (parser->lexer);
23384   return false;  
23385 }
23386
23387 /* This routine is a minimal replacement for
23388    c_parser_struct_declaration () used when parsing the list of
23389    types/names or ObjC++ properties.  For example, when parsing the
23390    code
23391
23392    @property (readonly) int a, b, c;
23393
23394    this function is responsible for parsing "int a, int b, int c" and
23395    returning the declarations as CHAIN of DECLs.
23396
23397    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23398    similar parsing.  */
23399 static tree
23400 cp_parser_objc_struct_declaration (cp_parser *parser)
23401 {
23402   tree decls = NULL_TREE;
23403   cp_decl_specifier_seq declspecs;
23404   int decl_class_or_enum_p;
23405   tree prefix_attributes;
23406
23407   cp_parser_decl_specifier_seq (parser,
23408                                 CP_PARSER_FLAGS_NONE,
23409                                 &declspecs,
23410                                 &decl_class_or_enum_p);
23411
23412   if (declspecs.type == error_mark_node)
23413     return error_mark_node;
23414
23415   /* auto, register, static, extern, mutable.  */
23416   if (declspecs.storage_class != sc_none)
23417     {
23418       cp_parser_error (parser, "invalid type for property");
23419       declspecs.storage_class = sc_none;
23420     }
23421   
23422   /* __thread.  */
23423   if (declspecs.specs[(int) ds_thread])
23424     {
23425       cp_parser_error (parser, "invalid type for property");
23426       declspecs.specs[(int) ds_thread] = 0;
23427     }
23428   
23429   /* typedef.  */
23430   if (declspecs.specs[(int) ds_typedef])
23431     {
23432       cp_parser_error (parser, "invalid type for property");
23433       declspecs.specs[(int) ds_typedef] = 0;
23434     }
23435
23436   prefix_attributes = declspecs.attributes;
23437   declspecs.attributes = NULL_TREE;
23438
23439   /* Keep going until we hit the `;' at the end of the declaration. */
23440   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23441     {
23442       tree attributes, first_attribute, decl;
23443       cp_declarator *declarator;
23444       cp_token *token;
23445
23446       /* Parse the declarator.  */
23447       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23448                                          NULL, NULL, false);
23449
23450       /* Look for attributes that apply to the ivar.  */
23451       attributes = cp_parser_attributes_opt (parser);
23452       /* Remember which attributes are prefix attributes and
23453          which are not.  */
23454       first_attribute = attributes;
23455       /* Combine the attributes.  */
23456       attributes = chainon (prefix_attributes, attributes);
23457       
23458       decl = grokfield (declarator, &declspecs,
23459                         NULL_TREE, /*init_const_expr_p=*/false,
23460                         NULL_TREE, attributes);
23461
23462       if (decl == error_mark_node || decl == NULL_TREE)
23463         return error_mark_node;
23464       
23465       /* Reset PREFIX_ATTRIBUTES.  */
23466       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23467         attributes = TREE_CHAIN (attributes);
23468       if (attributes)
23469         TREE_CHAIN (attributes) = NULL_TREE;
23470
23471       DECL_CHAIN (decl) = decls;
23472       decls = decl;
23473
23474       token = cp_lexer_peek_token (parser->lexer);
23475       if (token->type == CPP_COMMA)
23476         {
23477           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23478           continue;
23479         }
23480       else
23481         break;
23482     }
23483   return decls;
23484 }
23485
23486 /* Parse an Objective-C @property declaration.  The syntax is:
23487
23488    objc-property-declaration:
23489      '@property' objc-property-attributes[opt] struct-declaration ;
23490
23491    objc-property-attributes:
23492     '(' objc-property-attribute-list ')'
23493
23494    objc-property-attribute-list:
23495      objc-property-attribute
23496      objc-property-attribute-list, objc-property-attribute
23497
23498    objc-property-attribute
23499      'getter' = identifier
23500      'setter' = identifier
23501      'readonly'
23502      'readwrite'
23503      'assign'
23504      'retain'
23505      'copy'
23506      'nonatomic'
23507
23508   For example:
23509     @property NSString *name;
23510     @property (readonly) id object;
23511     @property (retain, nonatomic, getter=getTheName) id name;
23512     @property int a, b, c;
23513
23514    PS: This function is identical to
23515    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23516 static void 
23517 cp_parser_objc_at_property_declaration (cp_parser *parser)
23518 {
23519   /* The following variables hold the attributes of the properties as
23520      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23521      seen.  When we see an attribute, we set them to 'true' (if they
23522      are boolean properties) or to the identifier (if they have an
23523      argument, ie, for getter and setter).  Note that here we only
23524      parse the list of attributes, check the syntax and accumulate the
23525      attributes that we find.  objc_add_property_declaration() will
23526      then process the information.  */
23527   bool property_assign = false;
23528   bool property_copy = false;
23529   tree property_getter_ident = NULL_TREE;
23530   bool property_nonatomic = false;
23531   bool property_readonly = false;
23532   bool property_readwrite = false;
23533   bool property_retain = false;
23534   tree property_setter_ident = NULL_TREE;
23535
23536   /* 'properties' is the list of properties that we read.  Usually a
23537      single one, but maybe more (eg, in "@property int a, b, c;" there
23538      are three).  */
23539   tree properties;
23540   location_t loc;
23541
23542   loc = cp_lexer_peek_token (parser->lexer)->location;
23543
23544   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23545
23546   /* Parse the optional attribute list...  */
23547   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23548     {
23549       /* Eat the '('.  */
23550       cp_lexer_consume_token (parser->lexer);
23551
23552       while (true)
23553         {
23554           bool syntax_error = false;
23555           cp_token *token = cp_lexer_peek_token (parser->lexer);
23556           enum rid keyword;
23557
23558           if (token->type != CPP_NAME)
23559             {
23560               cp_parser_error (parser, "expected identifier");
23561               break;
23562             }
23563           keyword = C_RID_CODE (token->u.value);
23564           cp_lexer_consume_token (parser->lexer);
23565           switch (keyword)
23566             {
23567             case RID_ASSIGN:    property_assign = true;    break;
23568             case RID_COPY:      property_copy = true;      break;
23569             case RID_NONATOMIC: property_nonatomic = true; break;
23570             case RID_READONLY:  property_readonly = true;  break;
23571             case RID_READWRITE: property_readwrite = true; break;
23572             case RID_RETAIN:    property_retain = true;    break;
23573
23574             case RID_GETTER:
23575             case RID_SETTER:
23576               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23577                 {
23578                   if (keyword == RID_GETTER)
23579                     cp_parser_error (parser,
23580                                      "missing %<=%> (after %<getter%> attribute)");
23581                   else
23582                     cp_parser_error (parser,
23583                                      "missing %<=%> (after %<setter%> attribute)");
23584                   syntax_error = true;
23585                   break;
23586                 }
23587               cp_lexer_consume_token (parser->lexer); /* eat the = */
23588               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23589                 {
23590                   cp_parser_error (parser, "expected identifier");
23591                   syntax_error = true;
23592                   break;
23593                 }
23594               if (keyword == RID_SETTER)
23595                 {
23596                   if (property_setter_ident != NULL_TREE)
23597                     {
23598                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23599                       cp_lexer_consume_token (parser->lexer);
23600                     }
23601                   else
23602                     property_setter_ident = cp_parser_objc_selector (parser);
23603                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23604                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23605                   else
23606                     cp_lexer_consume_token (parser->lexer);
23607                 }
23608               else
23609                 {
23610                   if (property_getter_ident != NULL_TREE)
23611                     {
23612                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23613                       cp_lexer_consume_token (parser->lexer);
23614                     }
23615                   else
23616                     property_getter_ident = cp_parser_objc_selector (parser);
23617                 }
23618               break;
23619             default:
23620               cp_parser_error (parser, "unknown property attribute");
23621               syntax_error = true;
23622               break;
23623             }
23624
23625           if (syntax_error)
23626             break;
23627
23628           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23629             cp_lexer_consume_token (parser->lexer);
23630           else
23631             break;
23632         }
23633
23634       /* FIXME: "@property (setter, assign);" will generate a spurious
23635          "error: expected â€˜)’ before â€˜,’ token".  This is because
23636          cp_parser_require, unlike the C counterpart, will produce an
23637          error even if we are in error recovery.  */
23638       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23639         {
23640           cp_parser_skip_to_closing_parenthesis (parser,
23641                                                  /*recovering=*/true,
23642                                                  /*or_comma=*/false,
23643                                                  /*consume_paren=*/true);
23644         }
23645     }
23646
23647   /* ... and the property declaration(s).  */
23648   properties = cp_parser_objc_struct_declaration (parser);
23649
23650   if (properties == error_mark_node)
23651     {
23652       cp_parser_skip_to_end_of_statement (parser);
23653       /* If the next token is now a `;', consume it.  */
23654       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23655         cp_lexer_consume_token (parser->lexer);
23656       return;
23657     }
23658
23659   if (properties == NULL_TREE)
23660     cp_parser_error (parser, "expected identifier");
23661   else
23662     {
23663       /* Comma-separated properties are chained together in
23664          reverse order; add them one by one.  */
23665       properties = nreverse (properties);
23666       
23667       for (; properties; properties = TREE_CHAIN (properties))
23668         objc_add_property_declaration (loc, copy_node (properties),
23669                                        property_readonly, property_readwrite,
23670                                        property_assign, property_retain,
23671                                        property_copy, property_nonatomic,
23672                                        property_getter_ident, property_setter_ident);
23673     }
23674   
23675   cp_parser_consume_semicolon_at_end_of_statement (parser);
23676 }
23677
23678 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23679
23680    objc-synthesize-declaration:
23681      @synthesize objc-synthesize-identifier-list ;
23682
23683    objc-synthesize-identifier-list:
23684      objc-synthesize-identifier
23685      objc-synthesize-identifier-list, objc-synthesize-identifier
23686
23687    objc-synthesize-identifier
23688      identifier
23689      identifier = identifier
23690
23691   For example:
23692     @synthesize MyProperty;
23693     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23694
23695   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23696   for C.  Keep them in sync.
23697 */
23698 static void 
23699 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23700 {
23701   tree list = NULL_TREE;
23702   location_t loc;
23703   loc = cp_lexer_peek_token (parser->lexer)->location;
23704
23705   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23706   while (true)
23707     {
23708       tree property, ivar;
23709       property = cp_parser_identifier (parser);
23710       if (property == error_mark_node)
23711         {
23712           cp_parser_consume_semicolon_at_end_of_statement (parser);
23713           return;
23714         }
23715       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23716         {
23717           cp_lexer_consume_token (parser->lexer);
23718           ivar = cp_parser_identifier (parser);
23719           if (ivar == error_mark_node)
23720             {
23721               cp_parser_consume_semicolon_at_end_of_statement (parser);
23722               return;
23723             }
23724         }
23725       else
23726         ivar = NULL_TREE;
23727       list = chainon (list, build_tree_list (ivar, property));
23728       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23729         cp_lexer_consume_token (parser->lexer);
23730       else
23731         break;
23732     }
23733   cp_parser_consume_semicolon_at_end_of_statement (parser);
23734   objc_add_synthesize_declaration (loc, list);
23735 }
23736
23737 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23738
23739    objc-dynamic-declaration:
23740      @dynamic identifier-list ;
23741
23742    For example:
23743      @dynamic MyProperty;
23744      @dynamic MyProperty, AnotherProperty;
23745
23746   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23747   for C.  Keep them in sync.
23748 */
23749 static void 
23750 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23751 {
23752   tree list = NULL_TREE;
23753   location_t loc;
23754   loc = cp_lexer_peek_token (parser->lexer)->location;
23755
23756   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23757   while (true)
23758     {
23759       tree property;
23760       property = cp_parser_identifier (parser);
23761       if (property == error_mark_node)
23762         {
23763           cp_parser_consume_semicolon_at_end_of_statement (parser);
23764           return;
23765         }
23766       list = chainon (list, build_tree_list (NULL, property));
23767       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23768         cp_lexer_consume_token (parser->lexer);
23769       else
23770         break;
23771     }
23772   cp_parser_consume_semicolon_at_end_of_statement (parser);
23773   objc_add_dynamic_declaration (loc, list);
23774 }
23775
23776 \f
23777 /* OpenMP 2.5 parsing routines.  */
23778
23779 /* Returns name of the next clause.
23780    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23781    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23782    returned and the token is consumed.  */
23783
23784 static pragma_omp_clause
23785 cp_parser_omp_clause_name (cp_parser *parser)
23786 {
23787   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23788
23789   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23790     result = PRAGMA_OMP_CLAUSE_IF;
23791   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23792     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23793   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23794     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23795   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23796     {
23797       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23798       const char *p = IDENTIFIER_POINTER (id);
23799
23800       switch (p[0])
23801         {
23802         case 'c':
23803           if (!strcmp ("collapse", p))
23804             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23805           else if (!strcmp ("copyin", p))
23806             result = PRAGMA_OMP_CLAUSE_COPYIN;
23807           else if (!strcmp ("copyprivate", p))
23808             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23809           break;
23810         case 'f':
23811           if (!strcmp ("final", p))
23812             result = PRAGMA_OMP_CLAUSE_FINAL;
23813           else if (!strcmp ("firstprivate", p))
23814             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23815           break;
23816         case 'l':
23817           if (!strcmp ("lastprivate", p))
23818             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23819           break;
23820         case 'm':
23821           if (!strcmp ("mergeable", p))
23822             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
23823           break;
23824         case 'n':
23825           if (!strcmp ("nowait", p))
23826             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23827           else if (!strcmp ("num_threads", p))
23828             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23829           break;
23830         case 'o':
23831           if (!strcmp ("ordered", p))
23832             result = PRAGMA_OMP_CLAUSE_ORDERED;
23833           break;
23834         case 'r':
23835           if (!strcmp ("reduction", p))
23836             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23837           break;
23838         case 's':
23839           if (!strcmp ("schedule", p))
23840             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23841           else if (!strcmp ("shared", p))
23842             result = PRAGMA_OMP_CLAUSE_SHARED;
23843           break;
23844         case 'u':
23845           if (!strcmp ("untied", p))
23846             result = PRAGMA_OMP_CLAUSE_UNTIED;
23847           break;
23848         }
23849     }
23850
23851   if (result != PRAGMA_OMP_CLAUSE_NONE)
23852     cp_lexer_consume_token (parser->lexer);
23853
23854   return result;
23855 }
23856
23857 /* Validate that a clause of the given type does not already exist.  */
23858
23859 static void
23860 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23861                            const char *name, location_t location)
23862 {
23863   tree c;
23864
23865   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23866     if (OMP_CLAUSE_CODE (c) == code)
23867       {
23868         error_at (location, "too many %qs clauses", name);
23869         break;
23870       }
23871 }
23872
23873 /* OpenMP 2.5:
23874    variable-list:
23875      identifier
23876      variable-list , identifier
23877
23878    In addition, we match a closing parenthesis.  An opening parenthesis
23879    will have been consumed by the caller.
23880
23881    If KIND is nonzero, create the appropriate node and install the decl
23882    in OMP_CLAUSE_DECL and add the node to the head of the list.
23883
23884    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23885    return the list created.  */
23886
23887 static tree
23888 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23889                                 tree list)
23890 {
23891   cp_token *token;
23892   while (1)
23893     {
23894       tree name, decl;
23895
23896       token = cp_lexer_peek_token (parser->lexer);
23897       name = cp_parser_id_expression (parser, /*template_p=*/false,
23898                                       /*check_dependency_p=*/true,
23899                                       /*template_p=*/NULL,
23900                                       /*declarator_p=*/false,
23901                                       /*optional_p=*/false);
23902       if (name == error_mark_node)
23903         goto skip_comma;
23904
23905       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23906       if (decl == error_mark_node)
23907         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23908                                      token->location);
23909       else if (kind != 0)
23910         {
23911           tree u = build_omp_clause (token->location, kind);
23912           OMP_CLAUSE_DECL (u) = decl;
23913           OMP_CLAUSE_CHAIN (u) = list;
23914           list = u;
23915         }
23916       else
23917         list = tree_cons (decl, NULL_TREE, list);
23918
23919     get_comma:
23920       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23921         break;
23922       cp_lexer_consume_token (parser->lexer);
23923     }
23924
23925   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23926     {
23927       int ending;
23928
23929       /* Try to resync to an unnested comma.  Copied from
23930          cp_parser_parenthesized_expression_list.  */
23931     skip_comma:
23932       ending = cp_parser_skip_to_closing_parenthesis (parser,
23933                                                       /*recovering=*/true,
23934                                                       /*or_comma=*/true,
23935                                                       /*consume_paren=*/true);
23936       if (ending < 0)
23937         goto get_comma;
23938     }
23939
23940   return list;
23941 }
23942
23943 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23944    common case for omp clauses.  */
23945
23946 static tree
23947 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23948 {
23949   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23950     return cp_parser_omp_var_list_no_open (parser, kind, list);
23951   return list;
23952 }
23953
23954 /* OpenMP 3.0:
23955    collapse ( constant-expression ) */
23956
23957 static tree
23958 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23959 {
23960   tree c, num;
23961   location_t loc;
23962   HOST_WIDE_INT n;
23963
23964   loc = cp_lexer_peek_token (parser->lexer)->location;
23965   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23966     return list;
23967
23968   num = cp_parser_constant_expression (parser, false, NULL);
23969
23970   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23971     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23972                                            /*or_comma=*/false,
23973                                            /*consume_paren=*/true);
23974
23975   if (num == error_mark_node)
23976     return list;
23977   num = fold_non_dependent_expr (num);
23978   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23979       || !host_integerp (num, 0)
23980       || (n = tree_low_cst (num, 0)) <= 0
23981       || (int) n != n)
23982     {
23983       error_at (loc, "collapse argument needs positive constant integer expression");
23984       return list;
23985     }
23986
23987   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23988   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23989   OMP_CLAUSE_CHAIN (c) = list;
23990   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23991
23992   return c;
23993 }
23994
23995 /* OpenMP 2.5:
23996    default ( shared | none ) */
23997
23998 static tree
23999 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24000 {
24001   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24002   tree c;
24003
24004   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24005     return list;
24006   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24007     {
24008       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24009       const char *p = IDENTIFIER_POINTER (id);
24010
24011       switch (p[0])
24012         {
24013         case 'n':
24014           if (strcmp ("none", p) != 0)
24015             goto invalid_kind;
24016           kind = OMP_CLAUSE_DEFAULT_NONE;
24017           break;
24018
24019         case 's':
24020           if (strcmp ("shared", p) != 0)
24021             goto invalid_kind;
24022           kind = OMP_CLAUSE_DEFAULT_SHARED;
24023           break;
24024
24025         default:
24026           goto invalid_kind;
24027         }
24028
24029       cp_lexer_consume_token (parser->lexer);
24030     }
24031   else
24032     {
24033     invalid_kind:
24034       cp_parser_error (parser, "expected %<none%> or %<shared%>");
24035     }
24036
24037   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24038     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24039                                            /*or_comma=*/false,
24040                                            /*consume_paren=*/true);
24041
24042   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24043     return list;
24044
24045   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24046   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24047   OMP_CLAUSE_CHAIN (c) = list;
24048   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24049
24050   return c;
24051 }
24052
24053 /* OpenMP 3.1:
24054    final ( expression ) */
24055
24056 static tree
24057 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24058 {
24059   tree t, c;
24060
24061   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24062     return list;
24063
24064   t = cp_parser_condition (parser);
24065
24066   if (t == error_mark_node
24067       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24068     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24069                                            /*or_comma=*/false,
24070                                            /*consume_paren=*/true);
24071
24072   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24073
24074   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24075   OMP_CLAUSE_FINAL_EXPR (c) = t;
24076   OMP_CLAUSE_CHAIN (c) = list;
24077
24078   return c;
24079 }
24080
24081 /* OpenMP 2.5:
24082    if ( expression ) */
24083
24084 static tree
24085 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24086 {
24087   tree t, c;
24088
24089   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24090     return list;
24091
24092   t = cp_parser_condition (parser);
24093
24094   if (t == error_mark_node
24095       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24096     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24097                                            /*or_comma=*/false,
24098                                            /*consume_paren=*/true);
24099
24100   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24101
24102   c = build_omp_clause (location, OMP_CLAUSE_IF);
24103   OMP_CLAUSE_IF_EXPR (c) = t;
24104   OMP_CLAUSE_CHAIN (c) = list;
24105
24106   return c;
24107 }
24108
24109 /* OpenMP 3.1:
24110    mergeable */
24111
24112 static tree
24113 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24114                                 tree list, location_t location)
24115 {
24116   tree c;
24117
24118   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24119                              location);
24120
24121   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24122   OMP_CLAUSE_CHAIN (c) = list;
24123   return c;
24124 }
24125
24126 /* OpenMP 2.5:
24127    nowait */
24128
24129 static tree
24130 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24131                              tree list, location_t location)
24132 {
24133   tree c;
24134
24135   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24136
24137   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24138   OMP_CLAUSE_CHAIN (c) = list;
24139   return c;
24140 }
24141
24142 /* OpenMP 2.5:
24143    num_threads ( expression ) */
24144
24145 static tree
24146 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24147                                   location_t location)
24148 {
24149   tree t, c;
24150
24151   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24152     return list;
24153
24154   t = cp_parser_expression (parser, false, NULL);
24155
24156   if (t == error_mark_node
24157       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24158     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24159                                            /*or_comma=*/false,
24160                                            /*consume_paren=*/true);
24161
24162   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24163                              "num_threads", location);
24164
24165   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24166   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24167   OMP_CLAUSE_CHAIN (c) = list;
24168
24169   return c;
24170 }
24171
24172 /* OpenMP 2.5:
24173    ordered */
24174
24175 static tree
24176 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24177                               tree list, location_t location)
24178 {
24179   tree c;
24180
24181   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24182                              "ordered", location);
24183
24184   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24185   OMP_CLAUSE_CHAIN (c) = list;
24186   return c;
24187 }
24188
24189 /* OpenMP 2.5:
24190    reduction ( reduction-operator : variable-list )
24191
24192    reduction-operator:
24193      One of: + * - & ^ | && ||
24194
24195    OpenMP 3.1:
24196
24197    reduction-operator:
24198      One of: + * - & ^ | && || min max  */
24199
24200 static tree
24201 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24202 {
24203   enum tree_code code;
24204   tree nlist, c;
24205
24206   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24207     return list;
24208
24209   switch (cp_lexer_peek_token (parser->lexer)->type)
24210     {
24211     case CPP_PLUS:
24212       code = PLUS_EXPR;
24213       break;
24214     case CPP_MULT:
24215       code = MULT_EXPR;
24216       break;
24217     case CPP_MINUS:
24218       code = MINUS_EXPR;
24219       break;
24220     case CPP_AND:
24221       code = BIT_AND_EXPR;
24222       break;
24223     case CPP_XOR:
24224       code = BIT_XOR_EXPR;
24225       break;
24226     case CPP_OR:
24227       code = BIT_IOR_EXPR;
24228       break;
24229     case CPP_AND_AND:
24230       code = TRUTH_ANDIF_EXPR;
24231       break;
24232     case CPP_OR_OR:
24233       code = TRUTH_ORIF_EXPR;
24234       break;
24235     case CPP_NAME:
24236       {
24237         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24238         const char *p = IDENTIFIER_POINTER (id);
24239
24240         if (strcmp (p, "min") == 0)
24241           {
24242             code = MIN_EXPR;
24243             break;
24244           }
24245         if (strcmp (p, "max") == 0)
24246           {
24247             code = MAX_EXPR;
24248             break;
24249           }
24250       }
24251       /* FALLTHROUGH */
24252     default:
24253       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24254                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24255     resync_fail:
24256       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24257                                              /*or_comma=*/false,
24258                                              /*consume_paren=*/true);
24259       return list;
24260     }
24261   cp_lexer_consume_token (parser->lexer);
24262
24263   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24264     goto resync_fail;
24265
24266   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24267   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24268     OMP_CLAUSE_REDUCTION_CODE (c) = code;
24269
24270   return nlist;
24271 }
24272
24273 /* OpenMP 2.5:
24274    schedule ( schedule-kind )
24275    schedule ( schedule-kind , expression )
24276
24277    schedule-kind:
24278      static | dynamic | guided | runtime | auto  */
24279
24280 static tree
24281 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24282 {
24283   tree c, t;
24284
24285   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24286     return list;
24287
24288   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24289
24290   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24291     {
24292       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24293       const char *p = IDENTIFIER_POINTER (id);
24294
24295       switch (p[0])
24296         {
24297         case 'd':
24298           if (strcmp ("dynamic", p) != 0)
24299             goto invalid_kind;
24300           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24301           break;
24302
24303         case 'g':
24304           if (strcmp ("guided", p) != 0)
24305             goto invalid_kind;
24306           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24307           break;
24308
24309         case 'r':
24310           if (strcmp ("runtime", p) != 0)
24311             goto invalid_kind;
24312           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24313           break;
24314
24315         default:
24316           goto invalid_kind;
24317         }
24318     }
24319   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24320     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24321   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24322     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24323   else
24324     goto invalid_kind;
24325   cp_lexer_consume_token (parser->lexer);
24326
24327   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24328     {
24329       cp_token *token;
24330       cp_lexer_consume_token (parser->lexer);
24331
24332       token = cp_lexer_peek_token (parser->lexer);
24333       t = cp_parser_assignment_expression (parser, false, NULL);
24334
24335       if (t == error_mark_node)
24336         goto resync_fail;
24337       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24338         error_at (token->location, "schedule %<runtime%> does not take "
24339                   "a %<chunk_size%> parameter");
24340       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24341         error_at (token->location, "schedule %<auto%> does not take "
24342                   "a %<chunk_size%> parameter");
24343       else
24344         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24345
24346       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24347         goto resync_fail;
24348     }
24349   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
24350     goto resync_fail;
24351
24352   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
24353   OMP_CLAUSE_CHAIN (c) = list;
24354   return c;
24355
24356  invalid_kind:
24357   cp_parser_error (parser, "invalid schedule kind");
24358  resync_fail:
24359   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24360                                          /*or_comma=*/false,
24361                                          /*consume_paren=*/true);
24362   return list;
24363 }
24364
24365 /* OpenMP 3.0:
24366    untied */
24367
24368 static tree
24369 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
24370                              tree list, location_t location)
24371 {
24372   tree c;
24373
24374   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
24375
24376   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
24377   OMP_CLAUSE_CHAIN (c) = list;
24378   return c;
24379 }
24380
24381 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
24382    is a bitmask in MASK.  Return the list of clauses found; the result
24383    of clause default goes in *pdefault.  */
24384
24385 static tree
24386 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
24387                            const char *where, cp_token *pragma_tok)
24388 {
24389   tree clauses = NULL;
24390   bool first = true;
24391   cp_token *token = NULL;
24392
24393   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
24394     {
24395       pragma_omp_clause c_kind;
24396       const char *c_name;
24397       tree prev = clauses;
24398
24399       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24400         cp_lexer_consume_token (parser->lexer);
24401
24402       token = cp_lexer_peek_token (parser->lexer);
24403       c_kind = cp_parser_omp_clause_name (parser);
24404       first = false;
24405
24406       switch (c_kind)
24407         {
24408         case PRAGMA_OMP_CLAUSE_COLLAPSE:
24409           clauses = cp_parser_omp_clause_collapse (parser, clauses,
24410                                                    token->location);
24411           c_name = "collapse";
24412           break;
24413         case PRAGMA_OMP_CLAUSE_COPYIN:
24414           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
24415           c_name = "copyin";
24416           break;
24417         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
24418           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
24419                                             clauses);
24420           c_name = "copyprivate";
24421           break;
24422         case PRAGMA_OMP_CLAUSE_DEFAULT:
24423           clauses = cp_parser_omp_clause_default (parser, clauses,
24424                                                   token->location);
24425           c_name = "default";
24426           break;
24427         case PRAGMA_OMP_CLAUSE_FINAL:
24428           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
24429           c_name = "final";
24430           break;
24431         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
24432           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
24433                                             clauses);
24434           c_name = "firstprivate";
24435           break;
24436         case PRAGMA_OMP_CLAUSE_IF:
24437           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
24438           c_name = "if";
24439           break;
24440         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24441           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24442                                             clauses);
24443           c_name = "lastprivate";
24444           break;
24445         case PRAGMA_OMP_CLAUSE_MERGEABLE:
24446           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
24447                                                     token->location);
24448           c_name = "mergeable";
24449           break;
24450         case PRAGMA_OMP_CLAUSE_NOWAIT:
24451           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24452           c_name = "nowait";
24453           break;
24454         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24455           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24456                                                       token->location);
24457           c_name = "num_threads";
24458           break;
24459         case PRAGMA_OMP_CLAUSE_ORDERED:
24460           clauses = cp_parser_omp_clause_ordered (parser, clauses,
24461                                                   token->location);
24462           c_name = "ordered";
24463           break;
24464         case PRAGMA_OMP_CLAUSE_PRIVATE:
24465           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24466                                             clauses);
24467           c_name = "private";
24468           break;
24469         case PRAGMA_OMP_CLAUSE_REDUCTION:
24470           clauses = cp_parser_omp_clause_reduction (parser, clauses);
24471           c_name = "reduction";
24472           break;
24473         case PRAGMA_OMP_CLAUSE_SCHEDULE:
24474           clauses = cp_parser_omp_clause_schedule (parser, clauses,
24475                                                    token->location);
24476           c_name = "schedule";
24477           break;
24478         case PRAGMA_OMP_CLAUSE_SHARED:
24479           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24480                                             clauses);
24481           c_name = "shared";
24482           break;
24483         case PRAGMA_OMP_CLAUSE_UNTIED:
24484           clauses = cp_parser_omp_clause_untied (parser, clauses,
24485                                                  token->location);
24486           c_name = "nowait";
24487           break;
24488         default:
24489           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24490           goto saw_error;
24491         }
24492
24493       if (((mask >> c_kind) & 1) == 0)
24494         {
24495           /* Remove the invalid clause(s) from the list to avoid
24496              confusing the rest of the compiler.  */
24497           clauses = prev;
24498           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24499         }
24500     }
24501  saw_error:
24502   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24503   return finish_omp_clauses (clauses);
24504 }
24505
24506 /* OpenMP 2.5:
24507    structured-block:
24508      statement
24509
24510    In practice, we're also interested in adding the statement to an
24511    outer node.  So it is convenient if we work around the fact that
24512    cp_parser_statement calls add_stmt.  */
24513
24514 static unsigned
24515 cp_parser_begin_omp_structured_block (cp_parser *parser)
24516 {
24517   unsigned save = parser->in_statement;
24518
24519   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24520      This preserves the "not within loop or switch" style error messages
24521      for nonsense cases like
24522         void foo() {
24523         #pragma omp single
24524           break;
24525         }
24526   */
24527   if (parser->in_statement)
24528     parser->in_statement = IN_OMP_BLOCK;
24529
24530   return save;
24531 }
24532
24533 static void
24534 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24535 {
24536   parser->in_statement = save;
24537 }
24538
24539 static tree
24540 cp_parser_omp_structured_block (cp_parser *parser)
24541 {
24542   tree stmt = begin_omp_structured_block ();
24543   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24544
24545   cp_parser_statement (parser, NULL_TREE, false, NULL);
24546
24547   cp_parser_end_omp_structured_block (parser, save);
24548   return finish_omp_structured_block (stmt);
24549 }
24550
24551 /* OpenMP 2.5:
24552    # pragma omp atomic new-line
24553      expression-stmt
24554
24555    expression-stmt:
24556      x binop= expr | x++ | ++x | x-- | --x
24557    binop:
24558      +, *, -, /, &, ^, |, <<, >>
24559
24560   where x is an lvalue expression with scalar type.
24561
24562    OpenMP 3.1:
24563    # pragma omp atomic new-line
24564      update-stmt
24565
24566    # pragma omp atomic read new-line
24567      read-stmt
24568
24569    # pragma omp atomic write new-line
24570      write-stmt
24571
24572    # pragma omp atomic update new-line
24573      update-stmt
24574
24575    # pragma omp atomic capture new-line
24576      capture-stmt
24577
24578    # pragma omp atomic capture new-line
24579      capture-block
24580
24581    read-stmt:
24582      v = x
24583    write-stmt:
24584      x = expr
24585    update-stmt:
24586      expression-stmt | x = x binop expr
24587    capture-stmt:
24588      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
24589    capture-block:
24590      { v = x; update-stmt; } | { update-stmt; v = x; }
24591
24592   where x and v are lvalue expressions with scalar type.  */
24593
24594 static void
24595 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24596 {
24597   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
24598   tree rhs1 = NULL_TREE, orig_lhs;
24599   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
24600   bool structured_block = false;
24601
24602   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24603     {
24604       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24605       const char *p = IDENTIFIER_POINTER (id);
24606
24607       if (!strcmp (p, "read"))
24608         code = OMP_ATOMIC_READ;
24609       else if (!strcmp (p, "write"))
24610         code = NOP_EXPR;
24611       else if (!strcmp (p, "update"))
24612         code = OMP_ATOMIC;
24613       else if (!strcmp (p, "capture"))
24614         code = OMP_ATOMIC_CAPTURE_NEW;
24615       else
24616         p = NULL;
24617       if (p)
24618         cp_lexer_consume_token (parser->lexer);
24619     }
24620   cp_parser_require_pragma_eol (parser, pragma_tok);
24621
24622   switch (code)
24623     {
24624     case OMP_ATOMIC_READ:
24625     case NOP_EXPR: /* atomic write */
24626       v = cp_parser_unary_expression (parser, /*address_p=*/false,
24627                                       /*cast_p=*/false, NULL);
24628       if (v == error_mark_node)
24629         goto saw_error;
24630       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24631         goto saw_error;
24632       if (code == NOP_EXPR)
24633         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24634       else
24635         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24636                                           /*cast_p=*/false, NULL);
24637       if (lhs == error_mark_node)
24638         goto saw_error;
24639       if (code == NOP_EXPR)
24640         {
24641           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
24642              opcode.  */
24643           code = OMP_ATOMIC;
24644           rhs = lhs;
24645           lhs = v;
24646           v = NULL_TREE;
24647         }
24648       goto done;
24649     case OMP_ATOMIC_CAPTURE_NEW:
24650       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24651         {
24652           cp_lexer_consume_token (parser->lexer);
24653           structured_block = true;
24654         }
24655       else
24656         {
24657           v = cp_parser_unary_expression (parser, /*address_p=*/false,
24658                                           /*cast_p=*/false, NULL);
24659           if (v == error_mark_node)
24660             goto saw_error;
24661           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24662             goto saw_error;
24663         }
24664     default:
24665       break;
24666     }
24667
24668 restart:
24669   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24670                                     /*cast_p=*/false, NULL);
24671   orig_lhs = lhs;
24672   switch (TREE_CODE (lhs))
24673     {
24674     case ERROR_MARK:
24675       goto saw_error;
24676
24677     case POSTINCREMENT_EXPR:
24678       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24679         code = OMP_ATOMIC_CAPTURE_OLD;
24680       /* FALLTHROUGH */
24681     case PREINCREMENT_EXPR:
24682       lhs = TREE_OPERAND (lhs, 0);
24683       opcode = PLUS_EXPR;
24684       rhs = integer_one_node;
24685       break;
24686
24687     case POSTDECREMENT_EXPR:
24688       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24689         code = OMP_ATOMIC_CAPTURE_OLD;
24690       /* FALLTHROUGH */
24691     case PREDECREMENT_EXPR:
24692       lhs = TREE_OPERAND (lhs, 0);
24693       opcode = MINUS_EXPR;
24694       rhs = integer_one_node;
24695       break;
24696
24697     case COMPOUND_EXPR:
24698       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24699          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24700          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24701          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24702          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24703                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24704             == BOOLEAN_TYPE)
24705        /* Undo effects of boolean_increment for post {in,de}crement.  */
24706        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24707       /* FALLTHRU */
24708     case MODIFY_EXPR:
24709       if (TREE_CODE (lhs) == MODIFY_EXPR
24710          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24711         {
24712           /* Undo effects of boolean_increment.  */
24713           if (integer_onep (TREE_OPERAND (lhs, 1)))
24714             {
24715               /* This is pre or post increment.  */
24716               rhs = TREE_OPERAND (lhs, 1);
24717               lhs = TREE_OPERAND (lhs, 0);
24718               opcode = NOP_EXPR;
24719               if (code == OMP_ATOMIC_CAPTURE_NEW
24720                   && !structured_block
24721                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
24722                 code = OMP_ATOMIC_CAPTURE_OLD;
24723               break;
24724             }
24725         }
24726       /* FALLTHRU */
24727     default:
24728       switch (cp_lexer_peek_token (parser->lexer)->type)
24729         {
24730         case CPP_MULT_EQ:
24731           opcode = MULT_EXPR;
24732           break;
24733         case CPP_DIV_EQ:
24734           opcode = TRUNC_DIV_EXPR;
24735           break;
24736         case CPP_PLUS_EQ:
24737           opcode = PLUS_EXPR;
24738           break;
24739         case CPP_MINUS_EQ:
24740           opcode = MINUS_EXPR;
24741           break;
24742         case CPP_LSHIFT_EQ:
24743           opcode = LSHIFT_EXPR;
24744           break;
24745         case CPP_RSHIFT_EQ:
24746           opcode = RSHIFT_EXPR;
24747           break;
24748         case CPP_AND_EQ:
24749           opcode = BIT_AND_EXPR;
24750           break;
24751         case CPP_OR_EQ:
24752           opcode = BIT_IOR_EXPR;
24753           break;
24754         case CPP_XOR_EQ:
24755           opcode = BIT_XOR_EXPR;
24756           break;
24757         case CPP_EQ:
24758           if (structured_block || code == OMP_ATOMIC)
24759             {
24760               enum cp_parser_prec oprec;
24761               cp_token *token;
24762               cp_lexer_consume_token (parser->lexer);
24763               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24764                                                  /*cast_p=*/false, NULL);
24765               if (rhs1 == error_mark_node)
24766                 goto saw_error;
24767               token = cp_lexer_peek_token (parser->lexer);
24768               switch (token->type)
24769                 {
24770                 case CPP_SEMICOLON:
24771                   if (code == OMP_ATOMIC_CAPTURE_NEW)
24772                     {
24773                       code = OMP_ATOMIC_CAPTURE_OLD;
24774                       v = lhs;
24775                       lhs = NULL_TREE;
24776                       lhs1 = rhs1;
24777                       rhs1 = NULL_TREE;
24778                       cp_lexer_consume_token (parser->lexer);
24779                       goto restart;
24780                     }
24781                   cp_parser_error (parser,
24782                                    "invalid form of %<#pragma omp atomic%>");
24783                   goto saw_error;
24784                 case CPP_MULT:
24785                   opcode = MULT_EXPR;
24786                   break;
24787                 case CPP_DIV:
24788                   opcode = TRUNC_DIV_EXPR;
24789                   break;
24790                 case CPP_PLUS:
24791                   opcode = PLUS_EXPR;
24792                   break;
24793                 case CPP_MINUS:
24794                   opcode = MINUS_EXPR;
24795                   break;
24796                 case CPP_LSHIFT:
24797                   opcode = LSHIFT_EXPR;
24798                   break;
24799                 case CPP_RSHIFT:
24800                   opcode = RSHIFT_EXPR;
24801                   break;
24802                 case CPP_AND:
24803                   opcode = BIT_AND_EXPR;
24804                   break;
24805                 case CPP_OR:
24806                   opcode = BIT_IOR_EXPR;
24807                   break;
24808                 case CPP_XOR:
24809                   opcode = BIT_XOR_EXPR;
24810                   break;
24811                 default:
24812                   cp_parser_error (parser,
24813                                    "invalid operator for %<#pragma omp atomic%>");
24814                   goto saw_error;
24815                 }
24816               oprec = TOKEN_PRECEDENCE (token);
24817               gcc_assert (oprec != PREC_NOT_OPERATOR);
24818               if (commutative_tree_code (opcode))
24819                 oprec = (enum cp_parser_prec) (oprec - 1);
24820               cp_lexer_consume_token (parser->lexer);
24821               rhs = cp_parser_binary_expression (parser, false, false,
24822                                                  oprec, NULL);
24823               if (rhs == error_mark_node)
24824                 goto saw_error;
24825               goto stmt_done;
24826             }
24827           /* FALLTHROUGH */
24828         default:
24829           cp_parser_error (parser,
24830                            "invalid operator for %<#pragma omp atomic%>");
24831           goto saw_error;
24832         }
24833       cp_lexer_consume_token (parser->lexer);
24834
24835       rhs = cp_parser_expression (parser, false, NULL);
24836       if (rhs == error_mark_node)
24837         goto saw_error;
24838       break;
24839     }
24840 stmt_done:
24841   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
24842     {
24843       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24844         goto saw_error;
24845       v = cp_parser_unary_expression (parser, /*address_p=*/false,
24846                                       /*cast_p=*/false, NULL);
24847       if (v == error_mark_node)
24848         goto saw_error;
24849       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24850         goto saw_error;
24851       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24852                                          /*cast_p=*/false, NULL);
24853       if (lhs1 == error_mark_node)
24854         goto saw_error;
24855     }
24856   if (structured_block)
24857     {
24858       cp_parser_consume_semicolon_at_end_of_statement (parser);
24859       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24860     }
24861 done:
24862   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
24863   if (!structured_block)
24864     cp_parser_consume_semicolon_at_end_of_statement (parser);
24865   return;
24866
24867  saw_error:
24868   cp_parser_skip_to_end_of_block_or_statement (parser);
24869   if (structured_block)
24870     {
24871       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24872         cp_lexer_consume_token (parser->lexer);
24873       else if (code == OMP_ATOMIC_CAPTURE_NEW)
24874         {
24875           cp_parser_skip_to_end_of_block_or_statement (parser);
24876           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24877             cp_lexer_consume_token (parser->lexer);
24878         }
24879     }
24880 }
24881
24882
24883 /* OpenMP 2.5:
24884    # pragma omp barrier new-line  */
24885
24886 static void
24887 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24888 {
24889   cp_parser_require_pragma_eol (parser, pragma_tok);
24890   finish_omp_barrier ();
24891 }
24892
24893 /* OpenMP 2.5:
24894    # pragma omp critical [(name)] new-line
24895      structured-block  */
24896
24897 static tree
24898 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24899 {
24900   tree stmt, name = NULL;
24901
24902   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24903     {
24904       cp_lexer_consume_token (parser->lexer);
24905
24906       name = cp_parser_identifier (parser);
24907
24908       if (name == error_mark_node
24909           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24910         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24911                                                /*or_comma=*/false,
24912                                                /*consume_paren=*/true);
24913       if (name == error_mark_node)
24914         name = NULL;
24915     }
24916   cp_parser_require_pragma_eol (parser, pragma_tok);
24917
24918   stmt = cp_parser_omp_structured_block (parser);
24919   return c_finish_omp_critical (input_location, stmt, name);
24920 }
24921
24922 /* OpenMP 2.5:
24923    # pragma omp flush flush-vars[opt] new-line
24924
24925    flush-vars:
24926      ( variable-list ) */
24927
24928 static void
24929 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24930 {
24931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24932     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24933   cp_parser_require_pragma_eol (parser, pragma_tok);
24934
24935   finish_omp_flush ();
24936 }
24937
24938 /* Helper function, to parse omp for increment expression.  */
24939
24940 static tree
24941 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24942 {
24943   tree cond = cp_parser_binary_expression (parser, false, true,
24944                                            PREC_NOT_OPERATOR, NULL);
24945   if (cond == error_mark_node
24946       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24947     {
24948       cp_parser_skip_to_end_of_statement (parser);
24949       return error_mark_node;
24950     }
24951
24952   switch (TREE_CODE (cond))
24953     {
24954     case GT_EXPR:
24955     case GE_EXPR:
24956     case LT_EXPR:
24957     case LE_EXPR:
24958       break;
24959     default:
24960       return error_mark_node;
24961     }
24962
24963   /* If decl is an iterator, preserve LHS and RHS of the relational
24964      expr until finish_omp_for.  */
24965   if (decl
24966       && (type_dependent_expression_p (decl)
24967           || CLASS_TYPE_P (TREE_TYPE (decl))))
24968     return cond;
24969
24970   return build_x_binary_op (TREE_CODE (cond),
24971                             TREE_OPERAND (cond, 0), ERROR_MARK,
24972                             TREE_OPERAND (cond, 1), ERROR_MARK,
24973                             /*overload=*/NULL, tf_warning_or_error);
24974 }
24975
24976 /* Helper function, to parse omp for increment expression.  */
24977
24978 static tree
24979 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24980 {
24981   cp_token *token = cp_lexer_peek_token (parser->lexer);
24982   enum tree_code op;
24983   tree lhs, rhs;
24984   cp_id_kind idk;
24985   bool decl_first;
24986
24987   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24988     {
24989       op = (token->type == CPP_PLUS_PLUS
24990             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24991       cp_lexer_consume_token (parser->lexer);
24992       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24993       if (lhs != decl)
24994         return error_mark_node;
24995       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24996     }
24997
24998   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24999   if (lhs != decl)
25000     return error_mark_node;
25001
25002   token = cp_lexer_peek_token (parser->lexer);
25003   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25004     {
25005       op = (token->type == CPP_PLUS_PLUS
25006             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25007       cp_lexer_consume_token (parser->lexer);
25008       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25009     }
25010
25011   op = cp_parser_assignment_operator_opt (parser);
25012   if (op == ERROR_MARK)
25013     return error_mark_node;
25014
25015   if (op != NOP_EXPR)
25016     {
25017       rhs = cp_parser_assignment_expression (parser, false, NULL);
25018       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25019       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25020     }
25021
25022   lhs = cp_parser_binary_expression (parser, false, false,
25023                                      PREC_ADDITIVE_EXPRESSION, NULL);
25024   token = cp_lexer_peek_token (parser->lexer);
25025   decl_first = lhs == decl;
25026   if (decl_first)
25027     lhs = NULL_TREE;
25028   if (token->type != CPP_PLUS
25029       && token->type != CPP_MINUS)
25030     return error_mark_node;
25031
25032   do
25033     {
25034       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25035       cp_lexer_consume_token (parser->lexer);
25036       rhs = cp_parser_binary_expression (parser, false, false,
25037                                          PREC_ADDITIVE_EXPRESSION, NULL);
25038       token = cp_lexer_peek_token (parser->lexer);
25039       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25040         {
25041           if (lhs == NULL_TREE)
25042             {
25043               if (op == PLUS_EXPR)
25044                 lhs = rhs;
25045               else
25046                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25047             }
25048           else
25049             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25050                                      NULL, tf_warning_or_error);
25051         }
25052     }
25053   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25054
25055   if (!decl_first)
25056     {
25057       if (rhs != decl || op == MINUS_EXPR)
25058         return error_mark_node;
25059       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25060     }
25061   else
25062     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25063
25064   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25065 }
25066
25067 /* Parse the restricted form of the for statement allowed by OpenMP.  */
25068
25069 static tree
25070 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25071 {
25072   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25073   tree real_decl, initv, condv, incrv, declv;
25074   tree this_pre_body, cl;
25075   location_t loc_first;
25076   bool collapse_err = false;
25077   int i, collapse = 1, nbraces = 0;
25078   VEC(tree,gc) *for_block = make_tree_vector ();
25079
25080   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25081     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25082       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25083
25084   gcc_assert (collapse >= 1);
25085
25086   declv = make_tree_vec (collapse);
25087   initv = make_tree_vec (collapse);
25088   condv = make_tree_vec (collapse);
25089   incrv = make_tree_vec (collapse);
25090
25091   loc_first = cp_lexer_peek_token (parser->lexer)->location;
25092
25093   for (i = 0; i < collapse; i++)
25094     {
25095       int bracecount = 0;
25096       bool add_private_clause = false;
25097       location_t loc;
25098
25099       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25100         {
25101           cp_parser_error (parser, "for statement expected");
25102           return NULL;
25103         }
25104       loc = cp_lexer_consume_token (parser->lexer)->location;
25105
25106       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25107         return NULL;
25108
25109       init = decl = real_decl = NULL;
25110       this_pre_body = push_stmt_list ();
25111       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25112         {
25113           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25114
25115              init-expr:
25116                        var = lb
25117                        integer-type var = lb
25118                        random-access-iterator-type var = lb
25119                        pointer-type var = lb
25120           */
25121           cp_decl_specifier_seq type_specifiers;
25122
25123           /* First, try to parse as an initialized declaration.  See
25124              cp_parser_condition, from whence the bulk of this is copied.  */
25125
25126           cp_parser_parse_tentatively (parser);
25127           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25128                                         /*is_trailing_return=*/false,
25129                                         &type_specifiers);
25130           if (cp_parser_parse_definitely (parser))
25131             {
25132               /* If parsing a type specifier seq succeeded, then this
25133                  MUST be a initialized declaration.  */
25134               tree asm_specification, attributes;
25135               cp_declarator *declarator;
25136
25137               declarator = cp_parser_declarator (parser,
25138                                                  CP_PARSER_DECLARATOR_NAMED,
25139                                                  /*ctor_dtor_or_conv_p=*/NULL,
25140                                                  /*parenthesized_p=*/NULL,
25141                                                  /*member_p=*/false);
25142               attributes = cp_parser_attributes_opt (parser);
25143               asm_specification = cp_parser_asm_specification_opt (parser);
25144
25145               if (declarator == cp_error_declarator) 
25146                 cp_parser_skip_to_end_of_statement (parser);
25147
25148               else 
25149                 {
25150                   tree pushed_scope, auto_node;
25151
25152                   decl = start_decl (declarator, &type_specifiers,
25153                                      SD_INITIALIZED, attributes,
25154                                      /*prefix_attributes=*/NULL_TREE,
25155                                      &pushed_scope);
25156
25157                   auto_node = type_uses_auto (TREE_TYPE (decl));
25158                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25159                     {
25160                       if (cp_lexer_next_token_is (parser->lexer, 
25161                                                   CPP_OPEN_PAREN))
25162                         error ("parenthesized initialization is not allowed in "
25163                                "OpenMP %<for%> loop");
25164                       else
25165                         /* Trigger an error.  */
25166                         cp_parser_require (parser, CPP_EQ, RT_EQ);
25167
25168                       init = error_mark_node;
25169                       cp_parser_skip_to_end_of_statement (parser);
25170                     }
25171                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
25172                            || type_dependent_expression_p (decl)
25173                            || auto_node)
25174                     {
25175                       bool is_direct_init, is_non_constant_init;
25176
25177                       init = cp_parser_initializer (parser,
25178                                                     &is_direct_init,
25179                                                     &is_non_constant_init);
25180
25181                       if (auto_node)
25182                         {
25183                           TREE_TYPE (decl)
25184                             = do_auto_deduction (TREE_TYPE (decl), init,
25185                                                  auto_node);
25186
25187                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
25188                               && !type_dependent_expression_p (decl))
25189                             goto non_class;
25190                         }
25191                       
25192                       cp_finish_decl (decl, init, !is_non_constant_init,
25193                                       asm_specification,
25194                                       LOOKUP_ONLYCONVERTING);
25195                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
25196                         {
25197                           VEC_safe_push (tree, gc, for_block, this_pre_body);
25198                           init = NULL_TREE;
25199                         }
25200                       else
25201                         init = pop_stmt_list (this_pre_body);
25202                       this_pre_body = NULL_TREE;
25203                     }
25204                   else
25205                     {
25206                       /* Consume '='.  */
25207                       cp_lexer_consume_token (parser->lexer);
25208                       init = cp_parser_assignment_expression (parser, false, NULL);
25209
25210                     non_class:
25211                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25212                         init = error_mark_node;
25213                       else
25214                         cp_finish_decl (decl, NULL_TREE,
25215                                         /*init_const_expr_p=*/false,
25216                                         asm_specification,
25217                                         LOOKUP_ONLYCONVERTING);
25218                     }
25219
25220                   if (pushed_scope)
25221                     pop_scope (pushed_scope);
25222                 }
25223             }
25224           else 
25225             {
25226               cp_id_kind idk;
25227               /* If parsing a type specifier sequence failed, then
25228                  this MUST be a simple expression.  */
25229               cp_parser_parse_tentatively (parser);
25230               decl = cp_parser_primary_expression (parser, false, false,
25231                                                    false, &idk);
25232               if (!cp_parser_error_occurred (parser)
25233                   && decl
25234                   && DECL_P (decl)
25235                   && CLASS_TYPE_P (TREE_TYPE (decl)))
25236                 {
25237                   tree rhs;
25238
25239                   cp_parser_parse_definitely (parser);
25240                   cp_parser_require (parser, CPP_EQ, RT_EQ);
25241                   rhs = cp_parser_assignment_expression (parser, false, NULL);
25242                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25243                                                          rhs,
25244                                                          tf_warning_or_error));
25245                   add_private_clause = true;
25246                 }
25247               else
25248                 {
25249                   decl = NULL;
25250                   cp_parser_abort_tentative_parse (parser);
25251                   init = cp_parser_expression (parser, false, NULL);
25252                   if (init)
25253                     {
25254                       if (TREE_CODE (init) == MODIFY_EXPR
25255                           || TREE_CODE (init) == MODOP_EXPR)
25256                         real_decl = TREE_OPERAND (init, 0);
25257                     }
25258                 }
25259             }
25260         }
25261       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25262       if (this_pre_body)
25263         {
25264           this_pre_body = pop_stmt_list (this_pre_body);
25265           if (pre_body)
25266             {
25267               tree t = pre_body;
25268               pre_body = push_stmt_list ();
25269               add_stmt (t);
25270               add_stmt (this_pre_body);
25271               pre_body = pop_stmt_list (pre_body);
25272             }
25273           else
25274             pre_body = this_pre_body;
25275         }
25276
25277       if (decl)
25278         real_decl = decl;
25279       if (par_clauses != NULL && real_decl != NULL_TREE)
25280         {
25281           tree *c;
25282           for (c = par_clauses; *c ; )
25283             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25284                 && OMP_CLAUSE_DECL (*c) == real_decl)
25285               {
25286                 error_at (loc, "iteration variable %qD"
25287                           " should not be firstprivate", real_decl);
25288                 *c = OMP_CLAUSE_CHAIN (*c);
25289               }
25290             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25291                      && OMP_CLAUSE_DECL (*c) == real_decl)
25292               {
25293                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25294                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
25295                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25296                 OMP_CLAUSE_DECL (l) = real_decl;
25297                 OMP_CLAUSE_CHAIN (l) = clauses;
25298                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25299                 clauses = l;
25300                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25301                 CP_OMP_CLAUSE_INFO (*c) = NULL;
25302                 add_private_clause = false;
25303               }
25304             else
25305               {
25306                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25307                     && OMP_CLAUSE_DECL (*c) == real_decl)
25308                   add_private_clause = false;
25309                 c = &OMP_CLAUSE_CHAIN (*c);
25310               }
25311         }
25312
25313       if (add_private_clause)
25314         {
25315           tree c;
25316           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25317             {
25318               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25319                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25320                   && OMP_CLAUSE_DECL (c) == decl)
25321                 break;
25322               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25323                        && OMP_CLAUSE_DECL (c) == decl)
25324                 error_at (loc, "iteration variable %qD "
25325                           "should not be firstprivate",
25326                           decl);
25327               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25328                        && OMP_CLAUSE_DECL (c) == decl)
25329                 error_at (loc, "iteration variable %qD should not be reduction",
25330                           decl);
25331             }
25332           if (c == NULL)
25333             {
25334               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25335               OMP_CLAUSE_DECL (c) = decl;
25336               c = finish_omp_clauses (c);
25337               if (c)
25338                 {
25339                   OMP_CLAUSE_CHAIN (c) = clauses;
25340                   clauses = c;
25341                 }
25342             }
25343         }
25344
25345       cond = NULL;
25346       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25347         cond = cp_parser_omp_for_cond (parser, decl);
25348       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25349
25350       incr = NULL;
25351       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25352         {
25353           /* If decl is an iterator, preserve the operator on decl
25354              until finish_omp_for.  */
25355           if (decl
25356               && ((type_dependent_expression_p (decl)
25357                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
25358                   || CLASS_TYPE_P (TREE_TYPE (decl))))
25359             incr = cp_parser_omp_for_incr (parser, decl);
25360           else
25361             incr = cp_parser_expression (parser, false, NULL);
25362         }
25363
25364       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25365         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25366                                                /*or_comma=*/false,
25367                                                /*consume_paren=*/true);
25368
25369       TREE_VEC_ELT (declv, i) = decl;
25370       TREE_VEC_ELT (initv, i) = init;
25371       TREE_VEC_ELT (condv, i) = cond;
25372       TREE_VEC_ELT (incrv, i) = incr;
25373
25374       if (i == collapse - 1)
25375         break;
25376
25377       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
25378          in between the collapsed for loops to be still considered perfectly
25379          nested.  Hopefully the final version clarifies this.
25380          For now handle (multiple) {'s and empty statements.  */
25381       cp_parser_parse_tentatively (parser);
25382       do
25383         {
25384           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25385             break;
25386           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25387             {
25388               cp_lexer_consume_token (parser->lexer);
25389               bracecount++;
25390             }
25391           else if (bracecount
25392                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25393             cp_lexer_consume_token (parser->lexer);
25394           else
25395             {
25396               loc = cp_lexer_peek_token (parser->lexer)->location;
25397               error_at (loc, "not enough collapsed for loops");
25398               collapse_err = true;
25399               cp_parser_abort_tentative_parse (parser);
25400               declv = NULL_TREE;
25401               break;
25402             }
25403         }
25404       while (1);
25405
25406       if (declv)
25407         {
25408           cp_parser_parse_definitely (parser);
25409           nbraces += bracecount;
25410         }
25411     }
25412
25413   /* Note that we saved the original contents of this flag when we entered
25414      the structured block, and so we don't need to re-save it here.  */
25415   parser->in_statement = IN_OMP_FOR;
25416
25417   /* Note that the grammar doesn't call for a structured block here,
25418      though the loop as a whole is a structured block.  */
25419   body = push_stmt_list ();
25420   cp_parser_statement (parser, NULL_TREE, false, NULL);
25421   body = pop_stmt_list (body);
25422
25423   if (declv == NULL_TREE)
25424     ret = NULL_TREE;
25425   else
25426     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
25427                           pre_body, clauses);
25428
25429   while (nbraces)
25430     {
25431       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25432         {
25433           cp_lexer_consume_token (parser->lexer);
25434           nbraces--;
25435         }
25436       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25437         cp_lexer_consume_token (parser->lexer);
25438       else
25439         {
25440           if (!collapse_err)
25441             {
25442               error_at (cp_lexer_peek_token (parser->lexer)->location,
25443                         "collapsed loops not perfectly nested");
25444             }
25445           collapse_err = true;
25446           cp_parser_statement_seq_opt (parser, NULL);
25447           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25448             break;
25449         }
25450     }
25451
25452   while (!VEC_empty (tree, for_block))
25453     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
25454   release_tree_vector (for_block);
25455
25456   return ret;
25457 }
25458
25459 /* OpenMP 2.5:
25460    #pragma omp for for-clause[optseq] new-line
25461      for-loop  */
25462
25463 #define OMP_FOR_CLAUSE_MASK                             \
25464         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25465         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25466         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25467         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25468         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
25469         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
25470         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
25471         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
25472
25473 static tree
25474 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
25475 {
25476   tree clauses, sb, ret;
25477   unsigned int save;
25478
25479   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
25480                                        "#pragma omp for", pragma_tok);
25481
25482   sb = begin_omp_structured_block ();
25483   save = cp_parser_begin_omp_structured_block (parser);
25484
25485   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
25486
25487   cp_parser_end_omp_structured_block (parser, save);
25488   add_stmt (finish_omp_structured_block (sb));
25489
25490   return ret;
25491 }
25492
25493 /* OpenMP 2.5:
25494    # pragma omp master new-line
25495      structured-block  */
25496
25497 static tree
25498 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
25499 {
25500   cp_parser_require_pragma_eol (parser, pragma_tok);
25501   return c_finish_omp_master (input_location,
25502                               cp_parser_omp_structured_block (parser));
25503 }
25504
25505 /* OpenMP 2.5:
25506    # pragma omp ordered new-line
25507      structured-block  */
25508
25509 static tree
25510 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
25511 {
25512   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25513   cp_parser_require_pragma_eol (parser, pragma_tok);
25514   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
25515 }
25516
25517 /* OpenMP 2.5:
25518
25519    section-scope:
25520      { section-sequence }
25521
25522    section-sequence:
25523      section-directive[opt] structured-block
25524      section-sequence section-directive structured-block  */
25525
25526 static tree
25527 cp_parser_omp_sections_scope (cp_parser *parser)
25528 {
25529   tree stmt, substmt;
25530   bool error_suppress = false;
25531   cp_token *tok;
25532
25533   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25534     return NULL_TREE;
25535
25536   stmt = push_stmt_list ();
25537
25538   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
25539     {
25540       unsigned save;
25541
25542       substmt = begin_omp_structured_block ();
25543       save = cp_parser_begin_omp_structured_block (parser);
25544
25545       while (1)
25546         {
25547           cp_parser_statement (parser, NULL_TREE, false, NULL);
25548
25549           tok = cp_lexer_peek_token (parser->lexer);
25550           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25551             break;
25552           if (tok->type == CPP_CLOSE_BRACE)
25553             break;
25554           if (tok->type == CPP_EOF)
25555             break;
25556         }
25557
25558       cp_parser_end_omp_structured_block (parser, save);
25559       substmt = finish_omp_structured_block (substmt);
25560       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25561       add_stmt (substmt);
25562     }
25563
25564   while (1)
25565     {
25566       tok = cp_lexer_peek_token (parser->lexer);
25567       if (tok->type == CPP_CLOSE_BRACE)
25568         break;
25569       if (tok->type == CPP_EOF)
25570         break;
25571
25572       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25573         {
25574           cp_lexer_consume_token (parser->lexer);
25575           cp_parser_require_pragma_eol (parser, tok);
25576           error_suppress = false;
25577         }
25578       else if (!error_suppress)
25579         {
25580           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
25581           error_suppress = true;
25582         }
25583
25584       substmt = cp_parser_omp_structured_block (parser);
25585       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25586       add_stmt (substmt);
25587     }
25588   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25589
25590   substmt = pop_stmt_list (stmt);
25591
25592   stmt = make_node (OMP_SECTIONS);
25593   TREE_TYPE (stmt) = void_type_node;
25594   OMP_SECTIONS_BODY (stmt) = substmt;
25595
25596   add_stmt (stmt);
25597   return stmt;
25598 }
25599
25600 /* OpenMP 2.5:
25601    # pragma omp sections sections-clause[optseq] newline
25602      sections-scope  */
25603
25604 #define OMP_SECTIONS_CLAUSE_MASK                        \
25605         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25606         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25607         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25608         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25609         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25610
25611 static tree
25612 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
25613 {
25614   tree clauses, ret;
25615
25616   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
25617                                        "#pragma omp sections", pragma_tok);
25618
25619   ret = cp_parser_omp_sections_scope (parser);
25620   if (ret)
25621     OMP_SECTIONS_CLAUSES (ret) = clauses;
25622
25623   return ret;
25624 }
25625
25626 /* OpenMP 2.5:
25627    # pragma parallel parallel-clause new-line
25628    # pragma parallel for parallel-for-clause new-line
25629    # pragma parallel sections parallel-sections-clause new-line  */
25630
25631 #define OMP_PARALLEL_CLAUSE_MASK                        \
25632         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25633         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25634         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25635         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25636         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25637         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
25638         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25639         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
25640
25641 static tree
25642 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
25643 {
25644   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
25645   const char *p_name = "#pragma omp parallel";
25646   tree stmt, clauses, par_clause, ws_clause, block;
25647   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
25648   unsigned int save;
25649   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25650
25651   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25652     {
25653       cp_lexer_consume_token (parser->lexer);
25654       p_kind = PRAGMA_OMP_PARALLEL_FOR;
25655       p_name = "#pragma omp parallel for";
25656       mask |= OMP_FOR_CLAUSE_MASK;
25657       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25658     }
25659   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25660     {
25661       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25662       const char *p = IDENTIFIER_POINTER (id);
25663       if (strcmp (p, "sections") == 0)
25664         {
25665           cp_lexer_consume_token (parser->lexer);
25666           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25667           p_name = "#pragma omp parallel sections";
25668           mask |= OMP_SECTIONS_CLAUSE_MASK;
25669           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25670         }
25671     }
25672
25673   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25674   block = begin_omp_parallel ();
25675   save = cp_parser_begin_omp_structured_block (parser);
25676
25677   switch (p_kind)
25678     {
25679     case PRAGMA_OMP_PARALLEL:
25680       cp_parser_statement (parser, NULL_TREE, false, NULL);
25681       par_clause = clauses;
25682       break;
25683
25684     case PRAGMA_OMP_PARALLEL_FOR:
25685       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25686       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25687       break;
25688
25689     case PRAGMA_OMP_PARALLEL_SECTIONS:
25690       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25691       stmt = cp_parser_omp_sections_scope (parser);
25692       if (stmt)
25693         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25694       break;
25695
25696     default:
25697       gcc_unreachable ();
25698     }
25699
25700   cp_parser_end_omp_structured_block (parser, save);
25701   stmt = finish_omp_parallel (par_clause, block);
25702   if (p_kind != PRAGMA_OMP_PARALLEL)
25703     OMP_PARALLEL_COMBINED (stmt) = 1;
25704   return stmt;
25705 }
25706
25707 /* OpenMP 2.5:
25708    # pragma omp single single-clause[optseq] new-line
25709      structured-block  */
25710
25711 #define OMP_SINGLE_CLAUSE_MASK                          \
25712         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25713         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25714         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25715         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25716
25717 static tree
25718 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25719 {
25720   tree stmt = make_node (OMP_SINGLE);
25721   TREE_TYPE (stmt) = void_type_node;
25722
25723   OMP_SINGLE_CLAUSES (stmt)
25724     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25725                                  "#pragma omp single", pragma_tok);
25726   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25727
25728   return add_stmt (stmt);
25729 }
25730
25731 /* OpenMP 3.0:
25732    # pragma omp task task-clause[optseq] new-line
25733      structured-block  */
25734
25735 #define OMP_TASK_CLAUSE_MASK                            \
25736         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25737         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25738         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25739         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25740         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25741         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25742         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
25743         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
25744
25745 static tree
25746 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25747 {
25748   tree clauses, block;
25749   unsigned int save;
25750
25751   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25752                                        "#pragma omp task", pragma_tok);
25753   block = begin_omp_task ();
25754   save = cp_parser_begin_omp_structured_block (parser);
25755   cp_parser_statement (parser, NULL_TREE, false, NULL);
25756   cp_parser_end_omp_structured_block (parser, save);
25757   return finish_omp_task (clauses, block);
25758 }
25759
25760 /* OpenMP 3.0:
25761    # pragma omp taskwait new-line  */
25762
25763 static void
25764 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25765 {
25766   cp_parser_require_pragma_eol (parser, pragma_tok);
25767   finish_omp_taskwait ();
25768 }
25769
25770 /* OpenMP 3.1:
25771    # pragma omp taskyield new-line  */
25772
25773 static void
25774 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
25775 {
25776   cp_parser_require_pragma_eol (parser, pragma_tok);
25777   finish_omp_taskyield ();
25778 }
25779
25780 /* OpenMP 2.5:
25781    # pragma omp threadprivate (variable-list) */
25782
25783 static void
25784 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25785 {
25786   tree vars;
25787
25788   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25789   cp_parser_require_pragma_eol (parser, pragma_tok);
25790
25791   finish_omp_threadprivate (vars);
25792 }
25793
25794 /* Main entry point to OpenMP statement pragmas.  */
25795
25796 static void
25797 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25798 {
25799   tree stmt;
25800
25801   switch (pragma_tok->pragma_kind)
25802     {
25803     case PRAGMA_OMP_ATOMIC:
25804       cp_parser_omp_atomic (parser, pragma_tok);
25805       return;
25806     case PRAGMA_OMP_CRITICAL:
25807       stmt = cp_parser_omp_critical (parser, pragma_tok);
25808       break;
25809     case PRAGMA_OMP_FOR:
25810       stmt = cp_parser_omp_for (parser, pragma_tok);
25811       break;
25812     case PRAGMA_OMP_MASTER:
25813       stmt = cp_parser_omp_master (parser, pragma_tok);
25814       break;
25815     case PRAGMA_OMP_ORDERED:
25816       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25817       break;
25818     case PRAGMA_OMP_PARALLEL:
25819       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25820       break;
25821     case PRAGMA_OMP_SECTIONS:
25822       stmt = cp_parser_omp_sections (parser, pragma_tok);
25823       break;
25824     case PRAGMA_OMP_SINGLE:
25825       stmt = cp_parser_omp_single (parser, pragma_tok);
25826       break;
25827     case PRAGMA_OMP_TASK:
25828       stmt = cp_parser_omp_task (parser, pragma_tok);
25829       break;
25830     default:
25831       gcc_unreachable ();
25832     }
25833
25834   if (stmt)
25835     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25836 }
25837 \f
25838 /* The parser.  */
25839
25840 static GTY (()) cp_parser *the_parser;
25841
25842 \f
25843 /* Special handling for the first token or line in the file.  The first
25844    thing in the file might be #pragma GCC pch_preprocess, which loads a
25845    PCH file, which is a GC collection point.  So we need to handle this
25846    first pragma without benefit of an existing lexer structure.
25847
25848    Always returns one token to the caller in *FIRST_TOKEN.  This is
25849    either the true first token of the file, or the first token after
25850    the initial pragma.  */
25851
25852 static void
25853 cp_parser_initial_pragma (cp_token *first_token)
25854 {
25855   tree name = NULL;
25856
25857   cp_lexer_get_preprocessor_token (NULL, first_token);
25858   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25859     return;
25860
25861   cp_lexer_get_preprocessor_token (NULL, first_token);
25862   if (first_token->type == CPP_STRING)
25863     {
25864       name = first_token->u.value;
25865
25866       cp_lexer_get_preprocessor_token (NULL, first_token);
25867       if (first_token->type != CPP_PRAGMA_EOL)
25868         error_at (first_token->location,
25869                   "junk at end of %<#pragma GCC pch_preprocess%>");
25870     }
25871   else
25872     error_at (first_token->location, "expected string literal");
25873
25874   /* Skip to the end of the pragma.  */
25875   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25876     cp_lexer_get_preprocessor_token (NULL, first_token);
25877
25878   /* Now actually load the PCH file.  */
25879   if (name)
25880     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25881
25882   /* Read one more token to return to our caller.  We have to do this
25883      after reading the PCH file in, since its pointers have to be
25884      live.  */
25885   cp_lexer_get_preprocessor_token (NULL, first_token);
25886 }
25887
25888 /* Normal parsing of a pragma token.  Here we can (and must) use the
25889    regular lexer.  */
25890
25891 static bool
25892 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25893 {
25894   cp_token *pragma_tok;
25895   unsigned int id;
25896
25897   pragma_tok = cp_lexer_consume_token (parser->lexer);
25898   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25899   parser->lexer->in_pragma = true;
25900
25901   id = pragma_tok->pragma_kind;
25902   switch (id)
25903     {
25904     case PRAGMA_GCC_PCH_PREPROCESS:
25905       error_at (pragma_tok->location,
25906                 "%<#pragma GCC pch_preprocess%> must be first");
25907       break;
25908
25909     case PRAGMA_OMP_BARRIER:
25910       switch (context)
25911         {
25912         case pragma_compound:
25913           cp_parser_omp_barrier (parser, pragma_tok);
25914           return false;
25915         case pragma_stmt:
25916           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25917                     "used in compound statements");
25918           break;
25919         default:
25920           goto bad_stmt;
25921         }
25922       break;
25923
25924     case PRAGMA_OMP_FLUSH:
25925       switch (context)
25926         {
25927         case pragma_compound:
25928           cp_parser_omp_flush (parser, pragma_tok);
25929           return false;
25930         case pragma_stmt:
25931           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25932                     "used in compound statements");
25933           break;
25934         default:
25935           goto bad_stmt;
25936         }
25937       break;
25938
25939     case PRAGMA_OMP_TASKWAIT:
25940       switch (context)
25941         {
25942         case pragma_compound:
25943           cp_parser_omp_taskwait (parser, pragma_tok);
25944           return false;
25945         case pragma_stmt:
25946           error_at (pragma_tok->location,
25947                     "%<#pragma omp taskwait%> may only be "
25948                     "used in compound statements");
25949           break;
25950         default:
25951           goto bad_stmt;
25952         }
25953       break;
25954
25955     case PRAGMA_OMP_TASKYIELD:
25956       switch (context)
25957         {
25958         case pragma_compound:
25959           cp_parser_omp_taskyield (parser, pragma_tok);
25960           return false;
25961         case pragma_stmt:
25962           error_at (pragma_tok->location,
25963                     "%<#pragma omp taskyield%> may only be "
25964                     "used in compound statements");
25965           break;
25966         default:
25967           goto bad_stmt;
25968         }
25969       break;
25970
25971     case PRAGMA_OMP_THREADPRIVATE:
25972       cp_parser_omp_threadprivate (parser, pragma_tok);
25973       return false;
25974
25975     case PRAGMA_OMP_ATOMIC:
25976     case PRAGMA_OMP_CRITICAL:
25977     case PRAGMA_OMP_FOR:
25978     case PRAGMA_OMP_MASTER:
25979     case PRAGMA_OMP_ORDERED:
25980     case PRAGMA_OMP_PARALLEL:
25981     case PRAGMA_OMP_SECTIONS:
25982     case PRAGMA_OMP_SINGLE:
25983     case PRAGMA_OMP_TASK:
25984       if (context == pragma_external)
25985         goto bad_stmt;
25986       cp_parser_omp_construct (parser, pragma_tok);
25987       return true;
25988
25989     case PRAGMA_OMP_SECTION:
25990       error_at (pragma_tok->location, 
25991                 "%<#pragma omp section%> may only be used in "
25992                 "%<#pragma omp sections%> construct");
25993       break;
25994
25995     default:
25996       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25997       c_invoke_pragma_handler (id);
25998       break;
25999
26000     bad_stmt:
26001       cp_parser_error (parser, "expected declaration specifiers");
26002       break;
26003     }
26004
26005   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26006   return false;
26007 }
26008
26009 /* The interface the pragma parsers have to the lexer.  */
26010
26011 enum cpp_ttype
26012 pragma_lex (tree *value)
26013 {
26014   cp_token *tok;
26015   enum cpp_ttype ret;
26016
26017   tok = cp_lexer_peek_token (the_parser->lexer);
26018
26019   ret = tok->type;
26020   *value = tok->u.value;
26021
26022   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26023     ret = CPP_EOF;
26024   else if (ret == CPP_STRING)
26025     *value = cp_parser_string_literal (the_parser, false, false);
26026   else
26027     {
26028       cp_lexer_consume_token (the_parser->lexer);
26029       if (ret == CPP_KEYWORD)
26030         ret = CPP_NAME;
26031     }
26032
26033   return ret;
26034 }
26035
26036 \f
26037 /* External interface.  */
26038
26039 /* Parse one entire translation unit.  */
26040
26041 void
26042 c_parse_file (void)
26043 {
26044   static bool already_called = false;
26045
26046   if (already_called)
26047     {
26048       sorry ("inter-module optimizations not implemented for C++");
26049       return;
26050     }
26051   already_called = true;
26052
26053   the_parser = cp_parser_new ();
26054   push_deferring_access_checks (flag_access_control
26055                                 ? dk_no_deferred : dk_no_check);
26056   cp_parser_translation_unit (the_parser);
26057   the_parser = NULL;
26058 }
26059
26060 #include "gt-cp-parser.h"