OSDN Git Service

PR c++/49118
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215   (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217   (cp_lexer *);
218 static void cp_lexer_start_debugging
219   (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224    about passing NULL to functions that require non-NULL arguments
225    (fputs, fprintf).  It will never be used, so all we need is a value
226    of the right type that's guaranteed not to be NULL.  */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233   (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236   (cp_token *);
237
238 /* Manifest constants.  */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables.  */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written.  */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250    sizeof, typeof, or alignof.  */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE.  If NUM is 0, dump all the
255    tokens.  */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260   unsigned i;
261   cp_token *token;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (num == 0)
266     num = VEC_length (cp_token, buffer);
267
268   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269     {
270       cp_lexer_print_token (file, token);
271       switch (token->type)
272         {
273           case CPP_SEMICOLON:
274           case CPP_OPEN_BRACE:
275           case CPP_CLOSE_BRACE:
276           case CPP_EOF:
277             fputc ('\n', file);
278             break;
279
280           default:
281             fputc (' ', file);
282         }
283     }
284
285   if (i == num && i < VEC_length (cp_token, buffer))
286     {
287       fprintf (file, " ... ");
288       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289                             VEC_length (cp_token, buffer) - 1));
290     }
291
292   fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr.  */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301   cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it.  */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311   cp_lexer *lexer;
312
313   c_common_no_more_pch ();
314
315   /* Allocate the memory.  */
316   lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319   /* Initially we are not debugging.  */
320   lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323                                    CP_SAVED_TOKEN_STACK);
324
325   /* Create the buffer.  */
326   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328   return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333    preprocessor.  */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338   cp_lexer *lexer;
339   cp_token token;
340
341   /* It's possible that parsing the first pragma will load a PCH file,
342      which is a GC collection point.  So we have to do that before
343      allocating any memory.  */
344   cp_parser_initial_pragma (&token);
345
346   lexer = cp_lexer_alloc ();
347
348   /* Put the first token in the buffer.  */
349   VEC_quick_push (cp_token, lexer->buffer, &token);
350
351   /* Get the remaining tokens from the preprocessor.  */
352   while (token.type != CPP_EOF)
353     {
354       cp_lexer_get_preprocessor_token (lexer, &token);
355       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356     }
357
358   lexer->last_token = VEC_address (cp_token, lexer->buffer)
359                       + VEC_length (cp_token, lexer->buffer)
360                       - 1;
361   lexer->next_token = VEC_length (cp_token, lexer->buffer)
362                       ? VEC_address (cp_token, lexer->buffer)
363                       : &eof_token;
364
365   /* Subsequent preprocessor diagnostics should use compiler
366      diagnostic functions to get the compiler source location.  */
367   done_lexing = true;
368
369   gcc_assert (!lexer->next_token->purged_p);
370   return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379   cp_token *first = cache->first;
380   cp_token *last = cache->last;
381   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383   /* We do not own the buffer.  */
384   lexer->buffer = NULL;
385   lexer->next_token = first == last ? &eof_token : first;
386   lexer->last_token = last;
387
388   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389                                    CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392   /* Initially we are not debugging.  */
393   lexer->debugging_p = false;
394 #endif
395
396   gcc_assert (!lexer->next_token->purged_p);
397   return lexer;
398 }
399
400 /* Frees all resources associated with LEXER.  */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405   VEC_free (cp_token, gc, lexer->buffer);
406   VEC_free (cp_token_position, heap, lexer->saved_tokens);
407   ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output.  */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417   return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425   gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427   return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433   return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439   lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445   if (lexer->next_token == &eof_token)
446     return lexer->last_token - 1;
447   else
448     return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454   cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456   return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens.  */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN.  Return true
468    if we reach EOF.  If LEXER is NULL, assume we are handling an
469    initial #pragma pch_preprocess, and thus want the lexer to return
470    processed strings.  */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475   static int is_extern_c = 0;
476
477    /* Get a new token from the preprocessor.  */
478   token->type
479     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481   token->keyword = RID_MAX;
482   token->pragma_kind = PRAGMA_NONE;
483   token->purged_p = false;
484
485   /* On some systems, some header files are surrounded by an
486      implicit extern "C" block.  Set a flag in the token if it
487      comes from such a header.  */
488   is_extern_c += pending_lang_change;
489   pending_lang_change = 0;
490   token->implicit_extern_c = is_extern_c > 0;
491
492   /* Check to see if this token is a keyword.  */
493   if (token->type == CPP_NAME)
494     {
495       if (C_IS_RESERVED_WORD (token->u.value))
496         {
497           /* Mark this token as a keyword.  */
498           token->type = CPP_KEYWORD;
499           /* Record which keyword.  */
500           token->keyword = C_RID_CODE (token->u.value);
501         }
502       else
503         {
504           if (warn_cxx0x_compat
505               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507             {
508               /* Warn about the C++0x keyword (but still treat it as
509                  an identifier).  */
510               warning (OPT_Wc__0x_compat, 
511                        "identifier %qE will become a keyword in C++0x",
512                        token->u.value);
513
514               /* Clear out the C_RID_CODE so we don't warn about this
515                  particular identifier-turned-keyword again.  */
516               C_SET_RID_CODE (token->u.value, RID_MAX);
517             }
518
519           token->ambiguous_p = false;
520           token->keyword = RID_MAX;
521         }
522     }
523   else if (token->type == CPP_AT_NAME)
524     {
525       /* This only happens in Objective-C++; it must be a keyword.  */
526       token->type = CPP_KEYWORD;
527       switch (C_RID_CODE (token->u.value))
528         {
529           /* Replace 'class' with '@class', 'private' with '@private',
530              etc.  This prevents confusion with the C++ keyword
531              'class', and makes the tokens consistent with other
532              Objective-C 'AT' keywords.  For example '@class' is
533              reported as RID_AT_CLASS which is consistent with
534              '@synchronized', which is reported as
535              RID_AT_SYNCHRONIZED.
536           */
537         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
538         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
539         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
541         case RID_THROW:     token->keyword = RID_AT_THROW; break;
542         case RID_TRY:       token->keyword = RID_AT_TRY; break;
543         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
544         default:            token->keyword = C_RID_CODE (token->u.value);
545         }
546     }
547   else if (token->type == CPP_PRAGMA)
548     {
549       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
550       token->pragma_kind = ((enum pragma_kind)
551                             TREE_INT_CST_LOW (token->u.value));
552       token->u.value = NULL_TREE;
553     }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN.  */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560   if (token->type != CPP_EOF)
561     {
562       input_location = token->location;
563     }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567    consume it.  */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578   return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE.  */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586   return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE.  */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594   return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD.  */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602   return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD.  */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610   return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier.  */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618   cp_token *token;
619
620   token = cp_lexer_peek_token (lexer);
621   switch (token->keyword) 
622     {
623       /* auto specifier: storage-class-specifier in C++,
624          simple-type-specifier in C++0x.  */
625     case RID_AUTO:
626       /* Storage classes.  */
627     case RID_REGISTER:
628     case RID_STATIC:
629     case RID_EXTERN:
630     case RID_MUTABLE:
631     case RID_THREAD:
632       /* Elaborated type specifiers.  */
633     case RID_ENUM:
634     case RID_CLASS:
635     case RID_STRUCT:
636     case RID_UNION:
637     case RID_TYPENAME:
638       /* Simple type specifiers.  */
639     case RID_CHAR:
640     case RID_CHAR16:
641     case RID_CHAR32:
642     case RID_WCHAR:
643     case RID_BOOL:
644     case RID_SHORT:
645     case RID_INT:
646     case RID_LONG:
647     case RID_INT128:
648     case RID_SIGNED:
649     case RID_UNSIGNED:
650     case RID_FLOAT:
651     case RID_DOUBLE:
652     case RID_VOID:
653       /* GNU extensions.  */ 
654     case RID_ATTRIBUTE:
655     case RID_TYPEOF:
656       /* C++0x extensions.  */
657     case RID_DECLTYPE:
658     case RID_UNDERLYING_TYPE:
659       return true;
660
661     default:
662       return false;
663     }
664 }
665
666 /* Return a pointer to the Nth token in the token stream.  If N is 1,
667    then this is precisely equivalent to cp_lexer_peek_token (except
668    that it is not inline).  One would like to disallow that case, but
669    there is one case (cp_parser_nth_token_starts_template_id) where
670    the caller passes a variable for N and it might be 1.  */
671
672 static cp_token *
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
674 {
675   cp_token *token;
676
677   /* N is 1-based, not zero-based.  */
678   gcc_assert (n > 0);
679
680   if (cp_lexer_debugging_p (lexer))
681     fprintf (cp_lexer_debug_stream,
682              "cp_lexer: peeking ahead %ld at token: ", (long)n);
683
684   --n;
685   token = lexer->next_token;
686   gcc_assert (!n || token != &eof_token);
687   while (n != 0)
688     {
689       ++token;
690       if (token == lexer->last_token)
691         {
692           token = &eof_token;
693           break;
694         }
695
696       if (!token->purged_p)
697         --n;
698     }
699
700   if (cp_lexer_debugging_p (lexer))
701     {
702       cp_lexer_print_token (cp_lexer_debug_stream, token);
703       putc ('\n', cp_lexer_debug_stream);
704     }
705
706   return token;
707 }
708
709 /* Return the next token, and advance the lexer's next_token pointer
710    to point to the next non-purged token.  */
711
712 static cp_token *
713 cp_lexer_consume_token (cp_lexer* lexer)
714 {
715   cp_token *token = lexer->next_token;
716
717   gcc_assert (token != &eof_token);
718   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
719
720   do
721     {
722       lexer->next_token++;
723       if (lexer->next_token == lexer->last_token)
724         {
725           lexer->next_token = &eof_token;
726           break;
727         }
728
729     }
730   while (lexer->next_token->purged_p);
731
732   cp_lexer_set_source_position_from_token (token);
733
734   /* Provide debugging output.  */
735   if (cp_lexer_debugging_p (lexer))
736     {
737       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738       cp_lexer_print_token (cp_lexer_debug_stream, token);
739       putc ('\n', cp_lexer_debug_stream);
740     }
741
742   return token;
743 }
744
745 /* Permanently remove the next token from the token stream, and
746    advance the next_token pointer to refer to the next non-purged
747    token.  */
748
749 static void
750 cp_lexer_purge_token (cp_lexer *lexer)
751 {
752   cp_token *tok = lexer->next_token;
753
754   gcc_assert (tok != &eof_token);
755   tok->purged_p = true;
756   tok->location = UNKNOWN_LOCATION;
757   tok->u.value = NULL_TREE;
758   tok->keyword = RID_MAX;
759
760   do
761     {
762       tok++;
763       if (tok == lexer->last_token)
764         {
765           tok = &eof_token;
766           break;
767         }
768     }
769   while (tok->purged_p);
770   lexer->next_token = tok;
771 }
772
773 /* Permanently remove all tokens after TOK, up to, but not
774    including, the token that will be returned next by
775    cp_lexer_peek_token.  */
776
777 static void
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
779 {
780   cp_token *peek = lexer->next_token;
781
782   if (peek == &eof_token)
783     peek = lexer->last_token;
784
785   gcc_assert (tok < peek);
786
787   for ( tok += 1; tok != peek; tok += 1)
788     {
789       tok->purged_p = true;
790       tok->location = UNKNOWN_LOCATION;
791       tok->u.value = NULL_TREE;
792       tok->keyword = RID_MAX;
793     }
794 }
795
796 /* Begin saving tokens.  All tokens consumed after this point will be
797    preserved.  */
798
799 static void
800 cp_lexer_save_tokens (cp_lexer* lexer)
801 {
802   /* Provide debugging output.  */
803   if (cp_lexer_debugging_p (lexer))
804     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
805
806   VEC_safe_push (cp_token_position, heap,
807                  lexer->saved_tokens, lexer->next_token);
808 }
809
810 /* Commit to the portion of the token stream most recently saved.  */
811
812 static void
813 cp_lexer_commit_tokens (cp_lexer* lexer)
814 {
815   /* Provide debugging output.  */
816   if (cp_lexer_debugging_p (lexer))
817     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
818
819   VEC_pop (cp_token_position, lexer->saved_tokens);
820 }
821
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823    to the token stream.  Stop saving tokens.  */
824
825 static void
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
827 {
828   /* Provide debugging output.  */
829   if (cp_lexer_debugging_p (lexer))
830     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
831
832   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
833 }
834
835 /* Print a representation of the TOKEN on the STREAM.  */
836
837 #ifdef ENABLE_CHECKING
838
839 static void
840 cp_lexer_print_token (FILE * stream, cp_token *token)
841 {
842   /* We don't use cpp_type2name here because the parser defines
843      a few tokens of its own.  */
844   static const char *const token_names[] = {
845     /* cpplib-defined token types */
846 #define OP(e, s) #e,
847 #define TK(e, s) #e,
848     TTYPE_TABLE
849 #undef OP
850 #undef TK
851     /* C++ parser token types - see "Manifest constants", above.  */
852     "KEYWORD",
853     "TEMPLATE_ID",
854     "NESTED_NAME_SPECIFIER",
855   };
856
857   /* For some tokens, print the associated data.  */
858   switch (token->type)
859     {
860     case CPP_KEYWORD:
861       /* Some keywords have a value that is not an IDENTIFIER_NODE.
862          For example, `struct' is mapped to an INTEGER_CST.  */
863       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
864         break;
865       /* else fall through */
866     case CPP_NAME:
867       fputs (IDENTIFIER_POINTER (token->u.value), stream);
868       break;
869
870     case CPP_STRING:
871     case CPP_STRING16:
872     case CPP_STRING32:
873     case CPP_WSTRING:
874     case CPP_UTF8STRING:
875       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
876       break;
877
878     case CPP_NUMBER:
879       print_generic_expr (stream, token->u.value, 0);
880       break;
881
882     default:
883       /* If we have a name for the token, print it out.  Otherwise, we
884          simply give the numeric code.  */
885       if (token->type < ARRAY_SIZE(token_names))
886         fputs (token_names[token->type], stream);
887       else
888         fprintf (stream, "[%d]", token->type);
889       break;
890     }
891 }
892
893 /* Start emitting debugging information.  */
894
895 static void
896 cp_lexer_start_debugging (cp_lexer* lexer)
897 {
898   lexer->debugging_p = true;
899 }
900
901 /* Stop emitting debugging information.  */
902
903 static void
904 cp_lexer_stop_debugging (cp_lexer* lexer)
905 {
906   lexer->debugging_p = false;
907 }
908
909 #endif /* ENABLE_CHECKING */
910
911 /* Create a new cp_token_cache, representing a range of tokens.  */
912
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
915 {
916   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917   cache->first = first;
918   cache->last = last;
919   return cache;
920 }
921
922 \f
923 /* Decl-specifiers.  */
924
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
926
927 static void
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
929 {
930   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
931 }
932
933 /* Declarators.  */
934
935 /* Nothing other than the parser should be creating declarators;
936    declarators are a semi-syntactic representation of C++ entities.
937    Other parts of the front end that need to create entities (like
938    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
939
940 static cp_declarator *make_call_declarator
941   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
942 static cp_declarator *make_array_declarator
943   (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945   (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947   (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949   (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951   (cp_cv_quals, tree, cp_declarator *);
952
953 /* An erroneous declarator.  */
954 static cp_declarator *cp_error_declarator;
955
956 /* The obstack on which declarators and related data structures are
957    allocated.  */
958 static struct obstack declarator_obstack;
959
960 /* Alloc BYTES from the declarator memory pool.  */
961
962 static inline void *
963 alloc_declarator (size_t bytes)
964 {
965   return obstack_alloc (&declarator_obstack, bytes);
966 }
967
968 /* Allocate a declarator of the indicated KIND.  Clear fields that are
969    common to all declarators.  */
970
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
973 {
974   cp_declarator *declarator;
975
976   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977   declarator->kind = kind;
978   declarator->attributes = NULL_TREE;
979   declarator->declarator = NULL;
980   declarator->parameter_pack_p = false;
981   declarator->id_loc = UNKNOWN_LOCATION;
982
983   return declarator;
984 }
985
986 /* Make a declarator for a generalized identifier.  If
987    QUALIFYING_SCOPE is non-NULL, the identifier is
988    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
990    is, if any.   */
991
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994                     special_function_kind sfk)
995 {
996   cp_declarator *declarator;
997
998   /* It is valid to write:
999
1000        class C { void f(); };
1001        typedef C D;
1002        void D::f();
1003
1004      The standard is not clear about whether `typedef const C D' is
1005      legal; as of 2002-09-15 the committee is considering that
1006      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1007      well.  */
1008   if (qualifying_scope && TYPE_P (qualifying_scope))
1009     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1010
1011   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1014
1015   declarator = make_declarator (cdk_id);
1016   declarator->u.id.qualifying_scope = qualifying_scope;
1017   declarator->u.id.unqualified_name = unqualified_name;
1018   declarator->u.id.sfk = sfk;
1019   
1020   return declarator;
1021 }
1022
1023 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1024    of modifiers such as const or volatile to apply to the pointer
1025    type, represented as identifiers.  */
1026
1027 cp_declarator *
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1029 {
1030   cp_declarator *declarator;
1031
1032   declarator = make_declarator (cdk_pointer);
1033   declarator->declarator = target;
1034   declarator->u.pointer.qualifiers = cv_qualifiers;
1035   declarator->u.pointer.class_type = NULL_TREE;
1036   if (target)
1037     {
1038       declarator->id_loc = target->id_loc;
1039       declarator->parameter_pack_p = target->parameter_pack_p;
1040       target->parameter_pack_p = false;
1041     }
1042   else
1043     declarator->parameter_pack_p = false;
1044
1045   return declarator;
1046 }
1047
1048 /* Like make_pointer_declarator -- but for references.  */
1049
1050 cp_declarator *
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1052                            bool rvalue_ref)
1053 {
1054   cp_declarator *declarator;
1055
1056   declarator = make_declarator (cdk_reference);
1057   declarator->declarator = target;
1058   declarator->u.reference.qualifiers = cv_qualifiers;
1059   declarator->u.reference.rvalue_ref = rvalue_ref;
1060   if (target)
1061     {
1062       declarator->id_loc = target->id_loc;
1063       declarator->parameter_pack_p = target->parameter_pack_p;
1064       target->parameter_pack_p = false;
1065     }
1066   else
1067     declarator->parameter_pack_p = false;
1068
1069   return declarator;
1070 }
1071
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073    member of CLASS_TYPE.  */
1074
1075 cp_declarator *
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077                         cp_declarator *pointee)
1078 {
1079   cp_declarator *declarator;
1080
1081   declarator = make_declarator (cdk_ptrmem);
1082   declarator->declarator = pointee;
1083   declarator->u.pointer.qualifiers = cv_qualifiers;
1084   declarator->u.pointer.class_type = class_type;
1085
1086   if (pointee)
1087     {
1088       declarator->parameter_pack_p = pointee->parameter_pack_p;
1089       pointee->parameter_pack_p = false;
1090     }
1091   else
1092     declarator->parameter_pack_p = false;
1093
1094   return declarator;
1095 }
1096
1097 /* Make a declarator for the function given by TARGET, with the
1098    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1099    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1100    indicates what exceptions can be thrown.  */
1101
1102 cp_declarator *
1103 make_call_declarator (cp_declarator *target,
1104                       tree parms,
1105                       cp_cv_quals cv_qualifiers,
1106                       cp_virt_specifiers virt_specifiers,
1107                       tree exception_specification,
1108                       tree late_return_type)
1109 {
1110   cp_declarator *declarator;
1111
1112   declarator = make_declarator (cdk_function);
1113   declarator->declarator = target;
1114   declarator->u.function.parameters = parms;
1115   declarator->u.function.qualifiers = cv_qualifiers;
1116   declarator->u.function.virt_specifiers = virt_specifiers;
1117   declarator->u.function.exception_specification = exception_specification;
1118   declarator->u.function.late_return_type = late_return_type;
1119   if (target)
1120     {
1121       declarator->id_loc = target->id_loc;
1122       declarator->parameter_pack_p = target->parameter_pack_p;
1123       target->parameter_pack_p = false;
1124     }
1125   else
1126     declarator->parameter_pack_p = false;
1127
1128   return declarator;
1129 }
1130
1131 /* Make a declarator for an array of BOUNDS elements, each of which is
1132    defined by ELEMENT.  */
1133
1134 cp_declarator *
1135 make_array_declarator (cp_declarator *element, tree bounds)
1136 {
1137   cp_declarator *declarator;
1138
1139   declarator = make_declarator (cdk_array);
1140   declarator->declarator = element;
1141   declarator->u.array.bounds = bounds;
1142   if (element)
1143     {
1144       declarator->id_loc = element->id_loc;
1145       declarator->parameter_pack_p = element->parameter_pack_p;
1146       element->parameter_pack_p = false;
1147     }
1148   else
1149     declarator->parameter_pack_p = false;
1150
1151   return declarator;
1152 }
1153
1154 /* Determine whether the declarator we've seen so far can be a
1155    parameter pack, when followed by an ellipsis.  */
1156 static bool 
1157 declarator_can_be_parameter_pack (cp_declarator *declarator)
1158 {
1159   /* Search for a declarator name, or any other declarator that goes
1160      after the point where the ellipsis could appear in a parameter
1161      pack. If we find any of these, then this declarator can not be
1162      made into a parameter pack.  */
1163   bool found = false;
1164   while (declarator && !found)
1165     {
1166       switch ((int)declarator->kind)
1167         {
1168         case cdk_id:
1169         case cdk_array:
1170           found = true;
1171           break;
1172
1173         case cdk_error:
1174           return true;
1175
1176         default:
1177           declarator = declarator->declarator;
1178           break;
1179         }
1180     }
1181
1182   return !found;
1183 }
1184
1185 cp_parameter_declarator *no_parameters;
1186
1187 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1188    DECLARATOR and DEFAULT_ARGUMENT.  */
1189
1190 cp_parameter_declarator *
1191 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1192                            cp_declarator *declarator,
1193                            tree default_argument)
1194 {
1195   cp_parameter_declarator *parameter;
1196
1197   parameter = ((cp_parameter_declarator *)
1198                alloc_declarator (sizeof (cp_parameter_declarator)));
1199   parameter->next = NULL;
1200   if (decl_specifiers)
1201     parameter->decl_specifiers = *decl_specifiers;
1202   else
1203     clear_decl_specs (&parameter->decl_specifiers);
1204   parameter->declarator = declarator;
1205   parameter->default_argument = default_argument;
1206   parameter->ellipsis_p = false;
1207
1208   return parameter;
1209 }
1210
1211 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1212
1213 static bool
1214 function_declarator_p (const cp_declarator *declarator)
1215 {
1216   while (declarator)
1217     {
1218       if (declarator->kind == cdk_function
1219           && declarator->declarator->kind == cdk_id)
1220         return true;
1221       if (declarator->kind == cdk_id
1222           || declarator->kind == cdk_error)
1223         return false;
1224       declarator = declarator->declarator;
1225     }
1226   return false;
1227 }
1228  
1229 /* The parser.  */
1230
1231 /* Overview
1232    --------
1233
1234    A cp_parser parses the token stream as specified by the C++
1235    grammar.  Its job is purely parsing, not semantic analysis.  For
1236    example, the parser breaks the token stream into declarators,
1237    expressions, statements, and other similar syntactic constructs.
1238    It does not check that the types of the expressions on either side
1239    of an assignment-statement are compatible, or that a function is
1240    not declared with a parameter of type `void'.
1241
1242    The parser invokes routines elsewhere in the compiler to perform
1243    semantic analysis and to build up the abstract syntax tree for the
1244    code processed.
1245
1246    The parser (and the template instantiation code, which is, in a
1247    way, a close relative of parsing) are the only parts of the
1248    compiler that should be calling push_scope and pop_scope, or
1249    related functions.  The parser (and template instantiation code)
1250    keeps track of what scope is presently active; everything else
1251    should simply honor that.  (The code that generates static
1252    initializers may also need to set the scope, in order to check
1253    access control correctly when emitting the initializers.)
1254
1255    Methodology
1256    -----------
1257
1258    The parser is of the standard recursive-descent variety.  Upcoming
1259    tokens in the token stream are examined in order to determine which
1260    production to use when parsing a non-terminal.  Some C++ constructs
1261    require arbitrary look ahead to disambiguate.  For example, it is
1262    impossible, in the general case, to tell whether a statement is an
1263    expression or declaration without scanning the entire statement.
1264    Therefore, the parser is capable of "parsing tentatively."  When the
1265    parser is not sure what construct comes next, it enters this mode.
1266    Then, while we attempt to parse the construct, the parser queues up
1267    error messages, rather than issuing them immediately, and saves the
1268    tokens it consumes.  If the construct is parsed successfully, the
1269    parser "commits", i.e., it issues any queued error messages and
1270    the tokens that were being preserved are permanently discarded.
1271    If, however, the construct is not parsed successfully, the parser
1272    rolls back its state completely so that it can resume parsing using
1273    a different alternative.
1274
1275    Future Improvements
1276    -------------------
1277
1278    The performance of the parser could probably be improved substantially.
1279    We could often eliminate the need to parse tentatively by looking ahead
1280    a little bit.  In some places, this approach might not entirely eliminate
1281    the need to parse tentatively, but it might still speed up the average
1282    case.  */
1283
1284 /* Flags that are passed to some parsing functions.  These values can
1285    be bitwise-ored together.  */
1286
1287 enum
1288 {
1289   /* No flags.  */
1290   CP_PARSER_FLAGS_NONE = 0x0,
1291   /* The construct is optional.  If it is not present, then no error
1292      should be issued.  */
1293   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1294   /* When parsing a type-specifier, treat user-defined type-names
1295      as non-type identifiers.  */
1296   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1297   /* When parsing a type-specifier, do not try to parse a class-specifier
1298      or enum-specifier.  */
1299   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1300   /* When parsing a decl-specifier-seq, only allow type-specifier or
1301      constexpr.  */
1302   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1303 };
1304
1305 /* This type is used for parameters and variables which hold
1306    combinations of the above flags.  */
1307 typedef int cp_parser_flags;
1308
1309 /* The different kinds of declarators we want to parse.  */
1310
1311 typedef enum cp_parser_declarator_kind
1312 {
1313   /* We want an abstract declarator.  */
1314   CP_PARSER_DECLARATOR_ABSTRACT,
1315   /* We want a named declarator.  */
1316   CP_PARSER_DECLARATOR_NAMED,
1317   /* We don't mind, but the name must be an unqualified-id.  */
1318   CP_PARSER_DECLARATOR_EITHER
1319 } cp_parser_declarator_kind;
1320
1321 /* The precedence values used to parse binary expressions.  The minimum value
1322    of PREC must be 1, because zero is reserved to quickly discriminate
1323    binary operators from other tokens.  */
1324
1325 enum cp_parser_prec
1326 {
1327   PREC_NOT_OPERATOR,
1328   PREC_LOGICAL_OR_EXPRESSION,
1329   PREC_LOGICAL_AND_EXPRESSION,
1330   PREC_INCLUSIVE_OR_EXPRESSION,
1331   PREC_EXCLUSIVE_OR_EXPRESSION,
1332   PREC_AND_EXPRESSION,
1333   PREC_EQUALITY_EXPRESSION,
1334   PREC_RELATIONAL_EXPRESSION,
1335   PREC_SHIFT_EXPRESSION,
1336   PREC_ADDITIVE_EXPRESSION,
1337   PREC_MULTIPLICATIVE_EXPRESSION,
1338   PREC_PM_EXPRESSION,
1339   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1340 };
1341
1342 /* A mapping from a token type to a corresponding tree node type, with a
1343    precedence value.  */
1344
1345 typedef struct cp_parser_binary_operations_map_node
1346 {
1347   /* The token type.  */
1348   enum cpp_ttype token_type;
1349   /* The corresponding tree code.  */
1350   enum tree_code tree_type;
1351   /* The precedence of this operator.  */
1352   enum cp_parser_prec prec;
1353 } cp_parser_binary_operations_map_node;
1354
1355 typedef struct cp_parser_expression_stack_entry
1356 {
1357   /* Left hand side of the binary operation we are currently
1358      parsing.  */
1359   tree lhs;
1360   /* Original tree code for left hand side, if it was a binary
1361      expression itself (used for -Wparentheses).  */
1362   enum tree_code lhs_type;
1363   /* Tree code for the binary operation we are parsing.  */
1364   enum tree_code tree_type;
1365   /* Precedence of the binary operation we are parsing.  */
1366   enum cp_parser_prec prec;
1367 } cp_parser_expression_stack_entry;
1368
1369 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1370    entries because precedence levels on the stack are monotonically
1371    increasing.  */
1372 typedef struct cp_parser_expression_stack_entry
1373   cp_parser_expression_stack[NUM_PREC_VALUES];
1374
1375 /* Prototypes.  */
1376
1377 /* Constructors and destructors.  */
1378
1379 static cp_parser_context *cp_parser_context_new
1380   (cp_parser_context *);
1381
1382 /* Class variables.  */
1383
1384 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1385
1386 /* The operator-precedence table used by cp_parser_binary_expression.
1387    Transformed into an associative array (binops_by_token) by
1388    cp_parser_new.  */
1389
1390 static const cp_parser_binary_operations_map_node binops[] = {
1391   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1392   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1393
1394   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1397
1398   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1400
1401   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1403
1404   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1405   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1406   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1408
1409   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1410   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1411
1412   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1413
1414   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1415
1416   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1417
1418   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1419
1420   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1421 };
1422
1423 /* The same as binops, but initialized by cp_parser_new so that
1424    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1425    for speed.  */
1426 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1427
1428 /* Constructors and destructors.  */
1429
1430 /* Construct a new context.  The context below this one on the stack
1431    is given by NEXT.  */
1432
1433 static cp_parser_context *
1434 cp_parser_context_new (cp_parser_context* next)
1435 {
1436   cp_parser_context *context;
1437
1438   /* Allocate the storage.  */
1439   if (cp_parser_context_free_list != NULL)
1440     {
1441       /* Pull the first entry from the free list.  */
1442       context = cp_parser_context_free_list;
1443       cp_parser_context_free_list = context->next;
1444       memset (context, 0, sizeof (*context));
1445     }
1446   else
1447     context = ggc_alloc_cleared_cp_parser_context ();
1448
1449   /* No errors have occurred yet in this context.  */
1450   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1451   /* If this is not the bottommost context, copy information that we
1452      need from the previous context.  */
1453   if (next)
1454     {
1455       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1456          expression, then we are parsing one in this context, too.  */
1457       context->object_type = next->object_type;
1458       /* Thread the stack.  */
1459       context->next = next;
1460     }
1461
1462   return context;
1463 }
1464
1465 /* Managing the unparsed function queues.  */
1466
1467 #define unparsed_funs_with_default_args \
1468   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1469 #define unparsed_funs_with_definitions \
1470   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1471
1472 static void
1473 push_unparsed_function_queues (cp_parser *parser)
1474 {
1475   VEC_safe_push (cp_unparsed_functions_entry, gc,
1476                  parser->unparsed_queues, NULL);
1477   unparsed_funs_with_default_args = NULL;
1478   unparsed_funs_with_definitions = make_tree_vector ();
1479 }
1480
1481 static void
1482 pop_unparsed_function_queues (cp_parser *parser)
1483 {
1484   release_tree_vector (unparsed_funs_with_definitions);
1485   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1486 }
1487
1488 /* Prototypes.  */
1489
1490 /* Constructors and destructors.  */
1491
1492 static cp_parser *cp_parser_new
1493   (void);
1494
1495 /* Routines to parse various constructs.
1496
1497    Those that return `tree' will return the error_mark_node (rather
1498    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1499    Sometimes, they will return an ordinary node if error-recovery was
1500    attempted, even though a parse error occurred.  So, to check
1501    whether or not a parse error occurred, you should always use
1502    cp_parser_error_occurred.  If the construct is optional (indicated
1503    either by an `_opt' in the name of the function that does the
1504    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1505    the construct is not present.  */
1506
1507 /* Lexical conventions [gram.lex]  */
1508
1509 static tree cp_parser_identifier
1510   (cp_parser *);
1511 static tree cp_parser_string_literal
1512   (cp_parser *, bool, bool);
1513
1514 /* Basic concepts [gram.basic]  */
1515
1516 static bool cp_parser_translation_unit
1517   (cp_parser *);
1518
1519 /* Expressions [gram.expr]  */
1520
1521 static tree cp_parser_primary_expression
1522   (cp_parser *, bool, bool, bool, cp_id_kind *);
1523 static tree cp_parser_id_expression
1524   (cp_parser *, bool, bool, bool *, bool, bool);
1525 static tree cp_parser_unqualified_id
1526   (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier_opt
1528   (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_nested_name_specifier
1530   (cp_parser *, bool, bool, bool, bool);
1531 static tree cp_parser_qualifying_entity
1532   (cp_parser *, bool, bool, bool, bool, bool);
1533 static tree cp_parser_postfix_expression
1534   (cp_parser *, bool, bool, bool, cp_id_kind *);
1535 static tree cp_parser_postfix_open_square_expression
1536   (cp_parser *, tree, bool);
1537 static tree cp_parser_postfix_dot_deref_expression
1538   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1539 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1540   (cp_parser *, int, bool, bool, bool *);
1541 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1542 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1543 static void cp_parser_pseudo_destructor_name
1544   (cp_parser *, tree *, tree *);
1545 static tree cp_parser_unary_expression
1546   (cp_parser *, bool, bool, cp_id_kind *);
1547 static enum tree_code cp_parser_unary_operator
1548   (cp_token *);
1549 static tree cp_parser_new_expression
1550   (cp_parser *);
1551 static VEC(tree,gc) *cp_parser_new_placement
1552   (cp_parser *);
1553 static tree cp_parser_new_type_id
1554   (cp_parser *, tree *);
1555 static cp_declarator *cp_parser_new_declarator_opt
1556   (cp_parser *);
1557 static cp_declarator *cp_parser_direct_new_declarator
1558   (cp_parser *);
1559 static VEC(tree,gc) *cp_parser_new_initializer
1560   (cp_parser *);
1561 static tree cp_parser_delete_expression
1562   (cp_parser *);
1563 static tree cp_parser_cast_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static tree cp_parser_binary_expression
1566   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1567 static tree cp_parser_question_colon_clause
1568   (cp_parser *, tree);
1569 static tree cp_parser_assignment_expression
1570   (cp_parser *, bool, cp_id_kind *);
1571 static enum tree_code cp_parser_assignment_operator_opt
1572   (cp_parser *);
1573 static tree cp_parser_expression
1574   (cp_parser *, bool, cp_id_kind *);
1575 static tree cp_parser_constant_expression
1576   (cp_parser *, bool, bool *);
1577 static tree cp_parser_builtin_offsetof
1578   (cp_parser *);
1579 static tree cp_parser_lambda_expression
1580   (cp_parser *);
1581 static void cp_parser_lambda_introducer
1582   (cp_parser *, tree);
1583 static bool cp_parser_lambda_declarator_opt
1584   (cp_parser *, tree);
1585 static void cp_parser_lambda_body
1586   (cp_parser *, tree);
1587
1588 /* Statements [gram.stmt.stmt]  */
1589
1590 static void cp_parser_statement
1591   (cp_parser *, tree, bool, bool *);
1592 static void cp_parser_label_for_labeled_statement
1593   (cp_parser *);
1594 static tree cp_parser_expression_statement
1595   (cp_parser *, tree);
1596 static tree cp_parser_compound_statement
1597   (cp_parser *, tree, bool, bool);
1598 static void cp_parser_statement_seq_opt
1599   (cp_parser *, tree);
1600 static tree cp_parser_selection_statement
1601   (cp_parser *, bool *);
1602 static tree cp_parser_condition
1603   (cp_parser *);
1604 static tree cp_parser_iteration_statement
1605   (cp_parser *);
1606 static bool cp_parser_for_init_statement
1607   (cp_parser *, tree *decl);
1608 static tree cp_parser_for
1609   (cp_parser *);
1610 static tree cp_parser_c_for
1611   (cp_parser *, tree, tree);
1612 static tree cp_parser_range_for
1613   (cp_parser *, tree, tree, tree);
1614 static tree cp_parser_perform_range_for_lookup
1615   (tree, tree *, tree *);
1616 static tree cp_parser_range_for_member_function
1617   (tree, tree);
1618 static tree cp_parser_jump_statement
1619   (cp_parser *);
1620 static void cp_parser_declaration_statement
1621   (cp_parser *);
1622
1623 static tree cp_parser_implicitly_scoped_statement
1624   (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1626   (cp_parser *);
1627
1628 /* Declarations [gram.dcl.dcl] */
1629
1630 static void cp_parser_declaration_seq_opt
1631   (cp_parser *);
1632 static void cp_parser_declaration
1633   (cp_parser *);
1634 static void cp_parser_block_declaration
1635   (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637   (cp_parser *, bool, tree *);
1638 static void cp_parser_decl_specifier_seq
1639   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1641   (cp_parser *);
1642 static tree cp_parser_function_specifier_opt
1643   (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1646    int *, bool *);
1647 static tree cp_parser_simple_type_specifier
1648   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1650   (cp_parser *);
1651 static tree cp_parser_nonclass_name 
1652   (cp_parser* parser);
1653 static tree cp_parser_elaborated_type_specifier
1654   (cp_parser *, bool, bool);
1655 static tree cp_parser_enum_specifier
1656   (cp_parser *);
1657 static void cp_parser_enumerator_list
1658   (cp_parser *, tree);
1659 static void cp_parser_enumerator_definition
1660   (cp_parser *, tree);
1661 static tree cp_parser_namespace_name
1662   (cp_parser *);
1663 static void cp_parser_namespace_definition
1664   (cp_parser *);
1665 static void cp_parser_namespace_body
1666   (cp_parser *);
1667 static tree cp_parser_qualified_namespace_specifier
1668   (cp_parser *);
1669 static void cp_parser_namespace_alias_definition
1670   (cp_parser *);
1671 static bool cp_parser_using_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_using_directive
1674   (cp_parser *);
1675 static void cp_parser_asm_definition
1676   (cp_parser *);
1677 static void cp_parser_linkage_specification
1678   (cp_parser *);
1679 static void cp_parser_static_assert
1680   (cp_parser *, bool);
1681 static tree cp_parser_decltype
1682   (cp_parser *);
1683
1684 /* Declarators [gram.dcl.decl] */
1685
1686 static tree cp_parser_init_declarator
1687   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1688 static cp_declarator *cp_parser_declarator
1689   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1690 static cp_declarator *cp_parser_direct_declarator
1691   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1692 static enum tree_code cp_parser_ptr_operator
1693   (cp_parser *, tree *, cp_cv_quals *);
1694 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1695   (cp_parser *);
1696 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1697   (cp_parser *);
1698 static tree cp_parser_late_return_type_opt
1699   (cp_parser *);
1700 static tree cp_parser_declarator_id
1701   (cp_parser *, bool);
1702 static tree cp_parser_type_id
1703   (cp_parser *);
1704 static tree cp_parser_template_type_arg
1705   (cp_parser *);
1706 static tree cp_parser_trailing_type_id (cp_parser *);
1707 static tree cp_parser_type_id_1
1708   (cp_parser *, bool, bool);
1709 static void cp_parser_type_specifier_seq
1710   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1711 static tree cp_parser_parameter_declaration_clause
1712   (cp_parser *);
1713 static tree cp_parser_parameter_declaration_list
1714   (cp_parser *, bool *);
1715 static cp_parameter_declarator *cp_parser_parameter_declaration
1716   (cp_parser *, bool, bool *);
1717 static tree cp_parser_default_argument 
1718   (cp_parser *, bool);
1719 static void cp_parser_function_body
1720   (cp_parser *);
1721 static tree cp_parser_initializer
1722   (cp_parser *, bool *, bool *);
1723 static tree cp_parser_initializer_clause
1724   (cp_parser *, bool *);
1725 static tree cp_parser_braced_list
1726   (cp_parser*, bool*);
1727 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1728   (cp_parser *, bool *);
1729
1730 static bool cp_parser_ctor_initializer_opt_and_function_body
1731   (cp_parser *);
1732
1733 /* Classes [gram.class] */
1734
1735 static tree cp_parser_class_name
1736   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1737 static tree cp_parser_class_specifier
1738   (cp_parser *);
1739 static tree cp_parser_class_head
1740   (cp_parser *, bool *, tree *, tree *);
1741 static enum tag_types cp_parser_class_key
1742   (cp_parser *);
1743 static void cp_parser_member_specification_opt
1744   (cp_parser *);
1745 static void cp_parser_member_declaration
1746   (cp_parser *);
1747 static tree cp_parser_pure_specifier
1748   (cp_parser *);
1749 static tree cp_parser_constant_initializer
1750   (cp_parser *);
1751
1752 /* Derived classes [gram.class.derived] */
1753
1754 static tree cp_parser_base_clause
1755   (cp_parser *);
1756 static tree cp_parser_base_specifier
1757   (cp_parser *);
1758
1759 /* Special member functions [gram.special] */
1760
1761 static tree cp_parser_conversion_function_id
1762   (cp_parser *);
1763 static tree cp_parser_conversion_type_id
1764   (cp_parser *);
1765 static cp_declarator *cp_parser_conversion_declarator_opt
1766   (cp_parser *);
1767 static bool cp_parser_ctor_initializer_opt
1768   (cp_parser *);
1769 static void cp_parser_mem_initializer_list
1770   (cp_parser *);
1771 static tree cp_parser_mem_initializer
1772   (cp_parser *);
1773 static tree cp_parser_mem_initializer_id
1774   (cp_parser *);
1775
1776 /* Overloading [gram.over] */
1777
1778 static tree cp_parser_operator_function_id
1779   (cp_parser *);
1780 static tree cp_parser_operator
1781   (cp_parser *);
1782
1783 /* Templates [gram.temp] */
1784
1785 static void cp_parser_template_declaration
1786   (cp_parser *, bool);
1787 static tree cp_parser_template_parameter_list
1788   (cp_parser *);
1789 static tree cp_parser_template_parameter
1790   (cp_parser *, bool *, bool *);
1791 static tree cp_parser_type_parameter
1792   (cp_parser *, bool *);
1793 static tree cp_parser_template_id
1794   (cp_parser *, bool, bool, bool);
1795 static tree cp_parser_template_name
1796   (cp_parser *, bool, bool, bool, bool *);
1797 static tree cp_parser_template_argument_list
1798   (cp_parser *);
1799 static tree cp_parser_template_argument
1800   (cp_parser *);
1801 static void cp_parser_explicit_instantiation
1802   (cp_parser *);
1803 static void cp_parser_explicit_specialization
1804   (cp_parser *);
1805
1806 /* Exception handling [gram.exception] */
1807
1808 static tree cp_parser_try_block
1809   (cp_parser *);
1810 static bool cp_parser_function_try_block
1811   (cp_parser *);
1812 static void cp_parser_handler_seq
1813   (cp_parser *);
1814 static void cp_parser_handler
1815   (cp_parser *);
1816 static tree cp_parser_exception_declaration
1817   (cp_parser *);
1818 static tree cp_parser_throw_expression
1819   (cp_parser *);
1820 static tree cp_parser_exception_specification_opt
1821   (cp_parser *);
1822 static tree cp_parser_type_id_list
1823   (cp_parser *);
1824
1825 /* GNU Extensions */
1826
1827 static tree cp_parser_asm_specification_opt
1828   (cp_parser *);
1829 static tree cp_parser_asm_operand_list
1830   (cp_parser *);
1831 static tree cp_parser_asm_clobber_list
1832   (cp_parser *);
1833 static tree cp_parser_asm_label_list
1834   (cp_parser *);
1835 static tree cp_parser_attributes_opt
1836   (cp_parser *);
1837 static tree cp_parser_attribute_list
1838   (cp_parser *);
1839 static bool cp_parser_extension_opt
1840   (cp_parser *, int *);
1841 static void cp_parser_label_declaration
1842   (cp_parser *);
1843
1844 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1845 static bool cp_parser_pragma
1846   (cp_parser *, enum pragma_context);
1847
1848 /* Objective-C++ Productions */
1849
1850 static tree cp_parser_objc_message_receiver
1851   (cp_parser *);
1852 static tree cp_parser_objc_message_args
1853   (cp_parser *);
1854 static tree cp_parser_objc_message_expression
1855   (cp_parser *);
1856 static tree cp_parser_objc_encode_expression
1857   (cp_parser *);
1858 static tree cp_parser_objc_defs_expression
1859   (cp_parser *);
1860 static tree cp_parser_objc_protocol_expression
1861   (cp_parser *);
1862 static tree cp_parser_objc_selector_expression
1863   (cp_parser *);
1864 static tree cp_parser_objc_expression
1865   (cp_parser *);
1866 static bool cp_parser_objc_selector_p
1867   (enum cpp_ttype);
1868 static tree cp_parser_objc_selector
1869   (cp_parser *);
1870 static tree cp_parser_objc_protocol_refs_opt
1871   (cp_parser *);
1872 static void cp_parser_objc_declaration
1873   (cp_parser *, tree);
1874 static tree cp_parser_objc_statement
1875   (cp_parser *);
1876 static bool cp_parser_objc_valid_prefix_attributes
1877   (cp_parser *, tree *);
1878 static void cp_parser_objc_at_property_declaration 
1879   (cp_parser *) ;
1880 static void cp_parser_objc_at_synthesize_declaration 
1881   (cp_parser *) ;
1882 static void cp_parser_objc_at_dynamic_declaration
1883   (cp_parser *) ;
1884 static tree cp_parser_objc_struct_declaration
1885   (cp_parser *) ;
1886
1887 /* Utility Routines */
1888
1889 static tree cp_parser_lookup_name
1890   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1891 static tree cp_parser_lookup_name_simple
1892   (cp_parser *, tree, location_t);
1893 static tree cp_parser_maybe_treat_template_as_class
1894   (tree, bool);
1895 static bool cp_parser_check_declarator_template_parameters
1896   (cp_parser *, cp_declarator *, location_t);
1897 static bool cp_parser_check_template_parameters
1898   (cp_parser *, unsigned, location_t, cp_declarator *);
1899 static tree cp_parser_simple_cast_expression
1900   (cp_parser *);
1901 static tree cp_parser_global_scope_opt
1902   (cp_parser *, bool);
1903 static bool cp_parser_constructor_declarator_p
1904   (cp_parser *, bool);
1905 static tree cp_parser_function_definition_from_specifiers_and_declarator
1906   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1907 static tree cp_parser_function_definition_after_declarator
1908   (cp_parser *, bool);
1909 static void cp_parser_template_declaration_after_export
1910   (cp_parser *, bool);
1911 static void cp_parser_perform_template_parameter_access_checks
1912   (VEC (deferred_access_check,gc)*);
1913 static tree cp_parser_single_declaration
1914   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1915 static tree cp_parser_functional_cast
1916   (cp_parser *, tree);
1917 static tree cp_parser_save_member_function_body
1918   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1919 static tree cp_parser_enclosed_template_argument_list
1920   (cp_parser *);
1921 static void cp_parser_save_default_args
1922   (cp_parser *, tree);
1923 static void cp_parser_late_parsing_for_member
1924   (cp_parser *, tree);
1925 static void cp_parser_late_parsing_default_args
1926   (cp_parser *, tree);
1927 static tree cp_parser_sizeof_operand
1928   (cp_parser *, enum rid);
1929 static tree cp_parser_trait_expr
1930   (cp_parser *, enum rid);
1931 static bool cp_parser_declares_only_class_p
1932   (cp_parser *);
1933 static void cp_parser_set_storage_class
1934   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1935 static void cp_parser_set_decl_spec_type
1936   (cp_decl_specifier_seq *, tree, location_t, bool);
1937 static bool cp_parser_friend_p
1938   (const cp_decl_specifier_seq *);
1939 static void cp_parser_required_error
1940   (cp_parser *, required_token, bool);
1941 static cp_token *cp_parser_require
1942   (cp_parser *, enum cpp_ttype, required_token);
1943 static cp_token *cp_parser_require_keyword
1944   (cp_parser *, enum rid, required_token);
1945 static bool cp_parser_token_starts_function_definition_p
1946   (cp_token *);
1947 static bool cp_parser_next_token_starts_class_definition_p
1948   (cp_parser *);
1949 static bool cp_parser_next_token_ends_template_argument_p
1950   (cp_parser *);
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952   (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1954   (cp_token *);
1955 static void cp_parser_check_class_key
1956   (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958   (tree type, location_t location);
1959 static bool cp_parser_optional_template_keyword
1960   (cp_parser *);
1961 static void cp_parser_pre_parsed_nested_name_specifier
1962   (cp_parser *);
1963 static bool cp_parser_cache_group
1964   (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1966   (cp_parser *);
1967 static void cp_parser_commit_to_tentative_parse
1968   (cp_parser *);
1969 static void cp_parser_abort_tentative_parse
1970   (cp_parser *);
1971 static bool cp_parser_parse_definitely
1972   (cp_parser *);
1973 static inline bool cp_parser_parsing_tentatively
1974   (cp_parser *);
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1976   (cp_parser *);
1977 static void cp_parser_error
1978   (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980   (cp_parser *, tree, tree, name_lookup_error, location_t);
1981 static bool cp_parser_simulate_error
1982   (cp_parser *);
1983 static bool cp_parser_check_type_definition
1984   (cp_parser *);
1985 static void cp_parser_check_for_definition_in_return_type
1986   (cp_declarator *, tree, location_t type_location);
1987 static void cp_parser_check_for_invalid_template_id
1988   (cp_parser *, tree, location_t location);
1989 static bool cp_parser_non_integral_constant_expression
1990   (cp_parser *, non_integral_constant);
1991 static void cp_parser_diagnose_invalid_type_name
1992   (cp_parser *, tree, tree, location_t);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994   (cp_parser *);
1995 static int cp_parser_skip_to_closing_parenthesis
1996   (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1998   (cp_parser *);
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2000   (cp_parser *);
2001 static void cp_parser_skip_to_end_of_block_or_statement
2002   (cp_parser *);
2003 static bool cp_parser_skip_to_closing_brace
2004   (cp_parser *);
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2006   (cp_parser *);
2007 static void cp_parser_skip_to_pragma_eol
2008   (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2010   (cp_parser *);
2011 static bool cp_parser_allow_gnu_extensions_p
2012   (cp_parser *);
2013 static bool cp_parser_is_string_literal
2014   (cp_token *);
2015 static bool cp_parser_is_keyword
2016   (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018   (cp_parser *, tree, tree, location_t location);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021
2022 /* Returns nonzero if we are parsing tentatively.  */
2023
2024 static inline bool
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2026 {
2027   return parser->context->next != NULL;
2028 }
2029
2030 /* Returns nonzero if TOKEN is a string literal.  */
2031
2032 static bool
2033 cp_parser_is_string_literal (cp_token* token)
2034 {
2035   return (token->type == CPP_STRING ||
2036           token->type == CPP_STRING16 ||
2037           token->type == CPP_STRING32 ||
2038           token->type == CPP_WSTRING ||
2039           token->type == CPP_UTF8STRING);
2040 }
2041
2042 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2043
2044 static bool
2045 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2046 {
2047   return token->keyword == keyword;
2048 }
2049
2050 /* If not parsing tentatively, issue a diagnostic of the form
2051       FILE:LINE: MESSAGE before TOKEN
2052    where TOKEN is the next token in the input stream.  MESSAGE
2053    (specified by the caller) is usually of the form "expected
2054    OTHER-TOKEN".  */
2055
2056 static void
2057 cp_parser_error (cp_parser* parser, const char* gmsgid)
2058 {
2059   if (!cp_parser_simulate_error (parser))
2060     {
2061       cp_token *token = cp_lexer_peek_token (parser->lexer);
2062       /* This diagnostic makes more sense if it is tagged to the line
2063          of the token we just peeked at.  */
2064       cp_lexer_set_source_position_from_token (token);
2065
2066       if (token->type == CPP_PRAGMA)
2067         {
2068           error_at (token->location,
2069                     "%<#pragma%> is not allowed here");
2070           cp_parser_skip_to_pragma_eol (parser, token);
2071           return;
2072         }
2073
2074       c_parse_error (gmsgid,
2075                      /* Because c_parser_error does not understand
2076                         CPP_KEYWORD, keywords are treated like
2077                         identifiers.  */
2078                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079                      token->u.value, token->flags);
2080     }
2081 }
2082
2083 /* Issue an error about name-lookup failing.  NAME is the
2084    IDENTIFIER_NODE DECL is the result of
2085    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2086    the thing that we hoped to find.  */
2087
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090                              tree name,
2091                              tree decl,
2092                              name_lookup_error desired,
2093                              location_t location)
2094 {
2095   /* If name lookup completely failed, tell the user that NAME was not
2096      declared.  */
2097   if (decl == error_mark_node)
2098     {
2099       if (parser->scope && parser->scope != global_namespace)
2100         error_at (location, "%<%E::%E%> has not been declared",
2101                   parser->scope, name);
2102       else if (parser->scope == global_namespace)
2103         error_at (location, "%<::%E%> has not been declared", name);
2104       else if (parser->object_scope
2105                && !CLASS_TYPE_P (parser->object_scope))
2106         error_at (location, "request for member %qE in non-class type %qT",
2107                   name, parser->object_scope);
2108       else if (parser->object_scope)
2109         error_at (location, "%<%T::%E%> has not been declared",
2110                   parser->object_scope, name);
2111       else
2112         error_at (location, "%qE has not been declared", name);
2113     }
2114   else if (parser->scope && parser->scope != global_namespace)
2115     {
2116       switch (desired)
2117         {
2118           case NLE_TYPE:
2119             error_at (location, "%<%E::%E%> is not a type",
2120                                 parser->scope, name);
2121             break;
2122           case NLE_CXX98:
2123             error_at (location, "%<%E::%E%> is not a class or namespace",
2124                                 parser->scope, name);
2125             break;
2126           case NLE_NOT_CXX98:
2127             error_at (location,
2128                       "%<%E::%E%> is not a class, namespace, or enumeration",
2129                       parser->scope, name);
2130             break;
2131           default:
2132             gcc_unreachable ();
2133             
2134         }
2135     }
2136   else if (parser->scope == global_namespace)
2137     {
2138       switch (desired)
2139         {
2140           case NLE_TYPE:
2141             error_at (location, "%<::%E%> is not a type", name);
2142             break;
2143           case NLE_CXX98:
2144             error_at (location, "%<::%E%> is not a class or namespace", name);
2145             break;
2146           case NLE_NOT_CXX98:
2147             error_at (location,
2148                       "%<::%E%> is not a class, namespace, or enumeration",
2149                       name);
2150             break;
2151           default:
2152             gcc_unreachable ();
2153         }
2154     }
2155   else
2156     {
2157       switch (desired)
2158         {
2159           case NLE_TYPE:
2160             error_at (location, "%qE is not a type", name);
2161             break;
2162           case NLE_CXX98:
2163             error_at (location, "%qE is not a class or namespace", name);
2164             break;
2165           case NLE_NOT_CXX98:
2166             error_at (location,
2167                       "%qE is not a class, namespace, or enumeration", name);
2168             break;
2169           default:
2170             gcc_unreachable ();
2171         }
2172     }
2173 }
2174
2175 /* If we are parsing tentatively, remember that an error has occurred
2176    during this tentative parse.  Returns true if the error was
2177    simulated; false if a message should be issued by the caller.  */
2178
2179 static bool
2180 cp_parser_simulate_error (cp_parser* parser)
2181 {
2182   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2183     {
2184       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2185       return true;
2186     }
2187   return false;
2188 }
2189
2190 /* Check for repeated decl-specifiers.  */
2191
2192 static void
2193 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2194                            location_t location)
2195 {
2196   int ds;
2197
2198   for (ds = ds_first; ds != ds_last; ++ds)
2199     {
2200       unsigned count = decl_specs->specs[ds];
2201       if (count < 2)
2202         continue;
2203       /* The "long" specifier is a special case because of "long long".  */
2204       if (ds == ds_long)
2205         {
2206           if (count > 2)
2207             error_at (location, "%<long long long%> is too long for GCC");
2208           else 
2209             pedwarn_cxx98 (location, OPT_Wlong_long, 
2210                            "ISO C++ 1998 does not support %<long long%>");
2211         }
2212       else if (count > 1)
2213         {
2214           static const char *const decl_spec_names[] = {
2215             "signed",
2216             "unsigned",
2217             "short",
2218             "long",
2219             "const",
2220             "volatile",
2221             "restrict",
2222             "inline",
2223             "virtual",
2224             "explicit",
2225             "friend",
2226             "typedef",
2227             "constexpr",
2228             "__complex",
2229             "__thread"
2230           };
2231           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2232         }
2233     }
2234 }
2235
2236 /* This function is called when a type is defined.  If type
2237    definitions are forbidden at this point, an error message is
2238    issued.  */
2239
2240 static bool
2241 cp_parser_check_type_definition (cp_parser* parser)
2242 {
2243   /* If types are forbidden here, issue a message.  */
2244   if (parser->type_definition_forbidden_message)
2245     {
2246       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2247          in the message need to be interpreted.  */
2248       error (parser->type_definition_forbidden_message);
2249       return false;
2250     }
2251   return true;
2252 }
2253
2254 /* This function is called when the DECLARATOR is processed.  The TYPE
2255    was a type defined in the decl-specifiers.  If it is invalid to
2256    define a type in the decl-specifiers for DECLARATOR, an error is
2257    issued. TYPE_LOCATION is the location of TYPE and is used
2258    for error reporting.  */
2259
2260 static void
2261 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2262                                                tree type, location_t type_location)
2263 {
2264   /* [dcl.fct] forbids type definitions in return types.
2265      Unfortunately, it's not easy to know whether or not we are
2266      processing a return type until after the fact.  */
2267   while (declarator
2268          && (declarator->kind == cdk_pointer
2269              || declarator->kind == cdk_reference
2270              || declarator->kind == cdk_ptrmem))
2271     declarator = declarator->declarator;
2272   if (declarator
2273       && declarator->kind == cdk_function)
2274     {
2275       error_at (type_location,
2276                 "new types may not be defined in a return type");
2277       inform (type_location, 
2278               "(perhaps a semicolon is missing after the definition of %qT)",
2279               type);
2280     }
2281 }
2282
2283 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2284    "<" in any valid C++ program.  If the next token is indeed "<",
2285    issue a message warning the user about what appears to be an
2286    invalid attempt to form a template-id. LOCATION is the location
2287    of the type-specifier (TYPE) */
2288
2289 static void
2290 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2291                                          tree type, location_t location)
2292 {
2293   cp_token_position start = 0;
2294
2295   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2296     {
2297       if (TYPE_P (type))
2298         error_at (location, "%qT is not a template", type);
2299       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2300         error_at (location, "%qE is not a template", type);
2301       else
2302         error_at (location, "invalid template-id");
2303       /* Remember the location of the invalid "<".  */
2304       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2305         start = cp_lexer_token_position (parser->lexer, true);
2306       /* Consume the "<".  */
2307       cp_lexer_consume_token (parser->lexer);
2308       /* Parse the template arguments.  */
2309       cp_parser_enclosed_template_argument_list (parser);
2310       /* Permanently remove the invalid template arguments so that
2311          this error message is not issued again.  */
2312       if (start)
2313         cp_lexer_purge_tokens_after (parser->lexer, start);
2314     }
2315 }
2316
2317 /* If parsing an integral constant-expression, issue an error message
2318    about the fact that THING appeared and return true.  Otherwise,
2319    return false.  In either case, set
2320    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2321
2322 static bool
2323 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2324                                             non_integral_constant thing)
2325 {
2326   parser->non_integral_constant_expression_p = true;
2327   if (parser->integral_constant_expression_p)
2328     {
2329       if (!parser->allow_non_integral_constant_expression_p)
2330         {
2331           const char *msg = NULL;
2332           switch (thing)
2333             {
2334               case NIC_FLOAT:
2335                 error ("floating-point literal "
2336                        "cannot appear in a constant-expression");
2337                 return true;
2338               case NIC_CAST:
2339                 error ("a cast to a type other than an integral or "
2340                        "enumeration type cannot appear in a "
2341                        "constant-expression");
2342                 return true;
2343               case NIC_TYPEID:
2344                 error ("%<typeid%> operator "
2345                        "cannot appear in a constant-expression");
2346                 return true;
2347               case NIC_NCC:
2348                 error ("non-constant compound literals "
2349                        "cannot appear in a constant-expression");
2350                 return true;
2351               case NIC_FUNC_CALL:
2352                 error ("a function call "
2353                        "cannot appear in a constant-expression");
2354                 return true;
2355               case NIC_INC:
2356                 error ("an increment "
2357                        "cannot appear in a constant-expression");
2358                 return true;
2359               case NIC_DEC:
2360                 error ("an decrement "
2361                        "cannot appear in a constant-expression");
2362                 return true;
2363               case NIC_ARRAY_REF:
2364                 error ("an array reference "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_ADDR_LABEL:
2368                 error ("the address of a label "
2369                        "cannot appear in a constant-expression");
2370                 return true;
2371               case NIC_OVERLOADED:
2372                 error ("calls to overloaded operators "
2373                        "cannot appear in a constant-expression");
2374                 return true;
2375               case NIC_ASSIGNMENT:
2376                 error ("an assignment cannot appear in a constant-expression");
2377                 return true;
2378               case NIC_COMMA:
2379                 error ("a comma operator "
2380                        "cannot appear in a constant-expression");
2381                 return true;
2382               case NIC_CONSTRUCTOR:
2383                 error ("a call to a constructor "
2384                        "cannot appear in a constant-expression");
2385                 return true;
2386               case NIC_THIS:
2387                 msg = "this";
2388                 break;
2389               case NIC_FUNC_NAME:
2390                 msg = "__FUNCTION__";
2391                 break;
2392               case NIC_PRETTY_FUNC:
2393                 msg = "__PRETTY_FUNCTION__";
2394                 break;
2395               case NIC_C99_FUNC:
2396                 msg = "__func__";
2397                 break;
2398               case NIC_VA_ARG:
2399                 msg = "va_arg";
2400                 break;
2401               case NIC_ARROW:
2402                 msg = "->";
2403                 break;
2404               case NIC_POINT:
2405                 msg = ".";
2406                 break;
2407               case NIC_STAR:
2408                 msg = "*";
2409                 break;
2410               case NIC_ADDR:
2411                 msg = "&";
2412                 break;
2413               case NIC_PREINCREMENT:
2414                 msg = "++";
2415                 break;
2416               case NIC_PREDECREMENT:
2417                 msg = "--";
2418                 break;
2419               case NIC_NEW:
2420                 msg = "new";
2421                 break;
2422               case NIC_DEL:
2423                 msg = "delete";
2424                 break;
2425               default:
2426                 gcc_unreachable ();
2427             }
2428           if (msg)
2429             error ("%qs cannot appear in a constant-expression", msg);
2430           return true;
2431         }
2432     }
2433   return false;
2434 }
2435
2436 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2437    qualifying scope (or NULL, if none) for ID.  This function commits
2438    to the current active tentative parse, if any.  (Otherwise, the
2439    problematic construct might be encountered again later, resulting
2440    in duplicate error messages.) LOCATION is the location of ID.  */
2441
2442 static void
2443 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2444                                       tree scope, tree id,
2445                                       location_t location)
2446 {
2447   tree decl, old_scope;
2448   cp_parser_commit_to_tentative_parse (parser);
2449   /* Try to lookup the identifier.  */
2450   old_scope = parser->scope;
2451   parser->scope = scope;
2452   decl = cp_parser_lookup_name_simple (parser, id, location);
2453   parser->scope = old_scope;
2454   /* If the lookup found a template-name, it means that the user forgot
2455   to specify an argument list. Emit a useful error message.  */
2456   if (TREE_CODE (decl) == TEMPLATE_DECL)
2457     error_at (location,
2458               "invalid use of template-name %qE without an argument list",
2459               decl);
2460   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2461     error_at (location, "invalid use of destructor %qD as a type", id);
2462   else if (TREE_CODE (decl) == TYPE_DECL)
2463     /* Something like 'unsigned A a;'  */
2464     error_at (location, "invalid combination of multiple type-specifiers");
2465   else if (!parser->scope)
2466     {
2467       /* Issue an error message.  */
2468       error_at (location, "%qE does not name a type", id);
2469       /* If we're in a template class, it's possible that the user was
2470          referring to a type from a base class.  For example:
2471
2472            template <typename T> struct A { typedef T X; };
2473            template <typename T> struct B : public A<T> { X x; };
2474
2475          The user should have said "typename A<T>::X".  */
2476       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2477         inform (location, "C++0x %<constexpr%> only available with "
2478                 "-std=c++0x or -std=gnu++0x");
2479       else if (processing_template_decl && current_class_type
2480                && TYPE_BINFO (current_class_type))
2481         {
2482           tree b;
2483
2484           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2485                b;
2486                b = TREE_CHAIN (b))
2487             {
2488               tree base_type = BINFO_TYPE (b);
2489               if (CLASS_TYPE_P (base_type)
2490                   && dependent_type_p (base_type))
2491                 {
2492                   tree field;
2493                   /* Go from a particular instantiation of the
2494                      template (which will have an empty TYPE_FIELDs),
2495                      to the main version.  */
2496                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2497                   for (field = TYPE_FIELDS (base_type);
2498                        field;
2499                        field = DECL_CHAIN (field))
2500                     if (TREE_CODE (field) == TYPE_DECL
2501                         && DECL_NAME (field) == id)
2502                       {
2503                         inform (location, 
2504                                 "(perhaps %<typename %T::%E%> was intended)",
2505                                 BINFO_TYPE (b), id);
2506                         break;
2507                       }
2508                   if (field)
2509                     break;
2510                 }
2511             }
2512         }
2513     }
2514   /* Here we diagnose qualified-ids where the scope is actually correct,
2515      but the identifier does not resolve to a valid type name.  */
2516   else if (parser->scope != error_mark_node)
2517     {
2518       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2519         error_at (location, "%qE in namespace %qE does not name a type",
2520                   id, parser->scope);
2521       else if (CLASS_TYPE_P (parser->scope)
2522                && constructor_name_p (id, parser->scope))
2523         {
2524           /* A<T>::A<T>() */
2525           error_at (location, "%<%T::%E%> names the constructor, not"
2526                     " the type", parser->scope, id);
2527           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2528             error_at (location, "and %qT has no template constructors",
2529                       parser->scope);
2530         }
2531       else if (TYPE_P (parser->scope)
2532                && dependent_scope_p (parser->scope))
2533         error_at (location, "need %<typename%> before %<%T::%E%> because "
2534                   "%qT is a dependent scope",
2535                   parser->scope, id, parser->scope);
2536       else if (TYPE_P (parser->scope))
2537         error_at (location, "%qE in %q#T does not name a type",
2538                   id, parser->scope);
2539       else
2540         gcc_unreachable ();
2541     }
2542 }
2543
2544 /* Check for a common situation where a type-name should be present,
2545    but is not, and issue a sensible error message.  Returns true if an
2546    invalid type-name was detected.
2547
2548    The situation handled by this function are variable declarations of the
2549    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2550    Usually, `ID' should name a type, but if we got here it means that it
2551    does not. We try to emit the best possible error message depending on
2552    how exactly the id-expression looks like.  */
2553
2554 static bool
2555 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2556 {
2557   tree id;
2558   cp_token *token = cp_lexer_peek_token (parser->lexer);
2559
2560   /* Avoid duplicate error about ambiguous lookup.  */
2561   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2562     {
2563       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2564       if (next->type == CPP_NAME && next->ambiguous_p)
2565         goto out;
2566     }
2567
2568   cp_parser_parse_tentatively (parser);
2569   id = cp_parser_id_expression (parser,
2570                                 /*template_keyword_p=*/false,
2571                                 /*check_dependency_p=*/true,
2572                                 /*template_p=*/NULL,
2573                                 /*declarator_p=*/true,
2574                                 /*optional_p=*/false);
2575   /* If the next token is a (, this is a function with no explicit return
2576      type, i.e. constructor, destructor or conversion op.  */
2577   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2578       || TREE_CODE (id) == TYPE_DECL)
2579     {
2580       cp_parser_abort_tentative_parse (parser);
2581       return false;
2582     }
2583   if (!cp_parser_parse_definitely (parser))
2584     return false;
2585
2586   /* Emit a diagnostic for the invalid type.  */
2587   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2588                                         id, token->location);
2589  out:
2590   /* If we aren't in the middle of a declarator (i.e. in a
2591      parameter-declaration-clause), skip to the end of the declaration;
2592      there's no point in trying to process it.  */
2593   if (!parser->in_declarator_p)
2594     cp_parser_skip_to_end_of_block_or_statement (parser);
2595   return true;
2596 }
2597
2598 /* Consume tokens up to, and including, the next non-nested closing `)'.
2599    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2600    are doing error recovery. Returns -1 if OR_COMMA is true and we
2601    found an unnested comma.  */
2602
2603 static int
2604 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2605                                        bool recovering,
2606                                        bool or_comma,
2607                                        bool consume_paren)
2608 {
2609   unsigned paren_depth = 0;
2610   unsigned brace_depth = 0;
2611   unsigned square_depth = 0;
2612
2613   if (recovering && !or_comma
2614       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2615     return 0;
2616
2617   while (true)
2618     {
2619       cp_token * token = cp_lexer_peek_token (parser->lexer);
2620
2621       switch (token->type)
2622         {
2623         case CPP_EOF:
2624         case CPP_PRAGMA_EOL:
2625           /* If we've run out of tokens, then there is no closing `)'.  */
2626           return 0;
2627
2628         /* This is good for lambda expression capture-lists.  */
2629         case CPP_OPEN_SQUARE:
2630           ++square_depth;
2631           break;
2632         case CPP_CLOSE_SQUARE:
2633           if (!square_depth--)
2634             return 0;
2635           break;
2636
2637         case CPP_SEMICOLON:
2638           /* This matches the processing in skip_to_end_of_statement.  */
2639           if (!brace_depth)
2640             return 0;
2641           break;
2642
2643         case CPP_OPEN_BRACE:
2644           ++brace_depth;
2645           break;
2646         case CPP_CLOSE_BRACE:
2647           if (!brace_depth--)
2648             return 0;
2649           break;
2650
2651         case CPP_COMMA:
2652           if (recovering && or_comma && !brace_depth && !paren_depth
2653               && !square_depth)
2654             return -1;
2655           break;
2656
2657         case CPP_OPEN_PAREN:
2658           if (!brace_depth)
2659             ++paren_depth;
2660           break;
2661
2662         case CPP_CLOSE_PAREN:
2663           if (!brace_depth && !paren_depth--)
2664             {
2665               if (consume_paren)
2666                 cp_lexer_consume_token (parser->lexer);
2667               return 1;
2668             }
2669           break;
2670
2671         default:
2672           break;
2673         }
2674
2675       /* Consume the token.  */
2676       cp_lexer_consume_token (parser->lexer);
2677     }
2678 }
2679
2680 /* Consume tokens until we reach the end of the current statement.
2681    Normally, that will be just before consuming a `;'.  However, if a
2682    non-nested `}' comes first, then we stop before consuming that.  */
2683
2684 static void
2685 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2686 {
2687   unsigned nesting_depth = 0;
2688
2689   while (true)
2690     {
2691       cp_token *token = cp_lexer_peek_token (parser->lexer);
2692
2693       switch (token->type)
2694         {
2695         case CPP_EOF:
2696         case CPP_PRAGMA_EOL:
2697           /* If we've run out of tokens, stop.  */
2698           return;
2699
2700         case CPP_SEMICOLON:
2701           /* If the next token is a `;', we have reached the end of the
2702              statement.  */
2703           if (!nesting_depth)
2704             return;
2705           break;
2706
2707         case CPP_CLOSE_BRACE:
2708           /* If this is a non-nested '}', stop before consuming it.
2709              That way, when confronted with something like:
2710
2711                { 3 + }
2712
2713              we stop before consuming the closing '}', even though we
2714              have not yet reached a `;'.  */
2715           if (nesting_depth == 0)
2716             return;
2717
2718           /* If it is the closing '}' for a block that we have
2719              scanned, stop -- but only after consuming the token.
2720              That way given:
2721
2722                 void f g () { ... }
2723                 typedef int I;
2724
2725              we will stop after the body of the erroneously declared
2726              function, but before consuming the following `typedef'
2727              declaration.  */
2728           if (--nesting_depth == 0)
2729             {
2730               cp_lexer_consume_token (parser->lexer);
2731               return;
2732             }
2733
2734         case CPP_OPEN_BRACE:
2735           ++nesting_depth;
2736           break;
2737
2738         default:
2739           break;
2740         }
2741
2742       /* Consume the token.  */
2743       cp_lexer_consume_token (parser->lexer);
2744     }
2745 }
2746
2747 /* This function is called at the end of a statement or declaration.
2748    If the next token is a semicolon, it is consumed; otherwise, error
2749    recovery is attempted.  */
2750
2751 static void
2752 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2753 {
2754   /* Look for the trailing `;'.  */
2755   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2756     {
2757       /* If there is additional (erroneous) input, skip to the end of
2758          the statement.  */
2759       cp_parser_skip_to_end_of_statement (parser);
2760       /* If the next token is now a `;', consume it.  */
2761       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2762         cp_lexer_consume_token (parser->lexer);
2763     }
2764 }
2765
2766 /* Skip tokens until we have consumed an entire block, or until we
2767    have consumed a non-nested `;'.  */
2768
2769 static void
2770 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2771 {
2772   int nesting_depth = 0;
2773
2774   while (nesting_depth >= 0)
2775     {
2776       cp_token *token = cp_lexer_peek_token (parser->lexer);
2777
2778       switch (token->type)
2779         {
2780         case CPP_EOF:
2781         case CPP_PRAGMA_EOL:
2782           /* If we've run out of tokens, stop.  */
2783           return;
2784
2785         case CPP_SEMICOLON:
2786           /* Stop if this is an unnested ';'. */
2787           if (!nesting_depth)
2788             nesting_depth = -1;
2789           break;
2790
2791         case CPP_CLOSE_BRACE:
2792           /* Stop if this is an unnested '}', or closes the outermost
2793              nesting level.  */
2794           nesting_depth--;
2795           if (nesting_depth < 0)
2796             return;
2797           if (!nesting_depth)
2798             nesting_depth = -1;
2799           break;
2800
2801         case CPP_OPEN_BRACE:
2802           /* Nest. */
2803           nesting_depth++;
2804           break;
2805
2806         default:
2807           break;
2808         }
2809
2810       /* Consume the token.  */
2811       cp_lexer_consume_token (parser->lexer);
2812     }
2813 }
2814
2815 /* Skip tokens until a non-nested closing curly brace is the next
2816    token, or there are no more tokens. Return true in the first case,
2817    false otherwise.  */
2818
2819 static bool
2820 cp_parser_skip_to_closing_brace (cp_parser *parser)
2821 {
2822   unsigned nesting_depth = 0;
2823
2824   while (true)
2825     {
2826       cp_token *token = cp_lexer_peek_token (parser->lexer);
2827
2828       switch (token->type)
2829         {
2830         case CPP_EOF:
2831         case CPP_PRAGMA_EOL:
2832           /* If we've run out of tokens, stop.  */
2833           return false;
2834
2835         case CPP_CLOSE_BRACE:
2836           /* If the next token is a non-nested `}', then we have reached
2837              the end of the current block.  */
2838           if (nesting_depth-- == 0)
2839             return true;
2840           break;
2841
2842         case CPP_OPEN_BRACE:
2843           /* If it the next token is a `{', then we are entering a new
2844              block.  Consume the entire block.  */
2845           ++nesting_depth;
2846           break;
2847
2848         default:
2849           break;
2850         }
2851
2852       /* Consume the token.  */
2853       cp_lexer_consume_token (parser->lexer);
2854     }
2855 }
2856
2857 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2858    parameter is the PRAGMA token, allowing us to purge the entire pragma
2859    sequence.  */
2860
2861 static void
2862 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2863 {
2864   cp_token *token;
2865
2866   parser->lexer->in_pragma = false;
2867
2868   do
2869     token = cp_lexer_consume_token (parser->lexer);
2870   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2871
2872   /* Ensure that the pragma is not parsed again.  */
2873   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2874 }
2875
2876 /* Require pragma end of line, resyncing with it as necessary.  The
2877    arguments are as for cp_parser_skip_to_pragma_eol.  */
2878
2879 static void
2880 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2881 {
2882   parser->lexer->in_pragma = false;
2883   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2884     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2885 }
2886
2887 /* This is a simple wrapper around make_typename_type. When the id is
2888    an unresolved identifier node, we can provide a superior diagnostic
2889    using cp_parser_diagnose_invalid_type_name.  */
2890
2891 static tree
2892 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2893                               tree id, location_t id_location)
2894 {
2895   tree result;
2896   if (TREE_CODE (id) == IDENTIFIER_NODE)
2897     {
2898       result = make_typename_type (scope, id, typename_type,
2899                                    /*complain=*/tf_none);
2900       if (result == error_mark_node)
2901         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2902       return result;
2903     }
2904   return make_typename_type (scope, id, typename_type, tf_error);
2905 }
2906
2907 /* This is a wrapper around the
2908    make_{pointer,ptrmem,reference}_declarator functions that decides
2909    which one to call based on the CODE and CLASS_TYPE arguments. The
2910    CODE argument should be one of the values returned by
2911    cp_parser_ptr_operator. */
2912 static cp_declarator *
2913 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2914                                     cp_cv_quals cv_qualifiers,
2915                                     cp_declarator *target)
2916 {
2917   if (code == ERROR_MARK)
2918     return cp_error_declarator;
2919
2920   if (code == INDIRECT_REF)
2921     if (class_type == NULL_TREE)
2922       return make_pointer_declarator (cv_qualifiers, target);
2923     else
2924       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2925   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2926     return make_reference_declarator (cv_qualifiers, target, false);
2927   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2928     return make_reference_declarator (cv_qualifiers, target, true);
2929   gcc_unreachable ();
2930 }
2931
2932 /* Create a new C++ parser.  */
2933
2934 static cp_parser *
2935 cp_parser_new (void)
2936 {
2937   cp_parser *parser;
2938   cp_lexer *lexer;
2939   unsigned i;
2940
2941   /* cp_lexer_new_main is called before doing GC allocation because
2942      cp_lexer_new_main might load a PCH file.  */
2943   lexer = cp_lexer_new_main ();
2944
2945   /* Initialize the binops_by_token so that we can get the tree
2946      directly from the token.  */
2947   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2948     binops_by_token[binops[i].token_type] = binops[i];
2949
2950   parser = ggc_alloc_cleared_cp_parser ();
2951   parser->lexer = lexer;
2952   parser->context = cp_parser_context_new (NULL);
2953
2954   /* For now, we always accept GNU extensions.  */
2955   parser->allow_gnu_extensions_p = 1;
2956
2957   /* The `>' token is a greater-than operator, not the end of a
2958      template-id.  */
2959   parser->greater_than_is_operator_p = true;
2960
2961   parser->default_arg_ok_p = true;
2962
2963   /* We are not parsing a constant-expression.  */
2964   parser->integral_constant_expression_p = false;
2965   parser->allow_non_integral_constant_expression_p = false;
2966   parser->non_integral_constant_expression_p = false;
2967
2968   /* Local variable names are not forbidden.  */
2969   parser->local_variables_forbidden_p = false;
2970
2971   /* We are not processing an `extern "C"' declaration.  */
2972   parser->in_unbraced_linkage_specification_p = false;
2973
2974   /* We are not processing a declarator.  */
2975   parser->in_declarator_p = false;
2976
2977   /* We are not processing a template-argument-list.  */
2978   parser->in_template_argument_list_p = false;
2979
2980   /* We are not in an iteration statement.  */
2981   parser->in_statement = 0;
2982
2983   /* We are not in a switch statement.  */
2984   parser->in_switch_statement_p = false;
2985
2986   /* We are not parsing a type-id inside an expression.  */
2987   parser->in_type_id_in_expr_p = false;
2988
2989   /* Declarations aren't implicitly extern "C".  */
2990   parser->implicit_extern_c = false;
2991
2992   /* String literals should be translated to the execution character set.  */
2993   parser->translate_strings_p = true;
2994
2995   /* We are not parsing a function body.  */
2996   parser->in_function_body = false;
2997
2998   /* We can correct until told otherwise.  */
2999   parser->colon_corrects_to_scope_p = true;
3000
3001   /* The unparsed function queue is empty.  */
3002   push_unparsed_function_queues (parser);
3003
3004   /* There are no classes being defined.  */
3005   parser->num_classes_being_defined = 0;
3006
3007   /* No template parameters apply.  */
3008   parser->num_template_parameter_lists = 0;
3009
3010   return parser;
3011 }
3012
3013 /* Create a cp_lexer structure which will emit the tokens in CACHE
3014    and push it onto the parser's lexer stack.  This is used for delayed
3015    parsing of in-class method bodies and default arguments, and should
3016    not be confused with tentative parsing.  */
3017 static void
3018 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3019 {
3020   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3021   lexer->next = parser->lexer;
3022   parser->lexer = lexer;
3023
3024   /* Move the current source position to that of the first token in the
3025      new lexer.  */
3026   cp_lexer_set_source_position_from_token (lexer->next_token);
3027 }
3028
3029 /* Pop the top lexer off the parser stack.  This is never used for the
3030    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3031 static void
3032 cp_parser_pop_lexer (cp_parser *parser)
3033 {
3034   cp_lexer *lexer = parser->lexer;
3035   parser->lexer = lexer->next;
3036   cp_lexer_destroy (lexer);
3037
3038   /* Put the current source position back where it was before this
3039      lexer was pushed.  */
3040   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3041 }
3042
3043 /* Lexical conventions [gram.lex]  */
3044
3045 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3046    identifier.  */
3047
3048 static tree
3049 cp_parser_identifier (cp_parser* parser)
3050 {
3051   cp_token *token;
3052
3053   /* Look for the identifier.  */
3054   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3055   /* Return the value.  */
3056   return token ? token->u.value : error_mark_node;
3057 }
3058
3059 /* Parse a sequence of adjacent string constants.  Returns a
3060    TREE_STRING representing the combined, nul-terminated string
3061    constant.  If TRANSLATE is true, translate the string to the
3062    execution character set.  If WIDE_OK is true, a wide string is
3063    invalid here.
3064
3065    C++98 [lex.string] says that if a narrow string literal token is
3066    adjacent to a wide string literal token, the behavior is undefined.
3067    However, C99 6.4.5p4 says that this results in a wide string literal.
3068    We follow C99 here, for consistency with the C front end.
3069
3070    This code is largely lifted from lex_string() in c-lex.c.
3071
3072    FUTURE: ObjC++ will need to handle @-strings here.  */
3073 static tree
3074 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3075 {
3076   tree value;
3077   size_t count;
3078   struct obstack str_ob;
3079   cpp_string str, istr, *strs;
3080   cp_token *tok;
3081   enum cpp_ttype type;
3082
3083   tok = cp_lexer_peek_token (parser->lexer);
3084   if (!cp_parser_is_string_literal (tok))
3085     {
3086       cp_parser_error (parser, "expected string-literal");
3087       return error_mark_node;
3088     }
3089
3090   type = tok->type;
3091
3092   /* Try to avoid the overhead of creating and destroying an obstack
3093      for the common case of just one string.  */
3094   if (!cp_parser_is_string_literal
3095       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3096     {
3097       cp_lexer_consume_token (parser->lexer);
3098
3099       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3100       str.len = TREE_STRING_LENGTH (tok->u.value);
3101       count = 1;
3102
3103       strs = &str;
3104     }
3105   else
3106     {
3107       gcc_obstack_init (&str_ob);
3108       count = 0;
3109
3110       do
3111         {
3112           cp_lexer_consume_token (parser->lexer);
3113           count++;
3114           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3115           str.len = TREE_STRING_LENGTH (tok->u.value);
3116
3117           if (type != tok->type)
3118             {
3119               if (type == CPP_STRING)
3120                 type = tok->type;
3121               else if (tok->type != CPP_STRING)
3122                 error_at (tok->location,
3123                           "unsupported non-standard concatenation "
3124                           "of string literals");
3125             }
3126
3127           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3128
3129           tok = cp_lexer_peek_token (parser->lexer);
3130         }
3131       while (cp_parser_is_string_literal (tok));
3132
3133       strs = (cpp_string *) obstack_finish (&str_ob);
3134     }
3135
3136   if (type != CPP_STRING && !wide_ok)
3137     {
3138       cp_parser_error (parser, "a wide string is invalid in this context");
3139       type = CPP_STRING;
3140     }
3141
3142   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3143       (parse_in, strs, count, &istr, type))
3144     {
3145       value = build_string (istr.len, (const char *)istr.text);
3146       free (CONST_CAST (unsigned char *, istr.text));
3147
3148       switch (type)
3149         {
3150         default:
3151         case CPP_STRING:
3152         case CPP_UTF8STRING:
3153           TREE_TYPE (value) = char_array_type_node;
3154           break;
3155         case CPP_STRING16:
3156           TREE_TYPE (value) = char16_array_type_node;
3157           break;
3158         case CPP_STRING32:
3159           TREE_TYPE (value) = char32_array_type_node;
3160           break;
3161         case CPP_WSTRING:
3162           TREE_TYPE (value) = wchar_array_type_node;
3163           break;
3164         }
3165
3166       value = fix_string_type (value);
3167     }
3168   else
3169     /* cpp_interpret_string has issued an error.  */
3170     value = error_mark_node;
3171
3172   if (count > 1)
3173     obstack_free (&str_ob, 0);
3174
3175   return value;
3176 }
3177
3178
3179 /* Basic concepts [gram.basic]  */
3180
3181 /* Parse a translation-unit.
3182
3183    translation-unit:
3184      declaration-seq [opt]
3185
3186    Returns TRUE if all went well.  */
3187
3188 static bool
3189 cp_parser_translation_unit (cp_parser* parser)
3190 {
3191   /* The address of the first non-permanent object on the declarator
3192      obstack.  */
3193   static void *declarator_obstack_base;
3194
3195   bool success;
3196
3197   /* Create the declarator obstack, if necessary.  */
3198   if (!cp_error_declarator)
3199     {
3200       gcc_obstack_init (&declarator_obstack);
3201       /* Create the error declarator.  */
3202       cp_error_declarator = make_declarator (cdk_error);
3203       /* Create the empty parameter list.  */
3204       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3205       /* Remember where the base of the declarator obstack lies.  */
3206       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3207     }
3208
3209   cp_parser_declaration_seq_opt (parser);
3210
3211   /* If there are no tokens left then all went well.  */
3212   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3213     {
3214       /* Get rid of the token array; we don't need it any more.  */
3215       cp_lexer_destroy (parser->lexer);
3216       parser->lexer = NULL;
3217
3218       /* This file might have been a context that's implicitly extern
3219          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3220       if (parser->implicit_extern_c)
3221         {
3222           pop_lang_context ();
3223           parser->implicit_extern_c = false;
3224         }
3225
3226       /* Finish up.  */
3227       finish_translation_unit ();
3228
3229       success = true;
3230     }
3231   else
3232     {
3233       cp_parser_error (parser, "expected declaration");
3234       success = false;
3235     }
3236
3237   /* Make sure the declarator obstack was fully cleaned up.  */
3238   gcc_assert (obstack_next_free (&declarator_obstack)
3239               == declarator_obstack_base);
3240
3241   /* All went well.  */
3242   return success;
3243 }
3244
3245 /* Expressions [gram.expr] */
3246
3247 /* Parse a primary-expression.
3248
3249    primary-expression:
3250      literal
3251      this
3252      ( expression )
3253      id-expression
3254
3255    GNU Extensions:
3256
3257    primary-expression:
3258      ( compound-statement )
3259      __builtin_va_arg ( assignment-expression , type-id )
3260      __builtin_offsetof ( type-id , offsetof-expression )
3261
3262    C++ Extensions:
3263      __has_nothrow_assign ( type-id )   
3264      __has_nothrow_constructor ( type-id )
3265      __has_nothrow_copy ( type-id )
3266      __has_trivial_assign ( type-id )   
3267      __has_trivial_constructor ( type-id )
3268      __has_trivial_copy ( type-id )
3269      __has_trivial_destructor ( type-id )
3270      __has_virtual_destructor ( type-id )     
3271      __is_abstract ( type-id )
3272      __is_base_of ( type-id , type-id )
3273      __is_class ( type-id )
3274      __is_convertible_to ( type-id , type-id )     
3275      __is_empty ( type-id )
3276      __is_enum ( type-id )
3277      __is_literal_type ( type-id )
3278      __is_pod ( type-id )
3279      __is_polymorphic ( type-id )
3280      __is_std_layout ( type-id )
3281      __is_trivial ( type-id )
3282      __is_union ( type-id )
3283
3284    Objective-C++ Extension:
3285
3286    primary-expression:
3287      objc-expression
3288
3289    literal:
3290      __null
3291
3292    ADDRESS_P is true iff this expression was immediately preceded by
3293    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3294    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3295    true iff this expression is a template argument.
3296
3297    Returns a representation of the expression.  Upon return, *IDK
3298    indicates what kind of id-expression (if any) was present.  */
3299
3300 static tree
3301 cp_parser_primary_expression (cp_parser *parser,
3302                               bool address_p,
3303                               bool cast_p,
3304                               bool template_arg_p,
3305                               cp_id_kind *idk)
3306 {
3307   cp_token *token = NULL;
3308
3309   /* Assume the primary expression is not an id-expression.  */
3310   *idk = CP_ID_KIND_NONE;
3311
3312   /* Peek at the next token.  */
3313   token = cp_lexer_peek_token (parser->lexer);
3314   switch (token->type)
3315     {
3316       /* literal:
3317            integer-literal
3318            character-literal
3319            floating-literal
3320            string-literal
3321            boolean-literal  */
3322     case CPP_CHAR:
3323     case CPP_CHAR16:
3324     case CPP_CHAR32:
3325     case CPP_WCHAR:
3326     case CPP_NUMBER:
3327       token = cp_lexer_consume_token (parser->lexer);
3328       if (TREE_CODE (token->u.value) == FIXED_CST)
3329         {
3330           error_at (token->location,
3331                     "fixed-point types not supported in C++");
3332           return error_mark_node;
3333         }
3334       /* Floating-point literals are only allowed in an integral
3335          constant expression if they are cast to an integral or
3336          enumeration type.  */
3337       if (TREE_CODE (token->u.value) == REAL_CST
3338           && parser->integral_constant_expression_p
3339           && pedantic)
3340         {
3341           /* CAST_P will be set even in invalid code like "int(2.7 +
3342              ...)".   Therefore, we have to check that the next token
3343              is sure to end the cast.  */
3344           if (cast_p)
3345             {
3346               cp_token *next_token;
3347
3348               next_token = cp_lexer_peek_token (parser->lexer);
3349               if (/* The comma at the end of an
3350                      enumerator-definition.  */
3351                   next_token->type != CPP_COMMA
3352                   /* The curly brace at the end of an enum-specifier.  */
3353                   && next_token->type != CPP_CLOSE_BRACE
3354                   /* The end of a statement.  */
3355                   && next_token->type != CPP_SEMICOLON
3356                   /* The end of the cast-expression.  */
3357                   && next_token->type != CPP_CLOSE_PAREN
3358                   /* The end of an array bound.  */
3359                   && next_token->type != CPP_CLOSE_SQUARE
3360                   /* The closing ">" in a template-argument-list.  */
3361                   && (next_token->type != CPP_GREATER
3362                       || parser->greater_than_is_operator_p)
3363                   /* C++0x only: A ">>" treated like two ">" tokens,
3364                      in a template-argument-list.  */
3365                   && (next_token->type != CPP_RSHIFT
3366                       || (cxx_dialect == cxx98)
3367                       || parser->greater_than_is_operator_p))
3368                 cast_p = false;
3369             }
3370
3371           /* If we are within a cast, then the constraint that the
3372              cast is to an integral or enumeration type will be
3373              checked at that point.  If we are not within a cast, then
3374              this code is invalid.  */
3375           if (!cast_p)
3376             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3377         }
3378       return token->u.value;
3379
3380     case CPP_STRING:
3381     case CPP_STRING16:
3382     case CPP_STRING32:
3383     case CPP_WSTRING:
3384     case CPP_UTF8STRING:
3385       /* ??? Should wide strings be allowed when parser->translate_strings_p
3386          is false (i.e. in attributes)?  If not, we can kill the third
3387          argument to cp_parser_string_literal.  */
3388       return cp_parser_string_literal (parser,
3389                                        parser->translate_strings_p,
3390                                        true);
3391
3392     case CPP_OPEN_PAREN:
3393       {
3394         tree expr;
3395         bool saved_greater_than_is_operator_p;
3396
3397         /* Consume the `('.  */
3398         cp_lexer_consume_token (parser->lexer);
3399         /* Within a parenthesized expression, a `>' token is always
3400            the greater-than operator.  */
3401         saved_greater_than_is_operator_p
3402           = parser->greater_than_is_operator_p;
3403         parser->greater_than_is_operator_p = true;
3404         /* If we see `( { ' then we are looking at the beginning of
3405            a GNU statement-expression.  */
3406         if (cp_parser_allow_gnu_extensions_p (parser)
3407             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3408           {
3409             /* Statement-expressions are not allowed by the standard.  */
3410             pedwarn (token->location, OPT_pedantic, 
3411                      "ISO C++ forbids braced-groups within expressions");
3412
3413             /* And they're not allowed outside of a function-body; you
3414                cannot, for example, write:
3415
3416                  int i = ({ int j = 3; j + 1; });
3417
3418                at class or namespace scope.  */
3419             if (!parser->in_function_body
3420                 || parser->in_template_argument_list_p)
3421               {
3422                 error_at (token->location,
3423                           "statement-expressions are not allowed outside "
3424                           "functions nor in template-argument lists");
3425                 cp_parser_skip_to_end_of_block_or_statement (parser);
3426                 expr = error_mark_node;
3427               }
3428             else
3429               {
3430                 /* Start the statement-expression.  */
3431                 expr = begin_stmt_expr ();
3432                 /* Parse the compound-statement.  */
3433                 cp_parser_compound_statement (parser, expr, false, false);
3434                 /* Finish up.  */
3435                 expr = finish_stmt_expr (expr, false);
3436               }
3437           }
3438         else
3439           {
3440             /* Parse the parenthesized expression.  */
3441             expr = cp_parser_expression (parser, cast_p, idk);
3442             /* Let the front end know that this expression was
3443                enclosed in parentheses. This matters in case, for
3444                example, the expression is of the form `A::B', since
3445                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3446                not.  */
3447             finish_parenthesized_expr (expr);
3448             /* DR 705: Wrapping an unqualified name in parentheses
3449                suppresses arg-dependent lookup.  We want to pass back
3450                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3451                (c++/37862), but none of the others.  */
3452             if (*idk != CP_ID_KIND_QUALIFIED)
3453               *idk = CP_ID_KIND_NONE;
3454           }
3455         /* The `>' token might be the end of a template-id or
3456            template-parameter-list now.  */
3457         parser->greater_than_is_operator_p
3458           = saved_greater_than_is_operator_p;
3459         /* Consume the `)'.  */
3460         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3461           cp_parser_skip_to_end_of_statement (parser);
3462
3463         return expr;
3464       }
3465
3466     case CPP_OPEN_SQUARE:
3467       if (c_dialect_objc ())
3468         /* We have an Objective-C++ message. */
3469         return cp_parser_objc_expression (parser);
3470       {
3471         tree lam = cp_parser_lambda_expression (parser);
3472         /* Don't warn about a failed tentative parse.  */
3473         if (cp_parser_error_occurred (parser))
3474           return error_mark_node;
3475         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3476         return lam;
3477       }
3478
3479     case CPP_OBJC_STRING:
3480       if (c_dialect_objc ())
3481         /* We have an Objective-C++ string literal. */
3482         return cp_parser_objc_expression (parser);
3483       cp_parser_error (parser, "expected primary-expression");
3484       return error_mark_node;
3485
3486     case CPP_KEYWORD:
3487       switch (token->keyword)
3488         {
3489           /* These two are the boolean literals.  */
3490         case RID_TRUE:
3491           cp_lexer_consume_token (parser->lexer);
3492           return boolean_true_node;
3493         case RID_FALSE:
3494           cp_lexer_consume_token (parser->lexer);
3495           return boolean_false_node;
3496
3497           /* The `__null' literal.  */
3498         case RID_NULL:
3499           cp_lexer_consume_token (parser->lexer);
3500           return null_node;
3501
3502           /* The `nullptr' literal.  */
3503         case RID_NULLPTR:
3504           cp_lexer_consume_token (parser->lexer);
3505           return nullptr_node;
3506
3507           /* Recognize the `this' keyword.  */
3508         case RID_THIS:
3509           cp_lexer_consume_token (parser->lexer);
3510           if (parser->local_variables_forbidden_p)
3511             {
3512               error_at (token->location,
3513                         "%<this%> may not be used in this context");
3514               return error_mark_node;
3515             }
3516           /* Pointers cannot appear in constant-expressions.  */
3517           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3518             return error_mark_node;
3519           return finish_this_expr ();
3520
3521           /* The `operator' keyword can be the beginning of an
3522              id-expression.  */
3523         case RID_OPERATOR:
3524           goto id_expression;
3525
3526         case RID_FUNCTION_NAME:
3527         case RID_PRETTY_FUNCTION_NAME:
3528         case RID_C99_FUNCTION_NAME:
3529           {
3530             non_integral_constant name;
3531
3532             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3533                __func__ are the names of variables -- but they are
3534                treated specially.  Therefore, they are handled here,
3535                rather than relying on the generic id-expression logic
3536                below.  Grammatically, these names are id-expressions.
3537
3538                Consume the token.  */
3539             token = cp_lexer_consume_token (parser->lexer);
3540
3541             switch (token->keyword)
3542               {
3543               case RID_FUNCTION_NAME:
3544                 name = NIC_FUNC_NAME;
3545                 break;
3546               case RID_PRETTY_FUNCTION_NAME:
3547                 name = NIC_PRETTY_FUNC;
3548                 break;
3549               case RID_C99_FUNCTION_NAME:
3550                 name = NIC_C99_FUNC;
3551                 break;
3552               default:
3553                 gcc_unreachable ();
3554               }
3555
3556             if (cp_parser_non_integral_constant_expression (parser, name))
3557               return error_mark_node;
3558
3559             /* Look up the name.  */
3560             return finish_fname (token->u.value);
3561           }
3562
3563         case RID_VA_ARG:
3564           {
3565             tree expression;
3566             tree type;
3567
3568             /* The `__builtin_va_arg' construct is used to handle
3569                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3570             cp_lexer_consume_token (parser->lexer);
3571             /* Look for the opening `('.  */
3572             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3573             /* Now, parse the assignment-expression.  */
3574             expression = cp_parser_assignment_expression (parser,
3575                                                           /*cast_p=*/false, NULL);
3576             /* Look for the `,'.  */
3577             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3578             /* Parse the type-id.  */
3579             type = cp_parser_type_id (parser);
3580             /* Look for the closing `)'.  */
3581             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3582             /* Using `va_arg' in a constant-expression is not
3583                allowed.  */
3584             if (cp_parser_non_integral_constant_expression (parser,
3585                                                             NIC_VA_ARG))
3586               return error_mark_node;
3587             return build_x_va_arg (expression, type);
3588           }
3589
3590         case RID_OFFSETOF:
3591           return cp_parser_builtin_offsetof (parser);
3592
3593         case RID_HAS_NOTHROW_ASSIGN:
3594         case RID_HAS_NOTHROW_CONSTRUCTOR:
3595         case RID_HAS_NOTHROW_COPY:        
3596         case RID_HAS_TRIVIAL_ASSIGN:
3597         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3598         case RID_HAS_TRIVIAL_COPY:        
3599         case RID_HAS_TRIVIAL_DESTRUCTOR:
3600         case RID_HAS_VIRTUAL_DESTRUCTOR:
3601         case RID_IS_ABSTRACT:
3602         case RID_IS_BASE_OF:
3603         case RID_IS_CLASS:
3604         case RID_IS_CONVERTIBLE_TO:
3605         case RID_IS_EMPTY:
3606         case RID_IS_ENUM:
3607         case RID_IS_LITERAL_TYPE:
3608         case RID_IS_POD:
3609         case RID_IS_POLYMORPHIC:
3610         case RID_IS_STD_LAYOUT:
3611         case RID_IS_TRIVIAL:
3612         case RID_IS_UNION:
3613           return cp_parser_trait_expr (parser, token->keyword);
3614
3615         /* Objective-C++ expressions.  */
3616         case RID_AT_ENCODE:
3617         case RID_AT_PROTOCOL:
3618         case RID_AT_SELECTOR:
3619           return cp_parser_objc_expression (parser);
3620
3621         case RID_TEMPLATE:
3622           if (parser->in_function_body
3623               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3624                   == CPP_LESS))
3625             {
3626               error_at (token->location,
3627                         "a template declaration cannot appear at block scope");
3628               cp_parser_skip_to_end_of_block_or_statement (parser);
3629               return error_mark_node;
3630             }
3631         default:
3632           cp_parser_error (parser, "expected primary-expression");
3633           return error_mark_node;
3634         }
3635
3636       /* An id-expression can start with either an identifier, a
3637          `::' as the beginning of a qualified-id, or the "operator"
3638          keyword.  */
3639     case CPP_NAME:
3640     case CPP_SCOPE:
3641     case CPP_TEMPLATE_ID:
3642     case CPP_NESTED_NAME_SPECIFIER:
3643       {
3644         tree id_expression;
3645         tree decl;
3646         const char *error_msg;
3647         bool template_p;
3648         bool done;
3649         cp_token *id_expr_token;
3650
3651       id_expression:
3652         /* Parse the id-expression.  */
3653         id_expression
3654           = cp_parser_id_expression (parser,
3655                                      /*template_keyword_p=*/false,
3656                                      /*check_dependency_p=*/true,
3657                                      &template_p,
3658                                      /*declarator_p=*/false,
3659                                      /*optional_p=*/false);
3660         if (id_expression == error_mark_node)
3661           return error_mark_node;
3662         id_expr_token = token;
3663         token = cp_lexer_peek_token (parser->lexer);
3664         done = (token->type != CPP_OPEN_SQUARE
3665                 && token->type != CPP_OPEN_PAREN
3666                 && token->type != CPP_DOT
3667                 && token->type != CPP_DEREF
3668                 && token->type != CPP_PLUS_PLUS
3669                 && token->type != CPP_MINUS_MINUS);
3670         /* If we have a template-id, then no further lookup is
3671            required.  If the template-id was for a template-class, we
3672            will sometimes have a TYPE_DECL at this point.  */
3673         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3674                  || TREE_CODE (id_expression) == TYPE_DECL)
3675           decl = id_expression;
3676         /* Look up the name.  */
3677         else
3678           {
3679             tree ambiguous_decls;
3680
3681             /* If we already know that this lookup is ambiguous, then
3682                we've already issued an error message; there's no reason
3683                to check again.  */
3684             if (id_expr_token->type == CPP_NAME
3685                 && id_expr_token->ambiguous_p)
3686               {
3687                 cp_parser_simulate_error (parser);
3688                 return error_mark_node;
3689               }
3690
3691             decl = cp_parser_lookup_name (parser, id_expression,
3692                                           none_type,
3693                                           template_p,
3694                                           /*is_namespace=*/false,
3695                                           /*check_dependency=*/true,
3696                                           &ambiguous_decls,
3697                                           id_expr_token->location);
3698             /* If the lookup was ambiguous, an error will already have
3699                been issued.  */
3700             if (ambiguous_decls)
3701               return error_mark_node;
3702
3703             /* In Objective-C++, we may have an Objective-C 2.0
3704                dot-syntax for classes here.  */
3705             if (c_dialect_objc ()
3706                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3707                 && TREE_CODE (decl) == TYPE_DECL
3708                 && objc_is_class_name (decl))
3709               {
3710                 tree component;
3711                 cp_lexer_consume_token (parser->lexer);
3712                 component = cp_parser_identifier (parser);
3713                 if (component == error_mark_node)
3714                   return error_mark_node;
3715
3716                 return objc_build_class_component_ref (id_expression, component);
3717               }
3718
3719             /* In Objective-C++, an instance variable (ivar) may be preferred
3720                to whatever cp_parser_lookup_name() found.  */
3721             decl = objc_lookup_ivar (decl, id_expression);
3722
3723             /* If name lookup gives us a SCOPE_REF, then the
3724                qualifying scope was dependent.  */
3725             if (TREE_CODE (decl) == SCOPE_REF)
3726               {
3727                 /* At this point, we do not know if DECL is a valid
3728                    integral constant expression.  We assume that it is
3729                    in fact such an expression, so that code like:
3730
3731                       template <int N> struct A {
3732                         int a[B<N>::i];
3733                       };
3734                      
3735                    is accepted.  At template-instantiation time, we
3736                    will check that B<N>::i is actually a constant.  */
3737                 return decl;
3738               }
3739             /* Check to see if DECL is a local variable in a context
3740                where that is forbidden.  */
3741             if (parser->local_variables_forbidden_p
3742                 && local_variable_p (decl))
3743               {
3744                 /* It might be that we only found DECL because we are
3745                    trying to be generous with pre-ISO scoping rules.
3746                    For example, consider:
3747
3748                      int i;
3749                      void g() {
3750                        for (int i = 0; i < 10; ++i) {}
3751                        extern void f(int j = i);
3752                      }
3753
3754                    Here, name look up will originally find the out
3755                    of scope `i'.  We need to issue a warning message,
3756                    but then use the global `i'.  */
3757                 decl = check_for_out_of_scope_variable (decl);
3758                 if (local_variable_p (decl))
3759                   {
3760                     error_at (id_expr_token->location,
3761                               "local variable %qD may not appear in this context",
3762                               decl);
3763                     return error_mark_node;
3764                   }
3765               }
3766           }
3767
3768         decl = (finish_id_expression
3769                 (id_expression, decl, parser->scope,
3770                  idk,
3771                  parser->integral_constant_expression_p,
3772                  parser->allow_non_integral_constant_expression_p,
3773                  &parser->non_integral_constant_expression_p,
3774                  template_p, done, address_p,
3775                  template_arg_p,
3776                  &error_msg,
3777                  id_expr_token->location));
3778         if (error_msg)
3779           cp_parser_error (parser, error_msg);
3780         return decl;
3781       }
3782
3783       /* Anything else is an error.  */
3784     default:
3785       cp_parser_error (parser, "expected primary-expression");
3786       return error_mark_node;
3787     }
3788 }
3789
3790 /* Parse an id-expression.
3791
3792    id-expression:
3793      unqualified-id
3794      qualified-id
3795
3796    qualified-id:
3797      :: [opt] nested-name-specifier template [opt] unqualified-id
3798      :: identifier
3799      :: operator-function-id
3800      :: template-id
3801
3802    Return a representation of the unqualified portion of the
3803    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3804    a `::' or nested-name-specifier.
3805
3806    Often, if the id-expression was a qualified-id, the caller will
3807    want to make a SCOPE_REF to represent the qualified-id.  This
3808    function does not do this in order to avoid wastefully creating
3809    SCOPE_REFs when they are not required.
3810
3811    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3812    `template' keyword.
3813
3814    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3815    uninstantiated templates.
3816
3817    If *TEMPLATE_P is non-NULL, it is set to true iff the
3818    `template' keyword is used to explicitly indicate that the entity
3819    named is a template.
3820
3821    If DECLARATOR_P is true, the id-expression is appearing as part of
3822    a declarator, rather than as part of an expression.  */
3823
3824 static tree
3825 cp_parser_id_expression (cp_parser *parser,
3826                          bool template_keyword_p,
3827                          bool check_dependency_p,
3828                          bool *template_p,
3829                          bool declarator_p,
3830                          bool optional_p)
3831 {
3832   bool global_scope_p;
3833   bool nested_name_specifier_p;
3834
3835   /* Assume the `template' keyword was not used.  */
3836   if (template_p)
3837     *template_p = template_keyword_p;
3838
3839   /* Look for the optional `::' operator.  */
3840   global_scope_p
3841     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3842        != NULL_TREE);
3843   /* Look for the optional nested-name-specifier.  */
3844   nested_name_specifier_p
3845     = (cp_parser_nested_name_specifier_opt (parser,
3846                                             /*typename_keyword_p=*/false,
3847                                             check_dependency_p,
3848                                             /*type_p=*/false,
3849                                             declarator_p)
3850        != NULL_TREE);
3851   /* If there is a nested-name-specifier, then we are looking at
3852      the first qualified-id production.  */
3853   if (nested_name_specifier_p)
3854     {
3855       tree saved_scope;
3856       tree saved_object_scope;
3857       tree saved_qualifying_scope;
3858       tree unqualified_id;
3859       bool is_template;
3860
3861       /* See if the next token is the `template' keyword.  */
3862       if (!template_p)
3863         template_p = &is_template;
3864       *template_p = cp_parser_optional_template_keyword (parser);
3865       /* Name lookup we do during the processing of the
3866          unqualified-id might obliterate SCOPE.  */
3867       saved_scope = parser->scope;
3868       saved_object_scope = parser->object_scope;
3869       saved_qualifying_scope = parser->qualifying_scope;
3870       /* Process the final unqualified-id.  */
3871       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3872                                                  check_dependency_p,
3873                                                  declarator_p,
3874                                                  /*optional_p=*/false);
3875       /* Restore the SAVED_SCOPE for our caller.  */
3876       parser->scope = saved_scope;
3877       parser->object_scope = saved_object_scope;
3878       parser->qualifying_scope = saved_qualifying_scope;
3879
3880       return unqualified_id;
3881     }
3882   /* Otherwise, if we are in global scope, then we are looking at one
3883      of the other qualified-id productions.  */
3884   else if (global_scope_p)
3885     {
3886       cp_token *token;
3887       tree id;
3888
3889       /* Peek at the next token.  */
3890       token = cp_lexer_peek_token (parser->lexer);
3891
3892       /* If it's an identifier, and the next token is not a "<", then
3893          we can avoid the template-id case.  This is an optimization
3894          for this common case.  */
3895       if (token->type == CPP_NAME
3896           && !cp_parser_nth_token_starts_template_argument_list_p
3897                (parser, 2))
3898         return cp_parser_identifier (parser);
3899
3900       cp_parser_parse_tentatively (parser);
3901       /* Try a template-id.  */
3902       id = cp_parser_template_id (parser,
3903                                   /*template_keyword_p=*/false,
3904                                   /*check_dependency_p=*/true,
3905                                   declarator_p);
3906       /* If that worked, we're done.  */
3907       if (cp_parser_parse_definitely (parser))
3908         return id;
3909
3910       /* Peek at the next token.  (Changes in the token buffer may
3911          have invalidated the pointer obtained above.)  */
3912       token = cp_lexer_peek_token (parser->lexer);
3913
3914       switch (token->type)
3915         {
3916         case CPP_NAME:
3917           return cp_parser_identifier (parser);
3918
3919         case CPP_KEYWORD:
3920           if (token->keyword == RID_OPERATOR)
3921             return cp_parser_operator_function_id (parser);
3922           /* Fall through.  */
3923
3924         default:
3925           cp_parser_error (parser, "expected id-expression");
3926           return error_mark_node;
3927         }
3928     }
3929   else
3930     return cp_parser_unqualified_id (parser, template_keyword_p,
3931                                      /*check_dependency_p=*/true,
3932                                      declarator_p,
3933                                      optional_p);
3934 }
3935
3936 /* Parse an unqualified-id.
3937
3938    unqualified-id:
3939      identifier
3940      operator-function-id
3941      conversion-function-id
3942      ~ class-name
3943      template-id
3944
3945    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3946    keyword, in a construct like `A::template ...'.
3947
3948    Returns a representation of unqualified-id.  For the `identifier'
3949    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3950    production a BIT_NOT_EXPR is returned; the operand of the
3951    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3952    other productions, see the documentation accompanying the
3953    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3954    names are looked up in uninstantiated templates.  If DECLARATOR_P
3955    is true, the unqualified-id is appearing as part of a declarator,
3956    rather than as part of an expression.  */
3957
3958 static tree
3959 cp_parser_unqualified_id (cp_parser* parser,
3960                           bool template_keyword_p,
3961                           bool check_dependency_p,
3962                           bool declarator_p,
3963                           bool optional_p)
3964 {
3965   cp_token *token;
3966
3967   /* Peek at the next token.  */
3968   token = cp_lexer_peek_token (parser->lexer);
3969
3970   switch (token->type)
3971     {
3972     case CPP_NAME:
3973       {
3974         tree id;
3975
3976         /* We don't know yet whether or not this will be a
3977            template-id.  */
3978         cp_parser_parse_tentatively (parser);
3979         /* Try a template-id.  */
3980         id = cp_parser_template_id (parser, template_keyword_p,
3981                                     check_dependency_p,
3982                                     declarator_p);
3983         /* If it worked, we're done.  */
3984         if (cp_parser_parse_definitely (parser))
3985           return id;
3986         /* Otherwise, it's an ordinary identifier.  */
3987         return cp_parser_identifier (parser);
3988       }
3989
3990     case CPP_TEMPLATE_ID:
3991       return cp_parser_template_id (parser, template_keyword_p,
3992                                     check_dependency_p,
3993                                     declarator_p);
3994
3995     case CPP_COMPL:
3996       {
3997         tree type_decl;
3998         tree qualifying_scope;
3999         tree object_scope;
4000         tree scope;
4001         bool done;
4002
4003         /* Consume the `~' token.  */
4004         cp_lexer_consume_token (parser->lexer);
4005         /* Parse the class-name.  The standard, as written, seems to
4006            say that:
4007
4008              template <typename T> struct S { ~S (); };
4009              template <typename T> S<T>::~S() {}
4010
4011            is invalid, since `~' must be followed by a class-name, but
4012            `S<T>' is dependent, and so not known to be a class.
4013            That's not right; we need to look in uninstantiated
4014            templates.  A further complication arises from:
4015
4016              template <typename T> void f(T t) {
4017                t.T::~T();
4018              }
4019
4020            Here, it is not possible to look up `T' in the scope of `T'
4021            itself.  We must look in both the current scope, and the
4022            scope of the containing complete expression.
4023
4024            Yet another issue is:
4025
4026              struct S {
4027                int S;
4028                ~S();
4029              };
4030
4031              S::~S() {}
4032
4033            The standard does not seem to say that the `S' in `~S'
4034            should refer to the type `S' and not the data member
4035            `S::S'.  */
4036
4037         /* DR 244 says that we look up the name after the "~" in the
4038            same scope as we looked up the qualifying name.  That idea
4039            isn't fully worked out; it's more complicated than that.  */
4040         scope = parser->scope;
4041         object_scope = parser->object_scope;
4042         qualifying_scope = parser->qualifying_scope;
4043
4044         /* Check for invalid scopes.  */
4045         if (scope == error_mark_node)
4046           {
4047             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4048               cp_lexer_consume_token (parser->lexer);
4049             return error_mark_node;
4050           }
4051         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4052           {
4053             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4054               error_at (token->location,
4055                         "scope %qT before %<~%> is not a class-name",
4056                         scope);
4057             cp_parser_simulate_error (parser);
4058             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4059               cp_lexer_consume_token (parser->lexer);
4060             return error_mark_node;
4061           }
4062         gcc_assert (!scope || TYPE_P (scope));
4063
4064         /* If the name is of the form "X::~X" it's OK even if X is a
4065            typedef.  */
4066         token = cp_lexer_peek_token (parser->lexer);
4067         if (scope
4068             && token->type == CPP_NAME
4069             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4070                 != CPP_LESS)
4071             && (token->u.value == TYPE_IDENTIFIER (scope)
4072                 || (CLASS_TYPE_P (scope)
4073                     && constructor_name_p (token->u.value, scope))))
4074           {
4075             cp_lexer_consume_token (parser->lexer);
4076             return build_nt (BIT_NOT_EXPR, scope);
4077           }
4078
4079         /* If there was an explicit qualification (S::~T), first look
4080            in the scope given by the qualification (i.e., S).
4081
4082            Note: in the calls to cp_parser_class_name below we pass
4083            typename_type so that lookup finds the injected-class-name
4084            rather than the constructor.  */
4085         done = false;
4086         type_decl = NULL_TREE;
4087         if (scope)
4088           {
4089             cp_parser_parse_tentatively (parser);
4090             type_decl = cp_parser_class_name (parser,
4091                                               /*typename_keyword_p=*/false,
4092                                               /*template_keyword_p=*/false,
4093                                               typename_type,
4094                                               /*check_dependency=*/false,
4095                                               /*class_head_p=*/false,
4096                                               declarator_p);
4097             if (cp_parser_parse_definitely (parser))
4098               done = true;
4099           }
4100         /* In "N::S::~S", look in "N" as well.  */
4101         if (!done && scope && qualifying_scope)
4102           {
4103             cp_parser_parse_tentatively (parser);
4104             parser->scope = qualifying_scope;
4105             parser->object_scope = NULL_TREE;
4106             parser->qualifying_scope = NULL_TREE;
4107             type_decl
4108               = cp_parser_class_name (parser,
4109                                       /*typename_keyword_p=*/false,
4110                                       /*template_keyword_p=*/false,
4111                                       typename_type,
4112                                       /*check_dependency=*/false,
4113                                       /*class_head_p=*/false,
4114                                       declarator_p);
4115             if (cp_parser_parse_definitely (parser))
4116               done = true;
4117           }
4118         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4119         else if (!done && object_scope)
4120           {
4121             cp_parser_parse_tentatively (parser);
4122             parser->scope = object_scope;
4123             parser->object_scope = NULL_TREE;
4124             parser->qualifying_scope = NULL_TREE;
4125             type_decl
4126               = cp_parser_class_name (parser,
4127                                       /*typename_keyword_p=*/false,
4128                                       /*template_keyword_p=*/false,
4129                                       typename_type,
4130                                       /*check_dependency=*/false,
4131                                       /*class_head_p=*/false,
4132                                       declarator_p);
4133             if (cp_parser_parse_definitely (parser))
4134               done = true;
4135           }
4136         /* Look in the surrounding context.  */
4137         if (!done)
4138           {
4139             parser->scope = NULL_TREE;
4140             parser->object_scope = NULL_TREE;
4141             parser->qualifying_scope = NULL_TREE;
4142             if (processing_template_decl)
4143               cp_parser_parse_tentatively (parser);
4144             type_decl
4145               = cp_parser_class_name (parser,
4146                                       /*typename_keyword_p=*/false,
4147                                       /*template_keyword_p=*/false,
4148                                       typename_type,
4149                                       /*check_dependency=*/false,
4150                                       /*class_head_p=*/false,
4151                                       declarator_p);
4152             if (processing_template_decl
4153                 && ! cp_parser_parse_definitely (parser))
4154               {
4155                 /* We couldn't find a type with this name, so just accept
4156                    it and check for a match at instantiation time.  */
4157                 type_decl = cp_parser_identifier (parser);
4158                 if (type_decl != error_mark_node)
4159                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4160                 return type_decl;
4161               }
4162           }
4163         /* If an error occurred, assume that the name of the
4164            destructor is the same as the name of the qualifying
4165            class.  That allows us to keep parsing after running
4166            into ill-formed destructor names.  */
4167         if (type_decl == error_mark_node && scope)
4168           return build_nt (BIT_NOT_EXPR, scope);
4169         else if (type_decl == error_mark_node)
4170           return error_mark_node;
4171
4172         /* Check that destructor name and scope match.  */
4173         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4174           {
4175             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4176               error_at (token->location,
4177                         "declaration of %<~%T%> as member of %qT",
4178                         type_decl, scope);
4179             cp_parser_simulate_error (parser);
4180             return error_mark_node;
4181           }
4182
4183         /* [class.dtor]
4184
4185            A typedef-name that names a class shall not be used as the
4186            identifier in the declarator for a destructor declaration.  */
4187         if (declarator_p
4188             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4189             && !DECL_SELF_REFERENCE_P (type_decl)
4190             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4191           error_at (token->location,
4192                     "typedef-name %qD used as destructor declarator",
4193                     type_decl);
4194
4195         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4196       }
4197
4198     case CPP_KEYWORD:
4199       if (token->keyword == RID_OPERATOR)
4200         {
4201           tree id;
4202
4203           /* This could be a template-id, so we try that first.  */
4204           cp_parser_parse_tentatively (parser);
4205           /* Try a template-id.  */
4206           id = cp_parser_template_id (parser, template_keyword_p,
4207                                       /*check_dependency_p=*/true,
4208                                       declarator_p);
4209           /* If that worked, we're done.  */
4210           if (cp_parser_parse_definitely (parser))
4211             return id;
4212           /* We still don't know whether we're looking at an
4213              operator-function-id or a conversion-function-id.  */
4214           cp_parser_parse_tentatively (parser);
4215           /* Try an operator-function-id.  */
4216           id = cp_parser_operator_function_id (parser);
4217           /* If that didn't work, try a conversion-function-id.  */
4218           if (!cp_parser_parse_definitely (parser))
4219             id = cp_parser_conversion_function_id (parser);
4220
4221           return id;
4222         }
4223       /* Fall through.  */
4224
4225     default:
4226       if (optional_p)
4227         return NULL_TREE;
4228       cp_parser_error (parser, "expected unqualified-id");
4229       return error_mark_node;
4230     }
4231 }
4232
4233 /* Parse an (optional) nested-name-specifier.
4234
4235    nested-name-specifier: [C++98]
4236      class-or-namespace-name :: nested-name-specifier [opt]
4237      class-or-namespace-name :: template nested-name-specifier [opt]
4238
4239    nested-name-specifier: [C++0x]
4240      type-name ::
4241      namespace-name ::
4242      nested-name-specifier identifier ::
4243      nested-name-specifier template [opt] simple-template-id ::
4244
4245    PARSER->SCOPE should be set appropriately before this function is
4246    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4247    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4248    in name lookups.
4249
4250    Sets PARSER->SCOPE to the class (TYPE) or namespace
4251    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4252    it unchanged if there is no nested-name-specifier.  Returns the new
4253    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4254
4255    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4256    part of a declaration and/or decl-specifier.  */
4257
4258 static tree
4259 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4260                                      bool typename_keyword_p,
4261                                      bool check_dependency_p,
4262                                      bool type_p,
4263                                      bool is_declaration)
4264 {
4265   bool success = false;
4266   cp_token_position start = 0;
4267   cp_token *token;
4268
4269   /* Remember where the nested-name-specifier starts.  */
4270   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4271     {
4272       start = cp_lexer_token_position (parser->lexer, false);
4273       push_deferring_access_checks (dk_deferred);
4274     }
4275
4276   while (true)
4277     {
4278       tree new_scope;
4279       tree old_scope;
4280       tree saved_qualifying_scope;
4281       bool template_keyword_p;
4282
4283       /* Spot cases that cannot be the beginning of a
4284          nested-name-specifier.  */
4285       token = cp_lexer_peek_token (parser->lexer);
4286
4287       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4288          the already parsed nested-name-specifier.  */
4289       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4290         {
4291           /* Grab the nested-name-specifier and continue the loop.  */
4292           cp_parser_pre_parsed_nested_name_specifier (parser);
4293           /* If we originally encountered this nested-name-specifier
4294              with IS_DECLARATION set to false, we will not have
4295              resolved TYPENAME_TYPEs, so we must do so here.  */
4296           if (is_declaration
4297               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4298             {
4299               new_scope = resolve_typename_type (parser->scope,
4300                                                  /*only_current_p=*/false);
4301               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4302                 parser->scope = new_scope;
4303             }
4304           success = true;
4305           continue;
4306         }
4307
4308       /* Spot cases that cannot be the beginning of a
4309          nested-name-specifier.  On the second and subsequent times
4310          through the loop, we look for the `template' keyword.  */
4311       if (success && token->keyword == RID_TEMPLATE)
4312         ;
4313       /* A template-id can start a nested-name-specifier.  */
4314       else if (token->type == CPP_TEMPLATE_ID)
4315         ;
4316       else
4317         {
4318           /* If the next token is not an identifier, then it is
4319              definitely not a type-name or namespace-name.  */
4320           if (token->type != CPP_NAME)
4321             break;
4322           /* If the following token is neither a `<' (to begin a
4323              template-id), nor a `::', then we are not looking at a
4324              nested-name-specifier.  */
4325           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4326
4327           if (token->type == CPP_COLON
4328               && parser->colon_corrects_to_scope_p
4329               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4330             {
4331               error_at (token->location,
4332                         "found %<:%> in nested-name-specifier, expected %<::%>");
4333               token->type = CPP_SCOPE;
4334             }
4335
4336           if (token->type != CPP_SCOPE
4337               && !cp_parser_nth_token_starts_template_argument_list_p
4338                   (parser, 2))
4339             break;
4340         }
4341
4342       /* The nested-name-specifier is optional, so we parse
4343          tentatively.  */
4344       cp_parser_parse_tentatively (parser);
4345
4346       /* Look for the optional `template' keyword, if this isn't the
4347          first time through the loop.  */
4348       if (success)
4349         template_keyword_p = cp_parser_optional_template_keyword (parser);
4350       else
4351         template_keyword_p = false;
4352
4353       /* Save the old scope since the name lookup we are about to do
4354          might destroy it.  */
4355       old_scope = parser->scope;
4356       saved_qualifying_scope = parser->qualifying_scope;
4357       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4358          look up names in "X<T>::I" in order to determine that "Y" is
4359          a template.  So, if we have a typename at this point, we make
4360          an effort to look through it.  */
4361       if (is_declaration
4362           && !typename_keyword_p
4363           && parser->scope
4364           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4365         parser->scope = resolve_typename_type (parser->scope,
4366                                                /*only_current_p=*/false);
4367       /* Parse the qualifying entity.  */
4368       new_scope
4369         = cp_parser_qualifying_entity (parser,
4370                                        typename_keyword_p,
4371                                        template_keyword_p,
4372                                        check_dependency_p,
4373                                        type_p,
4374                                        is_declaration);
4375       /* Look for the `::' token.  */
4376       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4377
4378       /* If we found what we wanted, we keep going; otherwise, we're
4379          done.  */
4380       if (!cp_parser_parse_definitely (parser))
4381         {
4382           bool error_p = false;
4383
4384           /* Restore the OLD_SCOPE since it was valid before the
4385              failed attempt at finding the last
4386              class-or-namespace-name.  */
4387           parser->scope = old_scope;
4388           parser->qualifying_scope = saved_qualifying_scope;
4389           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4390             break;
4391           /* If the next token is an identifier, and the one after
4392              that is a `::', then any valid interpretation would have
4393              found a class-or-namespace-name.  */
4394           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4395                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4396                      == CPP_SCOPE)
4397                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4398                      != CPP_COMPL))
4399             {
4400               token = cp_lexer_consume_token (parser->lexer);
4401               if (!error_p)
4402                 {
4403                   if (!token->ambiguous_p)
4404                     {
4405                       tree decl;
4406                       tree ambiguous_decls;
4407
4408                       decl = cp_parser_lookup_name (parser, token->u.value,
4409                                                     none_type,
4410                                                     /*is_template=*/false,
4411                                                     /*is_namespace=*/false,
4412                                                     /*check_dependency=*/true,
4413                                                     &ambiguous_decls,
4414                                                     token->location);
4415                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4416                         error_at (token->location,
4417                                   "%qD used without template parameters",
4418                                   decl);
4419                       else if (ambiguous_decls)
4420                         {
4421                           error_at (token->location,
4422                                     "reference to %qD is ambiguous",
4423                                     token->u.value);
4424                           print_candidates (ambiguous_decls);
4425                           decl = error_mark_node;
4426                         }
4427                       else
4428                         {
4429                           if (cxx_dialect != cxx98)
4430                             cp_parser_name_lookup_error
4431                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4432                              token->location);
4433                           else
4434                             cp_parser_name_lookup_error
4435                             (parser, token->u.value, decl, NLE_CXX98,
4436                              token->location);
4437                         }
4438                     }
4439                   parser->scope = error_mark_node;
4440                   error_p = true;
4441                   /* Treat this as a successful nested-name-specifier
4442                      due to:
4443
4444                      [basic.lookup.qual]
4445
4446                      If the name found is not a class-name (clause
4447                      _class_) or namespace-name (_namespace.def_), the
4448                      program is ill-formed.  */
4449                   success = true;
4450                 }
4451               cp_lexer_consume_token (parser->lexer);
4452             }
4453           break;
4454         }
4455       /* We've found one valid nested-name-specifier.  */
4456       success = true;
4457       /* Name lookup always gives us a DECL.  */
4458       if (TREE_CODE (new_scope) == TYPE_DECL)
4459         new_scope = TREE_TYPE (new_scope);
4460       /* Uses of "template" must be followed by actual templates.  */
4461       if (template_keyword_p
4462           && !(CLASS_TYPE_P (new_scope)
4463                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4464                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4465                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4466           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4467                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4468                    == TEMPLATE_ID_EXPR)))
4469         permerror (input_location, TYPE_P (new_scope)
4470                    ? "%qT is not a template"
4471                    : "%qD is not a template",
4472                    new_scope);
4473       /* If it is a class scope, try to complete it; we are about to
4474          be looking up names inside the class.  */
4475       if (TYPE_P (new_scope)
4476           /* Since checking types for dependency can be expensive,
4477              avoid doing it if the type is already complete.  */
4478           && !COMPLETE_TYPE_P (new_scope)
4479           /* Do not try to complete dependent types.  */
4480           && !dependent_type_p (new_scope))
4481         {
4482           new_scope = complete_type (new_scope);
4483           /* If it is a typedef to current class, use the current
4484              class instead, as the typedef won't have any names inside
4485              it yet.  */
4486           if (!COMPLETE_TYPE_P (new_scope)
4487               && currently_open_class (new_scope))
4488             new_scope = TYPE_MAIN_VARIANT (new_scope);
4489         }
4490       /* Make sure we look in the right scope the next time through
4491          the loop.  */
4492       parser->scope = new_scope;
4493     }
4494
4495   /* If parsing tentatively, replace the sequence of tokens that makes
4496      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4497      token.  That way, should we re-parse the token stream, we will
4498      not have to repeat the effort required to do the parse, nor will
4499      we issue duplicate error messages.  */
4500   if (success && start)
4501     {
4502       cp_token *token;
4503
4504       token = cp_lexer_token_at (parser->lexer, start);
4505       /* Reset the contents of the START token.  */
4506       token->type = CPP_NESTED_NAME_SPECIFIER;
4507       /* Retrieve any deferred checks.  Do not pop this access checks yet
4508          so the memory will not be reclaimed during token replacing below.  */
4509       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4510       token->u.tree_check_value->value = parser->scope;
4511       token->u.tree_check_value->checks = get_deferred_access_checks ();
4512       token->u.tree_check_value->qualifying_scope =
4513         parser->qualifying_scope;
4514       token->keyword = RID_MAX;
4515
4516       /* Purge all subsequent tokens.  */
4517       cp_lexer_purge_tokens_after (parser->lexer, start);
4518     }
4519
4520   if (start)
4521     pop_to_parent_deferring_access_checks ();
4522
4523   return success ? parser->scope : NULL_TREE;
4524 }
4525
4526 /* Parse a nested-name-specifier.  See
4527    cp_parser_nested_name_specifier_opt for details.  This function
4528    behaves identically, except that it will an issue an error if no
4529    nested-name-specifier is present.  */
4530
4531 static tree
4532 cp_parser_nested_name_specifier (cp_parser *parser,
4533                                  bool typename_keyword_p,
4534                                  bool check_dependency_p,
4535                                  bool type_p,
4536                                  bool is_declaration)
4537 {
4538   tree scope;
4539
4540   /* Look for the nested-name-specifier.  */
4541   scope = cp_parser_nested_name_specifier_opt (parser,
4542                                                typename_keyword_p,
4543                                                check_dependency_p,
4544                                                type_p,
4545                                                is_declaration);
4546   /* If it was not present, issue an error message.  */
4547   if (!scope)
4548     {
4549       cp_parser_error (parser, "expected nested-name-specifier");
4550       parser->scope = NULL_TREE;
4551     }
4552
4553   return scope;
4554 }
4555
4556 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4557    this is either a class-name or a namespace-name (which corresponds
4558    to the class-or-namespace-name production in the grammar). For
4559    C++0x, it can also be a type-name that refers to an enumeration
4560    type.
4561
4562    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4563    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4564    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4565    TYPE_P is TRUE iff the next name should be taken as a class-name,
4566    even the same name is declared to be another entity in the same
4567    scope.
4568
4569    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4570    specified by the class-or-namespace-name.  If neither is found the
4571    ERROR_MARK_NODE is returned.  */
4572
4573 static tree
4574 cp_parser_qualifying_entity (cp_parser *parser,
4575                              bool typename_keyword_p,
4576                              bool template_keyword_p,
4577                              bool check_dependency_p,
4578                              bool type_p,
4579                              bool is_declaration)
4580 {
4581   tree saved_scope;
4582   tree saved_qualifying_scope;
4583   tree saved_object_scope;
4584   tree scope;
4585   bool only_class_p;
4586   bool successful_parse_p;
4587
4588   /* Before we try to parse the class-name, we must save away the
4589      current PARSER->SCOPE since cp_parser_class_name will destroy
4590      it.  */
4591   saved_scope = parser->scope;
4592   saved_qualifying_scope = parser->qualifying_scope;
4593   saved_object_scope = parser->object_scope;
4594   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4595      there is no need to look for a namespace-name.  */
4596   only_class_p = template_keyword_p 
4597     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4598   if (!only_class_p)
4599     cp_parser_parse_tentatively (parser);
4600   scope = cp_parser_class_name (parser,
4601                                 typename_keyword_p,
4602                                 template_keyword_p,
4603                                 type_p ? class_type : none_type,
4604                                 check_dependency_p,
4605                                 /*class_head_p=*/false,
4606                                 is_declaration);
4607   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4608   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4609   if (!only_class_p 
4610       && cxx_dialect != cxx98
4611       && !successful_parse_p)
4612     {
4613       /* Restore the saved scope.  */
4614       parser->scope = saved_scope;
4615       parser->qualifying_scope = saved_qualifying_scope;
4616       parser->object_scope = saved_object_scope;
4617
4618       /* Parse tentatively.  */
4619       cp_parser_parse_tentatively (parser);
4620      
4621       /* Parse a typedef-name or enum-name.  */
4622       scope = cp_parser_nonclass_name (parser);
4623
4624       /* "If the name found does not designate a namespace or a class,
4625          enumeration, or dependent type, the program is ill-formed."
4626
4627          We cover classes and dependent types above and namespaces below,
4628          so this code is only looking for enums.  */
4629       if (!scope || TREE_CODE (scope) != TYPE_DECL
4630           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4631         cp_parser_simulate_error (parser);
4632
4633       successful_parse_p = cp_parser_parse_definitely (parser);
4634     }
4635   /* If that didn't work, try for a namespace-name.  */
4636   if (!only_class_p && !successful_parse_p)
4637     {
4638       /* Restore the saved scope.  */
4639       parser->scope = saved_scope;
4640       parser->qualifying_scope = saved_qualifying_scope;
4641       parser->object_scope = saved_object_scope;
4642       /* If we are not looking at an identifier followed by the scope
4643          resolution operator, then this is not part of a
4644          nested-name-specifier.  (Note that this function is only used
4645          to parse the components of a nested-name-specifier.)  */
4646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4647           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4648         return error_mark_node;
4649       scope = cp_parser_namespace_name (parser);
4650     }
4651
4652   return scope;
4653 }
4654
4655 /* Parse a postfix-expression.
4656
4657    postfix-expression:
4658      primary-expression
4659      postfix-expression [ expression ]
4660      postfix-expression ( expression-list [opt] )
4661      simple-type-specifier ( expression-list [opt] )
4662      typename :: [opt] nested-name-specifier identifier
4663        ( expression-list [opt] )
4664      typename :: [opt] nested-name-specifier template [opt] template-id
4665        ( expression-list [opt] )
4666      postfix-expression . template [opt] id-expression
4667      postfix-expression -> template [opt] id-expression
4668      postfix-expression . pseudo-destructor-name
4669      postfix-expression -> pseudo-destructor-name
4670      postfix-expression ++
4671      postfix-expression --
4672      dynamic_cast < type-id > ( expression )
4673      static_cast < type-id > ( expression )
4674      reinterpret_cast < type-id > ( expression )
4675      const_cast < type-id > ( expression )
4676      typeid ( expression )
4677      typeid ( type-id )
4678
4679    GNU Extension:
4680
4681    postfix-expression:
4682      ( type-id ) { initializer-list , [opt] }
4683
4684    This extension is a GNU version of the C99 compound-literal
4685    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4686    but they are essentially the same concept.)
4687
4688    If ADDRESS_P is true, the postfix expression is the operand of the
4689    `&' operator.  CAST_P is true if this expression is the target of a
4690    cast.
4691
4692    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4693    class member access expressions [expr.ref].
4694
4695    Returns a representation of the expression.  */
4696
4697 static tree
4698 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4699                               bool member_access_only_p,
4700                               cp_id_kind * pidk_return)
4701 {
4702   cp_token *token;
4703   enum rid keyword;
4704   cp_id_kind idk = CP_ID_KIND_NONE;
4705   tree postfix_expression = NULL_TREE;
4706   bool is_member_access = false;
4707
4708   /* Peek at the next token.  */
4709   token = cp_lexer_peek_token (parser->lexer);
4710   /* Some of the productions are determined by keywords.  */
4711   keyword = token->keyword;
4712   switch (keyword)
4713     {
4714     case RID_DYNCAST:
4715     case RID_STATCAST:
4716     case RID_REINTCAST:
4717     case RID_CONSTCAST:
4718       {
4719         tree type;
4720         tree expression;
4721         const char *saved_message;
4722
4723         /* All of these can be handled in the same way from the point
4724            of view of parsing.  Begin by consuming the token
4725            identifying the cast.  */
4726         cp_lexer_consume_token (parser->lexer);
4727
4728         /* New types cannot be defined in the cast.  */
4729         saved_message = parser->type_definition_forbidden_message;
4730         parser->type_definition_forbidden_message
4731           = G_("types may not be defined in casts");
4732
4733         /* Look for the opening `<'.  */
4734         cp_parser_require (parser, CPP_LESS, RT_LESS);
4735         /* Parse the type to which we are casting.  */
4736         type = cp_parser_type_id (parser);
4737         /* Look for the closing `>'.  */
4738         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4739         /* Restore the old message.  */
4740         parser->type_definition_forbidden_message = saved_message;
4741
4742         /* And the expression which is being cast.  */
4743         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4744         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4745         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4746
4747         /* Only type conversions to integral or enumeration types
4748            can be used in constant-expressions.  */
4749         if (!cast_valid_in_integral_constant_expression_p (type)
4750             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4751           return error_mark_node;
4752
4753         switch (keyword)
4754           {
4755           case RID_DYNCAST:
4756             postfix_expression
4757               = build_dynamic_cast (type, expression, tf_warning_or_error);
4758             break;
4759           case RID_STATCAST:
4760             postfix_expression
4761               = build_static_cast (type, expression, tf_warning_or_error);
4762             break;
4763           case RID_REINTCAST:
4764             postfix_expression
4765               = build_reinterpret_cast (type, expression, 
4766                                         tf_warning_or_error);
4767             break;
4768           case RID_CONSTCAST:
4769             postfix_expression
4770               = build_const_cast (type, expression, tf_warning_or_error);
4771             break;
4772           default:
4773             gcc_unreachable ();
4774           }
4775       }
4776       break;
4777
4778     case RID_TYPEID:
4779       {
4780         tree type;
4781         const char *saved_message;
4782         bool saved_in_type_id_in_expr_p;
4783
4784         /* Consume the `typeid' token.  */
4785         cp_lexer_consume_token (parser->lexer);
4786         /* Look for the `(' token.  */
4787         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4788         /* Types cannot be defined in a `typeid' expression.  */
4789         saved_message = parser->type_definition_forbidden_message;
4790         parser->type_definition_forbidden_message
4791           = G_("types may not be defined in a %<typeid%> expression");
4792         /* We can't be sure yet whether we're looking at a type-id or an
4793            expression.  */
4794         cp_parser_parse_tentatively (parser);
4795         /* Try a type-id first.  */
4796         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4797         parser->in_type_id_in_expr_p = true;
4798         type = cp_parser_type_id (parser);
4799         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4800         /* Look for the `)' token.  Otherwise, we can't be sure that
4801            we're not looking at an expression: consider `typeid (int
4802            (3))', for example.  */
4803         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4804         /* If all went well, simply lookup the type-id.  */
4805         if (cp_parser_parse_definitely (parser))
4806           postfix_expression = get_typeid (type);
4807         /* Otherwise, fall back to the expression variant.  */
4808         else
4809           {
4810             tree expression;
4811
4812             /* Look for an expression.  */
4813             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4814             /* Compute its typeid.  */
4815             postfix_expression = build_typeid (expression);
4816             /* Look for the `)' token.  */
4817             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4818           }
4819         /* Restore the saved message.  */
4820         parser->type_definition_forbidden_message = saved_message;
4821         /* `typeid' may not appear in an integral constant expression.  */
4822         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4823           return error_mark_node;
4824       }
4825       break;
4826
4827     case RID_TYPENAME:
4828       {
4829         tree type;
4830         /* The syntax permitted here is the same permitted for an
4831            elaborated-type-specifier.  */
4832         type = cp_parser_elaborated_type_specifier (parser,
4833                                                     /*is_friend=*/false,
4834                                                     /*is_declaration=*/false);
4835         postfix_expression = cp_parser_functional_cast (parser, type);
4836       }
4837       break;
4838
4839     default:
4840       {
4841         tree type;
4842
4843         /* If the next thing is a simple-type-specifier, we may be
4844            looking at a functional cast.  We could also be looking at
4845            an id-expression.  So, we try the functional cast, and if
4846            that doesn't work we fall back to the primary-expression.  */
4847         cp_parser_parse_tentatively (parser);
4848         /* Look for the simple-type-specifier.  */
4849         type = cp_parser_simple_type_specifier (parser,
4850                                                 /*decl_specs=*/NULL,
4851                                                 CP_PARSER_FLAGS_NONE);
4852         /* Parse the cast itself.  */
4853         if (!cp_parser_error_occurred (parser))
4854           postfix_expression
4855             = cp_parser_functional_cast (parser, type);
4856         /* If that worked, we're done.  */
4857         if (cp_parser_parse_definitely (parser))
4858           break;
4859
4860         /* If the functional-cast didn't work out, try a
4861            compound-literal.  */
4862         if (cp_parser_allow_gnu_extensions_p (parser)
4863             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4864           {
4865             VEC(constructor_elt,gc) *initializer_list = NULL;
4866             bool saved_in_type_id_in_expr_p;
4867
4868             cp_parser_parse_tentatively (parser);
4869             /* Consume the `('.  */
4870             cp_lexer_consume_token (parser->lexer);
4871             /* Parse the type.  */
4872             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4873             parser->in_type_id_in_expr_p = true;
4874             type = cp_parser_type_id (parser);
4875             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4876             /* Look for the `)'.  */
4877             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4878             /* Look for the `{'.  */
4879             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4880             /* If things aren't going well, there's no need to
4881                keep going.  */
4882             if (!cp_parser_error_occurred (parser))
4883               {
4884                 bool non_constant_p;
4885                 /* Parse the initializer-list.  */
4886                 initializer_list
4887                   = cp_parser_initializer_list (parser, &non_constant_p);
4888                 /* Allow a trailing `,'.  */
4889                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4890                   cp_lexer_consume_token (parser->lexer);
4891                 /* Look for the final `}'.  */
4892                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4893               }
4894             /* If that worked, we're definitely looking at a
4895                compound-literal expression.  */
4896             if (cp_parser_parse_definitely (parser))
4897               {
4898                 /* Warn the user that a compound literal is not
4899                    allowed in standard C++.  */
4900                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4901                 /* For simplicity, we disallow compound literals in
4902                    constant-expressions.  We could
4903                    allow compound literals of integer type, whose
4904                    initializer was a constant, in constant
4905                    expressions.  Permitting that usage, as a further
4906                    extension, would not change the meaning of any
4907                    currently accepted programs.  (Of course, as
4908                    compound literals are not part of ISO C++, the
4909                    standard has nothing to say.)  */
4910                 if (cp_parser_non_integral_constant_expression (parser,
4911                                                                 NIC_NCC))
4912                   {
4913                     postfix_expression = error_mark_node;
4914                     break;
4915                   }
4916                 /* Form the representation of the compound-literal.  */
4917                 postfix_expression
4918                   = (finish_compound_literal
4919                      (type, build_constructor (init_list_type_node,
4920                                                initializer_list),
4921                       tf_warning_or_error));
4922                 break;
4923               }
4924           }
4925
4926         /* It must be a primary-expression.  */
4927         postfix_expression
4928           = cp_parser_primary_expression (parser, address_p, cast_p,
4929                                           /*template_arg_p=*/false,
4930                                           &idk);
4931       }
4932       break;
4933     }
4934
4935   /* Keep looping until the postfix-expression is complete.  */
4936   while (true)
4937     {
4938       if (idk == CP_ID_KIND_UNQUALIFIED
4939           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4940           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4941         /* It is not a Koenig lookup function call.  */
4942         postfix_expression
4943           = unqualified_name_lookup_error (postfix_expression);
4944
4945       /* Peek at the next token.  */
4946       token = cp_lexer_peek_token (parser->lexer);
4947
4948       switch (token->type)
4949         {
4950         case CPP_OPEN_SQUARE:
4951           postfix_expression
4952             = cp_parser_postfix_open_square_expression (parser,
4953                                                         postfix_expression,
4954                                                         false);
4955           idk = CP_ID_KIND_NONE;
4956           is_member_access = false;
4957           break;
4958
4959         case CPP_OPEN_PAREN:
4960           /* postfix-expression ( expression-list [opt] ) */
4961           {
4962             bool koenig_p;
4963             bool is_builtin_constant_p;
4964             bool saved_integral_constant_expression_p = false;
4965             bool saved_non_integral_constant_expression_p = false;
4966             VEC(tree,gc) *args;
4967
4968             is_member_access = false;
4969
4970             is_builtin_constant_p
4971               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4972             if (is_builtin_constant_p)
4973               {
4974                 /* The whole point of __builtin_constant_p is to allow
4975                    non-constant expressions to appear as arguments.  */
4976                 saved_integral_constant_expression_p
4977                   = parser->integral_constant_expression_p;
4978                 saved_non_integral_constant_expression_p
4979                   = parser->non_integral_constant_expression_p;
4980                 parser->integral_constant_expression_p = false;
4981               }
4982             args = (cp_parser_parenthesized_expression_list
4983                     (parser, non_attr,
4984                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4985                      /*non_constant_p=*/NULL));
4986             if (is_builtin_constant_p)
4987               {
4988                 parser->integral_constant_expression_p
4989                   = saved_integral_constant_expression_p;
4990                 parser->non_integral_constant_expression_p
4991                   = saved_non_integral_constant_expression_p;
4992               }
4993
4994             if (args == NULL)
4995               {
4996                 postfix_expression = error_mark_node;
4997                 break;
4998               }
4999
5000             /* Function calls are not permitted in
5001                constant-expressions.  */
5002             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5003                 && cp_parser_non_integral_constant_expression (parser,
5004                                                                NIC_FUNC_CALL))
5005               {
5006                 postfix_expression = error_mark_node;
5007                 release_tree_vector (args);
5008                 break;
5009               }
5010
5011             koenig_p = false;
5012             if (idk == CP_ID_KIND_UNQUALIFIED
5013                 || idk == CP_ID_KIND_TEMPLATE_ID)
5014               {
5015                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5016                   {
5017                     if (!VEC_empty (tree, args))
5018                       {
5019                         koenig_p = true;
5020                         if (!any_type_dependent_arguments_p (args))
5021                           postfix_expression
5022                             = perform_koenig_lookup (postfix_expression, args,
5023                                                      /*include_std=*/false,
5024                                                      tf_warning_or_error);
5025                       }
5026                     else
5027                       postfix_expression
5028                         = unqualified_fn_lookup_error (postfix_expression);
5029                   }
5030                 /* We do not perform argument-dependent lookup if
5031                    normal lookup finds a non-function, in accordance
5032                    with the expected resolution of DR 218.  */
5033                 else if (!VEC_empty (tree, args)
5034                          && is_overloaded_fn (postfix_expression))
5035                   {
5036                     tree fn = get_first_fn (postfix_expression);
5037                     fn = STRIP_TEMPLATE (fn);
5038
5039                     /* Do not do argument dependent lookup if regular
5040                        lookup finds a member function or a block-scope
5041                        function declaration.  [basic.lookup.argdep]/3  */
5042                     if (!DECL_FUNCTION_MEMBER_P (fn)
5043                         && !DECL_LOCAL_FUNCTION_P (fn))
5044                       {
5045                         koenig_p = true;
5046                         if (!any_type_dependent_arguments_p (args))
5047                           postfix_expression
5048                             = perform_koenig_lookup (postfix_expression, args,
5049                                                      /*include_std=*/false,
5050                                                      tf_warning_or_error);
5051                       }
5052                   }
5053               }
5054
5055             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5056               {
5057                 tree instance = TREE_OPERAND (postfix_expression, 0);
5058                 tree fn = TREE_OPERAND (postfix_expression, 1);
5059
5060                 if (processing_template_decl
5061                     && (type_dependent_expression_p (instance)
5062                         || (!BASELINK_P (fn)
5063                             && TREE_CODE (fn) != FIELD_DECL)
5064                         || type_dependent_expression_p (fn)
5065                         || any_type_dependent_arguments_p (args)))
5066                   {
5067                     postfix_expression
5068                       = build_nt_call_vec (postfix_expression, args);
5069                     release_tree_vector (args);
5070                     break;
5071                   }
5072
5073                 if (BASELINK_P (fn))
5074                   {
5075                   postfix_expression
5076                     = (build_new_method_call
5077                        (instance, fn, &args, NULL_TREE,
5078                         (idk == CP_ID_KIND_QUALIFIED
5079                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5080                          : LOOKUP_NORMAL),
5081                         /*fn_p=*/NULL,
5082                         tf_warning_or_error));
5083                   }
5084                 else
5085                   postfix_expression
5086                     = finish_call_expr (postfix_expression, &args,
5087                                         /*disallow_virtual=*/false,
5088                                         /*koenig_p=*/false,
5089                                         tf_warning_or_error);
5090               }
5091             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5092                      || TREE_CODE (postfix_expression) == MEMBER_REF
5093                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5094               postfix_expression = (build_offset_ref_call_from_tree
5095                                     (postfix_expression, &args));
5096             else if (idk == CP_ID_KIND_QUALIFIED)
5097               /* A call to a static class member, or a namespace-scope
5098                  function.  */
5099               postfix_expression
5100                 = finish_call_expr (postfix_expression, &args,
5101                                     /*disallow_virtual=*/true,
5102                                     koenig_p,
5103                                     tf_warning_or_error);
5104             else
5105               /* All other function calls.  */
5106               postfix_expression
5107                 = finish_call_expr (postfix_expression, &args,
5108                                     /*disallow_virtual=*/false,
5109                                     koenig_p,
5110                                     tf_warning_or_error);
5111
5112             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5113             idk = CP_ID_KIND_NONE;
5114
5115             release_tree_vector (args);
5116           }
5117           break;
5118
5119         case CPP_DOT:
5120         case CPP_DEREF:
5121           /* postfix-expression . template [opt] id-expression
5122              postfix-expression . pseudo-destructor-name
5123              postfix-expression -> template [opt] id-expression
5124              postfix-expression -> pseudo-destructor-name */
5125
5126           /* Consume the `.' or `->' operator.  */
5127           cp_lexer_consume_token (parser->lexer);
5128
5129           postfix_expression
5130             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5131                                                       postfix_expression,
5132                                                       false, &idk,
5133                                                       token->location);
5134
5135           is_member_access = true;
5136           break;
5137
5138         case CPP_PLUS_PLUS:
5139           /* postfix-expression ++  */
5140           /* Consume the `++' token.  */
5141           cp_lexer_consume_token (parser->lexer);
5142           /* Generate a representation for the complete expression.  */
5143           postfix_expression
5144             = finish_increment_expr (postfix_expression,
5145                                      POSTINCREMENT_EXPR);
5146           /* Increments may not appear in constant-expressions.  */
5147           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5148             postfix_expression = error_mark_node;
5149           idk = CP_ID_KIND_NONE;
5150           is_member_access = false;
5151           break;
5152
5153         case CPP_MINUS_MINUS:
5154           /* postfix-expression -- */
5155           /* Consume the `--' token.  */
5156           cp_lexer_consume_token (parser->lexer);
5157           /* Generate a representation for the complete expression.  */
5158           postfix_expression
5159             = finish_increment_expr (postfix_expression,
5160                                      POSTDECREMENT_EXPR);
5161           /* Decrements may not appear in constant-expressions.  */
5162           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5163             postfix_expression = error_mark_node;
5164           idk = CP_ID_KIND_NONE;
5165           is_member_access = false;
5166           break;
5167
5168         default:
5169           if (pidk_return != NULL)
5170             * pidk_return = idk;
5171           if (member_access_only_p)
5172             return is_member_access? postfix_expression : error_mark_node;
5173           else
5174             return postfix_expression;
5175         }
5176     }
5177
5178   /* We should never get here.  */
5179   gcc_unreachable ();
5180   return error_mark_node;
5181 }
5182
5183 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5184    by cp_parser_builtin_offsetof.  We're looking for
5185
5186      postfix-expression [ expression ]
5187
5188    FOR_OFFSETOF is set if we're being called in that context, which
5189    changes how we deal with integer constant expressions.  */
5190
5191 static tree
5192 cp_parser_postfix_open_square_expression (cp_parser *parser,
5193                                           tree postfix_expression,
5194                                           bool for_offsetof)
5195 {
5196   tree index;
5197
5198   /* Consume the `[' token.  */
5199   cp_lexer_consume_token (parser->lexer);
5200
5201   /* Parse the index expression.  */
5202   /* ??? For offsetof, there is a question of what to allow here.  If
5203      offsetof is not being used in an integral constant expression context,
5204      then we *could* get the right answer by computing the value at runtime.
5205      If we are in an integral constant expression context, then we might
5206      could accept any constant expression; hard to say without analysis.
5207      Rather than open the barn door too wide right away, allow only integer
5208      constant expressions here.  */
5209   if (for_offsetof)
5210     index = cp_parser_constant_expression (parser, false, NULL);
5211   else
5212     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5213
5214   /* Look for the closing `]'.  */
5215   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5216
5217   /* Build the ARRAY_REF.  */
5218   postfix_expression = grok_array_decl (postfix_expression, index);
5219
5220   /* When not doing offsetof, array references are not permitted in
5221      constant-expressions.  */
5222   if (!for_offsetof
5223       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5224     postfix_expression = error_mark_node;
5225
5226   return postfix_expression;
5227 }
5228
5229 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5230    by cp_parser_builtin_offsetof.  We're looking for
5231
5232      postfix-expression . template [opt] id-expression
5233      postfix-expression . pseudo-destructor-name
5234      postfix-expression -> template [opt] id-expression
5235      postfix-expression -> pseudo-destructor-name
5236
5237    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5238    limits what of the above we'll actually accept, but nevermind.
5239    TOKEN_TYPE is the "." or "->" token, which will already have been
5240    removed from the stream.  */
5241
5242 static tree
5243 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5244                                         enum cpp_ttype token_type,
5245                                         tree postfix_expression,
5246                                         bool for_offsetof, cp_id_kind *idk,
5247                                         location_t location)
5248 {
5249   tree name;
5250   bool dependent_p;
5251   bool pseudo_destructor_p;
5252   tree scope = NULL_TREE;
5253
5254   /* If this is a `->' operator, dereference the pointer.  */
5255   if (token_type == CPP_DEREF)
5256     postfix_expression = build_x_arrow (postfix_expression);
5257   /* Check to see whether or not the expression is type-dependent.  */
5258   dependent_p = type_dependent_expression_p (postfix_expression);
5259   /* The identifier following the `->' or `.' is not qualified.  */
5260   parser->scope = NULL_TREE;
5261   parser->qualifying_scope = NULL_TREE;
5262   parser->object_scope = NULL_TREE;
5263   *idk = CP_ID_KIND_NONE;
5264
5265   /* Enter the scope corresponding to the type of the object
5266      given by the POSTFIX_EXPRESSION.  */
5267   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5268     {
5269       scope = TREE_TYPE (postfix_expression);
5270       /* According to the standard, no expression should ever have
5271          reference type.  Unfortunately, we do not currently match
5272          the standard in this respect in that our internal representation
5273          of an expression may have reference type even when the standard
5274          says it does not.  Therefore, we have to manually obtain the
5275          underlying type here.  */
5276       scope = non_reference (scope);
5277       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5278       if (scope == unknown_type_node)
5279         {
5280           error_at (location, "%qE does not have class type",
5281                     postfix_expression);
5282           scope = NULL_TREE;
5283         }
5284       else
5285         scope = complete_type_or_else (scope, NULL_TREE);
5286       /* Let the name lookup machinery know that we are processing a
5287          class member access expression.  */
5288       parser->context->object_type = scope;
5289       /* If something went wrong, we want to be able to discern that case,
5290          as opposed to the case where there was no SCOPE due to the type
5291          of expression being dependent.  */
5292       if (!scope)
5293         scope = error_mark_node;
5294       /* If the SCOPE was erroneous, make the various semantic analysis
5295          functions exit quickly -- and without issuing additional error
5296          messages.  */
5297       if (scope == error_mark_node)
5298         postfix_expression = error_mark_node;
5299     }
5300
5301   /* Assume this expression is not a pseudo-destructor access.  */
5302   pseudo_destructor_p = false;
5303
5304   /* If the SCOPE is a scalar type, then, if this is a valid program,
5305      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5306      is type dependent, it can be pseudo-destructor-name or something else.
5307      Try to parse it as pseudo-destructor-name first.  */
5308   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5309     {
5310       tree s;
5311       tree type;
5312
5313       cp_parser_parse_tentatively (parser);
5314       /* Parse the pseudo-destructor-name.  */
5315       s = NULL_TREE;
5316       cp_parser_pseudo_destructor_name (parser, &s, &type);
5317       if (dependent_p
5318           && (cp_parser_error_occurred (parser)
5319               || TREE_CODE (type) != TYPE_DECL
5320               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5321         cp_parser_abort_tentative_parse (parser);
5322       else if (cp_parser_parse_definitely (parser))
5323         {
5324           pseudo_destructor_p = true;
5325           postfix_expression
5326             = finish_pseudo_destructor_expr (postfix_expression,
5327                                              s, TREE_TYPE (type));
5328         }
5329     }
5330
5331   if (!pseudo_destructor_p)
5332     {
5333       /* If the SCOPE is not a scalar type, we are looking at an
5334          ordinary class member access expression, rather than a
5335          pseudo-destructor-name.  */
5336       bool template_p;
5337       cp_token *token = cp_lexer_peek_token (parser->lexer);
5338       /* Parse the id-expression.  */
5339       name = (cp_parser_id_expression
5340               (parser,
5341                cp_parser_optional_template_keyword (parser),
5342                /*check_dependency_p=*/true,
5343                &template_p,
5344                /*declarator_p=*/false,
5345                /*optional_p=*/false));
5346       /* In general, build a SCOPE_REF if the member name is qualified.
5347          However, if the name was not dependent and has already been
5348          resolved; there is no need to build the SCOPE_REF.  For example;
5349
5350              struct X { void f(); };
5351              template <typename T> void f(T* t) { t->X::f(); }
5352
5353          Even though "t" is dependent, "X::f" is not and has been resolved
5354          to a BASELINK; there is no need to include scope information.  */
5355
5356       /* But we do need to remember that there was an explicit scope for
5357          virtual function calls.  */
5358       if (parser->scope)
5359         *idk = CP_ID_KIND_QUALIFIED;
5360
5361       /* If the name is a template-id that names a type, we will get a
5362          TYPE_DECL here.  That is invalid code.  */
5363       if (TREE_CODE (name) == TYPE_DECL)
5364         {
5365           error_at (token->location, "invalid use of %qD", name);
5366           postfix_expression = error_mark_node;
5367         }
5368       else
5369         {
5370           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5371             {
5372               name = build_qualified_name (/*type=*/NULL_TREE,
5373                                            parser->scope,
5374                                            name,
5375                                            template_p);
5376               parser->scope = NULL_TREE;
5377               parser->qualifying_scope = NULL_TREE;
5378               parser->object_scope = NULL_TREE;
5379             }
5380           if (scope && name && BASELINK_P (name))
5381             adjust_result_of_qualified_name_lookup
5382               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5383           postfix_expression
5384             = finish_class_member_access_expr (postfix_expression, name,
5385                                                template_p, 
5386                                                tf_warning_or_error);
5387         }
5388     }
5389
5390   /* We no longer need to look up names in the scope of the object on
5391      the left-hand side of the `.' or `->' operator.  */
5392   parser->context->object_type = NULL_TREE;
5393
5394   /* Outside of offsetof, these operators may not appear in
5395      constant-expressions.  */
5396   if (!for_offsetof
5397       && (cp_parser_non_integral_constant_expression
5398           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5399     postfix_expression = error_mark_node;
5400
5401   return postfix_expression;
5402 }
5403
5404 /* Parse a parenthesized expression-list.
5405
5406    expression-list:
5407      assignment-expression
5408      expression-list, assignment-expression
5409
5410    attribute-list:
5411      expression-list
5412      identifier
5413      identifier, expression-list
5414
5415    CAST_P is true if this expression is the target of a cast.
5416
5417    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5418    argument pack.
5419
5420    Returns a vector of trees.  Each element is a representation of an
5421    assignment-expression.  NULL is returned if the ( and or ) are
5422    missing.  An empty, but allocated, vector is returned on no
5423    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5424    if we are parsing an attribute list for an attribute that wants a
5425    plain identifier argument, normal_attr for an attribute that wants
5426    an expression, or non_attr if we aren't parsing an attribute list.  If
5427    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5428    not all of the expressions in the list were constant.  */
5429
5430 static VEC(tree,gc) *
5431 cp_parser_parenthesized_expression_list (cp_parser* parser,
5432                                          int is_attribute_list,
5433                                          bool cast_p,
5434                                          bool allow_expansion_p,
5435                                          bool *non_constant_p)
5436 {
5437   VEC(tree,gc) *expression_list;
5438   bool fold_expr_p = is_attribute_list != non_attr;
5439   tree identifier = NULL_TREE;
5440   bool saved_greater_than_is_operator_p;
5441
5442   /* Assume all the expressions will be constant.  */
5443   if (non_constant_p)
5444     *non_constant_p = false;
5445
5446   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5447     return NULL;
5448
5449   expression_list = make_tree_vector ();
5450
5451   /* Within a parenthesized expression, a `>' token is always
5452      the greater-than operator.  */
5453   saved_greater_than_is_operator_p
5454     = parser->greater_than_is_operator_p;
5455   parser->greater_than_is_operator_p = true;
5456
5457   /* Consume expressions until there are no more.  */
5458   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5459     while (true)
5460       {
5461         tree expr;
5462
5463         /* At the beginning of attribute lists, check to see if the
5464            next token is an identifier.  */
5465         if (is_attribute_list == id_attr
5466             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5467           {
5468             cp_token *token;
5469
5470             /* Consume the identifier.  */
5471             token = cp_lexer_consume_token (parser->lexer);
5472             /* Save the identifier.  */
5473             identifier = token->u.value;
5474           }
5475         else
5476           {
5477             bool expr_non_constant_p;
5478
5479             /* Parse the next assignment-expression.  */
5480             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5481               {
5482                 /* A braced-init-list.  */
5483                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5484                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5485                 if (non_constant_p && expr_non_constant_p)
5486                   *non_constant_p = true;
5487               }
5488             else if (non_constant_p)
5489               {
5490                 expr = (cp_parser_constant_expression
5491                         (parser, /*allow_non_constant_p=*/true,
5492                          &expr_non_constant_p));
5493                 if (expr_non_constant_p)
5494                   *non_constant_p = true;
5495               }
5496             else
5497               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5498
5499             if (fold_expr_p)
5500               expr = fold_non_dependent_expr (expr);
5501
5502             /* If we have an ellipsis, then this is an expression
5503                expansion.  */
5504             if (allow_expansion_p
5505                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5506               {
5507                 /* Consume the `...'.  */
5508                 cp_lexer_consume_token (parser->lexer);
5509
5510                 /* Build the argument pack.  */
5511                 expr = make_pack_expansion (expr);
5512               }
5513
5514              /* Add it to the list.  We add error_mark_node
5515                 expressions to the list, so that we can still tell if
5516                 the correct form for a parenthesized expression-list
5517                 is found. That gives better errors.  */
5518             VEC_safe_push (tree, gc, expression_list, expr);
5519
5520             if (expr == error_mark_node)
5521               goto skip_comma;
5522           }
5523
5524         /* After the first item, attribute lists look the same as
5525            expression lists.  */
5526         is_attribute_list = non_attr;
5527
5528       get_comma:;
5529         /* If the next token isn't a `,', then we are done.  */
5530         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5531           break;
5532
5533         /* Otherwise, consume the `,' and keep going.  */
5534         cp_lexer_consume_token (parser->lexer);
5535       }
5536
5537   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5538     {
5539       int ending;
5540
5541     skip_comma:;
5542       /* We try and resync to an unnested comma, as that will give the
5543          user better diagnostics.  */
5544       ending = cp_parser_skip_to_closing_parenthesis (parser,
5545                                                       /*recovering=*/true,
5546                                                       /*or_comma=*/true,
5547                                                       /*consume_paren=*/true);
5548       if (ending < 0)
5549         goto get_comma;
5550       if (!ending)
5551         {
5552           parser->greater_than_is_operator_p
5553             = saved_greater_than_is_operator_p;
5554           return NULL;
5555         }
5556     }
5557
5558   parser->greater_than_is_operator_p
5559     = saved_greater_than_is_operator_p;
5560
5561   if (identifier)
5562     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5563
5564   return expression_list;
5565 }
5566
5567 /* Parse a pseudo-destructor-name.
5568
5569    pseudo-destructor-name:
5570      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5571      :: [opt] nested-name-specifier template template-id :: ~ type-name
5572      :: [opt] nested-name-specifier [opt] ~ type-name
5573
5574    If either of the first two productions is used, sets *SCOPE to the
5575    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5576    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5577    or ERROR_MARK_NODE if the parse fails.  */
5578
5579 static void
5580 cp_parser_pseudo_destructor_name (cp_parser* parser,
5581                                   tree* scope,
5582                                   tree* type)
5583 {
5584   bool nested_name_specifier_p;
5585
5586   /* Assume that things will not work out.  */
5587   *type = error_mark_node;
5588
5589   /* Look for the optional `::' operator.  */
5590   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5591   /* Look for the optional nested-name-specifier.  */
5592   nested_name_specifier_p
5593     = (cp_parser_nested_name_specifier_opt (parser,
5594                                             /*typename_keyword_p=*/false,
5595                                             /*check_dependency_p=*/true,
5596                                             /*type_p=*/false,
5597                                             /*is_declaration=*/false)
5598        != NULL_TREE);
5599   /* Now, if we saw a nested-name-specifier, we might be doing the
5600      second production.  */
5601   if (nested_name_specifier_p
5602       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5603     {
5604       /* Consume the `template' keyword.  */
5605       cp_lexer_consume_token (parser->lexer);
5606       /* Parse the template-id.  */
5607       cp_parser_template_id (parser,
5608                              /*template_keyword_p=*/true,
5609                              /*check_dependency_p=*/false,
5610                              /*is_declaration=*/true);
5611       /* Look for the `::' token.  */
5612       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5613     }
5614   /* If the next token is not a `~', then there might be some
5615      additional qualification.  */
5616   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5617     {
5618       /* At this point, we're looking for "type-name :: ~".  The type-name
5619          must not be a class-name, since this is a pseudo-destructor.  So,
5620          it must be either an enum-name, or a typedef-name -- both of which
5621          are just identifiers.  So, we peek ahead to check that the "::"
5622          and "~" tokens are present; if they are not, then we can avoid
5623          calling type_name.  */
5624       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5625           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5626           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5627         {
5628           cp_parser_error (parser, "non-scalar type");
5629           return;
5630         }
5631
5632       /* Look for the type-name.  */
5633       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5634       if (*scope == error_mark_node)
5635         return;
5636
5637       /* Look for the `::' token.  */
5638       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5639     }
5640   else
5641     *scope = NULL_TREE;
5642
5643   /* Look for the `~'.  */
5644   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5645
5646   /* Once we see the ~, this has to be a pseudo-destructor.  */
5647   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5648     cp_parser_commit_to_tentative_parse (parser);
5649
5650   /* Look for the type-name again.  We are not responsible for
5651      checking that it matches the first type-name.  */
5652   *type = cp_parser_nonclass_name (parser);
5653 }
5654
5655 /* Parse a unary-expression.
5656
5657    unary-expression:
5658      postfix-expression
5659      ++ cast-expression
5660      -- cast-expression
5661      unary-operator cast-expression
5662      sizeof unary-expression
5663      sizeof ( type-id )
5664      alignof ( type-id )  [C++0x]
5665      new-expression
5666      delete-expression
5667
5668    GNU Extensions:
5669
5670    unary-expression:
5671      __extension__ cast-expression
5672      __alignof__ unary-expression
5673      __alignof__ ( type-id )
5674      alignof unary-expression  [C++0x]
5675      __real__ cast-expression
5676      __imag__ cast-expression
5677      && identifier
5678
5679    ADDRESS_P is true iff the unary-expression is appearing as the
5680    operand of the `&' operator.   CAST_P is true if this expression is
5681    the target of a cast.
5682
5683    Returns a representation of the expression.  */
5684
5685 static tree
5686 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5687                             cp_id_kind * pidk)
5688 {
5689   cp_token *token;
5690   enum tree_code unary_operator;
5691
5692   /* Peek at the next token.  */
5693   token = cp_lexer_peek_token (parser->lexer);
5694   /* Some keywords give away the kind of expression.  */
5695   if (token->type == CPP_KEYWORD)
5696     {
5697       enum rid keyword = token->keyword;
5698
5699       switch (keyword)
5700         {
5701         case RID_ALIGNOF:
5702         case RID_SIZEOF:
5703           {
5704             tree operand;
5705             enum tree_code op;
5706
5707             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5708             /* Consume the token.  */
5709             cp_lexer_consume_token (parser->lexer);
5710             /* Parse the operand.  */
5711             operand = cp_parser_sizeof_operand (parser, keyword);
5712
5713             if (TYPE_P (operand))
5714               return cxx_sizeof_or_alignof_type (operand, op, true);
5715             else
5716               {
5717                 /* ISO C++ defines alignof only with types, not with
5718                    expressions. So pedwarn if alignof is used with a non-
5719                    type expression. However, __alignof__ is ok.  */
5720                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5721                   pedwarn (token->location, OPT_pedantic,
5722                            "ISO C++ does not allow %<alignof%> "
5723                            "with a non-type");
5724
5725                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5726               }
5727           }
5728
5729         case RID_NEW:
5730           return cp_parser_new_expression (parser);
5731
5732         case RID_DELETE:
5733           return cp_parser_delete_expression (parser);
5734
5735         case RID_EXTENSION:
5736           {
5737             /* The saved value of the PEDANTIC flag.  */
5738             int saved_pedantic;
5739             tree expr;
5740
5741             /* Save away the PEDANTIC flag.  */
5742             cp_parser_extension_opt (parser, &saved_pedantic);
5743             /* Parse the cast-expression.  */
5744             expr = cp_parser_simple_cast_expression (parser);
5745             /* Restore the PEDANTIC flag.  */
5746             pedantic = saved_pedantic;
5747
5748             return expr;
5749           }
5750
5751         case RID_REALPART:
5752         case RID_IMAGPART:
5753           {
5754             tree expression;
5755
5756             /* Consume the `__real__' or `__imag__' token.  */
5757             cp_lexer_consume_token (parser->lexer);
5758             /* Parse the cast-expression.  */
5759             expression = cp_parser_simple_cast_expression (parser);
5760             /* Create the complete representation.  */
5761             return build_x_unary_op ((keyword == RID_REALPART
5762                                       ? REALPART_EXPR : IMAGPART_EXPR),
5763                                      expression,
5764                                      tf_warning_or_error);
5765           }
5766           break;
5767
5768         case RID_NOEXCEPT:
5769           {
5770             tree expr;
5771             const char *saved_message;
5772             bool saved_integral_constant_expression_p;
5773             bool saved_non_integral_constant_expression_p;
5774             bool saved_greater_than_is_operator_p;
5775
5776             cp_lexer_consume_token (parser->lexer);
5777             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5778
5779             saved_message = parser->type_definition_forbidden_message;
5780             parser->type_definition_forbidden_message
5781               = G_("types may not be defined in %<noexcept%> expressions");
5782
5783             saved_integral_constant_expression_p
5784               = parser->integral_constant_expression_p;
5785             saved_non_integral_constant_expression_p
5786               = parser->non_integral_constant_expression_p;
5787             parser->integral_constant_expression_p = false;
5788
5789             saved_greater_than_is_operator_p
5790               = parser->greater_than_is_operator_p;
5791             parser->greater_than_is_operator_p = true;
5792
5793             ++cp_unevaluated_operand;
5794             ++c_inhibit_evaluation_warnings;
5795             expr = cp_parser_expression (parser, false, NULL);
5796             --c_inhibit_evaluation_warnings;
5797             --cp_unevaluated_operand;
5798
5799             parser->greater_than_is_operator_p
5800               = saved_greater_than_is_operator_p;
5801
5802             parser->integral_constant_expression_p
5803               = saved_integral_constant_expression_p;
5804             parser->non_integral_constant_expression_p
5805               = saved_non_integral_constant_expression_p;
5806
5807             parser->type_definition_forbidden_message = saved_message;
5808
5809             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5810             return finish_noexcept_expr (expr, tf_warning_or_error);
5811           }
5812
5813         default:
5814           break;
5815         }
5816     }
5817
5818   /* Look for the `:: new' and `:: delete', which also signal the
5819      beginning of a new-expression, or delete-expression,
5820      respectively.  If the next token is `::', then it might be one of
5821      these.  */
5822   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5823     {
5824       enum rid keyword;
5825
5826       /* See if the token after the `::' is one of the keywords in
5827          which we're interested.  */
5828       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5829       /* If it's `new', we have a new-expression.  */
5830       if (keyword == RID_NEW)
5831         return cp_parser_new_expression (parser);
5832       /* Similarly, for `delete'.  */
5833       else if (keyword == RID_DELETE)
5834         return cp_parser_delete_expression (parser);
5835     }
5836
5837   /* Look for a unary operator.  */
5838   unary_operator = cp_parser_unary_operator (token);
5839   /* The `++' and `--' operators can be handled similarly, even though
5840      they are not technically unary-operators in the grammar.  */
5841   if (unary_operator == ERROR_MARK)
5842     {
5843       if (token->type == CPP_PLUS_PLUS)
5844         unary_operator = PREINCREMENT_EXPR;
5845       else if (token->type == CPP_MINUS_MINUS)
5846         unary_operator = PREDECREMENT_EXPR;
5847       /* Handle the GNU address-of-label extension.  */
5848       else if (cp_parser_allow_gnu_extensions_p (parser)
5849                && token->type == CPP_AND_AND)
5850         {
5851           tree identifier;
5852           tree expression;
5853           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5854
5855           /* Consume the '&&' token.  */
5856           cp_lexer_consume_token (parser->lexer);
5857           /* Look for the identifier.  */
5858           identifier = cp_parser_identifier (parser);
5859           /* Create an expression representing the address.  */
5860           expression = finish_label_address_expr (identifier, loc);
5861           if (cp_parser_non_integral_constant_expression (parser,
5862                                                           NIC_ADDR_LABEL))
5863             expression = error_mark_node;
5864           return expression;
5865         }
5866     }
5867   if (unary_operator != ERROR_MARK)
5868     {
5869       tree cast_expression;
5870       tree expression = error_mark_node;
5871       non_integral_constant non_constant_p = NIC_NONE;
5872
5873       /* Consume the operator token.  */
5874       token = cp_lexer_consume_token (parser->lexer);
5875       /* Parse the cast-expression.  */
5876       cast_expression
5877         = cp_parser_cast_expression (parser,
5878                                      unary_operator == ADDR_EXPR,
5879                                      /*cast_p=*/false, pidk);
5880       /* Now, build an appropriate representation.  */
5881       switch (unary_operator)
5882         {
5883         case INDIRECT_REF:
5884           non_constant_p = NIC_STAR;
5885           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5886                                              tf_warning_or_error);
5887           break;
5888
5889         case ADDR_EXPR:
5890            non_constant_p = NIC_ADDR;
5891           /* Fall through.  */
5892         case BIT_NOT_EXPR:
5893           expression = build_x_unary_op (unary_operator, cast_expression,
5894                                          tf_warning_or_error);
5895           break;
5896
5897         case PREINCREMENT_EXPR:
5898         case PREDECREMENT_EXPR:
5899           non_constant_p = unary_operator == PREINCREMENT_EXPR
5900                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5901           /* Fall through.  */
5902         case UNARY_PLUS_EXPR:
5903         case NEGATE_EXPR:
5904         case TRUTH_NOT_EXPR:
5905           expression = finish_unary_op_expr (unary_operator, cast_expression);
5906           break;
5907
5908         default:
5909           gcc_unreachable ();
5910         }
5911
5912       if (non_constant_p != NIC_NONE
5913           && cp_parser_non_integral_constant_expression (parser,
5914                                                          non_constant_p))
5915         expression = error_mark_node;
5916
5917       return expression;
5918     }
5919
5920   return cp_parser_postfix_expression (parser, address_p, cast_p,
5921                                        /*member_access_only_p=*/false,
5922                                        pidk);
5923 }
5924
5925 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5926    unary-operator, the corresponding tree code is returned.  */
5927
5928 static enum tree_code
5929 cp_parser_unary_operator (cp_token* token)
5930 {
5931   switch (token->type)
5932     {
5933     case CPP_MULT:
5934       return INDIRECT_REF;
5935
5936     case CPP_AND:
5937       return ADDR_EXPR;
5938
5939     case CPP_PLUS:
5940       return UNARY_PLUS_EXPR;
5941
5942     case CPP_MINUS:
5943       return NEGATE_EXPR;
5944
5945     case CPP_NOT:
5946       return TRUTH_NOT_EXPR;
5947
5948     case CPP_COMPL:
5949       return BIT_NOT_EXPR;
5950
5951     default:
5952       return ERROR_MARK;
5953     }
5954 }
5955
5956 /* Parse a new-expression.
5957
5958    new-expression:
5959      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5960      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5961
5962    Returns a representation of the expression.  */
5963
5964 static tree
5965 cp_parser_new_expression (cp_parser* parser)
5966 {
5967   bool global_scope_p;
5968   VEC(tree,gc) *placement;
5969   tree type;
5970   VEC(tree,gc) *initializer;
5971   tree nelts;
5972   tree ret;
5973
5974   /* Look for the optional `::' operator.  */
5975   global_scope_p
5976     = (cp_parser_global_scope_opt (parser,
5977                                    /*current_scope_valid_p=*/false)
5978        != NULL_TREE);
5979   /* Look for the `new' operator.  */
5980   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5981   /* There's no easy way to tell a new-placement from the
5982      `( type-id )' construct.  */
5983   cp_parser_parse_tentatively (parser);
5984   /* Look for a new-placement.  */
5985   placement = cp_parser_new_placement (parser);
5986   /* If that didn't work out, there's no new-placement.  */
5987   if (!cp_parser_parse_definitely (parser))
5988     {
5989       if (placement != NULL)
5990         release_tree_vector (placement);
5991       placement = NULL;
5992     }
5993
5994   /* If the next token is a `(', then we have a parenthesized
5995      type-id.  */
5996   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5997     {
5998       cp_token *token;
5999       /* Consume the `('.  */
6000       cp_lexer_consume_token (parser->lexer);
6001       /* Parse the type-id.  */
6002       type = cp_parser_type_id (parser);
6003       /* Look for the closing `)'.  */
6004       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6005       token = cp_lexer_peek_token (parser->lexer);
6006       /* There should not be a direct-new-declarator in this production,
6007          but GCC used to allowed this, so we check and emit a sensible error
6008          message for this case.  */
6009       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6010         {
6011           error_at (token->location,
6012                     "array bound forbidden after parenthesized type-id");
6013           inform (token->location, 
6014                   "try removing the parentheses around the type-id");
6015           cp_parser_direct_new_declarator (parser);
6016         }
6017       nelts = NULL_TREE;
6018     }
6019   /* Otherwise, there must be a new-type-id.  */
6020   else
6021     type = cp_parser_new_type_id (parser, &nelts);
6022
6023   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6024   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6025       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6026     initializer = cp_parser_new_initializer (parser);
6027   else
6028     initializer = NULL;
6029
6030   /* A new-expression may not appear in an integral constant
6031      expression.  */
6032   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6033     ret = error_mark_node;
6034   else
6035     {
6036       /* Create a representation of the new-expression.  */
6037       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6038                        tf_warning_or_error);
6039     }
6040
6041   if (placement != NULL)
6042     release_tree_vector (placement);
6043   if (initializer != NULL)
6044     release_tree_vector (initializer);
6045
6046   return ret;
6047 }
6048
6049 /* Parse a new-placement.
6050
6051    new-placement:
6052      ( expression-list )
6053
6054    Returns the same representation as for an expression-list.  */
6055
6056 static VEC(tree,gc) *
6057 cp_parser_new_placement (cp_parser* parser)
6058 {
6059   VEC(tree,gc) *expression_list;
6060
6061   /* Parse the expression-list.  */
6062   expression_list = (cp_parser_parenthesized_expression_list
6063                      (parser, non_attr, /*cast_p=*/false,
6064                       /*allow_expansion_p=*/true,
6065                       /*non_constant_p=*/NULL));
6066
6067   return expression_list;
6068 }
6069
6070 /* Parse a new-type-id.
6071
6072    new-type-id:
6073      type-specifier-seq new-declarator [opt]
6074
6075    Returns the TYPE allocated.  If the new-type-id indicates an array
6076    type, *NELTS is set to the number of elements in the last array
6077    bound; the TYPE will not include the last array bound.  */
6078
6079 static tree
6080 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6081 {
6082   cp_decl_specifier_seq type_specifier_seq;
6083   cp_declarator *new_declarator;
6084   cp_declarator *declarator;
6085   cp_declarator *outer_declarator;
6086   const char *saved_message;
6087   tree type;
6088
6089   /* The type-specifier sequence must not contain type definitions.
6090      (It cannot contain declarations of new types either, but if they
6091      are not definitions we will catch that because they are not
6092      complete.)  */
6093   saved_message = parser->type_definition_forbidden_message;
6094   parser->type_definition_forbidden_message
6095     = G_("types may not be defined in a new-type-id");
6096   /* Parse the type-specifier-seq.  */
6097   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6098                                 /*is_trailing_return=*/false,
6099                                 &type_specifier_seq);
6100   /* Restore the old message.  */
6101   parser->type_definition_forbidden_message = saved_message;
6102   /* Parse the new-declarator.  */
6103   new_declarator = cp_parser_new_declarator_opt (parser);
6104
6105   /* Determine the number of elements in the last array dimension, if
6106      any.  */
6107   *nelts = NULL_TREE;
6108   /* Skip down to the last array dimension.  */
6109   declarator = new_declarator;
6110   outer_declarator = NULL;
6111   while (declarator && (declarator->kind == cdk_pointer
6112                         || declarator->kind == cdk_ptrmem))
6113     {
6114       outer_declarator = declarator;
6115       declarator = declarator->declarator;
6116     }
6117   while (declarator
6118          && declarator->kind == cdk_array
6119          && declarator->declarator
6120          && declarator->declarator->kind == cdk_array)
6121     {
6122       outer_declarator = declarator;
6123       declarator = declarator->declarator;
6124     }
6125
6126   if (declarator && declarator->kind == cdk_array)
6127     {
6128       *nelts = declarator->u.array.bounds;
6129       if (*nelts == error_mark_node)
6130         *nelts = integer_one_node;
6131
6132       if (outer_declarator)
6133         outer_declarator->declarator = declarator->declarator;
6134       else
6135         new_declarator = NULL;
6136     }
6137
6138   type = groktypename (&type_specifier_seq, new_declarator, false);
6139   return type;
6140 }
6141
6142 /* Parse an (optional) new-declarator.
6143
6144    new-declarator:
6145      ptr-operator new-declarator [opt]
6146      direct-new-declarator
6147
6148    Returns the declarator.  */
6149
6150 static cp_declarator *
6151 cp_parser_new_declarator_opt (cp_parser* parser)
6152 {
6153   enum tree_code code;
6154   tree type;
6155   cp_cv_quals cv_quals;
6156
6157   /* We don't know if there's a ptr-operator next, or not.  */
6158   cp_parser_parse_tentatively (parser);
6159   /* Look for a ptr-operator.  */
6160   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6161   /* If that worked, look for more new-declarators.  */
6162   if (cp_parser_parse_definitely (parser))
6163     {
6164       cp_declarator *declarator;
6165
6166       /* Parse another optional declarator.  */
6167       declarator = cp_parser_new_declarator_opt (parser);
6168
6169       return cp_parser_make_indirect_declarator
6170         (code, type, cv_quals, declarator);
6171     }
6172
6173   /* If the next token is a `[', there is a direct-new-declarator.  */
6174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6175     return cp_parser_direct_new_declarator (parser);
6176
6177   return NULL;
6178 }
6179
6180 /* Parse a direct-new-declarator.
6181
6182    direct-new-declarator:
6183      [ expression ]
6184      direct-new-declarator [constant-expression]
6185
6186    */
6187
6188 static cp_declarator *
6189 cp_parser_direct_new_declarator (cp_parser* parser)
6190 {
6191   cp_declarator *declarator = NULL;
6192
6193   while (true)
6194     {
6195       tree expression;
6196
6197       /* Look for the opening `['.  */
6198       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6199       /* The first expression is not required to be constant.  */
6200       if (!declarator)
6201         {
6202           cp_token *token = cp_lexer_peek_token (parser->lexer);
6203           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6204           /* The standard requires that the expression have integral
6205              type.  DR 74 adds enumeration types.  We believe that the
6206              real intent is that these expressions be handled like the
6207              expression in a `switch' condition, which also allows
6208              classes with a single conversion to integral or
6209              enumeration type.  */
6210           if (!processing_template_decl)
6211             {
6212               expression
6213                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6214                                               expression,
6215                                               /*complain=*/true);
6216               if (!expression)
6217                 {
6218                   error_at (token->location,
6219                             "expression in new-declarator must have integral "
6220                             "or enumeration type");
6221                   expression = error_mark_node;
6222                 }
6223             }
6224         }
6225       /* But all the other expressions must be.  */
6226       else
6227         expression
6228           = cp_parser_constant_expression (parser,
6229                                            /*allow_non_constant=*/false,
6230                                            NULL);
6231       /* Look for the closing `]'.  */
6232       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6233
6234       /* Add this bound to the declarator.  */
6235       declarator = make_array_declarator (declarator, expression);
6236
6237       /* If the next token is not a `[', then there are no more
6238          bounds.  */
6239       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6240         break;
6241     }
6242
6243   return declarator;
6244 }
6245
6246 /* Parse a new-initializer.
6247
6248    new-initializer:
6249      ( expression-list [opt] )
6250      braced-init-list
6251
6252    Returns a representation of the expression-list.  */
6253
6254 static VEC(tree,gc) *
6255 cp_parser_new_initializer (cp_parser* parser)
6256 {
6257   VEC(tree,gc) *expression_list;
6258
6259   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6260     {
6261       tree t;
6262       bool expr_non_constant_p;
6263       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6264       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6265       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6266       expression_list = make_tree_vector_single (t);
6267     }
6268   else
6269     expression_list = (cp_parser_parenthesized_expression_list
6270                        (parser, non_attr, /*cast_p=*/false,
6271                         /*allow_expansion_p=*/true,
6272                         /*non_constant_p=*/NULL));
6273
6274   return expression_list;
6275 }
6276
6277 /* Parse a delete-expression.
6278
6279    delete-expression:
6280      :: [opt] delete cast-expression
6281      :: [opt] delete [ ] cast-expression
6282
6283    Returns a representation of the expression.  */
6284
6285 static tree
6286 cp_parser_delete_expression (cp_parser* parser)
6287 {
6288   bool global_scope_p;
6289   bool array_p;
6290   tree expression;
6291
6292   /* Look for the optional `::' operator.  */
6293   global_scope_p
6294     = (cp_parser_global_scope_opt (parser,
6295                                    /*current_scope_valid_p=*/false)
6296        != NULL_TREE);
6297   /* Look for the `delete' keyword.  */
6298   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6299   /* See if the array syntax is in use.  */
6300   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6301     {
6302       /* Consume the `[' token.  */
6303       cp_lexer_consume_token (parser->lexer);
6304       /* Look for the `]' token.  */
6305       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6306       /* Remember that this is the `[]' construct.  */
6307       array_p = true;
6308     }
6309   else
6310     array_p = false;
6311
6312   /* Parse the cast-expression.  */
6313   expression = cp_parser_simple_cast_expression (parser);
6314
6315   /* A delete-expression may not appear in an integral constant
6316      expression.  */
6317   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6318     return error_mark_node;
6319
6320   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6321                         tf_warning_or_error);
6322 }
6323
6324 /* Returns true if TOKEN may start a cast-expression and false
6325    otherwise.  */
6326
6327 static bool
6328 cp_parser_token_starts_cast_expression (cp_token *token)
6329 {
6330   switch (token->type)
6331     {
6332     case CPP_COMMA:
6333     case CPP_SEMICOLON:
6334     case CPP_QUERY:
6335     case CPP_COLON:
6336     case CPP_CLOSE_SQUARE:
6337     case CPP_CLOSE_PAREN:
6338     case CPP_CLOSE_BRACE:
6339     case CPP_DOT:
6340     case CPP_DOT_STAR:
6341     case CPP_DEREF:
6342     case CPP_DEREF_STAR:
6343     case CPP_DIV:
6344     case CPP_MOD:
6345     case CPP_LSHIFT:
6346     case CPP_RSHIFT:
6347     case CPP_LESS:
6348     case CPP_GREATER:
6349     case CPP_LESS_EQ:
6350     case CPP_GREATER_EQ:
6351     case CPP_EQ_EQ:
6352     case CPP_NOT_EQ:
6353     case CPP_EQ:
6354     case CPP_MULT_EQ:
6355     case CPP_DIV_EQ:
6356     case CPP_MOD_EQ:
6357     case CPP_PLUS_EQ:
6358     case CPP_MINUS_EQ:
6359     case CPP_RSHIFT_EQ:
6360     case CPP_LSHIFT_EQ:
6361     case CPP_AND_EQ:
6362     case CPP_XOR_EQ:
6363     case CPP_OR_EQ:
6364     case CPP_XOR:
6365     case CPP_OR:
6366     case CPP_OR_OR:
6367     case CPP_EOF:
6368       return false;
6369
6370       /* '[' may start a primary-expression in obj-c++.  */
6371     case CPP_OPEN_SQUARE:
6372       return c_dialect_objc ();
6373
6374     default:
6375       return true;
6376     }
6377 }
6378
6379 /* Parse a cast-expression.
6380
6381    cast-expression:
6382      unary-expression
6383      ( type-id ) cast-expression
6384
6385    ADDRESS_P is true iff the unary-expression is appearing as the
6386    operand of the `&' operator.   CAST_P is true if this expression is
6387    the target of a cast.
6388
6389    Returns a representation of the expression.  */
6390
6391 static tree
6392 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6393                            cp_id_kind * pidk)
6394 {
6395   /* If it's a `(', then we might be looking at a cast.  */
6396   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6397     {
6398       tree type = NULL_TREE;
6399       tree expr = NULL_TREE;
6400       bool compound_literal_p;
6401       const char *saved_message;
6402
6403       /* There's no way to know yet whether or not this is a cast.
6404          For example, `(int (3))' is a unary-expression, while `(int)
6405          3' is a cast.  So, we resort to parsing tentatively.  */
6406       cp_parser_parse_tentatively (parser);
6407       /* Types may not be defined in a cast.  */
6408       saved_message = parser->type_definition_forbidden_message;
6409       parser->type_definition_forbidden_message
6410         = G_("types may not be defined in casts");
6411       /* Consume the `('.  */
6412       cp_lexer_consume_token (parser->lexer);
6413       /* A very tricky bit is that `(struct S) { 3 }' is a
6414          compound-literal (which we permit in C++ as an extension).
6415          But, that construct is not a cast-expression -- it is a
6416          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6417          is legal; if the compound-literal were a cast-expression,
6418          you'd need an extra set of parentheses.)  But, if we parse
6419          the type-id, and it happens to be a class-specifier, then we
6420          will commit to the parse at that point, because we cannot
6421          undo the action that is done when creating a new class.  So,
6422          then we cannot back up and do a postfix-expression.
6423
6424          Therefore, we scan ahead to the closing `)', and check to see
6425          if the token after the `)' is a `{'.  If so, we are not
6426          looking at a cast-expression.
6427
6428          Save tokens so that we can put them back.  */
6429       cp_lexer_save_tokens (parser->lexer);
6430       /* Skip tokens until the next token is a closing parenthesis.
6431          If we find the closing `)', and the next token is a `{', then
6432          we are looking at a compound-literal.  */
6433       compound_literal_p
6434         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6435                                                   /*consume_paren=*/true)
6436            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6437       /* Roll back the tokens we skipped.  */
6438       cp_lexer_rollback_tokens (parser->lexer);
6439       /* If we were looking at a compound-literal, simulate an error
6440          so that the call to cp_parser_parse_definitely below will
6441          fail.  */
6442       if (compound_literal_p)
6443         cp_parser_simulate_error (parser);
6444       else
6445         {
6446           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6447           parser->in_type_id_in_expr_p = true;
6448           /* Look for the type-id.  */
6449           type = cp_parser_type_id (parser);
6450           /* Look for the closing `)'.  */
6451           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6452           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6453         }
6454
6455       /* Restore the saved message.  */
6456       parser->type_definition_forbidden_message = saved_message;
6457
6458       /* At this point this can only be either a cast or a
6459          parenthesized ctor such as `(T ())' that looks like a cast to
6460          function returning T.  */
6461       if (!cp_parser_error_occurred (parser)
6462           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6463                                                      (parser->lexer)))
6464         {
6465           cp_parser_parse_definitely (parser);
6466           expr = cp_parser_cast_expression (parser,
6467                                             /*address_p=*/false,
6468                                             /*cast_p=*/true, pidk);
6469
6470           /* Warn about old-style casts, if so requested.  */
6471           if (warn_old_style_cast
6472               && !in_system_header
6473               && !VOID_TYPE_P (type)
6474               && current_lang_name != lang_name_c)
6475             warning (OPT_Wold_style_cast, "use of old-style cast");
6476
6477           /* Only type conversions to integral or enumeration types
6478              can be used in constant-expressions.  */
6479           if (!cast_valid_in_integral_constant_expression_p (type)
6480               && cp_parser_non_integral_constant_expression (parser,
6481                                                              NIC_CAST))
6482             return error_mark_node;
6483
6484           /* Perform the cast.  */
6485           expr = build_c_cast (input_location, type, expr);
6486           return expr;
6487         }
6488       else 
6489         cp_parser_abort_tentative_parse (parser);
6490     }
6491
6492   /* If we get here, then it's not a cast, so it must be a
6493      unary-expression.  */
6494   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6495 }
6496
6497 /* Parse a binary expression of the general form:
6498
6499    pm-expression:
6500      cast-expression
6501      pm-expression .* cast-expression
6502      pm-expression ->* cast-expression
6503
6504    multiplicative-expression:
6505      pm-expression
6506      multiplicative-expression * pm-expression
6507      multiplicative-expression / pm-expression
6508      multiplicative-expression % pm-expression
6509
6510    additive-expression:
6511      multiplicative-expression
6512      additive-expression + multiplicative-expression
6513      additive-expression - multiplicative-expression
6514
6515    shift-expression:
6516      additive-expression
6517      shift-expression << additive-expression
6518      shift-expression >> additive-expression
6519
6520    relational-expression:
6521      shift-expression
6522      relational-expression < shift-expression
6523      relational-expression > shift-expression
6524      relational-expression <= shift-expression
6525      relational-expression >= shift-expression
6526
6527   GNU Extension:
6528
6529    relational-expression:
6530      relational-expression <? shift-expression
6531      relational-expression >? shift-expression
6532
6533    equality-expression:
6534      relational-expression
6535      equality-expression == relational-expression
6536      equality-expression != relational-expression
6537
6538    and-expression:
6539      equality-expression
6540      and-expression & equality-expression
6541
6542    exclusive-or-expression:
6543      and-expression
6544      exclusive-or-expression ^ and-expression
6545
6546    inclusive-or-expression:
6547      exclusive-or-expression
6548      inclusive-or-expression | exclusive-or-expression
6549
6550    logical-and-expression:
6551      inclusive-or-expression
6552      logical-and-expression && inclusive-or-expression
6553
6554    logical-or-expression:
6555      logical-and-expression
6556      logical-or-expression || logical-and-expression
6557
6558    All these are implemented with a single function like:
6559
6560    binary-expression:
6561      simple-cast-expression
6562      binary-expression <token> binary-expression
6563
6564    CAST_P is true if this expression is the target of a cast.
6565
6566    The binops_by_token map is used to get the tree codes for each <token> type.
6567    binary-expressions are associated according to a precedence table.  */
6568
6569 #define TOKEN_PRECEDENCE(token)                              \
6570 (((token->type == CPP_GREATER                                \
6571    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6572   && !parser->greater_than_is_operator_p)                    \
6573  ? PREC_NOT_OPERATOR                                         \
6574  : binops_by_token[token->type].prec)
6575
6576 static tree
6577 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6578                              bool no_toplevel_fold_p,
6579                              enum cp_parser_prec prec,
6580                              cp_id_kind * pidk)
6581 {
6582   cp_parser_expression_stack stack;
6583   cp_parser_expression_stack_entry *sp = &stack[0];
6584   tree lhs, rhs;
6585   cp_token *token;
6586   enum tree_code tree_type, lhs_type, rhs_type;
6587   enum cp_parser_prec new_prec, lookahead_prec;
6588   tree overload;
6589
6590   /* Parse the first expression.  */
6591   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6592   lhs_type = ERROR_MARK;
6593
6594   for (;;)
6595     {
6596       /* Get an operator token.  */
6597       token = cp_lexer_peek_token (parser->lexer);
6598
6599       if (warn_cxx0x_compat
6600           && token->type == CPP_RSHIFT
6601           && !parser->greater_than_is_operator_p)
6602         {
6603           if (warning_at (token->location, OPT_Wc__0x_compat, 
6604                           "%<>>%> operator will be treated as"
6605                           " two right angle brackets in C++0x"))
6606             inform (token->location,
6607                     "suggest parentheses around %<>>%> expression");
6608         }
6609
6610       new_prec = TOKEN_PRECEDENCE (token);
6611
6612       /* Popping an entry off the stack means we completed a subexpression:
6613          - either we found a token which is not an operator (`>' where it is not
6614            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6615            will happen repeatedly;
6616          - or, we found an operator which has lower priority.  This is the case
6617            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6618            parsing `3 * 4'.  */
6619       if (new_prec <= prec)
6620         {
6621           if (sp == stack)
6622             break;
6623           else
6624             goto pop;
6625         }
6626
6627      get_rhs:
6628       tree_type = binops_by_token[token->type].tree_type;
6629
6630       /* We used the operator token.  */
6631       cp_lexer_consume_token (parser->lexer);
6632
6633       /* For "false && x" or "true || x", x will never be executed;
6634          disable warnings while evaluating it.  */
6635       if (tree_type == TRUTH_ANDIF_EXPR)
6636         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6637       else if (tree_type == TRUTH_ORIF_EXPR)
6638         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6639
6640       /* Extract another operand.  It may be the RHS of this expression
6641          or the LHS of a new, higher priority expression.  */
6642       rhs = cp_parser_simple_cast_expression (parser);
6643       rhs_type = ERROR_MARK;
6644
6645       /* Get another operator token.  Look up its precedence to avoid
6646          building a useless (immediately popped) stack entry for common
6647          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6648       token = cp_lexer_peek_token (parser->lexer);
6649       lookahead_prec = TOKEN_PRECEDENCE (token);
6650       if (lookahead_prec > new_prec)
6651         {
6652           /* ... and prepare to parse the RHS of the new, higher priority
6653              expression.  Since precedence levels on the stack are
6654              monotonically increasing, we do not have to care about
6655              stack overflows.  */
6656           sp->prec = prec;
6657           sp->tree_type = tree_type;
6658           sp->lhs = lhs;
6659           sp->lhs_type = lhs_type;
6660           sp++;
6661           lhs = rhs;
6662           lhs_type = rhs_type;
6663           prec = new_prec;
6664           new_prec = lookahead_prec;
6665           goto get_rhs;
6666
6667          pop:
6668           lookahead_prec = new_prec;
6669           /* If the stack is not empty, we have parsed into LHS the right side
6670              (`4' in the example above) of an expression we had suspended.
6671              We can use the information on the stack to recover the LHS (`3')
6672              from the stack together with the tree code (`MULT_EXPR'), and
6673              the precedence of the higher level subexpression
6674              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6675              which will be used to actually build the additive expression.  */
6676           --sp;
6677           prec = sp->prec;
6678           tree_type = sp->tree_type;
6679           rhs = lhs;
6680           rhs_type = lhs_type;
6681           lhs = sp->lhs;
6682           lhs_type = sp->lhs_type;
6683         }
6684
6685       /* Undo the disabling of warnings done above.  */
6686       if (tree_type == TRUTH_ANDIF_EXPR)
6687         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6688       else if (tree_type == TRUTH_ORIF_EXPR)
6689         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6690
6691       overload = NULL;
6692       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6693          ERROR_MARK for everything that is not a binary expression.
6694          This makes warn_about_parentheses miss some warnings that
6695          involve unary operators.  For unary expressions we should
6696          pass the correct tree_code unless the unary expression was
6697          surrounded by parentheses.
6698       */
6699       if (no_toplevel_fold_p
6700           && lookahead_prec <= prec
6701           && sp == stack
6702           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6703         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6704       else
6705         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6706                                  &overload, tf_warning_or_error);
6707       lhs_type = tree_type;
6708
6709       /* If the binary operator required the use of an overloaded operator,
6710          then this expression cannot be an integral constant-expression.
6711          An overloaded operator can be used even if both operands are
6712          otherwise permissible in an integral constant-expression if at
6713          least one of the operands is of enumeration type.  */
6714
6715       if (overload
6716           && cp_parser_non_integral_constant_expression (parser,
6717                                                          NIC_OVERLOADED))
6718         return error_mark_node;
6719     }
6720
6721   return lhs;
6722 }
6723
6724
6725 /* Parse the `? expression : assignment-expression' part of a
6726    conditional-expression.  The LOGICAL_OR_EXPR is the
6727    logical-or-expression that started the conditional-expression.
6728    Returns a representation of the entire conditional-expression.
6729
6730    This routine is used by cp_parser_assignment_expression.
6731
6732      ? expression : assignment-expression
6733
6734    GNU Extensions:
6735
6736      ? : assignment-expression */
6737
6738 static tree
6739 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6740 {
6741   tree expr;
6742   tree assignment_expr;
6743   struct cp_token *token;
6744
6745   /* Consume the `?' token.  */
6746   cp_lexer_consume_token (parser->lexer);
6747   token = cp_lexer_peek_token (parser->lexer);
6748   if (cp_parser_allow_gnu_extensions_p (parser)
6749       && token->type == CPP_COLON)
6750     {
6751       pedwarn (token->location, OPT_pedantic, 
6752                "ISO C++ does not allow ?: with omitted middle operand");
6753       /* Implicit true clause.  */
6754       expr = NULL_TREE;
6755       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6756       warn_for_omitted_condop (token->location, logical_or_expr);
6757     }
6758   else
6759     {
6760       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6761       parser->colon_corrects_to_scope_p = false;
6762       /* Parse the expression.  */
6763       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6764       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6765       c_inhibit_evaluation_warnings +=
6766         ((logical_or_expr == truthvalue_true_node)
6767          - (logical_or_expr == truthvalue_false_node));
6768       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6769     }
6770
6771   /* The next token should be a `:'.  */
6772   cp_parser_require (parser, CPP_COLON, RT_COLON);
6773   /* Parse the assignment-expression.  */
6774   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6775   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6776
6777   /* Build the conditional-expression.  */
6778   return build_x_conditional_expr (logical_or_expr,
6779                                    expr,
6780                                    assignment_expr,
6781                                    tf_warning_or_error);
6782 }
6783
6784 /* Parse an assignment-expression.
6785
6786    assignment-expression:
6787      conditional-expression
6788      logical-or-expression assignment-operator assignment_expression
6789      throw-expression
6790
6791    CAST_P is true if this expression is the target of a cast.
6792
6793    Returns a representation for the expression.  */
6794
6795 static tree
6796 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6797                                  cp_id_kind * pidk)
6798 {
6799   tree expr;
6800
6801   /* If the next token is the `throw' keyword, then we're looking at
6802      a throw-expression.  */
6803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6804     expr = cp_parser_throw_expression (parser);
6805   /* Otherwise, it must be that we are looking at a
6806      logical-or-expression.  */
6807   else
6808     {
6809       /* Parse the binary expressions (logical-or-expression).  */
6810       expr = cp_parser_binary_expression (parser, cast_p, false,
6811                                           PREC_NOT_OPERATOR, pidk);
6812       /* If the next token is a `?' then we're actually looking at a
6813          conditional-expression.  */
6814       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6815         return cp_parser_question_colon_clause (parser, expr);
6816       else
6817         {
6818           enum tree_code assignment_operator;
6819
6820           /* If it's an assignment-operator, we're using the second
6821              production.  */
6822           assignment_operator
6823             = cp_parser_assignment_operator_opt (parser);
6824           if (assignment_operator != ERROR_MARK)
6825             {
6826               bool non_constant_p;
6827
6828               /* Parse the right-hand side of the assignment.  */
6829               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6830
6831               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6832                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6833
6834               /* An assignment may not appear in a
6835                  constant-expression.  */
6836               if (cp_parser_non_integral_constant_expression (parser,
6837                                                               NIC_ASSIGNMENT))
6838                 return error_mark_node;
6839               /* Build the assignment expression.  */
6840               expr = build_x_modify_expr (expr,
6841                                           assignment_operator,
6842                                           rhs,
6843                                           tf_warning_or_error);
6844             }
6845         }
6846     }
6847
6848   return expr;
6849 }
6850
6851 /* Parse an (optional) assignment-operator.
6852
6853    assignment-operator: one of
6854      = *= /= %= += -= >>= <<= &= ^= |=
6855
6856    GNU Extension:
6857
6858    assignment-operator: one of
6859      <?= >?=
6860
6861    If the next token is an assignment operator, the corresponding tree
6862    code is returned, and the token is consumed.  For example, for
6863    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6864    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6865    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6866    operator, ERROR_MARK is returned.  */
6867
6868 static enum tree_code
6869 cp_parser_assignment_operator_opt (cp_parser* parser)
6870 {
6871   enum tree_code op;
6872   cp_token *token;
6873
6874   /* Peek at the next token.  */
6875   token = cp_lexer_peek_token (parser->lexer);
6876
6877   switch (token->type)
6878     {
6879     case CPP_EQ:
6880       op = NOP_EXPR;
6881       break;
6882
6883     case CPP_MULT_EQ:
6884       op = MULT_EXPR;
6885       break;
6886
6887     case CPP_DIV_EQ:
6888       op = TRUNC_DIV_EXPR;
6889       break;
6890
6891     case CPP_MOD_EQ:
6892       op = TRUNC_MOD_EXPR;
6893       break;
6894
6895     case CPP_PLUS_EQ:
6896       op = PLUS_EXPR;
6897       break;
6898
6899     case CPP_MINUS_EQ:
6900       op = MINUS_EXPR;
6901       break;
6902
6903     case CPP_RSHIFT_EQ:
6904       op = RSHIFT_EXPR;
6905       break;
6906
6907     case CPP_LSHIFT_EQ:
6908       op = LSHIFT_EXPR;
6909       break;
6910
6911     case CPP_AND_EQ:
6912       op = BIT_AND_EXPR;
6913       break;
6914
6915     case CPP_XOR_EQ:
6916       op = BIT_XOR_EXPR;
6917       break;
6918
6919     case CPP_OR_EQ:
6920       op = BIT_IOR_EXPR;
6921       break;
6922
6923     default:
6924       /* Nothing else is an assignment operator.  */
6925       op = ERROR_MARK;
6926     }
6927
6928   /* If it was an assignment operator, consume it.  */
6929   if (op != ERROR_MARK)
6930     cp_lexer_consume_token (parser->lexer);
6931
6932   return op;
6933 }
6934
6935 /* Parse an expression.
6936
6937    expression:
6938      assignment-expression
6939      expression , assignment-expression
6940
6941    CAST_P is true if this expression is the target of a cast.
6942
6943    Returns a representation of the expression.  */
6944
6945 static tree
6946 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6947 {
6948   tree expression = NULL_TREE;
6949
6950   while (true)
6951     {
6952       tree assignment_expression;
6953
6954       /* Parse the next assignment-expression.  */
6955       assignment_expression
6956         = cp_parser_assignment_expression (parser, cast_p, pidk);
6957       /* If this is the first assignment-expression, we can just
6958          save it away.  */
6959       if (!expression)
6960         expression = assignment_expression;
6961       else
6962         expression = build_x_compound_expr (expression,
6963                                             assignment_expression,
6964                                             tf_warning_or_error);
6965       /* If the next token is not a comma, then we are done with the
6966          expression.  */
6967       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6968         break;
6969       /* Consume the `,'.  */
6970       cp_lexer_consume_token (parser->lexer);
6971       /* A comma operator cannot appear in a constant-expression.  */
6972       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6973         expression = error_mark_node;
6974     }
6975
6976   return expression;
6977 }
6978
6979 /* Parse a constant-expression.
6980
6981    constant-expression:
6982      conditional-expression
6983
6984   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6985   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6986   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6987   is false, NON_CONSTANT_P should be NULL.  */
6988
6989 static tree
6990 cp_parser_constant_expression (cp_parser* parser,
6991                                bool allow_non_constant_p,
6992                                bool *non_constant_p)
6993 {
6994   bool saved_integral_constant_expression_p;
6995   bool saved_allow_non_integral_constant_expression_p;
6996   bool saved_non_integral_constant_expression_p;
6997   tree expression;
6998
6999   /* It might seem that we could simply parse the
7000      conditional-expression, and then check to see if it were
7001      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7002      one that the compiler can figure out is constant, possibly after
7003      doing some simplifications or optimizations.  The standard has a
7004      precise definition of constant-expression, and we must honor
7005      that, even though it is somewhat more restrictive.
7006
7007      For example:
7008
7009        int i[(2, 3)];
7010
7011      is not a legal declaration, because `(2, 3)' is not a
7012      constant-expression.  The `,' operator is forbidden in a
7013      constant-expression.  However, GCC's constant-folding machinery
7014      will fold this operation to an INTEGER_CST for `3'.  */
7015
7016   /* Save the old settings.  */
7017   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7018   saved_allow_non_integral_constant_expression_p
7019     = parser->allow_non_integral_constant_expression_p;
7020   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7021   /* We are now parsing a constant-expression.  */
7022   parser->integral_constant_expression_p = true;
7023   parser->allow_non_integral_constant_expression_p
7024     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7025   parser->non_integral_constant_expression_p = false;
7026   /* Although the grammar says "conditional-expression", we parse an
7027      "assignment-expression", which also permits "throw-expression"
7028      and the use of assignment operators.  In the case that
7029      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7030      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7031      actually essential that we look for an assignment-expression.
7032      For example, cp_parser_initializer_clauses uses this function to
7033      determine whether a particular assignment-expression is in fact
7034      constant.  */
7035   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7036   /* Restore the old settings.  */
7037   parser->integral_constant_expression_p
7038     = saved_integral_constant_expression_p;
7039   parser->allow_non_integral_constant_expression_p
7040     = saved_allow_non_integral_constant_expression_p;
7041   if (cxx_dialect >= cxx0x)
7042     {
7043       /* Require an rvalue constant expression here; that's what our
7044          callers expect.  Reference constant expressions are handled
7045          separately in e.g. cp_parser_template_argument.  */
7046       bool is_const = potential_rvalue_constant_expression (expression);
7047       parser->non_integral_constant_expression_p = !is_const;
7048       if (!is_const && !allow_non_constant_p)
7049         require_potential_rvalue_constant_expression (expression);
7050     }
7051   if (allow_non_constant_p)
7052     *non_constant_p = parser->non_integral_constant_expression_p;
7053   parser->non_integral_constant_expression_p
7054     = saved_non_integral_constant_expression_p;
7055
7056   return expression;
7057 }
7058
7059 /* Parse __builtin_offsetof.
7060
7061    offsetof-expression:
7062      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7063
7064    offsetof-member-designator:
7065      id-expression
7066      | offsetof-member-designator "." id-expression
7067      | offsetof-member-designator "[" expression "]"
7068      | offsetof-member-designator "->" id-expression  */
7069
7070 static tree
7071 cp_parser_builtin_offsetof (cp_parser *parser)
7072 {
7073   int save_ice_p, save_non_ice_p;
7074   tree type, expr;
7075   cp_id_kind dummy;
7076   cp_token *token;
7077
7078   /* We're about to accept non-integral-constant things, but will
7079      definitely yield an integral constant expression.  Save and
7080      restore these values around our local parsing.  */
7081   save_ice_p = parser->integral_constant_expression_p;
7082   save_non_ice_p = parser->non_integral_constant_expression_p;
7083
7084   /* Consume the "__builtin_offsetof" token.  */
7085   cp_lexer_consume_token (parser->lexer);
7086   /* Consume the opening `('.  */
7087   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7088   /* Parse the type-id.  */
7089   type = cp_parser_type_id (parser);
7090   /* Look for the `,'.  */
7091   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7092   token = cp_lexer_peek_token (parser->lexer);
7093
7094   /* Build the (type *)null that begins the traditional offsetof macro.  */
7095   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7096                             tf_warning_or_error);
7097
7098   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7099   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7100                                                  true, &dummy, token->location);
7101   while (true)
7102     {
7103       token = cp_lexer_peek_token (parser->lexer);
7104       switch (token->type)
7105         {
7106         case CPP_OPEN_SQUARE:
7107           /* offsetof-member-designator "[" expression "]" */
7108           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7109           break;
7110
7111         case CPP_DEREF:
7112           /* offsetof-member-designator "->" identifier */
7113           expr = grok_array_decl (expr, integer_zero_node);
7114           /* FALLTHRU */
7115
7116         case CPP_DOT:
7117           /* offsetof-member-designator "." identifier */
7118           cp_lexer_consume_token (parser->lexer);
7119           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7120                                                          expr, true, &dummy,
7121                                                          token->location);
7122           break;
7123
7124         case CPP_CLOSE_PAREN:
7125           /* Consume the ")" token.  */
7126           cp_lexer_consume_token (parser->lexer);
7127           goto success;
7128
7129         default:
7130           /* Error.  We know the following require will fail, but
7131              that gives the proper error message.  */
7132           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7133           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7134           expr = error_mark_node;
7135           goto failure;
7136         }
7137     }
7138
7139  success:
7140   /* If we're processing a template, we can't finish the semantics yet.
7141      Otherwise we can fold the entire expression now.  */
7142   if (processing_template_decl)
7143     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7144   else
7145     expr = finish_offsetof (expr);
7146
7147  failure:
7148   parser->integral_constant_expression_p = save_ice_p;
7149   parser->non_integral_constant_expression_p = save_non_ice_p;
7150
7151   return expr;
7152 }
7153
7154 /* Parse a trait expression.
7155
7156    Returns a representation of the expression, the underlying type
7157    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7158
7159 static tree
7160 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7161 {
7162   cp_trait_kind kind;
7163   tree type1, type2 = NULL_TREE;
7164   bool binary = false;
7165   cp_decl_specifier_seq decl_specs;
7166
7167   switch (keyword)
7168     {
7169     case RID_HAS_NOTHROW_ASSIGN:
7170       kind = CPTK_HAS_NOTHROW_ASSIGN;
7171       break;
7172     case RID_HAS_NOTHROW_CONSTRUCTOR:
7173       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7174       break;
7175     case RID_HAS_NOTHROW_COPY:
7176       kind = CPTK_HAS_NOTHROW_COPY;
7177       break;
7178     case RID_HAS_TRIVIAL_ASSIGN:
7179       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7180       break;
7181     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7182       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7183       break;
7184     case RID_HAS_TRIVIAL_COPY:
7185       kind = CPTK_HAS_TRIVIAL_COPY;
7186       break;
7187     case RID_HAS_TRIVIAL_DESTRUCTOR:
7188       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7189       break;
7190     case RID_HAS_VIRTUAL_DESTRUCTOR:
7191       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7192       break;
7193     case RID_IS_ABSTRACT:
7194       kind = CPTK_IS_ABSTRACT;
7195       break;
7196     case RID_IS_BASE_OF:
7197       kind = CPTK_IS_BASE_OF;
7198       binary = true;
7199       break;
7200     case RID_IS_CLASS:
7201       kind = CPTK_IS_CLASS;
7202       break;
7203     case RID_IS_CONVERTIBLE_TO:
7204       kind = CPTK_IS_CONVERTIBLE_TO;
7205       binary = true;
7206       break;
7207     case RID_IS_EMPTY:
7208       kind = CPTK_IS_EMPTY;
7209       break;
7210     case RID_IS_ENUM:
7211       kind = CPTK_IS_ENUM;
7212       break;
7213     case RID_IS_LITERAL_TYPE:
7214       kind = CPTK_IS_LITERAL_TYPE;
7215       break;
7216     case RID_IS_POD:
7217       kind = CPTK_IS_POD;
7218       break;
7219     case RID_IS_POLYMORPHIC:
7220       kind = CPTK_IS_POLYMORPHIC;
7221       break;
7222     case RID_IS_STD_LAYOUT:
7223       kind = CPTK_IS_STD_LAYOUT;
7224       break;
7225     case RID_IS_TRIVIAL:
7226       kind = CPTK_IS_TRIVIAL;
7227       break;
7228     case RID_IS_UNION:
7229       kind = CPTK_IS_UNION;
7230       break;
7231     case RID_UNDERLYING_TYPE:
7232       kind = CPTK_UNDERLYING_TYPE;
7233       break;
7234     default:
7235       gcc_unreachable ();
7236     }
7237
7238   /* Consume the token.  */
7239   cp_lexer_consume_token (parser->lexer);
7240
7241   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7242
7243   type1 = cp_parser_type_id (parser);
7244
7245   if (type1 == error_mark_node)
7246     return error_mark_node;
7247
7248   /* Build a trivial decl-specifier-seq.  */
7249   clear_decl_specs (&decl_specs);
7250   decl_specs.type = type1;
7251
7252   /* Call grokdeclarator to figure out what type this is.  */
7253   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7254                           /*initialized=*/0, /*attrlist=*/NULL);
7255
7256   if (binary)
7257     {
7258       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7259  
7260       type2 = cp_parser_type_id (parser);
7261
7262       if (type2 == error_mark_node)
7263         return error_mark_node;
7264
7265       /* Build a trivial decl-specifier-seq.  */
7266       clear_decl_specs (&decl_specs);
7267       decl_specs.type = type2;
7268
7269       /* Call grokdeclarator to figure out what type this is.  */
7270       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7271                               /*initialized=*/0, /*attrlist=*/NULL);
7272     }
7273
7274   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7275
7276   /* Complete the trait expression, which may mean either processing
7277      the trait expr now or saving it for template instantiation.  */
7278   return kind != CPTK_UNDERLYING_TYPE
7279     ? finish_trait_expr (kind, type1, type2)
7280     : finish_underlying_type (type1);
7281 }
7282
7283 /* Lambdas that appear in variable initializer or default argument scope
7284    get that in their mangling, so we need to record it.  We might as well
7285    use the count for function and namespace scopes as well.  */
7286 static GTY(()) tree lambda_scope;
7287 static GTY(()) int lambda_count;
7288 typedef struct GTY(()) tree_int
7289 {
7290   tree t;
7291   int i;
7292 } tree_int;
7293 DEF_VEC_O(tree_int);
7294 DEF_VEC_ALLOC_O(tree_int,gc);
7295 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7296
7297 static void
7298 start_lambda_scope (tree decl)
7299 {
7300   tree_int ti;
7301   gcc_assert (decl);
7302   /* Once we're inside a function, we ignore other scopes and just push
7303      the function again so that popping works properly.  */
7304   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7305     decl = current_function_decl;
7306   ti.t = lambda_scope;
7307   ti.i = lambda_count;
7308   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7309   if (lambda_scope != decl)
7310     {
7311       /* Don't reset the count if we're still in the same function.  */
7312       lambda_scope = decl;
7313       lambda_count = 0;
7314     }
7315 }
7316
7317 static void
7318 record_lambda_scope (tree lambda)
7319 {
7320   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7321   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7322 }
7323
7324 static void
7325 finish_lambda_scope (void)
7326 {
7327   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7328   if (lambda_scope != p->t)
7329     {
7330       lambda_scope = p->t;
7331       lambda_count = p->i;
7332     }
7333   VEC_pop (tree_int, lambda_scope_stack);
7334 }
7335
7336 /* Parse a lambda expression.
7337
7338    lambda-expression:
7339      lambda-introducer lambda-declarator [opt] compound-statement
7340
7341    Returns a representation of the expression.  */
7342
7343 static tree
7344 cp_parser_lambda_expression (cp_parser* parser)
7345 {
7346   tree lambda_expr = build_lambda_expr ();
7347   tree type;
7348   bool ok;
7349
7350   LAMBDA_EXPR_LOCATION (lambda_expr)
7351     = cp_lexer_peek_token (parser->lexer)->location;
7352
7353   if (cp_unevaluated_operand)
7354     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7355               "lambda-expression in unevaluated context");
7356
7357   /* We may be in the middle of deferred access check.  Disable
7358      it now.  */
7359   push_deferring_access_checks (dk_no_deferred);
7360
7361   cp_parser_lambda_introducer (parser, lambda_expr);
7362
7363   type = begin_lambda_type (lambda_expr);
7364
7365   record_lambda_scope (lambda_expr);
7366
7367   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7368   determine_visibility (TYPE_NAME (type));
7369
7370   /* Now that we've started the type, add the capture fields for any
7371      explicit captures.  */
7372   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7373
7374   {
7375     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7376     unsigned int saved_num_template_parameter_lists
7377         = parser->num_template_parameter_lists;
7378
7379     parser->num_template_parameter_lists = 0;
7380
7381     /* By virtue of defining a local class, a lambda expression has access to
7382        the private variables of enclosing classes.  */
7383
7384     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7385
7386     if (ok)
7387       cp_parser_lambda_body (parser, lambda_expr);
7388     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7389       cp_parser_skip_to_end_of_block_or_statement (parser);
7390
7391     /* The capture list was built up in reverse order; fix that now.  */
7392     {
7393       tree newlist = NULL_TREE;
7394       tree elt, next;
7395
7396       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7397            elt; elt = next)
7398         {
7399           tree field = TREE_PURPOSE (elt);
7400           char *buf;
7401
7402           next = TREE_CHAIN (elt);
7403           TREE_CHAIN (elt) = newlist;
7404           newlist = elt;
7405
7406           /* Also add __ to the beginning of the field name so that code
7407              outside the lambda body can't see the captured name.  We could
7408              just remove the name entirely, but this is more useful for
7409              debugging.  */
7410           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7411             /* The 'this' capture already starts with __.  */
7412             continue;
7413
7414           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7415           buf[1] = buf[0] = '_';
7416           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7417                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7418           DECL_NAME (field) = get_identifier (buf);
7419         }
7420       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7421     }
7422
7423     if (ok)
7424       maybe_add_lambda_conv_op (type);
7425
7426     type = finish_struct (type, /*attributes=*/NULL_TREE);
7427
7428     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7429   }
7430
7431   pop_deferring_access_checks ();
7432
7433   if (ok)
7434     return build_lambda_object (lambda_expr);
7435   else
7436     return error_mark_node;
7437 }
7438
7439 /* Parse the beginning of a lambda expression.
7440
7441    lambda-introducer:
7442      [ lambda-capture [opt] ]
7443
7444    LAMBDA_EXPR is the current representation of the lambda expression.  */
7445
7446 static void
7447 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7448 {
7449   /* Need commas after the first capture.  */
7450   bool first = true;
7451
7452   /* Eat the leading `['.  */
7453   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7454
7455   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7456   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7457       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7458     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7459   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7460     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7461
7462   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7463     {
7464       cp_lexer_consume_token (parser->lexer);
7465       first = false;
7466     }
7467
7468   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7469     {
7470       cp_token* capture_token;
7471       tree capture_id;
7472       tree capture_init_expr;
7473       cp_id_kind idk = CP_ID_KIND_NONE;
7474       bool explicit_init_p = false;
7475
7476       enum capture_kind_type
7477       {
7478         BY_COPY,
7479         BY_REFERENCE
7480       };
7481       enum capture_kind_type capture_kind = BY_COPY;
7482
7483       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7484         {
7485           error ("expected end of capture-list");
7486           return;
7487         }
7488
7489       if (first)
7490         first = false;
7491       else
7492         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7493
7494       /* Possibly capture `this'.  */
7495       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7496         {
7497           cp_lexer_consume_token (parser->lexer);
7498           add_capture (lambda_expr,
7499                        /*id=*/get_identifier ("__this"),
7500                        /*initializer=*/finish_this_expr(),
7501                        /*by_reference_p=*/false,
7502                        explicit_init_p);
7503           continue;
7504         }
7505
7506       /* Remember whether we want to capture as a reference or not.  */
7507       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7508         {
7509           capture_kind = BY_REFERENCE;
7510           cp_lexer_consume_token (parser->lexer);
7511         }
7512
7513       /* Get the identifier.  */
7514       capture_token = cp_lexer_peek_token (parser->lexer);
7515       capture_id = cp_parser_identifier (parser);
7516
7517       if (capture_id == error_mark_node)
7518         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7519            delimiters, but I modified this to stop on unnested ']' as well.  It
7520            was already changed to stop on unnested '}', so the
7521            "closing_parenthesis" name is no more misleading with my change.  */
7522         {
7523           cp_parser_skip_to_closing_parenthesis (parser,
7524                                                  /*recovering=*/true,
7525                                                  /*or_comma=*/true,
7526                                                  /*consume_paren=*/true);
7527           break;
7528         }
7529
7530       /* Find the initializer for this capture.  */
7531       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7532         {
7533           /* An explicit expression exists.  */
7534           cp_lexer_consume_token (parser->lexer);
7535           pedwarn (input_location, OPT_pedantic,
7536                    "ISO C++ does not allow initializers "
7537                    "in lambda expression capture lists");
7538           capture_init_expr = cp_parser_assignment_expression (parser,
7539                                                                /*cast_p=*/true,
7540                                                                &idk);
7541           explicit_init_p = true;
7542         }
7543       else
7544         {
7545           const char* error_msg;
7546
7547           /* Turn the identifier into an id-expression.  */
7548           capture_init_expr
7549             = cp_parser_lookup_name
7550                 (parser,
7551                  capture_id,
7552                  none_type,
7553                  /*is_template=*/false,
7554                  /*is_namespace=*/false,
7555                  /*check_dependency=*/true,
7556                  /*ambiguous_decls=*/NULL,
7557                  capture_token->location);
7558
7559           capture_init_expr
7560             = finish_id_expression
7561                 (capture_id,
7562                  capture_init_expr,
7563                  parser->scope,
7564                  &idk,
7565                  /*integral_constant_expression_p=*/false,
7566                  /*allow_non_integral_constant_expression_p=*/false,
7567                  /*non_integral_constant_expression_p=*/NULL,
7568                  /*template_p=*/false,
7569                  /*done=*/true,
7570                  /*address_p=*/false,
7571                  /*template_arg_p=*/false,
7572                  &error_msg,
7573                  capture_token->location);
7574         }
7575
7576       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7577         capture_init_expr
7578           = unqualified_name_lookup_error (capture_init_expr);
7579
7580       add_capture (lambda_expr,
7581                    capture_id,
7582                    capture_init_expr,
7583                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7584                    explicit_init_p);
7585     }
7586
7587   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7588 }
7589
7590 /* Parse the (optional) middle of a lambda expression.
7591
7592    lambda-declarator:
7593      ( parameter-declaration-clause [opt] )
7594        attribute-specifier [opt]
7595        mutable [opt]
7596        exception-specification [opt]
7597        lambda-return-type-clause [opt]
7598
7599    LAMBDA_EXPR is the current representation of the lambda expression.  */
7600
7601 static bool
7602 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7603 {
7604   /* 5.1.1.4 of the standard says:
7605        If a lambda-expression does not include a lambda-declarator, it is as if
7606        the lambda-declarator were ().
7607      This means an empty parameter list, no attributes, and no exception
7608      specification.  */
7609   tree param_list = void_list_node;
7610   tree attributes = NULL_TREE;
7611   tree exception_spec = NULL_TREE;
7612   tree t;
7613
7614   /* The lambda-declarator is optional, but must begin with an opening
7615      parenthesis if present.  */
7616   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7617     {
7618       cp_lexer_consume_token (parser->lexer);
7619
7620       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7621
7622       /* Parse parameters.  */
7623       param_list = cp_parser_parameter_declaration_clause (parser);
7624
7625       /* Default arguments shall not be specified in the
7626          parameter-declaration-clause of a lambda-declarator.  */
7627       for (t = param_list; t; t = TREE_CHAIN (t))
7628         if (TREE_PURPOSE (t))
7629           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7630                    "default argument specified for lambda parameter");
7631
7632       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7633
7634       attributes = cp_parser_attributes_opt (parser);
7635
7636       /* Parse optional `mutable' keyword.  */
7637       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7638         {
7639           cp_lexer_consume_token (parser->lexer);
7640           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7641         }
7642
7643       /* Parse optional exception specification.  */
7644       exception_spec = cp_parser_exception_specification_opt (parser);
7645
7646       /* Parse optional trailing return type.  */
7647       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7648         {
7649           cp_lexer_consume_token (parser->lexer);
7650           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7651         }
7652
7653       /* The function parameters must be in scope all the way until after the
7654          trailing-return-type in case of decltype.  */
7655       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7656         pop_binding (DECL_NAME (t), t);
7657
7658       leave_scope ();
7659     }
7660
7661   /* Create the function call operator.
7662
7663      Messing with declarators like this is no uglier than building up the
7664      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7665      other code.  */
7666   {
7667     cp_decl_specifier_seq return_type_specs;
7668     cp_declarator* declarator;
7669     tree fco;
7670     int quals;
7671     void *p;
7672
7673     clear_decl_specs (&return_type_specs);
7674     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7675       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7676     else
7677       /* Maybe we will deduce the return type later, but we can use void
7678          as a placeholder return type anyways.  */
7679       return_type_specs.type = void_type_node;
7680
7681     p = obstack_alloc (&declarator_obstack, 0);
7682
7683     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7684                                      sfk_none);
7685
7686     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7687              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7688     declarator = make_call_declarator (declarator, param_list, quals,
7689                                        VIRT_SPEC_UNSPECIFIED,
7690                                        exception_spec,
7691                                        /*late_return_type=*/NULL_TREE);
7692     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7693
7694     fco = grokmethod (&return_type_specs,
7695                       declarator,
7696                       attributes);
7697     if (fco != error_mark_node)
7698       {
7699         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7700         DECL_ARTIFICIAL (fco) = 1;
7701       }
7702
7703     finish_member_declaration (fco);
7704
7705     obstack_free (&declarator_obstack, p);
7706
7707     return (fco != error_mark_node);
7708   }
7709 }
7710
7711 /* Parse the body of a lambda expression, which is simply
7712
7713    compound-statement
7714
7715    but which requires special handling.
7716    LAMBDA_EXPR is the current representation of the lambda expression.  */
7717
7718 static void
7719 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7720 {
7721   bool nested = (current_function_decl != NULL_TREE);
7722   if (nested)
7723     push_function_context ();
7724
7725   /* Finish the function call operator
7726      - class_specifier
7727      + late_parsing_for_member
7728      + function_definition_after_declarator
7729      + ctor_initializer_opt_and_function_body  */
7730   {
7731     tree fco = lambda_function (lambda_expr);
7732     tree body;
7733     bool done = false;
7734
7735     /* Let the front end know that we are going to be defining this
7736        function.  */
7737     start_preparsed_function (fco,
7738                               NULL_TREE,
7739                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7740
7741     start_lambda_scope (fco);
7742     body = begin_function_body ();
7743
7744     /* 5.1.1.4 of the standard says:
7745          If a lambda-expression does not include a trailing-return-type, it
7746          is as if the trailing-return-type denotes the following type:
7747           * if the compound-statement is of the form
7748                { return attribute-specifier [opt] expression ; }
7749              the type of the returned expression after lvalue-to-rvalue
7750              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7751              (_conv.array_ 4.2), and function-to-pointer conversion
7752              (_conv.func_ 4.3);
7753           * otherwise, void.  */
7754
7755     /* In a lambda that has neither a lambda-return-type-clause
7756        nor a deducible form, errors should be reported for return statements
7757        in the body.  Since we used void as the placeholder return type, parsing
7758        the body as usual will give such desired behavior.  */
7759     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7760         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7761         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7762         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7763       {
7764         tree compound_stmt;
7765         tree expr = NULL_TREE;
7766         cp_id_kind idk = CP_ID_KIND_NONE;
7767
7768         /* Parse tentatively in case there's more after the initial return
7769            statement.  */
7770         cp_parser_parse_tentatively (parser);
7771
7772         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7773         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7774
7775         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7776
7777         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7778         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7779
7780         if (cp_parser_parse_definitely (parser))
7781           {
7782             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7783
7784             compound_stmt = begin_compound_stmt (0);
7785             /* Will get error here if type not deduced yet.  */
7786             finish_return_stmt (expr);
7787             finish_compound_stmt (compound_stmt);
7788
7789             done = true;
7790           }
7791       }
7792
7793     if (!done)
7794       {
7795         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7796           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7797         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7798            cp_parser_compound_stmt does not pass it.  */
7799         cp_parser_function_body (parser);
7800         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7801       }
7802
7803     finish_function_body (body);
7804     finish_lambda_scope ();
7805
7806     /* Finish the function and generate code for it if necessary.  */
7807     expand_or_defer_fn (finish_function (/*inline*/2));
7808   }
7809
7810   if (nested)
7811     pop_function_context();
7812 }
7813
7814 /* Statements [gram.stmt.stmt]  */
7815
7816 /* Parse a statement.
7817
7818    statement:
7819      labeled-statement
7820      expression-statement
7821      compound-statement
7822      selection-statement
7823      iteration-statement
7824      jump-statement
7825      declaration-statement
7826      try-block
7827
7828   IN_COMPOUND is true when the statement is nested inside a
7829   cp_parser_compound_statement; this matters for certain pragmas.
7830
7831   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7832   is a (possibly labeled) if statement which is not enclosed in braces
7833   and has an else clause.  This is used to implement -Wparentheses.  */
7834
7835 static void
7836 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7837                      bool in_compound, bool *if_p)
7838 {
7839   tree statement;
7840   cp_token *token;
7841   location_t statement_location;
7842
7843  restart:
7844   if (if_p != NULL)
7845     *if_p = false;
7846   /* There is no statement yet.  */
7847   statement = NULL_TREE;
7848   /* Peek at the next token.  */
7849   token = cp_lexer_peek_token (parser->lexer);
7850   /* Remember the location of the first token in the statement.  */
7851   statement_location = token->location;
7852   /* If this is a keyword, then that will often determine what kind of
7853      statement we have.  */
7854   if (token->type == CPP_KEYWORD)
7855     {
7856       enum rid keyword = token->keyword;
7857
7858       switch (keyword)
7859         {
7860         case RID_CASE:
7861         case RID_DEFAULT:
7862           /* Looks like a labeled-statement with a case label.
7863              Parse the label, and then use tail recursion to parse
7864              the statement.  */
7865           cp_parser_label_for_labeled_statement (parser);
7866           goto restart;
7867
7868         case RID_IF:
7869         case RID_SWITCH:
7870           statement = cp_parser_selection_statement (parser, if_p);
7871           break;
7872
7873         case RID_WHILE:
7874         case RID_DO:
7875         case RID_FOR:
7876           statement = cp_parser_iteration_statement (parser);
7877           break;
7878
7879         case RID_BREAK:
7880         case RID_CONTINUE:
7881         case RID_RETURN:
7882         case RID_GOTO:
7883           statement = cp_parser_jump_statement (parser);
7884           break;
7885
7886           /* Objective-C++ exception-handling constructs.  */
7887         case RID_AT_TRY:
7888         case RID_AT_CATCH:
7889         case RID_AT_FINALLY:
7890         case RID_AT_SYNCHRONIZED:
7891         case RID_AT_THROW:
7892           statement = cp_parser_objc_statement (parser);
7893           break;
7894
7895         case RID_TRY:
7896           statement = cp_parser_try_block (parser);
7897           break;
7898
7899         case RID_NAMESPACE:
7900           /* This must be a namespace alias definition.  */
7901           cp_parser_declaration_statement (parser);
7902           return;
7903           
7904         default:
7905           /* It might be a keyword like `int' that can start a
7906              declaration-statement.  */
7907           break;
7908         }
7909     }
7910   else if (token->type == CPP_NAME)
7911     {
7912       /* If the next token is a `:', then we are looking at a
7913          labeled-statement.  */
7914       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7915       if (token->type == CPP_COLON)
7916         {
7917           /* Looks like a labeled-statement with an ordinary label.
7918              Parse the label, and then use tail recursion to parse
7919              the statement.  */
7920           cp_parser_label_for_labeled_statement (parser);
7921           goto restart;
7922         }
7923     }
7924   /* Anything that starts with a `{' must be a compound-statement.  */
7925   else if (token->type == CPP_OPEN_BRACE)
7926     statement = cp_parser_compound_statement (parser, NULL, false, false);
7927   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7928      a statement all its own.  */
7929   else if (token->type == CPP_PRAGMA)
7930     {
7931       /* Only certain OpenMP pragmas are attached to statements, and thus
7932          are considered statements themselves.  All others are not.  In
7933          the context of a compound, accept the pragma as a "statement" and
7934          return so that we can check for a close brace.  Otherwise we
7935          require a real statement and must go back and read one.  */
7936       if (in_compound)
7937         cp_parser_pragma (parser, pragma_compound);
7938       else if (!cp_parser_pragma (parser, pragma_stmt))
7939         goto restart;
7940       return;
7941     }
7942   else if (token->type == CPP_EOF)
7943     {
7944       cp_parser_error (parser, "expected statement");
7945       return;
7946     }
7947
7948   /* Everything else must be a declaration-statement or an
7949      expression-statement.  Try for the declaration-statement
7950      first, unless we are looking at a `;', in which case we know that
7951      we have an expression-statement.  */
7952   if (!statement)
7953     {
7954       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7955         {
7956           cp_parser_parse_tentatively (parser);
7957           /* Try to parse the declaration-statement.  */
7958           cp_parser_declaration_statement (parser);
7959           /* If that worked, we're done.  */
7960           if (cp_parser_parse_definitely (parser))
7961             return;
7962         }
7963       /* Look for an expression-statement instead.  */
7964       statement = cp_parser_expression_statement (parser, in_statement_expr);
7965     }
7966
7967   /* Set the line number for the statement.  */
7968   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7969     SET_EXPR_LOCATION (statement, statement_location);
7970 }
7971
7972 /* Parse the label for a labeled-statement, i.e.
7973
7974    identifier :
7975    case constant-expression :
7976    default :
7977
7978    GNU Extension:
7979    case constant-expression ... constant-expression : statement
7980
7981    When a label is parsed without errors, the label is added to the
7982    parse tree by the finish_* functions, so this function doesn't
7983    have to return the label.  */
7984
7985 static void
7986 cp_parser_label_for_labeled_statement (cp_parser* parser)
7987 {
7988   cp_token *token;
7989   tree label = NULL_TREE;
7990   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7991
7992   /* The next token should be an identifier.  */
7993   token = cp_lexer_peek_token (parser->lexer);
7994   if (token->type != CPP_NAME
7995       && token->type != CPP_KEYWORD)
7996     {
7997       cp_parser_error (parser, "expected labeled-statement");
7998       return;
7999     }
8000
8001   parser->colon_corrects_to_scope_p = false;
8002   switch (token->keyword)
8003     {
8004     case RID_CASE:
8005       {
8006         tree expr, expr_hi;
8007         cp_token *ellipsis;
8008
8009         /* Consume the `case' token.  */
8010         cp_lexer_consume_token (parser->lexer);
8011         /* Parse the constant-expression.  */
8012         expr = cp_parser_constant_expression (parser,
8013                                               /*allow_non_constant_p=*/false,
8014                                               NULL);
8015
8016         ellipsis = cp_lexer_peek_token (parser->lexer);
8017         if (ellipsis->type == CPP_ELLIPSIS)
8018           {
8019             /* Consume the `...' token.  */
8020             cp_lexer_consume_token (parser->lexer);
8021             expr_hi =
8022               cp_parser_constant_expression (parser,
8023                                              /*allow_non_constant_p=*/false,
8024                                              NULL);
8025             /* We don't need to emit warnings here, as the common code
8026                will do this for us.  */
8027           }
8028         else
8029           expr_hi = NULL_TREE;
8030
8031         if (parser->in_switch_statement_p)
8032           finish_case_label (token->location, expr, expr_hi);
8033         else
8034           error_at (token->location,
8035                     "case label %qE not within a switch statement",
8036                     expr);
8037       }
8038       break;
8039
8040     case RID_DEFAULT:
8041       /* Consume the `default' token.  */
8042       cp_lexer_consume_token (parser->lexer);
8043
8044       if (parser->in_switch_statement_p)
8045         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8046       else
8047         error_at (token->location, "case label not within a switch statement");
8048       break;
8049
8050     default:
8051       /* Anything else must be an ordinary label.  */
8052       label = finish_label_stmt (cp_parser_identifier (parser));
8053       break;
8054     }
8055
8056   /* Require the `:' token.  */
8057   cp_parser_require (parser, CPP_COLON, RT_COLON);
8058
8059   /* An ordinary label may optionally be followed by attributes.
8060      However, this is only permitted if the attributes are then
8061      followed by a semicolon.  This is because, for backward
8062      compatibility, when parsing
8063        lab: __attribute__ ((unused)) int i;
8064      we want the attribute to attach to "i", not "lab".  */
8065   if (label != NULL_TREE
8066       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8067     {
8068       tree attrs;
8069
8070       cp_parser_parse_tentatively (parser);
8071       attrs = cp_parser_attributes_opt (parser);
8072       if (attrs == NULL_TREE
8073           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8074         cp_parser_abort_tentative_parse (parser);
8075       else if (!cp_parser_parse_definitely (parser))
8076         ;
8077       else
8078         cplus_decl_attributes (&label, attrs, 0);
8079     }
8080
8081   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8082 }
8083
8084 /* Parse an expression-statement.
8085
8086    expression-statement:
8087      expression [opt] ;
8088
8089    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8090    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8091    indicates whether this expression-statement is part of an
8092    expression statement.  */
8093
8094 static tree
8095 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8096 {
8097   tree statement = NULL_TREE;
8098   cp_token *token = cp_lexer_peek_token (parser->lexer);
8099
8100   /* If the next token is a ';', then there is no expression
8101      statement.  */
8102   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8103     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8104
8105   /* Give a helpful message for "A<T>::type t;" and the like.  */
8106   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8107       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8108     {
8109       if (TREE_CODE (statement) == SCOPE_REF)
8110         error_at (token->location, "need %<typename%> before %qE because "
8111                   "%qT is a dependent scope",
8112                   statement, TREE_OPERAND (statement, 0));
8113       else if (is_overloaded_fn (statement)
8114                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8115         {
8116           /* A::A a; */
8117           tree fn = get_first_fn (statement);
8118           error_at (token->location,
8119                     "%<%T::%D%> names the constructor, not the type",
8120                     DECL_CONTEXT (fn), DECL_NAME (fn));
8121         }
8122     }
8123
8124   /* Consume the final `;'.  */
8125   cp_parser_consume_semicolon_at_end_of_statement (parser);
8126
8127   if (in_statement_expr
8128       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8129     /* This is the final expression statement of a statement
8130        expression.  */
8131     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8132   else if (statement)
8133     statement = finish_expr_stmt (statement);
8134   else
8135     finish_stmt ();
8136
8137   return statement;
8138 }
8139
8140 /* Parse a compound-statement.
8141
8142    compound-statement:
8143      { statement-seq [opt] }
8144
8145    GNU extension:
8146
8147    compound-statement:
8148      { label-declaration-seq [opt] statement-seq [opt] }
8149
8150    label-declaration-seq:
8151      label-declaration
8152      label-declaration-seq label-declaration
8153
8154    Returns a tree representing the statement.  */
8155
8156 static tree
8157 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8158                               bool in_try, bool function_body)
8159 {
8160   tree compound_stmt;
8161
8162   /* Consume the `{'.  */
8163   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8164     return error_mark_node;
8165   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8166       && !function_body)
8167     pedwarn (input_location, OPT_pedantic,
8168              "compound-statement in constexpr function");
8169   /* Begin the compound-statement.  */
8170   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8171   /* If the next keyword is `__label__' we have a label declaration.  */
8172   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8173     cp_parser_label_declaration (parser);
8174   /* Parse an (optional) statement-seq.  */
8175   cp_parser_statement_seq_opt (parser, in_statement_expr);
8176   /* Finish the compound-statement.  */
8177   finish_compound_stmt (compound_stmt);
8178   /* Consume the `}'.  */
8179   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8180
8181   return compound_stmt;
8182 }
8183
8184 /* Parse an (optional) statement-seq.
8185
8186    statement-seq:
8187      statement
8188      statement-seq [opt] statement  */
8189
8190 static void
8191 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8192 {
8193   /* Scan statements until there aren't any more.  */
8194   while (true)
8195     {
8196       cp_token *token = cp_lexer_peek_token (parser->lexer);
8197
8198       /* If we are looking at a `}', then we have run out of
8199          statements; the same is true if we have reached the end
8200          of file, or have stumbled upon a stray '@end'.  */
8201       if (token->type == CPP_CLOSE_BRACE
8202           || token->type == CPP_EOF
8203           || token->type == CPP_PRAGMA_EOL
8204           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8205         break;
8206       
8207       /* If we are in a compound statement and find 'else' then
8208          something went wrong.  */
8209       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8210         {
8211           if (parser->in_statement & IN_IF_STMT) 
8212             break;
8213           else
8214             {
8215               token = cp_lexer_consume_token (parser->lexer);
8216               error_at (token->location, "%<else%> without a previous %<if%>");
8217             }
8218         }
8219
8220       /* Parse the statement.  */
8221       cp_parser_statement (parser, in_statement_expr, true, NULL);
8222     }
8223 }
8224
8225 /* Parse a selection-statement.
8226
8227    selection-statement:
8228      if ( condition ) statement
8229      if ( condition ) statement else statement
8230      switch ( condition ) statement
8231
8232    Returns the new IF_STMT or SWITCH_STMT.
8233
8234    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8235    is a (possibly labeled) if statement which is not enclosed in
8236    braces and has an else clause.  This is used to implement
8237    -Wparentheses.  */
8238
8239 static tree
8240 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8241 {
8242   cp_token *token;
8243   enum rid keyword;
8244
8245   if (if_p != NULL)
8246     *if_p = false;
8247
8248   /* Peek at the next token.  */
8249   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8250
8251   /* See what kind of keyword it is.  */
8252   keyword = token->keyword;
8253   switch (keyword)
8254     {
8255     case RID_IF:
8256     case RID_SWITCH:
8257       {
8258         tree statement;
8259         tree condition;
8260
8261         /* Look for the `('.  */
8262         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8263           {
8264             cp_parser_skip_to_end_of_statement (parser);
8265             return error_mark_node;
8266           }
8267
8268         /* Begin the selection-statement.  */
8269         if (keyword == RID_IF)
8270           statement = begin_if_stmt ();
8271         else
8272           statement = begin_switch_stmt ();
8273
8274         /* Parse the condition.  */
8275         condition = cp_parser_condition (parser);
8276         /* Look for the `)'.  */
8277         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8278           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8279                                                  /*consume_paren=*/true);
8280
8281         if (keyword == RID_IF)
8282           {
8283             bool nested_if;
8284             unsigned char in_statement;
8285
8286             /* Add the condition.  */
8287             finish_if_stmt_cond (condition, statement);
8288
8289             /* Parse the then-clause.  */
8290             in_statement = parser->in_statement;
8291             parser->in_statement |= IN_IF_STMT;
8292             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8293               {
8294                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8295                 add_stmt (build_empty_stmt (loc));
8296                 cp_lexer_consume_token (parser->lexer);
8297                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8298                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8299                               "empty body in an %<if%> statement");
8300                 nested_if = false;
8301               }
8302             else
8303               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8304             parser->in_statement = in_statement;
8305
8306             finish_then_clause (statement);
8307
8308             /* If the next token is `else', parse the else-clause.  */
8309             if (cp_lexer_next_token_is_keyword (parser->lexer,
8310                                                 RID_ELSE))
8311               {
8312                 /* Consume the `else' keyword.  */
8313                 cp_lexer_consume_token (parser->lexer);
8314                 begin_else_clause (statement);
8315                 /* Parse the else-clause.  */
8316                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8317                   {
8318                     location_t loc;
8319                     loc = cp_lexer_peek_token (parser->lexer)->location;
8320                     warning_at (loc,
8321                                 OPT_Wempty_body, "suggest braces around "
8322                                 "empty body in an %<else%> statement");
8323                     add_stmt (build_empty_stmt (loc));
8324                     cp_lexer_consume_token (parser->lexer);
8325                   }
8326                 else
8327                   cp_parser_implicitly_scoped_statement (parser, NULL);
8328
8329                 finish_else_clause (statement);
8330
8331                 /* If we are currently parsing a then-clause, then
8332                    IF_P will not be NULL.  We set it to true to
8333                    indicate that this if statement has an else clause.
8334                    This may trigger the Wparentheses warning below
8335                    when we get back up to the parent if statement.  */
8336                 if (if_p != NULL)
8337                   *if_p = true;
8338               }
8339             else
8340               {
8341                 /* This if statement does not have an else clause.  If
8342                    NESTED_IF is true, then the then-clause is an if
8343                    statement which does have an else clause.  We warn
8344                    about the potential ambiguity.  */
8345                 if (nested_if)
8346                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8347                               "suggest explicit braces to avoid ambiguous"
8348                               " %<else%>");
8349               }
8350
8351             /* Now we're all done with the if-statement.  */
8352             finish_if_stmt (statement);
8353           }
8354         else
8355           {
8356             bool in_switch_statement_p;
8357             unsigned char in_statement;
8358
8359             /* Add the condition.  */
8360             finish_switch_cond (condition, statement);
8361
8362             /* Parse the body of the switch-statement.  */
8363             in_switch_statement_p = parser->in_switch_statement_p;
8364             in_statement = parser->in_statement;
8365             parser->in_switch_statement_p = true;
8366             parser->in_statement |= IN_SWITCH_STMT;
8367             cp_parser_implicitly_scoped_statement (parser, NULL);
8368             parser->in_switch_statement_p = in_switch_statement_p;
8369             parser->in_statement = in_statement;
8370
8371             /* Now we're all done with the switch-statement.  */
8372             finish_switch_stmt (statement);
8373           }
8374
8375         return statement;
8376       }
8377       break;
8378
8379     default:
8380       cp_parser_error (parser, "expected selection-statement");
8381       return error_mark_node;
8382     }
8383 }
8384
8385 /* Parse a condition.
8386
8387    condition:
8388      expression
8389      type-specifier-seq declarator = initializer-clause
8390      type-specifier-seq declarator braced-init-list
8391
8392    GNU Extension:
8393
8394    condition:
8395      type-specifier-seq declarator asm-specification [opt]
8396        attributes [opt] = assignment-expression
8397
8398    Returns the expression that should be tested.  */
8399
8400 static tree
8401 cp_parser_condition (cp_parser* parser)
8402 {
8403   cp_decl_specifier_seq type_specifiers;
8404   const char *saved_message;
8405   int declares_class_or_enum;
8406
8407   /* Try the declaration first.  */
8408   cp_parser_parse_tentatively (parser);
8409   /* New types are not allowed in the type-specifier-seq for a
8410      condition.  */
8411   saved_message = parser->type_definition_forbidden_message;
8412   parser->type_definition_forbidden_message
8413     = G_("types may not be defined in conditions");
8414   /* Parse the type-specifier-seq.  */
8415   cp_parser_decl_specifier_seq (parser,
8416                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8417                                 &type_specifiers,
8418                                 &declares_class_or_enum);
8419   /* Restore the saved message.  */
8420   parser->type_definition_forbidden_message = saved_message;
8421   /* If all is well, we might be looking at a declaration.  */
8422   if (!cp_parser_error_occurred (parser))
8423     {
8424       tree decl;
8425       tree asm_specification;
8426       tree attributes;
8427       cp_declarator *declarator;
8428       tree initializer = NULL_TREE;
8429
8430       /* Parse the declarator.  */
8431       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8432                                          /*ctor_dtor_or_conv_p=*/NULL,
8433                                          /*parenthesized_p=*/NULL,
8434                                          /*member_p=*/false);
8435       /* Parse the attributes.  */
8436       attributes = cp_parser_attributes_opt (parser);
8437       /* Parse the asm-specification.  */
8438       asm_specification = cp_parser_asm_specification_opt (parser);
8439       /* If the next token is not an `=' or '{', then we might still be
8440          looking at an expression.  For example:
8441
8442            if (A(a).x)
8443
8444          looks like a decl-specifier-seq and a declarator -- but then
8445          there is no `=', so this is an expression.  */
8446       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8447           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8448         cp_parser_simulate_error (parser);
8449         
8450       /* If we did see an `=' or '{', then we are looking at a declaration
8451          for sure.  */
8452       if (cp_parser_parse_definitely (parser))
8453         {
8454           tree pushed_scope;
8455           bool non_constant_p;
8456           bool flags = LOOKUP_ONLYCONVERTING;
8457
8458           /* Create the declaration.  */
8459           decl = start_decl (declarator, &type_specifiers,
8460                              /*initialized_p=*/true,
8461                              attributes, /*prefix_attributes=*/NULL_TREE,
8462                              &pushed_scope);
8463
8464           /* Parse the initializer.  */
8465           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8466             {
8467               initializer = cp_parser_braced_list (parser, &non_constant_p);
8468               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8469               flags = 0;
8470             }
8471           else
8472             {
8473               /* Consume the `='.  */
8474               cp_parser_require (parser, CPP_EQ, RT_EQ);
8475               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8476             }
8477           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8478             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8479
8480           /* Process the initializer.  */
8481           cp_finish_decl (decl,
8482                           initializer, !non_constant_p,
8483                           asm_specification,
8484                           flags);
8485
8486           if (pushed_scope)
8487             pop_scope (pushed_scope);
8488
8489           return convert_from_reference (decl);
8490         }
8491     }
8492   /* If we didn't even get past the declarator successfully, we are
8493      definitely not looking at a declaration.  */
8494   else
8495     cp_parser_abort_tentative_parse (parser);
8496
8497   /* Otherwise, we are looking at an expression.  */
8498   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8499 }
8500
8501 /* Parses a for-statement or range-for-statement until the closing ')',
8502    not included. */
8503
8504 static tree
8505 cp_parser_for (cp_parser *parser)
8506 {
8507   tree init, scope, decl;
8508   bool is_range_for;
8509
8510   /* Begin the for-statement.  */
8511   scope = begin_for_scope (&init);
8512
8513   /* Parse the initialization.  */
8514   is_range_for = cp_parser_for_init_statement (parser, &decl);
8515
8516   if (is_range_for)
8517     return cp_parser_range_for (parser, scope, init, decl);
8518   else
8519     return cp_parser_c_for (parser, scope, init);
8520 }
8521
8522 static tree
8523 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8524 {
8525   /* Normal for loop */
8526   tree condition = NULL_TREE;
8527   tree expression = NULL_TREE;
8528   tree stmt;
8529
8530   stmt = begin_for_stmt (scope, init);
8531   /* The for-init-statement has already been parsed in
8532      cp_parser_for_init_statement, so no work is needed here.  */
8533   finish_for_init_stmt (stmt);
8534
8535   /* If there's a condition, process it.  */
8536   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8537     condition = cp_parser_condition (parser);
8538   finish_for_cond (condition, stmt);
8539   /* Look for the `;'.  */
8540   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8541
8542   /* If there's an expression, process it.  */
8543   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8544     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8545   finish_for_expr (expression, stmt);
8546
8547   return stmt;
8548 }
8549
8550 /* Tries to parse a range-based for-statement:
8551
8552   range-based-for:
8553     decl-specifier-seq declarator : expression
8554
8555   The decl-specifier-seq declarator and the `:' are already parsed by
8556   cp_parser_for_init_statement. If processing_template_decl it returns a
8557   newly created RANGE_FOR_STMT; if not, it is converted to a
8558   regular FOR_STMT.  */
8559
8560 static tree
8561 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8562 {
8563   tree stmt, range_expr;
8564
8565   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8566     {
8567       bool expr_non_constant_p;
8568       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8569     }
8570   else
8571     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8572
8573   /* If in template, STMT is converted to a normal for-statement
8574      at instantiation. If not, it is done just ahead. */
8575   if (processing_template_decl)
8576     {
8577       stmt = begin_range_for_stmt (scope, init);
8578       finish_range_for_decl (stmt, range_decl, range_expr);
8579     }
8580   else
8581     {
8582       stmt = begin_for_stmt (scope, init);
8583       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8584     }
8585   return stmt;
8586 }
8587
8588 /* Converts a range-based for-statement into a normal
8589    for-statement, as per the definition.
8590
8591       for (RANGE_DECL : RANGE_EXPR)
8592         BLOCK
8593
8594    should be equivalent to:
8595
8596       {
8597         auto &&__range = RANGE_EXPR;
8598         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8599               __begin != __end;
8600               ++__begin)
8601           {
8602               RANGE_DECL = *__begin;
8603               BLOCK
8604           }
8605       }
8606
8607    If RANGE_EXPR is an array:
8608         BEGIN_EXPR = __range
8609         END_EXPR = __range + ARRAY_SIZE(__range)
8610    Else if RANGE_EXPR has a member 'begin' or 'end':
8611         BEGIN_EXPR = __range.begin()
8612         END_EXPR = __range.end()
8613    Else:
8614         BEGIN_EXPR = begin(__range)
8615         END_EXPR = end(__range);
8616
8617    If __range has a member 'begin' but not 'end', or vice versa, we must
8618    still use the second alternative (it will surely fail, however).
8619    When calling begin()/end() in the third alternative we must use
8620    argument dependent lookup, but always considering 'std' as an associated
8621    namespace.  */
8622
8623 tree
8624 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8625 {
8626   tree range_type, range_temp;
8627   tree begin, end;
8628   tree iter_type, begin_expr, end_expr;
8629   tree condition, expression;
8630
8631   if (range_decl == error_mark_node || range_expr == error_mark_node)
8632     /* If an error happened previously do nothing or else a lot of
8633        unhelpful errors would be issued.  */
8634     begin_expr = end_expr = iter_type = error_mark_node;
8635   else
8636     {
8637       /* Find out the type deduced by the declaration
8638          `auto &&__range = range_expr'.  */
8639       range_type = cp_build_reference_type (make_auto (), true);
8640       range_type = do_auto_deduction (range_type, range_expr,
8641                                       type_uses_auto (range_type));
8642
8643       /* Create the __range variable.  */
8644       range_temp = build_decl (input_location, VAR_DECL,
8645                                get_identifier ("__for_range"), range_type);
8646       TREE_USED (range_temp) = 1;
8647       DECL_ARTIFICIAL (range_temp) = 1;
8648       pushdecl (range_temp);
8649       cp_finish_decl (range_temp, range_expr,
8650                       /*is_constant_init*/false, NULL_TREE,
8651                       LOOKUP_ONLYCONVERTING);
8652
8653       range_temp = convert_from_reference (range_temp);
8654       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8655                                                       &begin_expr, &end_expr);
8656     }
8657
8658   /* The new for initialization statement.  */
8659   begin = build_decl (input_location, VAR_DECL,
8660                       get_identifier ("__for_begin"), iter_type);
8661   TREE_USED (begin) = 1;
8662   DECL_ARTIFICIAL (begin) = 1;
8663   pushdecl (begin);
8664   cp_finish_decl (begin, begin_expr,
8665                   /*is_constant_init*/false, NULL_TREE,
8666                   LOOKUP_ONLYCONVERTING);
8667
8668   end = build_decl (input_location, VAR_DECL,
8669                     get_identifier ("__for_end"), iter_type);
8670   TREE_USED (end) = 1;
8671   DECL_ARTIFICIAL (end) = 1;
8672   pushdecl (end);
8673   cp_finish_decl (end, end_expr,
8674                   /*is_constant_init*/false, NULL_TREE,
8675                   LOOKUP_ONLYCONVERTING);
8676
8677   finish_for_init_stmt (statement);
8678
8679   /* The new for condition.  */
8680   condition = build_x_binary_op (NE_EXPR,
8681                                  begin, ERROR_MARK,
8682                                  end, ERROR_MARK,
8683                                  NULL, tf_warning_or_error);
8684   finish_for_cond (condition, statement);
8685
8686   /* The new increment expression.  */
8687   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8688   finish_for_expr (expression, statement);
8689
8690   /* The declaration is initialized with *__begin inside the loop body.  */
8691   cp_finish_decl (range_decl,
8692                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8693                   /*is_constant_init*/false, NULL_TREE,
8694                   LOOKUP_ONLYCONVERTING);
8695
8696   return statement;
8697 }
8698
8699 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8700    We need to solve both at the same time because the method used
8701    depends on the existence of members begin or end.
8702    Returns the type deduced for the iterator expression.  */
8703
8704 static tree
8705 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8706 {
8707   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8708     {
8709       error ("range-based %<for%> expression of type %qT "
8710              "has incomplete type", TREE_TYPE (range));
8711       *begin = *end = error_mark_node;
8712       return error_mark_node;
8713     }
8714   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8715     {
8716       /* If RANGE is an array, we will use pointer arithmetic.  */
8717       *begin = range;
8718       *end = build_binary_op (input_location, PLUS_EXPR,
8719                               range,
8720                               array_type_nelts_top (TREE_TYPE (range)),
8721                               0);
8722       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8723     }
8724   else
8725     {
8726       /* If it is not an array, we must do a bit of magic.  */
8727       tree id_begin, id_end;
8728       tree member_begin, member_end;
8729
8730       *begin = *end = error_mark_node;
8731
8732       id_begin = get_identifier ("begin");
8733       id_end = get_identifier ("end");
8734       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8735                                     /*protect=*/2, /*want_type=*/false);
8736       member_end = lookup_member (TREE_TYPE (range), id_end,
8737                                   /*protect=*/2, /*want_type=*/false);
8738
8739       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8740         {
8741           /* Use the member functions.  */
8742           if (member_begin != NULL_TREE)
8743             *begin = cp_parser_range_for_member_function (range, id_begin);
8744           else
8745             error ("range-based %<for%> expression of type %qT has an "
8746                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8747
8748           if (member_end != NULL_TREE)
8749             *end = cp_parser_range_for_member_function (range, id_end);
8750           else
8751             error ("range-based %<for%> expression of type %qT has a "
8752                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8753         }
8754       else
8755         {
8756           /* Use global functions with ADL.  */
8757           VEC(tree,gc) *vec;
8758           vec = make_tree_vector ();
8759
8760           VEC_safe_push (tree, gc, vec, range);
8761
8762           member_begin = perform_koenig_lookup (id_begin, vec,
8763                                                 /*include_std=*/true,
8764                                                 tf_warning_or_error);
8765           *begin = finish_call_expr (member_begin, &vec, false, true,
8766                                      tf_warning_or_error);
8767           member_end = perform_koenig_lookup (id_end, vec,
8768                                               /*include_std=*/true,
8769                                               tf_warning_or_error);
8770           *end = finish_call_expr (member_end, &vec, false, true,
8771                                    tf_warning_or_error);
8772
8773           release_tree_vector (vec);
8774         }
8775
8776       /* Last common checks.  */
8777       if (*begin == error_mark_node || *end == error_mark_node)
8778         {
8779           /* If one of the expressions is an error do no more checks.  */
8780           *begin = *end = error_mark_node;
8781           return error_mark_node;
8782         }
8783       else
8784         {
8785           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8786           /* The unqualified type of the __begin and __end temporaries should
8787              be the same, as required by the multiple auto declaration.  */
8788           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8789             error ("inconsistent begin/end types in range-based %<for%> "
8790                    "statement: %qT and %qT",
8791                    TREE_TYPE (*begin), TREE_TYPE (*end));
8792           return iter_type;
8793         }
8794     }
8795 }
8796
8797 /* Helper function for cp_parser_perform_range_for_lookup.
8798    Builds a tree for RANGE.IDENTIFIER().  */
8799
8800 static tree
8801 cp_parser_range_for_member_function (tree range, tree identifier)
8802 {
8803   tree member, res;
8804   VEC(tree,gc) *vec;
8805
8806   member = finish_class_member_access_expr (range, identifier,
8807                                             false, tf_warning_or_error);
8808   if (member == error_mark_node)
8809     return error_mark_node;
8810
8811   vec = make_tree_vector ();
8812   res = finish_call_expr (member, &vec,
8813                           /*disallow_virtual=*/false,
8814                           /*koenig_p=*/false,
8815                           tf_warning_or_error);
8816   release_tree_vector (vec);
8817   return res;
8818 }
8819
8820 /* Parse an iteration-statement.
8821
8822    iteration-statement:
8823      while ( condition ) statement
8824      do statement while ( expression ) ;
8825      for ( for-init-statement condition [opt] ; expression [opt] )
8826        statement
8827
8828    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8829
8830 static tree
8831 cp_parser_iteration_statement (cp_parser* parser)
8832 {
8833   cp_token *token;
8834   enum rid keyword;
8835   tree statement;
8836   unsigned char in_statement;
8837
8838   /* Peek at the next token.  */
8839   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8840   if (!token)
8841     return error_mark_node;
8842
8843   /* Remember whether or not we are already within an iteration
8844      statement.  */
8845   in_statement = parser->in_statement;
8846
8847   /* See what kind of keyword it is.  */
8848   keyword = token->keyword;
8849   switch (keyword)
8850     {
8851     case RID_WHILE:
8852       {
8853         tree condition;
8854
8855         /* Begin the while-statement.  */
8856         statement = begin_while_stmt ();
8857         /* Look for the `('.  */
8858         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8859         /* Parse the condition.  */
8860         condition = cp_parser_condition (parser);
8861         finish_while_stmt_cond (condition, statement);
8862         /* Look for the `)'.  */
8863         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8864         /* Parse the dependent statement.  */
8865         parser->in_statement = IN_ITERATION_STMT;
8866         cp_parser_already_scoped_statement (parser);
8867         parser->in_statement = in_statement;
8868         /* We're done with the while-statement.  */
8869         finish_while_stmt (statement);
8870       }
8871       break;
8872
8873     case RID_DO:
8874       {
8875         tree expression;
8876
8877         /* Begin the do-statement.  */
8878         statement = begin_do_stmt ();
8879         /* Parse the body of the do-statement.  */
8880         parser->in_statement = IN_ITERATION_STMT;
8881         cp_parser_implicitly_scoped_statement (parser, NULL);
8882         parser->in_statement = in_statement;
8883         finish_do_body (statement);
8884         /* Look for the `while' keyword.  */
8885         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8886         /* Look for the `('.  */
8887         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8888         /* Parse the expression.  */
8889         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8890         /* We're done with the do-statement.  */
8891         finish_do_stmt (expression, statement);
8892         /* Look for the `)'.  */
8893         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8894         /* Look for the `;'.  */
8895         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8896       }
8897       break;
8898
8899     case RID_FOR:
8900       {
8901         /* Look for the `('.  */
8902         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8903
8904         statement = cp_parser_for (parser);
8905
8906         /* Look for the `)'.  */
8907         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8908
8909         /* Parse the body of the for-statement.  */
8910         parser->in_statement = IN_ITERATION_STMT;
8911         cp_parser_already_scoped_statement (parser);
8912         parser->in_statement = in_statement;
8913
8914         /* We're done with the for-statement.  */
8915         finish_for_stmt (statement);
8916       }
8917       break;
8918
8919     default:
8920       cp_parser_error (parser, "expected iteration-statement");
8921       statement = error_mark_node;
8922       break;
8923     }
8924
8925   return statement;
8926 }
8927
8928 /* Parse a for-init-statement or the declarator of a range-based-for.
8929    Returns true if a range-based-for declaration is seen.
8930
8931    for-init-statement:
8932      expression-statement
8933      simple-declaration  */
8934
8935 static bool
8936 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8937 {
8938   /* If the next token is a `;', then we have an empty
8939      expression-statement.  Grammatically, this is also a
8940      simple-declaration, but an invalid one, because it does not
8941      declare anything.  Therefore, if we did not handle this case
8942      specially, we would issue an error message about an invalid
8943      declaration.  */
8944   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8945     {
8946       bool is_range_for = false;
8947       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8948
8949       parser->colon_corrects_to_scope_p = false;
8950
8951       /* We're going to speculatively look for a declaration, falling back
8952          to an expression, if necessary.  */
8953       cp_parser_parse_tentatively (parser);
8954       /* Parse the declaration.  */
8955       cp_parser_simple_declaration (parser,
8956                                     /*function_definition_allowed_p=*/false,
8957                                     decl);
8958       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8959       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8960         {
8961           /* It is a range-for, consume the ':' */
8962           cp_lexer_consume_token (parser->lexer);
8963           is_range_for = true;
8964           if (cxx_dialect < cxx0x)
8965             {
8966               error_at (cp_lexer_peek_token (parser->lexer)->location,
8967                         "range-based %<for%> loops are not allowed "
8968                         "in C++98 mode");
8969               *decl = error_mark_node;
8970             }
8971         }
8972       else
8973           /* The ';' is not consumed yet because we told
8974              cp_parser_simple_declaration not to.  */
8975           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8976
8977       if (cp_parser_parse_definitely (parser))
8978         return is_range_for;
8979       /* If the tentative parse failed, then we shall need to look for an
8980          expression-statement.  */
8981     }
8982   /* If we are here, it is an expression-statement.  */
8983   cp_parser_expression_statement (parser, NULL_TREE);
8984   return false;
8985 }
8986
8987 /* Parse a jump-statement.
8988
8989    jump-statement:
8990      break ;
8991      continue ;
8992      return expression [opt] ;
8993      return braced-init-list ;
8994      goto identifier ;
8995
8996    GNU extension:
8997
8998    jump-statement:
8999      goto * expression ;
9000
9001    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9002
9003 static tree
9004 cp_parser_jump_statement (cp_parser* parser)
9005 {
9006   tree statement = error_mark_node;
9007   cp_token *token;
9008   enum rid keyword;
9009   unsigned char in_statement;
9010
9011   /* Peek at the next token.  */
9012   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9013   if (!token)
9014     return error_mark_node;
9015
9016   /* See what kind of keyword it is.  */
9017   keyword = token->keyword;
9018   switch (keyword)
9019     {
9020     case RID_BREAK:
9021       in_statement = parser->in_statement & ~IN_IF_STMT;      
9022       switch (in_statement)
9023         {
9024         case 0:
9025           error_at (token->location, "break statement not within loop or switch");
9026           break;
9027         default:
9028           gcc_assert ((in_statement & IN_SWITCH_STMT)
9029                       || in_statement == IN_ITERATION_STMT);
9030           statement = finish_break_stmt ();
9031           break;
9032         case IN_OMP_BLOCK:
9033           error_at (token->location, "invalid exit from OpenMP structured block");
9034           break;
9035         case IN_OMP_FOR:
9036           error_at (token->location, "break statement used with OpenMP for loop");
9037           break;
9038         }
9039       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9040       break;
9041
9042     case RID_CONTINUE:
9043       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9044         {
9045         case 0:
9046           error_at (token->location, "continue statement not within a loop");
9047           break;
9048         case IN_ITERATION_STMT:
9049         case IN_OMP_FOR:
9050           statement = finish_continue_stmt ();
9051           break;
9052         case IN_OMP_BLOCK:
9053           error_at (token->location, "invalid exit from OpenMP structured block");
9054           break;
9055         default:
9056           gcc_unreachable ();
9057         }
9058       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9059       break;
9060
9061     case RID_RETURN:
9062       {
9063         tree expr;
9064         bool expr_non_constant_p;
9065
9066         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9067           {
9068             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9069             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9070           }
9071         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9072           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9073         else
9074           /* If the next token is a `;', then there is no
9075              expression.  */
9076           expr = NULL_TREE;
9077         /* Build the return-statement.  */
9078         statement = finish_return_stmt (expr);
9079         /* Look for the final `;'.  */
9080         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9081       }
9082       break;
9083
9084     case RID_GOTO:
9085       /* Create the goto-statement.  */
9086       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9087         {
9088           /* Issue a warning about this use of a GNU extension.  */
9089           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9090           /* Consume the '*' token.  */
9091           cp_lexer_consume_token (parser->lexer);
9092           /* Parse the dependent expression.  */
9093           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9094         }
9095       else
9096         finish_goto_stmt (cp_parser_identifier (parser));
9097       /* Look for the final `;'.  */
9098       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9099       break;
9100
9101     default:
9102       cp_parser_error (parser, "expected jump-statement");
9103       break;
9104     }
9105
9106   return statement;
9107 }
9108
9109 /* Parse a declaration-statement.
9110
9111    declaration-statement:
9112      block-declaration  */
9113
9114 static void
9115 cp_parser_declaration_statement (cp_parser* parser)
9116 {
9117   void *p;
9118
9119   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9120   p = obstack_alloc (&declarator_obstack, 0);
9121
9122  /* Parse the block-declaration.  */
9123   cp_parser_block_declaration (parser, /*statement_p=*/true);
9124
9125   /* Free any declarators allocated.  */
9126   obstack_free (&declarator_obstack, p);
9127
9128   /* Finish off the statement.  */
9129   finish_stmt ();
9130 }
9131
9132 /* Some dependent statements (like `if (cond) statement'), are
9133    implicitly in their own scope.  In other words, if the statement is
9134    a single statement (as opposed to a compound-statement), it is
9135    none-the-less treated as if it were enclosed in braces.  Any
9136    declarations appearing in the dependent statement are out of scope
9137    after control passes that point.  This function parses a statement,
9138    but ensures that is in its own scope, even if it is not a
9139    compound-statement.
9140
9141    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9142    is a (possibly labeled) if statement which is not enclosed in
9143    braces and has an else clause.  This is used to implement
9144    -Wparentheses.
9145
9146    Returns the new statement.  */
9147
9148 static tree
9149 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9150 {
9151   tree statement;
9152
9153   if (if_p != NULL)
9154     *if_p = false;
9155
9156   /* Mark if () ; with a special NOP_EXPR.  */
9157   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9158     {
9159       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9160       cp_lexer_consume_token (parser->lexer);
9161       statement = add_stmt (build_empty_stmt (loc));
9162     }
9163   /* if a compound is opened, we simply parse the statement directly.  */
9164   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9165     statement = cp_parser_compound_statement (parser, NULL, false, false);
9166   /* If the token is not a `{', then we must take special action.  */
9167   else
9168     {
9169       /* Create a compound-statement.  */
9170       statement = begin_compound_stmt (0);
9171       /* Parse the dependent-statement.  */
9172       cp_parser_statement (parser, NULL_TREE, false, if_p);
9173       /* Finish the dummy compound-statement.  */
9174       finish_compound_stmt (statement);
9175     }
9176
9177   /* Return the statement.  */
9178   return statement;
9179 }
9180
9181 /* For some dependent statements (like `while (cond) statement'), we
9182    have already created a scope.  Therefore, even if the dependent
9183    statement is a compound-statement, we do not want to create another
9184    scope.  */
9185
9186 static void
9187 cp_parser_already_scoped_statement (cp_parser* parser)
9188 {
9189   /* If the token is a `{', then we must take special action.  */
9190   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9191     cp_parser_statement (parser, NULL_TREE, false, NULL);
9192   else
9193     {
9194       /* Avoid calling cp_parser_compound_statement, so that we
9195          don't create a new scope.  Do everything else by hand.  */
9196       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9197       /* If the next keyword is `__label__' we have a label declaration.  */
9198       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9199         cp_parser_label_declaration (parser);
9200       /* Parse an (optional) statement-seq.  */
9201       cp_parser_statement_seq_opt (parser, NULL_TREE);
9202       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9203     }
9204 }
9205
9206 /* Declarations [gram.dcl.dcl] */
9207
9208 /* Parse an optional declaration-sequence.
9209
9210    declaration-seq:
9211      declaration
9212      declaration-seq declaration  */
9213
9214 static void
9215 cp_parser_declaration_seq_opt (cp_parser* parser)
9216 {
9217   while (true)
9218     {
9219       cp_token *token;
9220
9221       token = cp_lexer_peek_token (parser->lexer);
9222
9223       if (token->type == CPP_CLOSE_BRACE
9224           || token->type == CPP_EOF
9225           || token->type == CPP_PRAGMA_EOL)
9226         break;
9227
9228       if (token->type == CPP_SEMICOLON)
9229         {
9230           /* A declaration consisting of a single semicolon is
9231              invalid.  Allow it unless we're being pedantic.  */
9232           cp_lexer_consume_token (parser->lexer);
9233           if (!in_system_header)
9234             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9235           continue;
9236         }
9237
9238       /* If we're entering or exiting a region that's implicitly
9239          extern "C", modify the lang context appropriately.  */
9240       if (!parser->implicit_extern_c && token->implicit_extern_c)
9241         {
9242           push_lang_context (lang_name_c);
9243           parser->implicit_extern_c = true;
9244         }
9245       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9246         {
9247           pop_lang_context ();
9248           parser->implicit_extern_c = false;
9249         }
9250
9251       if (token->type == CPP_PRAGMA)
9252         {
9253           /* A top-level declaration can consist solely of a #pragma.
9254              A nested declaration cannot, so this is done here and not
9255              in cp_parser_declaration.  (A #pragma at block scope is
9256              handled in cp_parser_statement.)  */
9257           cp_parser_pragma (parser, pragma_external);
9258           continue;
9259         }
9260
9261       /* Parse the declaration itself.  */
9262       cp_parser_declaration (parser);
9263     }
9264 }
9265
9266 /* Parse a declaration.
9267
9268    declaration:
9269      block-declaration
9270      function-definition
9271      template-declaration
9272      explicit-instantiation
9273      explicit-specialization
9274      linkage-specification
9275      namespace-definition
9276
9277    GNU extension:
9278
9279    declaration:
9280       __extension__ declaration */
9281
9282 static void
9283 cp_parser_declaration (cp_parser* parser)
9284 {
9285   cp_token token1;
9286   cp_token token2;
9287   int saved_pedantic;
9288   void *p;
9289   tree attributes = NULL_TREE;
9290
9291   /* Check for the `__extension__' keyword.  */
9292   if (cp_parser_extension_opt (parser, &saved_pedantic))
9293     {
9294       /* Parse the qualified declaration.  */
9295       cp_parser_declaration (parser);
9296       /* Restore the PEDANTIC flag.  */
9297       pedantic = saved_pedantic;
9298
9299       return;
9300     }
9301
9302   /* Try to figure out what kind of declaration is present.  */
9303   token1 = *cp_lexer_peek_token (parser->lexer);
9304
9305   if (token1.type != CPP_EOF)
9306     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9307   else
9308     {
9309       token2.type = CPP_EOF;
9310       token2.keyword = RID_MAX;
9311     }
9312
9313   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9314   p = obstack_alloc (&declarator_obstack, 0);
9315
9316   /* If the next token is `extern' and the following token is a string
9317      literal, then we have a linkage specification.  */
9318   if (token1.keyword == RID_EXTERN
9319       && cp_parser_is_string_literal (&token2))
9320     cp_parser_linkage_specification (parser);
9321   /* If the next token is `template', then we have either a template
9322      declaration, an explicit instantiation, or an explicit
9323      specialization.  */
9324   else if (token1.keyword == RID_TEMPLATE)
9325     {
9326       /* `template <>' indicates a template specialization.  */
9327       if (token2.type == CPP_LESS
9328           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9329         cp_parser_explicit_specialization (parser);
9330       /* `template <' indicates a template declaration.  */
9331       else if (token2.type == CPP_LESS)
9332         cp_parser_template_declaration (parser, /*member_p=*/false);
9333       /* Anything else must be an explicit instantiation.  */
9334       else
9335         cp_parser_explicit_instantiation (parser);
9336     }
9337   /* If the next token is `export', then we have a template
9338      declaration.  */
9339   else if (token1.keyword == RID_EXPORT)
9340     cp_parser_template_declaration (parser, /*member_p=*/false);
9341   /* If the next token is `extern', 'static' or 'inline' and the one
9342      after that is `template', we have a GNU extended explicit
9343      instantiation directive.  */
9344   else if (cp_parser_allow_gnu_extensions_p (parser)
9345            && (token1.keyword == RID_EXTERN
9346                || token1.keyword == RID_STATIC
9347                || token1.keyword == RID_INLINE)
9348            && token2.keyword == RID_TEMPLATE)
9349     cp_parser_explicit_instantiation (parser);
9350   /* If the next token is `namespace', check for a named or unnamed
9351      namespace definition.  */
9352   else if (token1.keyword == RID_NAMESPACE
9353            && (/* A named namespace definition.  */
9354                (token2.type == CPP_NAME
9355                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9356                     != CPP_EQ))
9357                /* An unnamed namespace definition.  */
9358                || token2.type == CPP_OPEN_BRACE
9359                || token2.keyword == RID_ATTRIBUTE))
9360     cp_parser_namespace_definition (parser);
9361   /* An inline (associated) namespace definition.  */
9362   else if (token1.keyword == RID_INLINE
9363            && token2.keyword == RID_NAMESPACE)
9364     cp_parser_namespace_definition (parser);
9365   /* Objective-C++ declaration/definition.  */
9366   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9367     cp_parser_objc_declaration (parser, NULL_TREE);
9368   else if (c_dialect_objc ()
9369            && token1.keyword == RID_ATTRIBUTE
9370            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9371     cp_parser_objc_declaration (parser, attributes);
9372   /* We must have either a block declaration or a function
9373      definition.  */
9374   else
9375     /* Try to parse a block-declaration, or a function-definition.  */
9376     cp_parser_block_declaration (parser, /*statement_p=*/false);
9377
9378   /* Free any declarators allocated.  */
9379   obstack_free (&declarator_obstack, p);
9380 }
9381
9382 /* Parse a block-declaration.
9383
9384    block-declaration:
9385      simple-declaration
9386      asm-definition
9387      namespace-alias-definition
9388      using-declaration
9389      using-directive
9390
9391    GNU Extension:
9392
9393    block-declaration:
9394      __extension__ block-declaration
9395
9396    C++0x Extension:
9397
9398    block-declaration:
9399      static_assert-declaration
9400
9401    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9402    part of a declaration-statement.  */
9403
9404 static void
9405 cp_parser_block_declaration (cp_parser *parser,
9406                              bool      statement_p)
9407 {
9408   cp_token *token1;
9409   int saved_pedantic;
9410
9411   /* Check for the `__extension__' keyword.  */
9412   if (cp_parser_extension_opt (parser, &saved_pedantic))
9413     {
9414       /* Parse the qualified declaration.  */
9415       cp_parser_block_declaration (parser, statement_p);
9416       /* Restore the PEDANTIC flag.  */
9417       pedantic = saved_pedantic;
9418
9419       return;
9420     }
9421
9422   /* Peek at the next token to figure out which kind of declaration is
9423      present.  */
9424   token1 = cp_lexer_peek_token (parser->lexer);
9425
9426   /* If the next keyword is `asm', we have an asm-definition.  */
9427   if (token1->keyword == RID_ASM)
9428     {
9429       if (statement_p)
9430         cp_parser_commit_to_tentative_parse (parser);
9431       cp_parser_asm_definition (parser);
9432     }
9433   /* If the next keyword is `namespace', we have a
9434      namespace-alias-definition.  */
9435   else if (token1->keyword == RID_NAMESPACE)
9436     cp_parser_namespace_alias_definition (parser);
9437   /* If the next keyword is `using', we have either a
9438      using-declaration or a using-directive.  */
9439   else if (token1->keyword == RID_USING)
9440     {
9441       cp_token *token2;
9442
9443       if (statement_p)
9444         cp_parser_commit_to_tentative_parse (parser);
9445       /* If the token after `using' is `namespace', then we have a
9446          using-directive.  */
9447       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9448       if (token2->keyword == RID_NAMESPACE)
9449         cp_parser_using_directive (parser);
9450       /* Otherwise, it's a using-declaration.  */
9451       else
9452         cp_parser_using_declaration (parser,
9453                                      /*access_declaration_p=*/false);
9454     }
9455   /* If the next keyword is `__label__' we have a misplaced label
9456      declaration.  */
9457   else if (token1->keyword == RID_LABEL)
9458     {
9459       cp_lexer_consume_token (parser->lexer);
9460       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9461       cp_parser_skip_to_end_of_statement (parser);
9462       /* If the next token is now a `;', consume it.  */
9463       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9464         cp_lexer_consume_token (parser->lexer);
9465     }
9466   /* If the next token is `static_assert' we have a static assertion.  */
9467   else if (token1->keyword == RID_STATIC_ASSERT)
9468     cp_parser_static_assert (parser, /*member_p=*/false);
9469   /* Anything else must be a simple-declaration.  */
9470   else
9471     cp_parser_simple_declaration (parser, !statement_p,
9472                                   /*maybe_range_for_decl*/NULL);
9473 }
9474
9475 /* Parse a simple-declaration.
9476
9477    simple-declaration:
9478      decl-specifier-seq [opt] init-declarator-list [opt] ;
9479
9480    init-declarator-list:
9481      init-declarator
9482      init-declarator-list , init-declarator
9483
9484    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9485    function-definition as a simple-declaration.
9486
9487    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9488    parsed declaration if it is an uninitialized single declarator not followed
9489    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9490    if present, will not be consumed.  */
9491
9492 static void
9493 cp_parser_simple_declaration (cp_parser* parser,
9494                               bool function_definition_allowed_p,
9495                               tree *maybe_range_for_decl)
9496 {
9497   cp_decl_specifier_seq decl_specifiers;
9498   int declares_class_or_enum;
9499   bool saw_declarator;
9500
9501   if (maybe_range_for_decl)
9502     *maybe_range_for_decl = NULL_TREE;
9503
9504   /* Defer access checks until we know what is being declared; the
9505      checks for names appearing in the decl-specifier-seq should be
9506      done as if we were in the scope of the thing being declared.  */
9507   push_deferring_access_checks (dk_deferred);
9508
9509   /* Parse the decl-specifier-seq.  We have to keep track of whether
9510      or not the decl-specifier-seq declares a named class or
9511      enumeration type, since that is the only case in which the
9512      init-declarator-list is allowed to be empty.
9513
9514      [dcl.dcl]
9515
9516      In a simple-declaration, the optional init-declarator-list can be
9517      omitted only when declaring a class or enumeration, that is when
9518      the decl-specifier-seq contains either a class-specifier, an
9519      elaborated-type-specifier, or an enum-specifier.  */
9520   cp_parser_decl_specifier_seq (parser,
9521                                 CP_PARSER_FLAGS_OPTIONAL,
9522                                 &decl_specifiers,
9523                                 &declares_class_or_enum);
9524   /* We no longer need to defer access checks.  */
9525   stop_deferring_access_checks ();
9526
9527   /* In a block scope, a valid declaration must always have a
9528      decl-specifier-seq.  By not trying to parse declarators, we can
9529      resolve the declaration/expression ambiguity more quickly.  */
9530   if (!function_definition_allowed_p
9531       && !decl_specifiers.any_specifiers_p)
9532     {
9533       cp_parser_error (parser, "expected declaration");
9534       goto done;
9535     }
9536
9537   /* If the next two tokens are both identifiers, the code is
9538      erroneous. The usual cause of this situation is code like:
9539
9540        T t;
9541
9542      where "T" should name a type -- but does not.  */
9543   if (!decl_specifiers.any_type_specifiers_p
9544       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9545     {
9546       /* If parsing tentatively, we should commit; we really are
9547          looking at a declaration.  */
9548       cp_parser_commit_to_tentative_parse (parser);
9549       /* Give up.  */
9550       goto done;
9551     }
9552
9553   /* If we have seen at least one decl-specifier, and the next token
9554      is not a parenthesis, then we must be looking at a declaration.
9555      (After "int (" we might be looking at a functional cast.)  */
9556   if (decl_specifiers.any_specifiers_p
9557       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9558       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9559       && !cp_parser_error_occurred (parser))
9560     cp_parser_commit_to_tentative_parse (parser);
9561
9562   /* Keep going until we hit the `;' at the end of the simple
9563      declaration.  */
9564   saw_declarator = false;
9565   while (cp_lexer_next_token_is_not (parser->lexer,
9566                                      CPP_SEMICOLON))
9567     {
9568       cp_token *token;
9569       bool function_definition_p;
9570       tree decl;
9571
9572       if (saw_declarator)
9573         {
9574           /* If we are processing next declarator, coma is expected */
9575           token = cp_lexer_peek_token (parser->lexer);
9576           gcc_assert (token->type == CPP_COMMA);
9577           cp_lexer_consume_token (parser->lexer);
9578           if (maybe_range_for_decl)
9579             *maybe_range_for_decl = error_mark_node;
9580         }
9581       else
9582         saw_declarator = true;
9583
9584       /* Parse the init-declarator.  */
9585       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9586                                         /*checks=*/NULL,
9587                                         function_definition_allowed_p,
9588                                         /*member_p=*/false,
9589                                         declares_class_or_enum,
9590                                         &function_definition_p,
9591                                         maybe_range_for_decl);
9592       /* If an error occurred while parsing tentatively, exit quickly.
9593          (That usually happens when in the body of a function; each
9594          statement is treated as a declaration-statement until proven
9595          otherwise.)  */
9596       if (cp_parser_error_occurred (parser))
9597         goto done;
9598       /* Handle function definitions specially.  */
9599       if (function_definition_p)
9600         {
9601           /* If the next token is a `,', then we are probably
9602              processing something like:
9603
9604                void f() {}, *p;
9605
9606              which is erroneous.  */
9607           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9608             {
9609               cp_token *token = cp_lexer_peek_token (parser->lexer);
9610               error_at (token->location,
9611                         "mixing"
9612                         " declarations and function-definitions is forbidden");
9613             }
9614           /* Otherwise, we're done with the list of declarators.  */
9615           else
9616             {
9617               pop_deferring_access_checks ();
9618               return;
9619             }
9620         }
9621       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9622         *maybe_range_for_decl = decl;
9623       /* The next token should be either a `,' or a `;'.  */
9624       token = cp_lexer_peek_token (parser->lexer);
9625       /* If it's a `,', there are more declarators to come.  */
9626       if (token->type == CPP_COMMA)
9627         /* will be consumed next time around */;
9628       /* If it's a `;', we are done.  */
9629       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9630         break;
9631       /* Anything else is an error.  */
9632       else
9633         {
9634           /* If we have already issued an error message we don't need
9635              to issue another one.  */
9636           if (decl != error_mark_node
9637               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9638             cp_parser_error (parser, "expected %<,%> or %<;%>");
9639           /* Skip tokens until we reach the end of the statement.  */
9640           cp_parser_skip_to_end_of_statement (parser);
9641           /* If the next token is now a `;', consume it.  */
9642           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9643             cp_lexer_consume_token (parser->lexer);
9644           goto done;
9645         }
9646       /* After the first time around, a function-definition is not
9647          allowed -- even if it was OK at first.  For example:
9648
9649            int i, f() {}
9650
9651          is not valid.  */
9652       function_definition_allowed_p = false;
9653     }
9654
9655   /* Issue an error message if no declarators are present, and the
9656      decl-specifier-seq does not itself declare a class or
9657      enumeration.  */
9658   if (!saw_declarator)
9659     {
9660       if (cp_parser_declares_only_class_p (parser))
9661         shadow_tag (&decl_specifiers);
9662       /* Perform any deferred access checks.  */
9663       perform_deferred_access_checks ();
9664     }
9665
9666   /* Consume the `;'.  */
9667   if (!maybe_range_for_decl)
9668       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9669
9670  done:
9671   pop_deferring_access_checks ();
9672 }
9673
9674 /* Parse a decl-specifier-seq.
9675
9676    decl-specifier-seq:
9677      decl-specifier-seq [opt] decl-specifier
9678
9679    decl-specifier:
9680      storage-class-specifier
9681      type-specifier
9682      function-specifier
9683      friend
9684      typedef
9685
9686    GNU Extension:
9687
9688    decl-specifier:
9689      attributes
9690
9691    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9692
9693    The parser flags FLAGS is used to control type-specifier parsing.
9694
9695    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9696    flags:
9697
9698      1: one of the decl-specifiers is an elaborated-type-specifier
9699         (i.e., a type declaration)
9700      2: one of the decl-specifiers is an enum-specifier or a
9701         class-specifier (i.e., a type definition)
9702
9703    */
9704
9705 static void
9706 cp_parser_decl_specifier_seq (cp_parser* parser,
9707                               cp_parser_flags flags,
9708                               cp_decl_specifier_seq *decl_specs,
9709                               int* declares_class_or_enum)
9710 {
9711   bool constructor_possible_p = !parser->in_declarator_p;
9712   cp_token *start_token = NULL;
9713
9714   /* Clear DECL_SPECS.  */
9715   clear_decl_specs (decl_specs);
9716
9717   /* Assume no class or enumeration type is declared.  */
9718   *declares_class_or_enum = 0;
9719
9720   /* Keep reading specifiers until there are no more to read.  */
9721   while (true)
9722     {
9723       bool constructor_p;
9724       bool found_decl_spec;
9725       cp_token *token;
9726
9727       /* Peek at the next token.  */
9728       token = cp_lexer_peek_token (parser->lexer);
9729
9730       /* Save the first token of the decl spec list for error
9731          reporting.  */
9732       if (!start_token)
9733         start_token = token;
9734       /* Handle attributes.  */
9735       if (token->keyword == RID_ATTRIBUTE)
9736         {
9737           /* Parse the attributes.  */
9738           decl_specs->attributes
9739             = chainon (decl_specs->attributes,
9740                        cp_parser_attributes_opt (parser));
9741           continue;
9742         }
9743       /* Assume we will find a decl-specifier keyword.  */
9744       found_decl_spec = true;
9745       /* If the next token is an appropriate keyword, we can simply
9746          add it to the list.  */
9747       switch (token->keyword)
9748         {
9749           /* decl-specifier:
9750                friend
9751                constexpr */
9752         case RID_FRIEND:
9753           if (!at_class_scope_p ())
9754             {
9755               error_at (token->location, "%<friend%> used outside of class");
9756               cp_lexer_purge_token (parser->lexer);
9757             }
9758           else
9759             {
9760               ++decl_specs->specs[(int) ds_friend];
9761               /* Consume the token.  */
9762               cp_lexer_consume_token (parser->lexer);
9763             }
9764           break;
9765
9766         case RID_CONSTEXPR:
9767           ++decl_specs->specs[(int) ds_constexpr];
9768           cp_lexer_consume_token (parser->lexer);
9769           break;
9770
9771           /* function-specifier:
9772                inline
9773                virtual
9774                explicit  */
9775         case RID_INLINE:
9776         case RID_VIRTUAL:
9777         case RID_EXPLICIT:
9778           cp_parser_function_specifier_opt (parser, decl_specs);
9779           break;
9780
9781           /* decl-specifier:
9782                typedef  */
9783         case RID_TYPEDEF:
9784           ++decl_specs->specs[(int) ds_typedef];
9785           /* Consume the token.  */
9786           cp_lexer_consume_token (parser->lexer);
9787           /* A constructor declarator cannot appear in a typedef.  */
9788           constructor_possible_p = false;
9789           /* The "typedef" keyword can only occur in a declaration; we
9790              may as well commit at this point.  */
9791           cp_parser_commit_to_tentative_parse (parser);
9792
9793           if (decl_specs->storage_class != sc_none)
9794             decl_specs->conflicting_specifiers_p = true;
9795           break;
9796
9797           /* storage-class-specifier:
9798                auto
9799                register
9800                static
9801                extern
9802                mutable
9803
9804              GNU Extension:
9805                thread  */
9806         case RID_AUTO:
9807           if (cxx_dialect == cxx98) 
9808             {
9809               /* Consume the token.  */
9810               cp_lexer_consume_token (parser->lexer);
9811
9812               /* Complain about `auto' as a storage specifier, if
9813                  we're complaining about C++0x compatibility.  */
9814               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9815                           " will change meaning in C++0x; please remove it");
9816
9817               /* Set the storage class anyway.  */
9818               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9819                                            token->location);
9820             }
9821           else
9822             /* C++0x auto type-specifier.  */
9823             found_decl_spec = false;
9824           break;
9825
9826         case RID_REGISTER:
9827         case RID_STATIC:
9828         case RID_EXTERN:
9829         case RID_MUTABLE:
9830           /* Consume the token.  */
9831           cp_lexer_consume_token (parser->lexer);
9832           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9833                                        token->location);
9834           break;
9835         case RID_THREAD:
9836           /* Consume the token.  */
9837           cp_lexer_consume_token (parser->lexer);
9838           ++decl_specs->specs[(int) ds_thread];
9839           break;
9840
9841         default:
9842           /* We did not yet find a decl-specifier yet.  */
9843           found_decl_spec = false;
9844           break;
9845         }
9846
9847       if (found_decl_spec
9848           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9849           && token->keyword != RID_CONSTEXPR)
9850         error ("decl-specifier invalid in condition");
9851
9852       /* Constructors are a special case.  The `S' in `S()' is not a
9853          decl-specifier; it is the beginning of the declarator.  */
9854       constructor_p
9855         = (!found_decl_spec
9856            && constructor_possible_p
9857            && (cp_parser_constructor_declarator_p
9858                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9859
9860       /* If we don't have a DECL_SPEC yet, then we must be looking at
9861          a type-specifier.  */
9862       if (!found_decl_spec && !constructor_p)
9863         {
9864           int decl_spec_declares_class_or_enum;
9865           bool is_cv_qualifier;
9866           tree type_spec;
9867
9868           type_spec
9869             = cp_parser_type_specifier (parser, flags,
9870                                         decl_specs,
9871                                         /*is_declaration=*/true,
9872                                         &decl_spec_declares_class_or_enum,
9873                                         &is_cv_qualifier);
9874           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9875
9876           /* If this type-specifier referenced a user-defined type
9877              (a typedef, class-name, etc.), then we can't allow any
9878              more such type-specifiers henceforth.
9879
9880              [dcl.spec]
9881
9882              The longest sequence of decl-specifiers that could
9883              possibly be a type name is taken as the
9884              decl-specifier-seq of a declaration.  The sequence shall
9885              be self-consistent as described below.
9886
9887              [dcl.type]
9888
9889              As a general rule, at most one type-specifier is allowed
9890              in the complete decl-specifier-seq of a declaration.  The
9891              only exceptions are the following:
9892
9893              -- const or volatile can be combined with any other
9894                 type-specifier.
9895
9896              -- signed or unsigned can be combined with char, long,
9897                 short, or int.
9898
9899              -- ..
9900
9901              Example:
9902
9903                typedef char* Pc;
9904                void g (const int Pc);
9905
9906              Here, Pc is *not* part of the decl-specifier seq; it's
9907              the declarator.  Therefore, once we see a type-specifier
9908              (other than a cv-qualifier), we forbid any additional
9909              user-defined types.  We *do* still allow things like `int
9910              int' to be considered a decl-specifier-seq, and issue the
9911              error message later.  */
9912           if (type_spec && !is_cv_qualifier)
9913             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9914           /* A constructor declarator cannot follow a type-specifier.  */
9915           if (type_spec)
9916             {
9917               constructor_possible_p = false;
9918               found_decl_spec = true;
9919               if (!is_cv_qualifier)
9920                 decl_specs->any_type_specifiers_p = true;
9921             }
9922         }
9923
9924       /* If we still do not have a DECL_SPEC, then there are no more
9925          decl-specifiers.  */
9926       if (!found_decl_spec)
9927         break;
9928
9929       decl_specs->any_specifiers_p = true;
9930       /* After we see one decl-specifier, further decl-specifiers are
9931          always optional.  */
9932       flags |= CP_PARSER_FLAGS_OPTIONAL;
9933     }
9934
9935   cp_parser_check_decl_spec (decl_specs, start_token->location);
9936
9937   /* Don't allow a friend specifier with a class definition.  */
9938   if (decl_specs->specs[(int) ds_friend] != 0
9939       && (*declares_class_or_enum & 2))
9940     error_at (start_token->location,
9941               "class definition may not be declared a friend");
9942 }
9943
9944 /* Parse an (optional) storage-class-specifier.
9945
9946    storage-class-specifier:
9947      auto
9948      register
9949      static
9950      extern
9951      mutable
9952
9953    GNU Extension:
9954
9955    storage-class-specifier:
9956      thread
9957
9958    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9959
9960 static tree
9961 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9962 {
9963   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9964     {
9965     case RID_AUTO:
9966       if (cxx_dialect != cxx98)
9967         return NULL_TREE;
9968       /* Fall through for C++98.  */
9969
9970     case RID_REGISTER:
9971     case RID_STATIC:
9972     case RID_EXTERN:
9973     case RID_MUTABLE:
9974     case RID_THREAD:
9975       /* Consume the token.  */
9976       return cp_lexer_consume_token (parser->lexer)->u.value;
9977
9978     default:
9979       return NULL_TREE;
9980     }
9981 }
9982
9983 /* Parse an (optional) function-specifier.
9984
9985    function-specifier:
9986      inline
9987      virtual
9988      explicit
9989
9990    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9991    Updates DECL_SPECS, if it is non-NULL.  */
9992
9993 static tree
9994 cp_parser_function_specifier_opt (cp_parser* parser,
9995                                   cp_decl_specifier_seq *decl_specs)
9996 {
9997   cp_token *token = cp_lexer_peek_token (parser->lexer);
9998   switch (token->keyword)
9999     {
10000     case RID_INLINE:
10001       if (decl_specs)
10002         ++decl_specs->specs[(int) ds_inline];
10003       break;
10004
10005     case RID_VIRTUAL:
10006       /* 14.5.2.3 [temp.mem]
10007
10008          A member function template shall not be virtual.  */
10009       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10010         error_at (token->location, "templates may not be %<virtual%>");
10011       else if (decl_specs)
10012         ++decl_specs->specs[(int) ds_virtual];
10013       break;
10014
10015     case RID_EXPLICIT:
10016       if (decl_specs)
10017         ++decl_specs->specs[(int) ds_explicit];
10018       break;
10019
10020     default:
10021       return NULL_TREE;
10022     }
10023
10024   /* Consume the token.  */
10025   return cp_lexer_consume_token (parser->lexer)->u.value;
10026 }
10027
10028 /* Parse a linkage-specification.
10029
10030    linkage-specification:
10031      extern string-literal { declaration-seq [opt] }
10032      extern string-literal declaration  */
10033
10034 static void
10035 cp_parser_linkage_specification (cp_parser* parser)
10036 {
10037   tree linkage;
10038
10039   /* Look for the `extern' keyword.  */
10040   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10041
10042   /* Look for the string-literal.  */
10043   linkage = cp_parser_string_literal (parser, false, false);
10044
10045   /* Transform the literal into an identifier.  If the literal is a
10046      wide-character string, or contains embedded NULs, then we can't
10047      handle it as the user wants.  */
10048   if (strlen (TREE_STRING_POINTER (linkage))
10049       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10050     {
10051       cp_parser_error (parser, "invalid linkage-specification");
10052       /* Assume C++ linkage.  */
10053       linkage = lang_name_cplusplus;
10054     }
10055   else
10056     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10057
10058   /* We're now using the new linkage.  */
10059   push_lang_context (linkage);
10060
10061   /* If the next token is a `{', then we're using the first
10062      production.  */
10063   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10064     {
10065       /* Consume the `{' token.  */
10066       cp_lexer_consume_token (parser->lexer);
10067       /* Parse the declarations.  */
10068       cp_parser_declaration_seq_opt (parser);
10069       /* Look for the closing `}'.  */
10070       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10071     }
10072   /* Otherwise, there's just one declaration.  */
10073   else
10074     {
10075       bool saved_in_unbraced_linkage_specification_p;
10076
10077       saved_in_unbraced_linkage_specification_p
10078         = parser->in_unbraced_linkage_specification_p;
10079       parser->in_unbraced_linkage_specification_p = true;
10080       cp_parser_declaration (parser);
10081       parser->in_unbraced_linkage_specification_p
10082         = saved_in_unbraced_linkage_specification_p;
10083     }
10084
10085   /* We're done with the linkage-specification.  */
10086   pop_lang_context ();
10087 }
10088
10089 /* Parse a static_assert-declaration.
10090
10091    static_assert-declaration:
10092      static_assert ( constant-expression , string-literal ) ; 
10093
10094    If MEMBER_P, this static_assert is a class member.  */
10095
10096 static void 
10097 cp_parser_static_assert(cp_parser *parser, bool member_p)
10098 {
10099   tree condition;
10100   tree message;
10101   cp_token *token;
10102   location_t saved_loc;
10103   bool dummy;
10104
10105   /* Peek at the `static_assert' token so we can keep track of exactly
10106      where the static assertion started.  */
10107   token = cp_lexer_peek_token (parser->lexer);
10108   saved_loc = token->location;
10109
10110   /* Look for the `static_assert' keyword.  */
10111   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10112                                   RT_STATIC_ASSERT))
10113     return;
10114
10115   /*  We know we are in a static assertion; commit to any tentative
10116       parse.  */
10117   if (cp_parser_parsing_tentatively (parser))
10118     cp_parser_commit_to_tentative_parse (parser);
10119
10120   /* Parse the `(' starting the static assertion condition.  */
10121   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10122
10123   /* Parse the constant-expression.  Allow a non-constant expression
10124      here in order to give better diagnostics in finish_static_assert.  */
10125   condition = 
10126     cp_parser_constant_expression (parser,
10127                                    /*allow_non_constant_p=*/true,
10128                                    /*non_constant_p=*/&dummy);
10129
10130   /* Parse the separating `,'.  */
10131   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10132
10133   /* Parse the string-literal message.  */
10134   message = cp_parser_string_literal (parser, 
10135                                       /*translate=*/false,
10136                                       /*wide_ok=*/true);
10137
10138   /* A `)' completes the static assertion.  */
10139   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10140     cp_parser_skip_to_closing_parenthesis (parser, 
10141                                            /*recovering=*/true, 
10142                                            /*or_comma=*/false,
10143                                            /*consume_paren=*/true);
10144
10145   /* A semicolon terminates the declaration.  */
10146   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10147
10148   /* Complete the static assertion, which may mean either processing 
10149      the static assert now or saving it for template instantiation.  */
10150   finish_static_assert (condition, message, saved_loc, member_p);
10151 }
10152
10153 /* Parse a `decltype' type. Returns the type. 
10154
10155    simple-type-specifier:
10156      decltype ( expression )  */
10157
10158 static tree
10159 cp_parser_decltype (cp_parser *parser)
10160 {
10161   tree expr;
10162   bool id_expression_or_member_access_p = false;
10163   const char *saved_message;
10164   bool saved_integral_constant_expression_p;
10165   bool saved_non_integral_constant_expression_p;
10166   cp_token *id_expr_start_token;
10167
10168   /* Look for the `decltype' token.  */
10169   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10170     return error_mark_node;
10171
10172   /* Types cannot be defined in a `decltype' expression.  Save away the
10173      old message.  */
10174   saved_message = parser->type_definition_forbidden_message;
10175
10176   /* And create the new one.  */
10177   parser->type_definition_forbidden_message
10178     = G_("types may not be defined in %<decltype%> expressions");
10179
10180   /* The restrictions on constant-expressions do not apply inside
10181      decltype expressions.  */
10182   saved_integral_constant_expression_p
10183     = parser->integral_constant_expression_p;
10184   saved_non_integral_constant_expression_p
10185     = parser->non_integral_constant_expression_p;
10186   parser->integral_constant_expression_p = false;
10187
10188   /* Do not actually evaluate the expression.  */
10189   ++cp_unevaluated_operand;
10190
10191   /* Do not warn about problems with the expression.  */
10192   ++c_inhibit_evaluation_warnings;
10193
10194   /* Parse the opening `('.  */
10195   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10196     return error_mark_node;
10197   
10198   /* First, try parsing an id-expression.  */
10199   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10200   cp_parser_parse_tentatively (parser);
10201   expr = cp_parser_id_expression (parser,
10202                                   /*template_keyword_p=*/false,
10203                                   /*check_dependency_p=*/true,
10204                                   /*template_p=*/NULL,
10205                                   /*declarator_p=*/false,
10206                                   /*optional_p=*/false);
10207
10208   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10209     {
10210       bool non_integral_constant_expression_p = false;
10211       tree id_expression = expr;
10212       cp_id_kind idk;
10213       const char *error_msg;
10214
10215       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10216         /* Lookup the name we got back from the id-expression.  */
10217         expr = cp_parser_lookup_name (parser, expr,
10218                                       none_type,
10219                                       /*is_template=*/false,
10220                                       /*is_namespace=*/false,
10221                                       /*check_dependency=*/true,
10222                                       /*ambiguous_decls=*/NULL,
10223                                       id_expr_start_token->location);
10224
10225       if (expr
10226           && expr != error_mark_node
10227           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10228           && TREE_CODE (expr) != TYPE_DECL
10229           && (TREE_CODE (expr) != BIT_NOT_EXPR
10230               || !TYPE_P (TREE_OPERAND (expr, 0)))
10231           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10232         {
10233           /* Complete lookup of the id-expression.  */
10234           expr = (finish_id_expression
10235                   (id_expression, expr, parser->scope, &idk,
10236                    /*integral_constant_expression_p=*/false,
10237                    /*allow_non_integral_constant_expression_p=*/true,
10238                    &non_integral_constant_expression_p,
10239                    /*template_p=*/false,
10240                    /*done=*/true,
10241                    /*address_p=*/false,
10242                    /*template_arg_p=*/false,
10243                    &error_msg,
10244                    id_expr_start_token->location));
10245
10246           if (expr == error_mark_node)
10247             /* We found an id-expression, but it was something that we
10248                should not have found. This is an error, not something
10249                we can recover from, so note that we found an
10250                id-expression and we'll recover as gracefully as
10251                possible.  */
10252             id_expression_or_member_access_p = true;
10253         }
10254
10255       if (expr 
10256           && expr != error_mark_node
10257           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10258         /* We have an id-expression.  */
10259         id_expression_or_member_access_p = true;
10260     }
10261
10262   if (!id_expression_or_member_access_p)
10263     {
10264       /* Abort the id-expression parse.  */
10265       cp_parser_abort_tentative_parse (parser);
10266
10267       /* Parsing tentatively, again.  */
10268       cp_parser_parse_tentatively (parser);
10269
10270       /* Parse a class member access.  */
10271       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10272                                            /*cast_p=*/false,
10273                                            /*member_access_only_p=*/true, NULL);
10274
10275       if (expr 
10276           && expr != error_mark_node
10277           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10278         /* We have an id-expression.  */
10279         id_expression_or_member_access_p = true;
10280     }
10281
10282   if (id_expression_or_member_access_p)
10283     /* We have parsed the complete id-expression or member access.  */
10284     cp_parser_parse_definitely (parser);
10285   else
10286     {
10287       bool saved_greater_than_is_operator_p;
10288
10289       /* Abort our attempt to parse an id-expression or member access
10290          expression.  */
10291       cp_parser_abort_tentative_parse (parser);
10292
10293       /* Within a parenthesized expression, a `>' token is always
10294          the greater-than operator.  */
10295       saved_greater_than_is_operator_p
10296         = parser->greater_than_is_operator_p;
10297       parser->greater_than_is_operator_p = true;
10298
10299       /* Parse a full expression.  */
10300       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10301
10302       /* The `>' token might be the end of a template-id or
10303          template-parameter-list now.  */
10304       parser->greater_than_is_operator_p
10305         = saved_greater_than_is_operator_p;
10306     }
10307
10308   /* Go back to evaluating expressions.  */
10309   --cp_unevaluated_operand;
10310   --c_inhibit_evaluation_warnings;
10311
10312   /* Restore the old message and the integral constant expression
10313      flags.  */
10314   parser->type_definition_forbidden_message = saved_message;
10315   parser->integral_constant_expression_p
10316     = saved_integral_constant_expression_p;
10317   parser->non_integral_constant_expression_p
10318     = saved_non_integral_constant_expression_p;
10319
10320   if (expr == error_mark_node)
10321     {
10322       /* Skip everything up to the closing `)'.  */
10323       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10324                                              /*consume_paren=*/true);
10325       return error_mark_node;
10326     }
10327   
10328   /* Parse to the closing `)'.  */
10329   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10330     {
10331       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10332                                              /*consume_paren=*/true);
10333       return error_mark_node;
10334     }
10335
10336   return finish_decltype_type (expr, id_expression_or_member_access_p,
10337                                tf_warning_or_error);
10338 }
10339
10340 /* Special member functions [gram.special] */
10341
10342 /* Parse a conversion-function-id.
10343
10344    conversion-function-id:
10345      operator conversion-type-id
10346
10347    Returns an IDENTIFIER_NODE representing the operator.  */
10348
10349 static tree
10350 cp_parser_conversion_function_id (cp_parser* parser)
10351 {
10352   tree type;
10353   tree saved_scope;
10354   tree saved_qualifying_scope;
10355   tree saved_object_scope;
10356   tree pushed_scope = NULL_TREE;
10357
10358   /* Look for the `operator' token.  */
10359   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10360     return error_mark_node;
10361   /* When we parse the conversion-type-id, the current scope will be
10362      reset.  However, we need that information in able to look up the
10363      conversion function later, so we save it here.  */
10364   saved_scope = parser->scope;
10365   saved_qualifying_scope = parser->qualifying_scope;
10366   saved_object_scope = parser->object_scope;
10367   /* We must enter the scope of the class so that the names of
10368      entities declared within the class are available in the
10369      conversion-type-id.  For example, consider:
10370
10371        struct S {
10372          typedef int I;
10373          operator I();
10374        };
10375
10376        S::operator I() { ... }
10377
10378      In order to see that `I' is a type-name in the definition, we
10379      must be in the scope of `S'.  */
10380   if (saved_scope)
10381     pushed_scope = push_scope (saved_scope);
10382   /* Parse the conversion-type-id.  */
10383   type = cp_parser_conversion_type_id (parser);
10384   /* Leave the scope of the class, if any.  */
10385   if (pushed_scope)
10386     pop_scope (pushed_scope);
10387   /* Restore the saved scope.  */
10388   parser->scope = saved_scope;
10389   parser->qualifying_scope = saved_qualifying_scope;
10390   parser->object_scope = saved_object_scope;
10391   /* If the TYPE is invalid, indicate failure.  */
10392   if (type == error_mark_node)
10393     return error_mark_node;
10394   return mangle_conv_op_name_for_type (type);
10395 }
10396
10397 /* Parse a conversion-type-id:
10398
10399    conversion-type-id:
10400      type-specifier-seq conversion-declarator [opt]
10401
10402    Returns the TYPE specified.  */
10403
10404 static tree
10405 cp_parser_conversion_type_id (cp_parser* parser)
10406 {
10407   tree attributes;
10408   cp_decl_specifier_seq type_specifiers;
10409   cp_declarator *declarator;
10410   tree type_specified;
10411
10412   /* Parse the attributes.  */
10413   attributes = cp_parser_attributes_opt (parser);
10414   /* Parse the type-specifiers.  */
10415   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10416                                 /*is_trailing_return=*/false,
10417                                 &type_specifiers);
10418   /* If that didn't work, stop.  */
10419   if (type_specifiers.type == error_mark_node)
10420     return error_mark_node;
10421   /* Parse the conversion-declarator.  */
10422   declarator = cp_parser_conversion_declarator_opt (parser);
10423
10424   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10425                                     /*initialized=*/0, &attributes);
10426   if (attributes)
10427     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10428
10429   /* Don't give this error when parsing tentatively.  This happens to
10430      work because we always parse this definitively once.  */
10431   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10432       && type_uses_auto (type_specified))
10433     {
10434       error ("invalid use of %<auto%> in conversion operator");
10435       return error_mark_node;
10436     }
10437
10438   return type_specified;
10439 }
10440
10441 /* Parse an (optional) conversion-declarator.
10442
10443    conversion-declarator:
10444      ptr-operator conversion-declarator [opt]
10445
10446    */
10447
10448 static cp_declarator *
10449 cp_parser_conversion_declarator_opt (cp_parser* parser)
10450 {
10451   enum tree_code code;
10452   tree class_type;
10453   cp_cv_quals cv_quals;
10454
10455   /* We don't know if there's a ptr-operator next, or not.  */
10456   cp_parser_parse_tentatively (parser);
10457   /* Try the ptr-operator.  */
10458   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10459   /* If it worked, look for more conversion-declarators.  */
10460   if (cp_parser_parse_definitely (parser))
10461     {
10462       cp_declarator *declarator;
10463
10464       /* Parse another optional declarator.  */
10465       declarator = cp_parser_conversion_declarator_opt (parser);
10466
10467       return cp_parser_make_indirect_declarator
10468         (code, class_type, cv_quals, declarator);
10469    }
10470
10471   return NULL;
10472 }
10473
10474 /* Parse an (optional) ctor-initializer.
10475
10476    ctor-initializer:
10477      : mem-initializer-list
10478
10479    Returns TRUE iff the ctor-initializer was actually present.  */
10480
10481 static bool
10482 cp_parser_ctor_initializer_opt (cp_parser* parser)
10483 {
10484   /* If the next token is not a `:', then there is no
10485      ctor-initializer.  */
10486   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10487     {
10488       /* Do default initialization of any bases and members.  */
10489       if (DECL_CONSTRUCTOR_P (current_function_decl))
10490         finish_mem_initializers (NULL_TREE);
10491
10492       return false;
10493     }
10494
10495   /* Consume the `:' token.  */
10496   cp_lexer_consume_token (parser->lexer);
10497   /* And the mem-initializer-list.  */
10498   cp_parser_mem_initializer_list (parser);
10499
10500   return true;
10501 }
10502
10503 /* Parse a mem-initializer-list.
10504
10505    mem-initializer-list:
10506      mem-initializer ... [opt]
10507      mem-initializer ... [opt] , mem-initializer-list  */
10508
10509 static void
10510 cp_parser_mem_initializer_list (cp_parser* parser)
10511 {
10512   tree mem_initializer_list = NULL_TREE;
10513   cp_token *token = cp_lexer_peek_token (parser->lexer);
10514
10515   /* Let the semantic analysis code know that we are starting the
10516      mem-initializer-list.  */
10517   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10518     error_at (token->location,
10519               "only constructors take member initializers");
10520
10521   /* Loop through the list.  */
10522   while (true)
10523     {
10524       tree mem_initializer;
10525
10526       token = cp_lexer_peek_token (parser->lexer);
10527       /* Parse the mem-initializer.  */
10528       mem_initializer = cp_parser_mem_initializer (parser);
10529       /* If the next token is a `...', we're expanding member initializers. */
10530       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10531         {
10532           /* Consume the `...'. */
10533           cp_lexer_consume_token (parser->lexer);
10534
10535           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10536              can be expanded but members cannot. */
10537           if (mem_initializer != error_mark_node
10538               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10539             {
10540               error_at (token->location,
10541                         "cannot expand initializer for member %<%D%>",
10542                         TREE_PURPOSE (mem_initializer));
10543               mem_initializer = error_mark_node;
10544             }
10545
10546           /* Construct the pack expansion type. */
10547           if (mem_initializer != error_mark_node)
10548             mem_initializer = make_pack_expansion (mem_initializer);
10549         }
10550       /* Add it to the list, unless it was erroneous.  */
10551       if (mem_initializer != error_mark_node)
10552         {
10553           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10554           mem_initializer_list = mem_initializer;
10555         }
10556       /* If the next token is not a `,', we're done.  */
10557       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10558         break;
10559       /* Consume the `,' token.  */
10560       cp_lexer_consume_token (parser->lexer);
10561     }
10562
10563   /* Perform semantic analysis.  */
10564   if (DECL_CONSTRUCTOR_P (current_function_decl))
10565     finish_mem_initializers (mem_initializer_list);
10566 }
10567
10568 /* Parse a mem-initializer.
10569
10570    mem-initializer:
10571      mem-initializer-id ( expression-list [opt] )
10572      mem-initializer-id braced-init-list
10573
10574    GNU extension:
10575
10576    mem-initializer:
10577      ( expression-list [opt] )
10578
10579    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10580    class) or FIELD_DECL (for a non-static data member) to initialize;
10581    the TREE_VALUE is the expression-list.  An empty initialization
10582    list is represented by void_list_node.  */
10583
10584 static tree
10585 cp_parser_mem_initializer (cp_parser* parser)
10586 {
10587   tree mem_initializer_id;
10588   tree expression_list;
10589   tree member;
10590   cp_token *token = cp_lexer_peek_token (parser->lexer);
10591
10592   /* Find out what is being initialized.  */
10593   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10594     {
10595       permerror (token->location,
10596                  "anachronistic old-style base class initializer");
10597       mem_initializer_id = NULL_TREE;
10598     }
10599   else
10600     {
10601       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10602       if (mem_initializer_id == error_mark_node)
10603         return mem_initializer_id;
10604     }
10605   member = expand_member_init (mem_initializer_id);
10606   if (member && !DECL_P (member))
10607     in_base_initializer = 1;
10608
10609   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10610     {
10611       bool expr_non_constant_p;
10612       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10613       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10614       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10615       expression_list = build_tree_list (NULL_TREE, expression_list);
10616     }
10617   else
10618     {
10619       VEC(tree,gc)* vec;
10620       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10621                                                      /*cast_p=*/false,
10622                                                      /*allow_expansion_p=*/true,
10623                                                      /*non_constant_p=*/NULL);
10624       if (vec == NULL)
10625         return error_mark_node;
10626       expression_list = build_tree_list_vec (vec);
10627       release_tree_vector (vec);
10628     }
10629
10630   if (expression_list == error_mark_node)
10631     return error_mark_node;
10632   if (!expression_list)
10633     expression_list = void_type_node;
10634
10635   in_base_initializer = 0;
10636
10637   return member ? build_tree_list (member, expression_list) : error_mark_node;
10638 }
10639
10640 /* Parse a mem-initializer-id.
10641
10642    mem-initializer-id:
10643      :: [opt] nested-name-specifier [opt] class-name
10644      identifier
10645
10646    Returns a TYPE indicating the class to be initializer for the first
10647    production.  Returns an IDENTIFIER_NODE indicating the data member
10648    to be initialized for the second production.  */
10649
10650 static tree
10651 cp_parser_mem_initializer_id (cp_parser* parser)
10652 {
10653   bool global_scope_p;
10654   bool nested_name_specifier_p;
10655   bool template_p = false;
10656   tree id;
10657
10658   cp_token *token = cp_lexer_peek_token (parser->lexer);
10659
10660   /* `typename' is not allowed in this context ([temp.res]).  */
10661   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10662     {
10663       error_at (token->location, 
10664                 "keyword %<typename%> not allowed in this context (a qualified "
10665                 "member initializer is implicitly a type)");
10666       cp_lexer_consume_token (parser->lexer);
10667     }
10668   /* Look for the optional `::' operator.  */
10669   global_scope_p
10670     = (cp_parser_global_scope_opt (parser,
10671                                    /*current_scope_valid_p=*/false)
10672        != NULL_TREE);
10673   /* Look for the optional nested-name-specifier.  The simplest way to
10674      implement:
10675
10676        [temp.res]
10677
10678        The keyword `typename' is not permitted in a base-specifier or
10679        mem-initializer; in these contexts a qualified name that
10680        depends on a template-parameter is implicitly assumed to be a
10681        type name.
10682
10683      is to assume that we have seen the `typename' keyword at this
10684      point.  */
10685   nested_name_specifier_p
10686     = (cp_parser_nested_name_specifier_opt (parser,
10687                                             /*typename_keyword_p=*/true,
10688                                             /*check_dependency_p=*/true,
10689                                             /*type_p=*/true,
10690                                             /*is_declaration=*/true)
10691        != NULL_TREE);
10692   if (nested_name_specifier_p)
10693     template_p = cp_parser_optional_template_keyword (parser);
10694   /* If there is a `::' operator or a nested-name-specifier, then we
10695      are definitely looking for a class-name.  */
10696   if (global_scope_p || nested_name_specifier_p)
10697     return cp_parser_class_name (parser,
10698                                  /*typename_keyword_p=*/true,
10699                                  /*template_keyword_p=*/template_p,
10700                                  typename_type,
10701                                  /*check_dependency_p=*/true,
10702                                  /*class_head_p=*/false,
10703                                  /*is_declaration=*/true);
10704   /* Otherwise, we could also be looking for an ordinary identifier.  */
10705   cp_parser_parse_tentatively (parser);
10706   /* Try a class-name.  */
10707   id = cp_parser_class_name (parser,
10708                              /*typename_keyword_p=*/true,
10709                              /*template_keyword_p=*/false,
10710                              none_type,
10711                              /*check_dependency_p=*/true,
10712                              /*class_head_p=*/false,
10713                              /*is_declaration=*/true);
10714   /* If we found one, we're done.  */
10715   if (cp_parser_parse_definitely (parser))
10716     return id;
10717   /* Otherwise, look for an ordinary identifier.  */
10718   return cp_parser_identifier (parser);
10719 }
10720
10721 /* Overloading [gram.over] */
10722
10723 /* Parse an operator-function-id.
10724
10725    operator-function-id:
10726      operator operator
10727
10728    Returns an IDENTIFIER_NODE for the operator which is a
10729    human-readable spelling of the identifier, e.g., `operator +'.  */
10730
10731 static tree
10732 cp_parser_operator_function_id (cp_parser* parser)
10733 {
10734   /* Look for the `operator' keyword.  */
10735   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10736     return error_mark_node;
10737   /* And then the name of the operator itself.  */
10738   return cp_parser_operator (parser);
10739 }
10740
10741 /* Parse an operator.
10742
10743    operator:
10744      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10745      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10746      || ++ -- , ->* -> () []
10747
10748    GNU Extensions:
10749
10750    operator:
10751      <? >? <?= >?=
10752
10753    Returns an IDENTIFIER_NODE for the operator which is a
10754    human-readable spelling of the identifier, e.g., `operator +'.  */
10755
10756 static tree
10757 cp_parser_operator (cp_parser* parser)
10758 {
10759   tree id = NULL_TREE;
10760   cp_token *token;
10761
10762   /* Peek at the next token.  */
10763   token = cp_lexer_peek_token (parser->lexer);
10764   /* Figure out which operator we have.  */
10765   switch (token->type)
10766     {
10767     case CPP_KEYWORD:
10768       {
10769         enum tree_code op;
10770
10771         /* The keyword should be either `new' or `delete'.  */
10772         if (token->keyword == RID_NEW)
10773           op = NEW_EXPR;
10774         else if (token->keyword == RID_DELETE)
10775           op = DELETE_EXPR;
10776         else
10777           break;
10778
10779         /* Consume the `new' or `delete' token.  */
10780         cp_lexer_consume_token (parser->lexer);
10781
10782         /* Peek at the next token.  */
10783         token = cp_lexer_peek_token (parser->lexer);
10784         /* If it's a `[' token then this is the array variant of the
10785            operator.  */
10786         if (token->type == CPP_OPEN_SQUARE)
10787           {
10788             /* Consume the `[' token.  */
10789             cp_lexer_consume_token (parser->lexer);
10790             /* Look for the `]' token.  */
10791             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10792             id = ansi_opname (op == NEW_EXPR
10793                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10794           }
10795         /* Otherwise, we have the non-array variant.  */
10796         else
10797           id = ansi_opname (op);
10798
10799         return id;
10800       }
10801
10802     case CPP_PLUS:
10803       id = ansi_opname (PLUS_EXPR);
10804       break;
10805
10806     case CPP_MINUS:
10807       id = ansi_opname (MINUS_EXPR);
10808       break;
10809
10810     case CPP_MULT:
10811       id = ansi_opname (MULT_EXPR);
10812       break;
10813
10814     case CPP_DIV:
10815       id = ansi_opname (TRUNC_DIV_EXPR);
10816       break;
10817
10818     case CPP_MOD:
10819       id = ansi_opname (TRUNC_MOD_EXPR);
10820       break;
10821
10822     case CPP_XOR:
10823       id = ansi_opname (BIT_XOR_EXPR);
10824       break;
10825
10826     case CPP_AND:
10827       id = ansi_opname (BIT_AND_EXPR);
10828       break;
10829
10830     case CPP_OR:
10831       id = ansi_opname (BIT_IOR_EXPR);
10832       break;
10833
10834     case CPP_COMPL:
10835       id = ansi_opname (BIT_NOT_EXPR);
10836       break;
10837
10838     case CPP_NOT:
10839       id = ansi_opname (TRUTH_NOT_EXPR);
10840       break;
10841
10842     case CPP_EQ:
10843       id = ansi_assopname (NOP_EXPR);
10844       break;
10845
10846     case CPP_LESS:
10847       id = ansi_opname (LT_EXPR);
10848       break;
10849
10850     case CPP_GREATER:
10851       id = ansi_opname (GT_EXPR);
10852       break;
10853
10854     case CPP_PLUS_EQ:
10855       id = ansi_assopname (PLUS_EXPR);
10856       break;
10857
10858     case CPP_MINUS_EQ:
10859       id = ansi_assopname (MINUS_EXPR);
10860       break;
10861
10862     case CPP_MULT_EQ:
10863       id = ansi_assopname (MULT_EXPR);
10864       break;
10865
10866     case CPP_DIV_EQ:
10867       id = ansi_assopname (TRUNC_DIV_EXPR);
10868       break;
10869
10870     case CPP_MOD_EQ:
10871       id = ansi_assopname (TRUNC_MOD_EXPR);
10872       break;
10873
10874     case CPP_XOR_EQ:
10875       id = ansi_assopname (BIT_XOR_EXPR);
10876       break;
10877
10878     case CPP_AND_EQ:
10879       id = ansi_assopname (BIT_AND_EXPR);
10880       break;
10881
10882     case CPP_OR_EQ:
10883       id = ansi_assopname (BIT_IOR_EXPR);
10884       break;
10885
10886     case CPP_LSHIFT:
10887       id = ansi_opname (LSHIFT_EXPR);
10888       break;
10889
10890     case CPP_RSHIFT:
10891       id = ansi_opname (RSHIFT_EXPR);
10892       break;
10893
10894     case CPP_LSHIFT_EQ:
10895       id = ansi_assopname (LSHIFT_EXPR);
10896       break;
10897
10898     case CPP_RSHIFT_EQ:
10899       id = ansi_assopname (RSHIFT_EXPR);
10900       break;
10901
10902     case CPP_EQ_EQ:
10903       id = ansi_opname (EQ_EXPR);
10904       break;
10905
10906     case CPP_NOT_EQ:
10907       id = ansi_opname (NE_EXPR);
10908       break;
10909
10910     case CPP_LESS_EQ:
10911       id = ansi_opname (LE_EXPR);
10912       break;
10913
10914     case CPP_GREATER_EQ:
10915       id = ansi_opname (GE_EXPR);
10916       break;
10917
10918     case CPP_AND_AND:
10919       id = ansi_opname (TRUTH_ANDIF_EXPR);
10920       break;
10921
10922     case CPP_OR_OR:
10923       id = ansi_opname (TRUTH_ORIF_EXPR);
10924       break;
10925
10926     case CPP_PLUS_PLUS:
10927       id = ansi_opname (POSTINCREMENT_EXPR);
10928       break;
10929
10930     case CPP_MINUS_MINUS:
10931       id = ansi_opname (PREDECREMENT_EXPR);
10932       break;
10933
10934     case CPP_COMMA:
10935       id = ansi_opname (COMPOUND_EXPR);
10936       break;
10937
10938     case CPP_DEREF_STAR:
10939       id = ansi_opname (MEMBER_REF);
10940       break;
10941
10942     case CPP_DEREF:
10943       id = ansi_opname (COMPONENT_REF);
10944       break;
10945
10946     case CPP_OPEN_PAREN:
10947       /* Consume the `('.  */
10948       cp_lexer_consume_token (parser->lexer);
10949       /* Look for the matching `)'.  */
10950       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10951       return ansi_opname (CALL_EXPR);
10952
10953     case CPP_OPEN_SQUARE:
10954       /* Consume the `['.  */
10955       cp_lexer_consume_token (parser->lexer);
10956       /* Look for the matching `]'.  */
10957       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10958       return ansi_opname (ARRAY_REF);
10959
10960     default:
10961       /* Anything else is an error.  */
10962       break;
10963     }
10964
10965   /* If we have selected an identifier, we need to consume the
10966      operator token.  */
10967   if (id)
10968     cp_lexer_consume_token (parser->lexer);
10969   /* Otherwise, no valid operator name was present.  */
10970   else
10971     {
10972       cp_parser_error (parser, "expected operator");
10973       id = error_mark_node;
10974     }
10975
10976   return id;
10977 }
10978
10979 /* Parse a template-declaration.
10980
10981    template-declaration:
10982      export [opt] template < template-parameter-list > declaration
10983
10984    If MEMBER_P is TRUE, this template-declaration occurs within a
10985    class-specifier.
10986
10987    The grammar rule given by the standard isn't correct.  What
10988    is really meant is:
10989
10990    template-declaration:
10991      export [opt] template-parameter-list-seq
10992        decl-specifier-seq [opt] init-declarator [opt] ;
10993      export [opt] template-parameter-list-seq
10994        function-definition
10995
10996    template-parameter-list-seq:
10997      template-parameter-list-seq [opt]
10998      template < template-parameter-list >  */
10999
11000 static void
11001 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11002 {
11003   /* Check for `export'.  */
11004   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11005     {
11006       /* Consume the `export' token.  */
11007       cp_lexer_consume_token (parser->lexer);
11008       /* Warn that we do not support `export'.  */
11009       warning (0, "keyword %<export%> not implemented, and will be ignored");
11010     }
11011
11012   cp_parser_template_declaration_after_export (parser, member_p);
11013 }
11014
11015 /* Parse a template-parameter-list.
11016
11017    template-parameter-list:
11018      template-parameter
11019      template-parameter-list , template-parameter
11020
11021    Returns a TREE_LIST.  Each node represents a template parameter.
11022    The nodes are connected via their TREE_CHAINs.  */
11023
11024 static tree
11025 cp_parser_template_parameter_list (cp_parser* parser)
11026 {
11027   tree parameter_list = NULL_TREE;
11028
11029   begin_template_parm_list ();
11030
11031   /* The loop below parses the template parms.  We first need to know
11032      the total number of template parms to be able to compute proper
11033      canonical types of each dependent type. So after the loop, when
11034      we know the total number of template parms,
11035      end_template_parm_list computes the proper canonical types and
11036      fixes up the dependent types accordingly.  */
11037   while (true)
11038     {
11039       tree parameter;
11040       bool is_non_type;
11041       bool is_parameter_pack;
11042       location_t parm_loc;
11043
11044       /* Parse the template-parameter.  */
11045       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11046       parameter = cp_parser_template_parameter (parser, 
11047                                                 &is_non_type,
11048                                                 &is_parameter_pack);
11049       /* Add it to the list.  */
11050       if (parameter != error_mark_node)
11051         parameter_list = process_template_parm (parameter_list,
11052                                                 parm_loc,
11053                                                 parameter,
11054                                                 is_non_type,
11055                                                 is_parameter_pack,
11056                                                 0);
11057       else
11058        {
11059          tree err_parm = build_tree_list (parameter, parameter);
11060          parameter_list = chainon (parameter_list, err_parm);
11061        }
11062
11063       /* If the next token is not a `,', we're done.  */
11064       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11065         break;
11066       /* Otherwise, consume the `,' token.  */
11067       cp_lexer_consume_token (parser->lexer);
11068     }
11069
11070   return end_template_parm_list (parameter_list);
11071 }
11072
11073 /* Parse a template-parameter.
11074
11075    template-parameter:
11076      type-parameter
11077      parameter-declaration
11078
11079    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11080    the parameter.  The TREE_PURPOSE is the default value, if any.
11081    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11082    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11083    set to true iff this parameter is a parameter pack. */
11084
11085 static tree
11086 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11087                               bool *is_parameter_pack)
11088 {
11089   cp_token *token;
11090   cp_parameter_declarator *parameter_declarator;
11091   cp_declarator *id_declarator;
11092   tree parm;
11093
11094   /* Assume it is a type parameter or a template parameter.  */
11095   *is_non_type = false;
11096   /* Assume it not a parameter pack. */
11097   *is_parameter_pack = false;
11098   /* Peek at the next token.  */
11099   token = cp_lexer_peek_token (parser->lexer);
11100   /* If it is `class' or `template', we have a type-parameter.  */
11101   if (token->keyword == RID_TEMPLATE)
11102     return cp_parser_type_parameter (parser, is_parameter_pack);
11103   /* If it is `class' or `typename' we do not know yet whether it is a
11104      type parameter or a non-type parameter.  Consider:
11105
11106        template <typename T, typename T::X X> ...
11107
11108      or:
11109
11110        template <class C, class D*> ...
11111
11112      Here, the first parameter is a type parameter, and the second is
11113      a non-type parameter.  We can tell by looking at the token after
11114      the identifier -- if it is a `,', `=', or `>' then we have a type
11115      parameter.  */
11116   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11117     {
11118       /* Peek at the token after `class' or `typename'.  */
11119       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11120       /* If it's an ellipsis, we have a template type parameter
11121          pack. */
11122       if (token->type == CPP_ELLIPSIS)
11123         return cp_parser_type_parameter (parser, is_parameter_pack);
11124       /* If it's an identifier, skip it.  */
11125       if (token->type == CPP_NAME)
11126         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11127       /* Now, see if the token looks like the end of a template
11128          parameter.  */
11129       if (token->type == CPP_COMMA
11130           || token->type == CPP_EQ
11131           || token->type == CPP_GREATER)
11132         return cp_parser_type_parameter (parser, is_parameter_pack);
11133     }
11134
11135   /* Otherwise, it is a non-type parameter.
11136
11137      [temp.param]
11138
11139      When parsing a default template-argument for a non-type
11140      template-parameter, the first non-nested `>' is taken as the end
11141      of the template parameter-list rather than a greater-than
11142      operator.  */
11143   *is_non_type = true;
11144   parameter_declarator
11145      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11146                                         /*parenthesized_p=*/NULL);
11147
11148   /* If the parameter declaration is marked as a parameter pack, set
11149      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11150      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11151      grokdeclarator. */
11152   if (parameter_declarator
11153       && parameter_declarator->declarator
11154       && parameter_declarator->declarator->parameter_pack_p)
11155     {
11156       *is_parameter_pack = true;
11157       parameter_declarator->declarator->parameter_pack_p = false;
11158     }
11159
11160   /* If the next token is an ellipsis, and we don't already have it
11161      marked as a parameter pack, then we have a parameter pack (that
11162      has no declarator).  */
11163   if (!*is_parameter_pack
11164       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11165       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11166     {
11167       /* Consume the `...'.  */
11168       cp_lexer_consume_token (parser->lexer);
11169       maybe_warn_variadic_templates ();
11170       
11171       *is_parameter_pack = true;
11172     }
11173   /* We might end up with a pack expansion as the type of the non-type
11174      template parameter, in which case this is a non-type template
11175      parameter pack.  */
11176   else if (parameter_declarator
11177            && parameter_declarator->decl_specifiers.type
11178            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11179     {
11180       *is_parameter_pack = true;
11181       parameter_declarator->decl_specifiers.type = 
11182         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11183     }
11184
11185   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11186     {
11187       /* Parameter packs cannot have default arguments.  However, a
11188          user may try to do so, so we'll parse them and give an
11189          appropriate diagnostic here.  */
11190
11191       /* Consume the `='.  */
11192       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11193       cp_lexer_consume_token (parser->lexer);
11194       
11195       /* Find the name of the parameter pack.  */     
11196       id_declarator = parameter_declarator->declarator;
11197       while (id_declarator && id_declarator->kind != cdk_id)
11198         id_declarator = id_declarator->declarator;
11199       
11200       if (id_declarator && id_declarator->kind == cdk_id)
11201         error_at (start_token->location,
11202                   "template parameter pack %qD cannot have a default argument",
11203                   id_declarator->u.id.unqualified_name);
11204       else
11205         error_at (start_token->location,
11206                   "template parameter pack cannot have a default argument");
11207       
11208       /* Parse the default argument, but throw away the result.  */
11209       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11210     }
11211
11212   parm = grokdeclarator (parameter_declarator->declarator,
11213                          &parameter_declarator->decl_specifiers,
11214                          TPARM, /*initialized=*/0,
11215                          /*attrlist=*/NULL);
11216   if (parm == error_mark_node)
11217     return error_mark_node;
11218
11219   return build_tree_list (parameter_declarator->default_argument, parm);
11220 }
11221
11222 /* Parse a type-parameter.
11223
11224    type-parameter:
11225      class identifier [opt]
11226      class identifier [opt] = type-id
11227      typename identifier [opt]
11228      typename identifier [opt] = type-id
11229      template < template-parameter-list > class identifier [opt]
11230      template < template-parameter-list > class identifier [opt]
11231        = id-expression
11232
11233    GNU Extension (variadic templates):
11234
11235    type-parameter:
11236      class ... identifier [opt]
11237      typename ... identifier [opt]
11238
11239    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11240    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11241    the declaration of the parameter.
11242
11243    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11244
11245 static tree
11246 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11247 {
11248   cp_token *token;
11249   tree parameter;
11250
11251   /* Look for a keyword to tell us what kind of parameter this is.  */
11252   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11253   if (!token)
11254     return error_mark_node;
11255
11256   switch (token->keyword)
11257     {
11258     case RID_CLASS:
11259     case RID_TYPENAME:
11260       {
11261         tree identifier;
11262         tree default_argument;
11263
11264         /* If the next token is an ellipsis, we have a template
11265            argument pack. */
11266         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11267           {
11268             /* Consume the `...' token. */
11269             cp_lexer_consume_token (parser->lexer);
11270             maybe_warn_variadic_templates ();
11271
11272             *is_parameter_pack = true;
11273           }
11274
11275         /* If the next token is an identifier, then it names the
11276            parameter.  */
11277         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11278           identifier = cp_parser_identifier (parser);
11279         else
11280           identifier = NULL_TREE;
11281
11282         /* Create the parameter.  */
11283         parameter = finish_template_type_parm (class_type_node, identifier);
11284
11285         /* If the next token is an `=', we have a default argument.  */
11286         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11287           {
11288             /* Consume the `=' token.  */
11289             cp_lexer_consume_token (parser->lexer);
11290             /* Parse the default-argument.  */
11291             push_deferring_access_checks (dk_no_deferred);
11292             default_argument = cp_parser_type_id (parser);
11293
11294             /* Template parameter packs cannot have default
11295                arguments. */
11296             if (*is_parameter_pack)
11297               {
11298                 if (identifier)
11299                   error_at (token->location,
11300                             "template parameter pack %qD cannot have a "
11301                             "default argument", identifier);
11302                 else
11303                   error_at (token->location,
11304                             "template parameter packs cannot have "
11305                             "default arguments");
11306                 default_argument = NULL_TREE;
11307               }
11308             pop_deferring_access_checks ();
11309           }
11310         else
11311           default_argument = NULL_TREE;
11312
11313         /* Create the combined representation of the parameter and the
11314            default argument.  */
11315         parameter = build_tree_list (default_argument, parameter);
11316       }
11317       break;
11318
11319     case RID_TEMPLATE:
11320       {
11321         tree identifier;
11322         tree default_argument;
11323
11324         /* Look for the `<'.  */
11325         cp_parser_require (parser, CPP_LESS, RT_LESS);
11326         /* Parse the template-parameter-list.  */
11327         cp_parser_template_parameter_list (parser);
11328         /* Look for the `>'.  */
11329         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11330         /* Look for the `class' keyword.  */
11331         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11332         /* If the next token is an ellipsis, we have a template
11333            argument pack. */
11334         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11335           {
11336             /* Consume the `...' token. */
11337             cp_lexer_consume_token (parser->lexer);
11338             maybe_warn_variadic_templates ();
11339
11340             *is_parameter_pack = true;
11341           }
11342         /* If the next token is an `=', then there is a
11343            default-argument.  If the next token is a `>', we are at
11344            the end of the parameter-list.  If the next token is a `,',
11345            then we are at the end of this parameter.  */
11346         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11347             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11348             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11349           {
11350             identifier = cp_parser_identifier (parser);
11351             /* Treat invalid names as if the parameter were nameless.  */
11352             if (identifier == error_mark_node)
11353               identifier = NULL_TREE;
11354           }
11355         else
11356           identifier = NULL_TREE;
11357
11358         /* Create the template parameter.  */
11359         parameter = finish_template_template_parm (class_type_node,
11360                                                    identifier);
11361
11362         /* If the next token is an `=', then there is a
11363            default-argument.  */
11364         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11365           {
11366             bool is_template;
11367
11368             /* Consume the `='.  */
11369             cp_lexer_consume_token (parser->lexer);
11370             /* Parse the id-expression.  */
11371             push_deferring_access_checks (dk_no_deferred);
11372             /* save token before parsing the id-expression, for error
11373                reporting */
11374             token = cp_lexer_peek_token (parser->lexer);
11375             default_argument
11376               = cp_parser_id_expression (parser,
11377                                          /*template_keyword_p=*/false,
11378                                          /*check_dependency_p=*/true,
11379                                          /*template_p=*/&is_template,
11380                                          /*declarator_p=*/false,
11381                                          /*optional_p=*/false);
11382             if (TREE_CODE (default_argument) == TYPE_DECL)
11383               /* If the id-expression was a template-id that refers to
11384                  a template-class, we already have the declaration here,
11385                  so no further lookup is needed.  */
11386                  ;
11387             else
11388               /* Look up the name.  */
11389               default_argument
11390                 = cp_parser_lookup_name (parser, default_argument,
11391                                          none_type,
11392                                          /*is_template=*/is_template,
11393                                          /*is_namespace=*/false,
11394                                          /*check_dependency=*/true,
11395                                          /*ambiguous_decls=*/NULL,
11396                                          token->location);
11397             /* See if the default argument is valid.  */
11398             default_argument
11399               = check_template_template_default_arg (default_argument);
11400
11401             /* Template parameter packs cannot have default
11402                arguments. */
11403             if (*is_parameter_pack)
11404               {
11405                 if (identifier)
11406                   error_at (token->location,
11407                             "template parameter pack %qD cannot "
11408                             "have a default argument",
11409                             identifier);
11410                 else
11411                   error_at (token->location, "template parameter packs cannot "
11412                             "have default arguments");
11413                 default_argument = NULL_TREE;
11414               }
11415             pop_deferring_access_checks ();
11416           }
11417         else
11418           default_argument = NULL_TREE;
11419
11420         /* Create the combined representation of the parameter and the
11421            default argument.  */
11422         parameter = build_tree_list (default_argument, parameter);
11423       }
11424       break;
11425
11426     default:
11427       gcc_unreachable ();
11428       break;
11429     }
11430
11431   return parameter;
11432 }
11433
11434 /* Parse a template-id.
11435
11436    template-id:
11437      template-name < template-argument-list [opt] >
11438
11439    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11440    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11441    returned.  Otherwise, if the template-name names a function, or set
11442    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11443    names a class, returns a TYPE_DECL for the specialization.
11444
11445    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11446    uninstantiated templates.  */
11447
11448 static tree
11449 cp_parser_template_id (cp_parser *parser,
11450                        bool template_keyword_p,
11451                        bool check_dependency_p,
11452                        bool is_declaration)
11453 {
11454   int i;
11455   tree templ;
11456   tree arguments;
11457   tree template_id;
11458   cp_token_position start_of_id = 0;
11459   deferred_access_check *chk;
11460   VEC (deferred_access_check,gc) *access_check;
11461   cp_token *next_token = NULL, *next_token_2 = NULL;
11462   bool is_identifier;
11463
11464   /* If the next token corresponds to a template-id, there is no need
11465      to reparse it.  */
11466   next_token = cp_lexer_peek_token (parser->lexer);
11467   if (next_token->type == CPP_TEMPLATE_ID)
11468     {
11469       struct tree_check *check_value;
11470
11471       /* Get the stored value.  */
11472       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11473       /* Perform any access checks that were deferred.  */
11474       access_check = check_value->checks;
11475       if (access_check)
11476         {
11477           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11478             perform_or_defer_access_check (chk->binfo,
11479                                            chk->decl,
11480                                            chk->diag_decl);
11481         }
11482       /* Return the stored value.  */
11483       return check_value->value;
11484     }
11485
11486   /* Avoid performing name lookup if there is no possibility of
11487      finding a template-id.  */
11488   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11489       || (next_token->type == CPP_NAME
11490           && !cp_parser_nth_token_starts_template_argument_list_p
11491                (parser, 2)))
11492     {
11493       cp_parser_error (parser, "expected template-id");
11494       return error_mark_node;
11495     }
11496
11497   /* Remember where the template-id starts.  */
11498   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11499     start_of_id = cp_lexer_token_position (parser->lexer, false);
11500
11501   push_deferring_access_checks (dk_deferred);
11502
11503   /* Parse the template-name.  */
11504   is_identifier = false;
11505   templ = cp_parser_template_name (parser, template_keyword_p,
11506                                    check_dependency_p,
11507                                    is_declaration,
11508                                    &is_identifier);
11509   if (templ == error_mark_node || is_identifier)
11510     {
11511       pop_deferring_access_checks ();
11512       return templ;
11513     }
11514
11515   /* If we find the sequence `[:' after a template-name, it's probably
11516      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11517      parse correctly the argument list.  */
11518   next_token = cp_lexer_peek_token (parser->lexer);
11519   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11520   if (next_token->type == CPP_OPEN_SQUARE
11521       && next_token->flags & DIGRAPH
11522       && next_token_2->type == CPP_COLON
11523       && !(next_token_2->flags & PREV_WHITE))
11524     {
11525       cp_parser_parse_tentatively (parser);
11526       /* Change `:' into `::'.  */
11527       next_token_2->type = CPP_SCOPE;
11528       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11529          CPP_LESS.  */
11530       cp_lexer_consume_token (parser->lexer);
11531
11532       /* Parse the arguments.  */
11533       arguments = cp_parser_enclosed_template_argument_list (parser);
11534       if (!cp_parser_parse_definitely (parser))
11535         {
11536           /* If we couldn't parse an argument list, then we revert our changes
11537              and return simply an error. Maybe this is not a template-id
11538              after all.  */
11539           next_token_2->type = CPP_COLON;
11540           cp_parser_error (parser, "expected %<<%>");
11541           pop_deferring_access_checks ();
11542           return error_mark_node;
11543         }
11544       /* Otherwise, emit an error about the invalid digraph, but continue
11545          parsing because we got our argument list.  */
11546       if (permerror (next_token->location,
11547                      "%<<::%> cannot begin a template-argument list"))
11548         {
11549           static bool hint = false;
11550           inform (next_token->location,
11551                   "%<<:%> is an alternate spelling for %<[%>."
11552                   " Insert whitespace between %<<%> and %<::%>");
11553           if (!hint && !flag_permissive)
11554             {
11555               inform (next_token->location, "(if you use %<-fpermissive%>"
11556                       " G++ will accept your code)");
11557               hint = true;
11558             }
11559         }
11560     }
11561   else
11562     {
11563       /* Look for the `<' that starts the template-argument-list.  */
11564       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11565         {
11566           pop_deferring_access_checks ();
11567           return error_mark_node;
11568         }
11569       /* Parse the arguments.  */
11570       arguments = cp_parser_enclosed_template_argument_list (parser);
11571     }
11572
11573   /* Build a representation of the specialization.  */
11574   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11575     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11576   else if (DECL_CLASS_TEMPLATE_P (templ)
11577            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11578     {
11579       bool entering_scope;
11580       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11581          template (rather than some instantiation thereof) only if
11582          is not nested within some other construct.  For example, in
11583          "template <typename T> void f(T) { A<T>::", A<T> is just an
11584          instantiation of A.  */
11585       entering_scope = (template_parm_scope_p ()
11586                         && cp_lexer_next_token_is (parser->lexer,
11587                                                    CPP_SCOPE));
11588       template_id
11589         = finish_template_type (templ, arguments, entering_scope);
11590     }
11591   else
11592     {
11593       /* If it's not a class-template or a template-template, it should be
11594          a function-template.  */
11595       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11596                    || TREE_CODE (templ) == OVERLOAD
11597                    || BASELINK_P (templ)));
11598
11599       template_id = lookup_template_function (templ, arguments);
11600     }
11601
11602   /* If parsing tentatively, replace the sequence of tokens that makes
11603      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11604      should we re-parse the token stream, we will not have to repeat
11605      the effort required to do the parse, nor will we issue duplicate
11606      error messages about problems during instantiation of the
11607      template.  */
11608   if (start_of_id)
11609     {
11610       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11611
11612       /* Reset the contents of the START_OF_ID token.  */
11613       token->type = CPP_TEMPLATE_ID;
11614       /* Retrieve any deferred checks.  Do not pop this access checks yet
11615          so the memory will not be reclaimed during token replacing below.  */
11616       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11617       token->u.tree_check_value->value = template_id;
11618       token->u.tree_check_value->checks = get_deferred_access_checks ();
11619       token->keyword = RID_MAX;
11620
11621       /* Purge all subsequent tokens.  */
11622       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11623
11624       /* ??? Can we actually assume that, if template_id ==
11625          error_mark_node, we will have issued a diagnostic to the
11626          user, as opposed to simply marking the tentative parse as
11627          failed?  */
11628       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11629         error_at (token->location, "parse error in template argument list");
11630     }
11631
11632   pop_deferring_access_checks ();
11633   return template_id;
11634 }
11635
11636 /* Parse a template-name.
11637
11638    template-name:
11639      identifier
11640
11641    The standard should actually say:
11642
11643    template-name:
11644      identifier
11645      operator-function-id
11646
11647    A defect report has been filed about this issue.
11648
11649    A conversion-function-id cannot be a template name because they cannot
11650    be part of a template-id. In fact, looking at this code:
11651
11652    a.operator K<int>()
11653
11654    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11655    It is impossible to call a templated conversion-function-id with an
11656    explicit argument list, since the only allowed template parameter is
11657    the type to which it is converting.
11658
11659    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11660    `template' keyword, in a construction like:
11661
11662      T::template f<3>()
11663
11664    In that case `f' is taken to be a template-name, even though there
11665    is no way of knowing for sure.
11666
11667    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11668    name refers to a set of overloaded functions, at least one of which
11669    is a template, or an IDENTIFIER_NODE with the name of the template,
11670    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11671    names are looked up inside uninstantiated templates.  */
11672
11673 static tree
11674 cp_parser_template_name (cp_parser* parser,
11675                          bool template_keyword_p,
11676                          bool check_dependency_p,
11677                          bool is_declaration,
11678                          bool *is_identifier)
11679 {
11680   tree identifier;
11681   tree decl;
11682   tree fns;
11683   cp_token *token = cp_lexer_peek_token (parser->lexer);
11684
11685   /* If the next token is `operator', then we have either an
11686      operator-function-id or a conversion-function-id.  */
11687   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11688     {
11689       /* We don't know whether we're looking at an
11690          operator-function-id or a conversion-function-id.  */
11691       cp_parser_parse_tentatively (parser);
11692       /* Try an operator-function-id.  */
11693       identifier = cp_parser_operator_function_id (parser);
11694       /* If that didn't work, try a conversion-function-id.  */
11695       if (!cp_parser_parse_definitely (parser))
11696         {
11697           cp_parser_error (parser, "expected template-name");
11698           return error_mark_node;
11699         }
11700     }
11701   /* Look for the identifier.  */
11702   else
11703     identifier = cp_parser_identifier (parser);
11704
11705   /* If we didn't find an identifier, we don't have a template-id.  */
11706   if (identifier == error_mark_node)
11707     return error_mark_node;
11708
11709   /* If the name immediately followed the `template' keyword, then it
11710      is a template-name.  However, if the next token is not `<', then
11711      we do not treat it as a template-name, since it is not being used
11712      as part of a template-id.  This enables us to handle constructs
11713      like:
11714
11715        template <typename T> struct S { S(); };
11716        template <typename T> S<T>::S();
11717
11718      correctly.  We would treat `S' as a template -- if it were `S<T>'
11719      -- but we do not if there is no `<'.  */
11720
11721   if (processing_template_decl
11722       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11723     {
11724       /* In a declaration, in a dependent context, we pretend that the
11725          "template" keyword was present in order to improve error
11726          recovery.  For example, given:
11727
11728            template <typename T> void f(T::X<int>);
11729
11730          we want to treat "X<int>" as a template-id.  */
11731       if (is_declaration
11732           && !template_keyword_p
11733           && parser->scope && TYPE_P (parser->scope)
11734           && check_dependency_p
11735           && dependent_scope_p (parser->scope)
11736           /* Do not do this for dtors (or ctors), since they never
11737              need the template keyword before their name.  */
11738           && !constructor_name_p (identifier, parser->scope))
11739         {
11740           cp_token_position start = 0;
11741
11742           /* Explain what went wrong.  */
11743           error_at (token->location, "non-template %qD used as template",
11744                     identifier);
11745           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11746                   parser->scope, identifier);
11747           /* If parsing tentatively, find the location of the "<" token.  */
11748           if (cp_parser_simulate_error (parser))
11749             start = cp_lexer_token_position (parser->lexer, true);
11750           /* Parse the template arguments so that we can issue error
11751              messages about them.  */
11752           cp_lexer_consume_token (parser->lexer);
11753           cp_parser_enclosed_template_argument_list (parser);
11754           /* Skip tokens until we find a good place from which to
11755              continue parsing.  */
11756           cp_parser_skip_to_closing_parenthesis (parser,
11757                                                  /*recovering=*/true,
11758                                                  /*or_comma=*/true,
11759                                                  /*consume_paren=*/false);
11760           /* If parsing tentatively, permanently remove the
11761              template argument list.  That will prevent duplicate
11762              error messages from being issued about the missing
11763              "template" keyword.  */
11764           if (start)
11765             cp_lexer_purge_tokens_after (parser->lexer, start);
11766           if (is_identifier)
11767             *is_identifier = true;
11768           return identifier;
11769         }
11770
11771       /* If the "template" keyword is present, then there is generally
11772          no point in doing name-lookup, so we just return IDENTIFIER.
11773          But, if the qualifying scope is non-dependent then we can
11774          (and must) do name-lookup normally.  */
11775       if (template_keyword_p
11776           && (!parser->scope
11777               || (TYPE_P (parser->scope)
11778                   && dependent_type_p (parser->scope))))
11779         return identifier;
11780     }
11781
11782   /* Look up the name.  */
11783   decl = cp_parser_lookup_name (parser, identifier,
11784                                 none_type,
11785                                 /*is_template=*/true,
11786                                 /*is_namespace=*/false,
11787                                 check_dependency_p,
11788                                 /*ambiguous_decls=*/NULL,
11789                                 token->location);
11790
11791   /* If DECL is a template, then the name was a template-name.  */
11792   if (TREE_CODE (decl) == TEMPLATE_DECL)
11793     ;
11794   else
11795     {
11796       tree fn = NULL_TREE;
11797
11798       /* The standard does not explicitly indicate whether a name that
11799          names a set of overloaded declarations, some of which are
11800          templates, is a template-name.  However, such a name should
11801          be a template-name; otherwise, there is no way to form a
11802          template-id for the overloaded templates.  */
11803       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11804       if (TREE_CODE (fns) == OVERLOAD)
11805         for (fn = fns; fn; fn = OVL_NEXT (fn))
11806           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11807             break;
11808
11809       if (!fn)
11810         {
11811           /* The name does not name a template.  */
11812           cp_parser_error (parser, "expected template-name");
11813           return error_mark_node;
11814         }
11815     }
11816
11817   /* If DECL is dependent, and refers to a function, then just return
11818      its name; we will look it up again during template instantiation.  */
11819   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11820     {
11821       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11822       if (TYPE_P (scope) && dependent_type_p (scope))
11823         return identifier;
11824     }
11825
11826   return decl;
11827 }
11828
11829 /* Parse a template-argument-list.
11830
11831    template-argument-list:
11832      template-argument ... [opt]
11833      template-argument-list , template-argument ... [opt]
11834
11835    Returns a TREE_VEC containing the arguments.  */
11836
11837 static tree
11838 cp_parser_template_argument_list (cp_parser* parser)
11839 {
11840   tree fixed_args[10];
11841   unsigned n_args = 0;
11842   unsigned alloced = 10;
11843   tree *arg_ary = fixed_args;
11844   tree vec;
11845   bool saved_in_template_argument_list_p;
11846   bool saved_ice_p;
11847   bool saved_non_ice_p;
11848
11849   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11850   parser->in_template_argument_list_p = true;
11851   /* Even if the template-id appears in an integral
11852      constant-expression, the contents of the argument list do
11853      not.  */
11854   saved_ice_p = parser->integral_constant_expression_p;
11855   parser->integral_constant_expression_p = false;
11856   saved_non_ice_p = parser->non_integral_constant_expression_p;
11857   parser->non_integral_constant_expression_p = false;
11858   /* Parse the arguments.  */
11859   do
11860     {
11861       tree argument;
11862
11863       if (n_args)
11864         /* Consume the comma.  */
11865         cp_lexer_consume_token (parser->lexer);
11866
11867       /* Parse the template-argument.  */
11868       argument = cp_parser_template_argument (parser);
11869
11870       /* If the next token is an ellipsis, we're expanding a template
11871          argument pack. */
11872       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11873         {
11874           if (argument == error_mark_node)
11875             {
11876               cp_token *token = cp_lexer_peek_token (parser->lexer);
11877               error_at (token->location,
11878                         "expected parameter pack before %<...%>");
11879             }
11880           /* Consume the `...' token. */
11881           cp_lexer_consume_token (parser->lexer);
11882
11883           /* Make the argument into a TYPE_PACK_EXPANSION or
11884              EXPR_PACK_EXPANSION. */
11885           argument = make_pack_expansion (argument);
11886         }
11887
11888       if (n_args == alloced)
11889         {
11890           alloced *= 2;
11891
11892           if (arg_ary == fixed_args)
11893             {
11894               arg_ary = XNEWVEC (tree, alloced);
11895               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11896             }
11897           else
11898             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11899         }
11900       arg_ary[n_args++] = argument;
11901     }
11902   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11903
11904   vec = make_tree_vec (n_args);
11905
11906   while (n_args--)
11907     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11908
11909   if (arg_ary != fixed_args)
11910     free (arg_ary);
11911   parser->non_integral_constant_expression_p = saved_non_ice_p;
11912   parser->integral_constant_expression_p = saved_ice_p;
11913   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11914 #ifdef ENABLE_CHECKING
11915   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11916 #endif
11917   return vec;
11918 }
11919
11920 /* Parse a template-argument.
11921
11922    template-argument:
11923      assignment-expression
11924      type-id
11925      id-expression
11926
11927    The representation is that of an assignment-expression, type-id, or
11928    id-expression -- except that the qualified id-expression is
11929    evaluated, so that the value returned is either a DECL or an
11930    OVERLOAD.
11931
11932    Although the standard says "assignment-expression", it forbids
11933    throw-expressions or assignments in the template argument.
11934    Therefore, we use "conditional-expression" instead.  */
11935
11936 static tree
11937 cp_parser_template_argument (cp_parser* parser)
11938 {
11939   tree argument;
11940   bool template_p;
11941   bool address_p;
11942   bool maybe_type_id = false;
11943   cp_token *token = NULL, *argument_start_token = NULL;
11944   cp_id_kind idk;
11945
11946   /* There's really no way to know what we're looking at, so we just
11947      try each alternative in order.
11948
11949        [temp.arg]
11950
11951        In a template-argument, an ambiguity between a type-id and an
11952        expression is resolved to a type-id, regardless of the form of
11953        the corresponding template-parameter.
11954
11955      Therefore, we try a type-id first.  */
11956   cp_parser_parse_tentatively (parser);
11957   argument = cp_parser_template_type_arg (parser);
11958   /* If there was no error parsing the type-id but the next token is a
11959      '>>', our behavior depends on which dialect of C++ we're
11960      parsing. In C++98, we probably found a typo for '> >'. But there
11961      are type-id which are also valid expressions. For instance:
11962
11963      struct X { int operator >> (int); };
11964      template <int V> struct Foo {};
11965      Foo<X () >> 5> r;
11966
11967      Here 'X()' is a valid type-id of a function type, but the user just
11968      wanted to write the expression "X() >> 5". Thus, we remember that we
11969      found a valid type-id, but we still try to parse the argument as an
11970      expression to see what happens. 
11971
11972      In C++0x, the '>>' will be considered two separate '>'
11973      tokens.  */
11974   if (!cp_parser_error_occurred (parser)
11975       && cxx_dialect == cxx98
11976       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11977     {
11978       maybe_type_id = true;
11979       cp_parser_abort_tentative_parse (parser);
11980     }
11981   else
11982     {
11983       /* If the next token isn't a `,' or a `>', then this argument wasn't
11984       really finished. This means that the argument is not a valid
11985       type-id.  */
11986       if (!cp_parser_next_token_ends_template_argument_p (parser))
11987         cp_parser_error (parser, "expected template-argument");
11988       /* If that worked, we're done.  */
11989       if (cp_parser_parse_definitely (parser))
11990         return argument;
11991     }
11992   /* We're still not sure what the argument will be.  */
11993   cp_parser_parse_tentatively (parser);
11994   /* Try a template.  */
11995   argument_start_token = cp_lexer_peek_token (parser->lexer);
11996   argument = cp_parser_id_expression (parser,
11997                                       /*template_keyword_p=*/false,
11998                                       /*check_dependency_p=*/true,
11999                                       &template_p,
12000                                       /*declarator_p=*/false,
12001                                       /*optional_p=*/false);
12002   /* If the next token isn't a `,' or a `>', then this argument wasn't
12003      really finished.  */
12004   if (!cp_parser_next_token_ends_template_argument_p (parser))
12005     cp_parser_error (parser, "expected template-argument");
12006   if (!cp_parser_error_occurred (parser))
12007     {
12008       /* Figure out what is being referred to.  If the id-expression
12009          was for a class template specialization, then we will have a
12010          TYPE_DECL at this point.  There is no need to do name lookup
12011          at this point in that case.  */
12012       if (TREE_CODE (argument) != TYPE_DECL)
12013         argument = cp_parser_lookup_name (parser, argument,
12014                                           none_type,
12015                                           /*is_template=*/template_p,
12016                                           /*is_namespace=*/false,
12017                                           /*check_dependency=*/true,
12018                                           /*ambiguous_decls=*/NULL,
12019                                           argument_start_token->location);
12020       if (TREE_CODE (argument) != TEMPLATE_DECL
12021           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12022         cp_parser_error (parser, "expected template-name");
12023     }
12024   if (cp_parser_parse_definitely (parser))
12025     return argument;
12026   /* It must be a non-type argument.  There permitted cases are given
12027      in [temp.arg.nontype]:
12028
12029      -- an integral constant-expression of integral or enumeration
12030         type; or
12031
12032      -- the name of a non-type template-parameter; or
12033
12034      -- the name of an object or function with external linkage...
12035
12036      -- the address of an object or function with external linkage...
12037
12038      -- a pointer to member...  */
12039   /* Look for a non-type template parameter.  */
12040   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12041     {
12042       cp_parser_parse_tentatively (parser);
12043       argument = cp_parser_primary_expression (parser,
12044                                                /*address_p=*/false,
12045                                                /*cast_p=*/false,
12046                                                /*template_arg_p=*/true,
12047                                                &idk);
12048       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12049           || !cp_parser_next_token_ends_template_argument_p (parser))
12050         cp_parser_simulate_error (parser);
12051       if (cp_parser_parse_definitely (parser))
12052         return argument;
12053     }
12054
12055   /* If the next token is "&", the argument must be the address of an
12056      object or function with external linkage.  */
12057   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12058   if (address_p)
12059     cp_lexer_consume_token (parser->lexer);
12060   /* See if we might have an id-expression.  */
12061   token = cp_lexer_peek_token (parser->lexer);
12062   if (token->type == CPP_NAME
12063       || token->keyword == RID_OPERATOR
12064       || token->type == CPP_SCOPE
12065       || token->type == CPP_TEMPLATE_ID
12066       || token->type == CPP_NESTED_NAME_SPECIFIER)
12067     {
12068       cp_parser_parse_tentatively (parser);
12069       argument = cp_parser_primary_expression (parser,
12070                                                address_p,
12071                                                /*cast_p=*/false,
12072                                                /*template_arg_p=*/true,
12073                                                &idk);
12074       if (cp_parser_error_occurred (parser)
12075           || !cp_parser_next_token_ends_template_argument_p (parser))
12076         cp_parser_abort_tentative_parse (parser);
12077       else
12078         {
12079           tree probe;
12080
12081           if (TREE_CODE (argument) == INDIRECT_REF)
12082             {
12083               gcc_assert (REFERENCE_REF_P (argument));
12084               argument = TREE_OPERAND (argument, 0);
12085             }
12086
12087           /* If we're in a template, we represent a qualified-id referring
12088              to a static data member as a SCOPE_REF even if the scope isn't
12089              dependent so that we can check access control later.  */
12090           probe = argument;
12091           if (TREE_CODE (probe) == SCOPE_REF)
12092             probe = TREE_OPERAND (probe, 1);
12093           if (TREE_CODE (probe) == VAR_DECL)
12094             {
12095               /* A variable without external linkage might still be a
12096                  valid constant-expression, so no error is issued here
12097                  if the external-linkage check fails.  */
12098               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12099                 cp_parser_simulate_error (parser);
12100             }
12101           else if (is_overloaded_fn (argument))
12102             /* All overloaded functions are allowed; if the external
12103                linkage test does not pass, an error will be issued
12104                later.  */
12105             ;
12106           else if (address_p
12107                    && (TREE_CODE (argument) == OFFSET_REF
12108                        || TREE_CODE (argument) == SCOPE_REF))
12109             /* A pointer-to-member.  */
12110             ;
12111           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12112             ;
12113           else
12114             cp_parser_simulate_error (parser);
12115
12116           if (cp_parser_parse_definitely (parser))
12117             {
12118               if (address_p)
12119                 argument = build_x_unary_op (ADDR_EXPR, argument,
12120                                              tf_warning_or_error);
12121               return argument;
12122             }
12123         }
12124     }
12125   /* If the argument started with "&", there are no other valid
12126      alternatives at this point.  */
12127   if (address_p)
12128     {
12129       cp_parser_error (parser, "invalid non-type template argument");
12130       return error_mark_node;
12131     }
12132
12133   /* If the argument wasn't successfully parsed as a type-id followed
12134      by '>>', the argument can only be a constant expression now.
12135      Otherwise, we try parsing the constant-expression tentatively,
12136      because the argument could really be a type-id.  */
12137   if (maybe_type_id)
12138     cp_parser_parse_tentatively (parser);
12139   argument = cp_parser_constant_expression (parser,
12140                                             /*allow_non_constant_p=*/false,
12141                                             /*non_constant_p=*/NULL);
12142   argument = fold_non_dependent_expr (argument);
12143   if (!maybe_type_id)
12144     return argument;
12145   if (!cp_parser_next_token_ends_template_argument_p (parser))
12146     cp_parser_error (parser, "expected template-argument");
12147   if (cp_parser_parse_definitely (parser))
12148     return argument;
12149   /* We did our best to parse the argument as a non type-id, but that
12150      was the only alternative that matched (albeit with a '>' after
12151      it). We can assume it's just a typo from the user, and a
12152      diagnostic will then be issued.  */
12153   return cp_parser_template_type_arg (parser);
12154 }
12155
12156 /* Parse an explicit-instantiation.
12157
12158    explicit-instantiation:
12159      template declaration
12160
12161    Although the standard says `declaration', what it really means is:
12162
12163    explicit-instantiation:
12164      template decl-specifier-seq [opt] declarator [opt] ;
12165
12166    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12167    supposed to be allowed.  A defect report has been filed about this
12168    issue.
12169
12170    GNU Extension:
12171
12172    explicit-instantiation:
12173      storage-class-specifier template
12174        decl-specifier-seq [opt] declarator [opt] ;
12175      function-specifier template
12176        decl-specifier-seq [opt] declarator [opt] ;  */
12177
12178 static void
12179 cp_parser_explicit_instantiation (cp_parser* parser)
12180 {
12181   int declares_class_or_enum;
12182   cp_decl_specifier_seq decl_specifiers;
12183   tree extension_specifier = NULL_TREE;
12184
12185   timevar_push (TV_TEMPLATE_INST);
12186
12187   /* Look for an (optional) storage-class-specifier or
12188      function-specifier.  */
12189   if (cp_parser_allow_gnu_extensions_p (parser))
12190     {
12191       extension_specifier
12192         = cp_parser_storage_class_specifier_opt (parser);
12193       if (!extension_specifier)
12194         extension_specifier
12195           = cp_parser_function_specifier_opt (parser,
12196                                               /*decl_specs=*/NULL);
12197     }
12198
12199   /* Look for the `template' keyword.  */
12200   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12201   /* Let the front end know that we are processing an explicit
12202      instantiation.  */
12203   begin_explicit_instantiation ();
12204   /* [temp.explicit] says that we are supposed to ignore access
12205      control while processing explicit instantiation directives.  */
12206   push_deferring_access_checks (dk_no_check);
12207   /* Parse a decl-specifier-seq.  */
12208   cp_parser_decl_specifier_seq (parser,
12209                                 CP_PARSER_FLAGS_OPTIONAL,
12210                                 &decl_specifiers,
12211                                 &declares_class_or_enum);
12212   /* If there was exactly one decl-specifier, and it declared a class,
12213      and there's no declarator, then we have an explicit type
12214      instantiation.  */
12215   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12216     {
12217       tree type;
12218
12219       type = check_tag_decl (&decl_specifiers);
12220       /* Turn access control back on for names used during
12221          template instantiation.  */
12222       pop_deferring_access_checks ();
12223       if (type)
12224         do_type_instantiation (type, extension_specifier,
12225                                /*complain=*/tf_error);
12226     }
12227   else
12228     {
12229       cp_declarator *declarator;
12230       tree decl;
12231
12232       /* Parse the declarator.  */
12233       declarator
12234         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12235                                 /*ctor_dtor_or_conv_p=*/NULL,
12236                                 /*parenthesized_p=*/NULL,
12237                                 /*member_p=*/false);
12238       if (declares_class_or_enum & 2)
12239         cp_parser_check_for_definition_in_return_type (declarator,
12240                                                        decl_specifiers.type,
12241                                                        decl_specifiers.type_location);
12242       if (declarator != cp_error_declarator)
12243         {
12244           if (decl_specifiers.specs[(int)ds_inline])
12245             permerror (input_location, "explicit instantiation shall not use"
12246                        " %<inline%> specifier");
12247           if (decl_specifiers.specs[(int)ds_constexpr])
12248             permerror (input_location, "explicit instantiation shall not use"
12249                        " %<constexpr%> specifier");
12250
12251           decl = grokdeclarator (declarator, &decl_specifiers,
12252                                  NORMAL, 0, &decl_specifiers.attributes);
12253           /* Turn access control back on for names used during
12254              template instantiation.  */
12255           pop_deferring_access_checks ();
12256           /* Do the explicit instantiation.  */
12257           do_decl_instantiation (decl, extension_specifier);
12258         }
12259       else
12260         {
12261           pop_deferring_access_checks ();
12262           /* Skip the body of the explicit instantiation.  */
12263           cp_parser_skip_to_end_of_statement (parser);
12264         }
12265     }
12266   /* We're done with the instantiation.  */
12267   end_explicit_instantiation ();
12268
12269   cp_parser_consume_semicolon_at_end_of_statement (parser);
12270
12271   timevar_pop (TV_TEMPLATE_INST);
12272 }
12273
12274 /* Parse an explicit-specialization.
12275
12276    explicit-specialization:
12277      template < > declaration
12278
12279    Although the standard says `declaration', what it really means is:
12280
12281    explicit-specialization:
12282      template <> decl-specifier [opt] init-declarator [opt] ;
12283      template <> function-definition
12284      template <> explicit-specialization
12285      template <> template-declaration  */
12286
12287 static void
12288 cp_parser_explicit_specialization (cp_parser* parser)
12289 {
12290   bool need_lang_pop;
12291   cp_token *token = cp_lexer_peek_token (parser->lexer);
12292
12293   /* Look for the `template' keyword.  */
12294   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12295   /* Look for the `<'.  */
12296   cp_parser_require (parser, CPP_LESS, RT_LESS);
12297   /* Look for the `>'.  */
12298   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12299   /* We have processed another parameter list.  */
12300   ++parser->num_template_parameter_lists;
12301   /* [temp]
12302
12303      A template ... explicit specialization ... shall not have C
12304      linkage.  */
12305   if (current_lang_name == lang_name_c)
12306     {
12307       error_at (token->location, "template specialization with C linkage");
12308       /* Give it C++ linkage to avoid confusing other parts of the
12309          front end.  */
12310       push_lang_context (lang_name_cplusplus);
12311       need_lang_pop = true;
12312     }
12313   else
12314     need_lang_pop = false;
12315   /* Let the front end know that we are beginning a specialization.  */
12316   if (!begin_specialization ())
12317     {
12318       end_specialization ();
12319       return;
12320     }
12321
12322   /* If the next keyword is `template', we need to figure out whether
12323      or not we're looking a template-declaration.  */
12324   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12325     {
12326       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12327           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12328         cp_parser_template_declaration_after_export (parser,
12329                                                      /*member_p=*/false);
12330       else
12331         cp_parser_explicit_specialization (parser);
12332     }
12333   else
12334     /* Parse the dependent declaration.  */
12335     cp_parser_single_declaration (parser,
12336                                   /*checks=*/NULL,
12337                                   /*member_p=*/false,
12338                                   /*explicit_specialization_p=*/true,
12339                                   /*friend_p=*/NULL);
12340   /* We're done with the specialization.  */
12341   end_specialization ();
12342   /* For the erroneous case of a template with C linkage, we pushed an
12343      implicit C++ linkage scope; exit that scope now.  */
12344   if (need_lang_pop)
12345     pop_lang_context ();
12346   /* We're done with this parameter list.  */
12347   --parser->num_template_parameter_lists;
12348 }
12349
12350 /* Parse a type-specifier.
12351
12352    type-specifier:
12353      simple-type-specifier
12354      class-specifier
12355      enum-specifier
12356      elaborated-type-specifier
12357      cv-qualifier
12358
12359    GNU Extension:
12360
12361    type-specifier:
12362      __complex__
12363
12364    Returns a representation of the type-specifier.  For a
12365    class-specifier, enum-specifier, or elaborated-type-specifier, a
12366    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12367
12368    The parser flags FLAGS is used to control type-specifier parsing.
12369
12370    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12371    in a decl-specifier-seq.
12372
12373    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12374    class-specifier, enum-specifier, or elaborated-type-specifier, then
12375    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12376    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12377    zero.
12378
12379    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12380    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12381    is set to FALSE.  */
12382
12383 static tree
12384 cp_parser_type_specifier (cp_parser* parser,
12385                           cp_parser_flags flags,
12386                           cp_decl_specifier_seq *decl_specs,
12387                           bool is_declaration,
12388                           int* declares_class_or_enum,
12389                           bool* is_cv_qualifier)
12390 {
12391   tree type_spec = NULL_TREE;
12392   cp_token *token;
12393   enum rid keyword;
12394   cp_decl_spec ds = ds_last;
12395
12396   /* Assume this type-specifier does not declare a new type.  */
12397   if (declares_class_or_enum)
12398     *declares_class_or_enum = 0;
12399   /* And that it does not specify a cv-qualifier.  */
12400   if (is_cv_qualifier)
12401     *is_cv_qualifier = false;
12402   /* Peek at the next token.  */
12403   token = cp_lexer_peek_token (parser->lexer);
12404
12405   /* If we're looking at a keyword, we can use that to guide the
12406      production we choose.  */
12407   keyword = token->keyword;
12408   switch (keyword)
12409     {
12410     case RID_ENUM:
12411       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12412         goto elaborated_type_specifier;
12413
12414       /* Look for the enum-specifier.  */
12415       type_spec = cp_parser_enum_specifier (parser);
12416       /* If that worked, we're done.  */
12417       if (type_spec)
12418         {
12419           if (declares_class_or_enum)
12420             *declares_class_or_enum = 2;
12421           if (decl_specs)
12422             cp_parser_set_decl_spec_type (decl_specs,
12423                                           type_spec,
12424                                           token->location,
12425                                           /*user_defined_p=*/true);
12426           return type_spec;
12427         }
12428       else
12429         goto elaborated_type_specifier;
12430
12431       /* Any of these indicate either a class-specifier, or an
12432          elaborated-type-specifier.  */
12433     case RID_CLASS:
12434     case RID_STRUCT:
12435     case RID_UNION:
12436       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12437         goto elaborated_type_specifier;
12438
12439       /* Parse tentatively so that we can back up if we don't find a
12440          class-specifier.  */
12441       cp_parser_parse_tentatively (parser);
12442       /* Look for the class-specifier.  */
12443       type_spec = cp_parser_class_specifier (parser);
12444       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12445       /* If that worked, we're done.  */
12446       if (cp_parser_parse_definitely (parser))
12447         {
12448           if (declares_class_or_enum)
12449             *declares_class_or_enum = 2;
12450           if (decl_specs)
12451             cp_parser_set_decl_spec_type (decl_specs,
12452                                           type_spec,
12453                                           token->location,
12454                                           /*user_defined_p=*/true);
12455           return type_spec;
12456         }
12457
12458       /* Fall through.  */
12459     elaborated_type_specifier:
12460       /* We're declaring (not defining) a class or enum.  */
12461       if (declares_class_or_enum)
12462         *declares_class_or_enum = 1;
12463
12464       /* Fall through.  */
12465     case RID_TYPENAME:
12466       /* Look for an elaborated-type-specifier.  */
12467       type_spec
12468         = (cp_parser_elaborated_type_specifier
12469            (parser,
12470             decl_specs && decl_specs->specs[(int) ds_friend],
12471             is_declaration));
12472       if (decl_specs)
12473         cp_parser_set_decl_spec_type (decl_specs,
12474                                       type_spec,
12475                                       token->location,
12476                                       /*user_defined_p=*/true);
12477       return type_spec;
12478
12479     case RID_CONST:
12480       ds = ds_const;
12481       if (is_cv_qualifier)
12482         *is_cv_qualifier = true;
12483       break;
12484
12485     case RID_VOLATILE:
12486       ds = ds_volatile;
12487       if (is_cv_qualifier)
12488         *is_cv_qualifier = true;
12489       break;
12490
12491     case RID_RESTRICT:
12492       ds = ds_restrict;
12493       if (is_cv_qualifier)
12494         *is_cv_qualifier = true;
12495       break;
12496
12497     case RID_COMPLEX:
12498       /* The `__complex__' keyword is a GNU extension.  */
12499       ds = ds_complex;
12500       break;
12501
12502     default:
12503       break;
12504     }
12505
12506   /* Handle simple keywords.  */
12507   if (ds != ds_last)
12508     {
12509       if (decl_specs)
12510         {
12511           ++decl_specs->specs[(int)ds];
12512           decl_specs->any_specifiers_p = true;
12513         }
12514       return cp_lexer_consume_token (parser->lexer)->u.value;
12515     }
12516
12517   /* If we do not already have a type-specifier, assume we are looking
12518      at a simple-type-specifier.  */
12519   type_spec = cp_parser_simple_type_specifier (parser,
12520                                                decl_specs,
12521                                                flags);
12522
12523   /* If we didn't find a type-specifier, and a type-specifier was not
12524      optional in this context, issue an error message.  */
12525   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12526     {
12527       cp_parser_error (parser, "expected type specifier");
12528       return error_mark_node;
12529     }
12530
12531   return type_spec;
12532 }
12533
12534 /* Parse a simple-type-specifier.
12535
12536    simple-type-specifier:
12537      :: [opt] nested-name-specifier [opt] type-name
12538      :: [opt] nested-name-specifier template template-id
12539      char
12540      wchar_t
12541      bool
12542      short
12543      int
12544      long
12545      signed
12546      unsigned
12547      float
12548      double
12549      void
12550
12551    C++0x Extension:
12552
12553    simple-type-specifier:
12554      auto
12555      decltype ( expression )   
12556      char16_t
12557      char32_t
12558      __underlying_type ( type-id )
12559
12560    GNU Extension:
12561
12562    simple-type-specifier:
12563      __int128
12564      __typeof__ unary-expression
12565      __typeof__ ( type-id )
12566
12567    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12568    appropriately updated.  */
12569
12570 static tree
12571 cp_parser_simple_type_specifier (cp_parser* parser,
12572                                  cp_decl_specifier_seq *decl_specs,
12573                                  cp_parser_flags flags)
12574 {
12575   tree type = NULL_TREE;
12576   cp_token *token;
12577
12578   /* Peek at the next token.  */
12579   token = cp_lexer_peek_token (parser->lexer);
12580
12581   /* If we're looking at a keyword, things are easy.  */
12582   switch (token->keyword)
12583     {
12584     case RID_CHAR:
12585       if (decl_specs)
12586         decl_specs->explicit_char_p = true;
12587       type = char_type_node;
12588       break;
12589     case RID_CHAR16:
12590       type = char16_type_node;
12591       break;
12592     case RID_CHAR32:
12593       type = char32_type_node;
12594       break;
12595     case RID_WCHAR:
12596       type = wchar_type_node;
12597       break;
12598     case RID_BOOL:
12599       type = boolean_type_node;
12600       break;
12601     case RID_SHORT:
12602       if (decl_specs)
12603         ++decl_specs->specs[(int) ds_short];
12604       type = short_integer_type_node;
12605       break;
12606     case RID_INT:
12607       if (decl_specs)
12608         decl_specs->explicit_int_p = true;
12609       type = integer_type_node;
12610       break;
12611     case RID_INT128:
12612       if (!int128_integer_type_node)
12613         break;
12614       if (decl_specs)
12615         decl_specs->explicit_int128_p = true;
12616       type = int128_integer_type_node;
12617       break;
12618     case RID_LONG:
12619       if (decl_specs)
12620         ++decl_specs->specs[(int) ds_long];
12621       type = long_integer_type_node;
12622       break;
12623     case RID_SIGNED:
12624       if (decl_specs)
12625         ++decl_specs->specs[(int) ds_signed];
12626       type = integer_type_node;
12627       break;
12628     case RID_UNSIGNED:
12629       if (decl_specs)
12630         ++decl_specs->specs[(int) ds_unsigned];
12631       type = unsigned_type_node;
12632       break;
12633     case RID_FLOAT:
12634       type = float_type_node;
12635       break;
12636     case RID_DOUBLE:
12637       type = double_type_node;
12638       break;
12639     case RID_VOID:
12640       type = void_type_node;
12641       break;
12642       
12643     case RID_AUTO:
12644       maybe_warn_cpp0x (CPP0X_AUTO);
12645       type = make_auto ();
12646       break;
12647
12648     case RID_DECLTYPE:
12649       /* Parse the `decltype' type.  */
12650       type = cp_parser_decltype (parser);
12651
12652       if (decl_specs)
12653         cp_parser_set_decl_spec_type (decl_specs, type,
12654                                       token->location,
12655                                       /*user_defined_p=*/true);
12656
12657       return type;
12658
12659     case RID_TYPEOF:
12660       /* Consume the `typeof' token.  */
12661       cp_lexer_consume_token (parser->lexer);
12662       /* Parse the operand to `typeof'.  */
12663       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12664       /* If it is not already a TYPE, take its type.  */
12665       if (!TYPE_P (type))
12666         type = finish_typeof (type);
12667
12668       if (decl_specs)
12669         cp_parser_set_decl_spec_type (decl_specs, type,
12670                                       token->location,
12671                                       /*user_defined_p=*/true);
12672
12673       return type;
12674
12675     case RID_UNDERLYING_TYPE:
12676       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12677
12678       if (decl_specs)
12679         cp_parser_set_decl_spec_type (decl_specs, type,
12680                                       token->location,
12681                                       /*user_defined_p=*/true);
12682
12683       return type;
12684
12685     default:
12686       break;
12687     }
12688
12689   /* If the type-specifier was for a built-in type, we're done.  */
12690   if (type)
12691     {
12692       /* Record the type.  */
12693       if (decl_specs
12694           && (token->keyword != RID_SIGNED
12695               && token->keyword != RID_UNSIGNED
12696               && token->keyword != RID_SHORT
12697               && token->keyword != RID_LONG))
12698         cp_parser_set_decl_spec_type (decl_specs,
12699                                       type,
12700                                       token->location,
12701                                       /*user_defined=*/false);
12702       if (decl_specs)
12703         decl_specs->any_specifiers_p = true;
12704
12705       /* Consume the token.  */
12706       cp_lexer_consume_token (parser->lexer);
12707
12708       /* There is no valid C++ program where a non-template type is
12709          followed by a "<".  That usually indicates that the user thought
12710          that the type was a template.  */
12711       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12712
12713       return TYPE_NAME (type);
12714     }
12715
12716   /* The type-specifier must be a user-defined type.  */
12717   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12718     {
12719       bool qualified_p;
12720       bool global_p;
12721
12722       /* Don't gobble tokens or issue error messages if this is an
12723          optional type-specifier.  */
12724       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12725         cp_parser_parse_tentatively (parser);
12726
12727       /* Look for the optional `::' operator.  */
12728       global_p
12729         = (cp_parser_global_scope_opt (parser,
12730                                        /*current_scope_valid_p=*/false)
12731            != NULL_TREE);
12732       /* Look for the nested-name specifier.  */
12733       qualified_p
12734         = (cp_parser_nested_name_specifier_opt (parser,
12735                                                 /*typename_keyword_p=*/false,
12736                                                 /*check_dependency_p=*/true,
12737                                                 /*type_p=*/false,
12738                                                 /*is_declaration=*/false)
12739            != NULL_TREE);
12740       token = cp_lexer_peek_token (parser->lexer);
12741       /* If we have seen a nested-name-specifier, and the next token
12742          is `template', then we are using the template-id production.  */
12743       if (parser->scope
12744           && cp_parser_optional_template_keyword (parser))
12745         {
12746           /* Look for the template-id.  */
12747           type = cp_parser_template_id (parser,
12748                                         /*template_keyword_p=*/true,
12749                                         /*check_dependency_p=*/true,
12750                                         /*is_declaration=*/false);
12751           /* If the template-id did not name a type, we are out of
12752              luck.  */
12753           if (TREE_CODE (type) != TYPE_DECL)
12754             {
12755               cp_parser_error (parser, "expected template-id for type");
12756               type = NULL_TREE;
12757             }
12758         }
12759       /* Otherwise, look for a type-name.  */
12760       else
12761         type = cp_parser_type_name (parser);
12762       /* Keep track of all name-lookups performed in class scopes.  */
12763       if (type
12764           && !global_p
12765           && !qualified_p
12766           && TREE_CODE (type) == TYPE_DECL
12767           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12768         maybe_note_name_used_in_class (DECL_NAME (type), type);
12769       /* If it didn't work out, we don't have a TYPE.  */
12770       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12771           && !cp_parser_parse_definitely (parser))
12772         type = NULL_TREE;
12773       if (type && decl_specs)
12774         cp_parser_set_decl_spec_type (decl_specs, type,
12775                                       token->location,
12776                                       /*user_defined=*/true);
12777     }
12778
12779   /* If we didn't get a type-name, issue an error message.  */
12780   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12781     {
12782       cp_parser_error (parser, "expected type-name");
12783       return error_mark_node;
12784     }
12785
12786   if (type && type != error_mark_node)
12787     {
12788       /* See if TYPE is an Objective-C type, and if so, parse and
12789          accept any protocol references following it.  Do this before
12790          the cp_parser_check_for_invalid_template_id() call, because
12791          Objective-C types can be followed by '<...>' which would
12792          enclose protocol names rather than template arguments, and so
12793          everything is fine.  */
12794       if (c_dialect_objc () && !parser->scope
12795           && (objc_is_id (type) || objc_is_class_name (type)))
12796         {
12797           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12798           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12799
12800           /* Clobber the "unqualified" type previously entered into
12801              DECL_SPECS with the new, improved protocol-qualified version.  */
12802           if (decl_specs)
12803             decl_specs->type = qual_type;
12804
12805           return qual_type;
12806         }
12807
12808       /* There is no valid C++ program where a non-template type is
12809          followed by a "<".  That usually indicates that the user
12810          thought that the type was a template.  */
12811       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12812                                                token->location);
12813     }
12814
12815   return type;
12816 }
12817
12818 /* Parse a type-name.
12819
12820    type-name:
12821      class-name
12822      enum-name
12823      typedef-name
12824
12825    enum-name:
12826      identifier
12827
12828    typedef-name:
12829      identifier
12830
12831    Returns a TYPE_DECL for the type.  */
12832
12833 static tree
12834 cp_parser_type_name (cp_parser* parser)
12835 {
12836   tree type_decl;
12837
12838   /* We can't know yet whether it is a class-name or not.  */
12839   cp_parser_parse_tentatively (parser);
12840   /* Try a class-name.  */
12841   type_decl = cp_parser_class_name (parser,
12842                                     /*typename_keyword_p=*/false,
12843                                     /*template_keyword_p=*/false,
12844                                     none_type,
12845                                     /*check_dependency_p=*/true,
12846                                     /*class_head_p=*/false,
12847                                     /*is_declaration=*/false);
12848   /* If it's not a class-name, keep looking.  */
12849   if (!cp_parser_parse_definitely (parser))
12850     {
12851       /* It must be a typedef-name or an enum-name.  */
12852       return cp_parser_nonclass_name (parser);
12853     }
12854
12855   return type_decl;
12856 }
12857
12858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12859
12860    enum-name:
12861      identifier
12862
12863    typedef-name:
12864      identifier
12865
12866    Returns a TYPE_DECL for the type.  */
12867
12868 static tree
12869 cp_parser_nonclass_name (cp_parser* parser)
12870 {
12871   tree type_decl;
12872   tree identifier;
12873
12874   cp_token *token = cp_lexer_peek_token (parser->lexer);
12875   identifier = cp_parser_identifier (parser);
12876   if (identifier == error_mark_node)
12877     return error_mark_node;
12878
12879   /* Look up the type-name.  */
12880   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12881
12882   if (TREE_CODE (type_decl) != TYPE_DECL
12883       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12884     {
12885       /* See if this is an Objective-C type.  */
12886       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12887       tree type = objc_get_protocol_qualified_type (identifier, protos);
12888       if (type)
12889         type_decl = TYPE_NAME (type);
12890     }
12891
12892   /* Issue an error if we did not find a type-name.  */
12893   if (TREE_CODE (type_decl) != TYPE_DECL
12894       /* In Objective-C, we have the complication that class names are
12895          normally type names and start declarations (eg, the
12896          "NSObject" in "NSObject *object;"), but can be used in an
12897          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12898          is an expression.  So, a classname followed by a dot is not a
12899          valid type-name.  */
12900       || (objc_is_class_name (TREE_TYPE (type_decl))
12901           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12902     {
12903       if (!cp_parser_simulate_error (parser))
12904         cp_parser_name_lookup_error (parser, identifier, type_decl,
12905                                      NLE_TYPE, token->location);
12906       return error_mark_node;
12907     }
12908   /* Remember that the name was used in the definition of the
12909      current class so that we can check later to see if the
12910      meaning would have been different after the class was
12911      entirely defined.  */
12912   else if (type_decl != error_mark_node
12913            && !parser->scope)
12914     maybe_note_name_used_in_class (identifier, type_decl);
12915   
12916   return type_decl;
12917 }
12918
12919 /* Parse an elaborated-type-specifier.  Note that the grammar given
12920    here incorporates the resolution to DR68.
12921
12922    elaborated-type-specifier:
12923      class-key :: [opt] nested-name-specifier [opt] identifier
12924      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12925      enum-key :: [opt] nested-name-specifier [opt] identifier
12926      typename :: [opt] nested-name-specifier identifier
12927      typename :: [opt] nested-name-specifier template [opt]
12928        template-id
12929
12930    GNU extension:
12931
12932    elaborated-type-specifier:
12933      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12934      class-key attributes :: [opt] nested-name-specifier [opt]
12935                template [opt] template-id
12936      enum attributes :: [opt] nested-name-specifier [opt] identifier
12937
12938    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12939    declared `friend'.  If IS_DECLARATION is TRUE, then this
12940    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12941    something is being declared.
12942
12943    Returns the TYPE specified.  */
12944
12945 static tree
12946 cp_parser_elaborated_type_specifier (cp_parser* parser,
12947                                      bool is_friend,
12948                                      bool is_declaration)
12949 {
12950   enum tag_types tag_type;
12951   tree identifier;
12952   tree type = NULL_TREE;
12953   tree attributes = NULL_TREE;
12954   tree globalscope;
12955   cp_token *token = NULL;
12956
12957   /* See if we're looking at the `enum' keyword.  */
12958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12959     {
12960       /* Consume the `enum' token.  */
12961       cp_lexer_consume_token (parser->lexer);
12962       /* Remember that it's an enumeration type.  */
12963       tag_type = enum_type;
12964       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12965          enums) is used here.  */
12966       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12967           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12968         {
12969             pedwarn (input_location, 0, "elaborated-type-specifier "
12970                       "for a scoped enum must not use the %<%D%> keyword",
12971                       cp_lexer_peek_token (parser->lexer)->u.value);
12972           /* Consume the `struct' or `class' and parse it anyway.  */
12973           cp_lexer_consume_token (parser->lexer);
12974         }
12975       /* Parse the attributes.  */
12976       attributes = cp_parser_attributes_opt (parser);
12977     }
12978   /* Or, it might be `typename'.  */
12979   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12980                                            RID_TYPENAME))
12981     {
12982       /* Consume the `typename' token.  */
12983       cp_lexer_consume_token (parser->lexer);
12984       /* Remember that it's a `typename' type.  */
12985       tag_type = typename_type;
12986     }
12987   /* Otherwise it must be a class-key.  */
12988   else
12989     {
12990       tag_type = cp_parser_class_key (parser);
12991       if (tag_type == none_type)
12992         return error_mark_node;
12993       /* Parse the attributes.  */
12994       attributes = cp_parser_attributes_opt (parser);
12995     }
12996
12997   /* Look for the `::' operator.  */
12998   globalscope =  cp_parser_global_scope_opt (parser,
12999                                              /*current_scope_valid_p=*/false);
13000   /* Look for the nested-name-specifier.  */
13001   if (tag_type == typename_type && !globalscope)
13002     {
13003       if (!cp_parser_nested_name_specifier (parser,
13004                                            /*typename_keyword_p=*/true,
13005                                            /*check_dependency_p=*/true,
13006                                            /*type_p=*/true,
13007                                             is_declaration))
13008         return error_mark_node;
13009     }
13010   else
13011     /* Even though `typename' is not present, the proposed resolution
13012        to Core Issue 180 says that in `class A<T>::B', `B' should be
13013        considered a type-name, even if `A<T>' is dependent.  */
13014     cp_parser_nested_name_specifier_opt (parser,
13015                                          /*typename_keyword_p=*/true,
13016                                          /*check_dependency_p=*/true,
13017                                          /*type_p=*/true,
13018                                          is_declaration);
13019  /* For everything but enumeration types, consider a template-id.
13020     For an enumeration type, consider only a plain identifier.  */
13021   if (tag_type != enum_type)
13022     {
13023       bool template_p = false;
13024       tree decl;
13025
13026       /* Allow the `template' keyword.  */
13027       template_p = cp_parser_optional_template_keyword (parser);
13028       /* If we didn't see `template', we don't know if there's a
13029          template-id or not.  */
13030       if (!template_p)
13031         cp_parser_parse_tentatively (parser);
13032       /* Parse the template-id.  */
13033       token = cp_lexer_peek_token (parser->lexer);
13034       decl = cp_parser_template_id (parser, template_p,
13035                                     /*check_dependency_p=*/true,
13036                                     is_declaration);
13037       /* If we didn't find a template-id, look for an ordinary
13038          identifier.  */
13039       if (!template_p && !cp_parser_parse_definitely (parser))
13040         ;
13041       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13042          in effect, then we must assume that, upon instantiation, the
13043          template will correspond to a class.  */
13044       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13045                && tag_type == typename_type)
13046         type = make_typename_type (parser->scope, decl,
13047                                    typename_type,
13048                                    /*complain=*/tf_error);
13049       /* If the `typename' keyword is in effect and DECL is not a type
13050          decl. Then type is non existant.   */
13051       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13052         type = NULL_TREE; 
13053       else 
13054         type = TREE_TYPE (decl);
13055     }
13056
13057   if (!type)
13058     {
13059       token = cp_lexer_peek_token (parser->lexer);
13060       identifier = cp_parser_identifier (parser);
13061
13062       if (identifier == error_mark_node)
13063         {
13064           parser->scope = NULL_TREE;
13065           return error_mark_node;
13066         }
13067
13068       /* For a `typename', we needn't call xref_tag.  */
13069       if (tag_type == typename_type
13070           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13071         return cp_parser_make_typename_type (parser, parser->scope,
13072                                              identifier,
13073                                              token->location);
13074       /* Look up a qualified name in the usual way.  */
13075       if (parser->scope)
13076         {
13077           tree decl;
13078           tree ambiguous_decls;
13079
13080           decl = cp_parser_lookup_name (parser, identifier,
13081                                         tag_type,
13082                                         /*is_template=*/false,
13083                                         /*is_namespace=*/false,
13084                                         /*check_dependency=*/true,
13085                                         &ambiguous_decls,
13086                                         token->location);
13087
13088           /* If the lookup was ambiguous, an error will already have been
13089              issued.  */
13090           if (ambiguous_decls)
13091             return error_mark_node;
13092
13093           /* If we are parsing friend declaration, DECL may be a
13094              TEMPLATE_DECL tree node here.  However, we need to check
13095              whether this TEMPLATE_DECL results in valid code.  Consider
13096              the following example:
13097
13098                namespace N {
13099                  template <class T> class C {};
13100                }
13101                class X {
13102                  template <class T> friend class N::C; // #1, valid code
13103                };
13104                template <class T> class Y {
13105                  friend class N::C;                    // #2, invalid code
13106                };
13107
13108              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13109              name lookup of `N::C'.  We see that friend declaration must
13110              be template for the code to be valid.  Note that
13111              processing_template_decl does not work here since it is
13112              always 1 for the above two cases.  */
13113
13114           decl = (cp_parser_maybe_treat_template_as_class
13115                   (decl, /*tag_name_p=*/is_friend
13116                          && parser->num_template_parameter_lists));
13117
13118           if (TREE_CODE (decl) != TYPE_DECL)
13119             {
13120               cp_parser_diagnose_invalid_type_name (parser,
13121                                                     parser->scope,
13122                                                     identifier,
13123                                                     token->location);
13124               return error_mark_node;
13125             }
13126
13127           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13128             {
13129               bool allow_template = (parser->num_template_parameter_lists
13130                                       || DECL_SELF_REFERENCE_P (decl));
13131               type = check_elaborated_type_specifier (tag_type, decl, 
13132                                                       allow_template);
13133
13134               if (type == error_mark_node)
13135                 return error_mark_node;
13136             }
13137
13138           /* Forward declarations of nested types, such as
13139
13140                class C1::C2;
13141                class C1::C2::C3;
13142
13143              are invalid unless all components preceding the final '::'
13144              are complete.  If all enclosing types are complete, these
13145              declarations become merely pointless.
13146
13147              Invalid forward declarations of nested types are errors
13148              caught elsewhere in parsing.  Those that are pointless arrive
13149              here.  */
13150
13151           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13152               && !is_friend && !processing_explicit_instantiation)
13153             warning (0, "declaration %qD does not declare anything", decl);
13154
13155           type = TREE_TYPE (decl);
13156         }
13157       else
13158         {
13159           /* An elaborated-type-specifier sometimes introduces a new type and
13160              sometimes names an existing type.  Normally, the rule is that it
13161              introduces a new type only if there is not an existing type of
13162              the same name already in scope.  For example, given:
13163
13164                struct S {};
13165                void f() { struct S s; }
13166
13167              the `struct S' in the body of `f' is the same `struct S' as in
13168              the global scope; the existing definition is used.  However, if
13169              there were no global declaration, this would introduce a new
13170              local class named `S'.
13171
13172              An exception to this rule applies to the following code:
13173
13174                namespace N { struct S; }
13175
13176              Here, the elaborated-type-specifier names a new type
13177              unconditionally; even if there is already an `S' in the
13178              containing scope this declaration names a new type.
13179              This exception only applies if the elaborated-type-specifier
13180              forms the complete declaration:
13181
13182                [class.name]
13183
13184                A declaration consisting solely of `class-key identifier ;' is
13185                either a redeclaration of the name in the current scope or a
13186                forward declaration of the identifier as a class name.  It
13187                introduces the name into the current scope.
13188
13189              We are in this situation precisely when the next token is a `;'.
13190
13191              An exception to the exception is that a `friend' declaration does
13192              *not* name a new type; i.e., given:
13193
13194                struct S { friend struct T; };
13195
13196              `T' is not a new type in the scope of `S'.
13197
13198              Also, `new struct S' or `sizeof (struct S)' never results in the
13199              definition of a new type; a new type can only be declared in a
13200              declaration context.  */
13201
13202           tag_scope ts;
13203           bool template_p;
13204
13205           if (is_friend)
13206             /* Friends have special name lookup rules.  */
13207             ts = ts_within_enclosing_non_class;
13208           else if (is_declaration
13209                    && cp_lexer_next_token_is (parser->lexer,
13210                                               CPP_SEMICOLON))
13211             /* This is a `class-key identifier ;' */
13212             ts = ts_current;
13213           else
13214             ts = ts_global;
13215
13216           template_p =
13217             (parser->num_template_parameter_lists
13218              && (cp_parser_next_token_starts_class_definition_p (parser)
13219                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13220           /* An unqualified name was used to reference this type, so
13221              there were no qualifying templates.  */
13222           if (!cp_parser_check_template_parameters (parser,
13223                                                     /*num_templates=*/0,
13224                                                     token->location,
13225                                                     /*declarator=*/NULL))
13226             return error_mark_node;
13227           type = xref_tag (tag_type, identifier, ts, template_p);
13228         }
13229     }
13230
13231   if (type == error_mark_node)
13232     return error_mark_node;
13233
13234   /* Allow attributes on forward declarations of classes.  */
13235   if (attributes)
13236     {
13237       if (TREE_CODE (type) == TYPENAME_TYPE)
13238         warning (OPT_Wattributes,
13239                  "attributes ignored on uninstantiated type");
13240       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13241                && ! processing_explicit_instantiation)
13242         warning (OPT_Wattributes,
13243                  "attributes ignored on template instantiation");
13244       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13245         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13246       else
13247         warning (OPT_Wattributes,
13248                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13249     }
13250
13251   if (tag_type != enum_type)
13252     cp_parser_check_class_key (tag_type, type);
13253
13254   /* A "<" cannot follow an elaborated type specifier.  If that
13255      happens, the user was probably trying to form a template-id.  */
13256   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13257
13258   return type;
13259 }
13260
13261 /* Parse an enum-specifier.
13262
13263    enum-specifier:
13264      enum-head { enumerator-list [opt] }
13265
13266    enum-head:
13267      enum-key identifier [opt] enum-base [opt]
13268      enum-key nested-name-specifier identifier enum-base [opt]
13269
13270    enum-key:
13271      enum
13272      enum class   [C++0x]
13273      enum struct  [C++0x]
13274
13275    enum-base:   [C++0x]
13276      : type-specifier-seq
13277
13278    opaque-enum-specifier:
13279      enum-key identifier enum-base [opt] ;
13280
13281    GNU Extensions:
13282      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13283        { enumerator-list [opt] }attributes[opt]
13284
13285    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13286    if the token stream isn't an enum-specifier after all.  */
13287
13288 static tree
13289 cp_parser_enum_specifier (cp_parser* parser)
13290 {
13291   tree identifier;
13292   tree type = NULL_TREE;
13293   tree prev_scope;
13294   tree nested_name_specifier = NULL_TREE;
13295   tree attributes;
13296   bool scoped_enum_p = false;
13297   bool has_underlying_type = false;
13298   bool nested_being_defined = false;
13299   bool new_value_list = false;
13300   bool is_new_type = false;
13301   bool is_anonymous = false;
13302   tree underlying_type = NULL_TREE;
13303   cp_token *type_start_token = NULL;
13304   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13305
13306   parser->colon_corrects_to_scope_p = false;
13307
13308   /* Parse tentatively so that we can back up if we don't find a
13309      enum-specifier.  */
13310   cp_parser_parse_tentatively (parser);
13311
13312   /* Caller guarantees that the current token is 'enum', an identifier
13313      possibly follows, and the token after that is an opening brace.
13314      If we don't have an identifier, fabricate an anonymous name for
13315      the enumeration being defined.  */
13316   cp_lexer_consume_token (parser->lexer);
13317
13318   /* Parse the "class" or "struct", which indicates a scoped
13319      enumeration type in C++0x.  */
13320   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13321       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13322     {
13323       if (cxx_dialect < cxx0x)
13324         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13325
13326       /* Consume the `struct' or `class' token.  */
13327       cp_lexer_consume_token (parser->lexer);
13328
13329       scoped_enum_p = true;
13330     }
13331
13332   attributes = cp_parser_attributes_opt (parser);
13333
13334   /* Clear the qualification.  */
13335   parser->scope = NULL_TREE;
13336   parser->qualifying_scope = NULL_TREE;
13337   parser->object_scope = NULL_TREE;
13338
13339   /* Figure out in what scope the declaration is being placed.  */
13340   prev_scope = current_scope ();
13341
13342   type_start_token = cp_lexer_peek_token (parser->lexer);
13343
13344   push_deferring_access_checks (dk_no_check);
13345   nested_name_specifier
13346       = cp_parser_nested_name_specifier_opt (parser,
13347                                              /*typename_keyword_p=*/true,
13348                                              /*check_dependency_p=*/false,
13349                                              /*type_p=*/false,
13350                                              /*is_declaration=*/false);
13351
13352   if (nested_name_specifier)
13353     {
13354       tree name;
13355
13356       identifier = cp_parser_identifier (parser);
13357       name =  cp_parser_lookup_name (parser, identifier,
13358                                      enum_type,
13359                                      /*is_template=*/false,
13360                                      /*is_namespace=*/false,
13361                                      /*check_dependency=*/true,
13362                                      /*ambiguous_decls=*/NULL,
13363                                      input_location);
13364       if (name)
13365         {
13366           type = TREE_TYPE (name);
13367           if (TREE_CODE (type) == TYPENAME_TYPE)
13368             {
13369               /* Are template enums allowed in ISO? */
13370               if (template_parm_scope_p ())
13371                 pedwarn (type_start_token->location, OPT_pedantic,
13372                          "%qD is an enumeration template", name);
13373               /* ignore a typename reference, for it will be solved by name
13374                  in start_enum.  */
13375               type = NULL_TREE;
13376             }
13377         }
13378       else
13379         error_at (type_start_token->location,
13380                   "%qD is not an enumerator-name", identifier);
13381     }
13382   else
13383     {
13384       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13385         identifier = cp_parser_identifier (parser);
13386       else
13387         {
13388           identifier = make_anon_name ();
13389           is_anonymous = true;
13390         }
13391     }
13392   pop_deferring_access_checks ();
13393
13394   /* Check for the `:' that denotes a specified underlying type in C++0x.
13395      Note that a ':' could also indicate a bitfield width, however.  */
13396   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13397     {
13398       cp_decl_specifier_seq type_specifiers;
13399
13400       /* Consume the `:'.  */
13401       cp_lexer_consume_token (parser->lexer);
13402
13403       /* Parse the type-specifier-seq.  */
13404       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13405                                     /*is_trailing_return=*/false,
13406                                     &type_specifiers);
13407
13408       /* At this point this is surely not elaborated type specifier.  */
13409       if (!cp_parser_parse_definitely (parser))
13410         return NULL_TREE;
13411
13412       if (cxx_dialect < cxx0x)
13413         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13414
13415       has_underlying_type = true;
13416
13417       /* If that didn't work, stop.  */
13418       if (type_specifiers.type != error_mark_node)
13419         {
13420           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13421                                             /*initialized=*/0, NULL);
13422           if (underlying_type == error_mark_node)
13423             underlying_type = NULL_TREE;
13424         }
13425     }
13426
13427   /* Look for the `{' but don't consume it yet.  */
13428   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13429     {
13430       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13431         {
13432           cp_parser_error (parser, "expected %<{%>");
13433           if (has_underlying_type)
13434             {
13435               type = NULL_TREE;
13436               goto out;
13437             }
13438         }
13439       /* An opaque-enum-specifier must have a ';' here.  */
13440       if ((scoped_enum_p || underlying_type)
13441           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13442         {
13443           cp_parser_error (parser, "expected %<;%> or %<{%>");
13444           if (has_underlying_type)
13445             {
13446               type = NULL_TREE;
13447               goto out;
13448             }
13449         }
13450     }
13451
13452   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13453     return NULL_TREE;
13454
13455   if (nested_name_specifier)
13456     {
13457       if (CLASS_TYPE_P (nested_name_specifier))
13458         {
13459           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13460           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13461           push_scope (nested_name_specifier);
13462         }
13463       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13464         {
13465           push_nested_namespace (nested_name_specifier);
13466         }
13467     }
13468
13469   /* Issue an error message if type-definitions are forbidden here.  */
13470   if (!cp_parser_check_type_definition (parser))
13471     type = error_mark_node;
13472   else
13473     /* Create the new type.  We do this before consuming the opening
13474        brace so the enum will be recorded as being on the line of its
13475        tag (or the 'enum' keyword, if there is no tag).  */
13476     type = start_enum (identifier, type, underlying_type,
13477                        scoped_enum_p, &is_new_type);
13478
13479   /* If the next token is not '{' it is an opaque-enum-specifier or an
13480      elaborated-type-specifier.  */
13481   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13482     {
13483       timevar_push (TV_PARSE_ENUM);
13484       if (nested_name_specifier)
13485         {
13486           /* The following catches invalid code such as:
13487              enum class S<int>::E { A, B, C }; */
13488           if (!processing_specialization
13489               && CLASS_TYPE_P (nested_name_specifier)
13490               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13491             error_at (type_start_token->location, "cannot add an enumerator "
13492                       "list to a template instantiation");
13493
13494           /* If that scope does not contain the scope in which the
13495              class was originally declared, the program is invalid.  */
13496           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13497             {
13498               if (at_namespace_scope_p ())
13499                 error_at (type_start_token->location,
13500                           "declaration of %qD in namespace %qD which does not "
13501                           "enclose %qD",
13502                           type, prev_scope, nested_name_specifier);
13503               else
13504                 error_at (type_start_token->location,
13505                           "declaration of %qD in %qD which does not enclose %qD",
13506                           type, prev_scope, nested_name_specifier);
13507               type = error_mark_node;
13508             }
13509         }
13510
13511       if (scoped_enum_p)
13512         begin_scope (sk_scoped_enum, type);
13513
13514       /* Consume the opening brace.  */
13515       cp_lexer_consume_token (parser->lexer);
13516
13517       if (type == error_mark_node)
13518         ; /* Nothing to add */
13519       else if (OPAQUE_ENUM_P (type)
13520                || (cxx_dialect > cxx98 && processing_specialization))
13521         {
13522           new_value_list = true;
13523           SET_OPAQUE_ENUM_P (type, false);
13524           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13525         }
13526       else
13527         {
13528           error_at (type_start_token->location, "multiple definition of %q#T", type);
13529           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13530                     "previous definition here");
13531           type = error_mark_node;
13532         }
13533
13534       if (type == error_mark_node)
13535         cp_parser_skip_to_end_of_block_or_statement (parser);
13536       /* If the next token is not '}', then there are some enumerators.  */
13537       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13538         cp_parser_enumerator_list (parser, type);
13539
13540       /* Consume the final '}'.  */
13541       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13542
13543       if (scoped_enum_p)
13544         finish_scope ();
13545       timevar_pop (TV_PARSE_ENUM);
13546     }
13547   else
13548     {
13549       /* If a ';' follows, then it is an opaque-enum-specifier
13550         and additional restrictions apply.  */
13551       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13552         {
13553           if (is_anonymous)
13554             error_at (type_start_token->location,
13555                       "opaque-enum-specifier without name");
13556           else if (nested_name_specifier)
13557             error_at (type_start_token->location,
13558                       "opaque-enum-specifier must use a simple identifier");
13559         }
13560     }
13561
13562   /* Look for trailing attributes to apply to this enumeration, and
13563      apply them if appropriate.  */
13564   if (cp_parser_allow_gnu_extensions_p (parser))
13565     {
13566       tree trailing_attr = cp_parser_attributes_opt (parser);
13567       trailing_attr = chainon (trailing_attr, attributes);
13568       cplus_decl_attributes (&type,
13569                              trailing_attr,
13570                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13571     }
13572
13573   /* Finish up the enumeration.  */
13574   if (type != error_mark_node)
13575     {
13576       if (new_value_list)
13577         finish_enum_value_list (type);
13578       if (is_new_type)
13579         finish_enum (type);
13580     }
13581
13582   if (nested_name_specifier)
13583     {
13584       if (CLASS_TYPE_P (nested_name_specifier))
13585         {
13586           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13587           pop_scope (nested_name_specifier);
13588         }
13589       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13590         {
13591           pop_nested_namespace (nested_name_specifier);
13592         }
13593     }
13594  out:
13595   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13596   return type;
13597 }
13598
13599 /* Parse an enumerator-list.  The enumerators all have the indicated
13600    TYPE.
13601
13602    enumerator-list:
13603      enumerator-definition
13604      enumerator-list , enumerator-definition  */
13605
13606 static void
13607 cp_parser_enumerator_list (cp_parser* parser, tree type)
13608 {
13609   while (true)
13610     {
13611       /* Parse an enumerator-definition.  */
13612       cp_parser_enumerator_definition (parser, type);
13613
13614       /* If the next token is not a ',', we've reached the end of
13615          the list.  */
13616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13617         break;
13618       /* Otherwise, consume the `,' and keep going.  */
13619       cp_lexer_consume_token (parser->lexer);
13620       /* If the next token is a `}', there is a trailing comma.  */
13621       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13622         {
13623           if (!in_system_header)
13624             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13625           break;
13626         }
13627     }
13628 }
13629
13630 /* Parse an enumerator-definition.  The enumerator has the indicated
13631    TYPE.
13632
13633    enumerator-definition:
13634      enumerator
13635      enumerator = constant-expression
13636
13637    enumerator:
13638      identifier  */
13639
13640 static void
13641 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13642 {
13643   tree identifier;
13644   tree value;
13645   location_t loc;
13646
13647   /* Save the input location because we are interested in the location
13648      of the identifier and not the location of the explicit value.  */
13649   loc = cp_lexer_peek_token (parser->lexer)->location;
13650
13651   /* Look for the identifier.  */
13652   identifier = cp_parser_identifier (parser);
13653   if (identifier == error_mark_node)
13654     return;
13655
13656   /* If the next token is an '=', then there is an explicit value.  */
13657   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13658     {
13659       /* Consume the `=' token.  */
13660       cp_lexer_consume_token (parser->lexer);
13661       /* Parse the value.  */
13662       value = cp_parser_constant_expression (parser,
13663                                              /*allow_non_constant_p=*/false,
13664                                              NULL);
13665     }
13666   else
13667     value = NULL_TREE;
13668
13669   /* If we are processing a template, make sure the initializer of the
13670      enumerator doesn't contain any bare template parameter pack.  */
13671   if (check_for_bare_parameter_packs (value))
13672     value = error_mark_node;
13673
13674   /* integral_constant_value will pull out this expression, so make sure
13675      it's folded as appropriate.  */
13676   value = fold_non_dependent_expr (value);
13677
13678   /* Create the enumerator.  */
13679   build_enumerator (identifier, value, type, loc);
13680 }
13681
13682 /* Parse a namespace-name.
13683
13684    namespace-name:
13685      original-namespace-name
13686      namespace-alias
13687
13688    Returns the NAMESPACE_DECL for the namespace.  */
13689
13690 static tree
13691 cp_parser_namespace_name (cp_parser* parser)
13692 {
13693   tree identifier;
13694   tree namespace_decl;
13695
13696   cp_token *token = cp_lexer_peek_token (parser->lexer);
13697
13698   /* Get the name of the namespace.  */
13699   identifier = cp_parser_identifier (parser);
13700   if (identifier == error_mark_node)
13701     return error_mark_node;
13702
13703   /* Look up the identifier in the currently active scope.  Look only
13704      for namespaces, due to:
13705
13706        [basic.lookup.udir]
13707
13708        When looking up a namespace-name in a using-directive or alias
13709        definition, only namespace names are considered.
13710
13711      And:
13712
13713        [basic.lookup.qual]
13714
13715        During the lookup of a name preceding the :: scope resolution
13716        operator, object, function, and enumerator names are ignored.
13717
13718      (Note that cp_parser_qualifying_entity only calls this
13719      function if the token after the name is the scope resolution
13720      operator.)  */
13721   namespace_decl = cp_parser_lookup_name (parser, identifier,
13722                                           none_type,
13723                                           /*is_template=*/false,
13724                                           /*is_namespace=*/true,
13725                                           /*check_dependency=*/true,
13726                                           /*ambiguous_decls=*/NULL,
13727                                           token->location);
13728   /* If it's not a namespace, issue an error.  */
13729   if (namespace_decl == error_mark_node
13730       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13731     {
13732       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13733         error_at (token->location, "%qD is not a namespace-name", identifier);
13734       cp_parser_error (parser, "expected namespace-name");
13735       namespace_decl = error_mark_node;
13736     }
13737
13738   return namespace_decl;
13739 }
13740
13741 /* Parse a namespace-definition.
13742
13743    namespace-definition:
13744      named-namespace-definition
13745      unnamed-namespace-definition
13746
13747    named-namespace-definition:
13748      original-namespace-definition
13749      extension-namespace-definition
13750
13751    original-namespace-definition:
13752      namespace identifier { namespace-body }
13753
13754    extension-namespace-definition:
13755      namespace original-namespace-name { namespace-body }
13756
13757    unnamed-namespace-definition:
13758      namespace { namespace-body } */
13759
13760 static void
13761 cp_parser_namespace_definition (cp_parser* parser)
13762 {
13763   tree identifier, attribs;
13764   bool has_visibility;
13765   bool is_inline;
13766
13767   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13768     {
13769       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13770       is_inline = true;
13771       cp_lexer_consume_token (parser->lexer);
13772     }
13773   else
13774     is_inline = false;
13775
13776   /* Look for the `namespace' keyword.  */
13777   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13778
13779   /* Get the name of the namespace.  We do not attempt to distinguish
13780      between an original-namespace-definition and an
13781      extension-namespace-definition at this point.  The semantic
13782      analysis routines are responsible for that.  */
13783   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13784     identifier = cp_parser_identifier (parser);
13785   else
13786     identifier = NULL_TREE;
13787
13788   /* Parse any specified attributes.  */
13789   attribs = cp_parser_attributes_opt (parser);
13790
13791   /* Look for the `{' to start the namespace.  */
13792   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13793   /* Start the namespace.  */
13794   push_namespace (identifier);
13795
13796   /* "inline namespace" is equivalent to a stub namespace definition
13797      followed by a strong using directive.  */
13798   if (is_inline)
13799     {
13800       tree name_space = current_namespace;
13801       /* Set up namespace association.  */
13802       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13803         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13804                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13805       /* Import the contents of the inline namespace.  */
13806       pop_namespace ();
13807       do_using_directive (name_space);
13808       push_namespace (identifier);
13809     }
13810
13811   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13812
13813   /* Parse the body of the namespace.  */
13814   cp_parser_namespace_body (parser);
13815
13816   if (has_visibility)
13817     pop_visibility (1);
13818
13819   /* Finish the namespace.  */
13820   pop_namespace ();
13821   /* Look for the final `}'.  */
13822   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13823 }
13824
13825 /* Parse a namespace-body.
13826
13827    namespace-body:
13828      declaration-seq [opt]  */
13829
13830 static void
13831 cp_parser_namespace_body (cp_parser* parser)
13832 {
13833   cp_parser_declaration_seq_opt (parser);
13834 }
13835
13836 /* Parse a namespace-alias-definition.
13837
13838    namespace-alias-definition:
13839      namespace identifier = qualified-namespace-specifier ;  */
13840
13841 static void
13842 cp_parser_namespace_alias_definition (cp_parser* parser)
13843 {
13844   tree identifier;
13845   tree namespace_specifier;
13846
13847   cp_token *token = cp_lexer_peek_token (parser->lexer);
13848
13849   /* Look for the `namespace' keyword.  */
13850   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13851   /* Look for the identifier.  */
13852   identifier = cp_parser_identifier (parser);
13853   if (identifier == error_mark_node)
13854     return;
13855   /* Look for the `=' token.  */
13856   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13857       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13858     {
13859       error_at (token->location, "%<namespace%> definition is not allowed here");
13860       /* Skip the definition.  */
13861       cp_lexer_consume_token (parser->lexer);
13862       if (cp_parser_skip_to_closing_brace (parser))
13863         cp_lexer_consume_token (parser->lexer);
13864       return;
13865     }
13866   cp_parser_require (parser, CPP_EQ, RT_EQ);
13867   /* Look for the qualified-namespace-specifier.  */
13868   namespace_specifier
13869     = cp_parser_qualified_namespace_specifier (parser);
13870   /* Look for the `;' token.  */
13871   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13872
13873   /* Register the alias in the symbol table.  */
13874   do_namespace_alias (identifier, namespace_specifier);
13875 }
13876
13877 /* Parse a qualified-namespace-specifier.
13878
13879    qualified-namespace-specifier:
13880      :: [opt] nested-name-specifier [opt] namespace-name
13881
13882    Returns a NAMESPACE_DECL corresponding to the specified
13883    namespace.  */
13884
13885 static tree
13886 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13887 {
13888   /* Look for the optional `::'.  */
13889   cp_parser_global_scope_opt (parser,
13890                               /*current_scope_valid_p=*/false);
13891
13892   /* Look for the optional nested-name-specifier.  */
13893   cp_parser_nested_name_specifier_opt (parser,
13894                                        /*typename_keyword_p=*/false,
13895                                        /*check_dependency_p=*/true,
13896                                        /*type_p=*/false,
13897                                        /*is_declaration=*/true);
13898
13899   return cp_parser_namespace_name (parser);
13900 }
13901
13902 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13903    access declaration.
13904
13905    using-declaration:
13906      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13907      using :: unqualified-id ;  
13908
13909    access-declaration:
13910      qualified-id ;  
13911
13912    */
13913
13914 static bool
13915 cp_parser_using_declaration (cp_parser* parser, 
13916                              bool access_declaration_p)
13917 {
13918   cp_token *token;
13919   bool typename_p = false;
13920   bool global_scope_p;
13921   tree decl;
13922   tree identifier;
13923   tree qscope;
13924
13925   if (access_declaration_p)
13926     cp_parser_parse_tentatively (parser);
13927   else
13928     {
13929       /* Look for the `using' keyword.  */
13930       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13931       
13932       /* Peek at the next token.  */
13933       token = cp_lexer_peek_token (parser->lexer);
13934       /* See if it's `typename'.  */
13935       if (token->keyword == RID_TYPENAME)
13936         {
13937           /* Remember that we've seen it.  */
13938           typename_p = true;
13939           /* Consume the `typename' token.  */
13940           cp_lexer_consume_token (parser->lexer);
13941         }
13942     }
13943
13944   /* Look for the optional global scope qualification.  */
13945   global_scope_p
13946     = (cp_parser_global_scope_opt (parser,
13947                                    /*current_scope_valid_p=*/false)
13948        != NULL_TREE);
13949
13950   /* If we saw `typename', or didn't see `::', then there must be a
13951      nested-name-specifier present.  */
13952   if (typename_p || !global_scope_p)
13953     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13954                                               /*check_dependency_p=*/true,
13955                                               /*type_p=*/false,
13956                                               /*is_declaration=*/true);
13957   /* Otherwise, we could be in either of the two productions.  In that
13958      case, treat the nested-name-specifier as optional.  */
13959   else
13960     qscope = cp_parser_nested_name_specifier_opt (parser,
13961                                                   /*typename_keyword_p=*/false,
13962                                                   /*check_dependency_p=*/true,
13963                                                   /*type_p=*/false,
13964                                                   /*is_declaration=*/true);
13965   if (!qscope)
13966     qscope = global_namespace;
13967
13968   if (access_declaration_p && cp_parser_error_occurred (parser))
13969     /* Something has already gone wrong; there's no need to parse
13970        further.  Since an error has occurred, the return value of
13971        cp_parser_parse_definitely will be false, as required.  */
13972     return cp_parser_parse_definitely (parser);
13973
13974   token = cp_lexer_peek_token (parser->lexer);
13975   /* Parse the unqualified-id.  */
13976   identifier = cp_parser_unqualified_id (parser,
13977                                          /*template_keyword_p=*/false,
13978                                          /*check_dependency_p=*/true,
13979                                          /*declarator_p=*/true,
13980                                          /*optional_p=*/false);
13981
13982   if (access_declaration_p)
13983     {
13984       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13985         cp_parser_simulate_error (parser);
13986       if (!cp_parser_parse_definitely (parser))
13987         return false;
13988     }
13989
13990   /* The function we call to handle a using-declaration is different
13991      depending on what scope we are in.  */
13992   if (qscope == error_mark_node || identifier == error_mark_node)
13993     ;
13994   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13995            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13996     /* [namespace.udecl]
13997
13998        A using declaration shall not name a template-id.  */
13999     error_at (token->location,
14000               "a template-id may not appear in a using-declaration");
14001   else
14002     {
14003       if (at_class_scope_p ())
14004         {
14005           /* Create the USING_DECL.  */
14006           decl = do_class_using_decl (parser->scope, identifier);
14007
14008           if (check_for_bare_parameter_packs (decl))
14009             return false;
14010           else
14011             /* Add it to the list of members in this class.  */
14012             finish_member_declaration (decl);
14013         }
14014       else
14015         {
14016           decl = cp_parser_lookup_name_simple (parser,
14017                                                identifier,
14018                                                token->location);
14019           if (decl == error_mark_node)
14020             cp_parser_name_lookup_error (parser, identifier,
14021                                          decl, NLE_NULL,
14022                                          token->location);
14023           else if (check_for_bare_parameter_packs (decl))
14024             return false;
14025           else if (!at_namespace_scope_p ())
14026             do_local_using_decl (decl, qscope, identifier);
14027           else
14028             do_toplevel_using_decl (decl, qscope, identifier);
14029         }
14030     }
14031
14032   /* Look for the final `;'.  */
14033   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14034   
14035   return true;
14036 }
14037
14038 /* Parse a using-directive.
14039
14040    using-directive:
14041      using namespace :: [opt] nested-name-specifier [opt]
14042        namespace-name ;  */
14043
14044 static void
14045 cp_parser_using_directive (cp_parser* parser)
14046 {
14047   tree namespace_decl;
14048   tree attribs;
14049
14050   /* Look for the `using' keyword.  */
14051   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14052   /* And the `namespace' keyword.  */
14053   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14054   /* Look for the optional `::' operator.  */
14055   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14056   /* And the optional nested-name-specifier.  */
14057   cp_parser_nested_name_specifier_opt (parser,
14058                                        /*typename_keyword_p=*/false,
14059                                        /*check_dependency_p=*/true,
14060                                        /*type_p=*/false,
14061                                        /*is_declaration=*/true);
14062   /* Get the namespace being used.  */
14063   namespace_decl = cp_parser_namespace_name (parser);
14064   /* And any specified attributes.  */
14065   attribs = cp_parser_attributes_opt (parser);
14066   /* Update the symbol table.  */
14067   parse_using_directive (namespace_decl, attribs);
14068   /* Look for the final `;'.  */
14069   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14070 }
14071
14072 /* Parse an asm-definition.
14073
14074    asm-definition:
14075      asm ( string-literal ) ;
14076
14077    GNU Extension:
14078
14079    asm-definition:
14080      asm volatile [opt] ( string-literal ) ;
14081      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14082      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14083                           : asm-operand-list [opt] ) ;
14084      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14085                           : asm-operand-list [opt]
14086                           : asm-clobber-list [opt] ) ;
14087      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14088                                : asm-clobber-list [opt]
14089                                : asm-goto-list ) ;  */
14090
14091 static void
14092 cp_parser_asm_definition (cp_parser* parser)
14093 {
14094   tree string;
14095   tree outputs = NULL_TREE;
14096   tree inputs = NULL_TREE;
14097   tree clobbers = NULL_TREE;
14098   tree labels = NULL_TREE;
14099   tree asm_stmt;
14100   bool volatile_p = false;
14101   bool extended_p = false;
14102   bool invalid_inputs_p = false;
14103   bool invalid_outputs_p = false;
14104   bool goto_p = false;
14105   required_token missing = RT_NONE;
14106
14107   /* Look for the `asm' keyword.  */
14108   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14109   /* See if the next token is `volatile'.  */
14110   if (cp_parser_allow_gnu_extensions_p (parser)
14111       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14112     {
14113       /* Remember that we saw the `volatile' keyword.  */
14114       volatile_p = true;
14115       /* Consume the token.  */
14116       cp_lexer_consume_token (parser->lexer);
14117     }
14118   if (cp_parser_allow_gnu_extensions_p (parser)
14119       && parser->in_function_body
14120       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14121     {
14122       /* Remember that we saw the `goto' keyword.  */
14123       goto_p = true;
14124       /* Consume the token.  */
14125       cp_lexer_consume_token (parser->lexer);
14126     }
14127   /* Look for the opening `('.  */
14128   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14129     return;
14130   /* Look for the string.  */
14131   string = cp_parser_string_literal (parser, false, false);
14132   if (string == error_mark_node)
14133     {
14134       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14135                                              /*consume_paren=*/true);
14136       return;
14137     }
14138
14139   /* If we're allowing GNU extensions, check for the extended assembly
14140      syntax.  Unfortunately, the `:' tokens need not be separated by
14141      a space in C, and so, for compatibility, we tolerate that here
14142      too.  Doing that means that we have to treat the `::' operator as
14143      two `:' tokens.  */
14144   if (cp_parser_allow_gnu_extensions_p (parser)
14145       && parser->in_function_body
14146       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14147           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14148     {
14149       bool inputs_p = false;
14150       bool clobbers_p = false;
14151       bool labels_p = false;
14152
14153       /* The extended syntax was used.  */
14154       extended_p = true;
14155
14156       /* Look for outputs.  */
14157       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14158         {
14159           /* Consume the `:'.  */
14160           cp_lexer_consume_token (parser->lexer);
14161           /* Parse the output-operands.  */
14162           if (cp_lexer_next_token_is_not (parser->lexer,
14163                                           CPP_COLON)
14164               && cp_lexer_next_token_is_not (parser->lexer,
14165                                              CPP_SCOPE)
14166               && cp_lexer_next_token_is_not (parser->lexer,
14167                                              CPP_CLOSE_PAREN)
14168               && !goto_p)
14169             outputs = cp_parser_asm_operand_list (parser);
14170
14171             if (outputs == error_mark_node)
14172               invalid_outputs_p = true;
14173         }
14174       /* If the next token is `::', there are no outputs, and the
14175          next token is the beginning of the inputs.  */
14176       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14177         /* The inputs are coming next.  */
14178         inputs_p = true;
14179
14180       /* Look for inputs.  */
14181       if (inputs_p
14182           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14183         {
14184           /* Consume the `:' or `::'.  */
14185           cp_lexer_consume_token (parser->lexer);
14186           /* Parse the output-operands.  */
14187           if (cp_lexer_next_token_is_not (parser->lexer,
14188                                           CPP_COLON)
14189               && cp_lexer_next_token_is_not (parser->lexer,
14190                                              CPP_SCOPE)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_CLOSE_PAREN))
14193             inputs = cp_parser_asm_operand_list (parser);
14194
14195             if (inputs == error_mark_node)
14196               invalid_inputs_p = true;
14197         }
14198       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14199         /* The clobbers are coming next.  */
14200         clobbers_p = true;
14201
14202       /* Look for clobbers.  */
14203       if (clobbers_p
14204           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14205         {
14206           clobbers_p = true;
14207           /* Consume the `:' or `::'.  */
14208           cp_lexer_consume_token (parser->lexer);
14209           /* Parse the clobbers.  */
14210           if (cp_lexer_next_token_is_not (parser->lexer,
14211                                           CPP_COLON)
14212               && cp_lexer_next_token_is_not (parser->lexer,
14213                                              CPP_CLOSE_PAREN))
14214             clobbers = cp_parser_asm_clobber_list (parser);
14215         }
14216       else if (goto_p
14217                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14218         /* The labels are coming next.  */
14219         labels_p = true;
14220
14221       /* Look for labels.  */
14222       if (labels_p
14223           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14224         {
14225           labels_p = true;
14226           /* Consume the `:' or `::'.  */
14227           cp_lexer_consume_token (parser->lexer);
14228           /* Parse the labels.  */
14229           labels = cp_parser_asm_label_list (parser);
14230         }
14231
14232       if (goto_p && !labels_p)
14233         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14234     }
14235   else if (goto_p)
14236     missing = RT_COLON_SCOPE;
14237
14238   /* Look for the closing `)'.  */
14239   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14240                           missing ? missing : RT_CLOSE_PAREN))
14241     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14242                                            /*consume_paren=*/true);
14243   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14244
14245   if (!invalid_inputs_p && !invalid_outputs_p)
14246     {
14247       /* Create the ASM_EXPR.  */
14248       if (parser->in_function_body)
14249         {
14250           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14251                                       inputs, clobbers, labels);
14252           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14253           if (!extended_p)
14254             {
14255               tree temp = asm_stmt;
14256               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14257                 temp = TREE_OPERAND (temp, 0);
14258
14259               ASM_INPUT_P (temp) = 1;
14260             }
14261         }
14262       else
14263         cgraph_add_asm_node (string);
14264     }
14265 }
14266
14267 /* Declarators [gram.dcl.decl] */
14268
14269 /* Parse an init-declarator.
14270
14271    init-declarator:
14272      declarator initializer [opt]
14273
14274    GNU Extension:
14275
14276    init-declarator:
14277      declarator asm-specification [opt] attributes [opt] initializer [opt]
14278
14279    function-definition:
14280      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14281        function-body
14282      decl-specifier-seq [opt] declarator function-try-block
14283
14284    GNU Extension:
14285
14286    function-definition:
14287      __extension__ function-definition
14288
14289    The DECL_SPECIFIERS apply to this declarator.  Returns a
14290    representation of the entity declared.  If MEMBER_P is TRUE, then
14291    this declarator appears in a class scope.  The new DECL created by
14292    this declarator is returned.
14293
14294    The CHECKS are access checks that should be performed once we know
14295    what entity is being declared (and, therefore, what classes have
14296    befriended it).
14297
14298    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14299    for a function-definition here as well.  If the declarator is a
14300    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14301    be TRUE upon return.  By that point, the function-definition will
14302    have been completely parsed.
14303
14304    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14305    is FALSE.
14306
14307    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14308    parsed declaration if it is an uninitialized single declarator not followed
14309    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14310    if present, will not be consumed.  If returned, this declarator will be
14311    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14312
14313 static tree
14314 cp_parser_init_declarator (cp_parser* parser,
14315                            cp_decl_specifier_seq *decl_specifiers,
14316                            VEC (deferred_access_check,gc)* checks,
14317                            bool function_definition_allowed_p,
14318                            bool member_p,
14319                            int declares_class_or_enum,
14320                            bool* function_definition_p,
14321                            tree* maybe_range_for_decl)
14322 {
14323   cp_token *token = NULL, *asm_spec_start_token = NULL,
14324            *attributes_start_token = NULL;
14325   cp_declarator *declarator;
14326   tree prefix_attributes;
14327   tree attributes;
14328   tree asm_specification;
14329   tree initializer;
14330   tree decl = NULL_TREE;
14331   tree scope;
14332   int is_initialized;
14333   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14334      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14335      "(...)".  */
14336   enum cpp_ttype initialization_kind;
14337   bool is_direct_init = false;
14338   bool is_non_constant_init;
14339   int ctor_dtor_or_conv_p;
14340   bool friend_p;
14341   tree pushed_scope = NULL_TREE;
14342   bool range_for_decl_p = false;
14343
14344   /* Gather the attributes that were provided with the
14345      decl-specifiers.  */
14346   prefix_attributes = decl_specifiers->attributes;
14347
14348   /* Assume that this is not the declarator for a function
14349      definition.  */
14350   if (function_definition_p)
14351     *function_definition_p = false;
14352
14353   /* Defer access checks while parsing the declarator; we cannot know
14354      what names are accessible until we know what is being
14355      declared.  */
14356   resume_deferring_access_checks ();
14357
14358   /* Parse the declarator.  */
14359   token = cp_lexer_peek_token (parser->lexer);
14360   declarator
14361     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14362                             &ctor_dtor_or_conv_p,
14363                             /*parenthesized_p=*/NULL,
14364                             /*member_p=*/false);
14365   /* Gather up the deferred checks.  */
14366   stop_deferring_access_checks ();
14367
14368   /* If the DECLARATOR was erroneous, there's no need to go
14369      further.  */
14370   if (declarator == cp_error_declarator)
14371     return error_mark_node;
14372
14373   /* Check that the number of template-parameter-lists is OK.  */
14374   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14375                                                        token->location))
14376     return error_mark_node;
14377
14378   if (declares_class_or_enum & 2)
14379     cp_parser_check_for_definition_in_return_type (declarator,
14380                                                    decl_specifiers->type,
14381                                                    decl_specifiers->type_location);
14382
14383   /* Figure out what scope the entity declared by the DECLARATOR is
14384      located in.  `grokdeclarator' sometimes changes the scope, so
14385      we compute it now.  */
14386   scope = get_scope_of_declarator (declarator);
14387
14388   /* Perform any lookups in the declared type which were thought to be
14389      dependent, but are not in the scope of the declarator.  */
14390   decl_specifiers->type
14391     = maybe_update_decl_type (decl_specifiers->type, scope);
14392
14393   /* If we're allowing GNU extensions, look for an asm-specification
14394      and attributes.  */
14395   if (cp_parser_allow_gnu_extensions_p (parser))
14396     {
14397       /* Look for an asm-specification.  */
14398       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14399       asm_specification = cp_parser_asm_specification_opt (parser);
14400       /* And attributes.  */
14401       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14402       attributes = cp_parser_attributes_opt (parser);
14403     }
14404   else
14405     {
14406       asm_specification = NULL_TREE;
14407       attributes = NULL_TREE;
14408     }
14409
14410   /* Peek at the next token.  */
14411   token = cp_lexer_peek_token (parser->lexer);
14412   /* Check to see if the token indicates the start of a
14413      function-definition.  */
14414   if (function_declarator_p (declarator)
14415       && cp_parser_token_starts_function_definition_p (token))
14416     {
14417       if (!function_definition_allowed_p)
14418         {
14419           /* If a function-definition should not appear here, issue an
14420              error message.  */
14421           cp_parser_error (parser,
14422                            "a function-definition is not allowed here");
14423           return error_mark_node;
14424         }
14425       else
14426         {
14427           location_t func_brace_location
14428             = cp_lexer_peek_token (parser->lexer)->location;
14429
14430           /* Neither attributes nor an asm-specification are allowed
14431              on a function-definition.  */
14432           if (asm_specification)
14433             error_at (asm_spec_start_token->location,
14434                       "an asm-specification is not allowed "
14435                       "on a function-definition");
14436           if (attributes)
14437             error_at (attributes_start_token->location,
14438                       "attributes are not allowed on a function-definition");
14439           /* This is a function-definition.  */
14440           *function_definition_p = true;
14441
14442           /* Parse the function definition.  */
14443           if (member_p)
14444             decl = cp_parser_save_member_function_body (parser,
14445                                                         decl_specifiers,
14446                                                         declarator,
14447                                                         prefix_attributes);
14448           else
14449             decl
14450               = (cp_parser_function_definition_from_specifiers_and_declarator
14451                  (parser, decl_specifiers, prefix_attributes, declarator));
14452
14453           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14454             {
14455               /* This is where the prologue starts...  */
14456               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14457                 = func_brace_location;
14458             }
14459
14460           return decl;
14461         }
14462     }
14463
14464   /* [dcl.dcl]
14465
14466      Only in function declarations for constructors, destructors, and
14467      type conversions can the decl-specifier-seq be omitted.
14468
14469      We explicitly postpone this check past the point where we handle
14470      function-definitions because we tolerate function-definitions
14471      that are missing their return types in some modes.  */
14472   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14473     {
14474       cp_parser_error (parser,
14475                        "expected constructor, destructor, or type conversion");
14476       return error_mark_node;
14477     }
14478
14479   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14480   if (token->type == CPP_EQ
14481       || token->type == CPP_OPEN_PAREN
14482       || token->type == CPP_OPEN_BRACE)
14483     {
14484       is_initialized = SD_INITIALIZED;
14485       initialization_kind = token->type;
14486       if (maybe_range_for_decl)
14487         *maybe_range_for_decl = error_mark_node;
14488
14489       if (token->type == CPP_EQ
14490           && function_declarator_p (declarator))
14491         {
14492           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14493           if (t2->keyword == RID_DEFAULT)
14494             is_initialized = SD_DEFAULTED;
14495           else if (t2->keyword == RID_DELETE)
14496             is_initialized = SD_DELETED;
14497         }
14498     }
14499   else
14500     {
14501       /* If the init-declarator isn't initialized and isn't followed by a
14502          `,' or `;', it's not a valid init-declarator.  */
14503       if (token->type != CPP_COMMA
14504           && token->type != CPP_SEMICOLON)
14505         {
14506           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14507             range_for_decl_p = true;
14508           else
14509             {
14510               cp_parser_error (parser, "expected initializer");
14511               return error_mark_node;
14512             }
14513         }
14514       is_initialized = SD_UNINITIALIZED;
14515       initialization_kind = CPP_EOF;
14516     }
14517
14518   /* Because start_decl has side-effects, we should only call it if we
14519      know we're going ahead.  By this point, we know that we cannot
14520      possibly be looking at any other construct.  */
14521   cp_parser_commit_to_tentative_parse (parser);
14522
14523   /* If the decl specifiers were bad, issue an error now that we're
14524      sure this was intended to be a declarator.  Then continue
14525      declaring the variable(s), as int, to try to cut down on further
14526      errors.  */
14527   if (decl_specifiers->any_specifiers_p
14528       && decl_specifiers->type == error_mark_node)
14529     {
14530       cp_parser_error (parser, "invalid type in declaration");
14531       decl_specifiers->type = integer_type_node;
14532     }
14533
14534   /* Check to see whether or not this declaration is a friend.  */
14535   friend_p = cp_parser_friend_p (decl_specifiers);
14536
14537   /* Enter the newly declared entry in the symbol table.  If we're
14538      processing a declaration in a class-specifier, we wait until
14539      after processing the initializer.  */
14540   if (!member_p)
14541     {
14542       if (parser->in_unbraced_linkage_specification_p)
14543         decl_specifiers->storage_class = sc_extern;
14544       decl = start_decl (declarator, decl_specifiers,
14545                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14546                          attributes, prefix_attributes,
14547                          &pushed_scope);
14548       /* Adjust location of decl if declarator->id_loc is more appropriate:
14549          set, and decl wasn't merged with another decl, in which case its
14550          location would be different from input_location, and more accurate.  */
14551       if (DECL_P (decl)
14552           && declarator->id_loc != UNKNOWN_LOCATION
14553           && DECL_SOURCE_LOCATION (decl) == input_location)
14554         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14555     }
14556   else if (scope)
14557     /* Enter the SCOPE.  That way unqualified names appearing in the
14558        initializer will be looked up in SCOPE.  */
14559     pushed_scope = push_scope (scope);
14560
14561   /* Perform deferred access control checks, now that we know in which
14562      SCOPE the declared entity resides.  */
14563   if (!member_p && decl)
14564     {
14565       tree saved_current_function_decl = NULL_TREE;
14566
14567       /* If the entity being declared is a function, pretend that we
14568          are in its scope.  If it is a `friend', it may have access to
14569          things that would not otherwise be accessible.  */
14570       if (TREE_CODE (decl) == FUNCTION_DECL)
14571         {
14572           saved_current_function_decl = current_function_decl;
14573           current_function_decl = decl;
14574         }
14575
14576       /* Perform access checks for template parameters.  */
14577       cp_parser_perform_template_parameter_access_checks (checks);
14578
14579       /* Perform the access control checks for the declarator and the
14580          decl-specifiers.  */
14581       perform_deferred_access_checks ();
14582
14583       /* Restore the saved value.  */
14584       if (TREE_CODE (decl) == FUNCTION_DECL)
14585         current_function_decl = saved_current_function_decl;
14586     }
14587
14588   /* Parse the initializer.  */
14589   initializer = NULL_TREE;
14590   is_direct_init = false;
14591   is_non_constant_init = true;
14592   if (is_initialized)
14593     {
14594       if (function_declarator_p (declarator))
14595         {
14596           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14597            if (initialization_kind == CPP_EQ)
14598              initializer = cp_parser_pure_specifier (parser);
14599            else
14600              {
14601                /* If the declaration was erroneous, we don't really
14602                   know what the user intended, so just silently
14603                   consume the initializer.  */
14604                if (decl != error_mark_node)
14605                  error_at (initializer_start_token->location,
14606                            "initializer provided for function");
14607                cp_parser_skip_to_closing_parenthesis (parser,
14608                                                       /*recovering=*/true,
14609                                                       /*or_comma=*/false,
14610                                                       /*consume_paren=*/true);
14611              }
14612         }
14613       else
14614         {
14615           /* We want to record the extra mangling scope for in-class
14616              initializers of class members and initializers of static data
14617              member templates.  The former is a C++0x feature which isn't
14618              implemented yet, and I expect it will involve deferring
14619              parsing of the initializer until end of class as with default
14620              arguments.  So right here we only handle the latter.  */
14621           if (!member_p && processing_template_decl)
14622             start_lambda_scope (decl);
14623           initializer = cp_parser_initializer (parser,
14624                                                &is_direct_init,
14625                                                &is_non_constant_init);
14626           if (!member_p && processing_template_decl)
14627             finish_lambda_scope ();
14628         }
14629     }
14630
14631   /* The old parser allows attributes to appear after a parenthesized
14632      initializer.  Mark Mitchell proposed removing this functionality
14633      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14634      attributes -- but ignores them.  */
14635   if (cp_parser_allow_gnu_extensions_p (parser)
14636       && initialization_kind == CPP_OPEN_PAREN)
14637     if (cp_parser_attributes_opt (parser))
14638       warning (OPT_Wattributes,
14639                "attributes after parenthesized initializer ignored");
14640
14641   /* For an in-class declaration, use `grokfield' to create the
14642      declaration.  */
14643   if (member_p)
14644     {
14645       if (pushed_scope)
14646         {
14647           pop_scope (pushed_scope);
14648           pushed_scope = NULL_TREE;
14649         }
14650       decl = grokfield (declarator, decl_specifiers,
14651                         initializer, !is_non_constant_init,
14652                         /*asmspec=*/NULL_TREE,
14653                         prefix_attributes);
14654       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14655         cp_parser_save_default_args (parser, decl);
14656     }
14657
14658   /* Finish processing the declaration.  But, skip member
14659      declarations.  */
14660   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14661     {
14662       cp_finish_decl (decl,
14663                       initializer, !is_non_constant_init,
14664                       asm_specification,
14665                       /* If the initializer is in parentheses, then this is
14666                          a direct-initialization, which means that an
14667                          `explicit' constructor is OK.  Otherwise, an
14668                          `explicit' constructor cannot be used.  */
14669                       ((is_direct_init || !is_initialized)
14670                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14671     }
14672   else if ((cxx_dialect != cxx98) && friend_p
14673            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14674     /* Core issue #226 (C++0x only): A default template-argument
14675        shall not be specified in a friend class template
14676        declaration. */
14677     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14678                              /*is_partial=*/0, /*is_friend_decl=*/1);
14679
14680   if (!friend_p && pushed_scope)
14681     pop_scope (pushed_scope);
14682
14683   return decl;
14684 }
14685
14686 /* Parse a declarator.
14687
14688    declarator:
14689      direct-declarator
14690      ptr-operator declarator
14691
14692    abstract-declarator:
14693      ptr-operator abstract-declarator [opt]
14694      direct-abstract-declarator
14695
14696    GNU Extensions:
14697
14698    declarator:
14699      attributes [opt] direct-declarator
14700      attributes [opt] ptr-operator declarator
14701
14702    abstract-declarator:
14703      attributes [opt] ptr-operator abstract-declarator [opt]
14704      attributes [opt] direct-abstract-declarator
14705
14706    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14707    detect constructor, destructor or conversion operators. It is set
14708    to -1 if the declarator is a name, and +1 if it is a
14709    function. Otherwise it is set to zero. Usually you just want to
14710    test for >0, but internally the negative value is used.
14711
14712    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14713    a decl-specifier-seq unless it declares a constructor, destructor,
14714    or conversion.  It might seem that we could check this condition in
14715    semantic analysis, rather than parsing, but that makes it difficult
14716    to handle something like `f()'.  We want to notice that there are
14717    no decl-specifiers, and therefore realize that this is an
14718    expression, not a declaration.)
14719
14720    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14721    the declarator is a direct-declarator of the form "(...)".
14722
14723    MEMBER_P is true iff this declarator is a member-declarator.  */
14724
14725 static cp_declarator *
14726 cp_parser_declarator (cp_parser* parser,
14727                       cp_parser_declarator_kind dcl_kind,
14728                       int* ctor_dtor_or_conv_p,
14729                       bool* parenthesized_p,
14730                       bool member_p)
14731 {
14732   cp_declarator *declarator;
14733   enum tree_code code;
14734   cp_cv_quals cv_quals;
14735   tree class_type;
14736   tree attributes = NULL_TREE;
14737
14738   /* Assume this is not a constructor, destructor, or type-conversion
14739      operator.  */
14740   if (ctor_dtor_or_conv_p)
14741     *ctor_dtor_or_conv_p = 0;
14742
14743   if (cp_parser_allow_gnu_extensions_p (parser))
14744     attributes = cp_parser_attributes_opt (parser);
14745
14746   /* Check for the ptr-operator production.  */
14747   cp_parser_parse_tentatively (parser);
14748   /* Parse the ptr-operator.  */
14749   code = cp_parser_ptr_operator (parser,
14750                                  &class_type,
14751                                  &cv_quals);
14752   /* If that worked, then we have a ptr-operator.  */
14753   if (cp_parser_parse_definitely (parser))
14754     {
14755       /* If a ptr-operator was found, then this declarator was not
14756          parenthesized.  */
14757       if (parenthesized_p)
14758         *parenthesized_p = true;
14759       /* The dependent declarator is optional if we are parsing an
14760          abstract-declarator.  */
14761       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14762         cp_parser_parse_tentatively (parser);
14763
14764       /* Parse the dependent declarator.  */
14765       declarator = cp_parser_declarator (parser, dcl_kind,
14766                                          /*ctor_dtor_or_conv_p=*/NULL,
14767                                          /*parenthesized_p=*/NULL,
14768                                          /*member_p=*/false);
14769
14770       /* If we are parsing an abstract-declarator, we must handle the
14771          case where the dependent declarator is absent.  */
14772       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14773           && !cp_parser_parse_definitely (parser))
14774         declarator = NULL;
14775
14776       declarator = cp_parser_make_indirect_declarator
14777         (code, class_type, cv_quals, declarator);
14778     }
14779   /* Everything else is a direct-declarator.  */
14780   else
14781     {
14782       if (parenthesized_p)
14783         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14784                                                    CPP_OPEN_PAREN);
14785       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14786                                                 ctor_dtor_or_conv_p,
14787                                                 member_p);
14788     }
14789
14790   if (attributes && declarator && declarator != cp_error_declarator)
14791     declarator->attributes = attributes;
14792
14793   return declarator;
14794 }
14795
14796 /* Parse a direct-declarator or direct-abstract-declarator.
14797
14798    direct-declarator:
14799      declarator-id
14800      direct-declarator ( parameter-declaration-clause )
14801        cv-qualifier-seq [opt]
14802        exception-specification [opt]
14803      direct-declarator [ constant-expression [opt] ]
14804      ( declarator )
14805
14806    direct-abstract-declarator:
14807      direct-abstract-declarator [opt]
14808        ( parameter-declaration-clause )
14809        cv-qualifier-seq [opt]
14810        exception-specification [opt]
14811      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14812      ( abstract-declarator )
14813
14814    Returns a representation of the declarator.  DCL_KIND is
14815    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14816    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14817    we are parsing a direct-declarator.  It is
14818    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14819    of ambiguity we prefer an abstract declarator, as per
14820    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14821    cp_parser_declarator.  */
14822
14823 static cp_declarator *
14824 cp_parser_direct_declarator (cp_parser* parser,
14825                              cp_parser_declarator_kind dcl_kind,
14826                              int* ctor_dtor_or_conv_p,
14827                              bool member_p)
14828 {
14829   cp_token *token;
14830   cp_declarator *declarator = NULL;
14831   tree scope = NULL_TREE;
14832   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14833   bool saved_in_declarator_p = parser->in_declarator_p;
14834   bool first = true;
14835   tree pushed_scope = NULL_TREE;
14836
14837   while (true)
14838     {
14839       /* Peek at the next token.  */
14840       token = cp_lexer_peek_token (parser->lexer);
14841       if (token->type == CPP_OPEN_PAREN)
14842         {
14843           /* This is either a parameter-declaration-clause, or a
14844              parenthesized declarator. When we know we are parsing a
14845              named declarator, it must be a parenthesized declarator
14846              if FIRST is true. For instance, `(int)' is a
14847              parameter-declaration-clause, with an omitted
14848              direct-abstract-declarator. But `((*))', is a
14849              parenthesized abstract declarator. Finally, when T is a
14850              template parameter `(T)' is a
14851              parameter-declaration-clause, and not a parenthesized
14852              named declarator.
14853
14854              We first try and parse a parameter-declaration-clause,
14855              and then try a nested declarator (if FIRST is true).
14856
14857              It is not an error for it not to be a
14858              parameter-declaration-clause, even when FIRST is
14859              false. Consider,
14860
14861                int i (int);
14862                int i (3);
14863
14864              The first is the declaration of a function while the
14865              second is the definition of a variable, including its
14866              initializer.
14867
14868              Having seen only the parenthesis, we cannot know which of
14869              these two alternatives should be selected.  Even more
14870              complex are examples like:
14871
14872                int i (int (a));
14873                int i (int (3));
14874
14875              The former is a function-declaration; the latter is a
14876              variable initialization.
14877
14878              Thus again, we try a parameter-declaration-clause, and if
14879              that fails, we back out and return.  */
14880
14881           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14882             {
14883               tree params;
14884               unsigned saved_num_template_parameter_lists;
14885               bool is_declarator = false;
14886               tree t;
14887
14888               /* In a member-declarator, the only valid interpretation
14889                  of a parenthesis is the start of a
14890                  parameter-declaration-clause.  (It is invalid to
14891                  initialize a static data member with a parenthesized
14892                  initializer; only the "=" form of initialization is
14893                  permitted.)  */
14894               if (!member_p)
14895                 cp_parser_parse_tentatively (parser);
14896
14897               /* Consume the `('.  */
14898               cp_lexer_consume_token (parser->lexer);
14899               if (first)
14900                 {
14901                   /* If this is going to be an abstract declarator, we're
14902                      in a declarator and we can't have default args.  */
14903                   parser->default_arg_ok_p = false;
14904                   parser->in_declarator_p = true;
14905                 }
14906
14907               /* Inside the function parameter list, surrounding
14908                  template-parameter-lists do not apply.  */
14909               saved_num_template_parameter_lists
14910                 = parser->num_template_parameter_lists;
14911               parser->num_template_parameter_lists = 0;
14912
14913               begin_scope (sk_function_parms, NULL_TREE);
14914
14915               /* Parse the parameter-declaration-clause.  */
14916               params = cp_parser_parameter_declaration_clause (parser);
14917
14918               parser->num_template_parameter_lists
14919                 = saved_num_template_parameter_lists;
14920
14921               /* Consume the `)'.  */
14922               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14923
14924               /* If all went well, parse the cv-qualifier-seq and the
14925                  exception-specification.  */
14926               if (member_p || cp_parser_parse_definitely (parser))
14927                 {
14928                   cp_cv_quals cv_quals;
14929                   cp_virt_specifiers virt_specifiers;
14930                   tree exception_specification;
14931                   tree late_return;
14932
14933                   is_declarator = true;
14934
14935                   if (ctor_dtor_or_conv_p)
14936                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14937                   first = false;
14938
14939                   /* Parse the cv-qualifier-seq.  */
14940                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14941                   /* And the exception-specification.  */
14942                   exception_specification
14943                     = cp_parser_exception_specification_opt (parser);
14944                   /* Parse the virt-specifier-seq.  */
14945                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14946
14947                   late_return
14948                     = cp_parser_late_return_type_opt (parser);
14949
14950                   /* Create the function-declarator.  */
14951                   declarator = make_call_declarator (declarator,
14952                                                      params,
14953                                                      cv_quals,
14954                                                      virt_specifiers,
14955                                                      exception_specification,
14956                                                      late_return);
14957                   /* Any subsequent parameter lists are to do with
14958                      return type, so are not those of the declared
14959                      function.  */
14960                   parser->default_arg_ok_p = false;
14961                 }
14962
14963               /* Remove the function parms from scope.  */
14964               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14965                 pop_binding (DECL_NAME (t), t);
14966               leave_scope();
14967
14968               if (is_declarator)
14969                 /* Repeat the main loop.  */
14970                 continue;
14971             }
14972
14973           /* If this is the first, we can try a parenthesized
14974              declarator.  */
14975           if (first)
14976             {
14977               bool saved_in_type_id_in_expr_p;
14978
14979               parser->default_arg_ok_p = saved_default_arg_ok_p;
14980               parser->in_declarator_p = saved_in_declarator_p;
14981
14982               /* Consume the `('.  */
14983               cp_lexer_consume_token (parser->lexer);
14984               /* Parse the nested declarator.  */
14985               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14986               parser->in_type_id_in_expr_p = true;
14987               declarator
14988                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14989                                         /*parenthesized_p=*/NULL,
14990                                         member_p);
14991               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14992               first = false;
14993               /* Expect a `)'.  */
14994               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14995                 declarator = cp_error_declarator;
14996               if (declarator == cp_error_declarator)
14997                 break;
14998
14999               goto handle_declarator;
15000             }
15001           /* Otherwise, we must be done.  */
15002           else
15003             break;
15004         }
15005       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15006                && token->type == CPP_OPEN_SQUARE)
15007         {
15008           /* Parse an array-declarator.  */
15009           tree bounds;
15010
15011           if (ctor_dtor_or_conv_p)
15012             *ctor_dtor_or_conv_p = 0;
15013
15014           first = false;
15015           parser->default_arg_ok_p = false;
15016           parser->in_declarator_p = true;
15017           /* Consume the `['.  */
15018           cp_lexer_consume_token (parser->lexer);
15019           /* Peek at the next token.  */
15020           token = cp_lexer_peek_token (parser->lexer);
15021           /* If the next token is `]', then there is no
15022              constant-expression.  */
15023           if (token->type != CPP_CLOSE_SQUARE)
15024             {
15025               bool non_constant_p;
15026
15027               bounds
15028                 = cp_parser_constant_expression (parser,
15029                                                  /*allow_non_constant=*/true,
15030                                                  &non_constant_p);
15031               if (!non_constant_p)
15032                 /* OK */;
15033               /* Normally, the array bound must be an integral constant
15034                  expression.  However, as an extension, we allow VLAs
15035                  in function scopes as long as they aren't part of a
15036                  parameter declaration.  */
15037               else if (!parser->in_function_body
15038                        || current_binding_level->kind == sk_function_parms)
15039                 {
15040                   cp_parser_error (parser,
15041                                    "array bound is not an integer constant");
15042                   bounds = error_mark_node;
15043                 }
15044               else if (processing_template_decl && !error_operand_p (bounds))
15045                 {
15046                   /* Remember this wasn't a constant-expression.  */
15047                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15048                   TREE_SIDE_EFFECTS (bounds) = 1;
15049                 }
15050             }
15051           else
15052             bounds = NULL_TREE;
15053           /* Look for the closing `]'.  */
15054           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15055             {
15056               declarator = cp_error_declarator;
15057               break;
15058             }
15059
15060           declarator = make_array_declarator (declarator, bounds);
15061         }
15062       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15063         {
15064           {
15065             tree qualifying_scope;
15066             tree unqualified_name;
15067             special_function_kind sfk;
15068             bool abstract_ok;
15069             bool pack_expansion_p = false;
15070             cp_token *declarator_id_start_token;
15071
15072             /* Parse a declarator-id */
15073             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15074             if (abstract_ok)
15075               {
15076                 cp_parser_parse_tentatively (parser);
15077
15078                 /* If we see an ellipsis, we should be looking at a
15079                    parameter pack. */
15080                 if (token->type == CPP_ELLIPSIS)
15081                   {
15082                     /* Consume the `...' */
15083                     cp_lexer_consume_token (parser->lexer);
15084
15085                     pack_expansion_p = true;
15086                   }
15087               }
15088
15089             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15090             unqualified_name
15091               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15092             qualifying_scope = parser->scope;
15093             if (abstract_ok)
15094               {
15095                 bool okay = false;
15096
15097                 if (!unqualified_name && pack_expansion_p)
15098                   {
15099                     /* Check whether an error occurred. */
15100                     okay = !cp_parser_error_occurred (parser);
15101
15102                     /* We already consumed the ellipsis to mark a
15103                        parameter pack, but we have no way to report it,
15104                        so abort the tentative parse. We will be exiting
15105                        immediately anyway. */
15106                     cp_parser_abort_tentative_parse (parser);
15107                   }
15108                 else
15109                   okay = cp_parser_parse_definitely (parser);
15110
15111                 if (!okay)
15112                   unqualified_name = error_mark_node;
15113                 else if (unqualified_name
15114                          && (qualifying_scope
15115                              || (TREE_CODE (unqualified_name)
15116                                  != IDENTIFIER_NODE)))
15117                   {
15118                     cp_parser_error (parser, "expected unqualified-id");
15119                     unqualified_name = error_mark_node;
15120                   }
15121               }
15122
15123             if (!unqualified_name)
15124               return NULL;
15125             if (unqualified_name == error_mark_node)
15126               {
15127                 declarator = cp_error_declarator;
15128                 pack_expansion_p = false;
15129                 declarator->parameter_pack_p = false;
15130                 break;
15131               }
15132
15133             if (qualifying_scope && at_namespace_scope_p ()
15134                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15135               {
15136                 /* In the declaration of a member of a template class
15137                    outside of the class itself, the SCOPE will sometimes
15138                    be a TYPENAME_TYPE.  For example, given:
15139
15140                    template <typename T>
15141                    int S<T>::R::i = 3;
15142
15143                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15144                    this context, we must resolve S<T>::R to an ordinary
15145                    type, rather than a typename type.
15146
15147                    The reason we normally avoid resolving TYPENAME_TYPEs
15148                    is that a specialization of `S' might render
15149                    `S<T>::R' not a type.  However, if `S' is
15150                    specialized, then this `i' will not be used, so there
15151                    is no harm in resolving the types here.  */
15152                 tree type;
15153
15154                 /* Resolve the TYPENAME_TYPE.  */
15155                 type = resolve_typename_type (qualifying_scope,
15156                                               /*only_current_p=*/false);
15157                 /* If that failed, the declarator is invalid.  */
15158                 if (TREE_CODE (type) == TYPENAME_TYPE)
15159                   {
15160                     if (typedef_variant_p (type))
15161                       error_at (declarator_id_start_token->location,
15162                                 "cannot define member of dependent typedef "
15163                                 "%qT", type);
15164                     else
15165                       error_at (declarator_id_start_token->location,
15166                                 "%<%T::%E%> is not a type",
15167                                 TYPE_CONTEXT (qualifying_scope),
15168                                 TYPE_IDENTIFIER (qualifying_scope));
15169                   }
15170                 qualifying_scope = type;
15171               }
15172
15173             sfk = sfk_none;
15174
15175             if (unqualified_name)
15176               {
15177                 tree class_type;
15178
15179                 if (qualifying_scope
15180                     && CLASS_TYPE_P (qualifying_scope))
15181                   class_type = qualifying_scope;
15182                 else
15183                   class_type = current_class_type;
15184
15185                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15186                   {
15187                     tree name_type = TREE_TYPE (unqualified_name);
15188                     if (class_type && same_type_p (name_type, class_type))
15189                       {
15190                         if (qualifying_scope
15191                             && CLASSTYPE_USE_TEMPLATE (name_type))
15192                           {
15193                             error_at (declarator_id_start_token->location,
15194                                       "invalid use of constructor as a template");
15195                             inform (declarator_id_start_token->location,
15196                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15197                                     "name the constructor in a qualified name",
15198                                     class_type,
15199                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15200                                     class_type, name_type);
15201                             declarator = cp_error_declarator;
15202                             break;
15203                           }
15204                         else
15205                           unqualified_name = constructor_name (class_type);
15206                       }
15207                     else
15208                       {
15209                         /* We do not attempt to print the declarator
15210                            here because we do not have enough
15211                            information about its original syntactic
15212                            form.  */
15213                         cp_parser_error (parser, "invalid declarator");
15214                         declarator = cp_error_declarator;
15215                         break;
15216                       }
15217                   }
15218
15219                 if (class_type)
15220                   {
15221                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15222                       sfk = sfk_destructor;
15223                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15224                       sfk = sfk_conversion;
15225                     else if (/* There's no way to declare a constructor
15226                                 for an anonymous type, even if the type
15227                                 got a name for linkage purposes.  */
15228                              !TYPE_WAS_ANONYMOUS (class_type)
15229                              && constructor_name_p (unqualified_name,
15230                                                     class_type))
15231                       {
15232                         unqualified_name = constructor_name (class_type);
15233                         sfk = sfk_constructor;
15234                       }
15235                     else if (is_overloaded_fn (unqualified_name)
15236                              && DECL_CONSTRUCTOR_P (get_first_fn
15237                                                     (unqualified_name)))
15238                       sfk = sfk_constructor;
15239
15240                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15241                       *ctor_dtor_or_conv_p = -1;
15242                   }
15243               }
15244             declarator = make_id_declarator (qualifying_scope,
15245                                              unqualified_name,
15246                                              sfk);
15247             declarator->id_loc = token->location;
15248             declarator->parameter_pack_p = pack_expansion_p;
15249
15250             if (pack_expansion_p)
15251               maybe_warn_variadic_templates ();
15252           }
15253
15254         handle_declarator:;
15255           scope = get_scope_of_declarator (declarator);
15256           if (scope)
15257             /* Any names that appear after the declarator-id for a
15258                member are looked up in the containing scope.  */
15259             pushed_scope = push_scope (scope);
15260           parser->in_declarator_p = true;
15261           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15262               || (declarator && declarator->kind == cdk_id))
15263             /* Default args are only allowed on function
15264                declarations.  */
15265             parser->default_arg_ok_p = saved_default_arg_ok_p;
15266           else
15267             parser->default_arg_ok_p = false;
15268
15269           first = false;
15270         }
15271       /* We're done.  */
15272       else
15273         break;
15274     }
15275
15276   /* For an abstract declarator, we might wind up with nothing at this
15277      point.  That's an error; the declarator is not optional.  */
15278   if (!declarator)
15279     cp_parser_error (parser, "expected declarator");
15280
15281   /* If we entered a scope, we must exit it now.  */
15282   if (pushed_scope)
15283     pop_scope (pushed_scope);
15284
15285   parser->default_arg_ok_p = saved_default_arg_ok_p;
15286   parser->in_declarator_p = saved_in_declarator_p;
15287
15288   return declarator;
15289 }
15290
15291 /* Parse a ptr-operator.
15292
15293    ptr-operator:
15294      * cv-qualifier-seq [opt]
15295      &
15296      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15297
15298    GNU Extension:
15299
15300    ptr-operator:
15301      & cv-qualifier-seq [opt]
15302
15303    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15304    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15305    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15306    filled in with the TYPE containing the member.  *CV_QUALS is
15307    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15308    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15309    Note that the tree codes returned by this function have nothing
15310    to do with the types of trees that will be eventually be created
15311    to represent the pointer or reference type being parsed. They are
15312    just constants with suggestive names. */
15313 static enum tree_code
15314 cp_parser_ptr_operator (cp_parser* parser,
15315                         tree* type,
15316                         cp_cv_quals *cv_quals)
15317 {
15318   enum tree_code code = ERROR_MARK;
15319   cp_token *token;
15320
15321   /* Assume that it's not a pointer-to-member.  */
15322   *type = NULL_TREE;
15323   /* And that there are no cv-qualifiers.  */
15324   *cv_quals = TYPE_UNQUALIFIED;
15325
15326   /* Peek at the next token.  */
15327   token = cp_lexer_peek_token (parser->lexer);
15328
15329   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15330   if (token->type == CPP_MULT)
15331     code = INDIRECT_REF;
15332   else if (token->type == CPP_AND)
15333     code = ADDR_EXPR;
15334   else if ((cxx_dialect != cxx98) &&
15335            token->type == CPP_AND_AND) /* C++0x only */
15336     code = NON_LVALUE_EXPR;
15337
15338   if (code != ERROR_MARK)
15339     {
15340       /* Consume the `*', `&' or `&&'.  */
15341       cp_lexer_consume_token (parser->lexer);
15342
15343       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15344          `&', if we are allowing GNU extensions.  (The only qualifier
15345          that can legally appear after `&' is `restrict', but that is
15346          enforced during semantic analysis.  */
15347       if (code == INDIRECT_REF
15348           || cp_parser_allow_gnu_extensions_p (parser))
15349         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15350     }
15351   else
15352     {
15353       /* Try the pointer-to-member case.  */
15354       cp_parser_parse_tentatively (parser);
15355       /* Look for the optional `::' operator.  */
15356       cp_parser_global_scope_opt (parser,
15357                                   /*current_scope_valid_p=*/false);
15358       /* Look for the nested-name specifier.  */
15359       token = cp_lexer_peek_token (parser->lexer);
15360       cp_parser_nested_name_specifier (parser,
15361                                        /*typename_keyword_p=*/false,
15362                                        /*check_dependency_p=*/true,
15363                                        /*type_p=*/false,
15364                                        /*is_declaration=*/false);
15365       /* If we found it, and the next token is a `*', then we are
15366          indeed looking at a pointer-to-member operator.  */
15367       if (!cp_parser_error_occurred (parser)
15368           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15369         {
15370           /* Indicate that the `*' operator was used.  */
15371           code = INDIRECT_REF;
15372
15373           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15374             error_at (token->location, "%qD is a namespace", parser->scope);
15375           else
15376             {
15377               /* The type of which the member is a member is given by the
15378                  current SCOPE.  */
15379               *type = parser->scope;
15380               /* The next name will not be qualified.  */
15381               parser->scope = NULL_TREE;
15382               parser->qualifying_scope = NULL_TREE;
15383               parser->object_scope = NULL_TREE;
15384               /* Look for the optional cv-qualifier-seq.  */
15385               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15386             }
15387         }
15388       /* If that didn't work we don't have a ptr-operator.  */
15389       if (!cp_parser_parse_definitely (parser))
15390         cp_parser_error (parser, "expected ptr-operator");
15391     }
15392
15393   return code;
15394 }
15395
15396 /* Parse an (optional) cv-qualifier-seq.
15397
15398    cv-qualifier-seq:
15399      cv-qualifier cv-qualifier-seq [opt]
15400
15401    cv-qualifier:
15402      const
15403      volatile
15404
15405    GNU Extension:
15406
15407    cv-qualifier:
15408      __restrict__
15409
15410    Returns a bitmask representing the cv-qualifiers.  */
15411
15412 static cp_cv_quals
15413 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15414 {
15415   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15416
15417   while (true)
15418     {
15419       cp_token *token;
15420       cp_cv_quals cv_qualifier;
15421
15422       /* Peek at the next token.  */
15423       token = cp_lexer_peek_token (parser->lexer);
15424       /* See if it's a cv-qualifier.  */
15425       switch (token->keyword)
15426         {
15427         case RID_CONST:
15428           cv_qualifier = TYPE_QUAL_CONST;
15429           break;
15430
15431         case RID_VOLATILE:
15432           cv_qualifier = TYPE_QUAL_VOLATILE;
15433           break;
15434
15435         case RID_RESTRICT:
15436           cv_qualifier = TYPE_QUAL_RESTRICT;
15437           break;
15438
15439         default:
15440           cv_qualifier = TYPE_UNQUALIFIED;
15441           break;
15442         }
15443
15444       if (!cv_qualifier)
15445         break;
15446
15447       if (cv_quals & cv_qualifier)
15448         {
15449           error_at (token->location, "duplicate cv-qualifier");
15450           cp_lexer_purge_token (parser->lexer);
15451         }
15452       else
15453         {
15454           cp_lexer_consume_token (parser->lexer);
15455           cv_quals |= cv_qualifier;
15456         }
15457     }
15458
15459   return cv_quals;
15460 }
15461
15462 /* Parse an (optional) virt-specifier-seq.
15463
15464    virt-specifier-seq:
15465      virt-specifier virt-specifier-seq [opt]
15466
15467    virt-specifier:
15468      override
15469      final
15470
15471    Returns a bitmask representing the virt-specifiers.  */
15472
15473 static cp_virt_specifiers
15474 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15475 {
15476   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15477
15478   while (true)
15479     {
15480       cp_token *token;
15481       cp_virt_specifiers virt_specifier;
15482
15483       /* Peek at the next token.  */
15484       token = cp_lexer_peek_token (parser->lexer);
15485       /* See if it's a virt-specifier-qualifier.  */
15486       if (token->type != CPP_NAME)
15487         break;
15488       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15489         virt_specifier = VIRT_SPEC_OVERRIDE;
15490       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15491         virt_specifier = VIRT_SPEC_FINAL;
15492       else
15493         break;
15494
15495       if (virt_specifiers & virt_specifier)
15496         {
15497           error_at (token->location, "duplicate virt-specifier");
15498           cp_lexer_purge_token (parser->lexer);
15499         }
15500       else
15501         {
15502           cp_lexer_consume_token (parser->lexer);
15503           virt_specifiers |= virt_specifier;
15504         }
15505     }
15506   return virt_specifiers;
15507 }
15508
15509 /* Parse a late-specified return type, if any.  This is not a separate
15510    non-terminal, but part of a function declarator, which looks like
15511
15512    -> trailing-type-specifier-seq abstract-declarator(opt)
15513
15514    Returns the type indicated by the type-id.  */
15515
15516 static tree
15517 cp_parser_late_return_type_opt (cp_parser* parser)
15518 {
15519   cp_token *token;
15520
15521   /* Peek at the next token.  */
15522   token = cp_lexer_peek_token (parser->lexer);
15523   /* A late-specified return type is indicated by an initial '->'. */
15524   if (token->type != CPP_DEREF)
15525     return NULL_TREE;
15526
15527   /* Consume the ->.  */
15528   cp_lexer_consume_token (parser->lexer);
15529
15530   return cp_parser_trailing_type_id (parser);
15531 }
15532
15533 /* Parse a declarator-id.
15534
15535    declarator-id:
15536      id-expression
15537      :: [opt] nested-name-specifier [opt] type-name
15538
15539    In the `id-expression' case, the value returned is as for
15540    cp_parser_id_expression if the id-expression was an unqualified-id.
15541    If the id-expression was a qualified-id, then a SCOPE_REF is
15542    returned.  The first operand is the scope (either a NAMESPACE_DECL
15543    or TREE_TYPE), but the second is still just a representation of an
15544    unqualified-id.  */
15545
15546 static tree
15547 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15548 {
15549   tree id;
15550   /* The expression must be an id-expression.  Assume that qualified
15551      names are the names of types so that:
15552
15553        template <class T>
15554        int S<T>::R::i = 3;
15555
15556      will work; we must treat `S<T>::R' as the name of a type.
15557      Similarly, assume that qualified names are templates, where
15558      required, so that:
15559
15560        template <class T>
15561        int S<T>::R<T>::i = 3;
15562
15563      will work, too.  */
15564   id = cp_parser_id_expression (parser,
15565                                 /*template_keyword_p=*/false,
15566                                 /*check_dependency_p=*/false,
15567                                 /*template_p=*/NULL,
15568                                 /*declarator_p=*/true,
15569                                 optional_p);
15570   if (id && BASELINK_P (id))
15571     id = BASELINK_FUNCTIONS (id);
15572   return id;
15573 }
15574
15575 /* Parse a type-id.
15576
15577    type-id:
15578      type-specifier-seq abstract-declarator [opt]
15579
15580    Returns the TYPE specified.  */
15581
15582 static tree
15583 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15584                      bool is_trailing_return)
15585 {
15586   cp_decl_specifier_seq type_specifier_seq;
15587   cp_declarator *abstract_declarator;
15588
15589   /* Parse the type-specifier-seq.  */
15590   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15591                                 is_trailing_return,
15592                                 &type_specifier_seq);
15593   if (type_specifier_seq.type == error_mark_node)
15594     return error_mark_node;
15595
15596   /* There might or might not be an abstract declarator.  */
15597   cp_parser_parse_tentatively (parser);
15598   /* Look for the declarator.  */
15599   abstract_declarator
15600     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15601                             /*parenthesized_p=*/NULL,
15602                             /*member_p=*/false);
15603   /* Check to see if there really was a declarator.  */
15604   if (!cp_parser_parse_definitely (parser))
15605     abstract_declarator = NULL;
15606
15607   if (type_specifier_seq.type
15608       && type_uses_auto (type_specifier_seq.type))
15609     {
15610       /* A type-id with type 'auto' is only ok if the abstract declarator
15611          is a function declarator with a late-specified return type.  */
15612       if (abstract_declarator
15613           && abstract_declarator->kind == cdk_function
15614           && abstract_declarator->u.function.late_return_type)
15615         /* OK */;
15616       else
15617         {
15618           error ("invalid use of %<auto%>");
15619           return error_mark_node;
15620         }
15621     }
15622   
15623   return groktypename (&type_specifier_seq, abstract_declarator,
15624                        is_template_arg);
15625 }
15626
15627 static tree cp_parser_type_id (cp_parser *parser)
15628 {
15629   return cp_parser_type_id_1 (parser, false, false);
15630 }
15631
15632 static tree cp_parser_template_type_arg (cp_parser *parser)
15633 {
15634   tree r;
15635   const char *saved_message = parser->type_definition_forbidden_message;
15636   parser->type_definition_forbidden_message
15637     = G_("types may not be defined in template arguments");
15638   r = cp_parser_type_id_1 (parser, true, false);
15639   parser->type_definition_forbidden_message = saved_message;
15640   return r;
15641 }
15642
15643 static tree cp_parser_trailing_type_id (cp_parser *parser)
15644 {
15645   return cp_parser_type_id_1 (parser, false, true);
15646 }
15647
15648 /* Parse a type-specifier-seq.
15649
15650    type-specifier-seq:
15651      type-specifier type-specifier-seq [opt]
15652
15653    GNU extension:
15654
15655    type-specifier-seq:
15656      attributes type-specifier-seq [opt]
15657
15658    If IS_DECLARATION is true, we are at the start of a "condition" or
15659    exception-declaration, so we might be followed by a declarator-id.
15660
15661    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15662    i.e. we've just seen "->".
15663
15664    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15665
15666 static void
15667 cp_parser_type_specifier_seq (cp_parser* parser,
15668                               bool is_declaration,
15669                               bool is_trailing_return,
15670                               cp_decl_specifier_seq *type_specifier_seq)
15671 {
15672   bool seen_type_specifier = false;
15673   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15674   cp_token *start_token = NULL;
15675
15676   /* Clear the TYPE_SPECIFIER_SEQ.  */
15677   clear_decl_specs (type_specifier_seq);
15678
15679   /* In the context of a trailing return type, enum E { } is an
15680      elaborated-type-specifier followed by a function-body, not an
15681      enum-specifier.  */
15682   if (is_trailing_return)
15683     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15684
15685   /* Parse the type-specifiers and attributes.  */
15686   while (true)
15687     {
15688       tree type_specifier;
15689       bool is_cv_qualifier;
15690
15691       /* Check for attributes first.  */
15692       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15693         {
15694           type_specifier_seq->attributes =
15695             chainon (type_specifier_seq->attributes,
15696                      cp_parser_attributes_opt (parser));
15697           continue;
15698         }
15699
15700       /* record the token of the beginning of the type specifier seq,
15701          for error reporting purposes*/
15702      if (!start_token)
15703        start_token = cp_lexer_peek_token (parser->lexer);
15704
15705       /* Look for the type-specifier.  */
15706       type_specifier = cp_parser_type_specifier (parser,
15707                                                  flags,
15708                                                  type_specifier_seq,
15709                                                  /*is_declaration=*/false,
15710                                                  NULL,
15711                                                  &is_cv_qualifier);
15712       if (!type_specifier)
15713         {
15714           /* If the first type-specifier could not be found, this is not a
15715              type-specifier-seq at all.  */
15716           if (!seen_type_specifier)
15717             {
15718               cp_parser_error (parser, "expected type-specifier");
15719               type_specifier_seq->type = error_mark_node;
15720               return;
15721             }
15722           /* If subsequent type-specifiers could not be found, the
15723              type-specifier-seq is complete.  */
15724           break;
15725         }
15726
15727       seen_type_specifier = true;
15728       /* The standard says that a condition can be:
15729
15730             type-specifier-seq declarator = assignment-expression
15731
15732          However, given:
15733
15734            struct S {};
15735            if (int S = ...)
15736
15737          we should treat the "S" as a declarator, not as a
15738          type-specifier.  The standard doesn't say that explicitly for
15739          type-specifier-seq, but it does say that for
15740          decl-specifier-seq in an ordinary declaration.  Perhaps it
15741          would be clearer just to allow a decl-specifier-seq here, and
15742          then add a semantic restriction that if any decl-specifiers
15743          that are not type-specifiers appear, the program is invalid.  */
15744       if (is_declaration && !is_cv_qualifier)
15745         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15746     }
15747
15748   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15749 }
15750
15751 /* Parse a parameter-declaration-clause.
15752
15753    parameter-declaration-clause:
15754      parameter-declaration-list [opt] ... [opt]
15755      parameter-declaration-list , ...
15756
15757    Returns a representation for the parameter declarations.  A return
15758    value of NULL indicates a parameter-declaration-clause consisting
15759    only of an ellipsis.  */
15760
15761 static tree
15762 cp_parser_parameter_declaration_clause (cp_parser* parser)
15763 {
15764   tree parameters;
15765   cp_token *token;
15766   bool ellipsis_p;
15767   bool is_error;
15768
15769   /* Peek at the next token.  */
15770   token = cp_lexer_peek_token (parser->lexer);
15771   /* Check for trivial parameter-declaration-clauses.  */
15772   if (token->type == CPP_ELLIPSIS)
15773     {
15774       /* Consume the `...' token.  */
15775       cp_lexer_consume_token (parser->lexer);
15776       return NULL_TREE;
15777     }
15778   else if (token->type == CPP_CLOSE_PAREN)
15779     /* There are no parameters.  */
15780     {
15781 #ifndef NO_IMPLICIT_EXTERN_C
15782       if (in_system_header && current_class_type == NULL
15783           && current_lang_name == lang_name_c)
15784         return NULL_TREE;
15785       else
15786 #endif
15787         return void_list_node;
15788     }
15789   /* Check for `(void)', too, which is a special case.  */
15790   else if (token->keyword == RID_VOID
15791            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15792                == CPP_CLOSE_PAREN))
15793     {
15794       /* Consume the `void' token.  */
15795       cp_lexer_consume_token (parser->lexer);
15796       /* There are no parameters.  */
15797       return void_list_node;
15798     }
15799
15800   /* Parse the parameter-declaration-list.  */
15801   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15802   /* If a parse error occurred while parsing the
15803      parameter-declaration-list, then the entire
15804      parameter-declaration-clause is erroneous.  */
15805   if (is_error)
15806     return NULL;
15807
15808   /* Peek at the next token.  */
15809   token = cp_lexer_peek_token (parser->lexer);
15810   /* If it's a `,', the clause should terminate with an ellipsis.  */
15811   if (token->type == CPP_COMMA)
15812     {
15813       /* Consume the `,'.  */
15814       cp_lexer_consume_token (parser->lexer);
15815       /* Expect an ellipsis.  */
15816       ellipsis_p
15817         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15818     }
15819   /* It might also be `...' if the optional trailing `,' was
15820      omitted.  */
15821   else if (token->type == CPP_ELLIPSIS)
15822     {
15823       /* Consume the `...' token.  */
15824       cp_lexer_consume_token (parser->lexer);
15825       /* And remember that we saw it.  */
15826       ellipsis_p = true;
15827     }
15828   else
15829     ellipsis_p = false;
15830
15831   /* Finish the parameter list.  */
15832   if (!ellipsis_p)
15833     parameters = chainon (parameters, void_list_node);
15834
15835   return parameters;
15836 }
15837
15838 /* Parse a parameter-declaration-list.
15839
15840    parameter-declaration-list:
15841      parameter-declaration
15842      parameter-declaration-list , parameter-declaration
15843
15844    Returns a representation of the parameter-declaration-list, as for
15845    cp_parser_parameter_declaration_clause.  However, the
15846    `void_list_node' is never appended to the list.  Upon return,
15847    *IS_ERROR will be true iff an error occurred.  */
15848
15849 static tree
15850 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15851 {
15852   tree parameters = NULL_TREE;
15853   tree *tail = &parameters; 
15854   bool saved_in_unbraced_linkage_specification_p;
15855   int index = 0;
15856
15857   /* Assume all will go well.  */
15858   *is_error = false;
15859   /* The special considerations that apply to a function within an
15860      unbraced linkage specifications do not apply to the parameters
15861      to the function.  */
15862   saved_in_unbraced_linkage_specification_p 
15863     = parser->in_unbraced_linkage_specification_p;
15864   parser->in_unbraced_linkage_specification_p = false;
15865
15866   /* Look for more parameters.  */
15867   while (true)
15868     {
15869       cp_parameter_declarator *parameter;
15870       tree decl = error_mark_node;
15871       bool parenthesized_p;
15872       /* Parse the parameter.  */
15873       parameter
15874         = cp_parser_parameter_declaration (parser,
15875                                            /*template_parm_p=*/false,
15876                                            &parenthesized_p);
15877
15878       /* We don't know yet if the enclosing context is deprecated, so wait
15879          and warn in grokparms if appropriate.  */
15880       deprecated_state = DEPRECATED_SUPPRESS;
15881
15882       if (parameter)
15883         decl = grokdeclarator (parameter->declarator,
15884                                &parameter->decl_specifiers,
15885                                PARM,
15886                                parameter->default_argument != NULL_TREE,
15887                                &parameter->decl_specifiers.attributes);
15888
15889       deprecated_state = DEPRECATED_NORMAL;
15890
15891       /* If a parse error occurred parsing the parameter declaration,
15892          then the entire parameter-declaration-list is erroneous.  */
15893       if (decl == error_mark_node)
15894         {
15895           *is_error = true;
15896           parameters = error_mark_node;
15897           break;
15898         }
15899
15900       if (parameter->decl_specifiers.attributes)
15901         cplus_decl_attributes (&decl,
15902                                parameter->decl_specifiers.attributes,
15903                                0);
15904       if (DECL_NAME (decl))
15905         decl = pushdecl (decl);
15906
15907       if (decl != error_mark_node)
15908         {
15909           retrofit_lang_decl (decl);
15910           DECL_PARM_INDEX (decl) = ++index;
15911           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15912         }
15913
15914       /* Add the new parameter to the list.  */
15915       *tail = build_tree_list (parameter->default_argument, decl);
15916       tail = &TREE_CHAIN (*tail);
15917
15918       /* Peek at the next token.  */
15919       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15920           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15921           /* These are for Objective-C++ */
15922           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15923           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15924         /* The parameter-declaration-list is complete.  */
15925         break;
15926       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15927         {
15928           cp_token *token;
15929
15930           /* Peek at the next token.  */
15931           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15932           /* If it's an ellipsis, then the list is complete.  */
15933           if (token->type == CPP_ELLIPSIS)
15934             break;
15935           /* Otherwise, there must be more parameters.  Consume the
15936              `,'.  */
15937           cp_lexer_consume_token (parser->lexer);
15938           /* When parsing something like:
15939
15940                 int i(float f, double d)
15941
15942              we can tell after seeing the declaration for "f" that we
15943              are not looking at an initialization of a variable "i",
15944              but rather at the declaration of a function "i".
15945
15946              Due to the fact that the parsing of template arguments
15947              (as specified to a template-id) requires backtracking we
15948              cannot use this technique when inside a template argument
15949              list.  */
15950           if (!parser->in_template_argument_list_p
15951               && !parser->in_type_id_in_expr_p
15952               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15953               /* However, a parameter-declaration of the form
15954                  "foat(f)" (which is a valid declaration of a
15955                  parameter "f") can also be interpreted as an
15956                  expression (the conversion of "f" to "float").  */
15957               && !parenthesized_p)
15958             cp_parser_commit_to_tentative_parse (parser);
15959         }
15960       else
15961         {
15962           cp_parser_error (parser, "expected %<,%> or %<...%>");
15963           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15964             cp_parser_skip_to_closing_parenthesis (parser,
15965                                                    /*recovering=*/true,
15966                                                    /*or_comma=*/false,
15967                                                    /*consume_paren=*/false);
15968           break;
15969         }
15970     }
15971
15972   parser->in_unbraced_linkage_specification_p
15973     = saved_in_unbraced_linkage_specification_p;
15974
15975   return parameters;
15976 }
15977
15978 /* Parse a parameter declaration.
15979
15980    parameter-declaration:
15981      decl-specifier-seq ... [opt] declarator
15982      decl-specifier-seq declarator = assignment-expression
15983      decl-specifier-seq ... [opt] abstract-declarator [opt]
15984      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15985
15986    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15987    declares a template parameter.  (In that case, a non-nested `>'
15988    token encountered during the parsing of the assignment-expression
15989    is not interpreted as a greater-than operator.)
15990
15991    Returns a representation of the parameter, or NULL if an error
15992    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15993    true iff the declarator is of the form "(p)".  */
15994
15995 static cp_parameter_declarator *
15996 cp_parser_parameter_declaration (cp_parser *parser,
15997                                  bool template_parm_p,
15998                                  bool *parenthesized_p)
15999 {
16000   int declares_class_or_enum;
16001   cp_decl_specifier_seq decl_specifiers;
16002   cp_declarator *declarator;
16003   tree default_argument;
16004   cp_token *token = NULL, *declarator_token_start = NULL;
16005   const char *saved_message;
16006
16007   /* In a template parameter, `>' is not an operator.
16008
16009      [temp.param]
16010
16011      When parsing a default template-argument for a non-type
16012      template-parameter, the first non-nested `>' is taken as the end
16013      of the template parameter-list rather than a greater-than
16014      operator.  */
16015
16016   /* Type definitions may not appear in parameter types.  */
16017   saved_message = parser->type_definition_forbidden_message;
16018   parser->type_definition_forbidden_message
16019     = G_("types may not be defined in parameter types");
16020
16021   /* Parse the declaration-specifiers.  */
16022   cp_parser_decl_specifier_seq (parser,
16023                                 CP_PARSER_FLAGS_NONE,
16024                                 &decl_specifiers,
16025                                 &declares_class_or_enum);
16026
16027   /* Complain about missing 'typename' or other invalid type names.  */
16028   if (!decl_specifiers.any_type_specifiers_p)
16029     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16030
16031   /* If an error occurred, there's no reason to attempt to parse the
16032      rest of the declaration.  */
16033   if (cp_parser_error_occurred (parser))
16034     {
16035       parser->type_definition_forbidden_message = saved_message;
16036       return NULL;
16037     }
16038
16039   /* Peek at the next token.  */
16040   token = cp_lexer_peek_token (parser->lexer);
16041
16042   /* If the next token is a `)', `,', `=', `>', or `...', then there
16043      is no declarator. However, when variadic templates are enabled,
16044      there may be a declarator following `...'.  */
16045   if (token->type == CPP_CLOSE_PAREN
16046       || token->type == CPP_COMMA
16047       || token->type == CPP_EQ
16048       || token->type == CPP_GREATER)
16049     {
16050       declarator = NULL;
16051       if (parenthesized_p)
16052         *parenthesized_p = false;
16053     }
16054   /* Otherwise, there should be a declarator.  */
16055   else
16056     {
16057       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16058       parser->default_arg_ok_p = false;
16059
16060       /* After seeing a decl-specifier-seq, if the next token is not a
16061          "(", there is no possibility that the code is a valid
16062          expression.  Therefore, if parsing tentatively, we commit at
16063          this point.  */
16064       if (!parser->in_template_argument_list_p
16065           /* In an expression context, having seen:
16066
16067                (int((char ...
16068
16069              we cannot be sure whether we are looking at a
16070              function-type (taking a "char" as a parameter) or a cast
16071              of some object of type "char" to "int".  */
16072           && !parser->in_type_id_in_expr_p
16073           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16074           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16075           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16076         cp_parser_commit_to_tentative_parse (parser);
16077       /* Parse the declarator.  */
16078       declarator_token_start = token;
16079       declarator = cp_parser_declarator (parser,
16080                                          CP_PARSER_DECLARATOR_EITHER,
16081                                          /*ctor_dtor_or_conv_p=*/NULL,
16082                                          parenthesized_p,
16083                                          /*member_p=*/false);
16084       parser->default_arg_ok_p = saved_default_arg_ok_p;
16085       /* After the declarator, allow more attributes.  */
16086       decl_specifiers.attributes
16087         = chainon (decl_specifiers.attributes,
16088                    cp_parser_attributes_opt (parser));
16089     }
16090
16091   /* If the next token is an ellipsis, and we have not seen a
16092      declarator name, and the type of the declarator contains parameter
16093      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16094      a parameter pack expansion expression. Otherwise, leave the
16095      ellipsis for a C-style variadic function. */
16096   token = cp_lexer_peek_token (parser->lexer);
16097   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16098     {
16099       tree type = decl_specifiers.type;
16100
16101       if (type && DECL_P (type))
16102         type = TREE_TYPE (type);
16103
16104       if (type
16105           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16106           && declarator_can_be_parameter_pack (declarator)
16107           && (!declarator || !declarator->parameter_pack_p)
16108           && uses_parameter_packs (type))
16109         {
16110           /* Consume the `...'. */
16111           cp_lexer_consume_token (parser->lexer);
16112           maybe_warn_variadic_templates ();
16113           
16114           /* Build a pack expansion type */
16115           if (declarator)
16116             declarator->parameter_pack_p = true;
16117           else
16118             decl_specifiers.type = make_pack_expansion (type);
16119         }
16120     }
16121
16122   /* The restriction on defining new types applies only to the type
16123      of the parameter, not to the default argument.  */
16124   parser->type_definition_forbidden_message = saved_message;
16125
16126   /* If the next token is `=', then process a default argument.  */
16127   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16128     {
16129       /* Consume the `='.  */
16130       cp_lexer_consume_token (parser->lexer);
16131
16132       /* If we are defining a class, then the tokens that make up the
16133          default argument must be saved and processed later.  */
16134       if (!template_parm_p && at_class_scope_p ()
16135           && TYPE_BEING_DEFINED (current_class_type)
16136           && !LAMBDA_TYPE_P (current_class_type))
16137         {
16138           unsigned depth = 0;
16139           int maybe_template_id = 0;
16140           cp_token *first_token;
16141           cp_token *token;
16142
16143           /* Add tokens until we have processed the entire default
16144              argument.  We add the range [first_token, token).  */
16145           first_token = cp_lexer_peek_token (parser->lexer);
16146           while (true)
16147             {
16148               bool done = false;
16149
16150               /* Peek at the next token.  */
16151               token = cp_lexer_peek_token (parser->lexer);
16152               /* What we do depends on what token we have.  */
16153               switch (token->type)
16154                 {
16155                   /* In valid code, a default argument must be
16156                      immediately followed by a `,' `)', or `...'.  */
16157                 case CPP_COMMA:
16158                   if (depth == 0 && maybe_template_id)
16159                     {
16160                       /* If we've seen a '<', we might be in a
16161                          template-argument-list.  Until Core issue 325 is
16162                          resolved, we don't know how this situation ought
16163                          to be handled, so try to DTRT.  We check whether
16164                          what comes after the comma is a valid parameter
16165                          declaration list.  If it is, then the comma ends
16166                          the default argument; otherwise the default
16167                          argument continues.  */
16168                       bool error = false;
16169                       tree t;
16170
16171                       /* Set ITALP so cp_parser_parameter_declaration_list
16172                          doesn't decide to commit to this parse.  */
16173                       bool saved_italp = parser->in_template_argument_list_p;
16174                       parser->in_template_argument_list_p = true;
16175
16176                       cp_parser_parse_tentatively (parser);
16177                       cp_lexer_consume_token (parser->lexer);
16178                       begin_scope (sk_function_parms, NULL_TREE);
16179                       cp_parser_parameter_declaration_list (parser, &error);
16180                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16181                         pop_binding (DECL_NAME (t), t);
16182                       leave_scope ();
16183                       if (!cp_parser_error_occurred (parser) && !error)
16184                         done = true;
16185                       cp_parser_abort_tentative_parse (parser);
16186
16187                       parser->in_template_argument_list_p = saved_italp;
16188                       break;
16189                     }
16190                 case CPP_CLOSE_PAREN:
16191                 case CPP_ELLIPSIS:
16192                   /* If we run into a non-nested `;', `}', or `]',
16193                      then the code is invalid -- but the default
16194                      argument is certainly over.  */
16195                 case CPP_SEMICOLON:
16196                 case CPP_CLOSE_BRACE:
16197                 case CPP_CLOSE_SQUARE:
16198                   if (depth == 0)
16199                     done = true;
16200                   /* Update DEPTH, if necessary.  */
16201                   else if (token->type == CPP_CLOSE_PAREN
16202                            || token->type == CPP_CLOSE_BRACE
16203                            || token->type == CPP_CLOSE_SQUARE)
16204                     --depth;
16205                   break;
16206
16207                 case CPP_OPEN_PAREN:
16208                 case CPP_OPEN_SQUARE:
16209                 case CPP_OPEN_BRACE:
16210                   ++depth;
16211                   break;
16212
16213                 case CPP_LESS:
16214                   if (depth == 0)
16215                     /* This might be the comparison operator, or it might
16216                        start a template argument list.  */
16217                     ++maybe_template_id;
16218                   break;
16219
16220                 case CPP_RSHIFT:
16221                   if (cxx_dialect == cxx98)
16222                     break;
16223                   /* Fall through for C++0x, which treats the `>>'
16224                      operator like two `>' tokens in certain
16225                      cases.  */
16226
16227                 case CPP_GREATER:
16228                   if (depth == 0)
16229                     {
16230                       /* This might be an operator, or it might close a
16231                          template argument list.  But if a previous '<'
16232                          started a template argument list, this will have
16233                          closed it, so we can't be in one anymore.  */
16234                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16235                       if (maybe_template_id < 0)
16236                         maybe_template_id = 0;
16237                     }
16238                   break;
16239
16240                   /* If we run out of tokens, issue an error message.  */
16241                 case CPP_EOF:
16242                 case CPP_PRAGMA_EOL:
16243                   error_at (token->location, "file ends in default argument");
16244                   done = true;
16245                   break;
16246
16247                 case CPP_NAME:
16248                 case CPP_SCOPE:
16249                   /* In these cases, we should look for template-ids.
16250                      For example, if the default argument is
16251                      `X<int, double>()', we need to do name lookup to
16252                      figure out whether or not `X' is a template; if
16253                      so, the `,' does not end the default argument.
16254
16255                      That is not yet done.  */
16256                   break;
16257
16258                 default:
16259                   break;
16260                 }
16261
16262               /* If we've reached the end, stop.  */
16263               if (done)
16264                 break;
16265
16266               /* Add the token to the token block.  */
16267               token = cp_lexer_consume_token (parser->lexer);
16268             }
16269
16270           /* Create a DEFAULT_ARG to represent the unparsed default
16271              argument.  */
16272           default_argument = make_node (DEFAULT_ARG);
16273           DEFARG_TOKENS (default_argument)
16274             = cp_token_cache_new (first_token, token);
16275           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16276         }
16277       /* Outside of a class definition, we can just parse the
16278          assignment-expression.  */
16279       else
16280         {
16281           token = cp_lexer_peek_token (parser->lexer);
16282           default_argument 
16283             = cp_parser_default_argument (parser, template_parm_p);
16284         }
16285
16286       if (!parser->default_arg_ok_p)
16287         {
16288           if (flag_permissive)
16289             warning (0, "deprecated use of default argument for parameter of non-function");
16290           else
16291             {
16292               error_at (token->location,
16293                         "default arguments are only "
16294                         "permitted for function parameters");
16295               default_argument = NULL_TREE;
16296             }
16297         }
16298       else if ((declarator && declarator->parameter_pack_p)
16299                || (decl_specifiers.type
16300                    && PACK_EXPANSION_P (decl_specifiers.type)))
16301         {
16302           /* Find the name of the parameter pack.  */     
16303           cp_declarator *id_declarator = declarator;
16304           while (id_declarator && id_declarator->kind != cdk_id)
16305             id_declarator = id_declarator->declarator;
16306           
16307           if (id_declarator && id_declarator->kind == cdk_id)
16308             error_at (declarator_token_start->location,
16309                       template_parm_p 
16310                       ? "template parameter pack %qD"
16311                       " cannot have a default argument"
16312                       : "parameter pack %qD cannot have a default argument",
16313                       id_declarator->u.id.unqualified_name);
16314           else
16315             error_at (declarator_token_start->location,
16316                       template_parm_p 
16317                       ? "template parameter pack cannot have a default argument"
16318                       : "parameter pack cannot have a default argument");
16319           
16320           default_argument = NULL_TREE;
16321         }
16322     }
16323   else
16324     default_argument = NULL_TREE;
16325
16326   return make_parameter_declarator (&decl_specifiers,
16327                                     declarator,
16328                                     default_argument);
16329 }
16330
16331 /* Parse a default argument and return it.
16332
16333    TEMPLATE_PARM_P is true if this is a default argument for a
16334    non-type template parameter.  */
16335 static tree
16336 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16337 {
16338   tree default_argument = NULL_TREE;
16339   bool saved_greater_than_is_operator_p;
16340   bool saved_local_variables_forbidden_p;
16341
16342   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16343      set correctly.  */
16344   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16345   parser->greater_than_is_operator_p = !template_parm_p;
16346   /* Local variable names (and the `this' keyword) may not
16347      appear in a default argument.  */
16348   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16349   parser->local_variables_forbidden_p = true;
16350   /* Parse the assignment-expression.  */
16351   if (template_parm_p)
16352     push_deferring_access_checks (dk_no_deferred);
16353   default_argument
16354     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16355   if (template_parm_p)
16356     pop_deferring_access_checks ();
16357   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16358   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16359
16360   return default_argument;
16361 }
16362
16363 /* Parse a function-body.
16364
16365    function-body:
16366      compound_statement  */
16367
16368 static void
16369 cp_parser_function_body (cp_parser *parser)
16370 {
16371   cp_parser_compound_statement (parser, NULL, false, true);
16372 }
16373
16374 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16375    true if a ctor-initializer was present.  */
16376
16377 static bool
16378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16379 {
16380   tree body, list;
16381   bool ctor_initializer_p;
16382   const bool check_body_p =
16383      DECL_CONSTRUCTOR_P (current_function_decl)
16384      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16385   tree last = NULL;
16386
16387   /* Begin the function body.  */
16388   body = begin_function_body ();
16389   /* Parse the optional ctor-initializer.  */
16390   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16391
16392   /* If we're parsing a constexpr constructor definition, we need
16393      to check that the constructor body is indeed empty.  However,
16394      before we get to cp_parser_function_body lot of junk has been
16395      generated, so we can't just check that we have an empty block.
16396      Rather we take a snapshot of the outermost block, and check whether
16397      cp_parser_function_body changed its state.  */
16398   if (check_body_p)
16399     {
16400       list = body;
16401       if (TREE_CODE (list) == BIND_EXPR)
16402         list = BIND_EXPR_BODY (list);
16403       if (TREE_CODE (list) == STATEMENT_LIST
16404           && STATEMENT_LIST_TAIL (list) != NULL)
16405         last = STATEMENT_LIST_TAIL (list)->stmt;
16406     }
16407   /* Parse the function-body.  */
16408   cp_parser_function_body (parser);
16409   if (check_body_p)
16410     check_constexpr_ctor_body (last, list);
16411   /* Finish the function body.  */
16412   finish_function_body (body);
16413
16414   return ctor_initializer_p;
16415 }
16416
16417 /* Parse an initializer.
16418
16419    initializer:
16420      = initializer-clause
16421      ( expression-list )
16422
16423    Returns an expression representing the initializer.  If no
16424    initializer is present, NULL_TREE is returned.
16425
16426    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16427    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16428    set to TRUE if there is no initializer present.  If there is an
16429    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16430    is set to true; otherwise it is set to false.  */
16431
16432 static tree
16433 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16434                        bool* non_constant_p)
16435 {
16436   cp_token *token;
16437   tree init;
16438
16439   /* Peek at the next token.  */
16440   token = cp_lexer_peek_token (parser->lexer);
16441
16442   /* Let our caller know whether or not this initializer was
16443      parenthesized.  */
16444   *is_direct_init = (token->type != CPP_EQ);
16445   /* Assume that the initializer is constant.  */
16446   *non_constant_p = false;
16447
16448   if (token->type == CPP_EQ)
16449     {
16450       /* Consume the `='.  */
16451       cp_lexer_consume_token (parser->lexer);
16452       /* Parse the initializer-clause.  */
16453       init = cp_parser_initializer_clause (parser, non_constant_p);
16454     }
16455   else if (token->type == CPP_OPEN_PAREN)
16456     {
16457       VEC(tree,gc) *vec;
16458       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16459                                                      /*cast_p=*/false,
16460                                                      /*allow_expansion_p=*/true,
16461                                                      non_constant_p);
16462       if (vec == NULL)
16463         return error_mark_node;
16464       init = build_tree_list_vec (vec);
16465       release_tree_vector (vec);
16466     }
16467   else if (token->type == CPP_OPEN_BRACE)
16468     {
16469       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16470       init = cp_parser_braced_list (parser, non_constant_p);
16471       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16472     }
16473   else
16474     {
16475       /* Anything else is an error.  */
16476       cp_parser_error (parser, "expected initializer");
16477       init = error_mark_node;
16478     }
16479
16480   return init;
16481 }
16482
16483 /* Parse an initializer-clause.
16484
16485    initializer-clause:
16486      assignment-expression
16487      braced-init-list
16488
16489    Returns an expression representing the initializer.
16490
16491    If the `assignment-expression' production is used the value
16492    returned is simply a representation for the expression.
16493
16494    Otherwise, calls cp_parser_braced_list.  */
16495
16496 static tree
16497 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16498 {
16499   tree initializer;
16500
16501   /* Assume the expression is constant.  */
16502   *non_constant_p = false;
16503
16504   /* If it is not a `{', then we are looking at an
16505      assignment-expression.  */
16506   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16507     {
16508       initializer
16509         = cp_parser_constant_expression (parser,
16510                                         /*allow_non_constant_p=*/true,
16511                                         non_constant_p);
16512       if (!*non_constant_p)
16513         {
16514           /* We only want to fold if this is really a constant
16515              expression.  FIXME Actually, we don't want to fold here, but in
16516              cp_finish_decl.  */
16517           tree folded = fold_non_dependent_expr (initializer);
16518           folded = maybe_constant_value (folded);
16519           if (TREE_CONSTANT (folded))
16520             initializer = folded;
16521         }
16522     }
16523   else
16524     initializer = cp_parser_braced_list (parser, non_constant_p);
16525
16526   return initializer;
16527 }
16528
16529 /* Parse a brace-enclosed initializer list.
16530
16531    braced-init-list:
16532      { initializer-list , [opt] }
16533      { }
16534
16535    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16536    the elements of the initializer-list (or NULL, if the last
16537    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16538    NULL_TREE.  There is no way to detect whether or not the optional
16539    trailing `,' was provided.  NON_CONSTANT_P is as for
16540    cp_parser_initializer.  */     
16541
16542 static tree
16543 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16544 {
16545   tree initializer;
16546
16547   /* Consume the `{' token.  */
16548   cp_lexer_consume_token (parser->lexer);
16549   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16550   initializer = make_node (CONSTRUCTOR);
16551   /* If it's not a `}', then there is a non-trivial initializer.  */
16552   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16553     {
16554       /* Parse the initializer list.  */
16555       CONSTRUCTOR_ELTS (initializer)
16556         = cp_parser_initializer_list (parser, non_constant_p);
16557       /* A trailing `,' token is allowed.  */
16558       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16559         cp_lexer_consume_token (parser->lexer);
16560     }
16561   /* Now, there should be a trailing `}'.  */
16562   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16563   TREE_TYPE (initializer) = init_list_type_node;
16564   return initializer;
16565 }
16566
16567 /* Parse an initializer-list.
16568
16569    initializer-list:
16570      initializer-clause ... [opt]
16571      initializer-list , initializer-clause ... [opt]
16572
16573    GNU Extension:
16574
16575    initializer-list:
16576      identifier : initializer-clause
16577      initializer-list, identifier : initializer-clause
16578
16579    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16580    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16581    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16582    as for cp_parser_initializer.  */
16583
16584 static VEC(constructor_elt,gc) *
16585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16586 {
16587   VEC(constructor_elt,gc) *v = NULL;
16588
16589   /* Assume all of the expressions are constant.  */
16590   *non_constant_p = false;
16591
16592   /* Parse the rest of the list.  */
16593   while (true)
16594     {
16595       cp_token *token;
16596       tree identifier;
16597       tree initializer;
16598       bool clause_non_constant_p;
16599
16600       /* If the next token is an identifier and the following one is a
16601          colon, we are looking at the GNU designated-initializer
16602          syntax.  */
16603       if (cp_parser_allow_gnu_extensions_p (parser)
16604           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16605           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16606         {
16607           /* Warn the user that they are using an extension.  */
16608           pedwarn (input_location, OPT_pedantic, 
16609                    "ISO C++ does not allow designated initializers");
16610           /* Consume the identifier.  */
16611           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16612           /* Consume the `:'.  */
16613           cp_lexer_consume_token (parser->lexer);
16614         }
16615       else
16616         identifier = NULL_TREE;
16617
16618       /* Parse the initializer.  */
16619       initializer = cp_parser_initializer_clause (parser,
16620                                                   &clause_non_constant_p);
16621       /* If any clause is non-constant, so is the entire initializer.  */
16622       if (clause_non_constant_p)
16623         *non_constant_p = true;
16624
16625       /* If we have an ellipsis, this is an initializer pack
16626          expansion.  */
16627       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16628         {
16629           /* Consume the `...'.  */
16630           cp_lexer_consume_token (parser->lexer);
16631
16632           /* Turn the initializer into an initializer expansion.  */
16633           initializer = make_pack_expansion (initializer);
16634         }
16635
16636       /* Add it to the vector.  */
16637       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16638
16639       /* If the next token is not a comma, we have reached the end of
16640          the list.  */
16641       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16642         break;
16643
16644       /* Peek at the next token.  */
16645       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16646       /* If the next token is a `}', then we're still done.  An
16647          initializer-clause can have a trailing `,' after the
16648          initializer-list and before the closing `}'.  */
16649       if (token->type == CPP_CLOSE_BRACE)
16650         break;
16651
16652       /* Consume the `,' token.  */
16653       cp_lexer_consume_token (parser->lexer);
16654     }
16655
16656   return v;
16657 }
16658
16659 /* Classes [gram.class] */
16660
16661 /* Parse a class-name.
16662
16663    class-name:
16664      identifier
16665      template-id
16666
16667    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16668    to indicate that names looked up in dependent types should be
16669    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16670    keyword has been used to indicate that the name that appears next
16671    is a template.  TAG_TYPE indicates the explicit tag given before
16672    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16673    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16674    is the class being defined in a class-head.
16675
16676    Returns the TYPE_DECL representing the class.  */
16677
16678 static tree
16679 cp_parser_class_name (cp_parser *parser,
16680                       bool typename_keyword_p,
16681                       bool template_keyword_p,
16682                       enum tag_types tag_type,
16683                       bool check_dependency_p,
16684                       bool class_head_p,
16685                       bool is_declaration)
16686 {
16687   tree decl;
16688   tree scope;
16689   bool typename_p;
16690   cp_token *token;
16691   tree identifier = NULL_TREE;
16692
16693   /* All class-names start with an identifier.  */
16694   token = cp_lexer_peek_token (parser->lexer);
16695   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16696     {
16697       cp_parser_error (parser, "expected class-name");
16698       return error_mark_node;
16699     }
16700
16701   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16702      to a template-id, so we save it here.  */
16703   scope = parser->scope;
16704   if (scope == error_mark_node)
16705     return error_mark_node;
16706
16707   /* Any name names a type if we're following the `typename' keyword
16708      in a qualified name where the enclosing scope is type-dependent.  */
16709   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16710                 && dependent_type_p (scope));
16711   /* Handle the common case (an identifier, but not a template-id)
16712      efficiently.  */
16713   if (token->type == CPP_NAME
16714       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16715     {
16716       cp_token *identifier_token;
16717       bool ambiguous_p;
16718
16719       /* Look for the identifier.  */
16720       identifier_token = cp_lexer_peek_token (parser->lexer);
16721       ambiguous_p = identifier_token->ambiguous_p;
16722       identifier = cp_parser_identifier (parser);
16723       /* If the next token isn't an identifier, we are certainly not
16724          looking at a class-name.  */
16725       if (identifier == error_mark_node)
16726         decl = error_mark_node;
16727       /* If we know this is a type-name, there's no need to look it
16728          up.  */
16729       else if (typename_p)
16730         decl = identifier;
16731       else
16732         {
16733           tree ambiguous_decls;
16734           /* If we already know that this lookup is ambiguous, then
16735              we've already issued an error message; there's no reason
16736              to check again.  */
16737           if (ambiguous_p)
16738             {
16739               cp_parser_simulate_error (parser);
16740               return error_mark_node;
16741             }
16742           /* If the next token is a `::', then the name must be a type
16743              name.
16744
16745              [basic.lookup.qual]
16746
16747              During the lookup for a name preceding the :: scope
16748              resolution operator, object, function, and enumerator
16749              names are ignored.  */
16750           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16751             tag_type = typename_type;
16752           /* Look up the name.  */
16753           decl = cp_parser_lookup_name (parser, identifier,
16754                                         tag_type,
16755                                         /*is_template=*/false,
16756                                         /*is_namespace=*/false,
16757                                         check_dependency_p,
16758                                         &ambiguous_decls,
16759                                         identifier_token->location);
16760           if (ambiguous_decls)
16761             {
16762               if (cp_parser_parsing_tentatively (parser))
16763                 cp_parser_simulate_error (parser);
16764               return error_mark_node;
16765             }
16766         }
16767     }
16768   else
16769     {
16770       /* Try a template-id.  */
16771       decl = cp_parser_template_id (parser, template_keyword_p,
16772                                     check_dependency_p,
16773                                     is_declaration);
16774       if (decl == error_mark_node)
16775         return error_mark_node;
16776     }
16777
16778   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16779
16780   /* If this is a typename, create a TYPENAME_TYPE.  */
16781   if (typename_p && decl != error_mark_node)
16782     {
16783       decl = make_typename_type (scope, decl, typename_type,
16784                                  /*complain=*/tf_error);
16785       if (decl != error_mark_node)
16786         decl = TYPE_NAME (decl);
16787     }
16788
16789   /* Check to see that it is really the name of a class.  */
16790   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16791       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16792       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16793     /* Situations like this:
16794
16795          template <typename T> struct A {
16796            typename T::template X<int>::I i;
16797          };
16798
16799        are problematic.  Is `T::template X<int>' a class-name?  The
16800        standard does not seem to be definitive, but there is no other
16801        valid interpretation of the following `::'.  Therefore, those
16802        names are considered class-names.  */
16803     {
16804       decl = make_typename_type (scope, decl, tag_type, tf_error);
16805       if (decl != error_mark_node)
16806         decl = TYPE_NAME (decl);
16807     }
16808   else if (TREE_CODE (decl) != TYPE_DECL
16809            || TREE_TYPE (decl) == error_mark_node
16810            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16811            /* In Objective-C 2.0, a classname followed by '.' starts a
16812               dot-syntax expression, and it's not a type-name.  */
16813            || (c_dialect_objc ()
16814                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16815                && objc_is_class_name (decl)))
16816     decl = error_mark_node;
16817
16818   if (decl == error_mark_node)
16819     cp_parser_error (parser, "expected class-name");
16820   else if (identifier && !parser->scope)
16821     maybe_note_name_used_in_class (identifier, decl);
16822
16823   return decl;
16824 }
16825
16826 /* Parse a class-specifier.
16827
16828    class-specifier:
16829      class-head { member-specification [opt] }
16830
16831    Returns the TREE_TYPE representing the class.  */
16832
16833 static tree
16834 cp_parser_class_specifier_1 (cp_parser* parser)
16835 {
16836   tree type;
16837   tree attributes = NULL_TREE;
16838   bool nested_name_specifier_p;
16839   unsigned saved_num_template_parameter_lists;
16840   bool saved_in_function_body;
16841   bool saved_in_unbraced_linkage_specification_p;
16842   tree old_scope = NULL_TREE;
16843   tree scope = NULL_TREE;
16844   tree bases;
16845   cp_token *closing_brace;
16846
16847   push_deferring_access_checks (dk_no_deferred);
16848
16849   /* Parse the class-head.  */
16850   type = cp_parser_class_head (parser,
16851                                &nested_name_specifier_p,
16852                                &attributes,
16853                                &bases);
16854   /* If the class-head was a semantic disaster, skip the entire body
16855      of the class.  */
16856   if (!type)
16857     {
16858       cp_parser_skip_to_end_of_block_or_statement (parser);
16859       pop_deferring_access_checks ();
16860       return error_mark_node;
16861     }
16862
16863   /* Look for the `{'.  */
16864   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16865     {
16866       pop_deferring_access_checks ();
16867       return error_mark_node;
16868     }
16869
16870   /* Process the base classes. If they're invalid, skip the 
16871      entire class body.  */
16872   if (!xref_basetypes (type, bases))
16873     {
16874       /* Consuming the closing brace yields better error messages
16875          later on.  */
16876       if (cp_parser_skip_to_closing_brace (parser))
16877         cp_lexer_consume_token (parser->lexer);
16878       pop_deferring_access_checks ();
16879       return error_mark_node;
16880     }
16881
16882   /* Issue an error message if type-definitions are forbidden here.  */
16883   cp_parser_check_type_definition (parser);
16884   /* Remember that we are defining one more class.  */
16885   ++parser->num_classes_being_defined;
16886   /* Inside the class, surrounding template-parameter-lists do not
16887      apply.  */
16888   saved_num_template_parameter_lists
16889     = parser->num_template_parameter_lists;
16890   parser->num_template_parameter_lists = 0;
16891   /* We are not in a function body.  */
16892   saved_in_function_body = parser->in_function_body;
16893   parser->in_function_body = false;
16894   /* We are not immediately inside an extern "lang" block.  */
16895   saved_in_unbraced_linkage_specification_p
16896     = parser->in_unbraced_linkage_specification_p;
16897   parser->in_unbraced_linkage_specification_p = false;
16898
16899   /* Start the class.  */
16900   if (nested_name_specifier_p)
16901     {
16902       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16903       old_scope = push_inner_scope (scope);
16904     }
16905   type = begin_class_definition (type, attributes);
16906
16907   if (type == error_mark_node)
16908     /* If the type is erroneous, skip the entire body of the class.  */
16909     cp_parser_skip_to_closing_brace (parser);
16910   else
16911     /* Parse the member-specification.  */
16912     cp_parser_member_specification_opt (parser);
16913
16914   /* Look for the trailing `}'.  */
16915   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16916   /* Look for trailing attributes to apply to this class.  */
16917   if (cp_parser_allow_gnu_extensions_p (parser))
16918     attributes = cp_parser_attributes_opt (parser);
16919   if (type != error_mark_node)
16920     type = finish_struct (type, attributes);
16921   if (nested_name_specifier_p)
16922     pop_inner_scope (old_scope, scope);
16923
16924   /* We've finished a type definition.  Check for the common syntax
16925      error of forgetting a semicolon after the definition.  We need to
16926      be careful, as we can't just check for not-a-semicolon and be done
16927      with it; the user might have typed:
16928
16929      class X { } c = ...;
16930      class X { } *p = ...;
16931
16932      and so forth.  Instead, enumerate all the possible tokens that
16933      might follow this production; if we don't see one of them, then
16934      complain and silently insert the semicolon.  */
16935   {
16936     cp_token *token = cp_lexer_peek_token (parser->lexer);
16937     bool want_semicolon = true;
16938
16939     switch (token->type)
16940       {
16941       case CPP_NAME:
16942       case CPP_SEMICOLON:
16943       case CPP_MULT:
16944       case CPP_AND:
16945       case CPP_OPEN_PAREN:
16946       case CPP_CLOSE_PAREN:
16947       case CPP_COMMA:
16948         want_semicolon = false;
16949         break;
16950
16951         /* While it's legal for type qualifiers and storage class
16952            specifiers to follow type definitions in the grammar, only
16953            compiler testsuites contain code like that.  Assume that if
16954            we see such code, then what we're really seeing is a case
16955            like:
16956
16957            class X { }
16958            const <type> var = ...;
16959
16960            or
16961
16962            class Y { }
16963            static <type> func (...) ...
16964
16965            i.e. the qualifier or specifier applies to the next
16966            declaration.  To do so, however, we need to look ahead one
16967            more token to see if *that* token is a type specifier.
16968
16969            This code could be improved to handle:
16970
16971            class Z { }
16972            static const <type> var = ...;  */
16973       case CPP_KEYWORD:
16974         if (keyword_is_decl_specifier (token->keyword))
16975           {
16976             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16977
16978             /* Handling user-defined types here would be nice, but very
16979                tricky.  */
16980             want_semicolon
16981               = (lookahead->type == CPP_KEYWORD
16982                  && keyword_begins_type_specifier (lookahead->keyword));
16983           }
16984         break;
16985       default:
16986         break;
16987       }
16988
16989     /* If we don't have a type, then something is very wrong and we
16990        shouldn't try to do anything clever.  Likewise for not seeing the
16991        closing brace.  */
16992     if (closing_brace && TYPE_P (type) && want_semicolon)
16993       {
16994         cp_token_position prev
16995           = cp_lexer_previous_token_position (parser->lexer);
16996         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16997         location_t loc = prev_token->location;
16998
16999         if (CLASSTYPE_DECLARED_CLASS (type))
17000           error_at (loc, "expected %<;%> after class definition");
17001         else if (TREE_CODE (type) == RECORD_TYPE)
17002           error_at (loc, "expected %<;%> after struct definition");
17003         else if (TREE_CODE (type) == UNION_TYPE)
17004           error_at (loc, "expected %<;%> after union definition");
17005         else
17006           gcc_unreachable ();
17007
17008         /* Unget one token and smash it to look as though we encountered
17009            a semicolon in the input stream.  */
17010         cp_lexer_set_token_position (parser->lexer, prev);
17011         token = cp_lexer_peek_token (parser->lexer);
17012         token->type = CPP_SEMICOLON;
17013         token->keyword = RID_MAX;
17014       }
17015   }
17016
17017   /* If this class is not itself within the scope of another class,
17018      then we need to parse the bodies of all of the queued function
17019      definitions.  Note that the queued functions defined in a class
17020      are not always processed immediately following the
17021      class-specifier for that class.  Consider:
17022
17023        struct A {
17024          struct B { void f() { sizeof (A); } };
17025        };
17026
17027      If `f' were processed before the processing of `A' were
17028      completed, there would be no way to compute the size of `A'.
17029      Note that the nesting we are interested in here is lexical --
17030      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17031      for:
17032
17033        struct A { struct B; };
17034        struct A::B { void f() { } };
17035
17036      there is no need to delay the parsing of `A::B::f'.  */
17037   if (--parser->num_classes_being_defined == 0)
17038     {
17039       tree fn;
17040       tree class_type = NULL_TREE;
17041       tree pushed_scope = NULL_TREE;
17042       unsigned ix;
17043       cp_default_arg_entry *e;
17044
17045       /* In a first pass, parse default arguments to the functions.
17046          Then, in a second pass, parse the bodies of the functions.
17047          This two-phased approach handles cases like:
17048
17049             struct S {
17050               void f() { g(); }
17051               void g(int i = 3);
17052             };
17053
17054          */
17055       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17056                         ix, e)
17057         {
17058           fn = e->decl;
17059           /* If there are default arguments that have not yet been processed,
17060              take care of them now.  */
17061           if (class_type != e->class_type)
17062             {
17063               if (pushed_scope)
17064                 pop_scope (pushed_scope);
17065               class_type = e->class_type;
17066               pushed_scope = push_scope (class_type);
17067             }
17068           /* Make sure that any template parameters are in scope.  */
17069           maybe_begin_member_template_processing (fn);
17070           /* Parse the default argument expressions.  */
17071           cp_parser_late_parsing_default_args (parser, fn);
17072           /* Remove any template parameters from the symbol table.  */
17073           maybe_end_member_template_processing ();
17074         }
17075       if (pushed_scope)
17076         pop_scope (pushed_scope);
17077       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17078       /* Now parse the body of the functions.  */
17079       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17080         cp_parser_late_parsing_for_member (parser, fn);
17081       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17082     }
17083
17084   /* Put back any saved access checks.  */
17085   pop_deferring_access_checks ();
17086
17087   /* Restore saved state.  */
17088   parser->in_function_body = saved_in_function_body;
17089   parser->num_template_parameter_lists
17090     = saved_num_template_parameter_lists;
17091   parser->in_unbraced_linkage_specification_p
17092     = saved_in_unbraced_linkage_specification_p;
17093
17094   return type;
17095 }
17096
17097 static tree
17098 cp_parser_class_specifier (cp_parser* parser)
17099 {
17100   tree ret;
17101   timevar_push (TV_PARSE_STRUCT);
17102   ret = cp_parser_class_specifier_1 (parser);
17103   timevar_pop (TV_PARSE_STRUCT);
17104   return ret;
17105 }
17106
17107 /* Parse a class-head.
17108
17109    class-head:
17110      class-key identifier [opt] base-clause [opt]
17111      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17112      class-key nested-name-specifier [opt] template-id
17113        base-clause [opt]
17114
17115    class-virt-specifier:
17116      final
17117
17118    GNU Extensions:
17119      class-key attributes identifier [opt] base-clause [opt]
17120      class-key attributes nested-name-specifier identifier base-clause [opt]
17121      class-key attributes nested-name-specifier [opt] template-id
17122        base-clause [opt]
17123
17124    Upon return BASES is initialized to the list of base classes (or
17125    NULL, if there are none) in the same form returned by
17126    cp_parser_base_clause.
17127
17128    Returns the TYPE of the indicated class.  Sets
17129    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17130    involving a nested-name-specifier was used, and FALSE otherwise.
17131
17132    Returns error_mark_node if this is not a class-head.
17133
17134    Returns NULL_TREE if the class-head is syntactically valid, but
17135    semantically invalid in a way that means we should skip the entire
17136    body of the class.  */
17137
17138 static tree
17139 cp_parser_class_head (cp_parser* parser,
17140                       bool* nested_name_specifier_p,
17141                       tree *attributes_p,
17142                       tree *bases)
17143 {
17144   tree nested_name_specifier;
17145   enum tag_types class_key;
17146   tree id = NULL_TREE;
17147   tree type = NULL_TREE;
17148   tree attributes;
17149   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17150   bool template_id_p = false;
17151   bool qualified_p = false;
17152   bool invalid_nested_name_p = false;
17153   bool invalid_explicit_specialization_p = false;
17154   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17155   tree pushed_scope = NULL_TREE;
17156   unsigned num_templates;
17157   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17158   /* Assume no nested-name-specifier will be present.  */
17159   *nested_name_specifier_p = false;
17160   /* Assume no template parameter lists will be used in defining the
17161      type.  */
17162   num_templates = 0;
17163   parser->colon_corrects_to_scope_p = false;
17164
17165   *bases = NULL_TREE;
17166
17167   /* Look for the class-key.  */
17168   class_key = cp_parser_class_key (parser);
17169   if (class_key == none_type)
17170     return error_mark_node;
17171
17172   /* Parse the attributes.  */
17173   attributes = cp_parser_attributes_opt (parser);
17174
17175   /* If the next token is `::', that is invalid -- but sometimes
17176      people do try to write:
17177
17178        struct ::S {};
17179
17180      Handle this gracefully by accepting the extra qualifier, and then
17181      issuing an error about it later if this really is a
17182      class-head.  If it turns out just to be an elaborated type
17183      specifier, remain silent.  */
17184   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17185     qualified_p = true;
17186
17187   push_deferring_access_checks (dk_no_check);
17188
17189   /* Determine the name of the class.  Begin by looking for an
17190      optional nested-name-specifier.  */
17191   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17192   nested_name_specifier
17193     = cp_parser_nested_name_specifier_opt (parser,
17194                                            /*typename_keyword_p=*/false,
17195                                            /*check_dependency_p=*/false,
17196                                            /*type_p=*/false,
17197                                            /*is_declaration=*/false);
17198   /* If there was a nested-name-specifier, then there *must* be an
17199      identifier.  */
17200   if (nested_name_specifier)
17201     {
17202       type_start_token = cp_lexer_peek_token (parser->lexer);
17203       /* Although the grammar says `identifier', it really means
17204          `class-name' or `template-name'.  You are only allowed to
17205          define a class that has already been declared with this
17206          syntax.
17207
17208          The proposed resolution for Core Issue 180 says that wherever
17209          you see `class T::X' you should treat `X' as a type-name.
17210
17211          It is OK to define an inaccessible class; for example:
17212
17213            class A { class B; };
17214            class A::B {};
17215
17216          We do not know if we will see a class-name, or a
17217          template-name.  We look for a class-name first, in case the
17218          class-name is a template-id; if we looked for the
17219          template-name first we would stop after the template-name.  */
17220       cp_parser_parse_tentatively (parser);
17221       type = cp_parser_class_name (parser,
17222                                    /*typename_keyword_p=*/false,
17223                                    /*template_keyword_p=*/false,
17224                                    class_type,
17225                                    /*check_dependency_p=*/false,
17226                                    /*class_head_p=*/true,
17227                                    /*is_declaration=*/false);
17228       /* If that didn't work, ignore the nested-name-specifier.  */
17229       if (!cp_parser_parse_definitely (parser))
17230         {
17231           invalid_nested_name_p = true;
17232           type_start_token = cp_lexer_peek_token (parser->lexer);
17233           id = cp_parser_identifier (parser);
17234           if (id == error_mark_node)
17235             id = NULL_TREE;
17236         }
17237       /* If we could not find a corresponding TYPE, treat this
17238          declaration like an unqualified declaration.  */
17239       if (type == error_mark_node)
17240         nested_name_specifier = NULL_TREE;
17241       /* Otherwise, count the number of templates used in TYPE and its
17242          containing scopes.  */
17243       else
17244         {
17245           tree scope;
17246
17247           for (scope = TREE_TYPE (type);
17248                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17249                scope = (TYPE_P (scope)
17250                         ? TYPE_CONTEXT (scope)
17251                         : DECL_CONTEXT (scope)))
17252             if (TYPE_P (scope)
17253                 && CLASS_TYPE_P (scope)
17254                 && CLASSTYPE_TEMPLATE_INFO (scope)
17255                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17256                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17257               ++num_templates;
17258         }
17259     }
17260   /* Otherwise, the identifier is optional.  */
17261   else
17262     {
17263       /* We don't know whether what comes next is a template-id,
17264          an identifier, or nothing at all.  */
17265       cp_parser_parse_tentatively (parser);
17266       /* Check for a template-id.  */
17267       type_start_token = cp_lexer_peek_token (parser->lexer);
17268       id = cp_parser_template_id (parser,
17269                                   /*template_keyword_p=*/false,
17270                                   /*check_dependency_p=*/true,
17271                                   /*is_declaration=*/true);
17272       /* If that didn't work, it could still be an identifier.  */
17273       if (!cp_parser_parse_definitely (parser))
17274         {
17275           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17276             {
17277               type_start_token = cp_lexer_peek_token (parser->lexer);
17278               id = cp_parser_identifier (parser);
17279             }
17280           else
17281             id = NULL_TREE;
17282         }
17283       else
17284         {
17285           template_id_p = true;
17286           ++num_templates;
17287         }
17288     }
17289
17290   pop_deferring_access_checks ();
17291
17292   if (id)
17293     {
17294       cp_parser_check_for_invalid_template_id (parser, id,
17295                                                type_start_token->location);
17296       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17297     }
17298
17299   /* If it's not a `:' or a `{' then we can't really be looking at a
17300      class-head, since a class-head only appears as part of a
17301      class-specifier.  We have to detect this situation before calling
17302      xref_tag, since that has irreversible side-effects.  */
17303   if (!cp_parser_next_token_starts_class_definition_p (parser))
17304     {
17305       cp_parser_error (parser, "expected %<{%> or %<:%>");
17306       type = error_mark_node;
17307       goto out;
17308     }
17309
17310   /* At this point, we're going ahead with the class-specifier, even
17311      if some other problem occurs.  */
17312   cp_parser_commit_to_tentative_parse (parser);
17313   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17314     {
17315       cp_parser_error (parser,
17316                        "cannot specify %<override%> for a class");
17317       type = error_mark_node;
17318       goto out;
17319     }
17320   /* Issue the error about the overly-qualified name now.  */
17321   if (qualified_p)
17322     {
17323       cp_parser_error (parser,
17324                        "global qualification of class name is invalid");
17325       type = error_mark_node;
17326       goto out;
17327     }
17328   else if (invalid_nested_name_p)
17329     {
17330       cp_parser_error (parser,
17331                        "qualified name does not name a class");
17332       type = error_mark_node;
17333       goto out;
17334     }
17335   else if (nested_name_specifier)
17336     {
17337       tree scope;
17338
17339       /* Reject typedef-names in class heads.  */
17340       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17341         {
17342           error_at (type_start_token->location,
17343                     "invalid class name in declaration of %qD",
17344                     type);
17345           type = NULL_TREE;
17346           goto done;
17347         }
17348
17349       /* Figure out in what scope the declaration is being placed.  */
17350       scope = current_scope ();
17351       /* If that scope does not contain the scope in which the
17352          class was originally declared, the program is invalid.  */
17353       if (scope && !is_ancestor (scope, nested_name_specifier))
17354         {
17355           if (at_namespace_scope_p ())
17356             error_at (type_start_token->location,
17357                       "declaration of %qD in namespace %qD which does not "
17358                       "enclose %qD",
17359                       type, scope, nested_name_specifier);
17360           else
17361             error_at (type_start_token->location,
17362                       "declaration of %qD in %qD which does not enclose %qD",
17363                       type, scope, nested_name_specifier);
17364           type = NULL_TREE;
17365           goto done;
17366         }
17367       /* [dcl.meaning]
17368
17369          A declarator-id shall not be qualified except for the
17370          definition of a ... nested class outside of its class
17371          ... [or] the definition or explicit instantiation of a
17372          class member of a namespace outside of its namespace.  */
17373       if (scope == nested_name_specifier)
17374         {
17375           permerror (nested_name_specifier_token_start->location,
17376                      "extra qualification not allowed");
17377           nested_name_specifier = NULL_TREE;
17378           num_templates = 0;
17379         }
17380     }
17381   /* An explicit-specialization must be preceded by "template <>".  If
17382      it is not, try to recover gracefully.  */
17383   if (at_namespace_scope_p ()
17384       && parser->num_template_parameter_lists == 0
17385       && template_id_p)
17386     {
17387       error_at (type_start_token->location,
17388                 "an explicit specialization must be preceded by %<template <>%>");
17389       invalid_explicit_specialization_p = true;
17390       /* Take the same action that would have been taken by
17391          cp_parser_explicit_specialization.  */
17392       ++parser->num_template_parameter_lists;
17393       begin_specialization ();
17394     }
17395   /* There must be no "return" statements between this point and the
17396      end of this function; set "type "to the correct return value and
17397      use "goto done;" to return.  */
17398   /* Make sure that the right number of template parameters were
17399      present.  */
17400   if (!cp_parser_check_template_parameters (parser, num_templates,
17401                                             type_start_token->location,
17402                                             /*declarator=*/NULL))
17403     {
17404       /* If something went wrong, there is no point in even trying to
17405          process the class-definition.  */
17406       type = NULL_TREE;
17407       goto done;
17408     }
17409
17410   /* Look up the type.  */
17411   if (template_id_p)
17412     {
17413       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17414           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17415               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17416         {
17417           error_at (type_start_token->location,
17418                     "function template %qD redeclared as a class template", id);
17419           type = error_mark_node;
17420         }
17421       else
17422         {
17423           type = TREE_TYPE (id);
17424           type = maybe_process_partial_specialization (type);
17425         }
17426       if (nested_name_specifier)
17427         pushed_scope = push_scope (nested_name_specifier);
17428     }
17429   else if (nested_name_specifier)
17430     {
17431       tree class_type;
17432
17433       /* Given:
17434
17435             template <typename T> struct S { struct T };
17436             template <typename T> struct S<T>::T { };
17437
17438          we will get a TYPENAME_TYPE when processing the definition of
17439          `S::T'.  We need to resolve it to the actual type before we
17440          try to define it.  */
17441       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17442         {
17443           class_type = resolve_typename_type (TREE_TYPE (type),
17444                                               /*only_current_p=*/false);
17445           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17446             type = TYPE_NAME (class_type);
17447           else
17448             {
17449               cp_parser_error (parser, "could not resolve typename type");
17450               type = error_mark_node;
17451             }
17452         }
17453
17454       if (maybe_process_partial_specialization (TREE_TYPE (type))
17455           == error_mark_node)
17456         {
17457           type = NULL_TREE;
17458           goto done;
17459         }
17460
17461       class_type = current_class_type;
17462       /* Enter the scope indicated by the nested-name-specifier.  */
17463       pushed_scope = push_scope (nested_name_specifier);
17464       /* Get the canonical version of this type.  */
17465       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17466       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17467           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17468         {
17469           type = push_template_decl (type);
17470           if (type == error_mark_node)
17471             {
17472               type = NULL_TREE;
17473               goto done;
17474             }
17475         }
17476
17477       type = TREE_TYPE (type);
17478       *nested_name_specifier_p = true;
17479     }
17480   else      /* The name is not a nested name.  */
17481     {
17482       /* If the class was unnamed, create a dummy name.  */
17483       if (!id)
17484         id = make_anon_name ();
17485       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17486                        parser->num_template_parameter_lists);
17487     }
17488
17489   /* Indicate whether this class was declared as a `class' or as a
17490      `struct'.  */
17491   if (TREE_CODE (type) == RECORD_TYPE)
17492     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17493   cp_parser_check_class_key (class_key, type);
17494
17495   /* If this type was already complete, and we see another definition,
17496      that's an error.  */
17497   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17498     {
17499       error_at (type_start_token->location, "redefinition of %q#T",
17500                 type);
17501       error_at (type_start_token->location, "previous definition of %q+#T",
17502                 type);
17503       type = NULL_TREE;
17504       goto done;
17505     }
17506   else if (type == error_mark_node)
17507     type = NULL_TREE;
17508
17509   /* We will have entered the scope containing the class; the names of
17510      base classes should be looked up in that context.  For example:
17511
17512        struct A { struct B {}; struct C; };
17513        struct A::C : B {};
17514
17515      is valid.  */
17516
17517   /* Get the list of base-classes, if there is one.  */
17518   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17519     *bases = cp_parser_base_clause (parser);
17520
17521  done:
17522   /* Leave the scope given by the nested-name-specifier.  We will
17523      enter the class scope itself while processing the members.  */
17524   if (pushed_scope)
17525     pop_scope (pushed_scope);
17526
17527   if (invalid_explicit_specialization_p)
17528     {
17529       end_specialization ();
17530       --parser->num_template_parameter_lists;
17531     }
17532
17533   if (type)
17534     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17535   *attributes_p = attributes;
17536   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17537     CLASSTYPE_FINAL (type) = 1;
17538  out:
17539   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17540   return type;
17541 }
17542
17543 /* Parse a class-key.
17544
17545    class-key:
17546      class
17547      struct
17548      union
17549
17550    Returns the kind of class-key specified, or none_type to indicate
17551    error.  */
17552
17553 static enum tag_types
17554 cp_parser_class_key (cp_parser* parser)
17555 {
17556   cp_token *token;
17557   enum tag_types tag_type;
17558
17559   /* Look for the class-key.  */
17560   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17561   if (!token)
17562     return none_type;
17563
17564   /* Check to see if the TOKEN is a class-key.  */
17565   tag_type = cp_parser_token_is_class_key (token);
17566   if (!tag_type)
17567     cp_parser_error (parser, "expected class-key");
17568   return tag_type;
17569 }
17570
17571 /* Parse an (optional) member-specification.
17572
17573    member-specification:
17574      member-declaration member-specification [opt]
17575      access-specifier : member-specification [opt]  */
17576
17577 static void
17578 cp_parser_member_specification_opt (cp_parser* parser)
17579 {
17580   while (true)
17581     {
17582       cp_token *token;
17583       enum rid keyword;
17584
17585       /* Peek at the next token.  */
17586       token = cp_lexer_peek_token (parser->lexer);
17587       /* If it's a `}', or EOF then we've seen all the members.  */
17588       if (token->type == CPP_CLOSE_BRACE
17589           || token->type == CPP_EOF
17590           || token->type == CPP_PRAGMA_EOL)
17591         break;
17592
17593       /* See if this token is a keyword.  */
17594       keyword = token->keyword;
17595       switch (keyword)
17596         {
17597         case RID_PUBLIC:
17598         case RID_PROTECTED:
17599         case RID_PRIVATE:
17600           /* Consume the access-specifier.  */
17601           cp_lexer_consume_token (parser->lexer);
17602           /* Remember which access-specifier is active.  */
17603           current_access_specifier = token->u.value;
17604           /* Look for the `:'.  */
17605           cp_parser_require (parser, CPP_COLON, RT_COLON);
17606           break;
17607
17608         default:
17609           /* Accept #pragmas at class scope.  */
17610           if (token->type == CPP_PRAGMA)
17611             {
17612               cp_parser_pragma (parser, pragma_external);
17613               break;
17614             }
17615
17616           /* Otherwise, the next construction must be a
17617              member-declaration.  */
17618           cp_parser_member_declaration (parser);
17619         }
17620     }
17621 }
17622
17623 /* Parse a member-declaration.
17624
17625    member-declaration:
17626      decl-specifier-seq [opt] member-declarator-list [opt] ;
17627      function-definition ; [opt]
17628      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17629      using-declaration
17630      template-declaration
17631
17632    member-declarator-list:
17633      member-declarator
17634      member-declarator-list , member-declarator
17635
17636    member-declarator:
17637      declarator pure-specifier [opt]
17638      declarator constant-initializer [opt]
17639      identifier [opt] : constant-expression
17640
17641    GNU Extensions:
17642
17643    member-declaration:
17644      __extension__ member-declaration
17645
17646    member-declarator:
17647      declarator attributes [opt] pure-specifier [opt]
17648      declarator attributes [opt] constant-initializer [opt]
17649      identifier [opt] attributes [opt] : constant-expression  
17650
17651    C++0x Extensions:
17652
17653    member-declaration:
17654      static_assert-declaration  */
17655
17656 static void
17657 cp_parser_member_declaration (cp_parser* parser)
17658 {
17659   cp_decl_specifier_seq decl_specifiers;
17660   tree prefix_attributes;
17661   tree decl;
17662   int declares_class_or_enum;
17663   bool friend_p;
17664   cp_token *token = NULL;
17665   cp_token *decl_spec_token_start = NULL;
17666   cp_token *initializer_token_start = NULL;
17667   int saved_pedantic;
17668   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17669
17670   /* Check for the `__extension__' keyword.  */
17671   if (cp_parser_extension_opt (parser, &saved_pedantic))
17672     {
17673       /* Recurse.  */
17674       cp_parser_member_declaration (parser);
17675       /* Restore the old value of the PEDANTIC flag.  */
17676       pedantic = saved_pedantic;
17677
17678       return;
17679     }
17680
17681   /* Check for a template-declaration.  */
17682   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17683     {
17684       /* An explicit specialization here is an error condition, and we
17685          expect the specialization handler to detect and report this.  */
17686       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17687           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17688         cp_parser_explicit_specialization (parser);
17689       else
17690         cp_parser_template_declaration (parser, /*member_p=*/true);
17691
17692       return;
17693     }
17694
17695   /* Check for a using-declaration.  */
17696   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17697     {
17698       /* Parse the using-declaration.  */
17699       cp_parser_using_declaration (parser,
17700                                    /*access_declaration_p=*/false);
17701       return;
17702     }
17703
17704   /* Check for @defs.  */
17705   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17706     {
17707       tree ivar, member;
17708       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17709       ivar = ivar_chains;
17710       while (ivar)
17711         {
17712           member = ivar;
17713           ivar = TREE_CHAIN (member);
17714           TREE_CHAIN (member) = NULL_TREE;
17715           finish_member_declaration (member);
17716         }
17717       return;
17718     }
17719
17720   /* If the next token is `static_assert' we have a static assertion.  */
17721   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17722     {
17723       cp_parser_static_assert (parser, /*member_p=*/true);
17724       return;
17725     }
17726
17727   parser->colon_corrects_to_scope_p = false;
17728
17729   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17730     goto out;
17731
17732   /* Parse the decl-specifier-seq.  */
17733   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17734   cp_parser_decl_specifier_seq (parser,
17735                                 CP_PARSER_FLAGS_OPTIONAL,
17736                                 &decl_specifiers,
17737                                 &declares_class_or_enum);
17738   prefix_attributes = decl_specifiers.attributes;
17739   decl_specifiers.attributes = NULL_TREE;
17740   /* Check for an invalid type-name.  */
17741   if (!decl_specifiers.any_type_specifiers_p
17742       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17743     goto out;
17744   /* If there is no declarator, then the decl-specifier-seq should
17745      specify a type.  */
17746   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17747     {
17748       /* If there was no decl-specifier-seq, and the next token is a
17749          `;', then we have something like:
17750
17751            struct S { ; };
17752
17753          [class.mem]
17754
17755          Each member-declaration shall declare at least one member
17756          name of the class.  */
17757       if (!decl_specifiers.any_specifiers_p)
17758         {
17759           cp_token *token = cp_lexer_peek_token (parser->lexer);
17760           if (!in_system_header_at (token->location))
17761             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17762         }
17763       else
17764         {
17765           tree type;
17766
17767           /* See if this declaration is a friend.  */
17768           friend_p = cp_parser_friend_p (&decl_specifiers);
17769           /* If there were decl-specifiers, check to see if there was
17770              a class-declaration.  */
17771           type = check_tag_decl (&decl_specifiers);
17772           /* Nested classes have already been added to the class, but
17773              a `friend' needs to be explicitly registered.  */
17774           if (friend_p)
17775             {
17776               /* If the `friend' keyword was present, the friend must
17777                  be introduced with a class-key.  */
17778                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17779                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17780                           "in C++03 a class-key must be used "
17781                           "when declaring a friend");
17782                /* In this case:
17783
17784                     template <typename T> struct A {
17785                       friend struct A<T>::B;
17786                     };
17787
17788                   A<T>::B will be represented by a TYPENAME_TYPE, and
17789                   therefore not recognized by check_tag_decl.  */
17790                if (!type)
17791                  {
17792                    type = decl_specifiers.type;
17793                    if (type && TREE_CODE (type) == TYPE_DECL)
17794                      type = TREE_TYPE (type);
17795                  }
17796                if (!type || !TYPE_P (type))
17797                  error_at (decl_spec_token_start->location,
17798                            "friend declaration does not name a class or "
17799                            "function");
17800                else
17801                  make_friend_class (current_class_type, type,
17802                                     /*complain=*/true);
17803             }
17804           /* If there is no TYPE, an error message will already have
17805              been issued.  */
17806           else if (!type || type == error_mark_node)
17807             ;
17808           /* An anonymous aggregate has to be handled specially; such
17809              a declaration really declares a data member (with a
17810              particular type), as opposed to a nested class.  */
17811           else if (ANON_AGGR_TYPE_P (type))
17812             {
17813               /* Remove constructors and such from TYPE, now that we
17814                  know it is an anonymous aggregate.  */
17815               fixup_anonymous_aggr (type);
17816               /* And make the corresponding data member.  */
17817               decl = build_decl (decl_spec_token_start->location,
17818                                  FIELD_DECL, NULL_TREE, type);
17819               /* Add it to the class.  */
17820               finish_member_declaration (decl);
17821             }
17822           else
17823             cp_parser_check_access_in_redeclaration
17824                                               (TYPE_NAME (type),
17825                                                decl_spec_token_start->location);
17826         }
17827     }
17828   else
17829     {
17830       bool assume_semicolon = false;
17831
17832       /* See if these declarations will be friends.  */
17833       friend_p = cp_parser_friend_p (&decl_specifiers);
17834
17835       /* Keep going until we hit the `;' at the end of the
17836          declaration.  */
17837       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17838         {
17839           tree attributes = NULL_TREE;
17840           tree first_attribute;
17841
17842           /* Peek at the next token.  */
17843           token = cp_lexer_peek_token (parser->lexer);
17844
17845           /* Check for a bitfield declaration.  */
17846           if (token->type == CPP_COLON
17847               || (token->type == CPP_NAME
17848                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17849                   == CPP_COLON))
17850             {
17851               tree identifier;
17852               tree width;
17853
17854               /* Get the name of the bitfield.  Note that we cannot just
17855                  check TOKEN here because it may have been invalidated by
17856                  the call to cp_lexer_peek_nth_token above.  */
17857               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17858                 identifier = cp_parser_identifier (parser);
17859               else
17860                 identifier = NULL_TREE;
17861
17862               /* Consume the `:' token.  */
17863               cp_lexer_consume_token (parser->lexer);
17864               /* Get the width of the bitfield.  */
17865               width
17866                 = cp_parser_constant_expression (parser,
17867                                                  /*allow_non_constant=*/false,
17868                                                  NULL);
17869
17870               /* Look for attributes that apply to the bitfield.  */
17871               attributes = cp_parser_attributes_opt (parser);
17872               /* Remember which attributes are prefix attributes and
17873                  which are not.  */
17874               first_attribute = attributes;
17875               /* Combine the attributes.  */
17876               attributes = chainon (prefix_attributes, attributes);
17877
17878               /* Create the bitfield declaration.  */
17879               decl = grokbitfield (identifier
17880                                    ? make_id_declarator (NULL_TREE,
17881                                                          identifier,
17882                                                          sfk_none)
17883                                    : NULL,
17884                                    &decl_specifiers,
17885                                    width,
17886                                    attributes);
17887             }
17888           else
17889             {
17890               cp_declarator *declarator;
17891               tree initializer;
17892               tree asm_specification;
17893               int ctor_dtor_or_conv_p;
17894
17895               /* Parse the declarator.  */
17896               declarator
17897                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17898                                         &ctor_dtor_or_conv_p,
17899                                         /*parenthesized_p=*/NULL,
17900                                         /*member_p=*/true);
17901
17902               /* If something went wrong parsing the declarator, make sure
17903                  that we at least consume some tokens.  */
17904               if (declarator == cp_error_declarator)
17905                 {
17906                   /* Skip to the end of the statement.  */
17907                   cp_parser_skip_to_end_of_statement (parser);
17908                   /* If the next token is not a semicolon, that is
17909                      probably because we just skipped over the body of
17910                      a function.  So, we consume a semicolon if
17911                      present, but do not issue an error message if it
17912                      is not present.  */
17913                   if (cp_lexer_next_token_is (parser->lexer,
17914                                               CPP_SEMICOLON))
17915                     cp_lexer_consume_token (parser->lexer);
17916                   goto out;
17917                 }
17918
17919               if (declares_class_or_enum & 2)
17920                 cp_parser_check_for_definition_in_return_type
17921                                             (declarator, decl_specifiers.type,
17922                                              decl_specifiers.type_location);
17923
17924               /* Look for an asm-specification.  */
17925               asm_specification = cp_parser_asm_specification_opt (parser);
17926               /* Look for attributes that apply to the declaration.  */
17927               attributes = cp_parser_attributes_opt (parser);
17928               /* Remember which attributes are prefix attributes and
17929                  which are not.  */
17930               first_attribute = attributes;
17931               /* Combine the attributes.  */
17932               attributes = chainon (prefix_attributes, attributes);
17933
17934               /* If it's an `=', then we have a constant-initializer or a
17935                  pure-specifier.  It is not correct to parse the
17936                  initializer before registering the member declaration
17937                  since the member declaration should be in scope while
17938                  its initializer is processed.  However, the rest of the
17939                  front end does not yet provide an interface that allows
17940                  us to handle this correctly.  */
17941               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17942                 {
17943                   /* In [class.mem]:
17944
17945                      A pure-specifier shall be used only in the declaration of
17946                      a virtual function.
17947
17948                      A member-declarator can contain a constant-initializer
17949                      only if it declares a static member of integral or
17950                      enumeration type.
17951
17952                      Therefore, if the DECLARATOR is for a function, we look
17953                      for a pure-specifier; otherwise, we look for a
17954                      constant-initializer.  When we call `grokfield', it will
17955                      perform more stringent semantics checks.  */
17956                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17957                   if (function_declarator_p (declarator))
17958                     initializer = cp_parser_pure_specifier (parser);
17959                   else
17960                     /* Parse the initializer.  */
17961                     initializer = cp_parser_constant_initializer (parser);
17962                 }
17963               /* Otherwise, there is no initializer.  */
17964               else
17965                 initializer = NULL_TREE;
17966
17967               /* See if we are probably looking at a function
17968                  definition.  We are certainly not looking at a
17969                  member-declarator.  Calling `grokfield' has
17970                  side-effects, so we must not do it unless we are sure
17971                  that we are looking at a member-declarator.  */
17972               if (cp_parser_token_starts_function_definition_p
17973                   (cp_lexer_peek_token (parser->lexer)))
17974                 {
17975                   /* The grammar does not allow a pure-specifier to be
17976                      used when a member function is defined.  (It is
17977                      possible that this fact is an oversight in the
17978                      standard, since a pure function may be defined
17979                      outside of the class-specifier.  */
17980                   if (initializer)
17981                     error_at (initializer_token_start->location,
17982                               "pure-specifier on function-definition");
17983                   decl = cp_parser_save_member_function_body (parser,
17984                                                               &decl_specifiers,
17985                                                               declarator,
17986                                                               attributes);
17987                   /* If the member was not a friend, declare it here.  */
17988                   if (!friend_p)
17989                     finish_member_declaration (decl);
17990                   /* Peek at the next token.  */
17991                   token = cp_lexer_peek_token (parser->lexer);
17992                   /* If the next token is a semicolon, consume it.  */
17993                   if (token->type == CPP_SEMICOLON)
17994                     cp_lexer_consume_token (parser->lexer);
17995                   goto out;
17996                 }
17997               else
17998                 if (declarator->kind == cdk_function)
17999                   declarator->id_loc = token->location;
18000                 /* Create the declaration.  */
18001                 decl = grokfield (declarator, &decl_specifiers,
18002                                   initializer, /*init_const_expr_p=*/true,
18003                                   asm_specification,
18004                                   attributes);
18005             }
18006
18007           /* Reset PREFIX_ATTRIBUTES.  */
18008           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18009             attributes = TREE_CHAIN (attributes);
18010           if (attributes)
18011             TREE_CHAIN (attributes) = NULL_TREE;
18012
18013           /* If there is any qualification still in effect, clear it
18014              now; we will be starting fresh with the next declarator.  */
18015           parser->scope = NULL_TREE;
18016           parser->qualifying_scope = NULL_TREE;
18017           parser->object_scope = NULL_TREE;
18018           /* If it's a `,', then there are more declarators.  */
18019           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18020             cp_lexer_consume_token (parser->lexer);
18021           /* If the next token isn't a `;', then we have a parse error.  */
18022           else if (cp_lexer_next_token_is_not (parser->lexer,
18023                                                CPP_SEMICOLON))
18024             {
18025               /* The next token might be a ways away from where the
18026                  actual semicolon is missing.  Find the previous token
18027                  and use that for our error position.  */
18028               cp_token *token = cp_lexer_previous_token (parser->lexer);
18029               error_at (token->location,
18030                         "expected %<;%> at end of member declaration");
18031
18032               /* Assume that the user meant to provide a semicolon.  If
18033                  we were to cp_parser_skip_to_end_of_statement, we might
18034                  skip to a semicolon inside a member function definition
18035                  and issue nonsensical error messages.  */
18036               assume_semicolon = true;
18037             }
18038
18039           if (decl)
18040             {
18041               /* Add DECL to the list of members.  */
18042               if (!friend_p)
18043                 finish_member_declaration (decl);
18044
18045               if (TREE_CODE (decl) == FUNCTION_DECL)
18046                 cp_parser_save_default_args (parser, decl);
18047             }
18048
18049           if (assume_semicolon)
18050             goto out;
18051         }
18052     }
18053
18054   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18055  out:
18056   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18057 }
18058
18059 /* Parse a pure-specifier.
18060
18061    pure-specifier:
18062      = 0
18063
18064    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18065    Otherwise, ERROR_MARK_NODE is returned.  */
18066
18067 static tree
18068 cp_parser_pure_specifier (cp_parser* parser)
18069 {
18070   cp_token *token;
18071
18072   /* Look for the `=' token.  */
18073   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18074     return error_mark_node;
18075   /* Look for the `0' token.  */
18076   token = cp_lexer_peek_token (parser->lexer);
18077
18078   if (token->type == CPP_EOF
18079       || token->type == CPP_PRAGMA_EOL)
18080     return error_mark_node;
18081
18082   cp_lexer_consume_token (parser->lexer);
18083
18084   /* Accept = default or = delete in c++0x mode.  */
18085   if (token->keyword == RID_DEFAULT
18086       || token->keyword == RID_DELETE)
18087     {
18088       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18089       return token->u.value;
18090     }
18091
18092   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18093   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18094     {
18095       cp_parser_error (parser,
18096                        "invalid pure specifier (only %<= 0%> is allowed)");
18097       cp_parser_skip_to_end_of_statement (parser);
18098       return error_mark_node;
18099     }
18100   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18101     {
18102       error_at (token->location, "templates may not be %<virtual%>");
18103       return error_mark_node;
18104     }
18105
18106   return integer_zero_node;
18107 }
18108
18109 /* Parse a constant-initializer.
18110
18111    constant-initializer:
18112      = constant-expression
18113
18114    Returns a representation of the constant-expression.  */
18115
18116 static tree
18117 cp_parser_constant_initializer (cp_parser* parser)
18118 {
18119   /* Look for the `=' token.  */
18120   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18121     return error_mark_node;
18122
18123   /* It is invalid to write:
18124
18125        struct S { static const int i = { 7 }; };
18126
18127      */
18128   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18129     {
18130       cp_parser_error (parser,
18131                        "a brace-enclosed initializer is not allowed here");
18132       /* Consume the opening brace.  */
18133       cp_lexer_consume_token (parser->lexer);
18134       /* Skip the initializer.  */
18135       cp_parser_skip_to_closing_brace (parser);
18136       /* Look for the trailing `}'.  */
18137       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18138
18139       return error_mark_node;
18140     }
18141
18142   return cp_parser_constant_expression (parser,
18143                                         /*allow_non_constant=*/false,
18144                                         NULL);
18145 }
18146
18147 /* Derived classes [gram.class.derived] */
18148
18149 /* Parse a base-clause.
18150
18151    base-clause:
18152      : base-specifier-list
18153
18154    base-specifier-list:
18155      base-specifier ... [opt]
18156      base-specifier-list , base-specifier ... [opt]
18157
18158    Returns a TREE_LIST representing the base-classes, in the order in
18159    which they were declared.  The representation of each node is as
18160    described by cp_parser_base_specifier.
18161
18162    In the case that no bases are specified, this function will return
18163    NULL_TREE, not ERROR_MARK_NODE.  */
18164
18165 static tree
18166 cp_parser_base_clause (cp_parser* parser)
18167 {
18168   tree bases = NULL_TREE;
18169
18170   /* Look for the `:' that begins the list.  */
18171   cp_parser_require (parser, CPP_COLON, RT_COLON);
18172
18173   /* Scan the base-specifier-list.  */
18174   while (true)
18175     {
18176       cp_token *token;
18177       tree base;
18178       bool pack_expansion_p = false;
18179
18180       /* Look for the base-specifier.  */
18181       base = cp_parser_base_specifier (parser);
18182       /* Look for the (optional) ellipsis. */
18183       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18184         {
18185           /* Consume the `...'. */
18186           cp_lexer_consume_token (parser->lexer);
18187
18188           pack_expansion_p = true;
18189         }
18190
18191       /* Add BASE to the front of the list.  */
18192       if (base != error_mark_node)
18193         {
18194           if (pack_expansion_p)
18195             /* Make this a pack expansion type. */
18196             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18197           
18198
18199           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18200             {
18201               TREE_CHAIN (base) = bases;
18202               bases = base;
18203             }
18204         }
18205       /* Peek at the next token.  */
18206       token = cp_lexer_peek_token (parser->lexer);
18207       /* If it's not a comma, then the list is complete.  */
18208       if (token->type != CPP_COMMA)
18209         break;
18210       /* Consume the `,'.  */
18211       cp_lexer_consume_token (parser->lexer);
18212     }
18213
18214   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18215      base class had a qualified name.  However, the next name that
18216      appears is certainly not qualified.  */
18217   parser->scope = NULL_TREE;
18218   parser->qualifying_scope = NULL_TREE;
18219   parser->object_scope = NULL_TREE;
18220
18221   return nreverse (bases);
18222 }
18223
18224 /* Parse a base-specifier.
18225
18226    base-specifier:
18227      :: [opt] nested-name-specifier [opt] class-name
18228      virtual access-specifier [opt] :: [opt] nested-name-specifier
18229        [opt] class-name
18230      access-specifier virtual [opt] :: [opt] nested-name-specifier
18231        [opt] class-name
18232
18233    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18234    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18235    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18236    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18237
18238 static tree
18239 cp_parser_base_specifier (cp_parser* parser)
18240 {
18241   cp_token *token;
18242   bool done = false;
18243   bool virtual_p = false;
18244   bool duplicate_virtual_error_issued_p = false;
18245   bool duplicate_access_error_issued_p = false;
18246   bool class_scope_p, template_p;
18247   tree access = access_default_node;
18248   tree type;
18249
18250   /* Process the optional `virtual' and `access-specifier'.  */
18251   while (!done)
18252     {
18253       /* Peek at the next token.  */
18254       token = cp_lexer_peek_token (parser->lexer);
18255       /* Process `virtual'.  */
18256       switch (token->keyword)
18257         {
18258         case RID_VIRTUAL:
18259           /* If `virtual' appears more than once, issue an error.  */
18260           if (virtual_p && !duplicate_virtual_error_issued_p)
18261             {
18262               cp_parser_error (parser,
18263                                "%<virtual%> specified more than once in base-specified");
18264               duplicate_virtual_error_issued_p = true;
18265             }
18266
18267           virtual_p = true;
18268
18269           /* Consume the `virtual' token.  */
18270           cp_lexer_consume_token (parser->lexer);
18271
18272           break;
18273
18274         case RID_PUBLIC:
18275         case RID_PROTECTED:
18276         case RID_PRIVATE:
18277           /* If more than one access specifier appears, issue an
18278              error.  */
18279           if (access != access_default_node
18280               && !duplicate_access_error_issued_p)
18281             {
18282               cp_parser_error (parser,
18283                                "more than one access specifier in base-specified");
18284               duplicate_access_error_issued_p = true;
18285             }
18286
18287           access = ridpointers[(int) token->keyword];
18288
18289           /* Consume the access-specifier.  */
18290           cp_lexer_consume_token (parser->lexer);
18291
18292           break;
18293
18294         default:
18295           done = true;
18296           break;
18297         }
18298     }
18299   /* It is not uncommon to see programs mechanically, erroneously, use
18300      the 'typename' keyword to denote (dependent) qualified types
18301      as base classes.  */
18302   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18303     {
18304       token = cp_lexer_peek_token (parser->lexer);
18305       if (!processing_template_decl)
18306         error_at (token->location,
18307                   "keyword %<typename%> not allowed outside of templates");
18308       else
18309         error_at (token->location,
18310                   "keyword %<typename%> not allowed in this context "
18311                   "(the base class is implicitly a type)");
18312       cp_lexer_consume_token (parser->lexer);
18313     }
18314
18315   /* Look for the optional `::' operator.  */
18316   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18317   /* Look for the nested-name-specifier.  The simplest way to
18318      implement:
18319
18320        [temp.res]
18321
18322        The keyword `typename' is not permitted in a base-specifier or
18323        mem-initializer; in these contexts a qualified name that
18324        depends on a template-parameter is implicitly assumed to be a
18325        type name.
18326
18327      is to pretend that we have seen the `typename' keyword at this
18328      point.  */
18329   cp_parser_nested_name_specifier_opt (parser,
18330                                        /*typename_keyword_p=*/true,
18331                                        /*check_dependency_p=*/true,
18332                                        typename_type,
18333                                        /*is_declaration=*/true);
18334   /* If the base class is given by a qualified name, assume that names
18335      we see are type names or templates, as appropriate.  */
18336   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18337   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18338
18339   /* Finally, look for the class-name.  */
18340   type = cp_parser_class_name (parser,
18341                                class_scope_p,
18342                                template_p,
18343                                typename_type,
18344                                /*check_dependency_p=*/true,
18345                                /*class_head_p=*/false,
18346                                /*is_declaration=*/true);
18347
18348   if (type == error_mark_node)
18349     return error_mark_node;
18350
18351   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18352 }
18353
18354 /* Exception handling [gram.exception] */
18355
18356 /* Parse an (optional) exception-specification.
18357
18358    exception-specification:
18359      throw ( type-id-list [opt] )
18360
18361    Returns a TREE_LIST representing the exception-specification.  The
18362    TREE_VALUE of each node is a type.  */
18363
18364 static tree
18365 cp_parser_exception_specification_opt (cp_parser* parser)
18366 {
18367   cp_token *token;
18368   tree type_id_list;
18369   const char *saved_message;
18370
18371   /* Peek at the next token.  */
18372   token = cp_lexer_peek_token (parser->lexer);
18373
18374   /* Is it a noexcept-specification?  */
18375   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18376     {
18377       tree expr;
18378       cp_lexer_consume_token (parser->lexer);
18379
18380       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18381         {
18382           cp_lexer_consume_token (parser->lexer);
18383
18384           /* Types may not be defined in an exception-specification.  */
18385           saved_message = parser->type_definition_forbidden_message;
18386           parser->type_definition_forbidden_message
18387             = G_("types may not be defined in an exception-specification");
18388
18389           expr = cp_parser_constant_expression (parser, false, NULL);
18390
18391           /* Restore the saved message.  */
18392           parser->type_definition_forbidden_message = saved_message;
18393
18394           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18395         }
18396       else
18397         expr = boolean_true_node;
18398
18399       return build_noexcept_spec (expr, tf_warning_or_error);
18400     }
18401
18402   /* If it's not `throw', then there's no exception-specification.  */
18403   if (!cp_parser_is_keyword (token, RID_THROW))
18404     return NULL_TREE;
18405
18406 #if 0
18407   /* Enable this once a lot of code has transitioned to noexcept?  */
18408   if (cxx_dialect == cxx0x && !in_system_header)
18409     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18410              "deprecated in C++0x; use %<noexcept%> instead");
18411 #endif
18412
18413   /* Consume the `throw'.  */
18414   cp_lexer_consume_token (parser->lexer);
18415
18416   /* Look for the `('.  */
18417   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18418
18419   /* Peek at the next token.  */
18420   token = cp_lexer_peek_token (parser->lexer);
18421   /* If it's not a `)', then there is a type-id-list.  */
18422   if (token->type != CPP_CLOSE_PAREN)
18423     {
18424       /* Types may not be defined in an exception-specification.  */
18425       saved_message = parser->type_definition_forbidden_message;
18426       parser->type_definition_forbidden_message
18427         = G_("types may not be defined in an exception-specification");
18428       /* Parse the type-id-list.  */
18429       type_id_list = cp_parser_type_id_list (parser);
18430       /* Restore the saved message.  */
18431       parser->type_definition_forbidden_message = saved_message;
18432     }
18433   else
18434     type_id_list = empty_except_spec;
18435
18436   /* Look for the `)'.  */
18437   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18438
18439   return type_id_list;
18440 }
18441
18442 /* Parse an (optional) type-id-list.
18443
18444    type-id-list:
18445      type-id ... [opt]
18446      type-id-list , type-id ... [opt]
18447
18448    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18449    in the order that the types were presented.  */
18450
18451 static tree
18452 cp_parser_type_id_list (cp_parser* parser)
18453 {
18454   tree types = NULL_TREE;
18455
18456   while (true)
18457     {
18458       cp_token *token;
18459       tree type;
18460
18461       /* Get the next type-id.  */
18462       type = cp_parser_type_id (parser);
18463       /* Parse the optional ellipsis. */
18464       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18465         {
18466           /* Consume the `...'. */
18467           cp_lexer_consume_token (parser->lexer);
18468
18469           /* Turn the type into a pack expansion expression. */
18470           type = make_pack_expansion (type);
18471         }
18472       /* Add it to the list.  */
18473       types = add_exception_specifier (types, type, /*complain=*/1);
18474       /* Peek at the next token.  */
18475       token = cp_lexer_peek_token (parser->lexer);
18476       /* If it is not a `,', we are done.  */
18477       if (token->type != CPP_COMMA)
18478         break;
18479       /* Consume the `,'.  */
18480       cp_lexer_consume_token (parser->lexer);
18481     }
18482
18483   return nreverse (types);
18484 }
18485
18486 /* Parse a try-block.
18487
18488    try-block:
18489      try compound-statement handler-seq  */
18490
18491 static tree
18492 cp_parser_try_block (cp_parser* parser)
18493 {
18494   tree try_block;
18495
18496   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18497   try_block = begin_try_block ();
18498   cp_parser_compound_statement (parser, NULL, true, false);
18499   finish_try_block (try_block);
18500   cp_parser_handler_seq (parser);
18501   finish_handler_sequence (try_block);
18502
18503   return try_block;
18504 }
18505
18506 /* Parse a function-try-block.
18507
18508    function-try-block:
18509      try ctor-initializer [opt] function-body handler-seq  */
18510
18511 static bool
18512 cp_parser_function_try_block (cp_parser* parser)
18513 {
18514   tree compound_stmt;
18515   tree try_block;
18516   bool ctor_initializer_p;
18517
18518   /* Look for the `try' keyword.  */
18519   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18520     return false;
18521   /* Let the rest of the front end know where we are.  */
18522   try_block = begin_function_try_block (&compound_stmt);
18523   /* Parse the function-body.  */
18524   ctor_initializer_p
18525     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18526   /* We're done with the `try' part.  */
18527   finish_function_try_block (try_block);
18528   /* Parse the handlers.  */
18529   cp_parser_handler_seq (parser);
18530   /* We're done with the handlers.  */
18531   finish_function_handler_sequence (try_block, compound_stmt);
18532
18533   return ctor_initializer_p;
18534 }
18535
18536 /* Parse a handler-seq.
18537
18538    handler-seq:
18539      handler handler-seq [opt]  */
18540
18541 static void
18542 cp_parser_handler_seq (cp_parser* parser)
18543 {
18544   while (true)
18545     {
18546       cp_token *token;
18547
18548       /* Parse the handler.  */
18549       cp_parser_handler (parser);
18550       /* Peek at the next token.  */
18551       token = cp_lexer_peek_token (parser->lexer);
18552       /* If it's not `catch' then there are no more handlers.  */
18553       if (!cp_parser_is_keyword (token, RID_CATCH))
18554         break;
18555     }
18556 }
18557
18558 /* Parse a handler.
18559
18560    handler:
18561      catch ( exception-declaration ) compound-statement  */
18562
18563 static void
18564 cp_parser_handler (cp_parser* parser)
18565 {
18566   tree handler;
18567   tree declaration;
18568
18569   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18570   handler = begin_handler ();
18571   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18572   declaration = cp_parser_exception_declaration (parser);
18573   finish_handler_parms (declaration, handler);
18574   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18575   cp_parser_compound_statement (parser, NULL, false, false);
18576   finish_handler (handler);
18577 }
18578
18579 /* Parse an exception-declaration.
18580
18581    exception-declaration:
18582      type-specifier-seq declarator
18583      type-specifier-seq abstract-declarator
18584      type-specifier-seq
18585      ...
18586
18587    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18588    ellipsis variant is used.  */
18589
18590 static tree
18591 cp_parser_exception_declaration (cp_parser* parser)
18592 {
18593   cp_decl_specifier_seq type_specifiers;
18594   cp_declarator *declarator;
18595   const char *saved_message;
18596
18597   /* If it's an ellipsis, it's easy to handle.  */
18598   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18599     {
18600       /* Consume the `...' token.  */
18601       cp_lexer_consume_token (parser->lexer);
18602       return NULL_TREE;
18603     }
18604
18605   /* Types may not be defined in exception-declarations.  */
18606   saved_message = parser->type_definition_forbidden_message;
18607   parser->type_definition_forbidden_message
18608     = G_("types may not be defined in exception-declarations");
18609
18610   /* Parse the type-specifier-seq.  */
18611   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18612                                 /*is_trailing_return=*/false,
18613                                 &type_specifiers);
18614   /* If it's a `)', then there is no declarator.  */
18615   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18616     declarator = NULL;
18617   else
18618     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18619                                        /*ctor_dtor_or_conv_p=*/NULL,
18620                                        /*parenthesized_p=*/NULL,
18621                                        /*member_p=*/false);
18622
18623   /* Restore the saved message.  */
18624   parser->type_definition_forbidden_message = saved_message;
18625
18626   if (!type_specifiers.any_specifiers_p)
18627     return error_mark_node;
18628
18629   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18630 }
18631
18632 /* Parse a throw-expression.
18633
18634    throw-expression:
18635      throw assignment-expression [opt]
18636
18637    Returns a THROW_EXPR representing the throw-expression.  */
18638
18639 static tree
18640 cp_parser_throw_expression (cp_parser* parser)
18641 {
18642   tree expression;
18643   cp_token* token;
18644
18645   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18646   token = cp_lexer_peek_token (parser->lexer);
18647   /* Figure out whether or not there is an assignment-expression
18648      following the "throw" keyword.  */
18649   if (token->type == CPP_COMMA
18650       || token->type == CPP_SEMICOLON
18651       || token->type == CPP_CLOSE_PAREN
18652       || token->type == CPP_CLOSE_SQUARE
18653       || token->type == CPP_CLOSE_BRACE
18654       || token->type == CPP_COLON)
18655     expression = NULL_TREE;
18656   else
18657     expression = cp_parser_assignment_expression (parser,
18658                                                   /*cast_p=*/false, NULL);
18659
18660   return build_throw (expression);
18661 }
18662
18663 /* GNU Extensions */
18664
18665 /* Parse an (optional) asm-specification.
18666
18667    asm-specification:
18668      asm ( string-literal )
18669
18670    If the asm-specification is present, returns a STRING_CST
18671    corresponding to the string-literal.  Otherwise, returns
18672    NULL_TREE.  */
18673
18674 static tree
18675 cp_parser_asm_specification_opt (cp_parser* parser)
18676 {
18677   cp_token *token;
18678   tree asm_specification;
18679
18680   /* Peek at the next token.  */
18681   token = cp_lexer_peek_token (parser->lexer);
18682   /* If the next token isn't the `asm' keyword, then there's no
18683      asm-specification.  */
18684   if (!cp_parser_is_keyword (token, RID_ASM))
18685     return NULL_TREE;
18686
18687   /* Consume the `asm' token.  */
18688   cp_lexer_consume_token (parser->lexer);
18689   /* Look for the `('.  */
18690   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18691
18692   /* Look for the string-literal.  */
18693   asm_specification = cp_parser_string_literal (parser, false, false);
18694
18695   /* Look for the `)'.  */
18696   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18697
18698   return asm_specification;
18699 }
18700
18701 /* Parse an asm-operand-list.
18702
18703    asm-operand-list:
18704      asm-operand
18705      asm-operand-list , asm-operand
18706
18707    asm-operand:
18708      string-literal ( expression )
18709      [ string-literal ] string-literal ( expression )
18710
18711    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18712    each node is the expression.  The TREE_PURPOSE is itself a
18713    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18714    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18715    is a STRING_CST for the string literal before the parenthesis. Returns
18716    ERROR_MARK_NODE if any of the operands are invalid.  */
18717
18718 static tree
18719 cp_parser_asm_operand_list (cp_parser* parser)
18720 {
18721   tree asm_operands = NULL_TREE;
18722   bool invalid_operands = false;
18723
18724   while (true)
18725     {
18726       tree string_literal;
18727       tree expression;
18728       tree name;
18729
18730       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18731         {
18732           /* Consume the `[' token.  */
18733           cp_lexer_consume_token (parser->lexer);
18734           /* Read the operand name.  */
18735           name = cp_parser_identifier (parser);
18736           if (name != error_mark_node)
18737             name = build_string (IDENTIFIER_LENGTH (name),
18738                                  IDENTIFIER_POINTER (name));
18739           /* Look for the closing `]'.  */
18740           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18741         }
18742       else
18743         name = NULL_TREE;
18744       /* Look for the string-literal.  */
18745       string_literal = cp_parser_string_literal (parser, false, false);
18746
18747       /* Look for the `('.  */
18748       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18749       /* Parse the expression.  */
18750       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18751       /* Look for the `)'.  */
18752       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18753
18754       if (name == error_mark_node 
18755           || string_literal == error_mark_node 
18756           || expression == error_mark_node)
18757         invalid_operands = true;
18758
18759       /* Add this operand to the list.  */
18760       asm_operands = tree_cons (build_tree_list (name, string_literal),
18761                                 expression,
18762                                 asm_operands);
18763       /* If the next token is not a `,', there are no more
18764          operands.  */
18765       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18766         break;
18767       /* Consume the `,'.  */
18768       cp_lexer_consume_token (parser->lexer);
18769     }
18770
18771   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18772 }
18773
18774 /* Parse an asm-clobber-list.
18775
18776    asm-clobber-list:
18777      string-literal
18778      asm-clobber-list , string-literal
18779
18780    Returns a TREE_LIST, indicating the clobbers in the order that they
18781    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18782
18783 static tree
18784 cp_parser_asm_clobber_list (cp_parser* parser)
18785 {
18786   tree clobbers = NULL_TREE;
18787
18788   while (true)
18789     {
18790       tree string_literal;
18791
18792       /* Look for the string literal.  */
18793       string_literal = cp_parser_string_literal (parser, false, false);
18794       /* Add it to the list.  */
18795       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18796       /* If the next token is not a `,', then the list is
18797          complete.  */
18798       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18799         break;
18800       /* Consume the `,' token.  */
18801       cp_lexer_consume_token (parser->lexer);
18802     }
18803
18804   return clobbers;
18805 }
18806
18807 /* Parse an asm-label-list.
18808
18809    asm-label-list:
18810      identifier
18811      asm-label-list , identifier
18812
18813    Returns a TREE_LIST, indicating the labels in the order that they
18814    appeared.  The TREE_VALUE of each node is a label.  */
18815
18816 static tree
18817 cp_parser_asm_label_list (cp_parser* parser)
18818 {
18819   tree labels = NULL_TREE;
18820
18821   while (true)
18822     {
18823       tree identifier, label, name;
18824
18825       /* Look for the identifier.  */
18826       identifier = cp_parser_identifier (parser);
18827       if (!error_operand_p (identifier))
18828         {
18829           label = lookup_label (identifier);
18830           if (TREE_CODE (label) == LABEL_DECL)
18831             {
18832               TREE_USED (label) = 1;
18833               check_goto (label);
18834               name = build_string (IDENTIFIER_LENGTH (identifier),
18835                                    IDENTIFIER_POINTER (identifier));
18836               labels = tree_cons (name, label, labels);
18837             }
18838         }
18839       /* If the next token is not a `,', then the list is
18840          complete.  */
18841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18842         break;
18843       /* Consume the `,' token.  */
18844       cp_lexer_consume_token (parser->lexer);
18845     }
18846
18847   return nreverse (labels);
18848 }
18849
18850 /* Parse an (optional) series of attributes.
18851
18852    attributes:
18853      attributes attribute
18854
18855    attribute:
18856      __attribute__ (( attribute-list [opt] ))
18857
18858    The return value is as for cp_parser_attribute_list.  */
18859
18860 static tree
18861 cp_parser_attributes_opt (cp_parser* parser)
18862 {
18863   tree attributes = NULL_TREE;
18864
18865   while (true)
18866     {
18867       cp_token *token;
18868       tree attribute_list;
18869
18870       /* Peek at the next token.  */
18871       token = cp_lexer_peek_token (parser->lexer);
18872       /* If it's not `__attribute__', then we're done.  */
18873       if (token->keyword != RID_ATTRIBUTE)
18874         break;
18875
18876       /* Consume the `__attribute__' keyword.  */
18877       cp_lexer_consume_token (parser->lexer);
18878       /* Look for the two `(' tokens.  */
18879       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18880       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18881
18882       /* Peek at the next token.  */
18883       token = cp_lexer_peek_token (parser->lexer);
18884       if (token->type != CPP_CLOSE_PAREN)
18885         /* Parse the attribute-list.  */
18886         attribute_list = cp_parser_attribute_list (parser);
18887       else
18888         /* If the next token is a `)', then there is no attribute
18889            list.  */
18890         attribute_list = NULL;
18891
18892       /* Look for the two `)' tokens.  */
18893       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18894       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18895
18896       /* Add these new attributes to the list.  */
18897       attributes = chainon (attributes, attribute_list);
18898     }
18899
18900   return attributes;
18901 }
18902
18903 /* Parse an attribute-list.
18904
18905    attribute-list:
18906      attribute
18907      attribute-list , attribute
18908
18909    attribute:
18910      identifier
18911      identifier ( identifier )
18912      identifier ( identifier , expression-list )
18913      identifier ( expression-list )
18914
18915    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18916    to an attribute.  The TREE_PURPOSE of each node is the identifier
18917    indicating which attribute is in use.  The TREE_VALUE represents
18918    the arguments, if any.  */
18919
18920 static tree
18921 cp_parser_attribute_list (cp_parser* parser)
18922 {
18923   tree attribute_list = NULL_TREE;
18924   bool save_translate_strings_p = parser->translate_strings_p;
18925
18926   parser->translate_strings_p = false;
18927   while (true)
18928     {
18929       cp_token *token;
18930       tree identifier;
18931       tree attribute;
18932
18933       /* Look for the identifier.  We also allow keywords here; for
18934          example `__attribute__ ((const))' is legal.  */
18935       token = cp_lexer_peek_token (parser->lexer);
18936       if (token->type == CPP_NAME
18937           || token->type == CPP_KEYWORD)
18938         {
18939           tree arguments = NULL_TREE;
18940
18941           /* Consume the token.  */
18942           token = cp_lexer_consume_token (parser->lexer);
18943
18944           /* Save away the identifier that indicates which attribute
18945              this is.  */
18946           identifier = (token->type == CPP_KEYWORD) 
18947             /* For keywords, use the canonical spelling, not the
18948                parsed identifier.  */
18949             ? ridpointers[(int) token->keyword]
18950             : token->u.value;
18951           
18952           attribute = build_tree_list (identifier, NULL_TREE);
18953
18954           /* Peek at the next token.  */
18955           token = cp_lexer_peek_token (parser->lexer);
18956           /* If it's an `(', then parse the attribute arguments.  */
18957           if (token->type == CPP_OPEN_PAREN)
18958             {
18959               VEC(tree,gc) *vec;
18960               int attr_flag = (attribute_takes_identifier_p (identifier)
18961                                ? id_attr : normal_attr);
18962               vec = cp_parser_parenthesized_expression_list
18963                     (parser, attr_flag, /*cast_p=*/false,
18964                      /*allow_expansion_p=*/false,
18965                      /*non_constant_p=*/NULL);
18966               if (vec == NULL)
18967                 arguments = error_mark_node;
18968               else
18969                 {
18970                   arguments = build_tree_list_vec (vec);
18971                   release_tree_vector (vec);
18972                 }
18973               /* Save the arguments away.  */
18974               TREE_VALUE (attribute) = arguments;
18975             }
18976
18977           if (arguments != error_mark_node)
18978             {
18979               /* Add this attribute to the list.  */
18980               TREE_CHAIN (attribute) = attribute_list;
18981               attribute_list = attribute;
18982             }
18983
18984           token = cp_lexer_peek_token (parser->lexer);
18985         }
18986       /* Now, look for more attributes.  If the next token isn't a
18987          `,', we're done.  */
18988       if (token->type != CPP_COMMA)
18989         break;
18990
18991       /* Consume the comma and keep going.  */
18992       cp_lexer_consume_token (parser->lexer);
18993     }
18994   parser->translate_strings_p = save_translate_strings_p;
18995
18996   /* We built up the list in reverse order.  */
18997   return nreverse (attribute_list);
18998 }
18999
19000 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19001    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19002    current value of the PEDANTIC flag, regardless of whether or not
19003    the `__extension__' keyword is present.  The caller is responsible
19004    for restoring the value of the PEDANTIC flag.  */
19005
19006 static bool
19007 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19008 {
19009   /* Save the old value of the PEDANTIC flag.  */
19010   *saved_pedantic = pedantic;
19011
19012   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19013     {
19014       /* Consume the `__extension__' token.  */
19015       cp_lexer_consume_token (parser->lexer);
19016       /* We're not being pedantic while the `__extension__' keyword is
19017          in effect.  */
19018       pedantic = 0;
19019
19020       return true;
19021     }
19022
19023   return false;
19024 }
19025
19026 /* Parse a label declaration.
19027
19028    label-declaration:
19029      __label__ label-declarator-seq ;
19030
19031    label-declarator-seq:
19032      identifier , label-declarator-seq
19033      identifier  */
19034
19035 static void
19036 cp_parser_label_declaration (cp_parser* parser)
19037 {
19038   /* Look for the `__label__' keyword.  */
19039   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19040
19041   while (true)
19042     {
19043       tree identifier;
19044
19045       /* Look for an identifier.  */
19046       identifier = cp_parser_identifier (parser);
19047       /* If we failed, stop.  */
19048       if (identifier == error_mark_node)
19049         break;
19050       /* Declare it as a label.  */
19051       finish_label_decl (identifier);
19052       /* If the next token is a `;', stop.  */
19053       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19054         break;
19055       /* Look for the `,' separating the label declarations.  */
19056       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19057     }
19058
19059   /* Look for the final `;'.  */
19060   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19061 }
19062
19063 /* Support Functions */
19064
19065 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19066    NAME should have one of the representations used for an
19067    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19068    is returned.  If PARSER->SCOPE is a dependent type, then a
19069    SCOPE_REF is returned.
19070
19071    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19072    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19073    was formed.  Abstractly, such entities should not be passed to this
19074    function, because they do not need to be looked up, but it is
19075    simpler to check for this special case here, rather than at the
19076    call-sites.
19077
19078    In cases not explicitly covered above, this function returns a
19079    DECL, OVERLOAD, or baselink representing the result of the lookup.
19080    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19081    is returned.
19082
19083    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19084    (e.g., "struct") that was used.  In that case bindings that do not
19085    refer to types are ignored.
19086
19087    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19088    ignored.
19089
19090    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19091    are ignored.
19092
19093    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19094    types.
19095
19096    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19097    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19098    NULL_TREE otherwise.  */
19099
19100 static tree
19101 cp_parser_lookup_name (cp_parser *parser, tree name,
19102                        enum tag_types tag_type,
19103                        bool is_template,
19104                        bool is_namespace,
19105                        bool check_dependency,
19106                        tree *ambiguous_decls,
19107                        location_t name_location)
19108 {
19109   int flags = 0;
19110   tree decl;
19111   tree object_type = parser->context->object_type;
19112
19113   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19114     flags |= LOOKUP_COMPLAIN;
19115
19116   /* Assume that the lookup will be unambiguous.  */
19117   if (ambiguous_decls)
19118     *ambiguous_decls = NULL_TREE;
19119
19120   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19121      no longer valid.  Note that if we are parsing tentatively, and
19122      the parse fails, OBJECT_TYPE will be automatically restored.  */
19123   parser->context->object_type = NULL_TREE;
19124
19125   if (name == error_mark_node)
19126     return error_mark_node;
19127
19128   /* A template-id has already been resolved; there is no lookup to
19129      do.  */
19130   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19131     return name;
19132   if (BASELINK_P (name))
19133     {
19134       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19135                   == TEMPLATE_ID_EXPR);
19136       return name;
19137     }
19138
19139   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19140      it should already have been checked to make sure that the name
19141      used matches the type being destroyed.  */
19142   if (TREE_CODE (name) == BIT_NOT_EXPR)
19143     {
19144       tree type;
19145
19146       /* Figure out to which type this destructor applies.  */
19147       if (parser->scope)
19148         type = parser->scope;
19149       else if (object_type)
19150         type = object_type;
19151       else
19152         type = current_class_type;
19153       /* If that's not a class type, there is no destructor.  */
19154       if (!type || !CLASS_TYPE_P (type))
19155         return error_mark_node;
19156       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19157         lazily_declare_fn (sfk_destructor, type);
19158       if (!CLASSTYPE_DESTRUCTORS (type))
19159           return error_mark_node;
19160       /* If it was a class type, return the destructor.  */
19161       return CLASSTYPE_DESTRUCTORS (type);
19162     }
19163
19164   /* By this point, the NAME should be an ordinary identifier.  If
19165      the id-expression was a qualified name, the qualifying scope is
19166      stored in PARSER->SCOPE at this point.  */
19167   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19168
19169   /* Perform the lookup.  */
19170   if (parser->scope)
19171     {
19172       bool dependent_p;
19173
19174       if (parser->scope == error_mark_node)
19175         return error_mark_node;
19176
19177       /* If the SCOPE is dependent, the lookup must be deferred until
19178          the template is instantiated -- unless we are explicitly
19179          looking up names in uninstantiated templates.  Even then, we
19180          cannot look up the name if the scope is not a class type; it
19181          might, for example, be a template type parameter.  */
19182       dependent_p = (TYPE_P (parser->scope)
19183                      && dependent_scope_p (parser->scope));
19184       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19185           && dependent_p)
19186         /* Defer lookup.  */
19187         decl = error_mark_node;
19188       else
19189         {
19190           tree pushed_scope = NULL_TREE;
19191
19192           /* If PARSER->SCOPE is a dependent type, then it must be a
19193              class type, and we must not be checking dependencies;
19194              otherwise, we would have processed this lookup above.  So
19195              that PARSER->SCOPE is not considered a dependent base by
19196              lookup_member, we must enter the scope here.  */
19197           if (dependent_p)
19198             pushed_scope = push_scope (parser->scope);
19199
19200           /* If the PARSER->SCOPE is a template specialization, it
19201              may be instantiated during name lookup.  In that case,
19202              errors may be issued.  Even if we rollback the current
19203              tentative parse, those errors are valid.  */
19204           decl = lookup_qualified_name (parser->scope, name,
19205                                         tag_type != none_type,
19206                                         /*complain=*/true);
19207
19208           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19209              lookup result and the nested-name-specifier nominates a class C:
19210                * if the name specified after the nested-name-specifier, when
19211                looked up in C, is the injected-class-name of C (Clause 9), or
19212                * if the name specified after the nested-name-specifier is the
19213                same as the identifier or the simple-template-id's template-
19214                name in the last component of the nested-name-specifier,
19215              the name is instead considered to name the constructor of
19216              class C. [ Note: for example, the constructor is not an
19217              acceptable lookup result in an elaborated-type-specifier so
19218              the constructor would not be used in place of the
19219              injected-class-name. --end note ] Such a constructor name
19220              shall be used only in the declarator-id of a declaration that
19221              names a constructor or in a using-declaration.  */
19222           if (tag_type == none_type
19223               && DECL_SELF_REFERENCE_P (decl)
19224               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19225             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19226                                           tag_type != none_type,
19227                                           /*complain=*/true);
19228
19229           /* If we have a single function from a using decl, pull it out.  */
19230           if (TREE_CODE (decl) == OVERLOAD
19231               && !really_overloaded_fn (decl))
19232             decl = OVL_FUNCTION (decl);
19233
19234           if (pushed_scope)
19235             pop_scope (pushed_scope);
19236         }
19237
19238       /* If the scope is a dependent type and either we deferred lookup or
19239          we did lookup but didn't find the name, rememeber the name.  */
19240       if (decl == error_mark_node && TYPE_P (parser->scope)
19241           && dependent_type_p (parser->scope))
19242         {
19243           if (tag_type)
19244             {
19245               tree type;
19246
19247               /* The resolution to Core Issue 180 says that `struct
19248                  A::B' should be considered a type-name, even if `A'
19249                  is dependent.  */
19250               type = make_typename_type (parser->scope, name, tag_type,
19251                                          /*complain=*/tf_error);
19252               decl = TYPE_NAME (type);
19253             }
19254           else if (is_template
19255                    && (cp_parser_next_token_ends_template_argument_p (parser)
19256                        || cp_lexer_next_token_is (parser->lexer,
19257                                                   CPP_CLOSE_PAREN)))
19258             decl = make_unbound_class_template (parser->scope,
19259                                                 name, NULL_TREE,
19260                                                 /*complain=*/tf_error);
19261           else
19262             decl = build_qualified_name (/*type=*/NULL_TREE,
19263                                          parser->scope, name,
19264                                          is_template);
19265         }
19266       parser->qualifying_scope = parser->scope;
19267       parser->object_scope = NULL_TREE;
19268     }
19269   else if (object_type)
19270     {
19271       tree object_decl = NULL_TREE;
19272       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19273          OBJECT_TYPE is not a class.  */
19274       if (CLASS_TYPE_P (object_type))
19275         /* If the OBJECT_TYPE is a template specialization, it may
19276            be instantiated during name lookup.  In that case, errors
19277            may be issued.  Even if we rollback the current tentative
19278            parse, those errors are valid.  */
19279         object_decl = lookup_member (object_type,
19280                                      name,
19281                                      /*protect=*/0,
19282                                      tag_type != none_type);
19283       /* Look it up in the enclosing context, too.  */
19284       decl = lookup_name_real (name, tag_type != none_type,
19285                                /*nonclass=*/0,
19286                                /*block_p=*/true, is_namespace, flags);
19287       parser->object_scope = object_type;
19288       parser->qualifying_scope = NULL_TREE;
19289       if (object_decl)
19290         decl = object_decl;
19291     }
19292   else
19293     {
19294       decl = lookup_name_real (name, tag_type != none_type,
19295                                /*nonclass=*/0,
19296                                /*block_p=*/true, is_namespace, flags);
19297       parser->qualifying_scope = NULL_TREE;
19298       parser->object_scope = NULL_TREE;
19299     }
19300
19301   /* If the lookup failed, let our caller know.  */
19302   if (!decl || decl == error_mark_node)
19303     return error_mark_node;
19304
19305   /* Pull out the template from an injected-class-name (or multiple).  */
19306   if (is_template)
19307     decl = maybe_get_template_decl_from_type_decl (decl);
19308
19309   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19310   if (TREE_CODE (decl) == TREE_LIST)
19311     {
19312       if (ambiguous_decls)
19313         *ambiguous_decls = decl;
19314       /* The error message we have to print is too complicated for
19315          cp_parser_error, so we incorporate its actions directly.  */
19316       if (!cp_parser_simulate_error (parser))
19317         {
19318           error_at (name_location, "reference to %qD is ambiguous",
19319                     name);
19320           print_candidates (decl);
19321         }
19322       return error_mark_node;
19323     }
19324
19325   gcc_assert (DECL_P (decl)
19326               || TREE_CODE (decl) == OVERLOAD
19327               || TREE_CODE (decl) == SCOPE_REF
19328               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19329               || BASELINK_P (decl));
19330
19331   /* If we have resolved the name of a member declaration, check to
19332      see if the declaration is accessible.  When the name resolves to
19333      set of overloaded functions, accessibility is checked when
19334      overload resolution is done.
19335
19336      During an explicit instantiation, access is not checked at all,
19337      as per [temp.explicit].  */
19338   if (DECL_P (decl))
19339     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19340
19341   return decl;
19342 }
19343
19344 /* Like cp_parser_lookup_name, but for use in the typical case where
19345    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19346    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19347
19348 static tree
19349 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19350 {
19351   return cp_parser_lookup_name (parser, name,
19352                                 none_type,
19353                                 /*is_template=*/false,
19354                                 /*is_namespace=*/false,
19355                                 /*check_dependency=*/true,
19356                                 /*ambiguous_decls=*/NULL,
19357                                 location);
19358 }
19359
19360 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19361    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19362    true, the DECL indicates the class being defined in a class-head,
19363    or declared in an elaborated-type-specifier.
19364
19365    Otherwise, return DECL.  */
19366
19367 static tree
19368 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19369 {
19370   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19371      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19372
19373        struct A {
19374          template <typename T> struct B;
19375        };
19376
19377        template <typename T> struct A::B {};
19378
19379      Similarly, in an elaborated-type-specifier:
19380
19381        namespace N { struct X{}; }
19382
19383        struct A {
19384          template <typename T> friend struct N::X;
19385        };
19386
19387      However, if the DECL refers to a class type, and we are in
19388      the scope of the class, then the name lookup automatically
19389      finds the TYPE_DECL created by build_self_reference rather
19390      than a TEMPLATE_DECL.  For example, in:
19391
19392        template <class T> struct S {
19393          S s;
19394        };
19395
19396      there is no need to handle such case.  */
19397
19398   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19399     return DECL_TEMPLATE_RESULT (decl);
19400
19401   return decl;
19402 }
19403
19404 /* If too many, or too few, template-parameter lists apply to the
19405    declarator, issue an error message.  Returns TRUE if all went well,
19406    and FALSE otherwise.  */
19407
19408 static bool
19409 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19410                                                 cp_declarator *declarator,
19411                                                 location_t declarator_location)
19412 {
19413   unsigned num_templates;
19414
19415   /* We haven't seen any classes that involve template parameters yet.  */
19416   num_templates = 0;
19417
19418   switch (declarator->kind)
19419     {
19420     case cdk_id:
19421       if (declarator->u.id.qualifying_scope)
19422         {
19423           tree scope;
19424
19425           scope = declarator->u.id.qualifying_scope;
19426
19427           while (scope && CLASS_TYPE_P (scope))
19428             {
19429               /* You're supposed to have one `template <...>'
19430                  for every template class, but you don't need one
19431                  for a full specialization.  For example:
19432
19433                  template <class T> struct S{};
19434                  template <> struct S<int> { void f(); };
19435                  void S<int>::f () {}
19436
19437                  is correct; there shouldn't be a `template <>' for
19438                  the definition of `S<int>::f'.  */
19439               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19440                 /* If SCOPE does not have template information of any
19441                    kind, then it is not a template, nor is it nested
19442                    within a template.  */
19443                 break;
19444               if (explicit_class_specialization_p (scope))
19445                 break;
19446               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19447                 ++num_templates;
19448
19449               scope = TYPE_CONTEXT (scope);
19450             }
19451         }
19452       else if (TREE_CODE (declarator->u.id.unqualified_name)
19453                == TEMPLATE_ID_EXPR)
19454         /* If the DECLARATOR has the form `X<y>' then it uses one
19455            additional level of template parameters.  */
19456         ++num_templates;
19457
19458       return cp_parser_check_template_parameters 
19459         (parser, num_templates, declarator_location, declarator);
19460
19461
19462     case cdk_function:
19463     case cdk_array:
19464     case cdk_pointer:
19465     case cdk_reference:
19466     case cdk_ptrmem:
19467       return (cp_parser_check_declarator_template_parameters
19468               (parser, declarator->declarator, declarator_location));
19469
19470     case cdk_error:
19471       return true;
19472
19473     default:
19474       gcc_unreachable ();
19475     }
19476   return false;
19477 }
19478
19479 /* NUM_TEMPLATES were used in the current declaration.  If that is
19480    invalid, return FALSE and issue an error messages.  Otherwise,
19481    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19482    declarator and we can print more accurate diagnostics.  */
19483
19484 static bool
19485 cp_parser_check_template_parameters (cp_parser* parser,
19486                                      unsigned num_templates,
19487                                      location_t location,
19488                                      cp_declarator *declarator)
19489 {
19490   /* If there are the same number of template classes and parameter
19491      lists, that's OK.  */
19492   if (parser->num_template_parameter_lists == num_templates)
19493     return true;
19494   /* If there are more, but only one more, then we are referring to a
19495      member template.  That's OK too.  */
19496   if (parser->num_template_parameter_lists == num_templates + 1)
19497     return true;
19498   /* If there are more template classes than parameter lists, we have
19499      something like:
19500
19501        template <class T> void S<T>::R<T>::f ();  */
19502   if (parser->num_template_parameter_lists < num_templates)
19503     {
19504       if (declarator && !current_function_decl)
19505         error_at (location, "specializing member %<%T::%E%> "
19506                   "requires %<template<>%> syntax", 
19507                   declarator->u.id.qualifying_scope,
19508                   declarator->u.id.unqualified_name);
19509       else if (declarator)
19510         error_at (location, "invalid declaration of %<%T::%E%>",
19511                   declarator->u.id.qualifying_scope,
19512                   declarator->u.id.unqualified_name);
19513       else 
19514         error_at (location, "too few template-parameter-lists");
19515       return false;
19516     }
19517   /* Otherwise, there are too many template parameter lists.  We have
19518      something like:
19519
19520      template <class T> template <class U> void S::f();  */
19521   error_at (location, "too many template-parameter-lists");
19522   return false;
19523 }
19524
19525 /* Parse an optional `::' token indicating that the following name is
19526    from the global namespace.  If so, PARSER->SCOPE is set to the
19527    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19528    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19529    Returns the new value of PARSER->SCOPE, if the `::' token is
19530    present, and NULL_TREE otherwise.  */
19531
19532 static tree
19533 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19534 {
19535   cp_token *token;
19536
19537   /* Peek at the next token.  */
19538   token = cp_lexer_peek_token (parser->lexer);
19539   /* If we're looking at a `::' token then we're starting from the
19540      global namespace, not our current location.  */
19541   if (token->type == CPP_SCOPE)
19542     {
19543       /* Consume the `::' token.  */
19544       cp_lexer_consume_token (parser->lexer);
19545       /* Set the SCOPE so that we know where to start the lookup.  */
19546       parser->scope = global_namespace;
19547       parser->qualifying_scope = global_namespace;
19548       parser->object_scope = NULL_TREE;
19549
19550       return parser->scope;
19551     }
19552   else if (!current_scope_valid_p)
19553     {
19554       parser->scope = NULL_TREE;
19555       parser->qualifying_scope = NULL_TREE;
19556       parser->object_scope = NULL_TREE;
19557     }
19558
19559   return NULL_TREE;
19560 }
19561
19562 /* Returns TRUE if the upcoming token sequence is the start of a
19563    constructor declarator.  If FRIEND_P is true, the declarator is
19564    preceded by the `friend' specifier.  */
19565
19566 static bool
19567 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19568 {
19569   bool constructor_p;
19570   tree nested_name_specifier;
19571   cp_token *next_token;
19572
19573   /* The common case is that this is not a constructor declarator, so
19574      try to avoid doing lots of work if at all possible.  It's not
19575      valid declare a constructor at function scope.  */
19576   if (parser->in_function_body)
19577     return false;
19578   /* And only certain tokens can begin a constructor declarator.  */
19579   next_token = cp_lexer_peek_token (parser->lexer);
19580   if (next_token->type != CPP_NAME
19581       && next_token->type != CPP_SCOPE
19582       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19583       && next_token->type != CPP_TEMPLATE_ID)
19584     return false;
19585
19586   /* Parse tentatively; we are going to roll back all of the tokens
19587      consumed here.  */
19588   cp_parser_parse_tentatively (parser);
19589   /* Assume that we are looking at a constructor declarator.  */
19590   constructor_p = true;
19591
19592   /* Look for the optional `::' operator.  */
19593   cp_parser_global_scope_opt (parser,
19594                               /*current_scope_valid_p=*/false);
19595   /* Look for the nested-name-specifier.  */
19596   nested_name_specifier
19597     = (cp_parser_nested_name_specifier_opt (parser,
19598                                             /*typename_keyword_p=*/false,
19599                                             /*check_dependency_p=*/false,
19600                                             /*type_p=*/false,
19601                                             /*is_declaration=*/false));
19602   /* Outside of a class-specifier, there must be a
19603      nested-name-specifier.  */
19604   if (!nested_name_specifier &&
19605       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19606        || friend_p))
19607     constructor_p = false;
19608   else if (nested_name_specifier == error_mark_node)
19609     constructor_p = false;
19610
19611   /* If we have a class scope, this is easy; DR 147 says that S::S always
19612      names the constructor, and no other qualified name could.  */
19613   if (constructor_p && nested_name_specifier
19614       && CLASS_TYPE_P (nested_name_specifier))
19615     {
19616       tree id = cp_parser_unqualified_id (parser,
19617                                           /*template_keyword_p=*/false,
19618                                           /*check_dependency_p=*/false,
19619                                           /*declarator_p=*/true,
19620                                           /*optional_p=*/false);
19621       if (is_overloaded_fn (id))
19622         id = DECL_NAME (get_first_fn (id));
19623       if (!constructor_name_p (id, nested_name_specifier))
19624         constructor_p = false;
19625     }
19626   /* If we still think that this might be a constructor-declarator,
19627      look for a class-name.  */
19628   else if (constructor_p)
19629     {
19630       /* If we have:
19631
19632            template <typename T> struct S {
19633              S();
19634            };
19635
19636          we must recognize that the nested `S' names a class.  */
19637       tree type_decl;
19638       type_decl = cp_parser_class_name (parser,
19639                                         /*typename_keyword_p=*/false,
19640                                         /*template_keyword_p=*/false,
19641                                         none_type,
19642                                         /*check_dependency_p=*/false,
19643                                         /*class_head_p=*/false,
19644                                         /*is_declaration=*/false);
19645       /* If there was no class-name, then this is not a constructor.  */
19646       constructor_p = !cp_parser_error_occurred (parser);
19647
19648       /* If we're still considering a constructor, we have to see a `(',
19649          to begin the parameter-declaration-clause, followed by either a
19650          `)', an `...', or a decl-specifier.  We need to check for a
19651          type-specifier to avoid being fooled into thinking that:
19652
19653            S (f) (int);
19654
19655          is a constructor.  (It is actually a function named `f' that
19656          takes one parameter (of type `int') and returns a value of type
19657          `S'.  */
19658       if (constructor_p
19659           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19660         constructor_p = false;
19661
19662       if (constructor_p
19663           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19664           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19665           /* A parameter declaration begins with a decl-specifier,
19666              which is either the "attribute" keyword, a storage class
19667              specifier, or (usually) a type-specifier.  */
19668           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19669         {
19670           tree type;
19671           tree pushed_scope = NULL_TREE;
19672           unsigned saved_num_template_parameter_lists;
19673
19674           /* Names appearing in the type-specifier should be looked up
19675              in the scope of the class.  */
19676           if (current_class_type)
19677             type = NULL_TREE;
19678           else
19679             {
19680               type = TREE_TYPE (type_decl);
19681               if (TREE_CODE (type) == TYPENAME_TYPE)
19682                 {
19683                   type = resolve_typename_type (type,
19684                                                 /*only_current_p=*/false);
19685                   if (TREE_CODE (type) == TYPENAME_TYPE)
19686                     {
19687                       cp_parser_abort_tentative_parse (parser);
19688                       return false;
19689                     }
19690                 }
19691               pushed_scope = push_scope (type);
19692             }
19693
19694           /* Inside the constructor parameter list, surrounding
19695              template-parameter-lists do not apply.  */
19696           saved_num_template_parameter_lists
19697             = parser->num_template_parameter_lists;
19698           parser->num_template_parameter_lists = 0;
19699
19700           /* Look for the type-specifier.  */
19701           cp_parser_type_specifier (parser,
19702                                     CP_PARSER_FLAGS_NONE,
19703                                     /*decl_specs=*/NULL,
19704                                     /*is_declarator=*/true,
19705                                     /*declares_class_or_enum=*/NULL,
19706                                     /*is_cv_qualifier=*/NULL);
19707
19708           parser->num_template_parameter_lists
19709             = saved_num_template_parameter_lists;
19710
19711           /* Leave the scope of the class.  */
19712           if (pushed_scope)
19713             pop_scope (pushed_scope);
19714
19715           constructor_p = !cp_parser_error_occurred (parser);
19716         }
19717     }
19718
19719   /* We did not really want to consume any tokens.  */
19720   cp_parser_abort_tentative_parse (parser);
19721
19722   return constructor_p;
19723 }
19724
19725 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19726    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19727    they must be performed once we are in the scope of the function.
19728
19729    Returns the function defined.  */
19730
19731 static tree
19732 cp_parser_function_definition_from_specifiers_and_declarator
19733   (cp_parser* parser,
19734    cp_decl_specifier_seq *decl_specifiers,
19735    tree attributes,
19736    const cp_declarator *declarator)
19737 {
19738   tree fn;
19739   bool success_p;
19740
19741   /* Begin the function-definition.  */
19742   success_p = start_function (decl_specifiers, declarator, attributes);
19743
19744   /* The things we're about to see are not directly qualified by any
19745      template headers we've seen thus far.  */
19746   reset_specialization ();
19747
19748   /* If there were names looked up in the decl-specifier-seq that we
19749      did not check, check them now.  We must wait until we are in the
19750      scope of the function to perform the checks, since the function
19751      might be a friend.  */
19752   perform_deferred_access_checks ();
19753
19754   if (!success_p)
19755     {
19756       /* Skip the entire function.  */
19757       cp_parser_skip_to_end_of_block_or_statement (parser);
19758       fn = error_mark_node;
19759     }
19760   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19761     {
19762       /* Seen already, skip it.  An error message has already been output.  */
19763       cp_parser_skip_to_end_of_block_or_statement (parser);
19764       fn = current_function_decl;
19765       current_function_decl = NULL_TREE;
19766       /* If this is a function from a class, pop the nested class.  */
19767       if (current_class_name)
19768         pop_nested_class ();
19769     }
19770   else
19771     {
19772       timevar_id_t tv;
19773       if (DECL_DECLARED_INLINE_P (current_function_decl))
19774         tv = TV_PARSE_INLINE;
19775       else
19776         tv = TV_PARSE_FUNC;
19777       timevar_push (tv);
19778       fn = cp_parser_function_definition_after_declarator (parser,
19779                                                          /*inline_p=*/false);
19780       timevar_pop (tv);
19781     }
19782
19783   return fn;
19784 }
19785
19786 /* Parse the part of a function-definition that follows the
19787    declarator.  INLINE_P is TRUE iff this function is an inline
19788    function defined within a class-specifier.
19789
19790    Returns the function defined.  */
19791
19792 static tree
19793 cp_parser_function_definition_after_declarator (cp_parser* parser,
19794                                                 bool inline_p)
19795 {
19796   tree fn;
19797   bool ctor_initializer_p = false;
19798   bool saved_in_unbraced_linkage_specification_p;
19799   bool saved_in_function_body;
19800   unsigned saved_num_template_parameter_lists;
19801   cp_token *token;
19802
19803   saved_in_function_body = parser->in_function_body;
19804   parser->in_function_body = true;
19805   /* If the next token is `return', then the code may be trying to
19806      make use of the "named return value" extension that G++ used to
19807      support.  */
19808   token = cp_lexer_peek_token (parser->lexer);
19809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19810     {
19811       /* Consume the `return' keyword.  */
19812       cp_lexer_consume_token (parser->lexer);
19813       /* Look for the identifier that indicates what value is to be
19814          returned.  */
19815       cp_parser_identifier (parser);
19816       /* Issue an error message.  */
19817       error_at (token->location,
19818                 "named return values are no longer supported");
19819       /* Skip tokens until we reach the start of the function body.  */
19820       while (true)
19821         {
19822           cp_token *token = cp_lexer_peek_token (parser->lexer);
19823           if (token->type == CPP_OPEN_BRACE
19824               || token->type == CPP_EOF
19825               || token->type == CPP_PRAGMA_EOL)
19826             break;
19827           cp_lexer_consume_token (parser->lexer);
19828         }
19829     }
19830   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19831      anything declared inside `f'.  */
19832   saved_in_unbraced_linkage_specification_p
19833     = parser->in_unbraced_linkage_specification_p;
19834   parser->in_unbraced_linkage_specification_p = false;
19835   /* Inside the function, surrounding template-parameter-lists do not
19836      apply.  */
19837   saved_num_template_parameter_lists
19838     = parser->num_template_parameter_lists;
19839   parser->num_template_parameter_lists = 0;
19840
19841   start_lambda_scope (current_function_decl);
19842
19843   /* If the next token is `try', then we are looking at a
19844      function-try-block.  */
19845   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19846     ctor_initializer_p = cp_parser_function_try_block (parser);
19847   /* A function-try-block includes the function-body, so we only do
19848      this next part if we're not processing a function-try-block.  */
19849   else
19850     ctor_initializer_p
19851       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19852
19853   finish_lambda_scope ();
19854
19855   /* Finish the function.  */
19856   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19857                         (inline_p ? 2 : 0));
19858   /* Generate code for it, if necessary.  */
19859   expand_or_defer_fn (fn);
19860   /* Restore the saved values.  */
19861   parser->in_unbraced_linkage_specification_p
19862     = saved_in_unbraced_linkage_specification_p;
19863   parser->num_template_parameter_lists
19864     = saved_num_template_parameter_lists;
19865   parser->in_function_body = saved_in_function_body;
19866
19867   return fn;
19868 }
19869
19870 /* Parse a template-declaration, assuming that the `export' (and
19871    `extern') keywords, if present, has already been scanned.  MEMBER_P
19872    is as for cp_parser_template_declaration.  */
19873
19874 static void
19875 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19876 {
19877   tree decl = NULL_TREE;
19878   VEC (deferred_access_check,gc) *checks;
19879   tree parameter_list;
19880   bool friend_p = false;
19881   bool need_lang_pop;
19882   cp_token *token;
19883
19884   /* Look for the `template' keyword.  */
19885   token = cp_lexer_peek_token (parser->lexer);
19886   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19887     return;
19888
19889   /* And the `<'.  */
19890   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19891     return;
19892   if (at_class_scope_p () && current_function_decl)
19893     {
19894       /* 14.5.2.2 [temp.mem]
19895
19896          A local class shall not have member templates.  */
19897       error_at (token->location,
19898                 "invalid declaration of member template in local class");
19899       cp_parser_skip_to_end_of_block_or_statement (parser);
19900       return;
19901     }
19902   /* [temp]
19903
19904      A template ... shall not have C linkage.  */
19905   if (current_lang_name == lang_name_c)
19906     {
19907       error_at (token->location, "template with C linkage");
19908       /* Give it C++ linkage to avoid confusing other parts of the
19909          front end.  */
19910       push_lang_context (lang_name_cplusplus);
19911       need_lang_pop = true;
19912     }
19913   else
19914     need_lang_pop = false;
19915
19916   /* We cannot perform access checks on the template parameter
19917      declarations until we know what is being declared, just as we
19918      cannot check the decl-specifier list.  */
19919   push_deferring_access_checks (dk_deferred);
19920
19921   /* If the next token is `>', then we have an invalid
19922      specialization.  Rather than complain about an invalid template
19923      parameter, issue an error message here.  */
19924   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19925     {
19926       cp_parser_error (parser, "invalid explicit specialization");
19927       begin_specialization ();
19928       parameter_list = NULL_TREE;
19929     }
19930   else
19931     {
19932       /* Parse the template parameters.  */
19933       parameter_list = cp_parser_template_parameter_list (parser);
19934       fixup_template_parms ();
19935     }
19936
19937   /* Get the deferred access checks from the parameter list.  These
19938      will be checked once we know what is being declared, as for a
19939      member template the checks must be performed in the scope of the
19940      class containing the member.  */
19941   checks = get_deferred_access_checks ();
19942
19943   /* Look for the `>'.  */
19944   cp_parser_skip_to_end_of_template_parameter_list (parser);
19945   /* We just processed one more parameter list.  */
19946   ++parser->num_template_parameter_lists;
19947   /* If the next token is `template', there are more template
19948      parameters.  */
19949   if (cp_lexer_next_token_is_keyword (parser->lexer,
19950                                       RID_TEMPLATE))
19951     cp_parser_template_declaration_after_export (parser, member_p);
19952   else
19953     {
19954       /* There are no access checks when parsing a template, as we do not
19955          know if a specialization will be a friend.  */
19956       push_deferring_access_checks (dk_no_check);
19957       token = cp_lexer_peek_token (parser->lexer);
19958       decl = cp_parser_single_declaration (parser,
19959                                            checks,
19960                                            member_p,
19961                                            /*explicit_specialization_p=*/false,
19962                                            &friend_p);
19963       pop_deferring_access_checks ();
19964
19965       /* If this is a member template declaration, let the front
19966          end know.  */
19967       if (member_p && !friend_p && decl)
19968         {
19969           if (TREE_CODE (decl) == TYPE_DECL)
19970             cp_parser_check_access_in_redeclaration (decl, token->location);
19971
19972           decl = finish_member_template_decl (decl);
19973         }
19974       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19975         make_friend_class (current_class_type, TREE_TYPE (decl),
19976                            /*complain=*/true);
19977     }
19978   /* We are done with the current parameter list.  */
19979   --parser->num_template_parameter_lists;
19980
19981   pop_deferring_access_checks ();
19982
19983   /* Finish up.  */
19984   finish_template_decl (parameter_list);
19985
19986   /* Register member declarations.  */
19987   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19988     finish_member_declaration (decl);
19989   /* For the erroneous case of a template with C linkage, we pushed an
19990      implicit C++ linkage scope; exit that scope now.  */
19991   if (need_lang_pop)
19992     pop_lang_context ();
19993   /* If DECL is a function template, we must return to parse it later.
19994      (Even though there is no definition, there might be default
19995      arguments that need handling.)  */
19996   if (member_p && decl
19997       && (TREE_CODE (decl) == FUNCTION_DECL
19998           || DECL_FUNCTION_TEMPLATE_P (decl)))
19999     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20000 }
20001
20002 /* Perform the deferred access checks from a template-parameter-list.
20003    CHECKS is a TREE_LIST of access checks, as returned by
20004    get_deferred_access_checks.  */
20005
20006 static void
20007 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20008 {
20009   ++processing_template_parmlist;
20010   perform_access_checks (checks);
20011   --processing_template_parmlist;
20012 }
20013
20014 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20015    `function-definition' sequence.  MEMBER_P is true, this declaration
20016    appears in a class scope.
20017
20018    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20019    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20020
20021 static tree
20022 cp_parser_single_declaration (cp_parser* parser,
20023                               VEC (deferred_access_check,gc)* checks,
20024                               bool member_p,
20025                               bool explicit_specialization_p,
20026                               bool* friend_p)
20027 {
20028   int declares_class_or_enum;
20029   tree decl = NULL_TREE;
20030   cp_decl_specifier_seq decl_specifiers;
20031   bool function_definition_p = false;
20032   cp_token *decl_spec_token_start;
20033
20034   /* This function is only used when processing a template
20035      declaration.  */
20036   gcc_assert (innermost_scope_kind () == sk_template_parms
20037               || innermost_scope_kind () == sk_template_spec);
20038
20039   /* Defer access checks until we know what is being declared.  */
20040   push_deferring_access_checks (dk_deferred);
20041
20042   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20043      alternative.  */
20044   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20045   cp_parser_decl_specifier_seq (parser,
20046                                 CP_PARSER_FLAGS_OPTIONAL,
20047                                 &decl_specifiers,
20048                                 &declares_class_or_enum);
20049   if (friend_p)
20050     *friend_p = cp_parser_friend_p (&decl_specifiers);
20051
20052   /* There are no template typedefs.  */
20053   if (decl_specifiers.specs[(int) ds_typedef])
20054     {
20055       error_at (decl_spec_token_start->location,
20056                 "template declaration of %<typedef%>");
20057       decl = error_mark_node;
20058     }
20059
20060   /* Gather up the access checks that occurred the
20061      decl-specifier-seq.  */
20062   stop_deferring_access_checks ();
20063
20064   /* Check for the declaration of a template class.  */
20065   if (declares_class_or_enum)
20066     {
20067       if (cp_parser_declares_only_class_p (parser))
20068         {
20069           decl = shadow_tag (&decl_specifiers);
20070
20071           /* In this case:
20072
20073                struct C {
20074                  friend template <typename T> struct A<T>::B;
20075                };
20076
20077              A<T>::B will be represented by a TYPENAME_TYPE, and
20078              therefore not recognized by shadow_tag.  */
20079           if (friend_p && *friend_p
20080               && !decl
20081               && decl_specifiers.type
20082               && TYPE_P (decl_specifiers.type))
20083             decl = decl_specifiers.type;
20084
20085           if (decl && decl != error_mark_node)
20086             decl = TYPE_NAME (decl);
20087           else
20088             decl = error_mark_node;
20089
20090           /* Perform access checks for template parameters.  */
20091           cp_parser_perform_template_parameter_access_checks (checks);
20092         }
20093     }
20094
20095   /* Complain about missing 'typename' or other invalid type names.  */
20096   if (!decl_specifiers.any_type_specifiers_p
20097       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20098     {
20099       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20100          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20101          the rest of this declaration.  */
20102       decl = error_mark_node;
20103       goto out;
20104     }
20105
20106   /* If it's not a template class, try for a template function.  If
20107      the next token is a `;', then this declaration does not declare
20108      anything.  But, if there were errors in the decl-specifiers, then
20109      the error might well have come from an attempted class-specifier.
20110      In that case, there's no need to warn about a missing declarator.  */
20111   if (!decl
20112       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20113           || decl_specifiers.type != error_mark_node))
20114     {
20115       decl = cp_parser_init_declarator (parser,
20116                                         &decl_specifiers,
20117                                         checks,
20118                                         /*function_definition_allowed_p=*/true,
20119                                         member_p,
20120                                         declares_class_or_enum,
20121                                         &function_definition_p,
20122                                         NULL);
20123
20124     /* 7.1.1-1 [dcl.stc]
20125
20126        A storage-class-specifier shall not be specified in an explicit
20127        specialization...  */
20128     if (decl
20129         && explicit_specialization_p
20130         && decl_specifiers.storage_class != sc_none)
20131       {
20132         error_at (decl_spec_token_start->location,
20133                   "explicit template specialization cannot have a storage class");
20134         decl = error_mark_node;
20135       }
20136     }
20137
20138   /* Look for a trailing `;' after the declaration.  */
20139   if (!function_definition_p
20140       && (decl == error_mark_node
20141           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20142     cp_parser_skip_to_end_of_block_or_statement (parser);
20143
20144  out:
20145   pop_deferring_access_checks ();
20146
20147   /* Clear any current qualification; whatever comes next is the start
20148      of something new.  */
20149   parser->scope = NULL_TREE;
20150   parser->qualifying_scope = NULL_TREE;
20151   parser->object_scope = NULL_TREE;
20152
20153   return decl;
20154 }
20155
20156 /* Parse a cast-expression that is not the operand of a unary "&".  */
20157
20158 static tree
20159 cp_parser_simple_cast_expression (cp_parser *parser)
20160 {
20161   return cp_parser_cast_expression (parser, /*address_p=*/false,
20162                                     /*cast_p=*/false, NULL);
20163 }
20164
20165 /* Parse a functional cast to TYPE.  Returns an expression
20166    representing the cast.  */
20167
20168 static tree
20169 cp_parser_functional_cast (cp_parser* parser, tree type)
20170 {
20171   VEC(tree,gc) *vec;
20172   tree expression_list;
20173   tree cast;
20174   bool nonconst_p;
20175
20176   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20177     {
20178       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20179       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20180       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20181       if (TREE_CODE (type) == TYPE_DECL)
20182         type = TREE_TYPE (type);
20183       return finish_compound_literal (type, expression_list,
20184                                       tf_warning_or_error);
20185     }
20186
20187
20188   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20189                                                  /*cast_p=*/true,
20190                                                  /*allow_expansion_p=*/true,
20191                                                  /*non_constant_p=*/NULL);
20192   if (vec == NULL)
20193     expression_list = error_mark_node;
20194   else
20195     {
20196       expression_list = build_tree_list_vec (vec);
20197       release_tree_vector (vec);
20198     }
20199
20200   cast = build_functional_cast (type, expression_list,
20201                                 tf_warning_or_error);
20202   /* [expr.const]/1: In an integral constant expression "only type
20203      conversions to integral or enumeration type can be used".  */
20204   if (TREE_CODE (type) == TYPE_DECL)
20205     type = TREE_TYPE (type);
20206   if (cast != error_mark_node
20207       && !cast_valid_in_integral_constant_expression_p (type)
20208       && cp_parser_non_integral_constant_expression (parser,
20209                                                      NIC_CONSTRUCTOR))
20210     return error_mark_node;
20211   return cast;
20212 }
20213
20214 /* Save the tokens that make up the body of a member function defined
20215    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20216    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20217    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20218    for the member function.  */
20219
20220 static tree
20221 cp_parser_save_member_function_body (cp_parser* parser,
20222                                      cp_decl_specifier_seq *decl_specifiers,
20223                                      cp_declarator *declarator,
20224                                      tree attributes)
20225 {
20226   cp_token *first;
20227   cp_token *last;
20228   tree fn;
20229
20230   /* Create the FUNCTION_DECL.  */
20231   fn = grokmethod (decl_specifiers, declarator, attributes);
20232   /* If something went badly wrong, bail out now.  */
20233   if (fn == error_mark_node)
20234     {
20235       /* If there's a function-body, skip it.  */
20236       if (cp_parser_token_starts_function_definition_p
20237           (cp_lexer_peek_token (parser->lexer)))
20238         cp_parser_skip_to_end_of_block_or_statement (parser);
20239       return error_mark_node;
20240     }
20241
20242   /* Remember it, if there default args to post process.  */
20243   cp_parser_save_default_args (parser, fn);
20244
20245   /* Save away the tokens that make up the body of the
20246      function.  */
20247   first = parser->lexer->next_token;
20248   /* We can have braced-init-list mem-initializers before the fn body.  */
20249   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20250     {
20251       cp_lexer_consume_token (parser->lexer);
20252       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20253              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20254         {
20255           /* cache_group will stop after an un-nested { } pair, too.  */
20256           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20257             break;
20258
20259           /* variadic mem-inits have ... after the ')'.  */
20260           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20261             cp_lexer_consume_token (parser->lexer);
20262         }
20263     }
20264   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20265   /* Handle function try blocks.  */
20266   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20267     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20268   last = parser->lexer->next_token;
20269
20270   /* Save away the inline definition; we will process it when the
20271      class is complete.  */
20272   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20273   DECL_PENDING_INLINE_P (fn) = 1;
20274
20275   /* We need to know that this was defined in the class, so that
20276      friend templates are handled correctly.  */
20277   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20278
20279   /* Add FN to the queue of functions to be parsed later.  */
20280   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20281
20282   return fn;
20283 }
20284
20285 /* Parse a template-argument-list, as well as the trailing ">" (but
20286    not the opening ">").  See cp_parser_template_argument_list for the
20287    return value.  */
20288
20289 static tree
20290 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20291 {
20292   tree arguments;
20293   tree saved_scope;
20294   tree saved_qualifying_scope;
20295   tree saved_object_scope;
20296   bool saved_greater_than_is_operator_p;
20297   int saved_unevaluated_operand;
20298   int saved_inhibit_evaluation_warnings;
20299
20300   /* [temp.names]
20301
20302      When parsing a template-id, the first non-nested `>' is taken as
20303      the end of the template-argument-list rather than a greater-than
20304      operator.  */
20305   saved_greater_than_is_operator_p
20306     = parser->greater_than_is_operator_p;
20307   parser->greater_than_is_operator_p = false;
20308   /* Parsing the argument list may modify SCOPE, so we save it
20309      here.  */
20310   saved_scope = parser->scope;
20311   saved_qualifying_scope = parser->qualifying_scope;
20312   saved_object_scope = parser->object_scope;
20313   /* We need to evaluate the template arguments, even though this
20314      template-id may be nested within a "sizeof".  */
20315   saved_unevaluated_operand = cp_unevaluated_operand;
20316   cp_unevaluated_operand = 0;
20317   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20318   c_inhibit_evaluation_warnings = 0;
20319   /* Parse the template-argument-list itself.  */
20320   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20321       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20322     arguments = NULL_TREE;
20323   else
20324     arguments = cp_parser_template_argument_list (parser);
20325   /* Look for the `>' that ends the template-argument-list. If we find
20326      a '>>' instead, it's probably just a typo.  */
20327   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20328     {
20329       if (cxx_dialect != cxx98)
20330         {
20331           /* In C++0x, a `>>' in a template argument list or cast
20332              expression is considered to be two separate `>'
20333              tokens. So, change the current token to a `>', but don't
20334              consume it: it will be consumed later when the outer
20335              template argument list (or cast expression) is parsed.
20336              Note that this replacement of `>' for `>>' is necessary
20337              even if we are parsing tentatively: in the tentative
20338              case, after calling
20339              cp_parser_enclosed_template_argument_list we will always
20340              throw away all of the template arguments and the first
20341              closing `>', either because the template argument list
20342              was erroneous or because we are replacing those tokens
20343              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20344              not have been thrown away) is needed either to close an
20345              outer template argument list or to complete a new-style
20346              cast.  */
20347           cp_token *token = cp_lexer_peek_token (parser->lexer);
20348           token->type = CPP_GREATER;
20349         }
20350       else if (!saved_greater_than_is_operator_p)
20351         {
20352           /* If we're in a nested template argument list, the '>>' has
20353             to be a typo for '> >'. We emit the error message, but we
20354             continue parsing and we push a '>' as next token, so that
20355             the argument list will be parsed correctly.  Note that the
20356             global source location is still on the token before the
20357             '>>', so we need to say explicitly where we want it.  */
20358           cp_token *token = cp_lexer_peek_token (parser->lexer);
20359           error_at (token->location, "%<>>%> should be %<> >%> "
20360                     "within a nested template argument list");
20361
20362           token->type = CPP_GREATER;
20363         }
20364       else
20365         {
20366           /* If this is not a nested template argument list, the '>>'
20367             is a typo for '>'. Emit an error message and continue.
20368             Same deal about the token location, but here we can get it
20369             right by consuming the '>>' before issuing the diagnostic.  */
20370           cp_token *token = cp_lexer_consume_token (parser->lexer);
20371           error_at (token->location,
20372                     "spurious %<>>%>, use %<>%> to terminate "
20373                     "a template argument list");
20374         }
20375     }
20376   else
20377     cp_parser_skip_to_end_of_template_parameter_list (parser);
20378   /* The `>' token might be a greater-than operator again now.  */
20379   parser->greater_than_is_operator_p
20380     = saved_greater_than_is_operator_p;
20381   /* Restore the SAVED_SCOPE.  */
20382   parser->scope = saved_scope;
20383   parser->qualifying_scope = saved_qualifying_scope;
20384   parser->object_scope = saved_object_scope;
20385   cp_unevaluated_operand = saved_unevaluated_operand;
20386   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20387
20388   return arguments;
20389 }
20390
20391 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20392    arguments, or the body of the function have not yet been parsed,
20393    parse them now.  */
20394
20395 static void
20396 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20397 {
20398   timevar_push (TV_PARSE_INMETH);
20399   /* If this member is a template, get the underlying
20400      FUNCTION_DECL.  */
20401   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20402     member_function = DECL_TEMPLATE_RESULT (member_function);
20403
20404   /* There should not be any class definitions in progress at this
20405      point; the bodies of members are only parsed outside of all class
20406      definitions.  */
20407   gcc_assert (parser->num_classes_being_defined == 0);
20408   /* While we're parsing the member functions we might encounter more
20409      classes.  We want to handle them right away, but we don't want
20410      them getting mixed up with functions that are currently in the
20411      queue.  */
20412   push_unparsed_function_queues (parser);
20413
20414   /* Make sure that any template parameters are in scope.  */
20415   maybe_begin_member_template_processing (member_function);
20416
20417   /* If the body of the function has not yet been parsed, parse it
20418      now.  */
20419   if (DECL_PENDING_INLINE_P (member_function))
20420     {
20421       tree function_scope;
20422       cp_token_cache *tokens;
20423
20424       /* The function is no longer pending; we are processing it.  */
20425       tokens = DECL_PENDING_INLINE_INFO (member_function);
20426       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20427       DECL_PENDING_INLINE_P (member_function) = 0;
20428
20429       /* If this is a local class, enter the scope of the containing
20430          function.  */
20431       function_scope = current_function_decl;
20432       if (function_scope)
20433         push_function_context ();
20434
20435       /* Push the body of the function onto the lexer stack.  */
20436       cp_parser_push_lexer_for_tokens (parser, tokens);
20437
20438       /* Let the front end know that we going to be defining this
20439          function.  */
20440       start_preparsed_function (member_function, NULL_TREE,
20441                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20442
20443       /* Don't do access checking if it is a templated function.  */
20444       if (processing_template_decl)
20445         push_deferring_access_checks (dk_no_check);
20446
20447       /* Now, parse the body of the function.  */
20448       cp_parser_function_definition_after_declarator (parser,
20449                                                       /*inline_p=*/true);
20450
20451       if (processing_template_decl)
20452         pop_deferring_access_checks ();
20453
20454       /* Leave the scope of the containing function.  */
20455       if (function_scope)
20456         pop_function_context ();
20457       cp_parser_pop_lexer (parser);
20458     }
20459
20460   /* Remove any template parameters from the symbol table.  */
20461   maybe_end_member_template_processing ();
20462
20463   /* Restore the queue.  */
20464   pop_unparsed_function_queues (parser);
20465   timevar_pop (TV_PARSE_INMETH);
20466 }
20467
20468 /* If DECL contains any default args, remember it on the unparsed
20469    functions queue.  */
20470
20471 static void
20472 cp_parser_save_default_args (cp_parser* parser, tree decl)
20473 {
20474   tree probe;
20475
20476   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20477        probe;
20478        probe = TREE_CHAIN (probe))
20479     if (TREE_PURPOSE (probe))
20480       {
20481         cp_default_arg_entry *entry
20482           = VEC_safe_push (cp_default_arg_entry, gc,
20483                            unparsed_funs_with_default_args, NULL);
20484         entry->class_type = current_class_type;
20485         entry->decl = decl;
20486         break;
20487       }
20488 }
20489
20490 /* FN is a FUNCTION_DECL which may contains a parameter with an
20491    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20492    assumes that the current scope is the scope in which the default
20493    argument should be processed.  */
20494
20495 static void
20496 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20497 {
20498   bool saved_local_variables_forbidden_p;
20499   tree parm, parmdecl;
20500
20501   /* While we're parsing the default args, we might (due to the
20502      statement expression extension) encounter more classes.  We want
20503      to handle them right away, but we don't want them getting mixed
20504      up with default args that are currently in the queue.  */
20505   push_unparsed_function_queues (parser);
20506
20507   /* Local variable names (and the `this' keyword) may not appear
20508      in a default argument.  */
20509   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20510   parser->local_variables_forbidden_p = true;
20511
20512   push_defarg_context (fn);
20513
20514   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20515          parmdecl = DECL_ARGUMENTS (fn);
20516        parm && parm != void_list_node;
20517        parm = TREE_CHAIN (parm),
20518          parmdecl = DECL_CHAIN (parmdecl))
20519     {
20520       cp_token_cache *tokens;
20521       tree default_arg = TREE_PURPOSE (parm);
20522       tree parsed_arg;
20523       VEC(tree,gc) *insts;
20524       tree copy;
20525       unsigned ix;
20526
20527       if (!default_arg)
20528         continue;
20529
20530       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20531         /* This can happen for a friend declaration for a function
20532            already declared with default arguments.  */
20533         continue;
20534
20535        /* Push the saved tokens for the default argument onto the parser's
20536           lexer stack.  */
20537       tokens = DEFARG_TOKENS (default_arg);
20538       cp_parser_push_lexer_for_tokens (parser, tokens);
20539
20540       start_lambda_scope (parmdecl);
20541
20542       /* Parse the assignment-expression.  */
20543       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20544       if (parsed_arg == error_mark_node)
20545         {
20546           cp_parser_pop_lexer (parser);
20547           continue;
20548         }
20549
20550       if (!processing_template_decl)
20551         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20552
20553       TREE_PURPOSE (parm) = parsed_arg;
20554
20555       /* Update any instantiations we've already created.  */
20556       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20557            VEC_iterate (tree, insts, ix, copy); ix++)
20558         TREE_PURPOSE (copy) = parsed_arg;
20559
20560       finish_lambda_scope ();
20561
20562       /* If the token stream has not been completely used up, then
20563          there was extra junk after the end of the default
20564          argument.  */
20565       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20566         cp_parser_error (parser, "expected %<,%>");
20567
20568       /* Revert to the main lexer.  */
20569       cp_parser_pop_lexer (parser);
20570     }
20571
20572   pop_defarg_context ();
20573
20574   /* Make sure no default arg is missing.  */
20575   check_default_args (fn);
20576
20577   /* Restore the state of local_variables_forbidden_p.  */
20578   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20579
20580   /* Restore the queue.  */
20581   pop_unparsed_function_queues (parser);
20582 }
20583
20584 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20585    either a TYPE or an expression, depending on the form of the
20586    input.  The KEYWORD indicates which kind of expression we have
20587    encountered.  */
20588
20589 static tree
20590 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20591 {
20592   tree expr = NULL_TREE;
20593   const char *saved_message;
20594   char *tmp;
20595   bool saved_integral_constant_expression_p;
20596   bool saved_non_integral_constant_expression_p;
20597   bool pack_expansion_p = false;
20598
20599   /* Types cannot be defined in a `sizeof' expression.  Save away the
20600      old message.  */
20601   saved_message = parser->type_definition_forbidden_message;
20602   /* And create the new one.  */
20603   tmp = concat ("types may not be defined in %<",
20604                 IDENTIFIER_POINTER (ridpointers[keyword]),
20605                 "%> expressions", NULL);
20606   parser->type_definition_forbidden_message = tmp;
20607
20608   /* The restrictions on constant-expressions do not apply inside
20609      sizeof expressions.  */
20610   saved_integral_constant_expression_p
20611     = parser->integral_constant_expression_p;
20612   saved_non_integral_constant_expression_p
20613     = parser->non_integral_constant_expression_p;
20614   parser->integral_constant_expression_p = false;
20615
20616   /* If it's a `...', then we are computing the length of a parameter
20617      pack.  */
20618   if (keyword == RID_SIZEOF
20619       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20620     {
20621       /* Consume the `...'.  */
20622       cp_lexer_consume_token (parser->lexer);
20623       maybe_warn_variadic_templates ();
20624
20625       /* Note that this is an expansion.  */
20626       pack_expansion_p = true;
20627     }
20628
20629   /* Do not actually evaluate the expression.  */
20630   ++cp_unevaluated_operand;
20631   ++c_inhibit_evaluation_warnings;
20632   /* If it's a `(', then we might be looking at the type-id
20633      construction.  */
20634   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20635     {
20636       tree type;
20637       bool saved_in_type_id_in_expr_p;
20638
20639       /* We can't be sure yet whether we're looking at a type-id or an
20640          expression.  */
20641       cp_parser_parse_tentatively (parser);
20642       /* Consume the `('.  */
20643       cp_lexer_consume_token (parser->lexer);
20644       /* Parse the type-id.  */
20645       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20646       parser->in_type_id_in_expr_p = true;
20647       type = cp_parser_type_id (parser);
20648       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20649       /* Now, look for the trailing `)'.  */
20650       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20651       /* If all went well, then we're done.  */
20652       if (cp_parser_parse_definitely (parser))
20653         {
20654           cp_decl_specifier_seq decl_specs;
20655
20656           /* Build a trivial decl-specifier-seq.  */
20657           clear_decl_specs (&decl_specs);
20658           decl_specs.type = type;
20659
20660           /* Call grokdeclarator to figure out what type this is.  */
20661           expr = grokdeclarator (NULL,
20662                                  &decl_specs,
20663                                  TYPENAME,
20664                                  /*initialized=*/0,
20665                                  /*attrlist=*/NULL);
20666         }
20667     }
20668
20669   /* If the type-id production did not work out, then we must be
20670      looking at the unary-expression production.  */
20671   if (!expr)
20672     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20673                                        /*cast_p=*/false, NULL);
20674
20675   if (pack_expansion_p)
20676     /* Build a pack expansion. */
20677     expr = make_pack_expansion (expr);
20678
20679   /* Go back to evaluating expressions.  */
20680   --cp_unevaluated_operand;
20681   --c_inhibit_evaluation_warnings;
20682
20683   /* Free the message we created.  */
20684   free (tmp);
20685   /* And restore the old one.  */
20686   parser->type_definition_forbidden_message = saved_message;
20687   parser->integral_constant_expression_p
20688     = saved_integral_constant_expression_p;
20689   parser->non_integral_constant_expression_p
20690     = saved_non_integral_constant_expression_p;
20691
20692   return expr;
20693 }
20694
20695 /* If the current declaration has no declarator, return true.  */
20696
20697 static bool
20698 cp_parser_declares_only_class_p (cp_parser *parser)
20699 {
20700   /* If the next token is a `;' or a `,' then there is no
20701      declarator.  */
20702   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20703           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20704 }
20705
20706 /* Update the DECL_SPECS to reflect the storage class indicated by
20707    KEYWORD.  */
20708
20709 static void
20710 cp_parser_set_storage_class (cp_parser *parser,
20711                              cp_decl_specifier_seq *decl_specs,
20712                              enum rid keyword,
20713                              location_t location)
20714 {
20715   cp_storage_class storage_class;
20716
20717   if (parser->in_unbraced_linkage_specification_p)
20718     {
20719       error_at (location, "invalid use of %qD in linkage specification",
20720                 ridpointers[keyword]);
20721       return;
20722     }
20723   else if (decl_specs->storage_class != sc_none)
20724     {
20725       decl_specs->conflicting_specifiers_p = true;
20726       return;
20727     }
20728
20729   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20730       && decl_specs->specs[(int) ds_thread])
20731     {
20732       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20733       decl_specs->specs[(int) ds_thread] = 0;
20734     }
20735
20736   switch (keyword)
20737     {
20738     case RID_AUTO:
20739       storage_class = sc_auto;
20740       break;
20741     case RID_REGISTER:
20742       storage_class = sc_register;
20743       break;
20744     case RID_STATIC:
20745       storage_class = sc_static;
20746       break;
20747     case RID_EXTERN:
20748       storage_class = sc_extern;
20749       break;
20750     case RID_MUTABLE:
20751       storage_class = sc_mutable;
20752       break;
20753     default:
20754       gcc_unreachable ();
20755     }
20756   decl_specs->storage_class = storage_class;
20757
20758   /* A storage class specifier cannot be applied alongside a typedef 
20759      specifier. If there is a typedef specifier present then set 
20760      conflicting_specifiers_p which will trigger an error later
20761      on in grokdeclarator. */
20762   if (decl_specs->specs[(int)ds_typedef])
20763     decl_specs->conflicting_specifiers_p = true;
20764 }
20765
20766 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20767    is true, the type is a user-defined type; otherwise it is a
20768    built-in type specified by a keyword.  */
20769
20770 static void
20771 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20772                               tree type_spec,
20773                               location_t location,
20774                               bool user_defined_p)
20775 {
20776   decl_specs->any_specifiers_p = true;
20777
20778   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20779      (with, for example, in "typedef int wchar_t;") we remember that
20780      this is what happened.  In system headers, we ignore these
20781      declarations so that G++ can work with system headers that are not
20782      C++-safe.  */
20783   if (decl_specs->specs[(int) ds_typedef]
20784       && !user_defined_p
20785       && (type_spec == boolean_type_node
20786           || type_spec == char16_type_node
20787           || type_spec == char32_type_node
20788           || type_spec == wchar_type_node)
20789       && (decl_specs->type
20790           || decl_specs->specs[(int) ds_long]
20791           || decl_specs->specs[(int) ds_short]
20792           || decl_specs->specs[(int) ds_unsigned]
20793           || decl_specs->specs[(int) ds_signed]))
20794     {
20795       decl_specs->redefined_builtin_type = type_spec;
20796       if (!decl_specs->type)
20797         {
20798           decl_specs->type = type_spec;
20799           decl_specs->user_defined_type_p = false;
20800           decl_specs->type_location = location;
20801         }
20802     }
20803   else if (decl_specs->type)
20804     decl_specs->multiple_types_p = true;
20805   else
20806     {
20807       decl_specs->type = type_spec;
20808       decl_specs->user_defined_type_p = user_defined_p;
20809       decl_specs->redefined_builtin_type = NULL_TREE;
20810       decl_specs->type_location = location;
20811     }
20812 }
20813
20814 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20815    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20816
20817 static bool
20818 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20819 {
20820   return decl_specifiers->specs[(int) ds_friend] != 0;
20821 }
20822
20823 /* Issue an error message indicating that TOKEN_DESC was expected.
20824    If KEYWORD is true, it indicated this function is called by
20825    cp_parser_require_keword and the required token can only be
20826    a indicated keyword. */
20827
20828 static void
20829 cp_parser_required_error (cp_parser *parser,
20830                           required_token token_desc,
20831                           bool keyword)
20832 {
20833   switch (token_desc)
20834     {
20835       case RT_NEW:
20836         cp_parser_error (parser, "expected %<new%>");
20837         return;
20838       case RT_DELETE:
20839         cp_parser_error (parser, "expected %<delete%>");
20840         return;
20841       case RT_RETURN:
20842         cp_parser_error (parser, "expected %<return%>");
20843         return;
20844       case RT_WHILE:
20845         cp_parser_error (parser, "expected %<while%>");
20846         return;
20847       case RT_EXTERN:
20848         cp_parser_error (parser, "expected %<extern%>");
20849         return;
20850       case RT_STATIC_ASSERT:
20851         cp_parser_error (parser, "expected %<static_assert%>");
20852         return;
20853       case RT_DECLTYPE:
20854         cp_parser_error (parser, "expected %<decltype%>");
20855         return;
20856       case RT_OPERATOR:
20857         cp_parser_error (parser, "expected %<operator%>");
20858         return;
20859       case RT_CLASS:
20860         cp_parser_error (parser, "expected %<class%>");
20861         return;
20862       case RT_TEMPLATE:
20863         cp_parser_error (parser, "expected %<template%>");
20864         return;
20865       case RT_NAMESPACE:
20866         cp_parser_error (parser, "expected %<namespace%>");
20867         return;
20868       case RT_USING:
20869         cp_parser_error (parser, "expected %<using%>");
20870         return;
20871       case RT_ASM:
20872         cp_parser_error (parser, "expected %<asm%>");
20873         return;
20874       case RT_TRY:
20875         cp_parser_error (parser, "expected %<try%>");
20876         return;
20877       case RT_CATCH:
20878         cp_parser_error (parser, "expected %<catch%>");
20879         return;
20880       case RT_THROW:
20881         cp_parser_error (parser, "expected %<throw%>");
20882         return;
20883       case RT_LABEL:
20884         cp_parser_error (parser, "expected %<__label__%>");
20885         return;
20886       case RT_AT_TRY:
20887         cp_parser_error (parser, "expected %<@try%>");
20888         return;
20889       case RT_AT_SYNCHRONIZED:
20890         cp_parser_error (parser, "expected %<@synchronized%>");
20891         return;
20892       case RT_AT_THROW:
20893         cp_parser_error (parser, "expected %<@throw%>");
20894         return;
20895       default:
20896         break;
20897     }
20898   if (!keyword)
20899     {
20900       switch (token_desc)
20901         {
20902           case RT_SEMICOLON:
20903             cp_parser_error (parser, "expected %<;%>");
20904             return;
20905           case RT_OPEN_PAREN:
20906             cp_parser_error (parser, "expected %<(%>");
20907             return;
20908           case RT_CLOSE_BRACE:
20909             cp_parser_error (parser, "expected %<}%>");
20910             return;
20911           case RT_OPEN_BRACE:
20912             cp_parser_error (parser, "expected %<{%>");
20913             return;
20914           case RT_CLOSE_SQUARE:
20915             cp_parser_error (parser, "expected %<]%>");
20916             return;
20917           case RT_OPEN_SQUARE:
20918             cp_parser_error (parser, "expected %<[%>");
20919             return;
20920           case RT_COMMA:
20921             cp_parser_error (parser, "expected %<,%>");
20922             return;
20923           case RT_SCOPE:
20924             cp_parser_error (parser, "expected %<::%>");
20925             return;
20926           case RT_LESS:
20927             cp_parser_error (parser, "expected %<<%>");
20928             return;
20929           case RT_GREATER:
20930             cp_parser_error (parser, "expected %<>%>");
20931             return;
20932           case RT_EQ:
20933             cp_parser_error (parser, "expected %<=%>");
20934             return;
20935           case RT_ELLIPSIS:
20936             cp_parser_error (parser, "expected %<...%>");
20937             return;
20938           case RT_MULT:
20939             cp_parser_error (parser, "expected %<*%>");
20940             return;
20941           case RT_COMPL:
20942             cp_parser_error (parser, "expected %<~%>");
20943             return;
20944           case RT_COLON:
20945             cp_parser_error (parser, "expected %<:%>");
20946             return;
20947           case RT_COLON_SCOPE:
20948             cp_parser_error (parser, "expected %<:%> or %<::%>");
20949             return;
20950           case RT_CLOSE_PAREN:
20951             cp_parser_error (parser, "expected %<)%>");
20952             return;
20953           case RT_COMMA_CLOSE_PAREN:
20954             cp_parser_error (parser, "expected %<,%> or %<)%>");
20955             return;
20956           case RT_PRAGMA_EOL:
20957             cp_parser_error (parser, "expected end of line");
20958             return;
20959           case RT_NAME:
20960             cp_parser_error (parser, "expected identifier");
20961             return;
20962           case RT_SELECT:
20963             cp_parser_error (parser, "expected selection-statement");
20964             return;
20965           case RT_INTERATION:
20966             cp_parser_error (parser, "expected iteration-statement");
20967             return;
20968           case RT_JUMP:
20969             cp_parser_error (parser, "expected jump-statement");
20970             return;
20971           case RT_CLASS_KEY:
20972             cp_parser_error (parser, "expected class-key");
20973             return;
20974           case RT_CLASS_TYPENAME_TEMPLATE:
20975             cp_parser_error (parser,
20976                  "expected %<class%>, %<typename%>, or %<template%>");
20977             return;
20978           default:
20979             gcc_unreachable ();
20980         }
20981     }
20982   else
20983     gcc_unreachable ();
20984 }
20985
20986
20987
20988 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20989    issue an error message indicating that TOKEN_DESC was expected.
20990
20991    Returns the token consumed, if the token had the appropriate type.
20992    Otherwise, returns NULL.  */
20993
20994 static cp_token *
20995 cp_parser_require (cp_parser* parser,
20996                    enum cpp_ttype type,
20997                    required_token token_desc)
20998 {
20999   if (cp_lexer_next_token_is (parser->lexer, type))
21000     return cp_lexer_consume_token (parser->lexer);
21001   else
21002     {
21003       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21004       if (!cp_parser_simulate_error (parser))
21005         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21006       return NULL;
21007     }
21008 }
21009
21010 /* An error message is produced if the next token is not '>'.
21011    All further tokens are skipped until the desired token is
21012    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21013
21014 static void
21015 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21016 {
21017   /* Current level of '< ... >'.  */
21018   unsigned level = 0;
21019   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21020   unsigned nesting_depth = 0;
21021
21022   /* Are we ready, yet?  If not, issue error message.  */
21023   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21024     return;
21025
21026   /* Skip tokens until the desired token is found.  */
21027   while (true)
21028     {
21029       /* Peek at the next token.  */
21030       switch (cp_lexer_peek_token (parser->lexer)->type)
21031         {
21032         case CPP_LESS:
21033           if (!nesting_depth)
21034             ++level;
21035           break;
21036
21037         case CPP_RSHIFT:
21038           if (cxx_dialect == cxx98)
21039             /* C++0x views the `>>' operator as two `>' tokens, but
21040                C++98 does not. */
21041             break;
21042           else if (!nesting_depth && level-- == 0)
21043             {
21044               /* We've hit a `>>' where the first `>' closes the
21045                  template argument list, and the second `>' is
21046                  spurious.  Just consume the `>>' and stop; we've
21047                  already produced at least one error.  */
21048               cp_lexer_consume_token (parser->lexer);
21049               return;
21050             }
21051           /* Fall through for C++0x, so we handle the second `>' in
21052              the `>>'.  */
21053
21054         case CPP_GREATER:
21055           if (!nesting_depth && level-- == 0)
21056             {
21057               /* We've reached the token we want, consume it and stop.  */
21058               cp_lexer_consume_token (parser->lexer);
21059               return;
21060             }
21061           break;
21062
21063         case CPP_OPEN_PAREN:
21064         case CPP_OPEN_SQUARE:
21065           ++nesting_depth;
21066           break;
21067
21068         case CPP_CLOSE_PAREN:
21069         case CPP_CLOSE_SQUARE:
21070           if (nesting_depth-- == 0)
21071             return;
21072           break;
21073
21074         case CPP_EOF:
21075         case CPP_PRAGMA_EOL:
21076         case CPP_SEMICOLON:
21077         case CPP_OPEN_BRACE:
21078         case CPP_CLOSE_BRACE:
21079           /* The '>' was probably forgotten, don't look further.  */
21080           return;
21081
21082         default:
21083           break;
21084         }
21085
21086       /* Consume this token.  */
21087       cp_lexer_consume_token (parser->lexer);
21088     }
21089 }
21090
21091 /* If the next token is the indicated keyword, consume it.  Otherwise,
21092    issue an error message indicating that TOKEN_DESC was expected.
21093
21094    Returns the token consumed, if the token had the appropriate type.
21095    Otherwise, returns NULL.  */
21096
21097 static cp_token *
21098 cp_parser_require_keyword (cp_parser* parser,
21099                            enum rid keyword,
21100                            required_token token_desc)
21101 {
21102   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21103
21104   if (token && token->keyword != keyword)
21105     {
21106       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21107       return NULL;
21108     }
21109
21110   return token;
21111 }
21112
21113 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21114    function-definition.  */
21115
21116 static bool
21117 cp_parser_token_starts_function_definition_p (cp_token* token)
21118 {
21119   return (/* An ordinary function-body begins with an `{'.  */
21120           token->type == CPP_OPEN_BRACE
21121           /* A ctor-initializer begins with a `:'.  */
21122           || token->type == CPP_COLON
21123           /* A function-try-block begins with `try'.  */
21124           || token->keyword == RID_TRY
21125           /* The named return value extension begins with `return'.  */
21126           || token->keyword == RID_RETURN);
21127 }
21128
21129 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21130    definition.  */
21131
21132 static bool
21133 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21134 {
21135   cp_token *token;
21136
21137   token = cp_lexer_peek_token (parser->lexer);
21138   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21139 }
21140
21141 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21142    C++0x) ending a template-argument.  */
21143
21144 static bool
21145 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21146 {
21147   cp_token *token;
21148
21149   token = cp_lexer_peek_token (parser->lexer);
21150   return (token->type == CPP_COMMA 
21151           || token->type == CPP_GREATER
21152           || token->type == CPP_ELLIPSIS
21153           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21154 }
21155
21156 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21157    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21158
21159 static bool
21160 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21161                                                      size_t n)
21162 {
21163   cp_token *token;
21164
21165   token = cp_lexer_peek_nth_token (parser->lexer, n);
21166   if (token->type == CPP_LESS)
21167     return true;
21168   /* Check for the sequence `<::' in the original code. It would be lexed as
21169      `[:', where `[' is a digraph, and there is no whitespace before
21170      `:'.  */
21171   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21172     {
21173       cp_token *token2;
21174       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21175       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21176         return true;
21177     }
21178   return false;
21179 }
21180
21181 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21182    or none_type otherwise.  */
21183
21184 static enum tag_types
21185 cp_parser_token_is_class_key (cp_token* token)
21186 {
21187   switch (token->keyword)
21188     {
21189     case RID_CLASS:
21190       return class_type;
21191     case RID_STRUCT:
21192       return record_type;
21193     case RID_UNION:
21194       return union_type;
21195
21196     default:
21197       return none_type;
21198     }
21199 }
21200
21201 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21202
21203 static void
21204 cp_parser_check_class_key (enum tag_types class_key, tree type)
21205 {
21206   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21207     permerror (input_location, "%qs tag used in naming %q#T",
21208             class_key == union_type ? "union"
21209              : class_key == record_type ? "struct" : "class",
21210              type);
21211 }
21212
21213 /* Issue an error message if DECL is redeclared with different
21214    access than its original declaration [class.access.spec/3].
21215    This applies to nested classes and nested class templates.
21216    [class.mem/1].  */
21217
21218 static void
21219 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21220 {
21221   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21222     return;
21223
21224   if ((TREE_PRIVATE (decl)
21225        != (current_access_specifier == access_private_node))
21226       || (TREE_PROTECTED (decl)
21227           != (current_access_specifier == access_protected_node)))
21228     error_at (location, "%qD redeclared with different access", decl);
21229 }
21230
21231 /* Look for the `template' keyword, as a syntactic disambiguator.
21232    Return TRUE iff it is present, in which case it will be
21233    consumed.  */
21234
21235 static bool
21236 cp_parser_optional_template_keyword (cp_parser *parser)
21237 {
21238   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21239     {
21240       /* The `template' keyword can only be used within templates;
21241          outside templates the parser can always figure out what is a
21242          template and what is not.  */
21243       if (!processing_template_decl)
21244         {
21245           cp_token *token = cp_lexer_peek_token (parser->lexer);
21246           error_at (token->location,
21247                     "%<template%> (as a disambiguator) is only allowed "
21248                     "within templates");
21249           /* If this part of the token stream is rescanned, the same
21250              error message would be generated.  So, we purge the token
21251              from the stream.  */
21252           cp_lexer_purge_token (parser->lexer);
21253           return false;
21254         }
21255       else
21256         {
21257           /* Consume the `template' keyword.  */
21258           cp_lexer_consume_token (parser->lexer);
21259           return true;
21260         }
21261     }
21262
21263   return false;
21264 }
21265
21266 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21267    set PARSER->SCOPE, and perform other related actions.  */
21268
21269 static void
21270 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21271 {
21272   int i;
21273   struct tree_check *check_value;
21274   deferred_access_check *chk;
21275   VEC (deferred_access_check,gc) *checks;
21276
21277   /* Get the stored value.  */
21278   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21279   /* Perform any access checks that were deferred.  */
21280   checks = check_value->checks;
21281   if (checks)
21282     {
21283       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21284         perform_or_defer_access_check (chk->binfo,
21285                                        chk->decl,
21286                                        chk->diag_decl);
21287     }
21288   /* Set the scope from the stored value.  */
21289   parser->scope = check_value->value;
21290   parser->qualifying_scope = check_value->qualifying_scope;
21291   parser->object_scope = NULL_TREE;
21292 }
21293
21294 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21295    encounter the end of a block before what we were looking for.  */
21296
21297 static bool
21298 cp_parser_cache_group (cp_parser *parser,
21299                        enum cpp_ttype end,
21300                        unsigned depth)
21301 {
21302   while (true)
21303     {
21304       cp_token *token = cp_lexer_peek_token (parser->lexer);
21305
21306       /* Abort a parenthesized expression if we encounter a semicolon.  */
21307       if ((end == CPP_CLOSE_PAREN || depth == 0)
21308           && token->type == CPP_SEMICOLON)
21309         return true;
21310       /* If we've reached the end of the file, stop.  */
21311       if (token->type == CPP_EOF
21312           || (end != CPP_PRAGMA_EOL
21313               && token->type == CPP_PRAGMA_EOL))
21314         return true;
21315       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21316         /* We've hit the end of an enclosing block, so there's been some
21317            kind of syntax error.  */
21318         return true;
21319
21320       /* Consume the token.  */
21321       cp_lexer_consume_token (parser->lexer);
21322       /* See if it starts a new group.  */
21323       if (token->type == CPP_OPEN_BRACE)
21324         {
21325           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21326           /* In theory this should probably check end == '}', but
21327              cp_parser_save_member_function_body needs it to exit
21328              after either '}' or ')' when called with ')'.  */
21329           if (depth == 0)
21330             return false;
21331         }
21332       else if (token->type == CPP_OPEN_PAREN)
21333         {
21334           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21335           if (depth == 0 && end == CPP_CLOSE_PAREN)
21336             return false;
21337         }
21338       else if (token->type == CPP_PRAGMA)
21339         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21340       else if (token->type == end)
21341         return false;
21342     }
21343 }
21344
21345 /* Begin parsing tentatively.  We always save tokens while parsing
21346    tentatively so that if the tentative parsing fails we can restore the
21347    tokens.  */
21348
21349 static void
21350 cp_parser_parse_tentatively (cp_parser* parser)
21351 {
21352   /* Enter a new parsing context.  */
21353   parser->context = cp_parser_context_new (parser->context);
21354   /* Begin saving tokens.  */
21355   cp_lexer_save_tokens (parser->lexer);
21356   /* In order to avoid repetitive access control error messages,
21357      access checks are queued up until we are no longer parsing
21358      tentatively.  */
21359   push_deferring_access_checks (dk_deferred);
21360 }
21361
21362 /* Commit to the currently active tentative parse.  */
21363
21364 static void
21365 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21366 {
21367   cp_parser_context *context;
21368   cp_lexer *lexer;
21369
21370   /* Mark all of the levels as committed.  */
21371   lexer = parser->lexer;
21372   for (context = parser->context; context->next; context = context->next)
21373     {
21374       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21375         break;
21376       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21377       while (!cp_lexer_saving_tokens (lexer))
21378         lexer = lexer->next;
21379       cp_lexer_commit_tokens (lexer);
21380     }
21381 }
21382
21383 /* Abort the currently active tentative parse.  All consumed tokens
21384    will be rolled back, and no diagnostics will be issued.  */
21385
21386 static void
21387 cp_parser_abort_tentative_parse (cp_parser* parser)
21388 {
21389   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21390               || errorcount > 0);
21391   cp_parser_simulate_error (parser);
21392   /* Now, pretend that we want to see if the construct was
21393      successfully parsed.  */
21394   cp_parser_parse_definitely (parser);
21395 }
21396
21397 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21398    token stream.  Otherwise, commit to the tokens we have consumed.
21399    Returns true if no error occurred; false otherwise.  */
21400
21401 static bool
21402 cp_parser_parse_definitely (cp_parser* parser)
21403 {
21404   bool error_occurred;
21405   cp_parser_context *context;
21406
21407   /* Remember whether or not an error occurred, since we are about to
21408      destroy that information.  */
21409   error_occurred = cp_parser_error_occurred (parser);
21410   /* Remove the topmost context from the stack.  */
21411   context = parser->context;
21412   parser->context = context->next;
21413   /* If no parse errors occurred, commit to the tentative parse.  */
21414   if (!error_occurred)
21415     {
21416       /* Commit to the tokens read tentatively, unless that was
21417          already done.  */
21418       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21419         cp_lexer_commit_tokens (parser->lexer);
21420
21421       pop_to_parent_deferring_access_checks ();
21422     }
21423   /* Otherwise, if errors occurred, roll back our state so that things
21424      are just as they were before we began the tentative parse.  */
21425   else
21426     {
21427       cp_lexer_rollback_tokens (parser->lexer);
21428       pop_deferring_access_checks ();
21429     }
21430   /* Add the context to the front of the free list.  */
21431   context->next = cp_parser_context_free_list;
21432   cp_parser_context_free_list = context;
21433
21434   return !error_occurred;
21435 }
21436
21437 /* Returns true if we are parsing tentatively and are not committed to
21438    this tentative parse.  */
21439
21440 static bool
21441 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21442 {
21443   return (cp_parser_parsing_tentatively (parser)
21444           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21445 }
21446
21447 /* Returns nonzero iff an error has occurred during the most recent
21448    tentative parse.  */
21449
21450 static bool
21451 cp_parser_error_occurred (cp_parser* parser)
21452 {
21453   return (cp_parser_parsing_tentatively (parser)
21454           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21455 }
21456
21457 /* Returns nonzero if GNU extensions are allowed.  */
21458
21459 static bool
21460 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21461 {
21462   return parser->allow_gnu_extensions_p;
21463 }
21464 \f
21465 /* Objective-C++ Productions */
21466
21467
21468 /* Parse an Objective-C expression, which feeds into a primary-expression
21469    above.
21470
21471    objc-expression:
21472      objc-message-expression
21473      objc-string-literal
21474      objc-encode-expression
21475      objc-protocol-expression
21476      objc-selector-expression
21477
21478   Returns a tree representation of the expression.  */
21479
21480 static tree
21481 cp_parser_objc_expression (cp_parser* parser)
21482 {
21483   /* Try to figure out what kind of declaration is present.  */
21484   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21485
21486   switch (kwd->type)
21487     {
21488     case CPP_OPEN_SQUARE:
21489       return cp_parser_objc_message_expression (parser);
21490
21491     case CPP_OBJC_STRING:
21492       kwd = cp_lexer_consume_token (parser->lexer);
21493       return objc_build_string_object (kwd->u.value);
21494
21495     case CPP_KEYWORD:
21496       switch (kwd->keyword)
21497         {
21498         case RID_AT_ENCODE:
21499           return cp_parser_objc_encode_expression (parser);
21500
21501         case RID_AT_PROTOCOL:
21502           return cp_parser_objc_protocol_expression (parser);
21503
21504         case RID_AT_SELECTOR:
21505           return cp_parser_objc_selector_expression (parser);
21506
21507         default:
21508           break;
21509         }
21510     default:
21511       error_at (kwd->location,
21512                 "misplaced %<@%D%> Objective-C++ construct",
21513                 kwd->u.value);
21514       cp_parser_skip_to_end_of_block_or_statement (parser);
21515     }
21516
21517   return error_mark_node;
21518 }
21519
21520 /* Parse an Objective-C message expression.
21521
21522    objc-message-expression:
21523      [ objc-message-receiver objc-message-args ]
21524
21525    Returns a representation of an Objective-C message.  */
21526
21527 static tree
21528 cp_parser_objc_message_expression (cp_parser* parser)
21529 {
21530   tree receiver, messageargs;
21531
21532   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21533   receiver = cp_parser_objc_message_receiver (parser);
21534   messageargs = cp_parser_objc_message_args (parser);
21535   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21536
21537   return objc_build_message_expr (receiver, messageargs);
21538 }
21539
21540 /* Parse an objc-message-receiver.
21541
21542    objc-message-receiver:
21543      expression
21544      simple-type-specifier
21545
21546   Returns a representation of the type or expression.  */
21547
21548 static tree
21549 cp_parser_objc_message_receiver (cp_parser* parser)
21550 {
21551   tree rcv;
21552
21553   /* An Objective-C message receiver may be either (1) a type
21554      or (2) an expression.  */
21555   cp_parser_parse_tentatively (parser);
21556   rcv = cp_parser_expression (parser, false, NULL);
21557
21558   if (cp_parser_parse_definitely (parser))
21559     return rcv;
21560
21561   rcv = cp_parser_simple_type_specifier (parser,
21562                                          /*decl_specs=*/NULL,
21563                                          CP_PARSER_FLAGS_NONE);
21564
21565   return objc_get_class_reference (rcv);
21566 }
21567
21568 /* Parse the arguments and selectors comprising an Objective-C message.
21569
21570    objc-message-args:
21571      objc-selector
21572      objc-selector-args
21573      objc-selector-args , objc-comma-args
21574
21575    objc-selector-args:
21576      objc-selector [opt] : assignment-expression
21577      objc-selector-args objc-selector [opt] : assignment-expression
21578
21579    objc-comma-args:
21580      assignment-expression
21581      objc-comma-args , assignment-expression
21582
21583    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21584    selector arguments and TREE_VALUE containing a list of comma
21585    arguments.  */
21586
21587 static tree
21588 cp_parser_objc_message_args (cp_parser* parser)
21589 {
21590   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21591   bool maybe_unary_selector_p = true;
21592   cp_token *token = cp_lexer_peek_token (parser->lexer);
21593
21594   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21595     {
21596       tree selector = NULL_TREE, arg;
21597
21598       if (token->type != CPP_COLON)
21599         selector = cp_parser_objc_selector (parser);
21600
21601       /* Detect if we have a unary selector.  */
21602       if (maybe_unary_selector_p
21603           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21604         return build_tree_list (selector, NULL_TREE);
21605
21606       maybe_unary_selector_p = false;
21607       cp_parser_require (parser, CPP_COLON, RT_COLON);
21608       arg = cp_parser_assignment_expression (parser, false, NULL);
21609
21610       sel_args
21611         = chainon (sel_args,
21612                    build_tree_list (selector, arg));
21613
21614       token = cp_lexer_peek_token (parser->lexer);
21615     }
21616
21617   /* Handle non-selector arguments, if any. */
21618   while (token->type == CPP_COMMA)
21619     {
21620       tree arg;
21621
21622       cp_lexer_consume_token (parser->lexer);
21623       arg = cp_parser_assignment_expression (parser, false, NULL);
21624
21625       addl_args
21626         = chainon (addl_args,
21627                    build_tree_list (NULL_TREE, arg));
21628
21629       token = cp_lexer_peek_token (parser->lexer);
21630     }
21631
21632   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21633     {
21634       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21635       return build_tree_list (error_mark_node, error_mark_node);
21636     }
21637
21638   return build_tree_list (sel_args, addl_args);
21639 }
21640
21641 /* Parse an Objective-C encode expression.
21642
21643    objc-encode-expression:
21644      @encode objc-typename
21645
21646    Returns an encoded representation of the type argument.  */
21647
21648 static tree
21649 cp_parser_objc_encode_expression (cp_parser* parser)
21650 {
21651   tree type;
21652   cp_token *token;
21653
21654   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21655   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21656   token = cp_lexer_peek_token (parser->lexer);
21657   type = complete_type (cp_parser_type_id (parser));
21658   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21659
21660   if (!type)
21661     {
21662       error_at (token->location, 
21663                 "%<@encode%> must specify a type as an argument");
21664       return error_mark_node;
21665     }
21666
21667   /* This happens if we find @encode(T) (where T is a template
21668      typename or something dependent on a template typename) when
21669      parsing a template.  In that case, we can't compile it
21670      immediately, but we rather create an AT_ENCODE_EXPR which will
21671      need to be instantiated when the template is used.
21672   */
21673   if (dependent_type_p (type))
21674     {
21675       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21676       TREE_READONLY (value) = 1;
21677       return value;
21678     }
21679
21680   return objc_build_encode_expr (type);
21681 }
21682
21683 /* Parse an Objective-C @defs expression.  */
21684
21685 static tree
21686 cp_parser_objc_defs_expression (cp_parser *parser)
21687 {
21688   tree name;
21689
21690   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21691   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21692   name = cp_parser_identifier (parser);
21693   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21694
21695   return objc_get_class_ivars (name);
21696 }
21697
21698 /* Parse an Objective-C protocol expression.
21699
21700   objc-protocol-expression:
21701     @protocol ( identifier )
21702
21703   Returns a representation of the protocol expression.  */
21704
21705 static tree
21706 cp_parser_objc_protocol_expression (cp_parser* parser)
21707 {
21708   tree proto;
21709
21710   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21711   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21712   proto = cp_parser_identifier (parser);
21713   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21714
21715   return objc_build_protocol_expr (proto);
21716 }
21717
21718 /* Parse an Objective-C selector expression.
21719
21720    objc-selector-expression:
21721      @selector ( objc-method-signature )
21722
21723    objc-method-signature:
21724      objc-selector
21725      objc-selector-seq
21726
21727    objc-selector-seq:
21728      objc-selector :
21729      objc-selector-seq objc-selector :
21730
21731   Returns a representation of the method selector.  */
21732
21733 static tree
21734 cp_parser_objc_selector_expression (cp_parser* parser)
21735 {
21736   tree sel_seq = NULL_TREE;
21737   bool maybe_unary_selector_p = true;
21738   cp_token *token;
21739   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21740
21741   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21742   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21743   token = cp_lexer_peek_token (parser->lexer);
21744
21745   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21746          || token->type == CPP_SCOPE)
21747     {
21748       tree selector = NULL_TREE;
21749
21750       if (token->type != CPP_COLON
21751           || token->type == CPP_SCOPE)
21752         selector = cp_parser_objc_selector (parser);
21753
21754       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21755           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21756         {
21757           /* Detect if we have a unary selector.  */
21758           if (maybe_unary_selector_p)
21759             {
21760               sel_seq = selector;
21761               goto finish_selector;
21762             }
21763           else
21764             {
21765               cp_parser_error (parser, "expected %<:%>");
21766             }
21767         }
21768       maybe_unary_selector_p = false;
21769       token = cp_lexer_consume_token (parser->lexer);
21770
21771       if (token->type == CPP_SCOPE)
21772         {
21773           sel_seq
21774             = chainon (sel_seq,
21775                        build_tree_list (selector, NULL_TREE));
21776           sel_seq
21777             = chainon (sel_seq,
21778                        build_tree_list (NULL_TREE, NULL_TREE));
21779         }
21780       else
21781         sel_seq
21782           = chainon (sel_seq,
21783                      build_tree_list (selector, NULL_TREE));
21784
21785       token = cp_lexer_peek_token (parser->lexer);
21786     }
21787
21788  finish_selector:
21789   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21790
21791   return objc_build_selector_expr (loc, sel_seq);
21792 }
21793
21794 /* Parse a list of identifiers.
21795
21796    objc-identifier-list:
21797      identifier
21798      objc-identifier-list , identifier
21799
21800    Returns a TREE_LIST of identifier nodes.  */
21801
21802 static tree
21803 cp_parser_objc_identifier_list (cp_parser* parser)
21804 {
21805   tree identifier;
21806   tree list;
21807   cp_token *sep;
21808
21809   identifier = cp_parser_identifier (parser);
21810   if (identifier == error_mark_node)
21811     return error_mark_node;      
21812
21813   list = build_tree_list (NULL_TREE, identifier);
21814   sep = cp_lexer_peek_token (parser->lexer);
21815
21816   while (sep->type == CPP_COMMA)
21817     {
21818       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21819       identifier = cp_parser_identifier (parser);
21820       if (identifier == error_mark_node)
21821         return list;
21822
21823       list = chainon (list, build_tree_list (NULL_TREE,
21824                                              identifier));
21825       sep = cp_lexer_peek_token (parser->lexer);
21826     }
21827   
21828   return list;
21829 }
21830
21831 /* Parse an Objective-C alias declaration.
21832
21833    objc-alias-declaration:
21834      @compatibility_alias identifier identifier ;
21835
21836    This function registers the alias mapping with the Objective-C front end.
21837    It returns nothing.  */
21838
21839 static void
21840 cp_parser_objc_alias_declaration (cp_parser* parser)
21841 {
21842   tree alias, orig;
21843
21844   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21845   alias = cp_parser_identifier (parser);
21846   orig = cp_parser_identifier (parser);
21847   objc_declare_alias (alias, orig);
21848   cp_parser_consume_semicolon_at_end_of_statement (parser);
21849 }
21850
21851 /* Parse an Objective-C class forward-declaration.
21852
21853    objc-class-declaration:
21854      @class objc-identifier-list ;
21855
21856    The function registers the forward declarations with the Objective-C
21857    front end.  It returns nothing.  */
21858
21859 static void
21860 cp_parser_objc_class_declaration (cp_parser* parser)
21861 {
21862   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21863   while (true)
21864     {
21865       tree id;
21866       
21867       id = cp_parser_identifier (parser);
21868       if (id == error_mark_node)
21869         break;
21870       
21871       objc_declare_class (id);
21872
21873       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21874         cp_lexer_consume_token (parser->lexer);
21875       else
21876         break;
21877     }
21878   cp_parser_consume_semicolon_at_end_of_statement (parser);
21879 }
21880
21881 /* Parse a list of Objective-C protocol references.
21882
21883    objc-protocol-refs-opt:
21884      objc-protocol-refs [opt]
21885
21886    objc-protocol-refs:
21887      < objc-identifier-list >
21888
21889    Returns a TREE_LIST of identifiers, if any.  */
21890
21891 static tree
21892 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21893 {
21894   tree protorefs = NULL_TREE;
21895
21896   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21897     {
21898       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21899       protorefs = cp_parser_objc_identifier_list (parser);
21900       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21901     }
21902
21903   return protorefs;
21904 }
21905
21906 /* Parse a Objective-C visibility specification.  */
21907
21908 static void
21909 cp_parser_objc_visibility_spec (cp_parser* parser)
21910 {
21911   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21912
21913   switch (vis->keyword)
21914     {
21915     case RID_AT_PRIVATE:
21916       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21917       break;
21918     case RID_AT_PROTECTED:
21919       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21920       break;
21921     case RID_AT_PUBLIC:
21922       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21923       break;
21924     case RID_AT_PACKAGE:
21925       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21926       break;
21927     default:
21928       return;
21929     }
21930
21931   /* Eat '@private'/'@protected'/'@public'.  */
21932   cp_lexer_consume_token (parser->lexer);
21933 }
21934
21935 /* Parse an Objective-C method type.  Return 'true' if it is a class
21936    (+) method, and 'false' if it is an instance (-) method.  */
21937
21938 static inline bool
21939 cp_parser_objc_method_type (cp_parser* parser)
21940 {
21941   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21942     return true;
21943   else
21944     return false;
21945 }
21946
21947 /* Parse an Objective-C protocol qualifier.  */
21948
21949 static tree
21950 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21951 {
21952   tree quals = NULL_TREE, node;
21953   cp_token *token = cp_lexer_peek_token (parser->lexer);
21954
21955   node = token->u.value;
21956
21957   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21958          && (node == ridpointers [(int) RID_IN]
21959              || node == ridpointers [(int) RID_OUT]
21960              || node == ridpointers [(int) RID_INOUT]
21961              || node == ridpointers [(int) RID_BYCOPY]
21962              || node == ridpointers [(int) RID_BYREF]
21963              || node == ridpointers [(int) RID_ONEWAY]))
21964     {
21965       quals = tree_cons (NULL_TREE, node, quals);
21966       cp_lexer_consume_token (parser->lexer);
21967       token = cp_lexer_peek_token (parser->lexer);
21968       node = token->u.value;
21969     }
21970
21971   return quals;
21972 }
21973
21974 /* Parse an Objective-C typename.  */
21975
21976 static tree
21977 cp_parser_objc_typename (cp_parser* parser)
21978 {
21979   tree type_name = NULL_TREE;
21980
21981   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21982     {
21983       tree proto_quals, cp_type = NULL_TREE;
21984
21985       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21986       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21987
21988       /* An ObjC type name may consist of just protocol qualifiers, in which
21989          case the type shall default to 'id'.  */
21990       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21991         {
21992           cp_type = cp_parser_type_id (parser);
21993           
21994           /* If the type could not be parsed, an error has already
21995              been produced.  For error recovery, behave as if it had
21996              not been specified, which will use the default type
21997              'id'.  */
21998           if (cp_type == error_mark_node)
21999             {
22000               cp_type = NULL_TREE;
22001               /* We need to skip to the closing parenthesis as
22002                  cp_parser_type_id() does not seem to do it for
22003                  us.  */
22004               cp_parser_skip_to_closing_parenthesis (parser,
22005                                                      /*recovering=*/true,
22006                                                      /*or_comma=*/false,
22007                                                      /*consume_paren=*/false);
22008             }
22009         }
22010
22011       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22012       type_name = build_tree_list (proto_quals, cp_type);
22013     }
22014
22015   return type_name;
22016 }
22017
22018 /* Check to see if TYPE refers to an Objective-C selector name.  */
22019
22020 static bool
22021 cp_parser_objc_selector_p (enum cpp_ttype type)
22022 {
22023   return (type == CPP_NAME || type == CPP_KEYWORD
22024           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22025           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22026           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22027           || type == CPP_XOR || type == CPP_XOR_EQ);
22028 }
22029
22030 /* Parse an Objective-C selector.  */
22031
22032 static tree
22033 cp_parser_objc_selector (cp_parser* parser)
22034 {
22035   cp_token *token = cp_lexer_consume_token (parser->lexer);
22036
22037   if (!cp_parser_objc_selector_p (token->type))
22038     {
22039       error_at (token->location, "invalid Objective-C++ selector name");
22040       return error_mark_node;
22041     }
22042
22043   /* C++ operator names are allowed to appear in ObjC selectors.  */
22044   switch (token->type)
22045     {
22046     case CPP_AND_AND: return get_identifier ("and");
22047     case CPP_AND_EQ: return get_identifier ("and_eq");
22048     case CPP_AND: return get_identifier ("bitand");
22049     case CPP_OR: return get_identifier ("bitor");
22050     case CPP_COMPL: return get_identifier ("compl");
22051     case CPP_NOT: return get_identifier ("not");
22052     case CPP_NOT_EQ: return get_identifier ("not_eq");
22053     case CPP_OR_OR: return get_identifier ("or");
22054     case CPP_OR_EQ: return get_identifier ("or_eq");
22055     case CPP_XOR: return get_identifier ("xor");
22056     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22057     default: return token->u.value;
22058     }
22059 }
22060
22061 /* Parse an Objective-C params list.  */
22062
22063 static tree
22064 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22065 {
22066   tree params = NULL_TREE;
22067   bool maybe_unary_selector_p = true;
22068   cp_token *token = cp_lexer_peek_token (parser->lexer);
22069
22070   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22071     {
22072       tree selector = NULL_TREE, type_name, identifier;
22073       tree parm_attr = NULL_TREE;
22074
22075       if (token->keyword == RID_ATTRIBUTE)
22076         break;
22077
22078       if (token->type != CPP_COLON)
22079         selector = cp_parser_objc_selector (parser);
22080
22081       /* Detect if we have a unary selector.  */
22082       if (maybe_unary_selector_p
22083           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22084         {
22085           params = selector; /* Might be followed by attributes.  */
22086           break;
22087         }
22088
22089       maybe_unary_selector_p = false;
22090       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22091         {
22092           /* Something went quite wrong.  There should be a colon
22093              here, but there is not.  Stop parsing parameters.  */
22094           break;
22095         }
22096       type_name = cp_parser_objc_typename (parser);
22097       /* New ObjC allows attributes on parameters too.  */
22098       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22099         parm_attr = cp_parser_attributes_opt (parser);
22100       identifier = cp_parser_identifier (parser);
22101
22102       params
22103         = chainon (params,
22104                    objc_build_keyword_decl (selector,
22105                                             type_name,
22106                                             identifier,
22107                                             parm_attr));
22108
22109       token = cp_lexer_peek_token (parser->lexer);
22110     }
22111
22112   if (params == NULL_TREE)
22113     {
22114       cp_parser_error (parser, "objective-c++ method declaration is expected");
22115       return error_mark_node;
22116     }
22117
22118   /* We allow tail attributes for the method.  */
22119   if (token->keyword == RID_ATTRIBUTE)
22120     {
22121       *attributes = cp_parser_attributes_opt (parser);
22122       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22123           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22124         return params;
22125       cp_parser_error (parser, 
22126                        "method attributes must be specified at the end");
22127       return error_mark_node;
22128     }
22129
22130   if (params == NULL_TREE)
22131     {
22132       cp_parser_error (parser, "objective-c++ method declaration is expected");
22133       return error_mark_node;
22134     }
22135   return params;
22136 }
22137
22138 /* Parse the non-keyword Objective-C params.  */
22139
22140 static tree
22141 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22142                                        tree* attributes)
22143 {
22144   tree params = make_node (TREE_LIST);
22145   cp_token *token = cp_lexer_peek_token (parser->lexer);
22146   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22147
22148   while (token->type == CPP_COMMA)
22149     {
22150       cp_parameter_declarator *parmdecl;
22151       tree parm;
22152
22153       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22154       token = cp_lexer_peek_token (parser->lexer);
22155
22156       if (token->type == CPP_ELLIPSIS)
22157         {
22158           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22159           *ellipsisp = true;
22160           token = cp_lexer_peek_token (parser->lexer);
22161           break;
22162         }
22163
22164       /* TODO: parse attributes for tail parameters.  */
22165       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22166       parm = grokdeclarator (parmdecl->declarator,
22167                              &parmdecl->decl_specifiers,
22168                              PARM, /*initialized=*/0,
22169                              /*attrlist=*/NULL);
22170
22171       chainon (params, build_tree_list (NULL_TREE, parm));
22172       token = cp_lexer_peek_token (parser->lexer);
22173     }
22174
22175   /* We allow tail attributes for the method.  */
22176   if (token->keyword == RID_ATTRIBUTE)
22177     {
22178       if (*attributes == NULL_TREE)
22179         {
22180           *attributes = cp_parser_attributes_opt (parser);
22181           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22182               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22183             return params;
22184         }
22185       else        
22186         /* We have an error, but parse the attributes, so that we can 
22187            carry on.  */
22188         *attributes = cp_parser_attributes_opt (parser);
22189
22190       cp_parser_error (parser, 
22191                        "method attributes must be specified at the end");
22192       return error_mark_node;
22193     }
22194
22195   return params;
22196 }
22197
22198 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22199
22200 static void
22201 cp_parser_objc_interstitial_code (cp_parser* parser)
22202 {
22203   cp_token *token = cp_lexer_peek_token (parser->lexer);
22204
22205   /* If the next token is `extern' and the following token is a string
22206      literal, then we have a linkage specification.  */
22207   if (token->keyword == RID_EXTERN
22208       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22209     cp_parser_linkage_specification (parser);
22210   /* Handle #pragma, if any.  */
22211   else if (token->type == CPP_PRAGMA)
22212     cp_parser_pragma (parser, pragma_external);
22213   /* Allow stray semicolons.  */
22214   else if (token->type == CPP_SEMICOLON)
22215     cp_lexer_consume_token (parser->lexer);
22216   /* Mark methods as optional or required, when building protocols.  */
22217   else if (token->keyword == RID_AT_OPTIONAL)
22218     {
22219       cp_lexer_consume_token (parser->lexer);
22220       objc_set_method_opt (true);
22221     }
22222   else if (token->keyword == RID_AT_REQUIRED)
22223     {
22224       cp_lexer_consume_token (parser->lexer);
22225       objc_set_method_opt (false);
22226     }
22227   else if (token->keyword == RID_NAMESPACE)
22228     cp_parser_namespace_definition (parser);
22229   /* Other stray characters must generate errors.  */
22230   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22231     {
22232       cp_lexer_consume_token (parser->lexer);
22233       error ("stray %qs between Objective-C++ methods",
22234              token->type == CPP_OPEN_BRACE ? "{" : "}");
22235     }
22236   /* Finally, try to parse a block-declaration, or a function-definition.  */
22237   else
22238     cp_parser_block_declaration (parser, /*statement_p=*/false);
22239 }
22240
22241 /* Parse a method signature.  */
22242
22243 static tree
22244 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22245 {
22246   tree rettype, kwdparms, optparms;
22247   bool ellipsis = false;
22248   bool is_class_method;
22249
22250   is_class_method = cp_parser_objc_method_type (parser);
22251   rettype = cp_parser_objc_typename (parser);
22252   *attributes = NULL_TREE;
22253   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22254   if (kwdparms == error_mark_node)
22255     return error_mark_node;
22256   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22257   if (optparms == error_mark_node)
22258     return error_mark_node;
22259
22260   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22261 }
22262
22263 static bool
22264 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22265 {
22266   tree tattr;  
22267   cp_lexer_save_tokens (parser->lexer);
22268   tattr = cp_parser_attributes_opt (parser);
22269   gcc_assert (tattr) ;
22270   
22271   /* If the attributes are followed by a method introducer, this is not allowed.
22272      Dump the attributes and flag the situation.  */
22273   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22274       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22275     return true;
22276
22277   /* Otherwise, the attributes introduce some interstitial code, possibly so
22278      rewind to allow that check.  */
22279   cp_lexer_rollback_tokens (parser->lexer);
22280   return false;  
22281 }
22282
22283 /* Parse an Objective-C method prototype list.  */
22284
22285 static void
22286 cp_parser_objc_method_prototype_list (cp_parser* parser)
22287 {
22288   cp_token *token = cp_lexer_peek_token (parser->lexer);
22289
22290   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22291     {
22292       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22293         {
22294           tree attributes, sig;
22295           bool is_class_method;
22296           if (token->type == CPP_PLUS)
22297             is_class_method = true;
22298           else
22299             is_class_method = false;
22300           sig = cp_parser_objc_method_signature (parser, &attributes);
22301           if (sig == error_mark_node)
22302             {
22303               cp_parser_skip_to_end_of_block_or_statement (parser);
22304               token = cp_lexer_peek_token (parser->lexer);
22305               continue;
22306             }
22307           objc_add_method_declaration (is_class_method, sig, attributes);
22308           cp_parser_consume_semicolon_at_end_of_statement (parser);
22309         }
22310       else if (token->keyword == RID_AT_PROPERTY)
22311         cp_parser_objc_at_property_declaration (parser);
22312       else if (token->keyword == RID_ATTRIBUTE 
22313                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22314         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22315                     OPT_Wattributes, 
22316                     "prefix attributes are ignored for methods");
22317       else
22318         /* Allow for interspersed non-ObjC++ code.  */
22319         cp_parser_objc_interstitial_code (parser);
22320
22321       token = cp_lexer_peek_token (parser->lexer);
22322     }
22323
22324   if (token->type != CPP_EOF)
22325     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22326   else
22327     cp_parser_error (parser, "expected %<@end%>");
22328
22329   objc_finish_interface ();
22330 }
22331
22332 /* Parse an Objective-C method definition list.  */
22333
22334 static void
22335 cp_parser_objc_method_definition_list (cp_parser* parser)
22336 {
22337   cp_token *token = cp_lexer_peek_token (parser->lexer);
22338
22339   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22340     {
22341       tree meth;
22342
22343       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22344         {
22345           cp_token *ptk;
22346           tree sig, attribute;
22347           bool is_class_method;
22348           if (token->type == CPP_PLUS)
22349             is_class_method = true;
22350           else
22351             is_class_method = false;
22352           push_deferring_access_checks (dk_deferred);
22353           sig = cp_parser_objc_method_signature (parser, &attribute);
22354           if (sig == error_mark_node)
22355             {
22356               cp_parser_skip_to_end_of_block_or_statement (parser);
22357               token = cp_lexer_peek_token (parser->lexer);
22358               continue;
22359             }
22360           objc_start_method_definition (is_class_method, sig, attribute,
22361                                         NULL_TREE);
22362
22363           /* For historical reasons, we accept an optional semicolon.  */
22364           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22365             cp_lexer_consume_token (parser->lexer);
22366
22367           ptk = cp_lexer_peek_token (parser->lexer);
22368           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22369                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22370             {
22371               perform_deferred_access_checks ();
22372               stop_deferring_access_checks ();
22373               meth = cp_parser_function_definition_after_declarator (parser,
22374                                                                      false);
22375               pop_deferring_access_checks ();
22376               objc_finish_method_definition (meth);
22377             }
22378         }
22379       /* The following case will be removed once @synthesize is
22380          completely implemented.  */
22381       else if (token->keyword == RID_AT_PROPERTY)
22382         cp_parser_objc_at_property_declaration (parser);
22383       else if (token->keyword == RID_AT_SYNTHESIZE)
22384         cp_parser_objc_at_synthesize_declaration (parser);
22385       else if (token->keyword == RID_AT_DYNAMIC)
22386         cp_parser_objc_at_dynamic_declaration (parser);
22387       else if (token->keyword == RID_ATTRIBUTE 
22388                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22389         warning_at (token->location, OPT_Wattributes,
22390                     "prefix attributes are ignored for methods");
22391       else
22392         /* Allow for interspersed non-ObjC++ code.  */
22393         cp_parser_objc_interstitial_code (parser);
22394
22395       token = cp_lexer_peek_token (parser->lexer);
22396     }
22397
22398   if (token->type != CPP_EOF)
22399     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22400   else
22401     cp_parser_error (parser, "expected %<@end%>");
22402
22403   objc_finish_implementation ();
22404 }
22405
22406 /* Parse Objective-C ivars.  */
22407
22408 static void
22409 cp_parser_objc_class_ivars (cp_parser* parser)
22410 {
22411   cp_token *token = cp_lexer_peek_token (parser->lexer);
22412
22413   if (token->type != CPP_OPEN_BRACE)
22414     return;     /* No ivars specified.  */
22415
22416   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22417   token = cp_lexer_peek_token (parser->lexer);
22418
22419   while (token->type != CPP_CLOSE_BRACE 
22420         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22421     {
22422       cp_decl_specifier_seq declspecs;
22423       int decl_class_or_enum_p;
22424       tree prefix_attributes;
22425
22426       cp_parser_objc_visibility_spec (parser);
22427
22428       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22429         break;
22430
22431       cp_parser_decl_specifier_seq (parser,
22432                                     CP_PARSER_FLAGS_OPTIONAL,
22433                                     &declspecs,
22434                                     &decl_class_or_enum_p);
22435
22436       /* auto, register, static, extern, mutable.  */
22437       if (declspecs.storage_class != sc_none)
22438         {
22439           cp_parser_error (parser, "invalid type for instance variable");         
22440           declspecs.storage_class = sc_none;
22441         }
22442
22443       /* __thread.  */
22444       if (declspecs.specs[(int) ds_thread])
22445         {
22446           cp_parser_error (parser, "invalid type for instance variable");
22447           declspecs.specs[(int) ds_thread] = 0;
22448         }
22449       
22450       /* typedef.  */
22451       if (declspecs.specs[(int) ds_typedef])
22452         {
22453           cp_parser_error (parser, "invalid type for instance variable");
22454           declspecs.specs[(int) ds_typedef] = 0;
22455         }
22456
22457       prefix_attributes = declspecs.attributes;
22458       declspecs.attributes = NULL_TREE;
22459
22460       /* Keep going until we hit the `;' at the end of the
22461          declaration.  */
22462       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22463         {
22464           tree width = NULL_TREE, attributes, first_attribute, decl;
22465           cp_declarator *declarator = NULL;
22466           int ctor_dtor_or_conv_p;
22467
22468           /* Check for a (possibly unnamed) bitfield declaration.  */
22469           token = cp_lexer_peek_token (parser->lexer);
22470           if (token->type == CPP_COLON)
22471             goto eat_colon;
22472
22473           if (token->type == CPP_NAME
22474               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22475                   == CPP_COLON))
22476             {
22477               /* Get the name of the bitfield.  */
22478               declarator = make_id_declarator (NULL_TREE,
22479                                                cp_parser_identifier (parser),
22480                                                sfk_none);
22481
22482              eat_colon:
22483               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22484               /* Get the width of the bitfield.  */
22485               width
22486                 = cp_parser_constant_expression (parser,
22487                                                  /*allow_non_constant=*/false,
22488                                                  NULL);
22489             }
22490           else
22491             {
22492               /* Parse the declarator.  */
22493               declarator
22494                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22495                                         &ctor_dtor_or_conv_p,
22496                                         /*parenthesized_p=*/NULL,
22497                                         /*member_p=*/false);
22498             }
22499
22500           /* Look for attributes that apply to the ivar.  */
22501           attributes = cp_parser_attributes_opt (parser);
22502           /* Remember which attributes are prefix attributes and
22503              which are not.  */
22504           first_attribute = attributes;
22505           /* Combine the attributes.  */
22506           attributes = chainon (prefix_attributes, attributes);
22507
22508           if (width)
22509               /* Create the bitfield declaration.  */
22510               decl = grokbitfield (declarator, &declspecs,
22511                                    width,
22512                                    attributes);
22513           else
22514             decl = grokfield (declarator, &declspecs,
22515                               NULL_TREE, /*init_const_expr_p=*/false,
22516                               NULL_TREE, attributes);
22517
22518           /* Add the instance variable.  */
22519           if (decl != error_mark_node && decl != NULL_TREE)
22520             objc_add_instance_variable (decl);
22521
22522           /* Reset PREFIX_ATTRIBUTES.  */
22523           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22524             attributes = TREE_CHAIN (attributes);
22525           if (attributes)
22526             TREE_CHAIN (attributes) = NULL_TREE;
22527
22528           token = cp_lexer_peek_token (parser->lexer);
22529
22530           if (token->type == CPP_COMMA)
22531             {
22532               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22533               continue;
22534             }
22535           break;
22536         }
22537
22538       cp_parser_consume_semicolon_at_end_of_statement (parser);
22539       token = cp_lexer_peek_token (parser->lexer);
22540     }
22541
22542   if (token->keyword == RID_AT_END)
22543     cp_parser_error (parser, "expected %<}%>");
22544
22545   /* Do not consume the RID_AT_END, so it will be read again as terminating
22546      the @interface of @implementation.  */ 
22547   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22548     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22549     
22550   /* For historical reasons, we accept an optional semicolon.  */
22551   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22552     cp_lexer_consume_token (parser->lexer);
22553 }
22554
22555 /* Parse an Objective-C protocol declaration.  */
22556
22557 static void
22558 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22559 {
22560   tree proto, protorefs;
22561   cp_token *tok;
22562
22563   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22564   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22565     {
22566       tok = cp_lexer_peek_token (parser->lexer);
22567       error_at (tok->location, "identifier expected after %<@protocol%>");
22568       cp_parser_consume_semicolon_at_end_of_statement (parser);
22569       return;
22570     }
22571
22572   /* See if we have a forward declaration or a definition.  */
22573   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22574
22575   /* Try a forward declaration first.  */
22576   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22577     {
22578       while (true)
22579         {
22580           tree id;
22581           
22582           id = cp_parser_identifier (parser);
22583           if (id == error_mark_node)
22584             break;
22585           
22586           objc_declare_protocol (id, attributes);
22587           
22588           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22589             cp_lexer_consume_token (parser->lexer);
22590           else
22591             break;
22592         }
22593       cp_parser_consume_semicolon_at_end_of_statement (parser);
22594     }
22595
22596   /* Ok, we got a full-fledged definition (or at least should).  */
22597   else
22598     {
22599       proto = cp_parser_identifier (parser);
22600       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22601       objc_start_protocol (proto, protorefs, attributes);
22602       cp_parser_objc_method_prototype_list (parser);
22603     }
22604 }
22605
22606 /* Parse an Objective-C superclass or category.  */
22607
22608 static void
22609 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22610                                        bool iface_p,
22611                                        tree *super,
22612                                        tree *categ, bool *is_class_extension)
22613 {
22614   cp_token *next = cp_lexer_peek_token (parser->lexer);
22615
22616   *super = *categ = NULL_TREE;
22617   *is_class_extension = false;
22618   if (next->type == CPP_COLON)
22619     {
22620       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22621       *super = cp_parser_identifier (parser);
22622     }
22623   else if (next->type == CPP_OPEN_PAREN)
22624     {
22625       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22626
22627       /* If there is no category name, and this is an @interface, we
22628          have a class extension.  */
22629       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22630         {
22631           *categ = NULL_TREE;
22632           *is_class_extension = true;
22633         }
22634       else
22635         *categ = cp_parser_identifier (parser);
22636
22637       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22638     }
22639 }
22640
22641 /* Parse an Objective-C class interface.  */
22642
22643 static void
22644 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22645 {
22646   tree name, super, categ, protos;
22647   bool is_class_extension;
22648
22649   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22650   name = cp_parser_identifier (parser);
22651   if (name == error_mark_node)
22652     {
22653       /* It's hard to recover because even if valid @interface stuff
22654          is to follow, we can't compile it (or validate it) if we
22655          don't even know which class it refers to.  Let's assume this
22656          was a stray '@interface' token in the stream and skip it.
22657       */
22658       return;
22659     }
22660   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22661                                          &is_class_extension);
22662   protos = cp_parser_objc_protocol_refs_opt (parser);
22663
22664   /* We have either a class or a category on our hands.  */
22665   if (categ || is_class_extension)
22666     objc_start_category_interface (name, categ, protos, attributes);
22667   else
22668     {
22669       objc_start_class_interface (name, super, protos, attributes);
22670       /* Handle instance variable declarations, if any.  */
22671       cp_parser_objc_class_ivars (parser);
22672       objc_continue_interface ();
22673     }
22674
22675   cp_parser_objc_method_prototype_list (parser);
22676 }
22677
22678 /* Parse an Objective-C class implementation.  */
22679
22680 static void
22681 cp_parser_objc_class_implementation (cp_parser* parser)
22682 {
22683   tree name, super, categ;
22684   bool is_class_extension;
22685
22686   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22687   name = cp_parser_identifier (parser);
22688   if (name == error_mark_node)
22689     {
22690       /* It's hard to recover because even if valid @implementation
22691          stuff is to follow, we can't compile it (or validate it) if
22692          we don't even know which class it refers to.  Let's assume
22693          this was a stray '@implementation' token in the stream and
22694          skip it.
22695       */
22696       return;
22697     }
22698   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22699                                          &is_class_extension);
22700
22701   /* We have either a class or a category on our hands.  */
22702   if (categ)
22703     objc_start_category_implementation (name, categ);
22704   else
22705     {
22706       objc_start_class_implementation (name, super);
22707       /* Handle instance variable declarations, if any.  */
22708       cp_parser_objc_class_ivars (parser);
22709       objc_continue_implementation ();
22710     }
22711
22712   cp_parser_objc_method_definition_list (parser);
22713 }
22714
22715 /* Consume the @end token and finish off the implementation.  */
22716
22717 static void
22718 cp_parser_objc_end_implementation (cp_parser* parser)
22719 {
22720   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22721   objc_finish_implementation ();
22722 }
22723
22724 /* Parse an Objective-C declaration.  */
22725
22726 static void
22727 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22728 {
22729   /* Try to figure out what kind of declaration is present.  */
22730   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22731
22732   if (attributes)
22733     switch (kwd->keyword)
22734       {
22735         case RID_AT_ALIAS:
22736         case RID_AT_CLASS:
22737         case RID_AT_END:
22738           error_at (kwd->location, "attributes may not be specified before"
22739                     " the %<@%D%> Objective-C++ keyword",
22740                     kwd->u.value);
22741           attributes = NULL;
22742           break;
22743         case RID_AT_IMPLEMENTATION:
22744           warning_at (kwd->location, OPT_Wattributes,
22745                       "prefix attributes are ignored before %<@%D%>",
22746                       kwd->u.value);
22747           attributes = NULL;
22748         default:
22749           break;
22750       }
22751
22752   switch (kwd->keyword)
22753     {
22754     case RID_AT_ALIAS:
22755       cp_parser_objc_alias_declaration (parser);
22756       break;
22757     case RID_AT_CLASS:
22758       cp_parser_objc_class_declaration (parser);
22759       break;
22760     case RID_AT_PROTOCOL:
22761       cp_parser_objc_protocol_declaration (parser, attributes);
22762       break;
22763     case RID_AT_INTERFACE:
22764       cp_parser_objc_class_interface (parser, attributes);
22765       break;
22766     case RID_AT_IMPLEMENTATION:
22767       cp_parser_objc_class_implementation (parser);
22768       break;
22769     case RID_AT_END:
22770       cp_parser_objc_end_implementation (parser);
22771       break;
22772     default:
22773       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22774                 kwd->u.value);
22775       cp_parser_skip_to_end_of_block_or_statement (parser);
22776     }
22777 }
22778
22779 /* Parse an Objective-C try-catch-finally statement.
22780
22781    objc-try-catch-finally-stmt:
22782      @try compound-statement objc-catch-clause-seq [opt]
22783        objc-finally-clause [opt]
22784
22785    objc-catch-clause-seq:
22786      objc-catch-clause objc-catch-clause-seq [opt]
22787
22788    objc-catch-clause:
22789      @catch ( objc-exception-declaration ) compound-statement
22790
22791    objc-finally-clause:
22792      @finally compound-statement
22793
22794    objc-exception-declaration:
22795      parameter-declaration
22796      '...'
22797
22798    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22799
22800    Returns NULL_TREE.
22801
22802    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22803    for C.  Keep them in sync.  */   
22804
22805 static tree
22806 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22807 {
22808   location_t location;
22809   tree stmt;
22810
22811   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22812   location = cp_lexer_peek_token (parser->lexer)->location;
22813   objc_maybe_warn_exceptions (location);
22814   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22815      node, lest it get absorbed into the surrounding block.  */
22816   stmt = push_stmt_list ();
22817   cp_parser_compound_statement (parser, NULL, false, false);
22818   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22819
22820   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22821     {
22822       cp_parameter_declarator *parm;
22823       tree parameter_declaration = error_mark_node;
22824       bool seen_open_paren = false;
22825
22826       cp_lexer_consume_token (parser->lexer);
22827       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22828         seen_open_paren = true;
22829       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22830         {
22831           /* We have "@catch (...)" (where the '...' are literally
22832              what is in the code).  Skip the '...'.
22833              parameter_declaration is set to NULL_TREE, and
22834              objc_being_catch_clauses() knows that that means
22835              '...'.  */
22836           cp_lexer_consume_token (parser->lexer);
22837           parameter_declaration = NULL_TREE;
22838         }
22839       else
22840         {
22841           /* We have "@catch (NSException *exception)" or something
22842              like that.  Parse the parameter declaration.  */
22843           parm = cp_parser_parameter_declaration (parser, false, NULL);
22844           if (parm == NULL)
22845             parameter_declaration = error_mark_node;
22846           else
22847             parameter_declaration = grokdeclarator (parm->declarator,
22848                                                     &parm->decl_specifiers,
22849                                                     PARM, /*initialized=*/0,
22850                                                     /*attrlist=*/NULL);
22851         }
22852       if (seen_open_paren)
22853         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22854       else
22855         {
22856           /* If there was no open parenthesis, we are recovering from
22857              an error, and we are trying to figure out what mistake
22858              the user has made.  */
22859
22860           /* If there is an immediate closing parenthesis, the user
22861              probably forgot the opening one (ie, they typed "@catch
22862              NSException *e)".  Parse the closing parenthesis and keep
22863              going.  */
22864           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22865             cp_lexer_consume_token (parser->lexer);
22866           
22867           /* If these is no immediate closing parenthesis, the user
22868              probably doesn't know that parenthesis are required at
22869              all (ie, they typed "@catch NSException *e").  So, just
22870              forget about the closing parenthesis and keep going.  */
22871         }
22872       objc_begin_catch_clause (parameter_declaration);
22873       cp_parser_compound_statement (parser, NULL, false, false);
22874       objc_finish_catch_clause ();
22875     }
22876   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22877     {
22878       cp_lexer_consume_token (parser->lexer);
22879       location = cp_lexer_peek_token (parser->lexer)->location;
22880       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22881          node, lest it get absorbed into the surrounding block.  */
22882       stmt = push_stmt_list ();
22883       cp_parser_compound_statement (parser, NULL, false, false);
22884       objc_build_finally_clause (location, pop_stmt_list (stmt));
22885     }
22886
22887   return objc_finish_try_stmt ();
22888 }
22889
22890 /* Parse an Objective-C synchronized statement.
22891
22892    objc-synchronized-stmt:
22893      @synchronized ( expression ) compound-statement
22894
22895    Returns NULL_TREE.  */
22896
22897 static tree
22898 cp_parser_objc_synchronized_statement (cp_parser *parser)
22899 {
22900   location_t location;
22901   tree lock, stmt;
22902
22903   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22904
22905   location = cp_lexer_peek_token (parser->lexer)->location;
22906   objc_maybe_warn_exceptions (location);
22907   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22908   lock = cp_parser_expression (parser, false, NULL);
22909   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22910
22911   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22912      node, lest it get absorbed into the surrounding block.  */
22913   stmt = push_stmt_list ();
22914   cp_parser_compound_statement (parser, NULL, false, false);
22915
22916   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22917 }
22918
22919 /* Parse an Objective-C throw statement.
22920
22921    objc-throw-stmt:
22922      @throw assignment-expression [opt] ;
22923
22924    Returns a constructed '@throw' statement.  */
22925
22926 static tree
22927 cp_parser_objc_throw_statement (cp_parser *parser)
22928 {
22929   tree expr = NULL_TREE;
22930   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22931
22932   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22933
22934   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22935     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22936
22937   cp_parser_consume_semicolon_at_end_of_statement (parser);
22938
22939   return objc_build_throw_stmt (loc, expr);
22940 }
22941
22942 /* Parse an Objective-C statement.  */
22943
22944 static tree
22945 cp_parser_objc_statement (cp_parser * parser)
22946 {
22947   /* Try to figure out what kind of declaration is present.  */
22948   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22949
22950   switch (kwd->keyword)
22951     {
22952     case RID_AT_TRY:
22953       return cp_parser_objc_try_catch_finally_statement (parser);
22954     case RID_AT_SYNCHRONIZED:
22955       return cp_parser_objc_synchronized_statement (parser);
22956     case RID_AT_THROW:
22957       return cp_parser_objc_throw_statement (parser);
22958     default:
22959       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22960                kwd->u.value);
22961       cp_parser_skip_to_end_of_block_or_statement (parser);
22962     }
22963
22964   return error_mark_node;
22965 }
22966
22967 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22968    look ahead to see if an objc keyword follows the attributes.  This
22969    is to detect the use of prefix attributes on ObjC @interface and 
22970    @protocol.  */
22971
22972 static bool
22973 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22974 {
22975   cp_lexer_save_tokens (parser->lexer);
22976   *attrib = cp_parser_attributes_opt (parser);
22977   gcc_assert (*attrib);
22978   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22979     {
22980       cp_lexer_commit_tokens (parser->lexer);
22981       return true;
22982     }
22983   cp_lexer_rollback_tokens (parser->lexer);
22984   return false;  
22985 }
22986
22987 /* This routine is a minimal replacement for
22988    c_parser_struct_declaration () used when parsing the list of
22989    types/names or ObjC++ properties.  For example, when parsing the
22990    code
22991
22992    @property (readonly) int a, b, c;
22993
22994    this function is responsible for parsing "int a, int b, int c" and
22995    returning the declarations as CHAIN of DECLs.
22996
22997    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22998    similar parsing.  */
22999 static tree
23000 cp_parser_objc_struct_declaration (cp_parser *parser)
23001 {
23002   tree decls = NULL_TREE;
23003   cp_decl_specifier_seq declspecs;
23004   int decl_class_or_enum_p;
23005   tree prefix_attributes;
23006
23007   cp_parser_decl_specifier_seq (parser,
23008                                 CP_PARSER_FLAGS_NONE,
23009                                 &declspecs,
23010                                 &decl_class_or_enum_p);
23011
23012   if (declspecs.type == error_mark_node)
23013     return error_mark_node;
23014
23015   /* auto, register, static, extern, mutable.  */
23016   if (declspecs.storage_class != sc_none)
23017     {
23018       cp_parser_error (parser, "invalid type for property");
23019       declspecs.storage_class = sc_none;
23020     }
23021   
23022   /* __thread.  */
23023   if (declspecs.specs[(int) ds_thread])
23024     {
23025       cp_parser_error (parser, "invalid type for property");
23026       declspecs.specs[(int) ds_thread] = 0;
23027     }
23028   
23029   /* typedef.  */
23030   if (declspecs.specs[(int) ds_typedef])
23031     {
23032       cp_parser_error (parser, "invalid type for property");
23033       declspecs.specs[(int) ds_typedef] = 0;
23034     }
23035
23036   prefix_attributes = declspecs.attributes;
23037   declspecs.attributes = NULL_TREE;
23038
23039   /* Keep going until we hit the `;' at the end of the declaration. */
23040   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23041     {
23042       tree attributes, first_attribute, decl;
23043       cp_declarator *declarator;
23044       cp_token *token;
23045
23046       /* Parse the declarator.  */
23047       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23048                                          NULL, NULL, false);
23049
23050       /* Look for attributes that apply to the ivar.  */
23051       attributes = cp_parser_attributes_opt (parser);
23052       /* Remember which attributes are prefix attributes and
23053          which are not.  */
23054       first_attribute = attributes;
23055       /* Combine the attributes.  */
23056       attributes = chainon (prefix_attributes, attributes);
23057       
23058       decl = grokfield (declarator, &declspecs,
23059                         NULL_TREE, /*init_const_expr_p=*/false,
23060                         NULL_TREE, attributes);
23061
23062       if (decl == error_mark_node || decl == NULL_TREE)
23063         return error_mark_node;
23064       
23065       /* Reset PREFIX_ATTRIBUTES.  */
23066       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23067         attributes = TREE_CHAIN (attributes);
23068       if (attributes)
23069         TREE_CHAIN (attributes) = NULL_TREE;
23070
23071       DECL_CHAIN (decl) = decls;
23072       decls = decl;
23073
23074       token = cp_lexer_peek_token (parser->lexer);
23075       if (token->type == CPP_COMMA)
23076         {
23077           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23078           continue;
23079         }
23080       else
23081         break;
23082     }
23083   return decls;
23084 }
23085
23086 /* Parse an Objective-C @property declaration.  The syntax is:
23087
23088    objc-property-declaration:
23089      '@property' objc-property-attributes[opt] struct-declaration ;
23090
23091    objc-property-attributes:
23092     '(' objc-property-attribute-list ')'
23093
23094    objc-property-attribute-list:
23095      objc-property-attribute
23096      objc-property-attribute-list, objc-property-attribute
23097
23098    objc-property-attribute
23099      'getter' = identifier
23100      'setter' = identifier
23101      'readonly'
23102      'readwrite'
23103      'assign'
23104      'retain'
23105      'copy'
23106      'nonatomic'
23107
23108   For example:
23109     @property NSString *name;
23110     @property (readonly) id object;
23111     @property (retain, nonatomic, getter=getTheName) id name;
23112     @property int a, b, c;
23113
23114    PS: This function is identical to
23115    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23116 static void 
23117 cp_parser_objc_at_property_declaration (cp_parser *parser)
23118 {
23119   /* The following variables hold the attributes of the properties as
23120      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23121      seen.  When we see an attribute, we set them to 'true' (if they
23122      are boolean properties) or to the identifier (if they have an
23123      argument, ie, for getter and setter).  Note that here we only
23124      parse the list of attributes, check the syntax and accumulate the
23125      attributes that we find.  objc_add_property_declaration() will
23126      then process the information.  */
23127   bool property_assign = false;
23128   bool property_copy = false;
23129   tree property_getter_ident = NULL_TREE;
23130   bool property_nonatomic = false;
23131   bool property_readonly = false;
23132   bool property_readwrite = false;
23133   bool property_retain = false;
23134   tree property_setter_ident = NULL_TREE;
23135
23136   /* 'properties' is the list of properties that we read.  Usually a
23137      single one, but maybe more (eg, in "@property int a, b, c;" there
23138      are three).  */
23139   tree properties;
23140   location_t loc;
23141
23142   loc = cp_lexer_peek_token (parser->lexer)->location;
23143
23144   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23145
23146   /* Parse the optional attribute list...  */
23147   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23148     {
23149       /* Eat the '('.  */
23150       cp_lexer_consume_token (parser->lexer);
23151
23152       while (true)
23153         {
23154           bool syntax_error = false;
23155           cp_token *token = cp_lexer_peek_token (parser->lexer);
23156           enum rid keyword;
23157
23158           if (token->type != CPP_NAME)
23159             {
23160               cp_parser_error (parser, "expected identifier");
23161               break;
23162             }
23163           keyword = C_RID_CODE (token->u.value);
23164           cp_lexer_consume_token (parser->lexer);
23165           switch (keyword)
23166             {
23167             case RID_ASSIGN:    property_assign = true;    break;
23168             case RID_COPY:      property_copy = true;      break;
23169             case RID_NONATOMIC: property_nonatomic = true; break;
23170             case RID_READONLY:  property_readonly = true;  break;
23171             case RID_READWRITE: property_readwrite = true; break;
23172             case RID_RETAIN:    property_retain = true;    break;
23173
23174             case RID_GETTER:
23175             case RID_SETTER:
23176               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23177                 {
23178                   if (keyword == RID_GETTER)
23179                     cp_parser_error (parser,
23180                                      "missing %<=%> (after %<getter%> attribute)");
23181                   else
23182                     cp_parser_error (parser,
23183                                      "missing %<=%> (after %<setter%> attribute)");
23184                   syntax_error = true;
23185                   break;
23186                 }
23187               cp_lexer_consume_token (parser->lexer); /* eat the = */
23188               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23189                 {
23190                   cp_parser_error (parser, "expected identifier");
23191                   syntax_error = true;
23192                   break;
23193                 }
23194               if (keyword == RID_SETTER)
23195                 {
23196                   if (property_setter_ident != NULL_TREE)
23197                     {
23198                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23199                       cp_lexer_consume_token (parser->lexer);
23200                     }
23201                   else
23202                     property_setter_ident = cp_parser_objc_selector (parser);
23203                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23204                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23205                   else
23206                     cp_lexer_consume_token (parser->lexer);
23207                 }
23208               else
23209                 {
23210                   if (property_getter_ident != NULL_TREE)
23211                     {
23212                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23213                       cp_lexer_consume_token (parser->lexer);
23214                     }
23215                   else
23216                     property_getter_ident = cp_parser_objc_selector (parser);
23217                 }
23218               break;
23219             default:
23220               cp_parser_error (parser, "unknown property attribute");
23221               syntax_error = true;
23222               break;
23223             }
23224
23225           if (syntax_error)
23226             break;
23227
23228           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23229             cp_lexer_consume_token (parser->lexer);
23230           else
23231             break;
23232         }
23233
23234       /* FIXME: "@property (setter, assign);" will generate a spurious
23235          "error: expected â€˜)’ before â€˜,’ token".  This is because
23236          cp_parser_require, unlike the C counterpart, will produce an
23237          error even if we are in error recovery.  */
23238       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23239         {
23240           cp_parser_skip_to_closing_parenthesis (parser,
23241                                                  /*recovering=*/true,
23242                                                  /*or_comma=*/false,
23243                                                  /*consume_paren=*/true);
23244         }
23245     }
23246
23247   /* ... and the property declaration(s).  */
23248   properties = cp_parser_objc_struct_declaration (parser);
23249
23250   if (properties == error_mark_node)
23251     {
23252       cp_parser_skip_to_end_of_statement (parser);
23253       /* If the next token is now a `;', consume it.  */
23254       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23255         cp_lexer_consume_token (parser->lexer);
23256       return;
23257     }
23258
23259   if (properties == NULL_TREE)
23260     cp_parser_error (parser, "expected identifier");
23261   else
23262     {
23263       /* Comma-separated properties are chained together in
23264          reverse order; add them one by one.  */
23265       properties = nreverse (properties);
23266       
23267       for (; properties; properties = TREE_CHAIN (properties))
23268         objc_add_property_declaration (loc, copy_node (properties),
23269                                        property_readonly, property_readwrite,
23270                                        property_assign, property_retain,
23271                                        property_copy, property_nonatomic,
23272                                        property_getter_ident, property_setter_ident);
23273     }
23274   
23275   cp_parser_consume_semicolon_at_end_of_statement (parser);
23276 }
23277
23278 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23279
23280    objc-synthesize-declaration:
23281      @synthesize objc-synthesize-identifier-list ;
23282
23283    objc-synthesize-identifier-list:
23284      objc-synthesize-identifier
23285      objc-synthesize-identifier-list, objc-synthesize-identifier
23286
23287    objc-synthesize-identifier
23288      identifier
23289      identifier = identifier
23290
23291   For example:
23292     @synthesize MyProperty;
23293     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23294
23295   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23296   for C.  Keep them in sync.
23297 */
23298 static void 
23299 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23300 {
23301   tree list = NULL_TREE;
23302   location_t loc;
23303   loc = cp_lexer_peek_token (parser->lexer)->location;
23304
23305   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23306   while (true)
23307     {
23308       tree property, ivar;
23309       property = cp_parser_identifier (parser);
23310       if (property == error_mark_node)
23311         {
23312           cp_parser_consume_semicolon_at_end_of_statement (parser);
23313           return;
23314         }
23315       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23316         {
23317           cp_lexer_consume_token (parser->lexer);
23318           ivar = cp_parser_identifier (parser);
23319           if (ivar == error_mark_node)
23320             {
23321               cp_parser_consume_semicolon_at_end_of_statement (parser);
23322               return;
23323             }
23324         }
23325       else
23326         ivar = NULL_TREE;
23327       list = chainon (list, build_tree_list (ivar, property));
23328       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23329         cp_lexer_consume_token (parser->lexer);
23330       else
23331         break;
23332     }
23333   cp_parser_consume_semicolon_at_end_of_statement (parser);
23334   objc_add_synthesize_declaration (loc, list);
23335 }
23336
23337 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23338
23339    objc-dynamic-declaration:
23340      @dynamic identifier-list ;
23341
23342    For example:
23343      @dynamic MyProperty;
23344      @dynamic MyProperty, AnotherProperty;
23345
23346   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23347   for C.  Keep them in sync.
23348 */
23349 static void 
23350 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23351 {
23352   tree list = NULL_TREE;
23353   location_t loc;
23354   loc = cp_lexer_peek_token (parser->lexer)->location;
23355
23356   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23357   while (true)
23358     {
23359       tree property;
23360       property = cp_parser_identifier (parser);
23361       if (property == error_mark_node)
23362         {
23363           cp_parser_consume_semicolon_at_end_of_statement (parser);
23364           return;
23365         }
23366       list = chainon (list, build_tree_list (NULL, property));
23367       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23368         cp_lexer_consume_token (parser->lexer);
23369       else
23370         break;
23371     }
23372   cp_parser_consume_semicolon_at_end_of_statement (parser);
23373   objc_add_dynamic_declaration (loc, list);
23374 }
23375
23376 \f
23377 /* OpenMP 2.5 parsing routines.  */
23378
23379 /* Returns name of the next clause.
23380    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23381    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23382    returned and the token is consumed.  */
23383
23384 static pragma_omp_clause
23385 cp_parser_omp_clause_name (cp_parser *parser)
23386 {
23387   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23388
23389   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23390     result = PRAGMA_OMP_CLAUSE_IF;
23391   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23392     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23393   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23394     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23395   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23396     {
23397       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23398       const char *p = IDENTIFIER_POINTER (id);
23399
23400       switch (p[0])
23401         {
23402         case 'c':
23403           if (!strcmp ("collapse", p))
23404             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23405           else if (!strcmp ("copyin", p))
23406             result = PRAGMA_OMP_CLAUSE_COPYIN;
23407           else if (!strcmp ("copyprivate", p))
23408             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23409           break;
23410         case 'f':
23411           if (!strcmp ("firstprivate", p))
23412             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23413           break;
23414         case 'l':
23415           if (!strcmp ("lastprivate", p))
23416             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23417           break;
23418         case 'n':
23419           if (!strcmp ("nowait", p))
23420             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23421           else if (!strcmp ("num_threads", p))
23422             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23423           break;
23424         case 'o':
23425           if (!strcmp ("ordered", p))
23426             result = PRAGMA_OMP_CLAUSE_ORDERED;
23427           break;
23428         case 'r':
23429           if (!strcmp ("reduction", p))
23430             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23431           break;
23432         case 's':
23433           if (!strcmp ("schedule", p))
23434             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23435           else if (!strcmp ("shared", p))
23436             result = PRAGMA_OMP_CLAUSE_SHARED;
23437           break;
23438         case 'u':
23439           if (!strcmp ("untied", p))
23440             result = PRAGMA_OMP_CLAUSE_UNTIED;
23441           break;
23442         }
23443     }
23444
23445   if (result != PRAGMA_OMP_CLAUSE_NONE)
23446     cp_lexer_consume_token (parser->lexer);
23447
23448   return result;
23449 }
23450
23451 /* Validate that a clause of the given type does not already exist.  */
23452
23453 static void
23454 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23455                            const char *name, location_t location)
23456 {
23457   tree c;
23458
23459   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23460     if (OMP_CLAUSE_CODE (c) == code)
23461       {
23462         error_at (location, "too many %qs clauses", name);
23463         break;
23464       }
23465 }
23466
23467 /* OpenMP 2.5:
23468    variable-list:
23469      identifier
23470      variable-list , identifier
23471
23472    In addition, we match a closing parenthesis.  An opening parenthesis
23473    will have been consumed by the caller.
23474
23475    If KIND is nonzero, create the appropriate node and install the decl
23476    in OMP_CLAUSE_DECL and add the node to the head of the list.
23477
23478    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23479    return the list created.  */
23480
23481 static tree
23482 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23483                                 tree list)
23484 {
23485   cp_token *token;
23486   while (1)
23487     {
23488       tree name, decl;
23489
23490       token = cp_lexer_peek_token (parser->lexer);
23491       name = cp_parser_id_expression (parser, /*template_p=*/false,
23492                                       /*check_dependency_p=*/true,
23493                                       /*template_p=*/NULL,
23494                                       /*declarator_p=*/false,
23495                                       /*optional_p=*/false);
23496       if (name == error_mark_node)
23497         goto skip_comma;
23498
23499       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23500       if (decl == error_mark_node)
23501         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23502                                      token->location);
23503       else if (kind != 0)
23504         {
23505           tree u = build_omp_clause (token->location, kind);
23506           OMP_CLAUSE_DECL (u) = decl;
23507           OMP_CLAUSE_CHAIN (u) = list;
23508           list = u;
23509         }
23510       else
23511         list = tree_cons (decl, NULL_TREE, list);
23512
23513     get_comma:
23514       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23515         break;
23516       cp_lexer_consume_token (parser->lexer);
23517     }
23518
23519   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23520     {
23521       int ending;
23522
23523       /* Try to resync to an unnested comma.  Copied from
23524          cp_parser_parenthesized_expression_list.  */
23525     skip_comma:
23526       ending = cp_parser_skip_to_closing_parenthesis (parser,
23527                                                       /*recovering=*/true,
23528                                                       /*or_comma=*/true,
23529                                                       /*consume_paren=*/true);
23530       if (ending < 0)
23531         goto get_comma;
23532     }
23533
23534   return list;
23535 }
23536
23537 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23538    common case for omp clauses.  */
23539
23540 static tree
23541 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23542 {
23543   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23544     return cp_parser_omp_var_list_no_open (parser, kind, list);
23545   return list;
23546 }
23547
23548 /* OpenMP 3.0:
23549    collapse ( constant-expression ) */
23550
23551 static tree
23552 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23553 {
23554   tree c, num;
23555   location_t loc;
23556   HOST_WIDE_INT n;
23557
23558   loc = cp_lexer_peek_token (parser->lexer)->location;
23559   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23560     return list;
23561
23562   num = cp_parser_constant_expression (parser, false, NULL);
23563
23564   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23565     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23566                                            /*or_comma=*/false,
23567                                            /*consume_paren=*/true);
23568
23569   if (num == error_mark_node)
23570     return list;
23571   num = fold_non_dependent_expr (num);
23572   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23573       || !host_integerp (num, 0)
23574       || (n = tree_low_cst (num, 0)) <= 0
23575       || (int) n != n)
23576     {
23577       error_at (loc, "collapse argument needs positive constant integer expression");
23578       return list;
23579     }
23580
23581   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23582   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23583   OMP_CLAUSE_CHAIN (c) = list;
23584   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23585
23586   return c;
23587 }
23588
23589 /* OpenMP 2.5:
23590    default ( shared | none ) */
23591
23592 static tree
23593 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23594 {
23595   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23596   tree c;
23597
23598   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23599     return list;
23600   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23601     {
23602       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23603       const char *p = IDENTIFIER_POINTER (id);
23604
23605       switch (p[0])
23606         {
23607         case 'n':
23608           if (strcmp ("none", p) != 0)
23609             goto invalid_kind;
23610           kind = OMP_CLAUSE_DEFAULT_NONE;
23611           break;
23612
23613         case 's':
23614           if (strcmp ("shared", p) != 0)
23615             goto invalid_kind;
23616           kind = OMP_CLAUSE_DEFAULT_SHARED;
23617           break;
23618
23619         default:
23620           goto invalid_kind;
23621         }
23622
23623       cp_lexer_consume_token (parser->lexer);
23624     }
23625   else
23626     {
23627     invalid_kind:
23628       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23629     }
23630
23631   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23632     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23633                                            /*or_comma=*/false,
23634                                            /*consume_paren=*/true);
23635
23636   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23637     return list;
23638
23639   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23640   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23641   OMP_CLAUSE_CHAIN (c) = list;
23642   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23643
23644   return c;
23645 }
23646
23647 /* OpenMP 2.5:
23648    if ( expression ) */
23649
23650 static tree
23651 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23652 {
23653   tree t, c;
23654
23655   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23656     return list;
23657
23658   t = cp_parser_condition (parser);
23659
23660   if (t == error_mark_node
23661       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23662     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23663                                            /*or_comma=*/false,
23664                                            /*consume_paren=*/true);
23665
23666   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23667
23668   c = build_omp_clause (location, OMP_CLAUSE_IF);
23669   OMP_CLAUSE_IF_EXPR (c) = t;
23670   OMP_CLAUSE_CHAIN (c) = list;
23671
23672   return c;
23673 }
23674
23675 /* OpenMP 2.5:
23676    nowait */
23677
23678 static tree
23679 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23680                              tree list, location_t location)
23681 {
23682   tree c;
23683
23684   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23685
23686   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23687   OMP_CLAUSE_CHAIN (c) = list;
23688   return c;
23689 }
23690
23691 /* OpenMP 2.5:
23692    num_threads ( expression ) */
23693
23694 static tree
23695 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23696                                   location_t location)
23697 {
23698   tree t, c;
23699
23700   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23701     return list;
23702
23703   t = cp_parser_expression (parser, false, NULL);
23704
23705   if (t == error_mark_node
23706       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23707     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23708                                            /*or_comma=*/false,
23709                                            /*consume_paren=*/true);
23710
23711   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23712                              "num_threads", location);
23713
23714   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23715   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23716   OMP_CLAUSE_CHAIN (c) = list;
23717
23718   return c;
23719 }
23720
23721 /* OpenMP 2.5:
23722    ordered */
23723
23724 static tree
23725 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23726                               tree list, location_t location)
23727 {
23728   tree c;
23729
23730   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23731                              "ordered", location);
23732
23733   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23734   OMP_CLAUSE_CHAIN (c) = list;
23735   return c;
23736 }
23737
23738 /* OpenMP 2.5:
23739    reduction ( reduction-operator : variable-list )
23740
23741    reduction-operator:
23742      One of: + * - & ^ | && || */
23743
23744 static tree
23745 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23746 {
23747   enum tree_code code;
23748   tree nlist, c;
23749
23750   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23751     return list;
23752
23753   switch (cp_lexer_peek_token (parser->lexer)->type)
23754     {
23755     case CPP_PLUS:
23756       code = PLUS_EXPR;
23757       break;
23758     case CPP_MULT:
23759       code = MULT_EXPR;
23760       break;
23761     case CPP_MINUS:
23762       code = MINUS_EXPR;
23763       break;
23764     case CPP_AND:
23765       code = BIT_AND_EXPR;
23766       break;
23767     case CPP_XOR:
23768       code = BIT_XOR_EXPR;
23769       break;
23770     case CPP_OR:
23771       code = BIT_IOR_EXPR;
23772       break;
23773     case CPP_AND_AND:
23774       code = TRUTH_ANDIF_EXPR;
23775       break;
23776     case CPP_OR_OR:
23777       code = TRUTH_ORIF_EXPR;
23778       break;
23779     default:
23780       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23781                                "%<|%>, %<&&%>, or %<||%>");
23782     resync_fail:
23783       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23784                                              /*or_comma=*/false,
23785                                              /*consume_paren=*/true);
23786       return list;
23787     }
23788   cp_lexer_consume_token (parser->lexer);
23789
23790   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23791     goto resync_fail;
23792
23793   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23794   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23795     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23796
23797   return nlist;
23798 }
23799
23800 /* OpenMP 2.5:
23801    schedule ( schedule-kind )
23802    schedule ( schedule-kind , expression )
23803
23804    schedule-kind:
23805      static | dynamic | guided | runtime | auto  */
23806
23807 static tree
23808 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23809 {
23810   tree c, t;
23811
23812   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23813     return list;
23814
23815   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23816
23817   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23818     {
23819       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23820       const char *p = IDENTIFIER_POINTER (id);
23821
23822       switch (p[0])
23823         {
23824         case 'd':
23825           if (strcmp ("dynamic", p) != 0)
23826             goto invalid_kind;
23827           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23828           break;
23829
23830         case 'g':
23831           if (strcmp ("guided", p) != 0)
23832             goto invalid_kind;
23833           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23834           break;
23835
23836         case 'r':
23837           if (strcmp ("runtime", p) != 0)
23838             goto invalid_kind;
23839           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23840           break;
23841
23842         default:
23843           goto invalid_kind;
23844         }
23845     }
23846   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23847     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23848   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23849     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23850   else
23851     goto invalid_kind;
23852   cp_lexer_consume_token (parser->lexer);
23853
23854   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23855     {
23856       cp_token *token;
23857       cp_lexer_consume_token (parser->lexer);
23858
23859       token = cp_lexer_peek_token (parser->lexer);
23860       t = cp_parser_assignment_expression (parser, false, NULL);
23861
23862       if (t == error_mark_node)
23863         goto resync_fail;
23864       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23865         error_at (token->location, "schedule %<runtime%> does not take "
23866                   "a %<chunk_size%> parameter");
23867       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23868         error_at (token->location, "schedule %<auto%> does not take "
23869                   "a %<chunk_size%> parameter");
23870       else
23871         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23872
23873       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23874         goto resync_fail;
23875     }
23876   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23877     goto resync_fail;
23878
23879   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23880   OMP_CLAUSE_CHAIN (c) = list;
23881   return c;
23882
23883  invalid_kind:
23884   cp_parser_error (parser, "invalid schedule kind");
23885  resync_fail:
23886   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23887                                          /*or_comma=*/false,
23888                                          /*consume_paren=*/true);
23889   return list;
23890 }
23891
23892 /* OpenMP 3.0:
23893    untied */
23894
23895 static tree
23896 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23897                              tree list, location_t location)
23898 {
23899   tree c;
23900
23901   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23902
23903   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23904   OMP_CLAUSE_CHAIN (c) = list;
23905   return c;
23906 }
23907
23908 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23909    is a bitmask in MASK.  Return the list of clauses found; the result
23910    of clause default goes in *pdefault.  */
23911
23912 static tree
23913 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23914                            const char *where, cp_token *pragma_tok)
23915 {
23916   tree clauses = NULL;
23917   bool first = true;
23918   cp_token *token = NULL;
23919
23920   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23921     {
23922       pragma_omp_clause c_kind;
23923       const char *c_name;
23924       tree prev = clauses;
23925
23926       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23927         cp_lexer_consume_token (parser->lexer);
23928
23929       token = cp_lexer_peek_token (parser->lexer);
23930       c_kind = cp_parser_omp_clause_name (parser);
23931       first = false;
23932
23933       switch (c_kind)
23934         {
23935         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23936           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23937                                                    token->location);
23938           c_name = "collapse";
23939           break;
23940         case PRAGMA_OMP_CLAUSE_COPYIN:
23941           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23942           c_name = "copyin";
23943           break;
23944         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23945           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23946                                             clauses);
23947           c_name = "copyprivate";
23948           break;
23949         case PRAGMA_OMP_CLAUSE_DEFAULT:
23950           clauses = cp_parser_omp_clause_default (parser, clauses,
23951                                                   token->location);
23952           c_name = "default";
23953           break;
23954         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23955           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23956                                             clauses);
23957           c_name = "firstprivate";
23958           break;
23959         case PRAGMA_OMP_CLAUSE_IF:
23960           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23961           c_name = "if";
23962           break;
23963         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23964           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23965                                             clauses);
23966           c_name = "lastprivate";
23967           break;
23968         case PRAGMA_OMP_CLAUSE_NOWAIT:
23969           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23970           c_name = "nowait";
23971           break;
23972         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23973           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23974                                                       token->location);
23975           c_name = "num_threads";
23976           break;
23977         case PRAGMA_OMP_CLAUSE_ORDERED:
23978           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23979                                                   token->location);
23980           c_name = "ordered";
23981           break;
23982         case PRAGMA_OMP_CLAUSE_PRIVATE:
23983           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23984                                             clauses);
23985           c_name = "private";
23986           break;
23987         case PRAGMA_OMP_CLAUSE_REDUCTION:
23988           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23989           c_name = "reduction";
23990           break;
23991         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23992           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23993                                                    token->location);
23994           c_name = "schedule";
23995           break;
23996         case PRAGMA_OMP_CLAUSE_SHARED:
23997           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23998                                             clauses);
23999           c_name = "shared";
24000           break;
24001         case PRAGMA_OMP_CLAUSE_UNTIED:
24002           clauses = cp_parser_omp_clause_untied (parser, clauses,
24003                                                  token->location);
24004           c_name = "nowait";
24005           break;
24006         default:
24007           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24008           goto saw_error;
24009         }
24010
24011       if (((mask >> c_kind) & 1) == 0)
24012         {
24013           /* Remove the invalid clause(s) from the list to avoid
24014              confusing the rest of the compiler.  */
24015           clauses = prev;
24016           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24017         }
24018     }
24019  saw_error:
24020   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24021   return finish_omp_clauses (clauses);
24022 }
24023
24024 /* OpenMP 2.5:
24025    structured-block:
24026      statement
24027
24028    In practice, we're also interested in adding the statement to an
24029    outer node.  So it is convenient if we work around the fact that
24030    cp_parser_statement calls add_stmt.  */
24031
24032 static unsigned
24033 cp_parser_begin_omp_structured_block (cp_parser *parser)
24034 {
24035   unsigned save = parser->in_statement;
24036
24037   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24038      This preserves the "not within loop or switch" style error messages
24039      for nonsense cases like
24040         void foo() {
24041         #pragma omp single
24042           break;
24043         }
24044   */
24045   if (parser->in_statement)
24046     parser->in_statement = IN_OMP_BLOCK;
24047
24048   return save;
24049 }
24050
24051 static void
24052 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24053 {
24054   parser->in_statement = save;
24055 }
24056
24057 static tree
24058 cp_parser_omp_structured_block (cp_parser *parser)
24059 {
24060   tree stmt = begin_omp_structured_block ();
24061   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24062
24063   cp_parser_statement (parser, NULL_TREE, false, NULL);
24064
24065   cp_parser_end_omp_structured_block (parser, save);
24066   return finish_omp_structured_block (stmt);
24067 }
24068
24069 /* OpenMP 2.5:
24070    # pragma omp atomic new-line
24071      expression-stmt
24072
24073    expression-stmt:
24074      x binop= expr | x++ | ++x | x-- | --x
24075    binop:
24076      +, *, -, /, &, ^, |, <<, >>
24077
24078   where x is an lvalue expression with scalar type.  */
24079
24080 static void
24081 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24082 {
24083   tree lhs, rhs;
24084   enum tree_code code;
24085
24086   cp_parser_require_pragma_eol (parser, pragma_tok);
24087
24088   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24089                                     /*cast_p=*/false, NULL);
24090   switch (TREE_CODE (lhs))
24091     {
24092     case ERROR_MARK:
24093       goto saw_error;
24094
24095     case PREINCREMENT_EXPR:
24096     case POSTINCREMENT_EXPR:
24097       lhs = TREE_OPERAND (lhs, 0);
24098       code = PLUS_EXPR;
24099       rhs = integer_one_node;
24100       break;
24101
24102     case PREDECREMENT_EXPR:
24103     case POSTDECREMENT_EXPR:
24104       lhs = TREE_OPERAND (lhs, 0);
24105       code = MINUS_EXPR;
24106       rhs = integer_one_node;
24107       break;
24108
24109     case COMPOUND_EXPR:
24110       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24111          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24112          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24113          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24114          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24115                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24116             == BOOLEAN_TYPE)
24117        /* Undo effects of boolean_increment for post {in,de}crement.  */
24118        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24119       /* FALLTHRU */
24120     case MODIFY_EXPR:
24121       if (TREE_CODE (lhs) == MODIFY_EXPR
24122          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24123        {
24124          /* Undo effects of boolean_increment.  */
24125          if (integer_onep (TREE_OPERAND (lhs, 1)))
24126            {
24127              /* This is pre or post increment.  */
24128              rhs = TREE_OPERAND (lhs, 1);
24129              lhs = TREE_OPERAND (lhs, 0);
24130              code = NOP_EXPR;
24131              break;
24132            }
24133        }
24134       /* FALLTHRU */
24135     default:
24136       switch (cp_lexer_peek_token (parser->lexer)->type)
24137         {
24138         case CPP_MULT_EQ:
24139           code = MULT_EXPR;
24140           break;
24141         case CPP_DIV_EQ:
24142           code = TRUNC_DIV_EXPR;
24143           break;
24144         case CPP_PLUS_EQ:
24145           code = PLUS_EXPR;
24146           break;
24147         case CPP_MINUS_EQ:
24148           code = MINUS_EXPR;
24149           break;
24150         case CPP_LSHIFT_EQ:
24151           code = LSHIFT_EXPR;
24152           break;
24153         case CPP_RSHIFT_EQ:
24154           code = RSHIFT_EXPR;
24155           break;
24156         case CPP_AND_EQ:
24157           code = BIT_AND_EXPR;
24158           break;
24159         case CPP_OR_EQ:
24160           code = BIT_IOR_EXPR;
24161           break;
24162         case CPP_XOR_EQ:
24163           code = BIT_XOR_EXPR;
24164           break;
24165         default:
24166           cp_parser_error (parser,
24167                            "invalid operator for %<#pragma omp atomic%>");
24168           goto saw_error;
24169         }
24170       cp_lexer_consume_token (parser->lexer);
24171
24172       rhs = cp_parser_expression (parser, false, NULL);
24173       if (rhs == error_mark_node)
24174         goto saw_error;
24175       break;
24176     }
24177   finish_omp_atomic (code, lhs, rhs);
24178   cp_parser_consume_semicolon_at_end_of_statement (parser);
24179   return;
24180
24181  saw_error:
24182   cp_parser_skip_to_end_of_block_or_statement (parser);
24183 }
24184
24185
24186 /* OpenMP 2.5:
24187    # pragma omp barrier new-line  */
24188
24189 static void
24190 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24191 {
24192   cp_parser_require_pragma_eol (parser, pragma_tok);
24193   finish_omp_barrier ();
24194 }
24195
24196 /* OpenMP 2.5:
24197    # pragma omp critical [(name)] new-line
24198      structured-block  */
24199
24200 static tree
24201 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24202 {
24203   tree stmt, name = NULL;
24204
24205   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24206     {
24207       cp_lexer_consume_token (parser->lexer);
24208
24209       name = cp_parser_identifier (parser);
24210
24211       if (name == error_mark_node
24212           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24213         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24214                                                /*or_comma=*/false,
24215                                                /*consume_paren=*/true);
24216       if (name == error_mark_node)
24217         name = NULL;
24218     }
24219   cp_parser_require_pragma_eol (parser, pragma_tok);
24220
24221   stmt = cp_parser_omp_structured_block (parser);
24222   return c_finish_omp_critical (input_location, stmt, name);
24223 }
24224
24225 /* OpenMP 2.5:
24226    # pragma omp flush flush-vars[opt] new-line
24227
24228    flush-vars:
24229      ( variable-list ) */
24230
24231 static void
24232 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24233 {
24234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24235     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24236   cp_parser_require_pragma_eol (parser, pragma_tok);
24237
24238   finish_omp_flush ();
24239 }
24240
24241 /* Helper function, to parse omp for increment expression.  */
24242
24243 static tree
24244 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24245 {
24246   tree cond = cp_parser_binary_expression (parser, false, true,
24247                                            PREC_NOT_OPERATOR, NULL);
24248   if (cond == error_mark_node
24249       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24250     {
24251       cp_parser_skip_to_end_of_statement (parser);
24252       return error_mark_node;
24253     }
24254
24255   switch (TREE_CODE (cond))
24256     {
24257     case GT_EXPR:
24258     case GE_EXPR:
24259     case LT_EXPR:
24260     case LE_EXPR:
24261       break;
24262     default:
24263       return error_mark_node;
24264     }
24265
24266   /* If decl is an iterator, preserve LHS and RHS of the relational
24267      expr until finish_omp_for.  */
24268   if (decl
24269       && (type_dependent_expression_p (decl)
24270           || CLASS_TYPE_P (TREE_TYPE (decl))))
24271     return cond;
24272
24273   return build_x_binary_op (TREE_CODE (cond),
24274                             TREE_OPERAND (cond, 0), ERROR_MARK,
24275                             TREE_OPERAND (cond, 1), ERROR_MARK,
24276                             /*overload=*/NULL, tf_warning_or_error);
24277 }
24278
24279 /* Helper function, to parse omp for increment expression.  */
24280
24281 static tree
24282 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24283 {
24284   cp_token *token = cp_lexer_peek_token (parser->lexer);
24285   enum tree_code op;
24286   tree lhs, rhs;
24287   cp_id_kind idk;
24288   bool decl_first;
24289
24290   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24291     {
24292       op = (token->type == CPP_PLUS_PLUS
24293             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24294       cp_lexer_consume_token (parser->lexer);
24295       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24296       if (lhs != decl)
24297         return error_mark_node;
24298       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24299     }
24300
24301   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24302   if (lhs != decl)
24303     return error_mark_node;
24304
24305   token = cp_lexer_peek_token (parser->lexer);
24306   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24307     {
24308       op = (token->type == CPP_PLUS_PLUS
24309             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24310       cp_lexer_consume_token (parser->lexer);
24311       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24312     }
24313
24314   op = cp_parser_assignment_operator_opt (parser);
24315   if (op == ERROR_MARK)
24316     return error_mark_node;
24317
24318   if (op != NOP_EXPR)
24319     {
24320       rhs = cp_parser_assignment_expression (parser, false, NULL);
24321       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24322       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24323     }
24324
24325   lhs = cp_parser_binary_expression (parser, false, false,
24326                                      PREC_ADDITIVE_EXPRESSION, NULL);
24327   token = cp_lexer_peek_token (parser->lexer);
24328   decl_first = lhs == decl;
24329   if (decl_first)
24330     lhs = NULL_TREE;
24331   if (token->type != CPP_PLUS
24332       && token->type != CPP_MINUS)
24333     return error_mark_node;
24334
24335   do
24336     {
24337       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24338       cp_lexer_consume_token (parser->lexer);
24339       rhs = cp_parser_binary_expression (parser, false, false,
24340                                          PREC_ADDITIVE_EXPRESSION, NULL);
24341       token = cp_lexer_peek_token (parser->lexer);
24342       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24343         {
24344           if (lhs == NULL_TREE)
24345             {
24346               if (op == PLUS_EXPR)
24347                 lhs = rhs;
24348               else
24349                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24350             }
24351           else
24352             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24353                                      NULL, tf_warning_or_error);
24354         }
24355     }
24356   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24357
24358   if (!decl_first)
24359     {
24360       if (rhs != decl || op == MINUS_EXPR)
24361         return error_mark_node;
24362       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24363     }
24364   else
24365     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24366
24367   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24368 }
24369
24370 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24371
24372 static tree
24373 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24374 {
24375   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24376   tree real_decl, initv, condv, incrv, declv;
24377   tree this_pre_body, cl;
24378   location_t loc_first;
24379   bool collapse_err = false;
24380   int i, collapse = 1, nbraces = 0;
24381   VEC(tree,gc) *for_block = make_tree_vector ();
24382
24383   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24384     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24385       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24386
24387   gcc_assert (collapse >= 1);
24388
24389   declv = make_tree_vec (collapse);
24390   initv = make_tree_vec (collapse);
24391   condv = make_tree_vec (collapse);
24392   incrv = make_tree_vec (collapse);
24393
24394   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24395
24396   for (i = 0; i < collapse; i++)
24397     {
24398       int bracecount = 0;
24399       bool add_private_clause = false;
24400       location_t loc;
24401
24402       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24403         {
24404           cp_parser_error (parser, "for statement expected");
24405           return NULL;
24406         }
24407       loc = cp_lexer_consume_token (parser->lexer)->location;
24408
24409       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24410         return NULL;
24411
24412       init = decl = real_decl = NULL;
24413       this_pre_body = push_stmt_list ();
24414       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24415         {
24416           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24417
24418              init-expr:
24419                        var = lb
24420                        integer-type var = lb
24421                        random-access-iterator-type var = lb
24422                        pointer-type var = lb
24423           */
24424           cp_decl_specifier_seq type_specifiers;
24425
24426           /* First, try to parse as an initialized declaration.  See
24427              cp_parser_condition, from whence the bulk of this is copied.  */
24428
24429           cp_parser_parse_tentatively (parser);
24430           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24431                                         /*is_trailing_return=*/false,
24432                                         &type_specifiers);
24433           if (cp_parser_parse_definitely (parser))
24434             {
24435               /* If parsing a type specifier seq succeeded, then this
24436                  MUST be a initialized declaration.  */
24437               tree asm_specification, attributes;
24438               cp_declarator *declarator;
24439
24440               declarator = cp_parser_declarator (parser,
24441                                                  CP_PARSER_DECLARATOR_NAMED,
24442                                                  /*ctor_dtor_or_conv_p=*/NULL,
24443                                                  /*parenthesized_p=*/NULL,
24444                                                  /*member_p=*/false);
24445               attributes = cp_parser_attributes_opt (parser);
24446               asm_specification = cp_parser_asm_specification_opt (parser);
24447
24448               if (declarator == cp_error_declarator) 
24449                 cp_parser_skip_to_end_of_statement (parser);
24450
24451               else 
24452                 {
24453                   tree pushed_scope, auto_node;
24454
24455                   decl = start_decl (declarator, &type_specifiers,
24456                                      SD_INITIALIZED, attributes,
24457                                      /*prefix_attributes=*/NULL_TREE,
24458                                      &pushed_scope);
24459
24460                   auto_node = type_uses_auto (TREE_TYPE (decl));
24461                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24462                     {
24463                       if (cp_lexer_next_token_is (parser->lexer, 
24464                                                   CPP_OPEN_PAREN))
24465                         error ("parenthesized initialization is not allowed in "
24466                                "OpenMP %<for%> loop");
24467                       else
24468                         /* Trigger an error.  */
24469                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24470
24471                       init = error_mark_node;
24472                       cp_parser_skip_to_end_of_statement (parser);
24473                     }
24474                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24475                            || type_dependent_expression_p (decl)
24476                            || auto_node)
24477                     {
24478                       bool is_direct_init, is_non_constant_init;
24479
24480                       init = cp_parser_initializer (parser,
24481                                                     &is_direct_init,
24482                                                     &is_non_constant_init);
24483
24484                       if (auto_node && describable_type (init))
24485                         {
24486                           TREE_TYPE (decl)
24487                             = do_auto_deduction (TREE_TYPE (decl), init,
24488                                                  auto_node);
24489
24490                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24491                               && !type_dependent_expression_p (decl))
24492                             goto non_class;
24493                         }
24494                       
24495                       cp_finish_decl (decl, init, !is_non_constant_init,
24496                                       asm_specification,
24497                                       LOOKUP_ONLYCONVERTING);
24498                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24499                         {
24500                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24501                           init = NULL_TREE;
24502                         }
24503                       else
24504                         init = pop_stmt_list (this_pre_body);
24505                       this_pre_body = NULL_TREE;
24506                     }
24507                   else
24508                     {
24509                       /* Consume '='.  */
24510                       cp_lexer_consume_token (parser->lexer);
24511                       init = cp_parser_assignment_expression (parser, false, NULL);
24512
24513                     non_class:
24514                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24515                         init = error_mark_node;
24516                       else
24517                         cp_finish_decl (decl, NULL_TREE,
24518                                         /*init_const_expr_p=*/false,
24519                                         asm_specification,
24520                                         LOOKUP_ONLYCONVERTING);
24521                     }
24522
24523                   if (pushed_scope)
24524                     pop_scope (pushed_scope);
24525                 }
24526             }
24527           else 
24528             {
24529               cp_id_kind idk;
24530               /* If parsing a type specifier sequence failed, then
24531                  this MUST be a simple expression.  */
24532               cp_parser_parse_tentatively (parser);
24533               decl = cp_parser_primary_expression (parser, false, false,
24534                                                    false, &idk);
24535               if (!cp_parser_error_occurred (parser)
24536                   && decl
24537                   && DECL_P (decl)
24538                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24539                 {
24540                   tree rhs;
24541
24542                   cp_parser_parse_definitely (parser);
24543                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24544                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24545                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24546                                                          rhs,
24547                                                          tf_warning_or_error));
24548                   add_private_clause = true;
24549                 }
24550               else
24551                 {
24552                   decl = NULL;
24553                   cp_parser_abort_tentative_parse (parser);
24554                   init = cp_parser_expression (parser, false, NULL);
24555                   if (init)
24556                     {
24557                       if (TREE_CODE (init) == MODIFY_EXPR
24558                           || TREE_CODE (init) == MODOP_EXPR)
24559                         real_decl = TREE_OPERAND (init, 0);
24560                     }
24561                 }
24562             }
24563         }
24564       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24565       if (this_pre_body)
24566         {
24567           this_pre_body = pop_stmt_list (this_pre_body);
24568           if (pre_body)
24569             {
24570               tree t = pre_body;
24571               pre_body = push_stmt_list ();
24572               add_stmt (t);
24573               add_stmt (this_pre_body);
24574               pre_body = pop_stmt_list (pre_body);
24575             }
24576           else
24577             pre_body = this_pre_body;
24578         }
24579
24580       if (decl)
24581         real_decl = decl;
24582       if (par_clauses != NULL && real_decl != NULL_TREE)
24583         {
24584           tree *c;
24585           for (c = par_clauses; *c ; )
24586             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24587                 && OMP_CLAUSE_DECL (*c) == real_decl)
24588               {
24589                 error_at (loc, "iteration variable %qD"
24590                           " should not be firstprivate", real_decl);
24591                 *c = OMP_CLAUSE_CHAIN (*c);
24592               }
24593             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24594                      && OMP_CLAUSE_DECL (*c) == real_decl)
24595               {
24596                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24597                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24598                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24599                 OMP_CLAUSE_DECL (l) = real_decl;
24600                 OMP_CLAUSE_CHAIN (l) = clauses;
24601                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24602                 clauses = l;
24603                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24604                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24605                 add_private_clause = false;
24606               }
24607             else
24608               {
24609                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24610                     && OMP_CLAUSE_DECL (*c) == real_decl)
24611                   add_private_clause = false;
24612                 c = &OMP_CLAUSE_CHAIN (*c);
24613               }
24614         }
24615
24616       if (add_private_clause)
24617         {
24618           tree c;
24619           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24620             {
24621               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24622                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24623                   && OMP_CLAUSE_DECL (c) == decl)
24624                 break;
24625               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24626                        && OMP_CLAUSE_DECL (c) == decl)
24627                 error_at (loc, "iteration variable %qD "
24628                           "should not be firstprivate",
24629                           decl);
24630               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24631                        && OMP_CLAUSE_DECL (c) == decl)
24632                 error_at (loc, "iteration variable %qD should not be reduction",
24633                           decl);
24634             }
24635           if (c == NULL)
24636             {
24637               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24638               OMP_CLAUSE_DECL (c) = decl;
24639               c = finish_omp_clauses (c);
24640               if (c)
24641                 {
24642                   OMP_CLAUSE_CHAIN (c) = clauses;
24643                   clauses = c;
24644                 }
24645             }
24646         }
24647
24648       cond = NULL;
24649       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24650         cond = cp_parser_omp_for_cond (parser, decl);
24651       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24652
24653       incr = NULL;
24654       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24655         {
24656           /* If decl is an iterator, preserve the operator on decl
24657              until finish_omp_for.  */
24658           if (decl
24659               && ((type_dependent_expression_p (decl)
24660                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24661                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24662             incr = cp_parser_omp_for_incr (parser, decl);
24663           else
24664             incr = cp_parser_expression (parser, false, NULL);
24665         }
24666
24667       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24668         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24669                                                /*or_comma=*/false,
24670                                                /*consume_paren=*/true);
24671
24672       TREE_VEC_ELT (declv, i) = decl;
24673       TREE_VEC_ELT (initv, i) = init;
24674       TREE_VEC_ELT (condv, i) = cond;
24675       TREE_VEC_ELT (incrv, i) = incr;
24676
24677       if (i == collapse - 1)
24678         break;
24679
24680       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24681          in between the collapsed for loops to be still considered perfectly
24682          nested.  Hopefully the final version clarifies this.
24683          For now handle (multiple) {'s and empty statements.  */
24684       cp_parser_parse_tentatively (parser);
24685       do
24686         {
24687           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24688             break;
24689           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24690             {
24691               cp_lexer_consume_token (parser->lexer);
24692               bracecount++;
24693             }
24694           else if (bracecount
24695                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24696             cp_lexer_consume_token (parser->lexer);
24697           else
24698             {
24699               loc = cp_lexer_peek_token (parser->lexer)->location;
24700               error_at (loc, "not enough collapsed for loops");
24701               collapse_err = true;
24702               cp_parser_abort_tentative_parse (parser);
24703               declv = NULL_TREE;
24704               break;
24705             }
24706         }
24707       while (1);
24708
24709       if (declv)
24710         {
24711           cp_parser_parse_definitely (parser);
24712           nbraces += bracecount;
24713         }
24714     }
24715
24716   /* Note that we saved the original contents of this flag when we entered
24717      the structured block, and so we don't need to re-save it here.  */
24718   parser->in_statement = IN_OMP_FOR;
24719
24720   /* Note that the grammar doesn't call for a structured block here,
24721      though the loop as a whole is a structured block.  */
24722   body = push_stmt_list ();
24723   cp_parser_statement (parser, NULL_TREE, false, NULL);
24724   body = pop_stmt_list (body);
24725
24726   if (declv == NULL_TREE)
24727     ret = NULL_TREE;
24728   else
24729     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24730                           pre_body, clauses);
24731
24732   while (nbraces)
24733     {
24734       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24735         {
24736           cp_lexer_consume_token (parser->lexer);
24737           nbraces--;
24738         }
24739       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24740         cp_lexer_consume_token (parser->lexer);
24741       else
24742         {
24743           if (!collapse_err)
24744             {
24745               error_at (cp_lexer_peek_token (parser->lexer)->location,
24746                         "collapsed loops not perfectly nested");
24747             }
24748           collapse_err = true;
24749           cp_parser_statement_seq_opt (parser, NULL);
24750           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24751             break;
24752         }
24753     }
24754
24755   while (!VEC_empty (tree, for_block))
24756     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24757   release_tree_vector (for_block);
24758
24759   return ret;
24760 }
24761
24762 /* OpenMP 2.5:
24763    #pragma omp for for-clause[optseq] new-line
24764      for-loop  */
24765
24766 #define OMP_FOR_CLAUSE_MASK                             \
24767         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24768         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24769         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24770         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24771         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24772         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24773         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24774         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24775
24776 static tree
24777 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24778 {
24779   tree clauses, sb, ret;
24780   unsigned int save;
24781
24782   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24783                                        "#pragma omp for", pragma_tok);
24784
24785   sb = begin_omp_structured_block ();
24786   save = cp_parser_begin_omp_structured_block (parser);
24787
24788   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24789
24790   cp_parser_end_omp_structured_block (parser, save);
24791   add_stmt (finish_omp_structured_block (sb));
24792
24793   return ret;
24794 }
24795
24796 /* OpenMP 2.5:
24797    # pragma omp master new-line
24798      structured-block  */
24799
24800 static tree
24801 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24802 {
24803   cp_parser_require_pragma_eol (parser, pragma_tok);
24804   return c_finish_omp_master (input_location,
24805                               cp_parser_omp_structured_block (parser));
24806 }
24807
24808 /* OpenMP 2.5:
24809    # pragma omp ordered new-line
24810      structured-block  */
24811
24812 static tree
24813 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24814 {
24815   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24816   cp_parser_require_pragma_eol (parser, pragma_tok);
24817   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24818 }
24819
24820 /* OpenMP 2.5:
24821
24822    section-scope:
24823      { section-sequence }
24824
24825    section-sequence:
24826      section-directive[opt] structured-block
24827      section-sequence section-directive structured-block  */
24828
24829 static tree
24830 cp_parser_omp_sections_scope (cp_parser *parser)
24831 {
24832   tree stmt, substmt;
24833   bool error_suppress = false;
24834   cp_token *tok;
24835
24836   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24837     return NULL_TREE;
24838
24839   stmt = push_stmt_list ();
24840
24841   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24842     {
24843       unsigned save;
24844
24845       substmt = begin_omp_structured_block ();
24846       save = cp_parser_begin_omp_structured_block (parser);
24847
24848       while (1)
24849         {
24850           cp_parser_statement (parser, NULL_TREE, false, NULL);
24851
24852           tok = cp_lexer_peek_token (parser->lexer);
24853           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24854             break;
24855           if (tok->type == CPP_CLOSE_BRACE)
24856             break;
24857           if (tok->type == CPP_EOF)
24858             break;
24859         }
24860
24861       cp_parser_end_omp_structured_block (parser, save);
24862       substmt = finish_omp_structured_block (substmt);
24863       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24864       add_stmt (substmt);
24865     }
24866
24867   while (1)
24868     {
24869       tok = cp_lexer_peek_token (parser->lexer);
24870       if (tok->type == CPP_CLOSE_BRACE)
24871         break;
24872       if (tok->type == CPP_EOF)
24873         break;
24874
24875       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24876         {
24877           cp_lexer_consume_token (parser->lexer);
24878           cp_parser_require_pragma_eol (parser, tok);
24879           error_suppress = false;
24880         }
24881       else if (!error_suppress)
24882         {
24883           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24884           error_suppress = true;
24885         }
24886
24887       substmt = cp_parser_omp_structured_block (parser);
24888       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24889       add_stmt (substmt);
24890     }
24891   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24892
24893   substmt = pop_stmt_list (stmt);
24894
24895   stmt = make_node (OMP_SECTIONS);
24896   TREE_TYPE (stmt) = void_type_node;
24897   OMP_SECTIONS_BODY (stmt) = substmt;
24898
24899   add_stmt (stmt);
24900   return stmt;
24901 }
24902
24903 /* OpenMP 2.5:
24904    # pragma omp sections sections-clause[optseq] newline
24905      sections-scope  */
24906
24907 #define OMP_SECTIONS_CLAUSE_MASK                        \
24908         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24909         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24910         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24911         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24912         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24913
24914 static tree
24915 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24916 {
24917   tree clauses, ret;
24918
24919   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24920                                        "#pragma omp sections", pragma_tok);
24921
24922   ret = cp_parser_omp_sections_scope (parser);
24923   if (ret)
24924     OMP_SECTIONS_CLAUSES (ret) = clauses;
24925
24926   return ret;
24927 }
24928
24929 /* OpenMP 2.5:
24930    # pragma parallel parallel-clause new-line
24931    # pragma parallel for parallel-for-clause new-line
24932    # pragma parallel sections parallel-sections-clause new-line  */
24933
24934 #define OMP_PARALLEL_CLAUSE_MASK                        \
24935         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24936         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24937         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24938         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24939         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24940         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24941         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24942         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24943
24944 static tree
24945 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24946 {
24947   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24948   const char *p_name = "#pragma omp parallel";
24949   tree stmt, clauses, par_clause, ws_clause, block;
24950   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24951   unsigned int save;
24952   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24953
24954   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24955     {
24956       cp_lexer_consume_token (parser->lexer);
24957       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24958       p_name = "#pragma omp parallel for";
24959       mask |= OMP_FOR_CLAUSE_MASK;
24960       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24961     }
24962   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24963     {
24964       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24965       const char *p = IDENTIFIER_POINTER (id);
24966       if (strcmp (p, "sections") == 0)
24967         {
24968           cp_lexer_consume_token (parser->lexer);
24969           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24970           p_name = "#pragma omp parallel sections";
24971           mask |= OMP_SECTIONS_CLAUSE_MASK;
24972           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24973         }
24974     }
24975
24976   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24977   block = begin_omp_parallel ();
24978   save = cp_parser_begin_omp_structured_block (parser);
24979
24980   switch (p_kind)
24981     {
24982     case PRAGMA_OMP_PARALLEL:
24983       cp_parser_statement (parser, NULL_TREE, false, NULL);
24984       par_clause = clauses;
24985       break;
24986
24987     case PRAGMA_OMP_PARALLEL_FOR:
24988       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24989       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24990       break;
24991
24992     case PRAGMA_OMP_PARALLEL_SECTIONS:
24993       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24994       stmt = cp_parser_omp_sections_scope (parser);
24995       if (stmt)
24996         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24997       break;
24998
24999     default:
25000       gcc_unreachable ();
25001     }
25002
25003   cp_parser_end_omp_structured_block (parser, save);
25004   stmt = finish_omp_parallel (par_clause, block);
25005   if (p_kind != PRAGMA_OMP_PARALLEL)
25006     OMP_PARALLEL_COMBINED (stmt) = 1;
25007   return stmt;
25008 }
25009
25010 /* OpenMP 2.5:
25011    # pragma omp single single-clause[optseq] new-line
25012      structured-block  */
25013
25014 #define OMP_SINGLE_CLAUSE_MASK                          \
25015         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25016         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25017         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25018         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25019
25020 static tree
25021 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25022 {
25023   tree stmt = make_node (OMP_SINGLE);
25024   TREE_TYPE (stmt) = void_type_node;
25025
25026   OMP_SINGLE_CLAUSES (stmt)
25027     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25028                                  "#pragma omp single", pragma_tok);
25029   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25030
25031   return add_stmt (stmt);
25032 }
25033
25034 /* OpenMP 3.0:
25035    # pragma omp task task-clause[optseq] new-line
25036      structured-block  */
25037
25038 #define OMP_TASK_CLAUSE_MASK                            \
25039         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25040         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25041         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25042         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25043         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25044         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25045
25046 static tree
25047 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25048 {
25049   tree clauses, block;
25050   unsigned int save;
25051
25052   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25053                                        "#pragma omp task", pragma_tok);
25054   block = begin_omp_task ();
25055   save = cp_parser_begin_omp_structured_block (parser);
25056   cp_parser_statement (parser, NULL_TREE, false, NULL);
25057   cp_parser_end_omp_structured_block (parser, save);
25058   return finish_omp_task (clauses, block);
25059 }
25060
25061 /* OpenMP 3.0:
25062    # pragma omp taskwait new-line  */
25063
25064 static void
25065 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25066 {
25067   cp_parser_require_pragma_eol (parser, pragma_tok);
25068   finish_omp_taskwait ();
25069 }
25070
25071 /* OpenMP 2.5:
25072    # pragma omp threadprivate (variable-list) */
25073
25074 static void
25075 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25076 {
25077   tree vars;
25078
25079   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25080   cp_parser_require_pragma_eol (parser, pragma_tok);
25081
25082   finish_omp_threadprivate (vars);
25083 }
25084
25085 /* Main entry point to OpenMP statement pragmas.  */
25086
25087 static void
25088 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25089 {
25090   tree stmt;
25091
25092   switch (pragma_tok->pragma_kind)
25093     {
25094     case PRAGMA_OMP_ATOMIC:
25095       cp_parser_omp_atomic (parser, pragma_tok);
25096       return;
25097     case PRAGMA_OMP_CRITICAL:
25098       stmt = cp_parser_omp_critical (parser, pragma_tok);
25099       break;
25100     case PRAGMA_OMP_FOR:
25101       stmt = cp_parser_omp_for (parser, pragma_tok);
25102       break;
25103     case PRAGMA_OMP_MASTER:
25104       stmt = cp_parser_omp_master (parser, pragma_tok);
25105       break;
25106     case PRAGMA_OMP_ORDERED:
25107       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25108       break;
25109     case PRAGMA_OMP_PARALLEL:
25110       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25111       break;
25112     case PRAGMA_OMP_SECTIONS:
25113       stmt = cp_parser_omp_sections (parser, pragma_tok);
25114       break;
25115     case PRAGMA_OMP_SINGLE:
25116       stmt = cp_parser_omp_single (parser, pragma_tok);
25117       break;
25118     case PRAGMA_OMP_TASK:
25119       stmt = cp_parser_omp_task (parser, pragma_tok);
25120       break;
25121     default:
25122       gcc_unreachable ();
25123     }
25124
25125   if (stmt)
25126     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25127 }
25128 \f
25129 /* The parser.  */
25130
25131 static GTY (()) cp_parser *the_parser;
25132
25133 \f
25134 /* Special handling for the first token or line in the file.  The first
25135    thing in the file might be #pragma GCC pch_preprocess, which loads a
25136    PCH file, which is a GC collection point.  So we need to handle this
25137    first pragma without benefit of an existing lexer structure.
25138
25139    Always returns one token to the caller in *FIRST_TOKEN.  This is
25140    either the true first token of the file, or the first token after
25141    the initial pragma.  */
25142
25143 static void
25144 cp_parser_initial_pragma (cp_token *first_token)
25145 {
25146   tree name = NULL;
25147
25148   cp_lexer_get_preprocessor_token (NULL, first_token);
25149   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25150     return;
25151
25152   cp_lexer_get_preprocessor_token (NULL, first_token);
25153   if (first_token->type == CPP_STRING)
25154     {
25155       name = first_token->u.value;
25156
25157       cp_lexer_get_preprocessor_token (NULL, first_token);
25158       if (first_token->type != CPP_PRAGMA_EOL)
25159         error_at (first_token->location,
25160                   "junk at end of %<#pragma GCC pch_preprocess%>");
25161     }
25162   else
25163     error_at (first_token->location, "expected string literal");
25164
25165   /* Skip to the end of the pragma.  */
25166   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25167     cp_lexer_get_preprocessor_token (NULL, first_token);
25168
25169   /* Now actually load the PCH file.  */
25170   if (name)
25171     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25172
25173   /* Read one more token to return to our caller.  We have to do this
25174      after reading the PCH file in, since its pointers have to be
25175      live.  */
25176   cp_lexer_get_preprocessor_token (NULL, first_token);
25177 }
25178
25179 /* Normal parsing of a pragma token.  Here we can (and must) use the
25180    regular lexer.  */
25181
25182 static bool
25183 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25184 {
25185   cp_token *pragma_tok;
25186   unsigned int id;
25187
25188   pragma_tok = cp_lexer_consume_token (parser->lexer);
25189   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25190   parser->lexer->in_pragma = true;
25191
25192   id = pragma_tok->pragma_kind;
25193   switch (id)
25194     {
25195     case PRAGMA_GCC_PCH_PREPROCESS:
25196       error_at (pragma_tok->location,
25197                 "%<#pragma GCC pch_preprocess%> must be first");
25198       break;
25199
25200     case PRAGMA_OMP_BARRIER:
25201       switch (context)
25202         {
25203         case pragma_compound:
25204           cp_parser_omp_barrier (parser, pragma_tok);
25205           return false;
25206         case pragma_stmt:
25207           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25208                     "used in compound statements");
25209           break;
25210         default:
25211           goto bad_stmt;
25212         }
25213       break;
25214
25215     case PRAGMA_OMP_FLUSH:
25216       switch (context)
25217         {
25218         case pragma_compound:
25219           cp_parser_omp_flush (parser, pragma_tok);
25220           return false;
25221         case pragma_stmt:
25222           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25223                     "used in compound statements");
25224           break;
25225         default:
25226           goto bad_stmt;
25227         }
25228       break;
25229
25230     case PRAGMA_OMP_TASKWAIT:
25231       switch (context)
25232         {
25233         case pragma_compound:
25234           cp_parser_omp_taskwait (parser, pragma_tok);
25235           return false;
25236         case pragma_stmt:
25237           error_at (pragma_tok->location,
25238                     "%<#pragma omp taskwait%> may only be "
25239                     "used in compound statements");
25240           break;
25241         default:
25242           goto bad_stmt;
25243         }
25244       break;
25245
25246     case PRAGMA_OMP_THREADPRIVATE:
25247       cp_parser_omp_threadprivate (parser, pragma_tok);
25248       return false;
25249
25250     case PRAGMA_OMP_ATOMIC:
25251     case PRAGMA_OMP_CRITICAL:
25252     case PRAGMA_OMP_FOR:
25253     case PRAGMA_OMP_MASTER:
25254     case PRAGMA_OMP_ORDERED:
25255     case PRAGMA_OMP_PARALLEL:
25256     case PRAGMA_OMP_SECTIONS:
25257     case PRAGMA_OMP_SINGLE:
25258     case PRAGMA_OMP_TASK:
25259       if (context == pragma_external)
25260         goto bad_stmt;
25261       cp_parser_omp_construct (parser, pragma_tok);
25262       return true;
25263
25264     case PRAGMA_OMP_SECTION:
25265       error_at (pragma_tok->location, 
25266                 "%<#pragma omp section%> may only be used in "
25267                 "%<#pragma omp sections%> construct");
25268       break;
25269
25270     default:
25271       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25272       c_invoke_pragma_handler (id);
25273       break;
25274
25275     bad_stmt:
25276       cp_parser_error (parser, "expected declaration specifiers");
25277       break;
25278     }
25279
25280   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25281   return false;
25282 }
25283
25284 /* The interface the pragma parsers have to the lexer.  */
25285
25286 enum cpp_ttype
25287 pragma_lex (tree *value)
25288 {
25289   cp_token *tok;
25290   enum cpp_ttype ret;
25291
25292   tok = cp_lexer_peek_token (the_parser->lexer);
25293
25294   ret = tok->type;
25295   *value = tok->u.value;
25296
25297   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25298     ret = CPP_EOF;
25299   else if (ret == CPP_STRING)
25300     *value = cp_parser_string_literal (the_parser, false, false);
25301   else
25302     {
25303       cp_lexer_consume_token (the_parser->lexer);
25304       if (ret == CPP_KEYWORD)
25305         ret = CPP_NAME;
25306     }
25307
25308   return ret;
25309 }
25310
25311 \f
25312 /* External interface.  */
25313
25314 /* Parse one entire translation unit.  */
25315
25316 void
25317 c_parse_file (void)
25318 {
25319   static bool already_called = false;
25320
25321   if (already_called)
25322     {
25323       sorry ("inter-module optimizations not implemented for C++");
25324       return;
25325     }
25326   already_called = true;
25327
25328   the_parser = cp_parser_new ();
25329   push_deferring_access_checks (flag_access_control
25330                                 ? dk_no_deferred : dk_no_check);
25331   cp_parser_translation_unit (the_parser);
25332   the_parser = NULL;
25333 }
25334
25335 #include "gt-cp-parser.h"