OSDN Git Service

2011-05-08 Paolo Carlini <paolo.carlini@oracle.com>
[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, 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                       tree exception_specification,
1107                       tree late_return_type)
1108 {
1109   cp_declarator *declarator;
1110
1111   declarator = make_declarator (cdk_function);
1112   declarator->declarator = target;
1113   declarator->u.function.parameters = parms;
1114   declarator->u.function.qualifiers = cv_qualifiers;
1115   declarator->u.function.exception_specification = exception_specification;
1116   declarator->u.function.late_return_type = late_return_type;
1117   if (target)
1118     {
1119       declarator->id_loc = target->id_loc;
1120       declarator->parameter_pack_p = target->parameter_pack_p;
1121       target->parameter_pack_p = false;
1122     }
1123   else
1124     declarator->parameter_pack_p = false;
1125
1126   return declarator;
1127 }
1128
1129 /* Make a declarator for an array of BOUNDS elements, each of which is
1130    defined by ELEMENT.  */
1131
1132 cp_declarator *
1133 make_array_declarator (cp_declarator *element, tree bounds)
1134 {
1135   cp_declarator *declarator;
1136
1137   declarator = make_declarator (cdk_array);
1138   declarator->declarator = element;
1139   declarator->u.array.bounds = bounds;
1140   if (element)
1141     {
1142       declarator->id_loc = element->id_loc;
1143       declarator->parameter_pack_p = element->parameter_pack_p;
1144       element->parameter_pack_p = false;
1145     }
1146   else
1147     declarator->parameter_pack_p = false;
1148
1149   return declarator;
1150 }
1151
1152 /* Determine whether the declarator we've seen so far can be a
1153    parameter pack, when followed by an ellipsis.  */
1154 static bool 
1155 declarator_can_be_parameter_pack (cp_declarator *declarator)
1156 {
1157   /* Search for a declarator name, or any other declarator that goes
1158      after the point where the ellipsis could appear in a parameter
1159      pack. If we find any of these, then this declarator can not be
1160      made into a parameter pack.  */
1161   bool found = false;
1162   while (declarator && !found)
1163     {
1164       switch ((int)declarator->kind)
1165         {
1166         case cdk_id:
1167         case cdk_array:
1168           found = true;
1169           break;
1170
1171         case cdk_error:
1172           return true;
1173
1174         default:
1175           declarator = declarator->declarator;
1176           break;
1177         }
1178     }
1179
1180   return !found;
1181 }
1182
1183 cp_parameter_declarator *no_parameters;
1184
1185 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1186    DECLARATOR and DEFAULT_ARGUMENT.  */
1187
1188 cp_parameter_declarator *
1189 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1190                            cp_declarator *declarator,
1191                            tree default_argument)
1192 {
1193   cp_parameter_declarator *parameter;
1194
1195   parameter = ((cp_parameter_declarator *)
1196                alloc_declarator (sizeof (cp_parameter_declarator)));
1197   parameter->next = NULL;
1198   if (decl_specifiers)
1199     parameter->decl_specifiers = *decl_specifiers;
1200   else
1201     clear_decl_specs (&parameter->decl_specifiers);
1202   parameter->declarator = declarator;
1203   parameter->default_argument = default_argument;
1204   parameter->ellipsis_p = false;
1205
1206   return parameter;
1207 }
1208
1209 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1210
1211 static bool
1212 function_declarator_p (const cp_declarator *declarator)
1213 {
1214   while (declarator)
1215     {
1216       if (declarator->kind == cdk_function
1217           && declarator->declarator->kind == cdk_id)
1218         return true;
1219       if (declarator->kind == cdk_id
1220           || declarator->kind == cdk_error)
1221         return false;
1222       declarator = declarator->declarator;
1223     }
1224   return false;
1225 }
1226  
1227 /* The parser.  */
1228
1229 /* Overview
1230    --------
1231
1232    A cp_parser parses the token stream as specified by the C++
1233    grammar.  Its job is purely parsing, not semantic analysis.  For
1234    example, the parser breaks the token stream into declarators,
1235    expressions, statements, and other similar syntactic constructs.
1236    It does not check that the types of the expressions on either side
1237    of an assignment-statement are compatible, or that a function is
1238    not declared with a parameter of type `void'.
1239
1240    The parser invokes routines elsewhere in the compiler to perform
1241    semantic analysis and to build up the abstract syntax tree for the
1242    code processed.
1243
1244    The parser (and the template instantiation code, which is, in a
1245    way, a close relative of parsing) are the only parts of the
1246    compiler that should be calling push_scope and pop_scope, or
1247    related functions.  The parser (and template instantiation code)
1248    keeps track of what scope is presently active; everything else
1249    should simply honor that.  (The code that generates static
1250    initializers may also need to set the scope, in order to check
1251    access control correctly when emitting the initializers.)
1252
1253    Methodology
1254    -----------
1255
1256    The parser is of the standard recursive-descent variety.  Upcoming
1257    tokens in the token stream are examined in order to determine which
1258    production to use when parsing a non-terminal.  Some C++ constructs
1259    require arbitrary look ahead to disambiguate.  For example, it is
1260    impossible, in the general case, to tell whether a statement is an
1261    expression or declaration without scanning the entire statement.
1262    Therefore, the parser is capable of "parsing tentatively."  When the
1263    parser is not sure what construct comes next, it enters this mode.
1264    Then, while we attempt to parse the construct, the parser queues up
1265    error messages, rather than issuing them immediately, and saves the
1266    tokens it consumes.  If the construct is parsed successfully, the
1267    parser "commits", i.e., it issues any queued error messages and
1268    the tokens that were being preserved are permanently discarded.
1269    If, however, the construct is not parsed successfully, the parser
1270    rolls back its state completely so that it can resume parsing using
1271    a different alternative.
1272
1273    Future Improvements
1274    -------------------
1275
1276    The performance of the parser could probably be improved substantially.
1277    We could often eliminate the need to parse tentatively by looking ahead
1278    a little bit.  In some places, this approach might not entirely eliminate
1279    the need to parse tentatively, but it might still speed up the average
1280    case.  */
1281
1282 /* Flags that are passed to some parsing functions.  These values can
1283    be bitwise-ored together.  */
1284
1285 enum
1286 {
1287   /* No flags.  */
1288   CP_PARSER_FLAGS_NONE = 0x0,
1289   /* The construct is optional.  If it is not present, then no error
1290      should be issued.  */
1291   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1292   /* When parsing a type-specifier, treat user-defined type-names
1293      as non-type identifiers.  */
1294   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1295   /* When parsing a type-specifier, do not try to parse a class-specifier
1296      or enum-specifier.  */
1297   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1298   /* When parsing a decl-specifier-seq, only allow type-specifier or
1299      constexpr.  */
1300   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1301 };
1302
1303 /* This type is used for parameters and variables which hold
1304    combinations of the above flags.  */
1305 typedef int cp_parser_flags;
1306
1307 /* The different kinds of declarators we want to parse.  */
1308
1309 typedef enum cp_parser_declarator_kind
1310 {
1311   /* We want an abstract declarator.  */
1312   CP_PARSER_DECLARATOR_ABSTRACT,
1313   /* We want a named declarator.  */
1314   CP_PARSER_DECLARATOR_NAMED,
1315   /* We don't mind, but the name must be an unqualified-id.  */
1316   CP_PARSER_DECLARATOR_EITHER
1317 } cp_parser_declarator_kind;
1318
1319 /* The precedence values used to parse binary expressions.  The minimum value
1320    of PREC must be 1, because zero is reserved to quickly discriminate
1321    binary operators from other tokens.  */
1322
1323 enum cp_parser_prec
1324 {
1325   PREC_NOT_OPERATOR,
1326   PREC_LOGICAL_OR_EXPRESSION,
1327   PREC_LOGICAL_AND_EXPRESSION,
1328   PREC_INCLUSIVE_OR_EXPRESSION,
1329   PREC_EXCLUSIVE_OR_EXPRESSION,
1330   PREC_AND_EXPRESSION,
1331   PREC_EQUALITY_EXPRESSION,
1332   PREC_RELATIONAL_EXPRESSION,
1333   PREC_SHIFT_EXPRESSION,
1334   PREC_ADDITIVE_EXPRESSION,
1335   PREC_MULTIPLICATIVE_EXPRESSION,
1336   PREC_PM_EXPRESSION,
1337   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1338 };
1339
1340 /* A mapping from a token type to a corresponding tree node type, with a
1341    precedence value.  */
1342
1343 typedef struct cp_parser_binary_operations_map_node
1344 {
1345   /* The token type.  */
1346   enum cpp_ttype token_type;
1347   /* The corresponding tree code.  */
1348   enum tree_code tree_type;
1349   /* The precedence of this operator.  */
1350   enum cp_parser_prec prec;
1351 } cp_parser_binary_operations_map_node;
1352
1353 typedef struct cp_parser_expression_stack_entry
1354 {
1355   /* Left hand side of the binary operation we are currently
1356      parsing.  */
1357   tree lhs;
1358   /* Original tree code for left hand side, if it was a binary
1359      expression itself (used for -Wparentheses).  */
1360   enum tree_code lhs_type;
1361   /* Tree code for the binary operation we are parsing.  */
1362   enum tree_code tree_type;
1363   /* Precedence of the binary operation we are parsing.  */
1364   enum cp_parser_prec prec;
1365 } cp_parser_expression_stack_entry;
1366
1367 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1368    entries because precedence levels on the stack are monotonically
1369    increasing.  */
1370 typedef struct cp_parser_expression_stack_entry
1371   cp_parser_expression_stack[NUM_PREC_VALUES];
1372
1373 /* Prototypes.  */
1374
1375 /* Constructors and destructors.  */
1376
1377 static cp_parser_context *cp_parser_context_new
1378   (cp_parser_context *);
1379
1380 /* Class variables.  */
1381
1382 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1383
1384 /* The operator-precedence table used by cp_parser_binary_expression.
1385    Transformed into an associative array (binops_by_token) by
1386    cp_parser_new.  */
1387
1388 static const cp_parser_binary_operations_map_node binops[] = {
1389   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1390   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1391
1392   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1393   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1394   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395
1396   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1397   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1398
1399   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1400   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1401
1402   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1403   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1404   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1405   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1406
1407   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1408   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1409
1410   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1411
1412   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1413
1414   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1415
1416   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1417
1418   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1419 };
1420
1421 /* The same as binops, but initialized by cp_parser_new so that
1422    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1423    for speed.  */
1424 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1425
1426 /* Constructors and destructors.  */
1427
1428 /* Construct a new context.  The context below this one on the stack
1429    is given by NEXT.  */
1430
1431 static cp_parser_context *
1432 cp_parser_context_new (cp_parser_context* next)
1433 {
1434   cp_parser_context *context;
1435
1436   /* Allocate the storage.  */
1437   if (cp_parser_context_free_list != NULL)
1438     {
1439       /* Pull the first entry from the free list.  */
1440       context = cp_parser_context_free_list;
1441       cp_parser_context_free_list = context->next;
1442       memset (context, 0, sizeof (*context));
1443     }
1444   else
1445     context = ggc_alloc_cleared_cp_parser_context ();
1446
1447   /* No errors have occurred yet in this context.  */
1448   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1449   /* If this is not the bottommost context, copy information that we
1450      need from the previous context.  */
1451   if (next)
1452     {
1453       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1454          expression, then we are parsing one in this context, too.  */
1455       context->object_type = next->object_type;
1456       /* Thread the stack.  */
1457       context->next = next;
1458     }
1459
1460   return context;
1461 }
1462
1463 /* Managing the unparsed function queues.  */
1464
1465 #define unparsed_funs_with_default_args \
1466   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1467 #define unparsed_funs_with_definitions \
1468   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1469
1470 static void
1471 push_unparsed_function_queues (cp_parser *parser)
1472 {
1473   VEC_safe_push (cp_unparsed_functions_entry, gc,
1474                  parser->unparsed_queues, NULL);
1475   unparsed_funs_with_default_args = NULL;
1476   unparsed_funs_with_definitions = make_tree_vector ();
1477 }
1478
1479 static void
1480 pop_unparsed_function_queues (cp_parser *parser)
1481 {
1482   release_tree_vector (unparsed_funs_with_definitions);
1483   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1484 }
1485
1486 /* Prototypes.  */
1487
1488 /* Constructors and destructors.  */
1489
1490 static cp_parser *cp_parser_new
1491   (void);
1492
1493 /* Routines to parse various constructs.
1494
1495    Those that return `tree' will return the error_mark_node (rather
1496    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1497    Sometimes, they will return an ordinary node if error-recovery was
1498    attempted, even though a parse error occurred.  So, to check
1499    whether or not a parse error occurred, you should always use
1500    cp_parser_error_occurred.  If the construct is optional (indicated
1501    either by an `_opt' in the name of the function that does the
1502    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1503    the construct is not present.  */
1504
1505 /* Lexical conventions [gram.lex]  */
1506
1507 static tree cp_parser_identifier
1508   (cp_parser *);
1509 static tree cp_parser_string_literal
1510   (cp_parser *, bool, bool);
1511
1512 /* Basic concepts [gram.basic]  */
1513
1514 static bool cp_parser_translation_unit
1515   (cp_parser *);
1516
1517 /* Expressions [gram.expr]  */
1518
1519 static tree cp_parser_primary_expression
1520   (cp_parser *, bool, bool, bool, cp_id_kind *);
1521 static tree cp_parser_id_expression
1522   (cp_parser *, bool, bool, bool *, bool, bool);
1523 static tree cp_parser_unqualified_id
1524   (cp_parser *, bool, bool, bool, bool);
1525 static tree cp_parser_nested_name_specifier_opt
1526   (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier
1528   (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_qualifying_entity
1530   (cp_parser *, bool, bool, bool, bool, bool);
1531 static tree cp_parser_postfix_expression
1532   (cp_parser *, bool, bool, bool, cp_id_kind *);
1533 static tree cp_parser_postfix_open_square_expression
1534   (cp_parser *, tree, bool);
1535 static tree cp_parser_postfix_dot_deref_expression
1536   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1537 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1538   (cp_parser *, int, bool, bool, bool *);
1539 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1540 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1541 static void cp_parser_pseudo_destructor_name
1542   (cp_parser *, tree *, tree *);
1543 static tree cp_parser_unary_expression
1544   (cp_parser *, bool, bool, cp_id_kind *);
1545 static enum tree_code cp_parser_unary_operator
1546   (cp_token *);
1547 static tree cp_parser_new_expression
1548   (cp_parser *);
1549 static VEC(tree,gc) *cp_parser_new_placement
1550   (cp_parser *);
1551 static tree cp_parser_new_type_id
1552   (cp_parser *, tree *);
1553 static cp_declarator *cp_parser_new_declarator_opt
1554   (cp_parser *);
1555 static cp_declarator *cp_parser_direct_new_declarator
1556   (cp_parser *);
1557 static VEC(tree,gc) *cp_parser_new_initializer
1558   (cp_parser *);
1559 static tree cp_parser_delete_expression
1560   (cp_parser *);
1561 static tree cp_parser_cast_expression
1562   (cp_parser *, bool, bool, cp_id_kind *);
1563 static tree cp_parser_binary_expression
1564   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1565 static tree cp_parser_question_colon_clause
1566   (cp_parser *, tree);
1567 static tree cp_parser_assignment_expression
1568   (cp_parser *, bool, cp_id_kind *);
1569 static enum tree_code cp_parser_assignment_operator_opt
1570   (cp_parser *);
1571 static tree cp_parser_expression
1572   (cp_parser *, bool, cp_id_kind *);
1573 static tree cp_parser_constant_expression
1574   (cp_parser *, bool, bool *);
1575 static tree cp_parser_builtin_offsetof
1576   (cp_parser *);
1577 static tree cp_parser_lambda_expression
1578   (cp_parser *);
1579 static void cp_parser_lambda_introducer
1580   (cp_parser *, tree);
1581 static void cp_parser_lambda_declarator_opt
1582   (cp_parser *, tree);
1583 static void cp_parser_lambda_body
1584   (cp_parser *, tree);
1585
1586 /* Statements [gram.stmt.stmt]  */
1587
1588 static void cp_parser_statement
1589   (cp_parser *, tree, bool, bool *);
1590 static void cp_parser_label_for_labeled_statement
1591   (cp_parser *);
1592 static tree cp_parser_expression_statement
1593   (cp_parser *, tree);
1594 static tree cp_parser_compound_statement
1595   (cp_parser *, tree, bool, bool);
1596 static void cp_parser_statement_seq_opt
1597   (cp_parser *, tree);
1598 static tree cp_parser_selection_statement
1599   (cp_parser *, bool *);
1600 static tree cp_parser_condition
1601   (cp_parser *);
1602 static tree cp_parser_iteration_statement
1603   (cp_parser *);
1604 static bool cp_parser_for_init_statement
1605   (cp_parser *, tree *decl);
1606 static tree cp_parser_for
1607   (cp_parser *);
1608 static tree cp_parser_c_for
1609   (cp_parser *, tree, tree);
1610 static tree cp_parser_range_for
1611   (cp_parser *, tree, tree, tree);
1612 static tree cp_parser_perform_range_for_lookup
1613   (tree, tree *, tree *);
1614 static tree cp_parser_range_for_member_function
1615   (tree, tree);
1616 static tree cp_parser_jump_statement
1617   (cp_parser *);
1618 static void cp_parser_declaration_statement
1619   (cp_parser *);
1620
1621 static tree cp_parser_implicitly_scoped_statement
1622   (cp_parser *, bool *);
1623 static void cp_parser_already_scoped_statement
1624   (cp_parser *);
1625
1626 /* Declarations [gram.dcl.dcl] */
1627
1628 static void cp_parser_declaration_seq_opt
1629   (cp_parser *);
1630 static void cp_parser_declaration
1631   (cp_parser *);
1632 static void cp_parser_block_declaration
1633   (cp_parser *, bool);
1634 static void cp_parser_simple_declaration
1635   (cp_parser *, bool, tree *);
1636 static void cp_parser_decl_specifier_seq
1637   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1639   (cp_parser *);
1640 static tree cp_parser_function_specifier_opt
1641   (cp_parser *, cp_decl_specifier_seq *);
1642 static tree cp_parser_type_specifier
1643   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1644    int *, bool *);
1645 static tree cp_parser_simple_type_specifier
1646   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1647 static tree cp_parser_type_name
1648   (cp_parser *);
1649 static tree cp_parser_nonclass_name 
1650   (cp_parser* parser);
1651 static tree cp_parser_elaborated_type_specifier
1652   (cp_parser *, bool, bool);
1653 static tree cp_parser_enum_specifier
1654   (cp_parser *);
1655 static void cp_parser_enumerator_list
1656   (cp_parser *, tree);
1657 static void cp_parser_enumerator_definition
1658   (cp_parser *, tree);
1659 static tree cp_parser_namespace_name
1660   (cp_parser *);
1661 static void cp_parser_namespace_definition
1662   (cp_parser *);
1663 static void cp_parser_namespace_body
1664   (cp_parser *);
1665 static tree cp_parser_qualified_namespace_specifier
1666   (cp_parser *);
1667 static void cp_parser_namespace_alias_definition
1668   (cp_parser *);
1669 static bool cp_parser_using_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_using_directive
1672   (cp_parser *);
1673 static void cp_parser_asm_definition
1674   (cp_parser *);
1675 static void cp_parser_linkage_specification
1676   (cp_parser *);
1677 static void cp_parser_static_assert
1678   (cp_parser *, bool);
1679 static tree cp_parser_decltype
1680   (cp_parser *);
1681
1682 /* Declarators [gram.dcl.decl] */
1683
1684 static tree cp_parser_init_declarator
1685   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1686 static cp_declarator *cp_parser_declarator
1687   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1688 static cp_declarator *cp_parser_direct_declarator
1689   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1690 static enum tree_code cp_parser_ptr_operator
1691   (cp_parser *, tree *, cp_cv_quals *);
1692 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1693   (cp_parser *);
1694 static tree cp_parser_late_return_type_opt
1695   (cp_parser *);
1696 static tree cp_parser_declarator_id
1697   (cp_parser *, bool);
1698 static tree cp_parser_type_id
1699   (cp_parser *);
1700 static tree cp_parser_template_type_arg
1701   (cp_parser *);
1702 static tree cp_parser_trailing_type_id (cp_parser *);
1703 static tree cp_parser_type_id_1
1704   (cp_parser *, bool, bool);
1705 static void cp_parser_type_specifier_seq
1706   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1707 static tree cp_parser_parameter_declaration_clause
1708   (cp_parser *);
1709 static tree cp_parser_parameter_declaration_list
1710   (cp_parser *, bool *);
1711 static cp_parameter_declarator *cp_parser_parameter_declaration
1712   (cp_parser *, bool, bool *);
1713 static tree cp_parser_default_argument 
1714   (cp_parser *, bool);
1715 static void cp_parser_function_body
1716   (cp_parser *);
1717 static tree cp_parser_initializer
1718   (cp_parser *, bool *, bool *);
1719 static tree cp_parser_initializer_clause
1720   (cp_parser *, bool *);
1721 static tree cp_parser_braced_list
1722   (cp_parser*, bool*);
1723 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1724   (cp_parser *, bool *);
1725
1726 static bool cp_parser_ctor_initializer_opt_and_function_body
1727   (cp_parser *);
1728
1729 /* Classes [gram.class] */
1730
1731 static tree cp_parser_class_name
1732   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1733 static tree cp_parser_class_specifier
1734   (cp_parser *);
1735 static tree cp_parser_class_head
1736   (cp_parser *, bool *, tree *, tree *);
1737 static enum tag_types cp_parser_class_key
1738   (cp_parser *);
1739 static void cp_parser_member_specification_opt
1740   (cp_parser *);
1741 static void cp_parser_member_declaration
1742   (cp_parser *);
1743 static tree cp_parser_pure_specifier
1744   (cp_parser *);
1745 static tree cp_parser_constant_initializer
1746   (cp_parser *);
1747
1748 /* Derived classes [gram.class.derived] */
1749
1750 static tree cp_parser_base_clause
1751   (cp_parser *);
1752 static tree cp_parser_base_specifier
1753   (cp_parser *);
1754
1755 /* Special member functions [gram.special] */
1756
1757 static tree cp_parser_conversion_function_id
1758   (cp_parser *);
1759 static tree cp_parser_conversion_type_id
1760   (cp_parser *);
1761 static cp_declarator *cp_parser_conversion_declarator_opt
1762   (cp_parser *);
1763 static bool cp_parser_ctor_initializer_opt
1764   (cp_parser *);
1765 static void cp_parser_mem_initializer_list
1766   (cp_parser *);
1767 static tree cp_parser_mem_initializer
1768   (cp_parser *);
1769 static tree cp_parser_mem_initializer_id
1770   (cp_parser *);
1771
1772 /* Overloading [gram.over] */
1773
1774 static tree cp_parser_operator_function_id
1775   (cp_parser *);
1776 static tree cp_parser_operator
1777   (cp_parser *);
1778
1779 /* Templates [gram.temp] */
1780
1781 static void cp_parser_template_declaration
1782   (cp_parser *, bool);
1783 static tree cp_parser_template_parameter_list
1784   (cp_parser *);
1785 static tree cp_parser_template_parameter
1786   (cp_parser *, bool *, bool *);
1787 static tree cp_parser_type_parameter
1788   (cp_parser *, bool *);
1789 static tree cp_parser_template_id
1790   (cp_parser *, bool, bool, bool);
1791 static tree cp_parser_template_name
1792   (cp_parser *, bool, bool, bool, bool *);
1793 static tree cp_parser_template_argument_list
1794   (cp_parser *);
1795 static tree cp_parser_template_argument
1796   (cp_parser *);
1797 static void cp_parser_explicit_instantiation
1798   (cp_parser *);
1799 static void cp_parser_explicit_specialization
1800   (cp_parser *);
1801
1802 /* Exception handling [gram.exception] */
1803
1804 static tree cp_parser_try_block
1805   (cp_parser *);
1806 static bool cp_parser_function_try_block
1807   (cp_parser *);
1808 static void cp_parser_handler_seq
1809   (cp_parser *);
1810 static void cp_parser_handler
1811   (cp_parser *);
1812 static tree cp_parser_exception_declaration
1813   (cp_parser *);
1814 static tree cp_parser_throw_expression
1815   (cp_parser *);
1816 static tree cp_parser_exception_specification_opt
1817   (cp_parser *);
1818 static tree cp_parser_type_id_list
1819   (cp_parser *);
1820
1821 /* GNU Extensions */
1822
1823 static tree cp_parser_asm_specification_opt
1824   (cp_parser *);
1825 static tree cp_parser_asm_operand_list
1826   (cp_parser *);
1827 static tree cp_parser_asm_clobber_list
1828   (cp_parser *);
1829 static tree cp_parser_asm_label_list
1830   (cp_parser *);
1831 static tree cp_parser_attributes_opt
1832   (cp_parser *);
1833 static tree cp_parser_attribute_list
1834   (cp_parser *);
1835 static bool cp_parser_extension_opt
1836   (cp_parser *, int *);
1837 static void cp_parser_label_declaration
1838   (cp_parser *);
1839
1840 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1841 static bool cp_parser_pragma
1842   (cp_parser *, enum pragma_context);
1843
1844 /* Objective-C++ Productions */
1845
1846 static tree cp_parser_objc_message_receiver
1847   (cp_parser *);
1848 static tree cp_parser_objc_message_args
1849   (cp_parser *);
1850 static tree cp_parser_objc_message_expression
1851   (cp_parser *);
1852 static tree cp_parser_objc_encode_expression
1853   (cp_parser *);
1854 static tree cp_parser_objc_defs_expression
1855   (cp_parser *);
1856 static tree cp_parser_objc_protocol_expression
1857   (cp_parser *);
1858 static tree cp_parser_objc_selector_expression
1859   (cp_parser *);
1860 static tree cp_parser_objc_expression
1861   (cp_parser *);
1862 static bool cp_parser_objc_selector_p
1863   (enum cpp_ttype);
1864 static tree cp_parser_objc_selector
1865   (cp_parser *);
1866 static tree cp_parser_objc_protocol_refs_opt
1867   (cp_parser *);
1868 static void cp_parser_objc_declaration
1869   (cp_parser *, tree);
1870 static tree cp_parser_objc_statement
1871   (cp_parser *);
1872 static bool cp_parser_objc_valid_prefix_attributes
1873   (cp_parser *, tree *);
1874 static void cp_parser_objc_at_property_declaration 
1875   (cp_parser *) ;
1876 static void cp_parser_objc_at_synthesize_declaration 
1877   (cp_parser *) ;
1878 static void cp_parser_objc_at_dynamic_declaration
1879   (cp_parser *) ;
1880 static tree cp_parser_objc_struct_declaration
1881   (cp_parser *) ;
1882
1883 /* Utility Routines */
1884
1885 static tree cp_parser_lookup_name
1886   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1887 static tree cp_parser_lookup_name_simple
1888   (cp_parser *, tree, location_t);
1889 static tree cp_parser_maybe_treat_template_as_class
1890   (tree, bool);
1891 static bool cp_parser_check_declarator_template_parameters
1892   (cp_parser *, cp_declarator *, location_t);
1893 static bool cp_parser_check_template_parameters
1894   (cp_parser *, unsigned, location_t, cp_declarator *);
1895 static tree cp_parser_simple_cast_expression
1896   (cp_parser *);
1897 static tree cp_parser_global_scope_opt
1898   (cp_parser *, bool);
1899 static bool cp_parser_constructor_declarator_p
1900   (cp_parser *, bool);
1901 static tree cp_parser_function_definition_from_specifiers_and_declarator
1902   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1903 static tree cp_parser_function_definition_after_declarator
1904   (cp_parser *, bool);
1905 static void cp_parser_template_declaration_after_export
1906   (cp_parser *, bool);
1907 static void cp_parser_perform_template_parameter_access_checks
1908   (VEC (deferred_access_check,gc)*);
1909 static tree cp_parser_single_declaration
1910   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1911 static tree cp_parser_functional_cast
1912   (cp_parser *, tree);
1913 static tree cp_parser_save_member_function_body
1914   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1915 static tree cp_parser_enclosed_template_argument_list
1916   (cp_parser *);
1917 static void cp_parser_save_default_args
1918   (cp_parser *, tree);
1919 static void cp_parser_late_parsing_for_member
1920   (cp_parser *, tree);
1921 static void cp_parser_late_parsing_default_args
1922   (cp_parser *, tree);
1923 static tree cp_parser_sizeof_operand
1924   (cp_parser *, enum rid);
1925 static tree cp_parser_trait_expr
1926   (cp_parser *, enum rid);
1927 static bool cp_parser_declares_only_class_p
1928   (cp_parser *);
1929 static void cp_parser_set_storage_class
1930   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1931 static void cp_parser_set_decl_spec_type
1932   (cp_decl_specifier_seq *, tree, location_t, bool);
1933 static bool cp_parser_friend_p
1934   (const cp_decl_specifier_seq *);
1935 static void cp_parser_required_error
1936   (cp_parser *, required_token, bool);
1937 static cp_token *cp_parser_require
1938   (cp_parser *, enum cpp_ttype, required_token);
1939 static cp_token *cp_parser_require_keyword
1940   (cp_parser *, enum rid, required_token);
1941 static bool cp_parser_token_starts_function_definition_p
1942   (cp_token *);
1943 static bool cp_parser_next_token_starts_class_definition_p
1944   (cp_parser *);
1945 static bool cp_parser_next_token_ends_template_argument_p
1946   (cp_parser *);
1947 static bool cp_parser_nth_token_starts_template_argument_list_p
1948   (cp_parser *, size_t);
1949 static enum tag_types cp_parser_token_is_class_key
1950   (cp_token *);
1951 static void cp_parser_check_class_key
1952   (enum tag_types, tree type);
1953 static void cp_parser_check_access_in_redeclaration
1954   (tree type, location_t location);
1955 static bool cp_parser_optional_template_keyword
1956   (cp_parser *);
1957 static void cp_parser_pre_parsed_nested_name_specifier
1958   (cp_parser *);
1959 static bool cp_parser_cache_group
1960   (cp_parser *, enum cpp_ttype, unsigned);
1961 static void cp_parser_parse_tentatively
1962   (cp_parser *);
1963 static void cp_parser_commit_to_tentative_parse
1964   (cp_parser *);
1965 static void cp_parser_abort_tentative_parse
1966   (cp_parser *);
1967 static bool cp_parser_parse_definitely
1968   (cp_parser *);
1969 static inline bool cp_parser_parsing_tentatively
1970   (cp_parser *);
1971 static bool cp_parser_uncommitted_to_tentative_parse_p
1972   (cp_parser *);
1973 static void cp_parser_error
1974   (cp_parser *, const char *);
1975 static void cp_parser_name_lookup_error
1976   (cp_parser *, tree, tree, name_lookup_error, location_t);
1977 static bool cp_parser_simulate_error
1978   (cp_parser *);
1979 static bool cp_parser_check_type_definition
1980   (cp_parser *);
1981 static void cp_parser_check_for_definition_in_return_type
1982   (cp_declarator *, tree, location_t type_location);
1983 static void cp_parser_check_for_invalid_template_id
1984   (cp_parser *, tree, location_t location);
1985 static bool cp_parser_non_integral_constant_expression
1986   (cp_parser *, non_integral_constant);
1987 static void cp_parser_diagnose_invalid_type_name
1988   (cp_parser *, tree, tree, location_t);
1989 static bool cp_parser_parse_and_diagnose_invalid_type_name
1990   (cp_parser *);
1991 static int cp_parser_skip_to_closing_parenthesis
1992   (cp_parser *, bool, bool, bool);
1993 static void cp_parser_skip_to_end_of_statement
1994   (cp_parser *);
1995 static void cp_parser_consume_semicolon_at_end_of_statement
1996   (cp_parser *);
1997 static void cp_parser_skip_to_end_of_block_or_statement
1998   (cp_parser *);
1999 static bool cp_parser_skip_to_closing_brace
2000   (cp_parser *);
2001 static void cp_parser_skip_to_end_of_template_parameter_list
2002   (cp_parser *);
2003 static void cp_parser_skip_to_pragma_eol
2004   (cp_parser*, cp_token *);
2005 static bool cp_parser_error_occurred
2006   (cp_parser *);
2007 static bool cp_parser_allow_gnu_extensions_p
2008   (cp_parser *);
2009 static bool cp_parser_is_string_literal
2010   (cp_token *);
2011 static bool cp_parser_is_keyword
2012   (cp_token *, enum rid);
2013 static tree cp_parser_make_typename_type
2014   (cp_parser *, tree, tree, location_t location);
2015 static cp_declarator * cp_parser_make_indirect_declarator
2016   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2017
2018 /* Returns nonzero if we are parsing tentatively.  */
2019
2020 static inline bool
2021 cp_parser_parsing_tentatively (cp_parser* parser)
2022 {
2023   return parser->context->next != NULL;
2024 }
2025
2026 /* Returns nonzero if TOKEN is a string literal.  */
2027
2028 static bool
2029 cp_parser_is_string_literal (cp_token* token)
2030 {
2031   return (token->type == CPP_STRING ||
2032           token->type == CPP_STRING16 ||
2033           token->type == CPP_STRING32 ||
2034           token->type == CPP_WSTRING ||
2035           token->type == CPP_UTF8STRING);
2036 }
2037
2038 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2039
2040 static bool
2041 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2042 {
2043   return token->keyword == keyword;
2044 }
2045
2046 /* If not parsing tentatively, issue a diagnostic of the form
2047       FILE:LINE: MESSAGE before TOKEN
2048    where TOKEN is the next token in the input stream.  MESSAGE
2049    (specified by the caller) is usually of the form "expected
2050    OTHER-TOKEN".  */
2051
2052 static void
2053 cp_parser_error (cp_parser* parser, const char* gmsgid)
2054 {
2055   if (!cp_parser_simulate_error (parser))
2056     {
2057       cp_token *token = cp_lexer_peek_token (parser->lexer);
2058       /* This diagnostic makes more sense if it is tagged to the line
2059          of the token we just peeked at.  */
2060       cp_lexer_set_source_position_from_token (token);
2061
2062       if (token->type == CPP_PRAGMA)
2063         {
2064           error_at (token->location,
2065                     "%<#pragma%> is not allowed here");
2066           cp_parser_skip_to_pragma_eol (parser, token);
2067           return;
2068         }
2069
2070       c_parse_error (gmsgid,
2071                      /* Because c_parser_error does not understand
2072                         CPP_KEYWORD, keywords are treated like
2073                         identifiers.  */
2074                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2075                      token->u.value, token->flags);
2076     }
2077 }
2078
2079 /* Issue an error about name-lookup failing.  NAME is the
2080    IDENTIFIER_NODE DECL is the result of
2081    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2082    the thing that we hoped to find.  */
2083
2084 static void
2085 cp_parser_name_lookup_error (cp_parser* parser,
2086                              tree name,
2087                              tree decl,
2088                              name_lookup_error desired,
2089                              location_t location)
2090 {
2091   /* If name lookup completely failed, tell the user that NAME was not
2092      declared.  */
2093   if (decl == error_mark_node)
2094     {
2095       if (parser->scope && parser->scope != global_namespace)
2096         error_at (location, "%<%E::%E%> has not been declared",
2097                   parser->scope, name);
2098       else if (parser->scope == global_namespace)
2099         error_at (location, "%<::%E%> has not been declared", name);
2100       else if (parser->object_scope
2101                && !CLASS_TYPE_P (parser->object_scope))
2102         error_at (location, "request for member %qE in non-class type %qT",
2103                   name, parser->object_scope);
2104       else if (parser->object_scope)
2105         error_at (location, "%<%T::%E%> has not been declared",
2106                   parser->object_scope, name);
2107       else
2108         error_at (location, "%qE has not been declared", name);
2109     }
2110   else if (parser->scope && parser->scope != global_namespace)
2111     {
2112       switch (desired)
2113         {
2114           case NLE_TYPE:
2115             error_at (location, "%<%E::%E%> is not a type",
2116                                 parser->scope, name);
2117             break;
2118           case NLE_CXX98:
2119             error_at (location, "%<%E::%E%> is not a class or namespace",
2120                                 parser->scope, name);
2121             break;
2122           case NLE_NOT_CXX98:
2123             error_at (location,
2124                       "%<%E::%E%> is not a class, namespace, or enumeration",
2125                       parser->scope, name);
2126             break;
2127           default:
2128             gcc_unreachable ();
2129             
2130         }
2131     }
2132   else if (parser->scope == global_namespace)
2133     {
2134       switch (desired)
2135         {
2136           case NLE_TYPE:
2137             error_at (location, "%<::%E%> is not a type", name);
2138             break;
2139           case NLE_CXX98:
2140             error_at (location, "%<::%E%> is not a class or namespace", name);
2141             break;
2142           case NLE_NOT_CXX98:
2143             error_at (location,
2144                       "%<::%E%> is not a class, namespace, or enumeration",
2145                       name);
2146             break;
2147           default:
2148             gcc_unreachable ();
2149         }
2150     }
2151   else
2152     {
2153       switch (desired)
2154         {
2155           case NLE_TYPE:
2156             error_at (location, "%qE is not a type", name);
2157             break;
2158           case NLE_CXX98:
2159             error_at (location, "%qE is not a class or namespace", name);
2160             break;
2161           case NLE_NOT_CXX98:
2162             error_at (location,
2163                       "%qE is not a class, namespace, or enumeration", name);
2164             break;
2165           default:
2166             gcc_unreachable ();
2167         }
2168     }
2169 }
2170
2171 /* If we are parsing tentatively, remember that an error has occurred
2172    during this tentative parse.  Returns true if the error was
2173    simulated; false if a message should be issued by the caller.  */
2174
2175 static bool
2176 cp_parser_simulate_error (cp_parser* parser)
2177 {
2178   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2179     {
2180       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2181       return true;
2182     }
2183   return false;
2184 }
2185
2186 /* Check for repeated decl-specifiers.  */
2187
2188 static void
2189 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2190                            location_t location)
2191 {
2192   int ds;
2193
2194   for (ds = ds_first; ds != ds_last; ++ds)
2195     {
2196       unsigned count = decl_specs->specs[ds];
2197       if (count < 2)
2198         continue;
2199       /* The "long" specifier is a special case because of "long long".  */
2200       if (ds == ds_long)
2201         {
2202           if (count > 2)
2203             error_at (location, "%<long long long%> is too long for GCC");
2204           else 
2205             pedwarn_cxx98 (location, OPT_Wlong_long, 
2206                            "ISO C++ 1998 does not support %<long long%>");
2207         }
2208       else if (count > 1)
2209         {
2210           static const char *const decl_spec_names[] = {
2211             "signed",
2212             "unsigned",
2213             "short",
2214             "long",
2215             "const",
2216             "volatile",
2217             "restrict",
2218             "inline",
2219             "virtual",
2220             "explicit",
2221             "friend",
2222             "typedef",
2223             "constexpr",
2224             "__complex",
2225             "__thread"
2226           };
2227           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2228         }
2229     }
2230 }
2231
2232 /* This function is called when a type is defined.  If type
2233    definitions are forbidden at this point, an error message is
2234    issued.  */
2235
2236 static bool
2237 cp_parser_check_type_definition (cp_parser* parser)
2238 {
2239   /* If types are forbidden here, issue a message.  */
2240   if (parser->type_definition_forbidden_message)
2241     {
2242       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2243          in the message need to be interpreted.  */
2244       error (parser->type_definition_forbidden_message);
2245       return false;
2246     }
2247   return true;
2248 }
2249
2250 /* This function is called when the DECLARATOR is processed.  The TYPE
2251    was a type defined in the decl-specifiers.  If it is invalid to
2252    define a type in the decl-specifiers for DECLARATOR, an error is
2253    issued. TYPE_LOCATION is the location of TYPE and is used
2254    for error reporting.  */
2255
2256 static void
2257 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2258                                                tree type, location_t type_location)
2259 {
2260   /* [dcl.fct] forbids type definitions in return types.
2261      Unfortunately, it's not easy to know whether or not we are
2262      processing a return type until after the fact.  */
2263   while (declarator
2264          && (declarator->kind == cdk_pointer
2265              || declarator->kind == cdk_reference
2266              || declarator->kind == cdk_ptrmem))
2267     declarator = declarator->declarator;
2268   if (declarator
2269       && declarator->kind == cdk_function)
2270     {
2271       error_at (type_location,
2272                 "new types may not be defined in a return type");
2273       inform (type_location, 
2274               "(perhaps a semicolon is missing after the definition of %qT)",
2275               type);
2276     }
2277 }
2278
2279 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2280    "<" in any valid C++ program.  If the next token is indeed "<",
2281    issue a message warning the user about what appears to be an
2282    invalid attempt to form a template-id. LOCATION is the location
2283    of the type-specifier (TYPE) */
2284
2285 static void
2286 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2287                                          tree type, location_t location)
2288 {
2289   cp_token_position start = 0;
2290
2291   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2292     {
2293       if (TYPE_P (type))
2294         error_at (location, "%qT is not a template", type);
2295       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2296         error_at (location, "%qE is not a template", type);
2297       else
2298         error_at (location, "invalid template-id");
2299       /* Remember the location of the invalid "<".  */
2300       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2301         start = cp_lexer_token_position (parser->lexer, true);
2302       /* Consume the "<".  */
2303       cp_lexer_consume_token (parser->lexer);
2304       /* Parse the template arguments.  */
2305       cp_parser_enclosed_template_argument_list (parser);
2306       /* Permanently remove the invalid template arguments so that
2307          this error message is not issued again.  */
2308       if (start)
2309         cp_lexer_purge_tokens_after (parser->lexer, start);
2310     }
2311 }
2312
2313 /* If parsing an integral constant-expression, issue an error message
2314    about the fact that THING appeared and return true.  Otherwise,
2315    return false.  In either case, set
2316    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2317
2318 static bool
2319 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2320                                             non_integral_constant thing)
2321 {
2322   parser->non_integral_constant_expression_p = true;
2323   if (parser->integral_constant_expression_p)
2324     {
2325       if (!parser->allow_non_integral_constant_expression_p)
2326         {
2327           const char *msg = NULL;
2328           switch (thing)
2329             {
2330               case NIC_FLOAT:
2331                 error ("floating-point literal "
2332                        "cannot appear in a constant-expression");
2333                 return true;
2334               case NIC_CAST:
2335                 error ("a cast to a type other than an integral or "
2336                        "enumeration type cannot appear in a "
2337                        "constant-expression");
2338                 return true;
2339               case NIC_TYPEID:
2340                 error ("%<typeid%> operator "
2341                        "cannot appear in a constant-expression");
2342                 return true;
2343               case NIC_NCC:
2344                 error ("non-constant compound literals "
2345                        "cannot appear in a constant-expression");
2346                 return true;
2347               case NIC_FUNC_CALL:
2348                 error ("a function call "
2349                        "cannot appear in a constant-expression");
2350                 return true;
2351               case NIC_INC:
2352                 error ("an increment "
2353                        "cannot appear in a constant-expression");
2354                 return true;
2355               case NIC_DEC:
2356                 error ("an decrement "
2357                        "cannot appear in a constant-expression");
2358                 return true;
2359               case NIC_ARRAY_REF:
2360                 error ("an array reference "
2361                        "cannot appear in a constant-expression");
2362                 return true;
2363               case NIC_ADDR_LABEL:
2364                 error ("the address of a label "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_OVERLOADED:
2368                 error ("calls to overloaded operators "
2369                        "cannot appear in a constant-expression");
2370                 return true;
2371               case NIC_ASSIGNMENT:
2372                 error ("an assignment cannot appear in a constant-expression");
2373                 return true;
2374               case NIC_COMMA:
2375                 error ("a comma operator "
2376                        "cannot appear in a constant-expression");
2377                 return true;
2378               case NIC_CONSTRUCTOR:
2379                 error ("a call to a constructor "
2380                        "cannot appear in a constant-expression");
2381                 return true;
2382               case NIC_THIS:
2383                 msg = "this";
2384                 break;
2385               case NIC_FUNC_NAME:
2386                 msg = "__FUNCTION__";
2387                 break;
2388               case NIC_PRETTY_FUNC:
2389                 msg = "__PRETTY_FUNCTION__";
2390                 break;
2391               case NIC_C99_FUNC:
2392                 msg = "__func__";
2393                 break;
2394               case NIC_VA_ARG:
2395                 msg = "va_arg";
2396                 break;
2397               case NIC_ARROW:
2398                 msg = "->";
2399                 break;
2400               case NIC_POINT:
2401                 msg = ".";
2402                 break;
2403               case NIC_STAR:
2404                 msg = "*";
2405                 break;
2406               case NIC_ADDR:
2407                 msg = "&";
2408                 break;
2409               case NIC_PREINCREMENT:
2410                 msg = "++";
2411                 break;
2412               case NIC_PREDECREMENT:
2413                 msg = "--";
2414                 break;
2415               case NIC_NEW:
2416                 msg = "new";
2417                 break;
2418               case NIC_DEL:
2419                 msg = "delete";
2420                 break;
2421               default:
2422                 gcc_unreachable ();
2423             }
2424           if (msg)
2425             error ("%qs cannot appear in a constant-expression", msg);
2426           return true;
2427         }
2428     }
2429   return false;
2430 }
2431
2432 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2433    qualifying scope (or NULL, if none) for ID.  This function commits
2434    to the current active tentative parse, if any.  (Otherwise, the
2435    problematic construct might be encountered again later, resulting
2436    in duplicate error messages.) LOCATION is the location of ID.  */
2437
2438 static void
2439 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2440                                       tree scope, tree id,
2441                                       location_t location)
2442 {
2443   tree decl, old_scope;
2444   cp_parser_commit_to_tentative_parse (parser);
2445   /* Try to lookup the identifier.  */
2446   old_scope = parser->scope;
2447   parser->scope = scope;
2448   decl = cp_parser_lookup_name_simple (parser, id, location);
2449   parser->scope = old_scope;
2450   /* If the lookup found a template-name, it means that the user forgot
2451   to specify an argument list. Emit a useful error message.  */
2452   if (TREE_CODE (decl) == TEMPLATE_DECL)
2453     error_at (location,
2454               "invalid use of template-name %qE without an argument list",
2455               decl);
2456   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2457     error_at (location, "invalid use of destructor %qD as a type", id);
2458   else if (TREE_CODE (decl) == TYPE_DECL)
2459     /* Something like 'unsigned A a;'  */
2460     error_at (location, "invalid combination of multiple type-specifiers");
2461   else if (!parser->scope)
2462     {
2463       /* Issue an error message.  */
2464       error_at (location, "%qE does not name a type", id);
2465       /* If we're in a template class, it's possible that the user was
2466          referring to a type from a base class.  For example:
2467
2468            template <typename T> struct A { typedef T X; };
2469            template <typename T> struct B : public A<T> { X x; };
2470
2471          The user should have said "typename A<T>::X".  */
2472       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2473         inform (location, "C++0x %<constexpr%> only available with "
2474                 "-std=c++0x or -std=gnu++0x");
2475       else if (processing_template_decl && current_class_type
2476                && TYPE_BINFO (current_class_type))
2477         {
2478           tree b;
2479
2480           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2481                b;
2482                b = TREE_CHAIN (b))
2483             {
2484               tree base_type = BINFO_TYPE (b);
2485               if (CLASS_TYPE_P (base_type)
2486                   && dependent_type_p (base_type))
2487                 {
2488                   tree field;
2489                   /* Go from a particular instantiation of the
2490                      template (which will have an empty TYPE_FIELDs),
2491                      to the main version.  */
2492                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2493                   for (field = TYPE_FIELDS (base_type);
2494                        field;
2495                        field = DECL_CHAIN (field))
2496                     if (TREE_CODE (field) == TYPE_DECL
2497                         && DECL_NAME (field) == id)
2498                       {
2499                         inform (location, 
2500                                 "(perhaps %<typename %T::%E%> was intended)",
2501                                 BINFO_TYPE (b), id);
2502                         break;
2503                       }
2504                   if (field)
2505                     break;
2506                 }
2507             }
2508         }
2509     }
2510   /* Here we diagnose qualified-ids where the scope is actually correct,
2511      but the identifier does not resolve to a valid type name.  */
2512   else if (parser->scope != error_mark_node)
2513     {
2514       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2515         error_at (location, "%qE in namespace %qE does not name a type",
2516                   id, parser->scope);
2517       else if (CLASS_TYPE_P (parser->scope)
2518                && constructor_name_p (id, parser->scope))
2519         {
2520           /* A<T>::A<T>() */
2521           error_at (location, "%<%T::%E%> names the constructor, not"
2522                     " the type", parser->scope, id);
2523           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2524             error_at (location, "and %qT has no template constructors",
2525                       parser->scope);
2526         }
2527       else if (TYPE_P (parser->scope)
2528                && dependent_scope_p (parser->scope))
2529         error_at (location, "need %<typename%> before %<%T::%E%> because "
2530                   "%qT is a dependent scope",
2531                   parser->scope, id, parser->scope);
2532       else if (TYPE_P (parser->scope))
2533         error_at (location, "%qE in class %qT does not name a type",
2534                   id, parser->scope);
2535       else
2536         gcc_unreachable ();
2537     }
2538 }
2539
2540 /* Check for a common situation where a type-name should be present,
2541    but is not, and issue a sensible error message.  Returns true if an
2542    invalid type-name was detected.
2543
2544    The situation handled by this function are variable declarations of the
2545    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2546    Usually, `ID' should name a type, but if we got here it means that it
2547    does not. We try to emit the best possible error message depending on
2548    how exactly the id-expression looks like.  */
2549
2550 static bool
2551 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2552 {
2553   tree id;
2554   cp_token *token = cp_lexer_peek_token (parser->lexer);
2555
2556   /* Avoid duplicate error about ambiguous lookup.  */
2557   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2558     {
2559       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2560       if (next->type == CPP_NAME && next->ambiguous_p)
2561         goto out;
2562     }
2563
2564   cp_parser_parse_tentatively (parser);
2565   id = cp_parser_id_expression (parser,
2566                                 /*template_keyword_p=*/false,
2567                                 /*check_dependency_p=*/true,
2568                                 /*template_p=*/NULL,
2569                                 /*declarator_p=*/true,
2570                                 /*optional_p=*/false);
2571   /* If the next token is a (, this is a function with no explicit return
2572      type, i.e. constructor, destructor or conversion op.  */
2573   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2574       || TREE_CODE (id) == TYPE_DECL)
2575     {
2576       cp_parser_abort_tentative_parse (parser);
2577       return false;
2578     }
2579   if (!cp_parser_parse_definitely (parser))
2580     return false;
2581
2582   /* Emit a diagnostic for the invalid type.  */
2583   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2584                                         id, token->location);
2585  out:
2586   /* If we aren't in the middle of a declarator (i.e. in a
2587      parameter-declaration-clause), skip to the end of the declaration;
2588      there's no point in trying to process it.  */
2589   if (!parser->in_declarator_p)
2590     cp_parser_skip_to_end_of_block_or_statement (parser);
2591   return true;
2592 }
2593
2594 /* Consume tokens up to, and including, the next non-nested closing `)'.
2595    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2596    are doing error recovery. Returns -1 if OR_COMMA is true and we
2597    found an unnested comma.  */
2598
2599 static int
2600 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2601                                        bool recovering,
2602                                        bool or_comma,
2603                                        bool consume_paren)
2604 {
2605   unsigned paren_depth = 0;
2606   unsigned brace_depth = 0;
2607   unsigned square_depth = 0;
2608
2609   if (recovering && !or_comma
2610       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2611     return 0;
2612
2613   while (true)
2614     {
2615       cp_token * token = cp_lexer_peek_token (parser->lexer);
2616
2617       switch (token->type)
2618         {
2619         case CPP_EOF:
2620         case CPP_PRAGMA_EOL:
2621           /* If we've run out of tokens, then there is no closing `)'.  */
2622           return 0;
2623
2624         /* This is good for lambda expression capture-lists.  */
2625         case CPP_OPEN_SQUARE:
2626           ++square_depth;
2627           break;
2628         case CPP_CLOSE_SQUARE:
2629           if (!square_depth--)
2630             return 0;
2631           break;
2632
2633         case CPP_SEMICOLON:
2634           /* This matches the processing in skip_to_end_of_statement.  */
2635           if (!brace_depth)
2636             return 0;
2637           break;
2638
2639         case CPP_OPEN_BRACE:
2640           ++brace_depth;
2641           break;
2642         case CPP_CLOSE_BRACE:
2643           if (!brace_depth--)
2644             return 0;
2645           break;
2646
2647         case CPP_COMMA:
2648           if (recovering && or_comma && !brace_depth && !paren_depth
2649               && !square_depth)
2650             return -1;
2651           break;
2652
2653         case CPP_OPEN_PAREN:
2654           if (!brace_depth)
2655             ++paren_depth;
2656           break;
2657
2658         case CPP_CLOSE_PAREN:
2659           if (!brace_depth && !paren_depth--)
2660             {
2661               if (consume_paren)
2662                 cp_lexer_consume_token (parser->lexer);
2663               return 1;
2664             }
2665           break;
2666
2667         default:
2668           break;
2669         }
2670
2671       /* Consume the token.  */
2672       cp_lexer_consume_token (parser->lexer);
2673     }
2674 }
2675
2676 /* Consume tokens until we reach the end of the current statement.
2677    Normally, that will be just before consuming a `;'.  However, if a
2678    non-nested `}' comes first, then we stop before consuming that.  */
2679
2680 static void
2681 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2682 {
2683   unsigned nesting_depth = 0;
2684
2685   while (true)
2686     {
2687       cp_token *token = cp_lexer_peek_token (parser->lexer);
2688
2689       switch (token->type)
2690         {
2691         case CPP_EOF:
2692         case CPP_PRAGMA_EOL:
2693           /* If we've run out of tokens, stop.  */
2694           return;
2695
2696         case CPP_SEMICOLON:
2697           /* If the next token is a `;', we have reached the end of the
2698              statement.  */
2699           if (!nesting_depth)
2700             return;
2701           break;
2702
2703         case CPP_CLOSE_BRACE:
2704           /* If this is a non-nested '}', stop before consuming it.
2705              That way, when confronted with something like:
2706
2707                { 3 + }
2708
2709              we stop before consuming the closing '}', even though we
2710              have not yet reached a `;'.  */
2711           if (nesting_depth == 0)
2712             return;
2713
2714           /* If it is the closing '}' for a block that we have
2715              scanned, stop -- but only after consuming the token.
2716              That way given:
2717
2718                 void f g () { ... }
2719                 typedef int I;
2720
2721              we will stop after the body of the erroneously declared
2722              function, but before consuming the following `typedef'
2723              declaration.  */
2724           if (--nesting_depth == 0)
2725             {
2726               cp_lexer_consume_token (parser->lexer);
2727               return;
2728             }
2729
2730         case CPP_OPEN_BRACE:
2731           ++nesting_depth;
2732           break;
2733
2734         default:
2735           break;
2736         }
2737
2738       /* Consume the token.  */
2739       cp_lexer_consume_token (parser->lexer);
2740     }
2741 }
2742
2743 /* This function is called at the end of a statement or declaration.
2744    If the next token is a semicolon, it is consumed; otherwise, error
2745    recovery is attempted.  */
2746
2747 static void
2748 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2749 {
2750   /* Look for the trailing `;'.  */
2751   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2752     {
2753       /* If there is additional (erroneous) input, skip to the end of
2754          the statement.  */
2755       cp_parser_skip_to_end_of_statement (parser);
2756       /* If the next token is now a `;', consume it.  */
2757       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2758         cp_lexer_consume_token (parser->lexer);
2759     }
2760 }
2761
2762 /* Skip tokens until we have consumed an entire block, or until we
2763    have consumed a non-nested `;'.  */
2764
2765 static void
2766 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2767 {
2768   int nesting_depth = 0;
2769
2770   while (nesting_depth >= 0)
2771     {
2772       cp_token *token = cp_lexer_peek_token (parser->lexer);
2773
2774       switch (token->type)
2775         {
2776         case CPP_EOF:
2777         case CPP_PRAGMA_EOL:
2778           /* If we've run out of tokens, stop.  */
2779           return;
2780
2781         case CPP_SEMICOLON:
2782           /* Stop if this is an unnested ';'. */
2783           if (!nesting_depth)
2784             nesting_depth = -1;
2785           break;
2786
2787         case CPP_CLOSE_BRACE:
2788           /* Stop if this is an unnested '}', or closes the outermost
2789              nesting level.  */
2790           nesting_depth--;
2791           if (nesting_depth < 0)
2792             return;
2793           if (!nesting_depth)
2794             nesting_depth = -1;
2795           break;
2796
2797         case CPP_OPEN_BRACE:
2798           /* Nest. */
2799           nesting_depth++;
2800           break;
2801
2802         default:
2803           break;
2804         }
2805
2806       /* Consume the token.  */
2807       cp_lexer_consume_token (parser->lexer);
2808     }
2809 }
2810
2811 /* Skip tokens until a non-nested closing curly brace is the next
2812    token, or there are no more tokens. Return true in the first case,
2813    false otherwise.  */
2814
2815 static bool
2816 cp_parser_skip_to_closing_brace (cp_parser *parser)
2817 {
2818   unsigned nesting_depth = 0;
2819
2820   while (true)
2821     {
2822       cp_token *token = cp_lexer_peek_token (parser->lexer);
2823
2824       switch (token->type)
2825         {
2826         case CPP_EOF:
2827         case CPP_PRAGMA_EOL:
2828           /* If we've run out of tokens, stop.  */
2829           return false;
2830
2831         case CPP_CLOSE_BRACE:
2832           /* If the next token is a non-nested `}', then we have reached
2833              the end of the current block.  */
2834           if (nesting_depth-- == 0)
2835             return true;
2836           break;
2837
2838         case CPP_OPEN_BRACE:
2839           /* If it the next token is a `{', then we are entering a new
2840              block.  Consume the entire block.  */
2841           ++nesting_depth;
2842           break;
2843
2844         default:
2845           break;
2846         }
2847
2848       /* Consume the token.  */
2849       cp_lexer_consume_token (parser->lexer);
2850     }
2851 }
2852
2853 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2854    parameter is the PRAGMA token, allowing us to purge the entire pragma
2855    sequence.  */
2856
2857 static void
2858 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2859 {
2860   cp_token *token;
2861
2862   parser->lexer->in_pragma = false;
2863
2864   do
2865     token = cp_lexer_consume_token (parser->lexer);
2866   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2867
2868   /* Ensure that the pragma is not parsed again.  */
2869   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2870 }
2871
2872 /* Require pragma end of line, resyncing with it as necessary.  The
2873    arguments are as for cp_parser_skip_to_pragma_eol.  */
2874
2875 static void
2876 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2877 {
2878   parser->lexer->in_pragma = false;
2879   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2880     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2881 }
2882
2883 /* This is a simple wrapper around make_typename_type. When the id is
2884    an unresolved identifier node, we can provide a superior diagnostic
2885    using cp_parser_diagnose_invalid_type_name.  */
2886
2887 static tree
2888 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2889                               tree id, location_t id_location)
2890 {
2891   tree result;
2892   if (TREE_CODE (id) == IDENTIFIER_NODE)
2893     {
2894       result = make_typename_type (scope, id, typename_type,
2895                                    /*complain=*/tf_none);
2896       if (result == error_mark_node)
2897         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2898       return result;
2899     }
2900   return make_typename_type (scope, id, typename_type, tf_error);
2901 }
2902
2903 /* This is a wrapper around the
2904    make_{pointer,ptrmem,reference}_declarator functions that decides
2905    which one to call based on the CODE and CLASS_TYPE arguments. The
2906    CODE argument should be one of the values returned by
2907    cp_parser_ptr_operator. */
2908 static cp_declarator *
2909 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2910                                     cp_cv_quals cv_qualifiers,
2911                                     cp_declarator *target)
2912 {
2913   if (code == ERROR_MARK)
2914     return cp_error_declarator;
2915
2916   if (code == INDIRECT_REF)
2917     if (class_type == NULL_TREE)
2918       return make_pointer_declarator (cv_qualifiers, target);
2919     else
2920       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2921   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2922     return make_reference_declarator (cv_qualifiers, target, false);
2923   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2924     return make_reference_declarator (cv_qualifiers, target, true);
2925   gcc_unreachable ();
2926 }
2927
2928 /* Create a new C++ parser.  */
2929
2930 static cp_parser *
2931 cp_parser_new (void)
2932 {
2933   cp_parser *parser;
2934   cp_lexer *lexer;
2935   unsigned i;
2936
2937   /* cp_lexer_new_main is called before doing GC allocation because
2938      cp_lexer_new_main might load a PCH file.  */
2939   lexer = cp_lexer_new_main ();
2940
2941   /* Initialize the binops_by_token so that we can get the tree
2942      directly from the token.  */
2943   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2944     binops_by_token[binops[i].token_type] = binops[i];
2945
2946   parser = ggc_alloc_cleared_cp_parser ();
2947   parser->lexer = lexer;
2948   parser->context = cp_parser_context_new (NULL);
2949
2950   /* For now, we always accept GNU extensions.  */
2951   parser->allow_gnu_extensions_p = 1;
2952
2953   /* The `>' token is a greater-than operator, not the end of a
2954      template-id.  */
2955   parser->greater_than_is_operator_p = true;
2956
2957   parser->default_arg_ok_p = true;
2958
2959   /* We are not parsing a constant-expression.  */
2960   parser->integral_constant_expression_p = false;
2961   parser->allow_non_integral_constant_expression_p = false;
2962   parser->non_integral_constant_expression_p = false;
2963
2964   /* Local variable names are not forbidden.  */
2965   parser->local_variables_forbidden_p = false;
2966
2967   /* We are not processing an `extern "C"' declaration.  */
2968   parser->in_unbraced_linkage_specification_p = false;
2969
2970   /* We are not processing a declarator.  */
2971   parser->in_declarator_p = false;
2972
2973   /* We are not processing a template-argument-list.  */
2974   parser->in_template_argument_list_p = false;
2975
2976   /* We are not in an iteration statement.  */
2977   parser->in_statement = 0;
2978
2979   /* We are not in a switch statement.  */
2980   parser->in_switch_statement_p = false;
2981
2982   /* We are not parsing a type-id inside an expression.  */
2983   parser->in_type_id_in_expr_p = false;
2984
2985   /* Declarations aren't implicitly extern "C".  */
2986   parser->implicit_extern_c = false;
2987
2988   /* String literals should be translated to the execution character set.  */
2989   parser->translate_strings_p = true;
2990
2991   /* We are not parsing a function body.  */
2992   parser->in_function_body = false;
2993
2994   /* We can correct until told otherwise.  */
2995   parser->colon_corrects_to_scope_p = true;
2996
2997   /* The unparsed function queue is empty.  */
2998   push_unparsed_function_queues (parser);
2999
3000   /* There are no classes being defined.  */
3001   parser->num_classes_being_defined = 0;
3002
3003   /* No template parameters apply.  */
3004   parser->num_template_parameter_lists = 0;
3005
3006   return parser;
3007 }
3008
3009 /* Create a cp_lexer structure which will emit the tokens in CACHE
3010    and push it onto the parser's lexer stack.  This is used for delayed
3011    parsing of in-class method bodies and default arguments, and should
3012    not be confused with tentative parsing.  */
3013 static void
3014 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3015 {
3016   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3017   lexer->next = parser->lexer;
3018   parser->lexer = lexer;
3019
3020   /* Move the current source position to that of the first token in the
3021      new lexer.  */
3022   cp_lexer_set_source_position_from_token (lexer->next_token);
3023 }
3024
3025 /* Pop the top lexer off the parser stack.  This is never used for the
3026    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3027 static void
3028 cp_parser_pop_lexer (cp_parser *parser)
3029 {
3030   cp_lexer *lexer = parser->lexer;
3031   parser->lexer = lexer->next;
3032   cp_lexer_destroy (lexer);
3033
3034   /* Put the current source position back where it was before this
3035      lexer was pushed.  */
3036   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3037 }
3038
3039 /* Lexical conventions [gram.lex]  */
3040
3041 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3042    identifier.  */
3043
3044 static tree
3045 cp_parser_identifier (cp_parser* parser)
3046 {
3047   cp_token *token;
3048
3049   /* Look for the identifier.  */
3050   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3051   /* Return the value.  */
3052   return token ? token->u.value : error_mark_node;
3053 }
3054
3055 /* Parse a sequence of adjacent string constants.  Returns a
3056    TREE_STRING representing the combined, nul-terminated string
3057    constant.  If TRANSLATE is true, translate the string to the
3058    execution character set.  If WIDE_OK is true, a wide string is
3059    invalid here.
3060
3061    C++98 [lex.string] says that if a narrow string literal token is
3062    adjacent to a wide string literal token, the behavior is undefined.
3063    However, C99 6.4.5p4 says that this results in a wide string literal.
3064    We follow C99 here, for consistency with the C front end.
3065
3066    This code is largely lifted from lex_string() in c-lex.c.
3067
3068    FUTURE: ObjC++ will need to handle @-strings here.  */
3069 static tree
3070 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3071 {
3072   tree value;
3073   size_t count;
3074   struct obstack str_ob;
3075   cpp_string str, istr, *strs;
3076   cp_token *tok;
3077   enum cpp_ttype type;
3078
3079   tok = cp_lexer_peek_token (parser->lexer);
3080   if (!cp_parser_is_string_literal (tok))
3081     {
3082       cp_parser_error (parser, "expected string-literal");
3083       return error_mark_node;
3084     }
3085
3086   type = tok->type;
3087
3088   /* Try to avoid the overhead of creating and destroying an obstack
3089      for the common case of just one string.  */
3090   if (!cp_parser_is_string_literal
3091       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3092     {
3093       cp_lexer_consume_token (parser->lexer);
3094
3095       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3096       str.len = TREE_STRING_LENGTH (tok->u.value);
3097       count = 1;
3098
3099       strs = &str;
3100     }
3101   else
3102     {
3103       gcc_obstack_init (&str_ob);
3104       count = 0;
3105
3106       do
3107         {
3108           cp_lexer_consume_token (parser->lexer);
3109           count++;
3110           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3111           str.len = TREE_STRING_LENGTH (tok->u.value);
3112
3113           if (type != tok->type)
3114             {
3115               if (type == CPP_STRING)
3116                 type = tok->type;
3117               else if (tok->type != CPP_STRING)
3118                 error_at (tok->location,
3119                           "unsupported non-standard concatenation "
3120                           "of string literals");
3121             }
3122
3123           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3124
3125           tok = cp_lexer_peek_token (parser->lexer);
3126         }
3127       while (cp_parser_is_string_literal (tok));
3128
3129       strs = (cpp_string *) obstack_finish (&str_ob);
3130     }
3131
3132   if (type != CPP_STRING && !wide_ok)
3133     {
3134       cp_parser_error (parser, "a wide string is invalid in this context");
3135       type = CPP_STRING;
3136     }
3137
3138   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3139       (parse_in, strs, count, &istr, type))
3140     {
3141       value = build_string (istr.len, (const char *)istr.text);
3142       free (CONST_CAST (unsigned char *, istr.text));
3143
3144       switch (type)
3145         {
3146         default:
3147         case CPP_STRING:
3148         case CPP_UTF8STRING:
3149           TREE_TYPE (value) = char_array_type_node;
3150           break;
3151         case CPP_STRING16:
3152           TREE_TYPE (value) = char16_array_type_node;
3153           break;
3154         case CPP_STRING32:
3155           TREE_TYPE (value) = char32_array_type_node;
3156           break;
3157         case CPP_WSTRING:
3158           TREE_TYPE (value) = wchar_array_type_node;
3159           break;
3160         }
3161
3162       value = fix_string_type (value);
3163     }
3164   else
3165     /* cpp_interpret_string has issued an error.  */
3166     value = error_mark_node;
3167
3168   if (count > 1)
3169     obstack_free (&str_ob, 0);
3170
3171   return value;
3172 }
3173
3174
3175 /* Basic concepts [gram.basic]  */
3176
3177 /* Parse a translation-unit.
3178
3179    translation-unit:
3180      declaration-seq [opt]
3181
3182    Returns TRUE if all went well.  */
3183
3184 static bool
3185 cp_parser_translation_unit (cp_parser* parser)
3186 {
3187   /* The address of the first non-permanent object on the declarator
3188      obstack.  */
3189   static void *declarator_obstack_base;
3190
3191   bool success;
3192
3193   /* Create the declarator obstack, if necessary.  */
3194   if (!cp_error_declarator)
3195     {
3196       gcc_obstack_init (&declarator_obstack);
3197       /* Create the error declarator.  */
3198       cp_error_declarator = make_declarator (cdk_error);
3199       /* Create the empty parameter list.  */
3200       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3201       /* Remember where the base of the declarator obstack lies.  */
3202       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3203     }
3204
3205   cp_parser_declaration_seq_opt (parser);
3206
3207   /* If there are no tokens left then all went well.  */
3208   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3209     {
3210       /* Get rid of the token array; we don't need it any more.  */
3211       cp_lexer_destroy (parser->lexer);
3212       parser->lexer = NULL;
3213
3214       /* This file might have been a context that's implicitly extern
3215          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3216       if (parser->implicit_extern_c)
3217         {
3218           pop_lang_context ();
3219           parser->implicit_extern_c = false;
3220         }
3221
3222       /* Finish up.  */
3223       finish_translation_unit ();
3224
3225       success = true;
3226     }
3227   else
3228     {
3229       cp_parser_error (parser, "expected declaration");
3230       success = false;
3231     }
3232
3233   /* Make sure the declarator obstack was fully cleaned up.  */
3234   gcc_assert (obstack_next_free (&declarator_obstack)
3235               == declarator_obstack_base);
3236
3237   /* All went well.  */
3238   return success;
3239 }
3240
3241 /* Expressions [gram.expr] */
3242
3243 /* Parse a primary-expression.
3244
3245    primary-expression:
3246      literal
3247      this
3248      ( expression )
3249      id-expression
3250
3251    GNU Extensions:
3252
3253    primary-expression:
3254      ( compound-statement )
3255      __builtin_va_arg ( assignment-expression , type-id )
3256      __builtin_offsetof ( type-id , offsetof-expression )
3257
3258    C++ Extensions:
3259      __has_nothrow_assign ( type-id )   
3260      __has_nothrow_constructor ( type-id )
3261      __has_nothrow_copy ( type-id )
3262      __has_trivial_assign ( type-id )   
3263      __has_trivial_constructor ( type-id )
3264      __has_trivial_copy ( type-id )
3265      __has_trivial_destructor ( type-id )
3266      __has_virtual_destructor ( type-id )     
3267      __is_abstract ( type-id )
3268      __is_base_of ( type-id , type-id )
3269      __is_class ( type-id )
3270      __is_convertible_to ( type-id , type-id )     
3271      __is_empty ( type-id )
3272      __is_enum ( type-id )
3273      __is_pod ( type-id )
3274      __is_polymorphic ( type-id )
3275      __is_union ( type-id )
3276
3277    Objective-C++ Extension:
3278
3279    primary-expression:
3280      objc-expression
3281
3282    literal:
3283      __null
3284
3285    ADDRESS_P is true iff this expression was immediately preceded by
3286    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3287    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3288    true iff this expression is a template argument.
3289
3290    Returns a representation of the expression.  Upon return, *IDK
3291    indicates what kind of id-expression (if any) was present.  */
3292
3293 static tree
3294 cp_parser_primary_expression (cp_parser *parser,
3295                               bool address_p,
3296                               bool cast_p,
3297                               bool template_arg_p,
3298                               cp_id_kind *idk)
3299 {
3300   cp_token *token = NULL;
3301
3302   /* Assume the primary expression is not an id-expression.  */
3303   *idk = CP_ID_KIND_NONE;
3304
3305   /* Peek at the next token.  */
3306   token = cp_lexer_peek_token (parser->lexer);
3307   switch (token->type)
3308     {
3309       /* literal:
3310            integer-literal
3311            character-literal
3312            floating-literal
3313            string-literal
3314            boolean-literal  */
3315     case CPP_CHAR:
3316     case CPP_CHAR16:
3317     case CPP_CHAR32:
3318     case CPP_WCHAR:
3319     case CPP_NUMBER:
3320       token = cp_lexer_consume_token (parser->lexer);
3321       if (TREE_CODE (token->u.value) == FIXED_CST)
3322         {
3323           error_at (token->location,
3324                     "fixed-point types not supported in C++");
3325           return error_mark_node;
3326         }
3327       /* Floating-point literals are only allowed in an integral
3328          constant expression if they are cast to an integral or
3329          enumeration type.  */
3330       if (TREE_CODE (token->u.value) == REAL_CST
3331           && parser->integral_constant_expression_p
3332           && pedantic)
3333         {
3334           /* CAST_P will be set even in invalid code like "int(2.7 +
3335              ...)".   Therefore, we have to check that the next token
3336              is sure to end the cast.  */
3337           if (cast_p)
3338             {
3339               cp_token *next_token;
3340
3341               next_token = cp_lexer_peek_token (parser->lexer);
3342               if (/* The comma at the end of an
3343                      enumerator-definition.  */
3344                   next_token->type != CPP_COMMA
3345                   /* The curly brace at the end of an enum-specifier.  */
3346                   && next_token->type != CPP_CLOSE_BRACE
3347                   /* The end of a statement.  */
3348                   && next_token->type != CPP_SEMICOLON
3349                   /* The end of the cast-expression.  */
3350                   && next_token->type != CPP_CLOSE_PAREN
3351                   /* The end of an array bound.  */
3352                   && next_token->type != CPP_CLOSE_SQUARE
3353                   /* The closing ">" in a template-argument-list.  */
3354                   && (next_token->type != CPP_GREATER
3355                       || parser->greater_than_is_operator_p)
3356                   /* C++0x only: A ">>" treated like two ">" tokens,
3357                      in a template-argument-list.  */
3358                   && (next_token->type != CPP_RSHIFT
3359                       || (cxx_dialect == cxx98)
3360                       || parser->greater_than_is_operator_p))
3361                 cast_p = false;
3362             }
3363
3364           /* If we are within a cast, then the constraint that the
3365              cast is to an integral or enumeration type will be
3366              checked at that point.  If we are not within a cast, then
3367              this code is invalid.  */
3368           if (!cast_p)
3369             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3370         }
3371       return token->u.value;
3372
3373     case CPP_STRING:
3374     case CPP_STRING16:
3375     case CPP_STRING32:
3376     case CPP_WSTRING:
3377     case CPP_UTF8STRING:
3378       /* ??? Should wide strings be allowed when parser->translate_strings_p
3379          is false (i.e. in attributes)?  If not, we can kill the third
3380          argument to cp_parser_string_literal.  */
3381       return cp_parser_string_literal (parser,
3382                                        parser->translate_strings_p,
3383                                        true);
3384
3385     case CPP_OPEN_PAREN:
3386       {
3387         tree expr;
3388         bool saved_greater_than_is_operator_p;
3389
3390         /* Consume the `('.  */
3391         cp_lexer_consume_token (parser->lexer);
3392         /* Within a parenthesized expression, a `>' token is always
3393            the greater-than operator.  */
3394         saved_greater_than_is_operator_p
3395           = parser->greater_than_is_operator_p;
3396         parser->greater_than_is_operator_p = true;
3397         /* If we see `( { ' then we are looking at the beginning of
3398            a GNU statement-expression.  */
3399         if (cp_parser_allow_gnu_extensions_p (parser)
3400             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3401           {
3402             /* Statement-expressions are not allowed by the standard.  */
3403             pedwarn (token->location, OPT_pedantic, 
3404                      "ISO C++ forbids braced-groups within expressions");
3405
3406             /* And they're not allowed outside of a function-body; you
3407                cannot, for example, write:
3408
3409                  int i = ({ int j = 3; j + 1; });
3410
3411                at class or namespace scope.  */
3412             if (!parser->in_function_body
3413                 || parser->in_template_argument_list_p)
3414               {
3415                 error_at (token->location,
3416                           "statement-expressions are not allowed outside "
3417                           "functions nor in template-argument lists");
3418                 cp_parser_skip_to_end_of_block_or_statement (parser);
3419                 expr = error_mark_node;
3420               }
3421             else
3422               {
3423                 /* Start the statement-expression.  */
3424                 expr = begin_stmt_expr ();
3425                 /* Parse the compound-statement.  */
3426                 cp_parser_compound_statement (parser, expr, false, false);
3427                 /* Finish up.  */
3428                 expr = finish_stmt_expr (expr, false);
3429               }
3430           }
3431         else
3432           {
3433             /* Parse the parenthesized expression.  */
3434             expr = cp_parser_expression (parser, cast_p, idk);
3435             /* Let the front end know that this expression was
3436                enclosed in parentheses. This matters in case, for
3437                example, the expression is of the form `A::B', since
3438                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3439                not.  */
3440             finish_parenthesized_expr (expr);
3441             /* DR 705: Wrapping an unqualified name in parentheses
3442                suppresses arg-dependent lookup.  We want to pass back
3443                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3444                (c++/37862), but none of the others.  */
3445             if (*idk != CP_ID_KIND_QUALIFIED)
3446               *idk = CP_ID_KIND_NONE;
3447           }
3448         /* The `>' token might be the end of a template-id or
3449            template-parameter-list now.  */
3450         parser->greater_than_is_operator_p
3451           = saved_greater_than_is_operator_p;
3452         /* Consume the `)'.  */
3453         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3454           cp_parser_skip_to_end_of_statement (parser);
3455
3456         return expr;
3457       }
3458
3459     case CPP_OPEN_SQUARE:
3460       if (c_dialect_objc ())
3461         /* We have an Objective-C++ message. */
3462         return cp_parser_objc_expression (parser);
3463       {
3464         tree lam = cp_parser_lambda_expression (parser);
3465         /* Don't warn about a failed tentative parse.  */
3466         if (cp_parser_error_occurred (parser))
3467           return error_mark_node;
3468         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3469         return lam;
3470       }
3471
3472     case CPP_OBJC_STRING:
3473       if (c_dialect_objc ())
3474         /* We have an Objective-C++ string literal. */
3475         return cp_parser_objc_expression (parser);
3476       cp_parser_error (parser, "expected primary-expression");
3477       return error_mark_node;
3478
3479     case CPP_KEYWORD:
3480       switch (token->keyword)
3481         {
3482           /* These two are the boolean literals.  */
3483         case RID_TRUE:
3484           cp_lexer_consume_token (parser->lexer);
3485           return boolean_true_node;
3486         case RID_FALSE:
3487           cp_lexer_consume_token (parser->lexer);
3488           return boolean_false_node;
3489
3490           /* The `__null' literal.  */
3491         case RID_NULL:
3492           cp_lexer_consume_token (parser->lexer);
3493           return null_node;
3494
3495           /* The `nullptr' literal.  */
3496         case RID_NULLPTR:
3497           cp_lexer_consume_token (parser->lexer);
3498           return nullptr_node;
3499
3500           /* Recognize the `this' keyword.  */
3501         case RID_THIS:
3502           cp_lexer_consume_token (parser->lexer);
3503           if (parser->local_variables_forbidden_p)
3504             {
3505               error_at (token->location,
3506                         "%<this%> may not be used in this context");
3507               return error_mark_node;
3508             }
3509           /* Pointers cannot appear in constant-expressions.  */
3510           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3511             return error_mark_node;
3512           return finish_this_expr ();
3513
3514           /* The `operator' keyword can be the beginning of an
3515              id-expression.  */
3516         case RID_OPERATOR:
3517           goto id_expression;
3518
3519         case RID_FUNCTION_NAME:
3520         case RID_PRETTY_FUNCTION_NAME:
3521         case RID_C99_FUNCTION_NAME:
3522           {
3523             non_integral_constant name;
3524
3525             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3526                __func__ are the names of variables -- but they are
3527                treated specially.  Therefore, they are handled here,
3528                rather than relying on the generic id-expression logic
3529                below.  Grammatically, these names are id-expressions.
3530
3531                Consume the token.  */
3532             token = cp_lexer_consume_token (parser->lexer);
3533
3534             switch (token->keyword)
3535               {
3536               case RID_FUNCTION_NAME:
3537                 name = NIC_FUNC_NAME;
3538                 break;
3539               case RID_PRETTY_FUNCTION_NAME:
3540                 name = NIC_PRETTY_FUNC;
3541                 break;
3542               case RID_C99_FUNCTION_NAME:
3543                 name = NIC_C99_FUNC;
3544                 break;
3545               default:
3546                 gcc_unreachable ();
3547               }
3548
3549             if (cp_parser_non_integral_constant_expression (parser, name))
3550               return error_mark_node;
3551
3552             /* Look up the name.  */
3553             return finish_fname (token->u.value);
3554           }
3555
3556         case RID_VA_ARG:
3557           {
3558             tree expression;
3559             tree type;
3560
3561             /* The `__builtin_va_arg' construct is used to handle
3562                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3563             cp_lexer_consume_token (parser->lexer);
3564             /* Look for the opening `('.  */
3565             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3566             /* Now, parse the assignment-expression.  */
3567             expression = cp_parser_assignment_expression (parser,
3568                                                           /*cast_p=*/false, NULL);
3569             /* Look for the `,'.  */
3570             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3571             /* Parse the type-id.  */
3572             type = cp_parser_type_id (parser);
3573             /* Look for the closing `)'.  */
3574             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3575             /* Using `va_arg' in a constant-expression is not
3576                allowed.  */
3577             if (cp_parser_non_integral_constant_expression (parser,
3578                                                             NIC_VA_ARG))
3579               return error_mark_node;
3580             return build_x_va_arg (expression, type);
3581           }
3582
3583         case RID_OFFSETOF:
3584           return cp_parser_builtin_offsetof (parser);
3585
3586         case RID_HAS_NOTHROW_ASSIGN:
3587         case RID_HAS_NOTHROW_CONSTRUCTOR:
3588         case RID_HAS_NOTHROW_COPY:        
3589         case RID_HAS_TRIVIAL_ASSIGN:
3590         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3591         case RID_HAS_TRIVIAL_COPY:        
3592         case RID_HAS_TRIVIAL_DESTRUCTOR:
3593         case RID_HAS_VIRTUAL_DESTRUCTOR:
3594         case RID_IS_ABSTRACT:
3595         case RID_IS_BASE_OF:
3596         case RID_IS_CLASS:
3597         case RID_IS_CONVERTIBLE_TO:
3598         case RID_IS_EMPTY:
3599         case RID_IS_ENUM:
3600         case RID_IS_POD:
3601         case RID_IS_POLYMORPHIC:
3602         case RID_IS_STD_LAYOUT:
3603         case RID_IS_TRIVIAL:
3604         case RID_IS_UNION:
3605         case RID_IS_LITERAL_TYPE:
3606           return cp_parser_trait_expr (parser, token->keyword);
3607
3608         /* Objective-C++ expressions.  */
3609         case RID_AT_ENCODE:
3610         case RID_AT_PROTOCOL:
3611         case RID_AT_SELECTOR:
3612           return cp_parser_objc_expression (parser);
3613
3614         case RID_TEMPLATE:
3615           if (parser->in_function_body
3616               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3617                   == CPP_LESS))
3618             {
3619               error_at (token->location,
3620                         "a template declaration cannot appear at block scope");
3621               cp_parser_skip_to_end_of_block_or_statement (parser);
3622               return error_mark_node;
3623             }
3624         default:
3625           cp_parser_error (parser, "expected primary-expression");
3626           return error_mark_node;
3627         }
3628
3629       /* An id-expression can start with either an identifier, a
3630          `::' as the beginning of a qualified-id, or the "operator"
3631          keyword.  */
3632     case CPP_NAME:
3633     case CPP_SCOPE:
3634     case CPP_TEMPLATE_ID:
3635     case CPP_NESTED_NAME_SPECIFIER:
3636       {
3637         tree id_expression;
3638         tree decl;
3639         const char *error_msg;
3640         bool template_p;
3641         bool done;
3642         cp_token *id_expr_token;
3643
3644       id_expression:
3645         /* Parse the id-expression.  */
3646         id_expression
3647           = cp_parser_id_expression (parser,
3648                                      /*template_keyword_p=*/false,
3649                                      /*check_dependency_p=*/true,
3650                                      &template_p,
3651                                      /*declarator_p=*/false,
3652                                      /*optional_p=*/false);
3653         if (id_expression == error_mark_node)
3654           return error_mark_node;
3655         id_expr_token = token;
3656         token = cp_lexer_peek_token (parser->lexer);
3657         done = (token->type != CPP_OPEN_SQUARE
3658                 && token->type != CPP_OPEN_PAREN
3659                 && token->type != CPP_DOT
3660                 && token->type != CPP_DEREF
3661                 && token->type != CPP_PLUS_PLUS
3662                 && token->type != CPP_MINUS_MINUS);
3663         /* If we have a template-id, then no further lookup is
3664            required.  If the template-id was for a template-class, we
3665            will sometimes have a TYPE_DECL at this point.  */
3666         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3667                  || TREE_CODE (id_expression) == TYPE_DECL)
3668           decl = id_expression;
3669         /* Look up the name.  */
3670         else
3671           {
3672             tree ambiguous_decls;
3673
3674             /* If we already know that this lookup is ambiguous, then
3675                we've already issued an error message; there's no reason
3676                to check again.  */
3677             if (id_expr_token->type == CPP_NAME
3678                 && id_expr_token->ambiguous_p)
3679               {
3680                 cp_parser_simulate_error (parser);
3681                 return error_mark_node;
3682               }
3683
3684             decl = cp_parser_lookup_name (parser, id_expression,
3685                                           none_type,
3686                                           template_p,
3687                                           /*is_namespace=*/false,
3688                                           /*check_dependency=*/true,
3689                                           &ambiguous_decls,
3690                                           id_expr_token->location);
3691             /* If the lookup was ambiguous, an error will already have
3692                been issued.  */
3693             if (ambiguous_decls)
3694               return error_mark_node;
3695
3696             /* In Objective-C++, we may have an Objective-C 2.0
3697                dot-syntax for classes here.  */
3698             if (c_dialect_objc ()
3699                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3700                 && TREE_CODE (decl) == TYPE_DECL
3701                 && objc_is_class_name (decl))
3702               {
3703                 tree component;
3704                 cp_lexer_consume_token (parser->lexer);
3705                 component = cp_parser_identifier (parser);
3706                 if (component == error_mark_node)
3707                   return error_mark_node;
3708
3709                 return objc_build_class_component_ref (id_expression, component);
3710               }
3711
3712             /* In Objective-C++, an instance variable (ivar) may be preferred
3713                to whatever cp_parser_lookup_name() found.  */
3714             decl = objc_lookup_ivar (decl, id_expression);
3715
3716             /* If name lookup gives us a SCOPE_REF, then the
3717                qualifying scope was dependent.  */
3718             if (TREE_CODE (decl) == SCOPE_REF)
3719               {
3720                 /* At this point, we do not know if DECL is a valid
3721                    integral constant expression.  We assume that it is
3722                    in fact such an expression, so that code like:
3723
3724                       template <int N> struct A {
3725                         int a[B<N>::i];
3726                       };
3727                      
3728                    is accepted.  At template-instantiation time, we
3729                    will check that B<N>::i is actually a constant.  */
3730                 return decl;
3731               }
3732             /* Check to see if DECL is a local variable in a context
3733                where that is forbidden.  */
3734             if (parser->local_variables_forbidden_p
3735                 && local_variable_p (decl))
3736               {
3737                 /* It might be that we only found DECL because we are
3738                    trying to be generous with pre-ISO scoping rules.
3739                    For example, consider:
3740
3741                      int i;
3742                      void g() {
3743                        for (int i = 0; i < 10; ++i) {}
3744                        extern void f(int j = i);
3745                      }
3746
3747                    Here, name look up will originally find the out
3748                    of scope `i'.  We need to issue a warning message,
3749                    but then use the global `i'.  */
3750                 decl = check_for_out_of_scope_variable (decl);
3751                 if (local_variable_p (decl))
3752                   {
3753                     error_at (id_expr_token->location,
3754                               "local variable %qD may not appear in this context",
3755                               decl);
3756                     return error_mark_node;
3757                   }
3758               }
3759           }
3760
3761         decl = (finish_id_expression
3762                 (id_expression, decl, parser->scope,
3763                  idk,
3764                  parser->integral_constant_expression_p,
3765                  parser->allow_non_integral_constant_expression_p,
3766                  &parser->non_integral_constant_expression_p,
3767                  template_p, done, address_p,
3768                  template_arg_p,
3769                  &error_msg,
3770                  id_expr_token->location));
3771         if (error_msg)
3772           cp_parser_error (parser, error_msg);
3773         return decl;
3774       }
3775
3776       /* Anything else is an error.  */
3777     default:
3778       cp_parser_error (parser, "expected primary-expression");
3779       return error_mark_node;
3780     }
3781 }
3782
3783 /* Parse an id-expression.
3784
3785    id-expression:
3786      unqualified-id
3787      qualified-id
3788
3789    qualified-id:
3790      :: [opt] nested-name-specifier template [opt] unqualified-id
3791      :: identifier
3792      :: operator-function-id
3793      :: template-id
3794
3795    Return a representation of the unqualified portion of the
3796    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3797    a `::' or nested-name-specifier.
3798
3799    Often, if the id-expression was a qualified-id, the caller will
3800    want to make a SCOPE_REF to represent the qualified-id.  This
3801    function does not do this in order to avoid wastefully creating
3802    SCOPE_REFs when they are not required.
3803
3804    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3805    `template' keyword.
3806
3807    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3808    uninstantiated templates.
3809
3810    If *TEMPLATE_P is non-NULL, it is set to true iff the
3811    `template' keyword is used to explicitly indicate that the entity
3812    named is a template.
3813
3814    If DECLARATOR_P is true, the id-expression is appearing as part of
3815    a declarator, rather than as part of an expression.  */
3816
3817 static tree
3818 cp_parser_id_expression (cp_parser *parser,
3819                          bool template_keyword_p,
3820                          bool check_dependency_p,
3821                          bool *template_p,
3822                          bool declarator_p,
3823                          bool optional_p)
3824 {
3825   bool global_scope_p;
3826   bool nested_name_specifier_p;
3827
3828   /* Assume the `template' keyword was not used.  */
3829   if (template_p)
3830     *template_p = template_keyword_p;
3831
3832   /* Look for the optional `::' operator.  */
3833   global_scope_p
3834     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3835        != NULL_TREE);
3836   /* Look for the optional nested-name-specifier.  */
3837   nested_name_specifier_p
3838     = (cp_parser_nested_name_specifier_opt (parser,
3839                                             /*typename_keyword_p=*/false,
3840                                             check_dependency_p,
3841                                             /*type_p=*/false,
3842                                             declarator_p)
3843        != NULL_TREE);
3844   /* If there is a nested-name-specifier, then we are looking at
3845      the first qualified-id production.  */
3846   if (nested_name_specifier_p)
3847     {
3848       tree saved_scope;
3849       tree saved_object_scope;
3850       tree saved_qualifying_scope;
3851       tree unqualified_id;
3852       bool is_template;
3853
3854       /* See if the next token is the `template' keyword.  */
3855       if (!template_p)
3856         template_p = &is_template;
3857       *template_p = cp_parser_optional_template_keyword (parser);
3858       /* Name lookup we do during the processing of the
3859          unqualified-id might obliterate SCOPE.  */
3860       saved_scope = parser->scope;
3861       saved_object_scope = parser->object_scope;
3862       saved_qualifying_scope = parser->qualifying_scope;
3863       /* Process the final unqualified-id.  */
3864       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3865                                                  check_dependency_p,
3866                                                  declarator_p,
3867                                                  /*optional_p=*/false);
3868       /* Restore the SAVED_SCOPE for our caller.  */
3869       parser->scope = saved_scope;
3870       parser->object_scope = saved_object_scope;
3871       parser->qualifying_scope = saved_qualifying_scope;
3872
3873       return unqualified_id;
3874     }
3875   /* Otherwise, if we are in global scope, then we are looking at one
3876      of the other qualified-id productions.  */
3877   else if (global_scope_p)
3878     {
3879       cp_token *token;
3880       tree id;
3881
3882       /* Peek at the next token.  */
3883       token = cp_lexer_peek_token (parser->lexer);
3884
3885       /* If it's an identifier, and the next token is not a "<", then
3886          we can avoid the template-id case.  This is an optimization
3887          for this common case.  */
3888       if (token->type == CPP_NAME
3889           && !cp_parser_nth_token_starts_template_argument_list_p
3890                (parser, 2))
3891         return cp_parser_identifier (parser);
3892
3893       cp_parser_parse_tentatively (parser);
3894       /* Try a template-id.  */
3895       id = cp_parser_template_id (parser,
3896                                   /*template_keyword_p=*/false,
3897                                   /*check_dependency_p=*/true,
3898                                   declarator_p);
3899       /* If that worked, we're done.  */
3900       if (cp_parser_parse_definitely (parser))
3901         return id;
3902
3903       /* Peek at the next token.  (Changes in the token buffer may
3904          have invalidated the pointer obtained above.)  */
3905       token = cp_lexer_peek_token (parser->lexer);
3906
3907       switch (token->type)
3908         {
3909         case CPP_NAME:
3910           return cp_parser_identifier (parser);
3911
3912         case CPP_KEYWORD:
3913           if (token->keyword == RID_OPERATOR)
3914             return cp_parser_operator_function_id (parser);
3915           /* Fall through.  */
3916
3917         default:
3918           cp_parser_error (parser, "expected id-expression");
3919           return error_mark_node;
3920         }
3921     }
3922   else
3923     return cp_parser_unqualified_id (parser, template_keyword_p,
3924                                      /*check_dependency_p=*/true,
3925                                      declarator_p,
3926                                      optional_p);
3927 }
3928
3929 /* Parse an unqualified-id.
3930
3931    unqualified-id:
3932      identifier
3933      operator-function-id
3934      conversion-function-id
3935      ~ class-name
3936      template-id
3937
3938    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3939    keyword, in a construct like `A::template ...'.
3940
3941    Returns a representation of unqualified-id.  For the `identifier'
3942    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3943    production a BIT_NOT_EXPR is returned; the operand of the
3944    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3945    other productions, see the documentation accompanying the
3946    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3947    names are looked up in uninstantiated templates.  If DECLARATOR_P
3948    is true, the unqualified-id is appearing as part of a declarator,
3949    rather than as part of an expression.  */
3950
3951 static tree
3952 cp_parser_unqualified_id (cp_parser* parser,
3953                           bool template_keyword_p,
3954                           bool check_dependency_p,
3955                           bool declarator_p,
3956                           bool optional_p)
3957 {
3958   cp_token *token;
3959
3960   /* Peek at the next token.  */
3961   token = cp_lexer_peek_token (parser->lexer);
3962
3963   switch (token->type)
3964     {
3965     case CPP_NAME:
3966       {
3967         tree id;
3968
3969         /* We don't know yet whether or not this will be a
3970            template-id.  */
3971         cp_parser_parse_tentatively (parser);
3972         /* Try a template-id.  */
3973         id = cp_parser_template_id (parser, template_keyword_p,
3974                                     check_dependency_p,
3975                                     declarator_p);
3976         /* If it worked, we're done.  */
3977         if (cp_parser_parse_definitely (parser))
3978           return id;
3979         /* Otherwise, it's an ordinary identifier.  */
3980         return cp_parser_identifier (parser);
3981       }
3982
3983     case CPP_TEMPLATE_ID:
3984       return cp_parser_template_id (parser, template_keyword_p,
3985                                     check_dependency_p,
3986                                     declarator_p);
3987
3988     case CPP_COMPL:
3989       {
3990         tree type_decl;
3991         tree qualifying_scope;
3992         tree object_scope;
3993         tree scope;
3994         bool done;
3995
3996         /* Consume the `~' token.  */
3997         cp_lexer_consume_token (parser->lexer);
3998         /* Parse the class-name.  The standard, as written, seems to
3999            say that:
4000
4001              template <typename T> struct S { ~S (); };
4002              template <typename T> S<T>::~S() {}
4003
4004            is invalid, since `~' must be followed by a class-name, but
4005            `S<T>' is dependent, and so not known to be a class.
4006            That's not right; we need to look in uninstantiated
4007            templates.  A further complication arises from:
4008
4009              template <typename T> void f(T t) {
4010                t.T::~T();
4011              }
4012
4013            Here, it is not possible to look up `T' in the scope of `T'
4014            itself.  We must look in both the current scope, and the
4015            scope of the containing complete expression.
4016
4017            Yet another issue is:
4018
4019              struct S {
4020                int S;
4021                ~S();
4022              };
4023
4024              S::~S() {}
4025
4026            The standard does not seem to say that the `S' in `~S'
4027            should refer to the type `S' and not the data member
4028            `S::S'.  */
4029
4030         /* DR 244 says that we look up the name after the "~" in the
4031            same scope as we looked up the qualifying name.  That idea
4032            isn't fully worked out; it's more complicated than that.  */
4033         scope = parser->scope;
4034         object_scope = parser->object_scope;
4035         qualifying_scope = parser->qualifying_scope;
4036
4037         /* Check for invalid scopes.  */
4038         if (scope == error_mark_node)
4039           {
4040             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4041               cp_lexer_consume_token (parser->lexer);
4042             return error_mark_node;
4043           }
4044         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4045           {
4046             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4047               error_at (token->location,
4048                         "scope %qT before %<~%> is not a class-name",
4049                         scope);
4050             cp_parser_simulate_error (parser);
4051             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4052               cp_lexer_consume_token (parser->lexer);
4053             return error_mark_node;
4054           }
4055         gcc_assert (!scope || TYPE_P (scope));
4056
4057         /* If the name is of the form "X::~X" it's OK even if X is a
4058            typedef.  */
4059         token = cp_lexer_peek_token (parser->lexer);
4060         if (scope
4061             && token->type == CPP_NAME
4062             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4063                 != CPP_LESS)
4064             && (token->u.value == TYPE_IDENTIFIER (scope)
4065                 || constructor_name_p (token->u.value, scope)))
4066           {
4067             cp_lexer_consume_token (parser->lexer);
4068             return build_nt (BIT_NOT_EXPR, scope);
4069           }
4070
4071         /* If there was an explicit qualification (S::~T), first look
4072            in the scope given by the qualification (i.e., S).
4073
4074            Note: in the calls to cp_parser_class_name below we pass
4075            typename_type so that lookup finds the injected-class-name
4076            rather than the constructor.  */
4077         done = false;
4078         type_decl = NULL_TREE;
4079         if (scope)
4080           {
4081             cp_parser_parse_tentatively (parser);
4082             type_decl = cp_parser_class_name (parser,
4083                                               /*typename_keyword_p=*/false,
4084                                               /*template_keyword_p=*/false,
4085                                               typename_type,
4086                                               /*check_dependency=*/false,
4087                                               /*class_head_p=*/false,
4088                                               declarator_p);
4089             if (cp_parser_parse_definitely (parser))
4090               done = true;
4091           }
4092         /* In "N::S::~S", look in "N" as well.  */
4093         if (!done && scope && qualifying_scope)
4094           {
4095             cp_parser_parse_tentatively (parser);
4096             parser->scope = qualifying_scope;
4097             parser->object_scope = NULL_TREE;
4098             parser->qualifying_scope = NULL_TREE;
4099             type_decl
4100               = cp_parser_class_name (parser,
4101                                       /*typename_keyword_p=*/false,
4102                                       /*template_keyword_p=*/false,
4103                                       typename_type,
4104                                       /*check_dependency=*/false,
4105                                       /*class_head_p=*/false,
4106                                       declarator_p);
4107             if (cp_parser_parse_definitely (parser))
4108               done = true;
4109           }
4110         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4111         else if (!done && object_scope)
4112           {
4113             cp_parser_parse_tentatively (parser);
4114             parser->scope = object_scope;
4115             parser->object_scope = NULL_TREE;
4116             parser->qualifying_scope = NULL_TREE;
4117             type_decl
4118               = cp_parser_class_name (parser,
4119                                       /*typename_keyword_p=*/false,
4120                                       /*template_keyword_p=*/false,
4121                                       typename_type,
4122                                       /*check_dependency=*/false,
4123                                       /*class_head_p=*/false,
4124                                       declarator_p);
4125             if (cp_parser_parse_definitely (parser))
4126               done = true;
4127           }
4128         /* Look in the surrounding context.  */
4129         if (!done)
4130           {
4131             parser->scope = NULL_TREE;
4132             parser->object_scope = NULL_TREE;
4133             parser->qualifying_scope = NULL_TREE;
4134             if (processing_template_decl)
4135               cp_parser_parse_tentatively (parser);
4136             type_decl
4137               = cp_parser_class_name (parser,
4138                                       /*typename_keyword_p=*/false,
4139                                       /*template_keyword_p=*/false,
4140                                       typename_type,
4141                                       /*check_dependency=*/false,
4142                                       /*class_head_p=*/false,
4143                                       declarator_p);
4144             if (processing_template_decl
4145                 && ! cp_parser_parse_definitely (parser))
4146               {
4147                 /* We couldn't find a type with this name, so just accept
4148                    it and check for a match at instantiation time.  */
4149                 type_decl = cp_parser_identifier (parser);
4150                 if (type_decl != error_mark_node)
4151                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4152                 return type_decl;
4153               }
4154           }
4155         /* If an error occurred, assume that the name of the
4156            destructor is the same as the name of the qualifying
4157            class.  That allows us to keep parsing after running
4158            into ill-formed destructor names.  */
4159         if (type_decl == error_mark_node && scope)
4160           return build_nt (BIT_NOT_EXPR, scope);
4161         else if (type_decl == error_mark_node)
4162           return error_mark_node;
4163
4164         /* Check that destructor name and scope match.  */
4165         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4166           {
4167             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4168               error_at (token->location,
4169                         "declaration of %<~%T%> as member of %qT",
4170                         type_decl, scope);
4171             cp_parser_simulate_error (parser);
4172             return error_mark_node;
4173           }
4174
4175         /* [class.dtor]
4176
4177            A typedef-name that names a class shall not be used as the
4178            identifier in the declarator for a destructor declaration.  */
4179         if (declarator_p
4180             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4181             && !DECL_SELF_REFERENCE_P (type_decl)
4182             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4183           error_at (token->location,
4184                     "typedef-name %qD used as destructor declarator",
4185                     type_decl);
4186
4187         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4188       }
4189
4190     case CPP_KEYWORD:
4191       if (token->keyword == RID_OPERATOR)
4192         {
4193           tree id;
4194
4195           /* This could be a template-id, so we try that first.  */
4196           cp_parser_parse_tentatively (parser);
4197           /* Try a template-id.  */
4198           id = cp_parser_template_id (parser, template_keyword_p,
4199                                       /*check_dependency_p=*/true,
4200                                       declarator_p);
4201           /* If that worked, we're done.  */
4202           if (cp_parser_parse_definitely (parser))
4203             return id;
4204           /* We still don't know whether we're looking at an
4205              operator-function-id or a conversion-function-id.  */
4206           cp_parser_parse_tentatively (parser);
4207           /* Try an operator-function-id.  */
4208           id = cp_parser_operator_function_id (parser);
4209           /* If that didn't work, try a conversion-function-id.  */
4210           if (!cp_parser_parse_definitely (parser))
4211             id = cp_parser_conversion_function_id (parser);
4212
4213           return id;
4214         }
4215       /* Fall through.  */
4216
4217     default:
4218       if (optional_p)
4219         return NULL_TREE;
4220       cp_parser_error (parser, "expected unqualified-id");
4221       return error_mark_node;
4222     }
4223 }
4224
4225 /* Parse an (optional) nested-name-specifier.
4226
4227    nested-name-specifier: [C++98]
4228      class-or-namespace-name :: nested-name-specifier [opt]
4229      class-or-namespace-name :: template nested-name-specifier [opt]
4230
4231    nested-name-specifier: [C++0x]
4232      type-name ::
4233      namespace-name ::
4234      nested-name-specifier identifier ::
4235      nested-name-specifier template [opt] simple-template-id ::
4236
4237    PARSER->SCOPE should be set appropriately before this function is
4238    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4239    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4240    in name lookups.
4241
4242    Sets PARSER->SCOPE to the class (TYPE) or namespace
4243    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4244    it unchanged if there is no nested-name-specifier.  Returns the new
4245    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4246
4247    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4248    part of a declaration and/or decl-specifier.  */
4249
4250 static tree
4251 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4252                                      bool typename_keyword_p,
4253                                      bool check_dependency_p,
4254                                      bool type_p,
4255                                      bool is_declaration)
4256 {
4257   bool success = false;
4258   cp_token_position start = 0;
4259   cp_token *token;
4260
4261   /* Remember where the nested-name-specifier starts.  */
4262   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4263     {
4264       start = cp_lexer_token_position (parser->lexer, false);
4265       push_deferring_access_checks (dk_deferred);
4266     }
4267
4268   while (true)
4269     {
4270       tree new_scope;
4271       tree old_scope;
4272       tree saved_qualifying_scope;
4273       bool template_keyword_p;
4274
4275       /* Spot cases that cannot be the beginning of a
4276          nested-name-specifier.  */
4277       token = cp_lexer_peek_token (parser->lexer);
4278
4279       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4280          the already parsed nested-name-specifier.  */
4281       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4282         {
4283           /* Grab the nested-name-specifier and continue the loop.  */
4284           cp_parser_pre_parsed_nested_name_specifier (parser);
4285           /* If we originally encountered this nested-name-specifier
4286              with IS_DECLARATION set to false, we will not have
4287              resolved TYPENAME_TYPEs, so we must do so here.  */
4288           if (is_declaration
4289               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4290             {
4291               new_scope = resolve_typename_type (parser->scope,
4292                                                  /*only_current_p=*/false);
4293               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4294                 parser->scope = new_scope;
4295             }
4296           success = true;
4297           continue;
4298         }
4299
4300       /* Spot cases that cannot be the beginning of a
4301          nested-name-specifier.  On the second and subsequent times
4302          through the loop, we look for the `template' keyword.  */
4303       if (success && token->keyword == RID_TEMPLATE)
4304         ;
4305       /* A template-id can start a nested-name-specifier.  */
4306       else if (token->type == CPP_TEMPLATE_ID)
4307         ;
4308       else
4309         {
4310           /* If the next token is not an identifier, then it is
4311              definitely not a type-name or namespace-name.  */
4312           if (token->type != CPP_NAME)
4313             break;
4314           /* If the following token is neither a `<' (to begin a
4315              template-id), nor a `::', then we are not looking at a
4316              nested-name-specifier.  */
4317           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4318
4319           if (token->type == CPP_COLON
4320               && parser->colon_corrects_to_scope_p
4321               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4322             {
4323               error_at (token->location,
4324                         "found %<:%> in nested-name-specifier, expected %<::%>");
4325               token->type = CPP_SCOPE;
4326             }
4327
4328           if (token->type != CPP_SCOPE
4329               && !cp_parser_nth_token_starts_template_argument_list_p
4330                   (parser, 2))
4331             break;
4332         }
4333
4334       /* The nested-name-specifier is optional, so we parse
4335          tentatively.  */
4336       cp_parser_parse_tentatively (parser);
4337
4338       /* Look for the optional `template' keyword, if this isn't the
4339          first time through the loop.  */
4340       if (success)
4341         template_keyword_p = cp_parser_optional_template_keyword (parser);
4342       else
4343         template_keyword_p = false;
4344
4345       /* Save the old scope since the name lookup we are about to do
4346          might destroy it.  */
4347       old_scope = parser->scope;
4348       saved_qualifying_scope = parser->qualifying_scope;
4349       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4350          look up names in "X<T>::I" in order to determine that "Y" is
4351          a template.  So, if we have a typename at this point, we make
4352          an effort to look through it.  */
4353       if (is_declaration
4354           && !typename_keyword_p
4355           && parser->scope
4356           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4357         parser->scope = resolve_typename_type (parser->scope,
4358                                                /*only_current_p=*/false);
4359       /* Parse the qualifying entity.  */
4360       new_scope
4361         = cp_parser_qualifying_entity (parser,
4362                                        typename_keyword_p,
4363                                        template_keyword_p,
4364                                        check_dependency_p,
4365                                        type_p,
4366                                        is_declaration);
4367       /* Look for the `::' token.  */
4368       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4369
4370       /* If we found what we wanted, we keep going; otherwise, we're
4371          done.  */
4372       if (!cp_parser_parse_definitely (parser))
4373         {
4374           bool error_p = false;
4375
4376           /* Restore the OLD_SCOPE since it was valid before the
4377              failed attempt at finding the last
4378              class-or-namespace-name.  */
4379           parser->scope = old_scope;
4380           parser->qualifying_scope = saved_qualifying_scope;
4381           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4382             break;
4383           /* If the next token is an identifier, and the one after
4384              that is a `::', then any valid interpretation would have
4385              found a class-or-namespace-name.  */
4386           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4387                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4388                      == CPP_SCOPE)
4389                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4390                      != CPP_COMPL))
4391             {
4392               token = cp_lexer_consume_token (parser->lexer);
4393               if (!error_p)
4394                 {
4395                   if (!token->ambiguous_p)
4396                     {
4397                       tree decl;
4398                       tree ambiguous_decls;
4399
4400                       decl = cp_parser_lookup_name (parser, token->u.value,
4401                                                     none_type,
4402                                                     /*is_template=*/false,
4403                                                     /*is_namespace=*/false,
4404                                                     /*check_dependency=*/true,
4405                                                     &ambiguous_decls,
4406                                                     token->location);
4407                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4408                         error_at (token->location,
4409                                   "%qD used without template parameters",
4410                                   decl);
4411                       else if (ambiguous_decls)
4412                         {
4413                           error_at (token->location,
4414                                     "reference to %qD is ambiguous",
4415                                     token->u.value);
4416                           print_candidates (ambiguous_decls);
4417                           decl = error_mark_node;
4418                         }
4419                       else
4420                         {
4421                           if (cxx_dialect != cxx98)
4422                             cp_parser_name_lookup_error
4423                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4424                              token->location);
4425                           else
4426                             cp_parser_name_lookup_error
4427                             (parser, token->u.value, decl, NLE_CXX98,
4428                              token->location);
4429                         }
4430                     }
4431                   parser->scope = error_mark_node;
4432                   error_p = true;
4433                   /* Treat this as a successful nested-name-specifier
4434                      due to:
4435
4436                      [basic.lookup.qual]
4437
4438                      If the name found is not a class-name (clause
4439                      _class_) or namespace-name (_namespace.def_), the
4440                      program is ill-formed.  */
4441                   success = true;
4442                 }
4443               cp_lexer_consume_token (parser->lexer);
4444             }
4445           break;
4446         }
4447       /* We've found one valid nested-name-specifier.  */
4448       success = true;
4449       /* Name lookup always gives us a DECL.  */
4450       if (TREE_CODE (new_scope) == TYPE_DECL)
4451         new_scope = TREE_TYPE (new_scope);
4452       /* Uses of "template" must be followed by actual templates.  */
4453       if (template_keyword_p
4454           && !(CLASS_TYPE_P (new_scope)
4455                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4456                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4457                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4458           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4459                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4460                    == TEMPLATE_ID_EXPR)))
4461         permerror (input_location, TYPE_P (new_scope)
4462                    ? "%qT is not a template"
4463                    : "%qD is not a template",
4464                    new_scope);
4465       /* If it is a class scope, try to complete it; we are about to
4466          be looking up names inside the class.  */
4467       if (TYPE_P (new_scope)
4468           /* Since checking types for dependency can be expensive,
4469              avoid doing it if the type is already complete.  */
4470           && !COMPLETE_TYPE_P (new_scope)
4471           /* Do not try to complete dependent types.  */
4472           && !dependent_type_p (new_scope))
4473         {
4474           new_scope = complete_type (new_scope);
4475           /* If it is a typedef to current class, use the current
4476              class instead, as the typedef won't have any names inside
4477              it yet.  */
4478           if (!COMPLETE_TYPE_P (new_scope)
4479               && currently_open_class (new_scope))
4480             new_scope = TYPE_MAIN_VARIANT (new_scope);
4481         }
4482       /* Make sure we look in the right scope the next time through
4483          the loop.  */
4484       parser->scope = new_scope;
4485     }
4486
4487   /* If parsing tentatively, replace the sequence of tokens that makes
4488      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4489      token.  That way, should we re-parse the token stream, we will
4490      not have to repeat the effort required to do the parse, nor will
4491      we issue duplicate error messages.  */
4492   if (success && start)
4493     {
4494       cp_token *token;
4495
4496       token = cp_lexer_token_at (parser->lexer, start);
4497       /* Reset the contents of the START token.  */
4498       token->type = CPP_NESTED_NAME_SPECIFIER;
4499       /* Retrieve any deferred checks.  Do not pop this access checks yet
4500          so the memory will not be reclaimed during token replacing below.  */
4501       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4502       token->u.tree_check_value->value = parser->scope;
4503       token->u.tree_check_value->checks = get_deferred_access_checks ();
4504       token->u.tree_check_value->qualifying_scope =
4505         parser->qualifying_scope;
4506       token->keyword = RID_MAX;
4507
4508       /* Purge all subsequent tokens.  */
4509       cp_lexer_purge_tokens_after (parser->lexer, start);
4510     }
4511
4512   if (start)
4513     pop_to_parent_deferring_access_checks ();
4514
4515   return success ? parser->scope : NULL_TREE;
4516 }
4517
4518 /* Parse a nested-name-specifier.  See
4519    cp_parser_nested_name_specifier_opt for details.  This function
4520    behaves identically, except that it will an issue an error if no
4521    nested-name-specifier is present.  */
4522
4523 static tree
4524 cp_parser_nested_name_specifier (cp_parser *parser,
4525                                  bool typename_keyword_p,
4526                                  bool check_dependency_p,
4527                                  bool type_p,
4528                                  bool is_declaration)
4529 {
4530   tree scope;
4531
4532   /* Look for the nested-name-specifier.  */
4533   scope = cp_parser_nested_name_specifier_opt (parser,
4534                                                typename_keyword_p,
4535                                                check_dependency_p,
4536                                                type_p,
4537                                                is_declaration);
4538   /* If it was not present, issue an error message.  */
4539   if (!scope)
4540     {
4541       cp_parser_error (parser, "expected nested-name-specifier");
4542       parser->scope = NULL_TREE;
4543     }
4544
4545   return scope;
4546 }
4547
4548 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4549    this is either a class-name or a namespace-name (which corresponds
4550    to the class-or-namespace-name production in the grammar). For
4551    C++0x, it can also be a type-name that refers to an enumeration
4552    type.
4553
4554    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4555    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4556    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4557    TYPE_P is TRUE iff the next name should be taken as a class-name,
4558    even the same name is declared to be another entity in the same
4559    scope.
4560
4561    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4562    specified by the class-or-namespace-name.  If neither is found the
4563    ERROR_MARK_NODE is returned.  */
4564
4565 static tree
4566 cp_parser_qualifying_entity (cp_parser *parser,
4567                              bool typename_keyword_p,
4568                              bool template_keyword_p,
4569                              bool check_dependency_p,
4570                              bool type_p,
4571                              bool is_declaration)
4572 {
4573   tree saved_scope;
4574   tree saved_qualifying_scope;
4575   tree saved_object_scope;
4576   tree scope;
4577   bool only_class_p;
4578   bool successful_parse_p;
4579
4580   /* Before we try to parse the class-name, we must save away the
4581      current PARSER->SCOPE since cp_parser_class_name will destroy
4582      it.  */
4583   saved_scope = parser->scope;
4584   saved_qualifying_scope = parser->qualifying_scope;
4585   saved_object_scope = parser->object_scope;
4586   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4587      there is no need to look for a namespace-name.  */
4588   only_class_p = template_keyword_p 
4589     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4590   if (!only_class_p)
4591     cp_parser_parse_tentatively (parser);
4592   scope = cp_parser_class_name (parser,
4593                                 typename_keyword_p,
4594                                 template_keyword_p,
4595                                 type_p ? class_type : none_type,
4596                                 check_dependency_p,
4597                                 /*class_head_p=*/false,
4598                                 is_declaration);
4599   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4600   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4601   if (!only_class_p 
4602       && cxx_dialect != cxx98
4603       && !successful_parse_p)
4604     {
4605       /* Restore the saved scope.  */
4606       parser->scope = saved_scope;
4607       parser->qualifying_scope = saved_qualifying_scope;
4608       parser->object_scope = saved_object_scope;
4609
4610       /* Parse tentatively.  */
4611       cp_parser_parse_tentatively (parser);
4612      
4613       /* Parse a typedef-name or enum-name.  */
4614       scope = cp_parser_nonclass_name (parser);
4615
4616       /* "If the name found does not designate a namespace or a class,
4617          enumeration, or dependent type, the program is ill-formed."
4618
4619          We cover classes and dependent types above and namespaces below,
4620          so this code is only looking for enums.  */
4621       if (!scope || TREE_CODE (scope) != TYPE_DECL
4622           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4623         cp_parser_simulate_error (parser);
4624
4625       successful_parse_p = cp_parser_parse_definitely (parser);
4626     }
4627   /* If that didn't work, try for a namespace-name.  */
4628   if (!only_class_p && !successful_parse_p)
4629     {
4630       /* Restore the saved scope.  */
4631       parser->scope = saved_scope;
4632       parser->qualifying_scope = saved_qualifying_scope;
4633       parser->object_scope = saved_object_scope;
4634       /* If we are not looking at an identifier followed by the scope
4635          resolution operator, then this is not part of a
4636          nested-name-specifier.  (Note that this function is only used
4637          to parse the components of a nested-name-specifier.)  */
4638       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4639           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4640         return error_mark_node;
4641       scope = cp_parser_namespace_name (parser);
4642     }
4643
4644   return scope;
4645 }
4646
4647 /* Parse a postfix-expression.
4648
4649    postfix-expression:
4650      primary-expression
4651      postfix-expression [ expression ]
4652      postfix-expression ( expression-list [opt] )
4653      simple-type-specifier ( expression-list [opt] )
4654      typename :: [opt] nested-name-specifier identifier
4655        ( expression-list [opt] )
4656      typename :: [opt] nested-name-specifier template [opt] template-id
4657        ( expression-list [opt] )
4658      postfix-expression . template [opt] id-expression
4659      postfix-expression -> template [opt] id-expression
4660      postfix-expression . pseudo-destructor-name
4661      postfix-expression -> pseudo-destructor-name
4662      postfix-expression ++
4663      postfix-expression --
4664      dynamic_cast < type-id > ( expression )
4665      static_cast < type-id > ( expression )
4666      reinterpret_cast < type-id > ( expression )
4667      const_cast < type-id > ( expression )
4668      typeid ( expression )
4669      typeid ( type-id )
4670
4671    GNU Extension:
4672
4673    postfix-expression:
4674      ( type-id ) { initializer-list , [opt] }
4675
4676    This extension is a GNU version of the C99 compound-literal
4677    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4678    but they are essentially the same concept.)
4679
4680    If ADDRESS_P is true, the postfix expression is the operand of the
4681    `&' operator.  CAST_P is true if this expression is the target of a
4682    cast.
4683
4684    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4685    class member access expressions [expr.ref].
4686
4687    Returns a representation of the expression.  */
4688
4689 static tree
4690 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4691                               bool member_access_only_p,
4692                               cp_id_kind * pidk_return)
4693 {
4694   cp_token *token;
4695   enum rid keyword;
4696   cp_id_kind idk = CP_ID_KIND_NONE;
4697   tree postfix_expression = NULL_TREE;
4698   bool is_member_access = false;
4699
4700   /* Peek at the next token.  */
4701   token = cp_lexer_peek_token (parser->lexer);
4702   /* Some of the productions are determined by keywords.  */
4703   keyword = token->keyword;
4704   switch (keyword)
4705     {
4706     case RID_DYNCAST:
4707     case RID_STATCAST:
4708     case RID_REINTCAST:
4709     case RID_CONSTCAST:
4710       {
4711         tree type;
4712         tree expression;
4713         const char *saved_message;
4714
4715         /* All of these can be handled in the same way from the point
4716            of view of parsing.  Begin by consuming the token
4717            identifying the cast.  */
4718         cp_lexer_consume_token (parser->lexer);
4719
4720         /* New types cannot be defined in the cast.  */
4721         saved_message = parser->type_definition_forbidden_message;
4722         parser->type_definition_forbidden_message
4723           = G_("types may not be defined in casts");
4724
4725         /* Look for the opening `<'.  */
4726         cp_parser_require (parser, CPP_LESS, RT_LESS);
4727         /* Parse the type to which we are casting.  */
4728         type = cp_parser_type_id (parser);
4729         /* Look for the closing `>'.  */
4730         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4731         /* Restore the old message.  */
4732         parser->type_definition_forbidden_message = saved_message;
4733
4734         /* And the expression which is being cast.  */
4735         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4736         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4737         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4738
4739         /* Only type conversions to integral or enumeration types
4740            can be used in constant-expressions.  */
4741         if (!cast_valid_in_integral_constant_expression_p (type)
4742             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4743           return error_mark_node;
4744
4745         switch (keyword)
4746           {
4747           case RID_DYNCAST:
4748             postfix_expression
4749               = build_dynamic_cast (type, expression, tf_warning_or_error);
4750             break;
4751           case RID_STATCAST:
4752             postfix_expression
4753               = build_static_cast (type, expression, tf_warning_or_error);
4754             break;
4755           case RID_REINTCAST:
4756             postfix_expression
4757               = build_reinterpret_cast (type, expression, 
4758                                         tf_warning_or_error);
4759             break;
4760           case RID_CONSTCAST:
4761             postfix_expression
4762               = build_const_cast (type, expression, tf_warning_or_error);
4763             break;
4764           default:
4765             gcc_unreachable ();
4766           }
4767       }
4768       break;
4769
4770     case RID_TYPEID:
4771       {
4772         tree type;
4773         const char *saved_message;
4774         bool saved_in_type_id_in_expr_p;
4775
4776         /* Consume the `typeid' token.  */
4777         cp_lexer_consume_token (parser->lexer);
4778         /* Look for the `(' token.  */
4779         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4780         /* Types cannot be defined in a `typeid' expression.  */
4781         saved_message = parser->type_definition_forbidden_message;
4782         parser->type_definition_forbidden_message
4783           = G_("types may not be defined in a %<typeid%> expression");
4784         /* We can't be sure yet whether we're looking at a type-id or an
4785            expression.  */
4786         cp_parser_parse_tentatively (parser);
4787         /* Try a type-id first.  */
4788         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4789         parser->in_type_id_in_expr_p = true;
4790         type = cp_parser_type_id (parser);
4791         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4792         /* Look for the `)' token.  Otherwise, we can't be sure that
4793            we're not looking at an expression: consider `typeid (int
4794            (3))', for example.  */
4795         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4796         /* If all went well, simply lookup the type-id.  */
4797         if (cp_parser_parse_definitely (parser))
4798           postfix_expression = get_typeid (type);
4799         /* Otherwise, fall back to the expression variant.  */
4800         else
4801           {
4802             tree expression;
4803
4804             /* Look for an expression.  */
4805             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4806             /* Compute its typeid.  */
4807             postfix_expression = build_typeid (expression);
4808             /* Look for the `)' token.  */
4809             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4810           }
4811         /* Restore the saved message.  */
4812         parser->type_definition_forbidden_message = saved_message;
4813         /* `typeid' may not appear in an integral constant expression.  */
4814         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4815           return error_mark_node;
4816       }
4817       break;
4818
4819     case RID_TYPENAME:
4820       {
4821         tree type;
4822         /* The syntax permitted here is the same permitted for an
4823            elaborated-type-specifier.  */
4824         type = cp_parser_elaborated_type_specifier (parser,
4825                                                     /*is_friend=*/false,
4826                                                     /*is_declaration=*/false);
4827         postfix_expression = cp_parser_functional_cast (parser, type);
4828       }
4829       break;
4830
4831     default:
4832       {
4833         tree type;
4834
4835         /* If the next thing is a simple-type-specifier, we may be
4836            looking at a functional cast.  We could also be looking at
4837            an id-expression.  So, we try the functional cast, and if
4838            that doesn't work we fall back to the primary-expression.  */
4839         cp_parser_parse_tentatively (parser);
4840         /* Look for the simple-type-specifier.  */
4841         type = cp_parser_simple_type_specifier (parser,
4842                                                 /*decl_specs=*/NULL,
4843                                                 CP_PARSER_FLAGS_NONE);
4844         /* Parse the cast itself.  */
4845         if (!cp_parser_error_occurred (parser))
4846           postfix_expression
4847             = cp_parser_functional_cast (parser, type);
4848         /* If that worked, we're done.  */
4849         if (cp_parser_parse_definitely (parser))
4850           break;
4851
4852         /* If the functional-cast didn't work out, try a
4853            compound-literal.  */
4854         if (cp_parser_allow_gnu_extensions_p (parser)
4855             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4856           {
4857             VEC(constructor_elt,gc) *initializer_list = NULL;
4858             bool saved_in_type_id_in_expr_p;
4859
4860             cp_parser_parse_tentatively (parser);
4861             /* Consume the `('.  */
4862             cp_lexer_consume_token (parser->lexer);
4863             /* Parse the type.  */
4864             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4865             parser->in_type_id_in_expr_p = true;
4866             type = cp_parser_type_id (parser);
4867             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4868             /* Look for the `)'.  */
4869             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4870             /* Look for the `{'.  */
4871             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4872             /* If things aren't going well, there's no need to
4873                keep going.  */
4874             if (!cp_parser_error_occurred (parser))
4875               {
4876                 bool non_constant_p;
4877                 /* Parse the initializer-list.  */
4878                 initializer_list
4879                   = cp_parser_initializer_list (parser, &non_constant_p);
4880                 /* Allow a trailing `,'.  */
4881                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4882                   cp_lexer_consume_token (parser->lexer);
4883                 /* Look for the final `}'.  */
4884                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4885               }
4886             /* If that worked, we're definitely looking at a
4887                compound-literal expression.  */
4888             if (cp_parser_parse_definitely (parser))
4889               {
4890                 /* Warn the user that a compound literal is not
4891                    allowed in standard C++.  */
4892                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4893                 /* For simplicity, we disallow compound literals in
4894                    constant-expressions.  We could
4895                    allow compound literals of integer type, whose
4896                    initializer was a constant, in constant
4897                    expressions.  Permitting that usage, as a further
4898                    extension, would not change the meaning of any
4899                    currently accepted programs.  (Of course, as
4900                    compound literals are not part of ISO C++, the
4901                    standard has nothing to say.)  */
4902                 if (cp_parser_non_integral_constant_expression (parser,
4903                                                                 NIC_NCC))
4904                   {
4905                     postfix_expression = error_mark_node;
4906                     break;
4907                   }
4908                 /* Form the representation of the compound-literal.  */
4909                 postfix_expression
4910                   = (finish_compound_literal
4911                      (type, build_constructor (init_list_type_node,
4912                                                initializer_list),
4913                       tf_warning_or_error));
4914                 break;
4915               }
4916           }
4917
4918         /* It must be a primary-expression.  */
4919         postfix_expression
4920           = cp_parser_primary_expression (parser, address_p, cast_p,
4921                                           /*template_arg_p=*/false,
4922                                           &idk);
4923       }
4924       break;
4925     }
4926
4927   /* Keep looping until the postfix-expression is complete.  */
4928   while (true)
4929     {
4930       if (idk == CP_ID_KIND_UNQUALIFIED
4931           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4932           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4933         /* It is not a Koenig lookup function call.  */
4934         postfix_expression
4935           = unqualified_name_lookup_error (postfix_expression);
4936
4937       /* Peek at the next token.  */
4938       token = cp_lexer_peek_token (parser->lexer);
4939
4940       switch (token->type)
4941         {
4942         case CPP_OPEN_SQUARE:
4943           postfix_expression
4944             = cp_parser_postfix_open_square_expression (parser,
4945                                                         postfix_expression,
4946                                                         false);
4947           idk = CP_ID_KIND_NONE;
4948           is_member_access = false;
4949           break;
4950
4951         case CPP_OPEN_PAREN:
4952           /* postfix-expression ( expression-list [opt] ) */
4953           {
4954             bool koenig_p;
4955             bool is_builtin_constant_p;
4956             bool saved_integral_constant_expression_p = false;
4957             bool saved_non_integral_constant_expression_p = false;
4958             VEC(tree,gc) *args;
4959
4960             is_member_access = false;
4961
4962             is_builtin_constant_p
4963               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4964             if (is_builtin_constant_p)
4965               {
4966                 /* The whole point of __builtin_constant_p is to allow
4967                    non-constant expressions to appear as arguments.  */
4968                 saved_integral_constant_expression_p
4969                   = parser->integral_constant_expression_p;
4970                 saved_non_integral_constant_expression_p
4971                   = parser->non_integral_constant_expression_p;
4972                 parser->integral_constant_expression_p = false;
4973               }
4974             args = (cp_parser_parenthesized_expression_list
4975                     (parser, non_attr,
4976                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4977                      /*non_constant_p=*/NULL));
4978             if (is_builtin_constant_p)
4979               {
4980                 parser->integral_constant_expression_p
4981                   = saved_integral_constant_expression_p;
4982                 parser->non_integral_constant_expression_p
4983                   = saved_non_integral_constant_expression_p;
4984               }
4985
4986             if (args == NULL)
4987               {
4988                 postfix_expression = error_mark_node;
4989                 break;
4990               }
4991
4992             /* Function calls are not permitted in
4993                constant-expressions.  */
4994             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4995                 && cp_parser_non_integral_constant_expression (parser,
4996                                                                NIC_FUNC_CALL))
4997               {
4998                 postfix_expression = error_mark_node;
4999                 release_tree_vector (args);
5000                 break;
5001               }
5002
5003             koenig_p = false;
5004             if (idk == CP_ID_KIND_UNQUALIFIED
5005                 || idk == CP_ID_KIND_TEMPLATE_ID)
5006               {
5007                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5008                   {
5009                     if (!VEC_empty (tree, args))
5010                       {
5011                         koenig_p = true;
5012                         if (!any_type_dependent_arguments_p (args))
5013                           postfix_expression
5014                             = perform_koenig_lookup (postfix_expression, args,
5015                                                      /*include_std=*/false);
5016                       }
5017                     else
5018                       postfix_expression
5019                         = unqualified_fn_lookup_error (postfix_expression);
5020                   }
5021                 /* We do not perform argument-dependent lookup if
5022                    normal lookup finds a non-function, in accordance
5023                    with the expected resolution of DR 218.  */
5024                 else if (!VEC_empty (tree, args)
5025                          && is_overloaded_fn (postfix_expression))
5026                   {
5027                     tree fn = get_first_fn (postfix_expression);
5028                     fn = STRIP_TEMPLATE (fn);
5029
5030                     /* Do not do argument dependent lookup if regular
5031                        lookup finds a member function or a block-scope
5032                        function declaration.  [basic.lookup.argdep]/3  */
5033                     if (!DECL_FUNCTION_MEMBER_P (fn)
5034                         && !DECL_LOCAL_FUNCTION_P (fn))
5035                       {
5036                         koenig_p = true;
5037                         if (!any_type_dependent_arguments_p (args))
5038                           postfix_expression
5039                             = perform_koenig_lookup (postfix_expression, args,
5040                                                      /*include_std=*/false);
5041                       }
5042                   }
5043               }
5044
5045             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5046               {
5047                 tree instance = TREE_OPERAND (postfix_expression, 0);
5048                 tree fn = TREE_OPERAND (postfix_expression, 1);
5049
5050                 if (processing_template_decl
5051                     && (type_dependent_expression_p (instance)
5052                         || (!BASELINK_P (fn)
5053                             && TREE_CODE (fn) != FIELD_DECL)
5054                         || type_dependent_expression_p (fn)
5055                         || any_type_dependent_arguments_p (args)))
5056                   {
5057                     postfix_expression
5058                       = build_nt_call_vec (postfix_expression, args);
5059                     release_tree_vector (args);
5060                     break;
5061                   }
5062
5063                 if (BASELINK_P (fn))
5064                   {
5065                   postfix_expression
5066                     = (build_new_method_call
5067                        (instance, fn, &args, NULL_TREE,
5068                         (idk == CP_ID_KIND_QUALIFIED
5069                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5070                          : LOOKUP_NORMAL),
5071                         /*fn_p=*/NULL,
5072                         tf_warning_or_error));
5073                   }
5074                 else
5075                   postfix_expression
5076                     = finish_call_expr (postfix_expression, &args,
5077                                         /*disallow_virtual=*/false,
5078                                         /*koenig_p=*/false,
5079                                         tf_warning_or_error);
5080               }
5081             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5082                      || TREE_CODE (postfix_expression) == MEMBER_REF
5083                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5084               postfix_expression = (build_offset_ref_call_from_tree
5085                                     (postfix_expression, &args));
5086             else if (idk == CP_ID_KIND_QUALIFIED)
5087               /* A call to a static class member, or a namespace-scope
5088                  function.  */
5089               postfix_expression
5090                 = finish_call_expr (postfix_expression, &args,
5091                                     /*disallow_virtual=*/true,
5092                                     koenig_p,
5093                                     tf_warning_or_error);
5094             else
5095               /* All other function calls.  */
5096               postfix_expression
5097                 = finish_call_expr (postfix_expression, &args,
5098                                     /*disallow_virtual=*/false,
5099                                     koenig_p,
5100                                     tf_warning_or_error);
5101
5102             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5103             idk = CP_ID_KIND_NONE;
5104
5105             release_tree_vector (args);
5106           }
5107           break;
5108
5109         case CPP_DOT:
5110         case CPP_DEREF:
5111           /* postfix-expression . template [opt] id-expression
5112              postfix-expression . pseudo-destructor-name
5113              postfix-expression -> template [opt] id-expression
5114              postfix-expression -> pseudo-destructor-name */
5115
5116           /* Consume the `.' or `->' operator.  */
5117           cp_lexer_consume_token (parser->lexer);
5118
5119           postfix_expression
5120             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5121                                                       postfix_expression,
5122                                                       false, &idk,
5123                                                       token->location);
5124
5125           is_member_access = true;
5126           break;
5127
5128         case CPP_PLUS_PLUS:
5129           /* postfix-expression ++  */
5130           /* Consume the `++' token.  */
5131           cp_lexer_consume_token (parser->lexer);
5132           /* Generate a representation for the complete expression.  */
5133           postfix_expression
5134             = finish_increment_expr (postfix_expression,
5135                                      POSTINCREMENT_EXPR);
5136           /* Increments may not appear in constant-expressions.  */
5137           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5138             postfix_expression = error_mark_node;
5139           idk = CP_ID_KIND_NONE;
5140           is_member_access = false;
5141           break;
5142
5143         case CPP_MINUS_MINUS:
5144           /* postfix-expression -- */
5145           /* Consume the `--' token.  */
5146           cp_lexer_consume_token (parser->lexer);
5147           /* Generate a representation for the complete expression.  */
5148           postfix_expression
5149             = finish_increment_expr (postfix_expression,
5150                                      POSTDECREMENT_EXPR);
5151           /* Decrements may not appear in constant-expressions.  */
5152           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5153             postfix_expression = error_mark_node;
5154           idk = CP_ID_KIND_NONE;
5155           is_member_access = false;
5156           break;
5157
5158         default:
5159           if (pidk_return != NULL)
5160             * pidk_return = idk;
5161           if (member_access_only_p)
5162             return is_member_access? postfix_expression : error_mark_node;
5163           else
5164             return postfix_expression;
5165         }
5166     }
5167
5168   /* We should never get here.  */
5169   gcc_unreachable ();
5170   return error_mark_node;
5171 }
5172
5173 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5174    by cp_parser_builtin_offsetof.  We're looking for
5175
5176      postfix-expression [ expression ]
5177
5178    FOR_OFFSETOF is set if we're being called in that context, which
5179    changes how we deal with integer constant expressions.  */
5180
5181 static tree
5182 cp_parser_postfix_open_square_expression (cp_parser *parser,
5183                                           tree postfix_expression,
5184                                           bool for_offsetof)
5185 {
5186   tree index;
5187
5188   /* Consume the `[' token.  */
5189   cp_lexer_consume_token (parser->lexer);
5190
5191   /* Parse the index expression.  */
5192   /* ??? For offsetof, there is a question of what to allow here.  If
5193      offsetof is not being used in an integral constant expression context,
5194      then we *could* get the right answer by computing the value at runtime.
5195      If we are in an integral constant expression context, then we might
5196      could accept any constant expression; hard to say without analysis.
5197      Rather than open the barn door too wide right away, allow only integer
5198      constant expressions here.  */
5199   if (for_offsetof)
5200     index = cp_parser_constant_expression (parser, false, NULL);
5201   else
5202     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5203
5204   /* Look for the closing `]'.  */
5205   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5206
5207   /* Build the ARRAY_REF.  */
5208   postfix_expression = grok_array_decl (postfix_expression, index);
5209
5210   /* When not doing offsetof, array references are not permitted in
5211      constant-expressions.  */
5212   if (!for_offsetof
5213       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5214     postfix_expression = error_mark_node;
5215
5216   return postfix_expression;
5217 }
5218
5219 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5220    by cp_parser_builtin_offsetof.  We're looking for
5221
5222      postfix-expression . template [opt] id-expression
5223      postfix-expression . pseudo-destructor-name
5224      postfix-expression -> template [opt] id-expression
5225      postfix-expression -> pseudo-destructor-name
5226
5227    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5228    limits what of the above we'll actually accept, but nevermind.
5229    TOKEN_TYPE is the "." or "->" token, which will already have been
5230    removed from the stream.  */
5231
5232 static tree
5233 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5234                                         enum cpp_ttype token_type,
5235                                         tree postfix_expression,
5236                                         bool for_offsetof, cp_id_kind *idk,
5237                                         location_t location)
5238 {
5239   tree name;
5240   bool dependent_p;
5241   bool pseudo_destructor_p;
5242   tree scope = NULL_TREE;
5243
5244   /* If this is a `->' operator, dereference the pointer.  */
5245   if (token_type == CPP_DEREF)
5246     postfix_expression = build_x_arrow (postfix_expression);
5247   /* Check to see whether or not the expression is type-dependent.  */
5248   dependent_p = type_dependent_expression_p (postfix_expression);
5249   /* The identifier following the `->' or `.' is not qualified.  */
5250   parser->scope = NULL_TREE;
5251   parser->qualifying_scope = NULL_TREE;
5252   parser->object_scope = NULL_TREE;
5253   *idk = CP_ID_KIND_NONE;
5254
5255   /* Enter the scope corresponding to the type of the object
5256      given by the POSTFIX_EXPRESSION.  */
5257   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5258     {
5259       scope = TREE_TYPE (postfix_expression);
5260       /* According to the standard, no expression should ever have
5261          reference type.  Unfortunately, we do not currently match
5262          the standard in this respect in that our internal representation
5263          of an expression may have reference type even when the standard
5264          says it does not.  Therefore, we have to manually obtain the
5265          underlying type here.  */
5266       scope = non_reference (scope);
5267       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5268       if (scope == unknown_type_node)
5269         {
5270           error_at (location, "%qE does not have class type",
5271                     postfix_expression);
5272           scope = NULL_TREE;
5273         }
5274       else
5275         scope = complete_type_or_else (scope, NULL_TREE);
5276       /* Let the name lookup machinery know that we are processing a
5277          class member access expression.  */
5278       parser->context->object_type = scope;
5279       /* If something went wrong, we want to be able to discern that case,
5280          as opposed to the case where there was no SCOPE due to the type
5281          of expression being dependent.  */
5282       if (!scope)
5283         scope = error_mark_node;
5284       /* If the SCOPE was erroneous, make the various semantic analysis
5285          functions exit quickly -- and without issuing additional error
5286          messages.  */
5287       if (scope == error_mark_node)
5288         postfix_expression = error_mark_node;
5289     }
5290
5291   /* Assume this expression is not a pseudo-destructor access.  */
5292   pseudo_destructor_p = false;
5293
5294   /* If the SCOPE is a scalar type, then, if this is a valid program,
5295      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5296      is type dependent, it can be pseudo-destructor-name or something else.
5297      Try to parse it as pseudo-destructor-name first.  */
5298   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5299     {
5300       tree s;
5301       tree type;
5302
5303       cp_parser_parse_tentatively (parser);
5304       /* Parse the pseudo-destructor-name.  */
5305       s = NULL_TREE;
5306       cp_parser_pseudo_destructor_name (parser, &s, &type);
5307       if (dependent_p
5308           && (cp_parser_error_occurred (parser)
5309               || TREE_CODE (type) != TYPE_DECL
5310               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5311         cp_parser_abort_tentative_parse (parser);
5312       else if (cp_parser_parse_definitely (parser))
5313         {
5314           pseudo_destructor_p = true;
5315           postfix_expression
5316             = finish_pseudo_destructor_expr (postfix_expression,
5317                                              s, TREE_TYPE (type));
5318         }
5319     }
5320
5321   if (!pseudo_destructor_p)
5322     {
5323       /* If the SCOPE is not a scalar type, we are looking at an
5324          ordinary class member access expression, rather than a
5325          pseudo-destructor-name.  */
5326       bool template_p;
5327       cp_token *token = cp_lexer_peek_token (parser->lexer);
5328       /* Parse the id-expression.  */
5329       name = (cp_parser_id_expression
5330               (parser,
5331                cp_parser_optional_template_keyword (parser),
5332                /*check_dependency_p=*/true,
5333                &template_p,
5334                /*declarator_p=*/false,
5335                /*optional_p=*/false));
5336       /* In general, build a SCOPE_REF if the member name is qualified.
5337          However, if the name was not dependent and has already been
5338          resolved; there is no need to build the SCOPE_REF.  For example;
5339
5340              struct X { void f(); };
5341              template <typename T> void f(T* t) { t->X::f(); }
5342
5343          Even though "t" is dependent, "X::f" is not and has been resolved
5344          to a BASELINK; there is no need to include scope information.  */
5345
5346       /* But we do need to remember that there was an explicit scope for
5347          virtual function calls.  */
5348       if (parser->scope)
5349         *idk = CP_ID_KIND_QUALIFIED;
5350
5351       /* If the name is a template-id that names a type, we will get a
5352          TYPE_DECL here.  That is invalid code.  */
5353       if (TREE_CODE (name) == TYPE_DECL)
5354         {
5355           error_at (token->location, "invalid use of %qD", name);
5356           postfix_expression = error_mark_node;
5357         }
5358       else
5359         {
5360           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5361             {
5362               name = build_qualified_name (/*type=*/NULL_TREE,
5363                                            parser->scope,
5364                                            name,
5365                                            template_p);
5366               parser->scope = NULL_TREE;
5367               parser->qualifying_scope = NULL_TREE;
5368               parser->object_scope = NULL_TREE;
5369             }
5370           if (scope && name && BASELINK_P (name))
5371             adjust_result_of_qualified_name_lookup
5372               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5373           postfix_expression
5374             = finish_class_member_access_expr (postfix_expression, name,
5375                                                template_p, 
5376                                                tf_warning_or_error);
5377         }
5378     }
5379
5380   /* We no longer need to look up names in the scope of the object on
5381      the left-hand side of the `.' or `->' operator.  */
5382   parser->context->object_type = NULL_TREE;
5383
5384   /* Outside of offsetof, these operators may not appear in
5385      constant-expressions.  */
5386   if (!for_offsetof
5387       && (cp_parser_non_integral_constant_expression
5388           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5389     postfix_expression = error_mark_node;
5390
5391   return postfix_expression;
5392 }
5393
5394 /* Parse a parenthesized expression-list.
5395
5396    expression-list:
5397      assignment-expression
5398      expression-list, assignment-expression
5399
5400    attribute-list:
5401      expression-list
5402      identifier
5403      identifier, expression-list
5404
5405    CAST_P is true if this expression is the target of a cast.
5406
5407    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5408    argument pack.
5409
5410    Returns a vector of trees.  Each element is a representation of an
5411    assignment-expression.  NULL is returned if the ( and or ) are
5412    missing.  An empty, but allocated, vector is returned on no
5413    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5414    if we are parsing an attribute list for an attribute that wants a
5415    plain identifier argument, normal_attr for an attribute that wants
5416    an expression, or non_attr if we aren't parsing an attribute list.  If
5417    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5418    not all of the expressions in the list were constant.  */
5419
5420 static VEC(tree,gc) *
5421 cp_parser_parenthesized_expression_list (cp_parser* parser,
5422                                          int is_attribute_list,
5423                                          bool cast_p,
5424                                          bool allow_expansion_p,
5425                                          bool *non_constant_p)
5426 {
5427   VEC(tree,gc) *expression_list;
5428   bool fold_expr_p = is_attribute_list != non_attr;
5429   tree identifier = NULL_TREE;
5430   bool saved_greater_than_is_operator_p;
5431
5432   /* Assume all the expressions will be constant.  */
5433   if (non_constant_p)
5434     *non_constant_p = false;
5435
5436   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5437     return NULL;
5438
5439   expression_list = make_tree_vector ();
5440
5441   /* Within a parenthesized expression, a `>' token is always
5442      the greater-than operator.  */
5443   saved_greater_than_is_operator_p
5444     = parser->greater_than_is_operator_p;
5445   parser->greater_than_is_operator_p = true;
5446
5447   /* Consume expressions until there are no more.  */
5448   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5449     while (true)
5450       {
5451         tree expr;
5452
5453         /* At the beginning of attribute lists, check to see if the
5454            next token is an identifier.  */
5455         if (is_attribute_list == id_attr
5456             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5457           {
5458             cp_token *token;
5459
5460             /* Consume the identifier.  */
5461             token = cp_lexer_consume_token (parser->lexer);
5462             /* Save the identifier.  */
5463             identifier = token->u.value;
5464           }
5465         else
5466           {
5467             bool expr_non_constant_p;
5468
5469             /* Parse the next assignment-expression.  */
5470             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5471               {
5472                 /* A braced-init-list.  */
5473                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5474                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5475                 if (non_constant_p && expr_non_constant_p)
5476                   *non_constant_p = true;
5477               }
5478             else if (non_constant_p)
5479               {
5480                 expr = (cp_parser_constant_expression
5481                         (parser, /*allow_non_constant_p=*/true,
5482                          &expr_non_constant_p));
5483                 if (expr_non_constant_p)
5484                   *non_constant_p = true;
5485               }
5486             else
5487               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5488
5489             if (fold_expr_p)
5490               expr = fold_non_dependent_expr (expr);
5491
5492             /* If we have an ellipsis, then this is an expression
5493                expansion.  */
5494             if (allow_expansion_p
5495                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5496               {
5497                 /* Consume the `...'.  */
5498                 cp_lexer_consume_token (parser->lexer);
5499
5500                 /* Build the argument pack.  */
5501                 expr = make_pack_expansion (expr);
5502               }
5503
5504              /* Add it to the list.  We add error_mark_node
5505                 expressions to the list, so that we can still tell if
5506                 the correct form for a parenthesized expression-list
5507                 is found. That gives better errors.  */
5508             VEC_safe_push (tree, gc, expression_list, expr);
5509
5510             if (expr == error_mark_node)
5511               goto skip_comma;
5512           }
5513
5514         /* After the first item, attribute lists look the same as
5515            expression lists.  */
5516         is_attribute_list = non_attr;
5517
5518       get_comma:;
5519         /* If the next token isn't a `,', then we are done.  */
5520         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5521           break;
5522
5523         /* Otherwise, consume the `,' and keep going.  */
5524         cp_lexer_consume_token (parser->lexer);
5525       }
5526
5527   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5528     {
5529       int ending;
5530
5531     skip_comma:;
5532       /* We try and resync to an unnested comma, as that will give the
5533          user better diagnostics.  */
5534       ending = cp_parser_skip_to_closing_parenthesis (parser,
5535                                                       /*recovering=*/true,
5536                                                       /*or_comma=*/true,
5537                                                       /*consume_paren=*/true);
5538       if (ending < 0)
5539         goto get_comma;
5540       if (!ending)
5541         {
5542           parser->greater_than_is_operator_p
5543             = saved_greater_than_is_operator_p;
5544           return NULL;
5545         }
5546     }
5547
5548   parser->greater_than_is_operator_p
5549     = saved_greater_than_is_operator_p;
5550
5551   if (identifier)
5552     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5553
5554   return expression_list;
5555 }
5556
5557 /* Parse a pseudo-destructor-name.
5558
5559    pseudo-destructor-name:
5560      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5561      :: [opt] nested-name-specifier template template-id :: ~ type-name
5562      :: [opt] nested-name-specifier [opt] ~ type-name
5563
5564    If either of the first two productions is used, sets *SCOPE to the
5565    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5566    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5567    or ERROR_MARK_NODE if the parse fails.  */
5568
5569 static void
5570 cp_parser_pseudo_destructor_name (cp_parser* parser,
5571                                   tree* scope,
5572                                   tree* type)
5573 {
5574   bool nested_name_specifier_p;
5575
5576   /* Assume that things will not work out.  */
5577   *type = error_mark_node;
5578
5579   /* Look for the optional `::' operator.  */
5580   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5581   /* Look for the optional nested-name-specifier.  */
5582   nested_name_specifier_p
5583     = (cp_parser_nested_name_specifier_opt (parser,
5584                                             /*typename_keyword_p=*/false,
5585                                             /*check_dependency_p=*/true,
5586                                             /*type_p=*/false,
5587                                             /*is_declaration=*/false)
5588        != NULL_TREE);
5589   /* Now, if we saw a nested-name-specifier, we might be doing the
5590      second production.  */
5591   if (nested_name_specifier_p
5592       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5593     {
5594       /* Consume the `template' keyword.  */
5595       cp_lexer_consume_token (parser->lexer);
5596       /* Parse the template-id.  */
5597       cp_parser_template_id (parser,
5598                              /*template_keyword_p=*/true,
5599                              /*check_dependency_p=*/false,
5600                              /*is_declaration=*/true);
5601       /* Look for the `::' token.  */
5602       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5603     }
5604   /* If the next token is not a `~', then there might be some
5605      additional qualification.  */
5606   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5607     {
5608       /* At this point, we're looking for "type-name :: ~".  The type-name
5609          must not be a class-name, since this is a pseudo-destructor.  So,
5610          it must be either an enum-name, or a typedef-name -- both of which
5611          are just identifiers.  So, we peek ahead to check that the "::"
5612          and "~" tokens are present; if they are not, then we can avoid
5613          calling type_name.  */
5614       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5615           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5616           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5617         {
5618           cp_parser_error (parser, "non-scalar type");
5619           return;
5620         }
5621
5622       /* Look for the type-name.  */
5623       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5624       if (*scope == error_mark_node)
5625         return;
5626
5627       /* Look for the `::' token.  */
5628       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5629     }
5630   else
5631     *scope = NULL_TREE;
5632
5633   /* Look for the `~'.  */
5634   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5635   /* Look for the type-name again.  We are not responsible for
5636      checking that it matches the first type-name.  */
5637   *type = cp_parser_nonclass_name (parser);
5638 }
5639
5640 /* Parse a unary-expression.
5641
5642    unary-expression:
5643      postfix-expression
5644      ++ cast-expression
5645      -- cast-expression
5646      unary-operator cast-expression
5647      sizeof unary-expression
5648      sizeof ( type-id )
5649      alignof ( type-id )  [C++0x]
5650      new-expression
5651      delete-expression
5652
5653    GNU Extensions:
5654
5655    unary-expression:
5656      __extension__ cast-expression
5657      __alignof__ unary-expression
5658      __alignof__ ( type-id )
5659      alignof unary-expression  [C++0x]
5660      __real__ cast-expression
5661      __imag__ cast-expression
5662      && identifier
5663
5664    ADDRESS_P is true iff the unary-expression is appearing as the
5665    operand of the `&' operator.   CAST_P is true if this expression is
5666    the target of a cast.
5667
5668    Returns a representation of the expression.  */
5669
5670 static tree
5671 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5672                             cp_id_kind * pidk)
5673 {
5674   cp_token *token;
5675   enum tree_code unary_operator;
5676
5677   /* Peek at the next token.  */
5678   token = cp_lexer_peek_token (parser->lexer);
5679   /* Some keywords give away the kind of expression.  */
5680   if (token->type == CPP_KEYWORD)
5681     {
5682       enum rid keyword = token->keyword;
5683
5684       switch (keyword)
5685         {
5686         case RID_ALIGNOF:
5687         case RID_SIZEOF:
5688           {
5689             tree operand;
5690             enum tree_code op;
5691
5692             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5693             /* Consume the token.  */
5694             cp_lexer_consume_token (parser->lexer);
5695             /* Parse the operand.  */
5696             operand = cp_parser_sizeof_operand (parser, keyword);
5697
5698             if (TYPE_P (operand))
5699               return cxx_sizeof_or_alignof_type (operand, op, true);
5700             else
5701               {
5702                 /* ISO C++ defines alignof only with types, not with
5703                    expressions. So pedwarn if alignof is used with a non-
5704                    type expression. However, __alignof__ is ok.  */
5705                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5706                   pedwarn (token->location, OPT_pedantic,
5707                            "ISO C++ does not allow %<alignof%> "
5708                            "with a non-type");
5709
5710                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5711               }
5712           }
5713
5714         case RID_NEW:
5715           return cp_parser_new_expression (parser);
5716
5717         case RID_DELETE:
5718           return cp_parser_delete_expression (parser);
5719
5720         case RID_EXTENSION:
5721           {
5722             /* The saved value of the PEDANTIC flag.  */
5723             int saved_pedantic;
5724             tree expr;
5725
5726             /* Save away the PEDANTIC flag.  */
5727             cp_parser_extension_opt (parser, &saved_pedantic);
5728             /* Parse the cast-expression.  */
5729             expr = cp_parser_simple_cast_expression (parser);
5730             /* Restore the PEDANTIC flag.  */
5731             pedantic = saved_pedantic;
5732
5733             return expr;
5734           }
5735
5736         case RID_REALPART:
5737         case RID_IMAGPART:
5738           {
5739             tree expression;
5740
5741             /* Consume the `__real__' or `__imag__' token.  */
5742             cp_lexer_consume_token (parser->lexer);
5743             /* Parse the cast-expression.  */
5744             expression = cp_parser_simple_cast_expression (parser);
5745             /* Create the complete representation.  */
5746             return build_x_unary_op ((keyword == RID_REALPART
5747                                       ? REALPART_EXPR : IMAGPART_EXPR),
5748                                      expression,
5749                                      tf_warning_or_error);
5750           }
5751           break;
5752
5753         case RID_NOEXCEPT:
5754           {
5755             tree expr;
5756             const char *saved_message;
5757             bool saved_integral_constant_expression_p;
5758             bool saved_non_integral_constant_expression_p;
5759             bool saved_greater_than_is_operator_p;
5760
5761             cp_lexer_consume_token (parser->lexer);
5762             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5763
5764             saved_message = parser->type_definition_forbidden_message;
5765             parser->type_definition_forbidden_message
5766               = G_("types may not be defined in %<noexcept%> expressions");
5767
5768             saved_integral_constant_expression_p
5769               = parser->integral_constant_expression_p;
5770             saved_non_integral_constant_expression_p
5771               = parser->non_integral_constant_expression_p;
5772             parser->integral_constant_expression_p = false;
5773
5774             saved_greater_than_is_operator_p
5775               = parser->greater_than_is_operator_p;
5776             parser->greater_than_is_operator_p = true;
5777
5778             ++cp_unevaluated_operand;
5779             ++c_inhibit_evaluation_warnings;
5780             expr = cp_parser_expression (parser, false, NULL);
5781             --c_inhibit_evaluation_warnings;
5782             --cp_unevaluated_operand;
5783
5784             parser->greater_than_is_operator_p
5785               = saved_greater_than_is_operator_p;
5786
5787             parser->integral_constant_expression_p
5788               = saved_integral_constant_expression_p;
5789             parser->non_integral_constant_expression_p
5790               = saved_non_integral_constant_expression_p;
5791
5792             parser->type_definition_forbidden_message = saved_message;
5793
5794             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5795             return finish_noexcept_expr (expr, tf_warning_or_error);
5796           }
5797
5798         default:
5799           break;
5800         }
5801     }
5802
5803   /* Look for the `:: new' and `:: delete', which also signal the
5804      beginning of a new-expression, or delete-expression,
5805      respectively.  If the next token is `::', then it might be one of
5806      these.  */
5807   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5808     {
5809       enum rid keyword;
5810
5811       /* See if the token after the `::' is one of the keywords in
5812          which we're interested.  */
5813       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5814       /* If it's `new', we have a new-expression.  */
5815       if (keyword == RID_NEW)
5816         return cp_parser_new_expression (parser);
5817       /* Similarly, for `delete'.  */
5818       else if (keyword == RID_DELETE)
5819         return cp_parser_delete_expression (parser);
5820     }
5821
5822   /* Look for a unary operator.  */
5823   unary_operator = cp_parser_unary_operator (token);
5824   /* The `++' and `--' operators can be handled similarly, even though
5825      they are not technically unary-operators in the grammar.  */
5826   if (unary_operator == ERROR_MARK)
5827     {
5828       if (token->type == CPP_PLUS_PLUS)
5829         unary_operator = PREINCREMENT_EXPR;
5830       else if (token->type == CPP_MINUS_MINUS)
5831         unary_operator = PREDECREMENT_EXPR;
5832       /* Handle the GNU address-of-label extension.  */
5833       else if (cp_parser_allow_gnu_extensions_p (parser)
5834                && token->type == CPP_AND_AND)
5835         {
5836           tree identifier;
5837           tree expression;
5838           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5839
5840           /* Consume the '&&' token.  */
5841           cp_lexer_consume_token (parser->lexer);
5842           /* Look for the identifier.  */
5843           identifier = cp_parser_identifier (parser);
5844           /* Create an expression representing the address.  */
5845           expression = finish_label_address_expr (identifier, loc);
5846           if (cp_parser_non_integral_constant_expression (parser,
5847                                                           NIC_ADDR_LABEL))
5848             expression = error_mark_node;
5849           return expression;
5850         }
5851     }
5852   if (unary_operator != ERROR_MARK)
5853     {
5854       tree cast_expression;
5855       tree expression = error_mark_node;
5856       non_integral_constant non_constant_p = NIC_NONE;
5857
5858       /* Consume the operator token.  */
5859       token = cp_lexer_consume_token (parser->lexer);
5860       /* Parse the cast-expression.  */
5861       cast_expression
5862         = cp_parser_cast_expression (parser,
5863                                      unary_operator == ADDR_EXPR,
5864                                      /*cast_p=*/false, pidk);
5865       /* Now, build an appropriate representation.  */
5866       switch (unary_operator)
5867         {
5868         case INDIRECT_REF:
5869           non_constant_p = NIC_STAR;
5870           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5871                                              tf_warning_or_error);
5872           break;
5873
5874         case ADDR_EXPR:
5875            non_constant_p = NIC_ADDR;
5876           /* Fall through.  */
5877         case BIT_NOT_EXPR:
5878           expression = build_x_unary_op (unary_operator, cast_expression,
5879                                          tf_warning_or_error);
5880           break;
5881
5882         case PREINCREMENT_EXPR:
5883         case PREDECREMENT_EXPR:
5884           non_constant_p = unary_operator == PREINCREMENT_EXPR
5885                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5886           /* Fall through.  */
5887         case UNARY_PLUS_EXPR:
5888         case NEGATE_EXPR:
5889         case TRUTH_NOT_EXPR:
5890           expression = finish_unary_op_expr (unary_operator, cast_expression);
5891           break;
5892
5893         default:
5894           gcc_unreachable ();
5895         }
5896
5897       if (non_constant_p != NIC_NONE
5898           && cp_parser_non_integral_constant_expression (parser,
5899                                                          non_constant_p))
5900         expression = error_mark_node;
5901
5902       return expression;
5903     }
5904
5905   return cp_parser_postfix_expression (parser, address_p, cast_p,
5906                                        /*member_access_only_p=*/false,
5907                                        pidk);
5908 }
5909
5910 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5911    unary-operator, the corresponding tree code is returned.  */
5912
5913 static enum tree_code
5914 cp_parser_unary_operator (cp_token* token)
5915 {
5916   switch (token->type)
5917     {
5918     case CPP_MULT:
5919       return INDIRECT_REF;
5920
5921     case CPP_AND:
5922       return ADDR_EXPR;
5923
5924     case CPP_PLUS:
5925       return UNARY_PLUS_EXPR;
5926
5927     case CPP_MINUS:
5928       return NEGATE_EXPR;
5929
5930     case CPP_NOT:
5931       return TRUTH_NOT_EXPR;
5932
5933     case CPP_COMPL:
5934       return BIT_NOT_EXPR;
5935
5936     default:
5937       return ERROR_MARK;
5938     }
5939 }
5940
5941 /* Parse a new-expression.
5942
5943    new-expression:
5944      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5945      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5946
5947    Returns a representation of the expression.  */
5948
5949 static tree
5950 cp_parser_new_expression (cp_parser* parser)
5951 {
5952   bool global_scope_p;
5953   VEC(tree,gc) *placement;
5954   tree type;
5955   VEC(tree,gc) *initializer;
5956   tree nelts;
5957   tree ret;
5958
5959   /* Look for the optional `::' operator.  */
5960   global_scope_p
5961     = (cp_parser_global_scope_opt (parser,
5962                                    /*current_scope_valid_p=*/false)
5963        != NULL_TREE);
5964   /* Look for the `new' operator.  */
5965   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5966   /* There's no easy way to tell a new-placement from the
5967      `( type-id )' construct.  */
5968   cp_parser_parse_tentatively (parser);
5969   /* Look for a new-placement.  */
5970   placement = cp_parser_new_placement (parser);
5971   /* If that didn't work out, there's no new-placement.  */
5972   if (!cp_parser_parse_definitely (parser))
5973     {
5974       if (placement != NULL)
5975         release_tree_vector (placement);
5976       placement = NULL;
5977     }
5978
5979   /* If the next token is a `(', then we have a parenthesized
5980      type-id.  */
5981   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5982     {
5983       cp_token *token;
5984       /* Consume the `('.  */
5985       cp_lexer_consume_token (parser->lexer);
5986       /* Parse the type-id.  */
5987       type = cp_parser_type_id (parser);
5988       /* Look for the closing `)'.  */
5989       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5990       token = cp_lexer_peek_token (parser->lexer);
5991       /* There should not be a direct-new-declarator in this production,
5992          but GCC used to allowed this, so we check and emit a sensible error
5993          message for this case.  */
5994       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5995         {
5996           error_at (token->location,
5997                     "array bound forbidden after parenthesized type-id");
5998           inform (token->location, 
5999                   "try removing the parentheses around the type-id");
6000           cp_parser_direct_new_declarator (parser);
6001         }
6002       nelts = NULL_TREE;
6003     }
6004   /* Otherwise, there must be a new-type-id.  */
6005   else
6006     type = cp_parser_new_type_id (parser, &nelts);
6007
6008   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6009   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6010       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6011     initializer = cp_parser_new_initializer (parser);
6012   else
6013     initializer = NULL;
6014
6015   /* A new-expression may not appear in an integral constant
6016      expression.  */
6017   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6018     ret = error_mark_node;
6019   else
6020     {
6021       /* Create a representation of the new-expression.  */
6022       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6023                        tf_warning_or_error);
6024     }
6025
6026   if (placement != NULL)
6027     release_tree_vector (placement);
6028   if (initializer != NULL)
6029     release_tree_vector (initializer);
6030
6031   return ret;
6032 }
6033
6034 /* Parse a new-placement.
6035
6036    new-placement:
6037      ( expression-list )
6038
6039    Returns the same representation as for an expression-list.  */
6040
6041 static VEC(tree,gc) *
6042 cp_parser_new_placement (cp_parser* parser)
6043 {
6044   VEC(tree,gc) *expression_list;
6045
6046   /* Parse the expression-list.  */
6047   expression_list = (cp_parser_parenthesized_expression_list
6048                      (parser, non_attr, /*cast_p=*/false,
6049                       /*allow_expansion_p=*/true,
6050                       /*non_constant_p=*/NULL));
6051
6052   return expression_list;
6053 }
6054
6055 /* Parse a new-type-id.
6056
6057    new-type-id:
6058      type-specifier-seq new-declarator [opt]
6059
6060    Returns the TYPE allocated.  If the new-type-id indicates an array
6061    type, *NELTS is set to the number of elements in the last array
6062    bound; the TYPE will not include the last array bound.  */
6063
6064 static tree
6065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6066 {
6067   cp_decl_specifier_seq type_specifier_seq;
6068   cp_declarator *new_declarator;
6069   cp_declarator *declarator;
6070   cp_declarator *outer_declarator;
6071   const char *saved_message;
6072   tree type;
6073
6074   /* The type-specifier sequence must not contain type definitions.
6075      (It cannot contain declarations of new types either, but if they
6076      are not definitions we will catch that because they are not
6077      complete.)  */
6078   saved_message = parser->type_definition_forbidden_message;
6079   parser->type_definition_forbidden_message
6080     = G_("types may not be defined in a new-type-id");
6081   /* Parse the type-specifier-seq.  */
6082   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6083                                 /*is_trailing_return=*/false,
6084                                 &type_specifier_seq);
6085   /* Restore the old message.  */
6086   parser->type_definition_forbidden_message = saved_message;
6087   /* Parse the new-declarator.  */
6088   new_declarator = cp_parser_new_declarator_opt (parser);
6089
6090   /* Determine the number of elements in the last array dimension, if
6091      any.  */
6092   *nelts = NULL_TREE;
6093   /* Skip down to the last array dimension.  */
6094   declarator = new_declarator;
6095   outer_declarator = NULL;
6096   while (declarator && (declarator->kind == cdk_pointer
6097                         || declarator->kind == cdk_ptrmem))
6098     {
6099       outer_declarator = declarator;
6100       declarator = declarator->declarator;
6101     }
6102   while (declarator
6103          && declarator->kind == cdk_array
6104          && declarator->declarator
6105          && declarator->declarator->kind == cdk_array)
6106     {
6107       outer_declarator = declarator;
6108       declarator = declarator->declarator;
6109     }
6110
6111   if (declarator && declarator->kind == cdk_array)
6112     {
6113       *nelts = declarator->u.array.bounds;
6114       if (*nelts == error_mark_node)
6115         *nelts = integer_one_node;
6116
6117       if (outer_declarator)
6118         outer_declarator->declarator = declarator->declarator;
6119       else
6120         new_declarator = NULL;
6121     }
6122
6123   type = groktypename (&type_specifier_seq, new_declarator, false);
6124   return type;
6125 }
6126
6127 /* Parse an (optional) new-declarator.
6128
6129    new-declarator:
6130      ptr-operator new-declarator [opt]
6131      direct-new-declarator
6132
6133    Returns the declarator.  */
6134
6135 static cp_declarator *
6136 cp_parser_new_declarator_opt (cp_parser* parser)
6137 {
6138   enum tree_code code;
6139   tree type;
6140   cp_cv_quals cv_quals;
6141
6142   /* We don't know if there's a ptr-operator next, or not.  */
6143   cp_parser_parse_tentatively (parser);
6144   /* Look for a ptr-operator.  */
6145   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6146   /* If that worked, look for more new-declarators.  */
6147   if (cp_parser_parse_definitely (parser))
6148     {
6149       cp_declarator *declarator;
6150
6151       /* Parse another optional declarator.  */
6152       declarator = cp_parser_new_declarator_opt (parser);
6153
6154       return cp_parser_make_indirect_declarator
6155         (code, type, cv_quals, declarator);
6156     }
6157
6158   /* If the next token is a `[', there is a direct-new-declarator.  */
6159   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6160     return cp_parser_direct_new_declarator (parser);
6161
6162   return NULL;
6163 }
6164
6165 /* Parse a direct-new-declarator.
6166
6167    direct-new-declarator:
6168      [ expression ]
6169      direct-new-declarator [constant-expression]
6170
6171    */
6172
6173 static cp_declarator *
6174 cp_parser_direct_new_declarator (cp_parser* parser)
6175 {
6176   cp_declarator *declarator = NULL;
6177
6178   while (true)
6179     {
6180       tree expression;
6181
6182       /* Look for the opening `['.  */
6183       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6184       /* The first expression is not required to be constant.  */
6185       if (!declarator)
6186         {
6187           cp_token *token = cp_lexer_peek_token (parser->lexer);
6188           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6189           /* The standard requires that the expression have integral
6190              type.  DR 74 adds enumeration types.  We believe that the
6191              real intent is that these expressions be handled like the
6192              expression in a `switch' condition, which also allows
6193              classes with a single conversion to integral or
6194              enumeration type.  */
6195           if (!processing_template_decl)
6196             {
6197               expression
6198                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6199                                               expression,
6200                                               /*complain=*/true);
6201               if (!expression)
6202                 {
6203                   error_at (token->location,
6204                             "expression in new-declarator must have integral "
6205                             "or enumeration type");
6206                   expression = error_mark_node;
6207                 }
6208             }
6209         }
6210       /* But all the other expressions must be.  */
6211       else
6212         expression
6213           = cp_parser_constant_expression (parser,
6214                                            /*allow_non_constant=*/false,
6215                                            NULL);
6216       /* Look for the closing `]'.  */
6217       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6218
6219       /* Add this bound to the declarator.  */
6220       declarator = make_array_declarator (declarator, expression);
6221
6222       /* If the next token is not a `[', then there are no more
6223          bounds.  */
6224       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6225         break;
6226     }
6227
6228   return declarator;
6229 }
6230
6231 /* Parse a new-initializer.
6232
6233    new-initializer:
6234      ( expression-list [opt] )
6235      braced-init-list
6236
6237    Returns a representation of the expression-list.  */
6238
6239 static VEC(tree,gc) *
6240 cp_parser_new_initializer (cp_parser* parser)
6241 {
6242   VEC(tree,gc) *expression_list;
6243
6244   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6245     {
6246       tree t;
6247       bool expr_non_constant_p;
6248       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6249       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6250       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6251       expression_list = make_tree_vector_single (t);
6252     }
6253   else
6254     expression_list = (cp_parser_parenthesized_expression_list
6255                        (parser, non_attr, /*cast_p=*/false,
6256                         /*allow_expansion_p=*/true,
6257                         /*non_constant_p=*/NULL));
6258
6259   return expression_list;
6260 }
6261
6262 /* Parse a delete-expression.
6263
6264    delete-expression:
6265      :: [opt] delete cast-expression
6266      :: [opt] delete [ ] cast-expression
6267
6268    Returns a representation of the expression.  */
6269
6270 static tree
6271 cp_parser_delete_expression (cp_parser* parser)
6272 {
6273   bool global_scope_p;
6274   bool array_p;
6275   tree expression;
6276
6277   /* Look for the optional `::' operator.  */
6278   global_scope_p
6279     = (cp_parser_global_scope_opt (parser,
6280                                    /*current_scope_valid_p=*/false)
6281        != NULL_TREE);
6282   /* Look for the `delete' keyword.  */
6283   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6284   /* See if the array syntax is in use.  */
6285   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6286     {
6287       /* Consume the `[' token.  */
6288       cp_lexer_consume_token (parser->lexer);
6289       /* Look for the `]' token.  */
6290       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6291       /* Remember that this is the `[]' construct.  */
6292       array_p = true;
6293     }
6294   else
6295     array_p = false;
6296
6297   /* Parse the cast-expression.  */
6298   expression = cp_parser_simple_cast_expression (parser);
6299
6300   /* A delete-expression may not appear in an integral constant
6301      expression.  */
6302   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6303     return error_mark_node;
6304
6305   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6306                         tf_warning_or_error);
6307 }
6308
6309 /* Returns true if TOKEN may start a cast-expression and false
6310    otherwise.  */
6311
6312 static bool
6313 cp_parser_token_starts_cast_expression (cp_token *token)
6314 {
6315   switch (token->type)
6316     {
6317     case CPP_COMMA:
6318     case CPP_SEMICOLON:
6319     case CPP_QUERY:
6320     case CPP_COLON:
6321     case CPP_CLOSE_SQUARE:
6322     case CPP_CLOSE_PAREN:
6323     case CPP_CLOSE_BRACE:
6324     case CPP_DOT:
6325     case CPP_DOT_STAR:
6326     case CPP_DEREF:
6327     case CPP_DEREF_STAR:
6328     case CPP_DIV:
6329     case CPP_MOD:
6330     case CPP_LSHIFT:
6331     case CPP_RSHIFT:
6332     case CPP_LESS:
6333     case CPP_GREATER:
6334     case CPP_LESS_EQ:
6335     case CPP_GREATER_EQ:
6336     case CPP_EQ_EQ:
6337     case CPP_NOT_EQ:
6338     case CPP_EQ:
6339     case CPP_MULT_EQ:
6340     case CPP_DIV_EQ:
6341     case CPP_MOD_EQ:
6342     case CPP_PLUS_EQ:
6343     case CPP_MINUS_EQ:
6344     case CPP_RSHIFT_EQ:
6345     case CPP_LSHIFT_EQ:
6346     case CPP_AND_EQ:
6347     case CPP_XOR_EQ:
6348     case CPP_OR_EQ:
6349     case CPP_XOR:
6350     case CPP_OR:
6351     case CPP_OR_OR:
6352     case CPP_EOF:
6353       return false;
6354
6355       /* '[' may start a primary-expression in obj-c++.  */
6356     case CPP_OPEN_SQUARE:
6357       return c_dialect_objc ();
6358
6359     default:
6360       return true;
6361     }
6362 }
6363
6364 /* Parse a cast-expression.
6365
6366    cast-expression:
6367      unary-expression
6368      ( type-id ) cast-expression
6369
6370    ADDRESS_P is true iff the unary-expression is appearing as the
6371    operand of the `&' operator.   CAST_P is true if this expression is
6372    the target of a cast.
6373
6374    Returns a representation of the expression.  */
6375
6376 static tree
6377 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6378                            cp_id_kind * pidk)
6379 {
6380   /* If it's a `(', then we might be looking at a cast.  */
6381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6382     {
6383       tree type = NULL_TREE;
6384       tree expr = NULL_TREE;
6385       bool compound_literal_p;
6386       const char *saved_message;
6387
6388       /* There's no way to know yet whether or not this is a cast.
6389          For example, `(int (3))' is a unary-expression, while `(int)
6390          3' is a cast.  So, we resort to parsing tentatively.  */
6391       cp_parser_parse_tentatively (parser);
6392       /* Types may not be defined in a cast.  */
6393       saved_message = parser->type_definition_forbidden_message;
6394       parser->type_definition_forbidden_message
6395         = G_("types may not be defined in casts");
6396       /* Consume the `('.  */
6397       cp_lexer_consume_token (parser->lexer);
6398       /* A very tricky bit is that `(struct S) { 3 }' is a
6399          compound-literal (which we permit in C++ as an extension).
6400          But, that construct is not a cast-expression -- it is a
6401          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6402          is legal; if the compound-literal were a cast-expression,
6403          you'd need an extra set of parentheses.)  But, if we parse
6404          the type-id, and it happens to be a class-specifier, then we
6405          will commit to the parse at that point, because we cannot
6406          undo the action that is done when creating a new class.  So,
6407          then we cannot back up and do a postfix-expression.
6408
6409          Therefore, we scan ahead to the closing `)', and check to see
6410          if the token after the `)' is a `{'.  If so, we are not
6411          looking at a cast-expression.
6412
6413          Save tokens so that we can put them back.  */
6414       cp_lexer_save_tokens (parser->lexer);
6415       /* Skip tokens until the next token is a closing parenthesis.
6416          If we find the closing `)', and the next token is a `{', then
6417          we are looking at a compound-literal.  */
6418       compound_literal_p
6419         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6420                                                   /*consume_paren=*/true)
6421            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6422       /* Roll back the tokens we skipped.  */
6423       cp_lexer_rollback_tokens (parser->lexer);
6424       /* If we were looking at a compound-literal, simulate an error
6425          so that the call to cp_parser_parse_definitely below will
6426          fail.  */
6427       if (compound_literal_p)
6428         cp_parser_simulate_error (parser);
6429       else
6430         {
6431           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6432           parser->in_type_id_in_expr_p = true;
6433           /* Look for the type-id.  */
6434           type = cp_parser_type_id (parser);
6435           /* Look for the closing `)'.  */
6436           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6437           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6438         }
6439
6440       /* Restore the saved message.  */
6441       parser->type_definition_forbidden_message = saved_message;
6442
6443       /* At this point this can only be either a cast or a
6444          parenthesized ctor such as `(T ())' that looks like a cast to
6445          function returning T.  */
6446       if (!cp_parser_error_occurred (parser)
6447           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6448                                                      (parser->lexer)))
6449         {
6450           cp_parser_parse_definitely (parser);
6451           expr = cp_parser_cast_expression (parser,
6452                                             /*address_p=*/false,
6453                                             /*cast_p=*/true, pidk);
6454
6455           /* Warn about old-style casts, if so requested.  */
6456           if (warn_old_style_cast
6457               && !in_system_header
6458               && !VOID_TYPE_P (type)
6459               && current_lang_name != lang_name_c)
6460             warning (OPT_Wold_style_cast, "use of old-style cast");
6461
6462           /* Only type conversions to integral or enumeration types
6463              can be used in constant-expressions.  */
6464           if (!cast_valid_in_integral_constant_expression_p (type)
6465               && cp_parser_non_integral_constant_expression (parser,
6466                                                              NIC_CAST))
6467             return error_mark_node;
6468
6469           /* Perform the cast.  */
6470           expr = build_c_cast (input_location, type, expr);
6471           return expr;
6472         }
6473       else 
6474         cp_parser_abort_tentative_parse (parser);
6475     }
6476
6477   /* If we get here, then it's not a cast, so it must be a
6478      unary-expression.  */
6479   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6480 }
6481
6482 /* Parse a binary expression of the general form:
6483
6484    pm-expression:
6485      cast-expression
6486      pm-expression .* cast-expression
6487      pm-expression ->* cast-expression
6488
6489    multiplicative-expression:
6490      pm-expression
6491      multiplicative-expression * pm-expression
6492      multiplicative-expression / pm-expression
6493      multiplicative-expression % pm-expression
6494
6495    additive-expression:
6496      multiplicative-expression
6497      additive-expression + multiplicative-expression
6498      additive-expression - multiplicative-expression
6499
6500    shift-expression:
6501      additive-expression
6502      shift-expression << additive-expression
6503      shift-expression >> additive-expression
6504
6505    relational-expression:
6506      shift-expression
6507      relational-expression < shift-expression
6508      relational-expression > shift-expression
6509      relational-expression <= shift-expression
6510      relational-expression >= shift-expression
6511
6512   GNU Extension:
6513
6514    relational-expression:
6515      relational-expression <? shift-expression
6516      relational-expression >? shift-expression
6517
6518    equality-expression:
6519      relational-expression
6520      equality-expression == relational-expression
6521      equality-expression != relational-expression
6522
6523    and-expression:
6524      equality-expression
6525      and-expression & equality-expression
6526
6527    exclusive-or-expression:
6528      and-expression
6529      exclusive-or-expression ^ and-expression
6530
6531    inclusive-or-expression:
6532      exclusive-or-expression
6533      inclusive-or-expression | exclusive-or-expression
6534
6535    logical-and-expression:
6536      inclusive-or-expression
6537      logical-and-expression && inclusive-or-expression
6538
6539    logical-or-expression:
6540      logical-and-expression
6541      logical-or-expression || logical-and-expression
6542
6543    All these are implemented with a single function like:
6544
6545    binary-expression:
6546      simple-cast-expression
6547      binary-expression <token> binary-expression
6548
6549    CAST_P is true if this expression is the target of a cast.
6550
6551    The binops_by_token map is used to get the tree codes for each <token> type.
6552    binary-expressions are associated according to a precedence table.  */
6553
6554 #define TOKEN_PRECEDENCE(token)                              \
6555 (((token->type == CPP_GREATER                                \
6556    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6557   && !parser->greater_than_is_operator_p)                    \
6558  ? PREC_NOT_OPERATOR                                         \
6559  : binops_by_token[token->type].prec)
6560
6561 static tree
6562 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6563                              bool no_toplevel_fold_p,
6564                              enum cp_parser_prec prec,
6565                              cp_id_kind * pidk)
6566 {
6567   cp_parser_expression_stack stack;
6568   cp_parser_expression_stack_entry *sp = &stack[0];
6569   tree lhs, rhs;
6570   cp_token *token;
6571   enum tree_code tree_type, lhs_type, rhs_type;
6572   enum cp_parser_prec new_prec, lookahead_prec;
6573   bool overloaded_p;
6574
6575   /* Parse the first expression.  */
6576   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6577   lhs_type = ERROR_MARK;
6578
6579   for (;;)
6580     {
6581       /* Get an operator token.  */
6582       token = cp_lexer_peek_token (parser->lexer);
6583
6584       if (warn_cxx0x_compat
6585           && token->type == CPP_RSHIFT
6586           && !parser->greater_than_is_operator_p)
6587         {
6588           if (warning_at (token->location, OPT_Wc__0x_compat, 
6589                           "%<>>%> operator will be treated as"
6590                           " two right angle brackets in C++0x"))
6591             inform (token->location,
6592                     "suggest parentheses around %<>>%> expression");
6593         }
6594
6595       new_prec = TOKEN_PRECEDENCE (token);
6596
6597       /* Popping an entry off the stack means we completed a subexpression:
6598          - either we found a token which is not an operator (`>' where it is not
6599            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6600            will happen repeatedly;
6601          - or, we found an operator which has lower priority.  This is the case
6602            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6603            parsing `3 * 4'.  */
6604       if (new_prec <= prec)
6605         {
6606           if (sp == stack)
6607             break;
6608           else
6609             goto pop;
6610         }
6611
6612      get_rhs:
6613       tree_type = binops_by_token[token->type].tree_type;
6614
6615       /* We used the operator token.  */
6616       cp_lexer_consume_token (parser->lexer);
6617
6618       /* For "false && x" or "true || x", x will never be executed;
6619          disable warnings while evaluating it.  */
6620       if (tree_type == TRUTH_ANDIF_EXPR)
6621         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6622       else if (tree_type == TRUTH_ORIF_EXPR)
6623         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6624
6625       /* Extract another operand.  It may be the RHS of this expression
6626          or the LHS of a new, higher priority expression.  */
6627       rhs = cp_parser_simple_cast_expression (parser);
6628       rhs_type = ERROR_MARK;
6629
6630       /* Get another operator token.  Look up its precedence to avoid
6631          building a useless (immediately popped) stack entry for common
6632          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6633       token = cp_lexer_peek_token (parser->lexer);
6634       lookahead_prec = TOKEN_PRECEDENCE (token);
6635       if (lookahead_prec > new_prec)
6636         {
6637           /* ... and prepare to parse the RHS of the new, higher priority
6638              expression.  Since precedence levels on the stack are
6639              monotonically increasing, we do not have to care about
6640              stack overflows.  */
6641           sp->prec = prec;
6642           sp->tree_type = tree_type;
6643           sp->lhs = lhs;
6644           sp->lhs_type = lhs_type;
6645           sp++;
6646           lhs = rhs;
6647           lhs_type = rhs_type;
6648           prec = new_prec;
6649           new_prec = lookahead_prec;
6650           goto get_rhs;
6651
6652          pop:
6653           lookahead_prec = new_prec;
6654           /* If the stack is not empty, we have parsed into LHS the right side
6655              (`4' in the example above) of an expression we had suspended.
6656              We can use the information on the stack to recover the LHS (`3')
6657              from the stack together with the tree code (`MULT_EXPR'), and
6658              the precedence of the higher level subexpression
6659              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6660              which will be used to actually build the additive expression.  */
6661           --sp;
6662           prec = sp->prec;
6663           tree_type = sp->tree_type;
6664           rhs = lhs;
6665           rhs_type = lhs_type;
6666           lhs = sp->lhs;
6667           lhs_type = sp->lhs_type;
6668         }
6669
6670       /* Undo the disabling of warnings done above.  */
6671       if (tree_type == TRUTH_ANDIF_EXPR)
6672         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6673       else if (tree_type == TRUTH_ORIF_EXPR)
6674         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6675
6676       overloaded_p = false;
6677       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6678          ERROR_MARK for everything that is not a binary expression.
6679          This makes warn_about_parentheses miss some warnings that
6680          involve unary operators.  For unary expressions we should
6681          pass the correct tree_code unless the unary expression was
6682          surrounded by parentheses.
6683       */
6684       if (no_toplevel_fold_p
6685           && lookahead_prec <= prec
6686           && sp == stack
6687           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6688         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6689       else
6690         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6691                                  &overloaded_p, tf_warning_or_error);
6692       lhs_type = tree_type;
6693
6694       /* If the binary operator required the use of an overloaded operator,
6695          then this expression cannot be an integral constant-expression.
6696          An overloaded operator can be used even if both operands are
6697          otherwise permissible in an integral constant-expression if at
6698          least one of the operands is of enumeration type.  */
6699
6700       if (overloaded_p
6701           && cp_parser_non_integral_constant_expression (parser,
6702                                                          NIC_OVERLOADED))
6703         return error_mark_node;
6704     }
6705
6706   return lhs;
6707 }
6708
6709
6710 /* Parse the `? expression : assignment-expression' part of a
6711    conditional-expression.  The LOGICAL_OR_EXPR is the
6712    logical-or-expression that started the conditional-expression.
6713    Returns a representation of the entire conditional-expression.
6714
6715    This routine is used by cp_parser_assignment_expression.
6716
6717      ? expression : assignment-expression
6718
6719    GNU Extensions:
6720
6721      ? : assignment-expression */
6722
6723 static tree
6724 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6725 {
6726   tree expr;
6727   tree assignment_expr;
6728   struct cp_token *token;
6729
6730   /* Consume the `?' token.  */
6731   cp_lexer_consume_token (parser->lexer);
6732   token = cp_lexer_peek_token (parser->lexer);
6733   if (cp_parser_allow_gnu_extensions_p (parser)
6734       && token->type == CPP_COLON)
6735     {
6736       pedwarn (token->location, OPT_pedantic, 
6737                "ISO C++ does not allow ?: with omitted middle operand");
6738       /* Implicit true clause.  */
6739       expr = NULL_TREE;
6740       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6741       warn_for_omitted_condop (token->location, logical_or_expr);
6742     }
6743   else
6744     {
6745       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6746       parser->colon_corrects_to_scope_p = false;
6747       /* Parse the expression.  */
6748       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6749       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6750       c_inhibit_evaluation_warnings +=
6751         ((logical_or_expr == truthvalue_true_node)
6752          - (logical_or_expr == truthvalue_false_node));
6753       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6754     }
6755
6756   /* The next token should be a `:'.  */
6757   cp_parser_require (parser, CPP_COLON, RT_COLON);
6758   /* Parse the assignment-expression.  */
6759   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6760   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6761
6762   /* Build the conditional-expression.  */
6763   return build_x_conditional_expr (logical_or_expr,
6764                                    expr,
6765                                    assignment_expr,
6766                                    tf_warning_or_error);
6767 }
6768
6769 /* Parse an assignment-expression.
6770
6771    assignment-expression:
6772      conditional-expression
6773      logical-or-expression assignment-operator assignment_expression
6774      throw-expression
6775
6776    CAST_P is true if this expression is the target of a cast.
6777
6778    Returns a representation for the expression.  */
6779
6780 static tree
6781 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6782                                  cp_id_kind * pidk)
6783 {
6784   tree expr;
6785
6786   /* If the next token is the `throw' keyword, then we're looking at
6787      a throw-expression.  */
6788   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6789     expr = cp_parser_throw_expression (parser);
6790   /* Otherwise, it must be that we are looking at a
6791      logical-or-expression.  */
6792   else
6793     {
6794       /* Parse the binary expressions (logical-or-expression).  */
6795       expr = cp_parser_binary_expression (parser, cast_p, false,
6796                                           PREC_NOT_OPERATOR, pidk);
6797       /* If the next token is a `?' then we're actually looking at a
6798          conditional-expression.  */
6799       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6800         return cp_parser_question_colon_clause (parser, expr);
6801       else
6802         {
6803           enum tree_code assignment_operator;
6804
6805           /* If it's an assignment-operator, we're using the second
6806              production.  */
6807           assignment_operator
6808             = cp_parser_assignment_operator_opt (parser);
6809           if (assignment_operator != ERROR_MARK)
6810             {
6811               bool non_constant_p;
6812
6813               /* Parse the right-hand side of the assignment.  */
6814               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6815
6816               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6817                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6818
6819               /* An assignment may not appear in a
6820                  constant-expression.  */
6821               if (cp_parser_non_integral_constant_expression (parser,
6822                                                               NIC_ASSIGNMENT))
6823                 return error_mark_node;
6824               /* Build the assignment expression.  */
6825               expr = build_x_modify_expr (expr,
6826                                           assignment_operator,
6827                                           rhs,
6828                                           tf_warning_or_error);
6829             }
6830         }
6831     }
6832
6833   return expr;
6834 }
6835
6836 /* Parse an (optional) assignment-operator.
6837
6838    assignment-operator: one of
6839      = *= /= %= += -= >>= <<= &= ^= |=
6840
6841    GNU Extension:
6842
6843    assignment-operator: one of
6844      <?= >?=
6845
6846    If the next token is an assignment operator, the corresponding tree
6847    code is returned, and the token is consumed.  For example, for
6848    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6849    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6850    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6851    operator, ERROR_MARK is returned.  */
6852
6853 static enum tree_code
6854 cp_parser_assignment_operator_opt (cp_parser* parser)
6855 {
6856   enum tree_code op;
6857   cp_token *token;
6858
6859   /* Peek at the next token.  */
6860   token = cp_lexer_peek_token (parser->lexer);
6861
6862   switch (token->type)
6863     {
6864     case CPP_EQ:
6865       op = NOP_EXPR;
6866       break;
6867
6868     case CPP_MULT_EQ:
6869       op = MULT_EXPR;
6870       break;
6871
6872     case CPP_DIV_EQ:
6873       op = TRUNC_DIV_EXPR;
6874       break;
6875
6876     case CPP_MOD_EQ:
6877       op = TRUNC_MOD_EXPR;
6878       break;
6879
6880     case CPP_PLUS_EQ:
6881       op = PLUS_EXPR;
6882       break;
6883
6884     case CPP_MINUS_EQ:
6885       op = MINUS_EXPR;
6886       break;
6887
6888     case CPP_RSHIFT_EQ:
6889       op = RSHIFT_EXPR;
6890       break;
6891
6892     case CPP_LSHIFT_EQ:
6893       op = LSHIFT_EXPR;
6894       break;
6895
6896     case CPP_AND_EQ:
6897       op = BIT_AND_EXPR;
6898       break;
6899
6900     case CPP_XOR_EQ:
6901       op = BIT_XOR_EXPR;
6902       break;
6903
6904     case CPP_OR_EQ:
6905       op = BIT_IOR_EXPR;
6906       break;
6907
6908     default:
6909       /* Nothing else is an assignment operator.  */
6910       op = ERROR_MARK;
6911     }
6912
6913   /* If it was an assignment operator, consume it.  */
6914   if (op != ERROR_MARK)
6915     cp_lexer_consume_token (parser->lexer);
6916
6917   return op;
6918 }
6919
6920 /* Parse an expression.
6921
6922    expression:
6923      assignment-expression
6924      expression , assignment-expression
6925
6926    CAST_P is true if this expression is the target of a cast.
6927
6928    Returns a representation of the expression.  */
6929
6930 static tree
6931 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6932 {
6933   tree expression = NULL_TREE;
6934
6935   while (true)
6936     {
6937       tree assignment_expression;
6938
6939       /* Parse the next assignment-expression.  */
6940       assignment_expression
6941         = cp_parser_assignment_expression (parser, cast_p, pidk);
6942       /* If this is the first assignment-expression, we can just
6943          save it away.  */
6944       if (!expression)
6945         expression = assignment_expression;
6946       else
6947         expression = build_x_compound_expr (expression,
6948                                             assignment_expression,
6949                                             tf_warning_or_error);
6950       /* If the next token is not a comma, then we are done with the
6951          expression.  */
6952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6953         break;
6954       /* Consume the `,'.  */
6955       cp_lexer_consume_token (parser->lexer);
6956       /* A comma operator cannot appear in a constant-expression.  */
6957       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6958         expression = error_mark_node;
6959     }
6960
6961   return expression;
6962 }
6963
6964 /* Parse a constant-expression.
6965
6966    constant-expression:
6967      conditional-expression
6968
6969   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6970   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6971   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6972   is false, NON_CONSTANT_P should be NULL.  */
6973
6974 static tree
6975 cp_parser_constant_expression (cp_parser* parser,
6976                                bool allow_non_constant_p,
6977                                bool *non_constant_p)
6978 {
6979   bool saved_integral_constant_expression_p;
6980   bool saved_allow_non_integral_constant_expression_p;
6981   bool saved_non_integral_constant_expression_p;
6982   tree expression;
6983
6984   /* It might seem that we could simply parse the
6985      conditional-expression, and then check to see if it were
6986      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6987      one that the compiler can figure out is constant, possibly after
6988      doing some simplifications or optimizations.  The standard has a
6989      precise definition of constant-expression, and we must honor
6990      that, even though it is somewhat more restrictive.
6991
6992      For example:
6993
6994        int i[(2, 3)];
6995
6996      is not a legal declaration, because `(2, 3)' is not a
6997      constant-expression.  The `,' operator is forbidden in a
6998      constant-expression.  However, GCC's constant-folding machinery
6999      will fold this operation to an INTEGER_CST for `3'.  */
7000
7001   /* Save the old settings.  */
7002   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7003   saved_allow_non_integral_constant_expression_p
7004     = parser->allow_non_integral_constant_expression_p;
7005   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7006   /* We are now parsing a constant-expression.  */
7007   parser->integral_constant_expression_p = true;
7008   parser->allow_non_integral_constant_expression_p
7009     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7010   parser->non_integral_constant_expression_p = false;
7011   /* Although the grammar says "conditional-expression", we parse an
7012      "assignment-expression", which also permits "throw-expression"
7013      and the use of assignment operators.  In the case that
7014      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7015      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7016      actually essential that we look for an assignment-expression.
7017      For example, cp_parser_initializer_clauses uses this function to
7018      determine whether a particular assignment-expression is in fact
7019      constant.  */
7020   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7021   /* Restore the old settings.  */
7022   parser->integral_constant_expression_p
7023     = saved_integral_constant_expression_p;
7024   parser->allow_non_integral_constant_expression_p
7025     = saved_allow_non_integral_constant_expression_p;
7026   if (cxx_dialect >= cxx0x)
7027     {
7028       /* Require an rvalue constant expression here; that's what our
7029          callers expect.  Reference constant expressions are handled
7030          separately in e.g. cp_parser_template_argument.  */
7031       bool is_const = potential_rvalue_constant_expression (expression);
7032       parser->non_integral_constant_expression_p = !is_const;
7033       if (!is_const && !allow_non_constant_p)
7034         require_potential_rvalue_constant_expression (expression);
7035     }
7036   if (allow_non_constant_p)
7037     *non_constant_p = parser->non_integral_constant_expression_p;
7038   else if (parser->non_integral_constant_expression_p)
7039     expression = error_mark_node;
7040   parser->non_integral_constant_expression_p
7041     = saved_non_integral_constant_expression_p;
7042
7043   return expression;
7044 }
7045
7046 /* Parse __builtin_offsetof.
7047
7048    offsetof-expression:
7049      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7050
7051    offsetof-member-designator:
7052      id-expression
7053      | offsetof-member-designator "." id-expression
7054      | offsetof-member-designator "[" expression "]"
7055      | offsetof-member-designator "->" id-expression  */
7056
7057 static tree
7058 cp_parser_builtin_offsetof (cp_parser *parser)
7059 {
7060   int save_ice_p, save_non_ice_p;
7061   tree type, expr;
7062   cp_id_kind dummy;
7063   cp_token *token;
7064
7065   /* We're about to accept non-integral-constant things, but will
7066      definitely yield an integral constant expression.  Save and
7067      restore these values around our local parsing.  */
7068   save_ice_p = parser->integral_constant_expression_p;
7069   save_non_ice_p = parser->non_integral_constant_expression_p;
7070
7071   /* Consume the "__builtin_offsetof" token.  */
7072   cp_lexer_consume_token (parser->lexer);
7073   /* Consume the opening `('.  */
7074   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7075   /* Parse the type-id.  */
7076   type = cp_parser_type_id (parser);
7077   /* Look for the `,'.  */
7078   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7079   token = cp_lexer_peek_token (parser->lexer);
7080
7081   /* Build the (type *)null that begins the traditional offsetof macro.  */
7082   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7083                             tf_warning_or_error);
7084
7085   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7086   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7087                                                  true, &dummy, token->location);
7088   while (true)
7089     {
7090       token = cp_lexer_peek_token (parser->lexer);
7091       switch (token->type)
7092         {
7093         case CPP_OPEN_SQUARE:
7094           /* offsetof-member-designator "[" expression "]" */
7095           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7096           break;
7097
7098         case CPP_DEREF:
7099           /* offsetof-member-designator "->" identifier */
7100           expr = grok_array_decl (expr, integer_zero_node);
7101           /* FALLTHRU */
7102
7103         case CPP_DOT:
7104           /* offsetof-member-designator "." identifier */
7105           cp_lexer_consume_token (parser->lexer);
7106           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7107                                                          expr, true, &dummy,
7108                                                          token->location);
7109           break;
7110
7111         case CPP_CLOSE_PAREN:
7112           /* Consume the ")" token.  */
7113           cp_lexer_consume_token (parser->lexer);
7114           goto success;
7115
7116         default:
7117           /* Error.  We know the following require will fail, but
7118              that gives the proper error message.  */
7119           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7120           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7121           expr = error_mark_node;
7122           goto failure;
7123         }
7124     }
7125
7126  success:
7127   /* If we're processing a template, we can't finish the semantics yet.
7128      Otherwise we can fold the entire expression now.  */
7129   if (processing_template_decl)
7130     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7131   else
7132     expr = finish_offsetof (expr);
7133
7134  failure:
7135   parser->integral_constant_expression_p = save_ice_p;
7136   parser->non_integral_constant_expression_p = save_non_ice_p;
7137
7138   return expr;
7139 }
7140
7141 /* Parse a trait expression.
7142
7143    Returns a representation of the expression, the underlying type
7144    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7145
7146 static tree
7147 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7148 {
7149   cp_trait_kind kind;
7150   tree type1, type2 = NULL_TREE;
7151   bool binary = false;
7152   cp_decl_specifier_seq decl_specs;
7153
7154   switch (keyword)
7155     {
7156     case RID_HAS_NOTHROW_ASSIGN:
7157       kind = CPTK_HAS_NOTHROW_ASSIGN;
7158       break;
7159     case RID_HAS_NOTHROW_CONSTRUCTOR:
7160       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7161       break;
7162     case RID_HAS_NOTHROW_COPY:
7163       kind = CPTK_HAS_NOTHROW_COPY;
7164       break;
7165     case RID_HAS_TRIVIAL_ASSIGN:
7166       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7167       break;
7168     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7169       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7170       break;
7171     case RID_HAS_TRIVIAL_COPY:
7172       kind = CPTK_HAS_TRIVIAL_COPY;
7173       break;
7174     case RID_HAS_TRIVIAL_DESTRUCTOR:
7175       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7176       break;
7177     case RID_HAS_VIRTUAL_DESTRUCTOR:
7178       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7179       break;
7180     case RID_IS_ABSTRACT:
7181       kind = CPTK_IS_ABSTRACT;
7182       break;
7183     case RID_IS_BASE_OF:
7184       kind = CPTK_IS_BASE_OF;
7185       binary = true;
7186       break;
7187     case RID_IS_CLASS:
7188       kind = CPTK_IS_CLASS;
7189       break;
7190     case RID_IS_CONVERTIBLE_TO:
7191       kind = CPTK_IS_CONVERTIBLE_TO;
7192       binary = true;
7193       break;
7194     case RID_IS_EMPTY:
7195       kind = CPTK_IS_EMPTY;
7196       break;
7197     case RID_IS_ENUM:
7198       kind = CPTK_IS_ENUM;
7199       break;
7200     case RID_IS_LITERAL_TYPE:
7201       kind = CPTK_IS_LITERAL_TYPE;
7202       break;
7203     case RID_IS_POD:
7204       kind = CPTK_IS_POD;
7205       break;
7206     case RID_IS_POLYMORPHIC:
7207       kind = CPTK_IS_POLYMORPHIC;
7208       break;
7209     case RID_IS_STD_LAYOUT:
7210       kind = CPTK_IS_STD_LAYOUT;
7211       break;
7212     case RID_IS_TRIVIAL:
7213       kind = CPTK_IS_TRIVIAL;
7214       break;
7215     case RID_IS_UNION:
7216       kind = CPTK_IS_UNION;
7217       break;
7218     case RID_UNDERLYING_TYPE:
7219       kind = CPTK_UNDERLYING_TYPE;
7220       break;
7221     default:
7222       gcc_unreachable ();
7223     }
7224
7225   /* Consume the token.  */
7226   cp_lexer_consume_token (parser->lexer);
7227
7228   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7229
7230   type1 = cp_parser_type_id (parser);
7231
7232   if (type1 == error_mark_node)
7233     return error_mark_node;
7234
7235   /* Build a trivial decl-specifier-seq.  */
7236   clear_decl_specs (&decl_specs);
7237   decl_specs.type = type1;
7238
7239   /* Call grokdeclarator to figure out what type this is.  */
7240   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7241                           /*initialized=*/0, /*attrlist=*/NULL);
7242
7243   if (binary)
7244     {
7245       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7246  
7247       type2 = cp_parser_type_id (parser);
7248
7249       if (type2 == error_mark_node)
7250         return error_mark_node;
7251
7252       /* Build a trivial decl-specifier-seq.  */
7253       clear_decl_specs (&decl_specs);
7254       decl_specs.type = type2;
7255
7256       /* Call grokdeclarator to figure out what type this is.  */
7257       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7258                               /*initialized=*/0, /*attrlist=*/NULL);
7259     }
7260
7261   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7262
7263   /* Complete the trait expression, which may mean either processing
7264      the trait expr now or saving it for template instantiation.  */
7265   return kind != CPTK_UNDERLYING_TYPE
7266     ? finish_trait_expr (kind, type1, type2)
7267     : finish_underlying_type (type1);
7268 }
7269
7270 /* Lambdas that appear in variable initializer or default argument scope
7271    get that in their mangling, so we need to record it.  We might as well
7272    use the count for function and namespace scopes as well.  */
7273 static GTY(()) tree lambda_scope;
7274 static GTY(()) int lambda_count;
7275 typedef struct GTY(()) tree_int
7276 {
7277   tree t;
7278   int i;
7279 } tree_int;
7280 DEF_VEC_O(tree_int);
7281 DEF_VEC_ALLOC_O(tree_int,gc);
7282 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7283
7284 static void
7285 start_lambda_scope (tree decl)
7286 {
7287   tree_int ti;
7288   gcc_assert (decl);
7289   /* Once we're inside a function, we ignore other scopes and just push
7290      the function again so that popping works properly.  */
7291   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7292     decl = current_function_decl;
7293   ti.t = lambda_scope;
7294   ti.i = lambda_count;
7295   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7296   if (lambda_scope != decl)
7297     {
7298       /* Don't reset the count if we're still in the same function.  */
7299       lambda_scope = decl;
7300       lambda_count = 0;
7301     }
7302 }
7303
7304 static void
7305 record_lambda_scope (tree lambda)
7306 {
7307   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7308   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7309 }
7310
7311 static void
7312 finish_lambda_scope (void)
7313 {
7314   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7315   if (lambda_scope != p->t)
7316     {
7317       lambda_scope = p->t;
7318       lambda_count = p->i;
7319     }
7320   VEC_pop (tree_int, lambda_scope_stack);
7321 }
7322
7323 /* Parse a lambda expression.
7324
7325    lambda-expression:
7326      lambda-introducer lambda-declarator [opt] compound-statement
7327
7328    Returns a representation of the expression.  */
7329
7330 static tree
7331 cp_parser_lambda_expression (cp_parser* parser)
7332 {
7333   tree lambda_expr = build_lambda_expr ();
7334   tree type;
7335
7336   LAMBDA_EXPR_LOCATION (lambda_expr)
7337     = cp_lexer_peek_token (parser->lexer)->location;
7338
7339   if (cp_unevaluated_operand)
7340     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7341               "lambda-expression in unevaluated context");
7342
7343   /* We may be in the middle of deferred access check.  Disable
7344      it now.  */
7345   push_deferring_access_checks (dk_no_deferred);
7346
7347   cp_parser_lambda_introducer (parser, lambda_expr);
7348
7349   type = begin_lambda_type (lambda_expr);
7350
7351   record_lambda_scope (lambda_expr);
7352
7353   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7354   determine_visibility (TYPE_NAME (type));
7355
7356   /* Now that we've started the type, add the capture fields for any
7357      explicit captures.  */
7358   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7359
7360   {
7361     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7362     unsigned int saved_num_template_parameter_lists
7363         = parser->num_template_parameter_lists;
7364
7365     parser->num_template_parameter_lists = 0;
7366
7367     /* By virtue of defining a local class, a lambda expression has access to
7368        the private variables of enclosing classes.  */
7369
7370     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7371
7372     cp_parser_lambda_body (parser, lambda_expr);
7373
7374     /* The capture list was built up in reverse order; fix that now.  */
7375     {
7376       tree newlist = NULL_TREE;
7377       tree elt, next;
7378
7379       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7380            elt; elt = next)
7381         {
7382           tree field = TREE_PURPOSE (elt);
7383           char *buf;
7384
7385           next = TREE_CHAIN (elt);
7386           TREE_CHAIN (elt) = newlist;
7387           newlist = elt;
7388
7389           /* Also add __ to the beginning of the field name so that code
7390              outside the lambda body can't see the captured name.  We could
7391              just remove the name entirely, but this is more useful for
7392              debugging.  */
7393           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7394             /* The 'this' capture already starts with __.  */
7395             continue;
7396
7397           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7398           buf[1] = buf[0] = '_';
7399           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7400                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7401           DECL_NAME (field) = get_identifier (buf);
7402         }
7403       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7404     }
7405
7406     maybe_add_lambda_conv_op (type);
7407
7408     type = finish_struct (type, /*attributes=*/NULL_TREE);
7409
7410     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7411   }
7412
7413   pop_deferring_access_checks ();
7414
7415   return build_lambda_object (lambda_expr);
7416 }
7417
7418 /* Parse the beginning of a lambda expression.
7419
7420    lambda-introducer:
7421      [ lambda-capture [opt] ]
7422
7423    LAMBDA_EXPR is the current representation of the lambda expression.  */
7424
7425 static void
7426 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7427 {
7428   /* Need commas after the first capture.  */
7429   bool first = true;
7430
7431   /* Eat the leading `['.  */
7432   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7433
7434   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7435   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7436       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7437     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7438   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7439     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7440
7441   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7442     {
7443       cp_lexer_consume_token (parser->lexer);
7444       first = false;
7445     }
7446
7447   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7448     {
7449       cp_token* capture_token;
7450       tree capture_id;
7451       tree capture_init_expr;
7452       cp_id_kind idk = CP_ID_KIND_NONE;
7453       bool explicit_init_p = false;
7454
7455       enum capture_kind_type
7456       {
7457         BY_COPY,
7458         BY_REFERENCE
7459       };
7460       enum capture_kind_type capture_kind = BY_COPY;
7461
7462       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7463         {
7464           error ("expected end of capture-list");
7465           return;
7466         }
7467
7468       if (first)
7469         first = false;
7470       else
7471         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7472
7473       /* Possibly capture `this'.  */
7474       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7475         {
7476           cp_lexer_consume_token (parser->lexer);
7477           add_capture (lambda_expr,
7478                        /*id=*/get_identifier ("__this"),
7479                        /*initializer=*/finish_this_expr(),
7480                        /*by_reference_p=*/false,
7481                        explicit_init_p);
7482           continue;
7483         }
7484
7485       /* Remember whether we want to capture as a reference or not.  */
7486       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7487         {
7488           capture_kind = BY_REFERENCE;
7489           cp_lexer_consume_token (parser->lexer);
7490         }
7491
7492       /* Get the identifier.  */
7493       capture_token = cp_lexer_peek_token (parser->lexer);
7494       capture_id = cp_parser_identifier (parser);
7495
7496       if (capture_id == error_mark_node)
7497         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7498            delimiters, but I modified this to stop on unnested ']' as well.  It
7499            was already changed to stop on unnested '}', so the
7500            "closing_parenthesis" name is no more misleading with my change.  */
7501         {
7502           cp_parser_skip_to_closing_parenthesis (parser,
7503                                                  /*recovering=*/true,
7504                                                  /*or_comma=*/true,
7505                                                  /*consume_paren=*/true);
7506           break;
7507         }
7508
7509       /* Find the initializer for this capture.  */
7510       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7511         {
7512           /* An explicit expression exists.  */
7513           cp_lexer_consume_token (parser->lexer);
7514           pedwarn (input_location, OPT_pedantic,
7515                    "ISO C++ does not allow initializers "
7516                    "in lambda expression capture lists");
7517           capture_init_expr = cp_parser_assignment_expression (parser,
7518                                                                /*cast_p=*/true,
7519                                                                &idk);
7520           explicit_init_p = true;
7521         }
7522       else
7523         {
7524           const char* error_msg;
7525
7526           /* Turn the identifier into an id-expression.  */
7527           capture_init_expr
7528             = cp_parser_lookup_name
7529                 (parser,
7530                  capture_id,
7531                  none_type,
7532                  /*is_template=*/false,
7533                  /*is_namespace=*/false,
7534                  /*check_dependency=*/true,
7535                  /*ambiguous_decls=*/NULL,
7536                  capture_token->location);
7537
7538           capture_init_expr
7539             = finish_id_expression
7540                 (capture_id,
7541                  capture_init_expr,
7542                  parser->scope,
7543                  &idk,
7544                  /*integral_constant_expression_p=*/false,
7545                  /*allow_non_integral_constant_expression_p=*/false,
7546                  /*non_integral_constant_expression_p=*/NULL,
7547                  /*template_p=*/false,
7548                  /*done=*/true,
7549                  /*address_p=*/false,
7550                  /*template_arg_p=*/false,
7551                  &error_msg,
7552                  capture_token->location);
7553         }
7554
7555       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7556         capture_init_expr
7557           = unqualified_name_lookup_error (capture_init_expr);
7558
7559       add_capture (lambda_expr,
7560                    capture_id,
7561                    capture_init_expr,
7562                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7563                    explicit_init_p);
7564     }
7565
7566   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7567 }
7568
7569 /* Parse the (optional) middle of a lambda expression.
7570
7571    lambda-declarator:
7572      ( parameter-declaration-clause [opt] )
7573        attribute-specifier [opt]
7574        mutable [opt]
7575        exception-specification [opt]
7576        lambda-return-type-clause [opt]
7577
7578    LAMBDA_EXPR is the current representation of the lambda expression.  */
7579
7580 static void
7581 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7582 {
7583   /* 5.1.1.4 of the standard says:
7584        If a lambda-expression does not include a lambda-declarator, it is as if
7585        the lambda-declarator were ().
7586      This means an empty parameter list, no attributes, and no exception
7587      specification.  */
7588   tree param_list = void_list_node;
7589   tree attributes = NULL_TREE;
7590   tree exception_spec = NULL_TREE;
7591   tree t;
7592
7593   /* The lambda-declarator is optional, but must begin with an opening
7594      parenthesis if present.  */
7595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7596     {
7597       cp_lexer_consume_token (parser->lexer);
7598
7599       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7600
7601       /* Parse parameters.  */
7602       param_list = cp_parser_parameter_declaration_clause (parser);
7603
7604       /* Default arguments shall not be specified in the
7605          parameter-declaration-clause of a lambda-declarator.  */
7606       for (t = param_list; t; t = TREE_CHAIN (t))
7607         if (TREE_PURPOSE (t))
7608           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7609                    "default argument specified for lambda parameter");
7610
7611       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7612
7613       attributes = cp_parser_attributes_opt (parser);
7614
7615       /* Parse optional `mutable' keyword.  */
7616       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7617         {
7618           cp_lexer_consume_token (parser->lexer);
7619           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7620         }
7621
7622       /* Parse optional exception specification.  */
7623       exception_spec = cp_parser_exception_specification_opt (parser);
7624
7625       /* Parse optional trailing return type.  */
7626       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7627         {
7628           cp_lexer_consume_token (parser->lexer);
7629           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7630         }
7631
7632       /* The function parameters must be in scope all the way until after the
7633          trailing-return-type in case of decltype.  */
7634       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7635         pop_binding (DECL_NAME (t), t);
7636
7637       leave_scope ();
7638     }
7639
7640   /* Create the function call operator.
7641
7642      Messing with declarators like this is no uglier than building up the
7643      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7644      other code.  */
7645   {
7646     cp_decl_specifier_seq return_type_specs;
7647     cp_declarator* declarator;
7648     tree fco;
7649     int quals;
7650     void *p;
7651
7652     clear_decl_specs (&return_type_specs);
7653     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7654       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7655     else
7656       /* Maybe we will deduce the return type later, but we can use void
7657          as a placeholder return type anyways.  */
7658       return_type_specs.type = void_type_node;
7659
7660     p = obstack_alloc (&declarator_obstack, 0);
7661
7662     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7663                                      sfk_none);
7664
7665     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7666              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7667     declarator = make_call_declarator (declarator, param_list, quals,
7668                                        exception_spec,
7669                                        /*late_return_type=*/NULL_TREE);
7670     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7671
7672     fco = grokmethod (&return_type_specs,
7673                       declarator,
7674                       attributes);
7675     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7676     DECL_ARTIFICIAL (fco) = 1;
7677
7678     finish_member_declaration (fco);
7679
7680     obstack_free (&declarator_obstack, p);
7681   }
7682 }
7683
7684 /* Parse the body of a lambda expression, which is simply
7685
7686    compound-statement
7687
7688    but which requires special handling.
7689    LAMBDA_EXPR is the current representation of the lambda expression.  */
7690
7691 static void
7692 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7693 {
7694   bool nested = (current_function_decl != NULL_TREE);
7695   if (nested)
7696     push_function_context ();
7697
7698   /* Finish the function call operator
7699      - class_specifier
7700      + late_parsing_for_member
7701      + function_definition_after_declarator
7702      + ctor_initializer_opt_and_function_body  */
7703   {
7704     tree fco = lambda_function (lambda_expr);
7705     tree body;
7706     bool done = false;
7707
7708     /* Let the front end know that we are going to be defining this
7709        function.  */
7710     start_preparsed_function (fco,
7711                               NULL_TREE,
7712                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7713
7714     start_lambda_scope (fco);
7715     body = begin_function_body ();
7716
7717     /* 5.1.1.4 of the standard says:
7718          If a lambda-expression does not include a trailing-return-type, it
7719          is as if the trailing-return-type denotes the following type:
7720           * if the compound-statement is of the form
7721                { return attribute-specifier [opt] expression ; }
7722              the type of the returned expression after lvalue-to-rvalue
7723              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7724              (_conv.array_ 4.2), and function-to-pointer conversion
7725              (_conv.func_ 4.3);
7726           * otherwise, void.  */
7727
7728     /* In a lambda that has neither a lambda-return-type-clause
7729        nor a deducible form, errors should be reported for return statements
7730        in the body.  Since we used void as the placeholder return type, parsing
7731        the body as usual will give such desired behavior.  */
7732     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7733         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7734         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7735         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7736       {
7737         tree compound_stmt;
7738         tree expr = NULL_TREE;
7739         cp_id_kind idk = CP_ID_KIND_NONE;
7740
7741         /* Parse tentatively in case there's more after the initial return
7742            statement.  */
7743         cp_parser_parse_tentatively (parser);
7744
7745         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7746         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7747
7748         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7749
7750         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7751         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7752
7753         if (cp_parser_parse_definitely (parser))
7754           {
7755             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7756
7757             compound_stmt = begin_compound_stmt (0);
7758             /* Will get error here if type not deduced yet.  */
7759             finish_return_stmt (expr);
7760             finish_compound_stmt (compound_stmt);
7761
7762             done = true;
7763           }
7764       }
7765
7766     if (!done)
7767       {
7768         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7769           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7770         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7771            cp_parser_compound_stmt does not pass it.  */
7772         cp_parser_function_body (parser);
7773         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7774       }
7775
7776     finish_function_body (body);
7777     finish_lambda_scope ();
7778
7779     /* Finish the function and generate code for it if necessary.  */
7780     expand_or_defer_fn (finish_function (/*inline*/2));
7781   }
7782
7783   if (nested)
7784     pop_function_context();
7785 }
7786
7787 /* Statements [gram.stmt.stmt]  */
7788
7789 /* Parse a statement.
7790
7791    statement:
7792      labeled-statement
7793      expression-statement
7794      compound-statement
7795      selection-statement
7796      iteration-statement
7797      jump-statement
7798      declaration-statement
7799      try-block
7800
7801   IN_COMPOUND is true when the statement is nested inside a
7802   cp_parser_compound_statement; this matters for certain pragmas.
7803
7804   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7805   is a (possibly labeled) if statement which is not enclosed in braces
7806   and has an else clause.  This is used to implement -Wparentheses.  */
7807
7808 static void
7809 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7810                      bool in_compound, bool *if_p)
7811 {
7812   tree statement;
7813   cp_token *token;
7814   location_t statement_location;
7815
7816  restart:
7817   if (if_p != NULL)
7818     *if_p = false;
7819   /* There is no statement yet.  */
7820   statement = NULL_TREE;
7821   /* Peek at the next token.  */
7822   token = cp_lexer_peek_token (parser->lexer);
7823   /* Remember the location of the first token in the statement.  */
7824   statement_location = token->location;
7825   /* If this is a keyword, then that will often determine what kind of
7826      statement we have.  */
7827   if (token->type == CPP_KEYWORD)
7828     {
7829       enum rid keyword = token->keyword;
7830
7831       switch (keyword)
7832         {
7833         case RID_CASE:
7834         case RID_DEFAULT:
7835           /* Looks like a labeled-statement with a case label.
7836              Parse the label, and then use tail recursion to parse
7837              the statement.  */
7838           cp_parser_label_for_labeled_statement (parser);
7839           goto restart;
7840
7841         case RID_IF:
7842         case RID_SWITCH:
7843           statement = cp_parser_selection_statement (parser, if_p);
7844           break;
7845
7846         case RID_WHILE:
7847         case RID_DO:
7848         case RID_FOR:
7849           statement = cp_parser_iteration_statement (parser);
7850           break;
7851
7852         case RID_BREAK:
7853         case RID_CONTINUE:
7854         case RID_RETURN:
7855         case RID_GOTO:
7856           statement = cp_parser_jump_statement (parser);
7857           break;
7858
7859           /* Objective-C++ exception-handling constructs.  */
7860         case RID_AT_TRY:
7861         case RID_AT_CATCH:
7862         case RID_AT_FINALLY:
7863         case RID_AT_SYNCHRONIZED:
7864         case RID_AT_THROW:
7865           statement = cp_parser_objc_statement (parser);
7866           break;
7867
7868         case RID_TRY:
7869           statement = cp_parser_try_block (parser);
7870           break;
7871
7872         case RID_NAMESPACE:
7873           /* This must be a namespace alias definition.  */
7874           cp_parser_declaration_statement (parser);
7875           return;
7876           
7877         default:
7878           /* It might be a keyword like `int' that can start a
7879              declaration-statement.  */
7880           break;
7881         }
7882     }
7883   else if (token->type == CPP_NAME)
7884     {
7885       /* If the next token is a `:', then we are looking at a
7886          labeled-statement.  */
7887       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7888       if (token->type == CPP_COLON)
7889         {
7890           /* Looks like a labeled-statement with an ordinary label.
7891              Parse the label, and then use tail recursion to parse
7892              the statement.  */
7893           cp_parser_label_for_labeled_statement (parser);
7894           goto restart;
7895         }
7896     }
7897   /* Anything that starts with a `{' must be a compound-statement.  */
7898   else if (token->type == CPP_OPEN_BRACE)
7899     statement = cp_parser_compound_statement (parser, NULL, false, false);
7900   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7901      a statement all its own.  */
7902   else if (token->type == CPP_PRAGMA)
7903     {
7904       /* Only certain OpenMP pragmas are attached to statements, and thus
7905          are considered statements themselves.  All others are not.  In
7906          the context of a compound, accept the pragma as a "statement" and
7907          return so that we can check for a close brace.  Otherwise we
7908          require a real statement and must go back and read one.  */
7909       if (in_compound)
7910         cp_parser_pragma (parser, pragma_compound);
7911       else if (!cp_parser_pragma (parser, pragma_stmt))
7912         goto restart;
7913       return;
7914     }
7915   else if (token->type == CPP_EOF)
7916     {
7917       cp_parser_error (parser, "expected statement");
7918       return;
7919     }
7920
7921   /* Everything else must be a declaration-statement or an
7922      expression-statement.  Try for the declaration-statement
7923      first, unless we are looking at a `;', in which case we know that
7924      we have an expression-statement.  */
7925   if (!statement)
7926     {
7927       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7928         {
7929           cp_parser_parse_tentatively (parser);
7930           /* Try to parse the declaration-statement.  */
7931           cp_parser_declaration_statement (parser);
7932           /* If that worked, we're done.  */
7933           if (cp_parser_parse_definitely (parser))
7934             return;
7935         }
7936       /* Look for an expression-statement instead.  */
7937       statement = cp_parser_expression_statement (parser, in_statement_expr);
7938     }
7939
7940   /* Set the line number for the statement.  */
7941   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7942     SET_EXPR_LOCATION (statement, statement_location);
7943 }
7944
7945 /* Parse the label for a labeled-statement, i.e.
7946
7947    identifier :
7948    case constant-expression :
7949    default :
7950
7951    GNU Extension:
7952    case constant-expression ... constant-expression : statement
7953
7954    When a label is parsed without errors, the label is added to the
7955    parse tree by the finish_* functions, so this function doesn't
7956    have to return the label.  */
7957
7958 static void
7959 cp_parser_label_for_labeled_statement (cp_parser* parser)
7960 {
7961   cp_token *token;
7962   tree label = NULL_TREE;
7963   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7964
7965   /* The next token should be an identifier.  */
7966   token = cp_lexer_peek_token (parser->lexer);
7967   if (token->type != CPP_NAME
7968       && token->type != CPP_KEYWORD)
7969     {
7970       cp_parser_error (parser, "expected labeled-statement");
7971       return;
7972     }
7973
7974   parser->colon_corrects_to_scope_p = false;
7975   switch (token->keyword)
7976     {
7977     case RID_CASE:
7978       {
7979         tree expr, expr_hi;
7980         cp_token *ellipsis;
7981
7982         /* Consume the `case' token.  */
7983         cp_lexer_consume_token (parser->lexer);
7984         /* Parse the constant-expression.  */
7985         expr = cp_parser_constant_expression (parser,
7986                                               /*allow_non_constant_p=*/false,
7987                                               NULL);
7988
7989         ellipsis = cp_lexer_peek_token (parser->lexer);
7990         if (ellipsis->type == CPP_ELLIPSIS)
7991           {
7992             /* Consume the `...' token.  */
7993             cp_lexer_consume_token (parser->lexer);
7994             expr_hi =
7995               cp_parser_constant_expression (parser,
7996                                              /*allow_non_constant_p=*/false,
7997                                              NULL);
7998             /* We don't need to emit warnings here, as the common code
7999                will do this for us.  */
8000           }
8001         else
8002           expr_hi = NULL_TREE;
8003
8004         if (parser->in_switch_statement_p)
8005           finish_case_label (token->location, expr, expr_hi);
8006         else
8007           error_at (token->location,
8008                     "case label %qE not within a switch statement",
8009                     expr);
8010       }
8011       break;
8012
8013     case RID_DEFAULT:
8014       /* Consume the `default' token.  */
8015       cp_lexer_consume_token (parser->lexer);
8016
8017       if (parser->in_switch_statement_p)
8018         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8019       else
8020         error_at (token->location, "case label not within a switch statement");
8021       break;
8022
8023     default:
8024       /* Anything else must be an ordinary label.  */
8025       label = finish_label_stmt (cp_parser_identifier (parser));
8026       break;
8027     }
8028
8029   /* Require the `:' token.  */
8030   cp_parser_require (parser, CPP_COLON, RT_COLON);
8031
8032   /* An ordinary label may optionally be followed by attributes.
8033      However, this is only permitted if the attributes are then
8034      followed by a semicolon.  This is because, for backward
8035      compatibility, when parsing
8036        lab: __attribute__ ((unused)) int i;
8037      we want the attribute to attach to "i", not "lab".  */
8038   if (label != NULL_TREE
8039       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8040     {
8041       tree attrs;
8042
8043       cp_parser_parse_tentatively (parser);
8044       attrs = cp_parser_attributes_opt (parser);
8045       if (attrs == NULL_TREE
8046           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8047         cp_parser_abort_tentative_parse (parser);
8048       else if (!cp_parser_parse_definitely (parser))
8049         ;
8050       else
8051         cplus_decl_attributes (&label, attrs, 0);
8052     }
8053
8054   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8055 }
8056
8057 /* Parse an expression-statement.
8058
8059    expression-statement:
8060      expression [opt] ;
8061
8062    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8063    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8064    indicates whether this expression-statement is part of an
8065    expression statement.  */
8066
8067 static tree
8068 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8069 {
8070   tree statement = NULL_TREE;
8071   cp_token *token = cp_lexer_peek_token (parser->lexer);
8072
8073   /* If the next token is a ';', then there is no expression
8074      statement.  */
8075   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8076     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8077
8078   /* Give a helpful message for "A<T>::type t;" and the like.  */
8079   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8080       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8081     {
8082       if (TREE_CODE (statement) == SCOPE_REF)
8083         error_at (token->location, "need %<typename%> before %qE because "
8084                   "%qT is a dependent scope",
8085                   statement, TREE_OPERAND (statement, 0));
8086       else if (is_overloaded_fn (statement)
8087                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8088         {
8089           /* A::A a; */
8090           tree fn = get_first_fn (statement);
8091           error_at (token->location,
8092                     "%<%T::%D%> names the constructor, not the type",
8093                     DECL_CONTEXT (fn), DECL_NAME (fn));
8094         }
8095     }
8096
8097   /* Consume the final `;'.  */
8098   cp_parser_consume_semicolon_at_end_of_statement (parser);
8099
8100   if (in_statement_expr
8101       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8102     /* This is the final expression statement of a statement
8103        expression.  */
8104     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8105   else if (statement)
8106     statement = finish_expr_stmt (statement);
8107   else
8108     finish_stmt ();
8109
8110   return statement;
8111 }
8112
8113 /* Parse a compound-statement.
8114
8115    compound-statement:
8116      { statement-seq [opt] }
8117
8118    GNU extension:
8119
8120    compound-statement:
8121      { label-declaration-seq [opt] statement-seq [opt] }
8122
8123    label-declaration-seq:
8124      label-declaration
8125      label-declaration-seq label-declaration
8126
8127    Returns a tree representing the statement.  */
8128
8129 static tree
8130 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8131                               bool in_try, bool function_body)
8132 {
8133   tree compound_stmt;
8134
8135   /* Consume the `{'.  */
8136   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8137     return error_mark_node;
8138   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8139       && !function_body)
8140     pedwarn (input_location, OPT_pedantic,
8141              "compound-statement in constexpr function");
8142   /* Begin the compound-statement.  */
8143   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8144   /* If the next keyword is `__label__' we have a label declaration.  */
8145   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8146     cp_parser_label_declaration (parser);
8147   /* Parse an (optional) statement-seq.  */
8148   cp_parser_statement_seq_opt (parser, in_statement_expr);
8149   /* Finish the compound-statement.  */
8150   finish_compound_stmt (compound_stmt);
8151   /* Consume the `}'.  */
8152   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8153
8154   return compound_stmt;
8155 }
8156
8157 /* Parse an (optional) statement-seq.
8158
8159    statement-seq:
8160      statement
8161      statement-seq [opt] statement  */
8162
8163 static void
8164 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8165 {
8166   /* Scan statements until there aren't any more.  */
8167   while (true)
8168     {
8169       cp_token *token = cp_lexer_peek_token (parser->lexer);
8170
8171       /* If we are looking at a `}', then we have run out of
8172          statements; the same is true if we have reached the end
8173          of file, or have stumbled upon a stray '@end'.  */
8174       if (token->type == CPP_CLOSE_BRACE
8175           || token->type == CPP_EOF
8176           || token->type == CPP_PRAGMA_EOL
8177           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8178         break;
8179       
8180       /* If we are in a compound statement and find 'else' then
8181          something went wrong.  */
8182       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8183         {
8184           if (parser->in_statement & IN_IF_STMT) 
8185             break;
8186           else
8187             {
8188               token = cp_lexer_consume_token (parser->lexer);
8189               error_at (token->location, "%<else%> without a previous %<if%>");
8190             }
8191         }
8192
8193       /* Parse the statement.  */
8194       cp_parser_statement (parser, in_statement_expr, true, NULL);
8195     }
8196 }
8197
8198 /* Parse a selection-statement.
8199
8200    selection-statement:
8201      if ( condition ) statement
8202      if ( condition ) statement else statement
8203      switch ( condition ) statement
8204
8205    Returns the new IF_STMT or SWITCH_STMT.
8206
8207    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8208    is a (possibly labeled) if statement which is not enclosed in
8209    braces and has an else clause.  This is used to implement
8210    -Wparentheses.  */
8211
8212 static tree
8213 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8214 {
8215   cp_token *token;
8216   enum rid keyword;
8217
8218   if (if_p != NULL)
8219     *if_p = false;
8220
8221   /* Peek at the next token.  */
8222   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8223
8224   /* See what kind of keyword it is.  */
8225   keyword = token->keyword;
8226   switch (keyword)
8227     {
8228     case RID_IF:
8229     case RID_SWITCH:
8230       {
8231         tree statement;
8232         tree condition;
8233
8234         /* Look for the `('.  */
8235         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8236           {
8237             cp_parser_skip_to_end_of_statement (parser);
8238             return error_mark_node;
8239           }
8240
8241         /* Begin the selection-statement.  */
8242         if (keyword == RID_IF)
8243           statement = begin_if_stmt ();
8244         else
8245           statement = begin_switch_stmt ();
8246
8247         /* Parse the condition.  */
8248         condition = cp_parser_condition (parser);
8249         /* Look for the `)'.  */
8250         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8251           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8252                                                  /*consume_paren=*/true);
8253
8254         if (keyword == RID_IF)
8255           {
8256             bool nested_if;
8257             unsigned char in_statement;
8258
8259             /* Add the condition.  */
8260             finish_if_stmt_cond (condition, statement);
8261
8262             /* Parse the then-clause.  */
8263             in_statement = parser->in_statement;
8264             parser->in_statement |= IN_IF_STMT;
8265             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8266               {
8267                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8268                 add_stmt (build_empty_stmt (loc));
8269                 cp_lexer_consume_token (parser->lexer);
8270                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8271                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8272                               "empty body in an %<if%> statement");
8273                 nested_if = false;
8274               }
8275             else
8276               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8277             parser->in_statement = in_statement;
8278
8279             finish_then_clause (statement);
8280
8281             /* If the next token is `else', parse the else-clause.  */
8282             if (cp_lexer_next_token_is_keyword (parser->lexer,
8283                                                 RID_ELSE))
8284               {
8285                 /* Consume the `else' keyword.  */
8286                 cp_lexer_consume_token (parser->lexer);
8287                 begin_else_clause (statement);
8288                 /* Parse the else-clause.  */
8289                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8290                   {
8291                     location_t loc;
8292                     loc = cp_lexer_peek_token (parser->lexer)->location;
8293                     warning_at (loc,
8294                                 OPT_Wempty_body, "suggest braces around "
8295                                 "empty body in an %<else%> statement");
8296                     add_stmt (build_empty_stmt (loc));
8297                     cp_lexer_consume_token (parser->lexer);
8298                   }
8299                 else
8300                   cp_parser_implicitly_scoped_statement (parser, NULL);
8301
8302                 finish_else_clause (statement);
8303
8304                 /* If we are currently parsing a then-clause, then
8305                    IF_P will not be NULL.  We set it to true to
8306                    indicate that this if statement has an else clause.
8307                    This may trigger the Wparentheses warning below
8308                    when we get back up to the parent if statement.  */
8309                 if (if_p != NULL)
8310                   *if_p = true;
8311               }
8312             else
8313               {
8314                 /* This if statement does not have an else clause.  If
8315                    NESTED_IF is true, then the then-clause is an if
8316                    statement which does have an else clause.  We warn
8317                    about the potential ambiguity.  */
8318                 if (nested_if)
8319                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8320                               "suggest explicit braces to avoid ambiguous"
8321                               " %<else%>");
8322               }
8323
8324             /* Now we're all done with the if-statement.  */
8325             finish_if_stmt (statement);
8326           }
8327         else
8328           {
8329             bool in_switch_statement_p;
8330             unsigned char in_statement;
8331
8332             /* Add the condition.  */
8333             finish_switch_cond (condition, statement);
8334
8335             /* Parse the body of the switch-statement.  */
8336             in_switch_statement_p = parser->in_switch_statement_p;
8337             in_statement = parser->in_statement;
8338             parser->in_switch_statement_p = true;
8339             parser->in_statement |= IN_SWITCH_STMT;
8340             cp_parser_implicitly_scoped_statement (parser, NULL);
8341             parser->in_switch_statement_p = in_switch_statement_p;
8342             parser->in_statement = in_statement;
8343
8344             /* Now we're all done with the switch-statement.  */
8345             finish_switch_stmt (statement);
8346           }
8347
8348         return statement;
8349       }
8350       break;
8351
8352     default:
8353       cp_parser_error (parser, "expected selection-statement");
8354       return error_mark_node;
8355     }
8356 }
8357
8358 /* Parse a condition.
8359
8360    condition:
8361      expression
8362      type-specifier-seq declarator = initializer-clause
8363      type-specifier-seq declarator braced-init-list
8364
8365    GNU Extension:
8366
8367    condition:
8368      type-specifier-seq declarator asm-specification [opt]
8369        attributes [opt] = assignment-expression
8370
8371    Returns the expression that should be tested.  */
8372
8373 static tree
8374 cp_parser_condition (cp_parser* parser)
8375 {
8376   cp_decl_specifier_seq type_specifiers;
8377   const char *saved_message;
8378   int declares_class_or_enum;
8379
8380   /* Try the declaration first.  */
8381   cp_parser_parse_tentatively (parser);
8382   /* New types are not allowed in the type-specifier-seq for a
8383      condition.  */
8384   saved_message = parser->type_definition_forbidden_message;
8385   parser->type_definition_forbidden_message
8386     = G_("types may not be defined in conditions");
8387   /* Parse the type-specifier-seq.  */
8388   cp_parser_decl_specifier_seq (parser,
8389                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8390                                 &type_specifiers,
8391                                 &declares_class_or_enum);
8392   /* Restore the saved message.  */
8393   parser->type_definition_forbidden_message = saved_message;
8394   /* If all is well, we might be looking at a declaration.  */
8395   if (!cp_parser_error_occurred (parser))
8396     {
8397       tree decl;
8398       tree asm_specification;
8399       tree attributes;
8400       cp_declarator *declarator;
8401       tree initializer = NULL_TREE;
8402
8403       /* Parse the declarator.  */
8404       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8405                                          /*ctor_dtor_or_conv_p=*/NULL,
8406                                          /*parenthesized_p=*/NULL,
8407                                          /*member_p=*/false);
8408       /* Parse the attributes.  */
8409       attributes = cp_parser_attributes_opt (parser);
8410       /* Parse the asm-specification.  */
8411       asm_specification = cp_parser_asm_specification_opt (parser);
8412       /* If the next token is not an `=' or '{', then we might still be
8413          looking at an expression.  For example:
8414
8415            if (A(a).x)
8416
8417          looks like a decl-specifier-seq and a declarator -- but then
8418          there is no `=', so this is an expression.  */
8419       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8420           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8421         cp_parser_simulate_error (parser);
8422         
8423       /* If we did see an `=' or '{', then we are looking at a declaration
8424          for sure.  */
8425       if (cp_parser_parse_definitely (parser))
8426         {
8427           tree pushed_scope;
8428           bool non_constant_p;
8429           bool flags = LOOKUP_ONLYCONVERTING;
8430
8431           /* Create the declaration.  */
8432           decl = start_decl (declarator, &type_specifiers,
8433                              /*initialized_p=*/true,
8434                              attributes, /*prefix_attributes=*/NULL_TREE,
8435                              &pushed_scope);
8436
8437           /* Parse the initializer.  */
8438           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8439             {
8440               initializer = cp_parser_braced_list (parser, &non_constant_p);
8441               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8442               flags = 0;
8443             }
8444           else
8445             {
8446               /* Consume the `='.  */
8447               cp_parser_require (parser, CPP_EQ, RT_EQ);
8448               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8449             }
8450           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8451             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8452
8453           /* Process the initializer.  */
8454           cp_finish_decl (decl,
8455                           initializer, !non_constant_p,
8456                           asm_specification,
8457                           flags);
8458
8459           if (pushed_scope)
8460             pop_scope (pushed_scope);
8461
8462           return convert_from_reference (decl);
8463         }
8464     }
8465   /* If we didn't even get past the declarator successfully, we are
8466      definitely not looking at a declaration.  */
8467   else
8468     cp_parser_abort_tentative_parse (parser);
8469
8470   /* Otherwise, we are looking at an expression.  */
8471   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8472 }
8473
8474 /* Parses a for-statement or range-for-statement until the closing ')',
8475    not included. */
8476
8477 static tree
8478 cp_parser_for (cp_parser *parser)
8479 {
8480   tree init, scope, decl;
8481   bool is_range_for;
8482
8483   /* Begin the for-statement.  */
8484   scope = begin_for_scope (&init);
8485
8486   /* Parse the initialization.  */
8487   is_range_for = cp_parser_for_init_statement (parser, &decl);
8488
8489   if (is_range_for)
8490     return cp_parser_range_for (parser, scope, init, decl);
8491   else
8492     return cp_parser_c_for (parser, scope, init);
8493 }
8494
8495 static tree
8496 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8497 {
8498   /* Normal for loop */
8499   tree condition = NULL_TREE;
8500   tree expression = NULL_TREE;
8501   tree stmt;
8502
8503   stmt = begin_for_stmt (scope, init);
8504   /* The for-init-statement has already been parsed in
8505      cp_parser_for_init_statement, so no work is needed here.  */
8506   finish_for_init_stmt (stmt);
8507
8508   /* If there's a condition, process it.  */
8509   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8510     condition = cp_parser_condition (parser);
8511   finish_for_cond (condition, stmt);
8512   /* Look for the `;'.  */
8513   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8514
8515   /* If there's an expression, process it.  */
8516   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8517     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8518   finish_for_expr (expression, stmt);
8519
8520   return stmt;
8521 }
8522
8523 /* Tries to parse a range-based for-statement:
8524
8525   range-based-for:
8526     decl-specifier-seq declarator : expression
8527
8528   The decl-specifier-seq declarator and the `:' are already parsed by
8529   cp_parser_for_init_statement. If processing_template_decl it returns a
8530   newly created RANGE_FOR_STMT; if not, it is converted to a
8531   regular FOR_STMT.  */
8532
8533 static tree
8534 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8535 {
8536   tree stmt, range_expr;
8537
8538   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8539     {
8540       bool expr_non_constant_p;
8541       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8542     }
8543   else
8544     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8545
8546   /* If in template, STMT is converted to a normal for-statement
8547      at instantiation. If not, it is done just ahead. */
8548   if (processing_template_decl)
8549     {
8550       stmt = begin_range_for_stmt (scope, init);
8551       finish_range_for_decl (stmt, range_decl, range_expr);
8552     }
8553   else
8554     {
8555       stmt = begin_for_stmt (scope, init);
8556       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8557     }
8558   return stmt;
8559 }
8560
8561 /* Converts a range-based for-statement into a normal
8562    for-statement, as per the definition.
8563
8564       for (RANGE_DECL : RANGE_EXPR)
8565         BLOCK
8566
8567    should be equivalent to:
8568
8569       {
8570         auto &&__range = RANGE_EXPR;
8571         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8572               __begin != __end;
8573               ++__begin)
8574           {
8575               RANGE_DECL = *__begin;
8576               BLOCK
8577           }
8578       }
8579
8580    If RANGE_EXPR is an array:
8581         BEGIN_EXPR = __range
8582         END_EXPR = __range + ARRAY_SIZE(__range)
8583    Else if RANGE_EXPR has a member 'begin' or 'end':
8584         BEGIN_EXPR = __range.begin()
8585         END_EXPR = __range.end()
8586    Else:
8587         BEGIN_EXPR = begin(__range)
8588         END_EXPR = end(__range);
8589
8590    If __range has a member 'begin' but not 'end', or vice versa, we must
8591    still use the second alternative (it will surely fail, however).
8592    When calling begin()/end() in the third alternative we must use
8593    argument dependent lookup, but always considering 'std' as an associated
8594    namespace.  */
8595
8596 tree
8597 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8598 {
8599   tree range_type, range_temp;
8600   tree begin, end;
8601   tree iter_type, begin_expr, end_expr;
8602   tree condition, expression;
8603
8604   if (range_decl == error_mark_node || range_expr == error_mark_node)
8605     /* If an error happened previously do nothing or else a lot of
8606        unhelpful errors would be issued.  */
8607     begin_expr = end_expr = iter_type = error_mark_node;
8608   else
8609     {
8610       /* Find out the type deduced by the declaration
8611          `auto &&__range = range_expr'.  */
8612       range_type = cp_build_reference_type (make_auto (), true);
8613       range_type = do_auto_deduction (range_type, range_expr,
8614                                       type_uses_auto (range_type));
8615
8616       /* Create the __range variable.  */
8617       range_temp = build_decl (input_location, VAR_DECL,
8618                                get_identifier ("__for_range"), range_type);
8619       TREE_USED (range_temp) = 1;
8620       DECL_ARTIFICIAL (range_temp) = 1;
8621       pushdecl (range_temp);
8622       cp_finish_decl (range_temp, range_expr,
8623                       /*is_constant_init*/false, NULL_TREE,
8624                       LOOKUP_ONLYCONVERTING);
8625
8626       range_temp = convert_from_reference (range_temp);
8627       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8628                                                       &begin_expr, &end_expr);
8629     }
8630
8631   /* The new for initialization statement.  */
8632   begin = build_decl (input_location, VAR_DECL,
8633                       get_identifier ("__for_begin"), iter_type);
8634   TREE_USED (begin) = 1;
8635   DECL_ARTIFICIAL (begin) = 1;
8636   pushdecl (begin);
8637   cp_finish_decl (begin, begin_expr,
8638                   /*is_constant_init*/false, NULL_TREE,
8639                   LOOKUP_ONLYCONVERTING);
8640
8641   end = build_decl (input_location, VAR_DECL,
8642                     get_identifier ("__for_end"), iter_type);
8643   TREE_USED (end) = 1;
8644   DECL_ARTIFICIAL (end) = 1;
8645   pushdecl (end);
8646   cp_finish_decl (end, end_expr,
8647                   /*is_constant_init*/false, NULL_TREE,
8648                   LOOKUP_ONLYCONVERTING);
8649
8650   finish_for_init_stmt (statement);
8651
8652   /* The new for condition.  */
8653   condition = build_x_binary_op (NE_EXPR,
8654                                  begin, ERROR_MARK,
8655                                  end, ERROR_MARK,
8656                                  NULL, tf_warning_or_error);
8657   finish_for_cond (condition, statement);
8658
8659   /* The new increment expression.  */
8660   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8661   finish_for_expr (expression, statement);
8662
8663   /* The declaration is initialized with *__begin inside the loop body.  */
8664   cp_finish_decl (range_decl,
8665                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8666                   /*is_constant_init*/false, NULL_TREE,
8667                   LOOKUP_ONLYCONVERTING);
8668
8669   return statement;
8670 }
8671
8672 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8673    We need to solve both at the same time because the method used
8674    depends on the existence of members begin or end.
8675    Returns the type deduced for the iterator expression.  */
8676
8677 static tree
8678 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8679 {
8680   if (!COMPLETE_TYPE_P (TREE_TYPE (range)))
8681     {
8682       error ("range-based %<for%> expression of type %qT "
8683              "has incomplete type", TREE_TYPE (range));
8684       *begin = *end = error_mark_node;
8685       return error_mark_node;
8686     }
8687   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8688     {
8689       /* If RANGE is an array, we will use pointer arithmetic.  */
8690       *begin = range;
8691       *end = build_binary_op (input_location, PLUS_EXPR,
8692                               range,
8693                               array_type_nelts_top (TREE_TYPE (range)),
8694                               0);
8695       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8696     }
8697   else
8698     {
8699       /* If it is not an array, we must do a bit of magic.  */
8700       tree id_begin, id_end;
8701       tree member_begin, member_end;
8702
8703       *begin = *end = error_mark_node;
8704
8705       id_begin = get_identifier ("begin");
8706       id_end = get_identifier ("end");
8707       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8708                                     /*protect=*/2, /*want_type=*/false);
8709       member_end = lookup_member (TREE_TYPE (range), id_end,
8710                                   /*protect=*/2, /*want_type=*/false);
8711
8712       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8713         {
8714           /* Use the member functions.  */
8715           if (member_begin != NULL_TREE)
8716             *begin = cp_parser_range_for_member_function (range, id_begin);
8717           else
8718             error ("range-based %<for%> expression of type %qT has an "
8719                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8720
8721           if (member_end != NULL_TREE)
8722             *end = cp_parser_range_for_member_function (range, id_end);
8723           else
8724             error ("range-based %<for%> expression of type %qT has a "
8725                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8726         }
8727       else
8728         {
8729           /* Use global functions with ADL.  */
8730           VEC(tree,gc) *vec;
8731           vec = make_tree_vector ();
8732
8733           VEC_safe_push (tree, gc, vec, range);
8734
8735           member_begin = perform_koenig_lookup (id_begin, vec,
8736                                                 /*include_std=*/true);
8737           *begin = finish_call_expr (member_begin, &vec, false, true,
8738                                      tf_warning_or_error);
8739           member_end = perform_koenig_lookup (id_end, vec,
8740                                               /*include_std=*/true);
8741           *end = finish_call_expr (member_end, &vec, false, true,
8742                                    tf_warning_or_error);
8743
8744           release_tree_vector (vec);
8745         }
8746
8747       /* Last common checks.  */
8748       if (*begin == error_mark_node || *end == error_mark_node)
8749         {
8750           /* If one of the expressions is an error do no more checks.  */
8751           *begin = *end = error_mark_node;
8752           return error_mark_node;
8753         }
8754       else
8755         {
8756           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8757           /* The unqualified type of the __begin and __end temporaries should
8758              be the same, as required by the multiple auto declaration.  */
8759           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8760             error ("inconsistent begin/end types in range-based %<for%> "
8761                    "statement: %qT and %qT",
8762                    TREE_TYPE (*begin), TREE_TYPE (*end));
8763           return iter_type;
8764         }
8765     }
8766 }
8767
8768 /* Helper function for cp_parser_perform_range_for_lookup.
8769    Builds a tree for RANGE.IDENTIFIER().  */
8770
8771 static tree
8772 cp_parser_range_for_member_function (tree range, tree identifier)
8773 {
8774   tree member, res;
8775   VEC(tree,gc) *vec;
8776
8777   member = finish_class_member_access_expr (range, identifier,
8778                                             false, tf_warning_or_error);
8779   if (member == error_mark_node)
8780     return error_mark_node;
8781
8782   vec = make_tree_vector ();
8783   res = finish_call_expr (member, &vec,
8784                           /*disallow_virtual=*/false,
8785                           /*koenig_p=*/false,
8786                           tf_warning_or_error);
8787   release_tree_vector (vec);
8788   return res;
8789 }
8790
8791 /* Parse an iteration-statement.
8792
8793    iteration-statement:
8794      while ( condition ) statement
8795      do statement while ( expression ) ;
8796      for ( for-init-statement condition [opt] ; expression [opt] )
8797        statement
8798
8799    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8800
8801 static tree
8802 cp_parser_iteration_statement (cp_parser* parser)
8803 {
8804   cp_token *token;
8805   enum rid keyword;
8806   tree statement;
8807   unsigned char in_statement;
8808
8809   /* Peek at the next token.  */
8810   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8811   if (!token)
8812     return error_mark_node;
8813
8814   /* Remember whether or not we are already within an iteration
8815      statement.  */
8816   in_statement = parser->in_statement;
8817
8818   /* See what kind of keyword it is.  */
8819   keyword = token->keyword;
8820   switch (keyword)
8821     {
8822     case RID_WHILE:
8823       {
8824         tree condition;
8825
8826         /* Begin the while-statement.  */
8827         statement = begin_while_stmt ();
8828         /* Look for the `('.  */
8829         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8830         /* Parse the condition.  */
8831         condition = cp_parser_condition (parser);
8832         finish_while_stmt_cond (condition, statement);
8833         /* Look for the `)'.  */
8834         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8835         /* Parse the dependent statement.  */
8836         parser->in_statement = IN_ITERATION_STMT;
8837         cp_parser_already_scoped_statement (parser);
8838         parser->in_statement = in_statement;
8839         /* We're done with the while-statement.  */
8840         finish_while_stmt (statement);
8841       }
8842       break;
8843
8844     case RID_DO:
8845       {
8846         tree expression;
8847
8848         /* Begin the do-statement.  */
8849         statement = begin_do_stmt ();
8850         /* Parse the body of the do-statement.  */
8851         parser->in_statement = IN_ITERATION_STMT;
8852         cp_parser_implicitly_scoped_statement (parser, NULL);
8853         parser->in_statement = in_statement;
8854         finish_do_body (statement);
8855         /* Look for the `while' keyword.  */
8856         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8857         /* Look for the `('.  */
8858         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8859         /* Parse the expression.  */
8860         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8861         /* We're done with the do-statement.  */
8862         finish_do_stmt (expression, statement);
8863         /* Look for the `)'.  */
8864         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8865         /* Look for the `;'.  */
8866         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8867       }
8868       break;
8869
8870     case RID_FOR:
8871       {
8872         /* Look for the `('.  */
8873         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8874
8875         statement = cp_parser_for (parser);
8876
8877         /* Look for the `)'.  */
8878         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8879
8880         /* Parse the body of the for-statement.  */
8881         parser->in_statement = IN_ITERATION_STMT;
8882         cp_parser_already_scoped_statement (parser);
8883         parser->in_statement = in_statement;
8884
8885         /* We're done with the for-statement.  */
8886         finish_for_stmt (statement);
8887       }
8888       break;
8889
8890     default:
8891       cp_parser_error (parser, "expected iteration-statement");
8892       statement = error_mark_node;
8893       break;
8894     }
8895
8896   return statement;
8897 }
8898
8899 /* Parse a for-init-statement or the declarator of a range-based-for.
8900    Returns true if a range-based-for declaration is seen.
8901
8902    for-init-statement:
8903      expression-statement
8904      simple-declaration  */
8905
8906 static bool
8907 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8908 {
8909   /* If the next token is a `;', then we have an empty
8910      expression-statement.  Grammatically, this is also a
8911      simple-declaration, but an invalid one, because it does not
8912      declare anything.  Therefore, if we did not handle this case
8913      specially, we would issue an error message about an invalid
8914      declaration.  */
8915   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8916     {
8917       bool is_range_for = false;
8918       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8919
8920       parser->colon_corrects_to_scope_p = false;
8921
8922       /* We're going to speculatively look for a declaration, falling back
8923          to an expression, if necessary.  */
8924       cp_parser_parse_tentatively (parser);
8925       /* Parse the declaration.  */
8926       cp_parser_simple_declaration (parser,
8927                                     /*function_definition_allowed_p=*/false,
8928                                     decl);
8929       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8930       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8931         {
8932           /* It is a range-for, consume the ':' */
8933           cp_lexer_consume_token (parser->lexer);
8934           is_range_for = true;
8935           if (cxx_dialect < cxx0x)
8936             {
8937               error_at (cp_lexer_peek_token (parser->lexer)->location,
8938                         "range-based %<for%> loops are not allowed "
8939                         "in C++98 mode");
8940               *decl = error_mark_node;
8941             }
8942         }
8943       else
8944           /* The ';' is not consumed yet because we told
8945              cp_parser_simple_declaration not to.  */
8946           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8947
8948       if (cp_parser_parse_definitely (parser))
8949         return is_range_for;
8950       /* If the tentative parse failed, then we shall need to look for an
8951          expression-statement.  */
8952     }
8953   /* If we are here, it is an expression-statement.  */
8954   cp_parser_expression_statement (parser, NULL_TREE);
8955   return false;
8956 }
8957
8958 /* Parse a jump-statement.
8959
8960    jump-statement:
8961      break ;
8962      continue ;
8963      return expression [opt] ;
8964      return braced-init-list ;
8965      goto identifier ;
8966
8967    GNU extension:
8968
8969    jump-statement:
8970      goto * expression ;
8971
8972    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8973
8974 static tree
8975 cp_parser_jump_statement (cp_parser* parser)
8976 {
8977   tree statement = error_mark_node;
8978   cp_token *token;
8979   enum rid keyword;
8980   unsigned char in_statement;
8981
8982   /* Peek at the next token.  */
8983   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8984   if (!token)
8985     return error_mark_node;
8986
8987   /* See what kind of keyword it is.  */
8988   keyword = token->keyword;
8989   switch (keyword)
8990     {
8991     case RID_BREAK:
8992       in_statement = parser->in_statement & ~IN_IF_STMT;      
8993       switch (in_statement)
8994         {
8995         case 0:
8996           error_at (token->location, "break statement not within loop or switch");
8997           break;
8998         default:
8999           gcc_assert ((in_statement & IN_SWITCH_STMT)
9000                       || in_statement == IN_ITERATION_STMT);
9001           statement = finish_break_stmt ();
9002           break;
9003         case IN_OMP_BLOCK:
9004           error_at (token->location, "invalid exit from OpenMP structured block");
9005           break;
9006         case IN_OMP_FOR:
9007           error_at (token->location, "break statement used with OpenMP for loop");
9008           break;
9009         }
9010       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9011       break;
9012
9013     case RID_CONTINUE:
9014       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9015         {
9016         case 0:
9017           error_at (token->location, "continue statement not within a loop");
9018           break;
9019         case IN_ITERATION_STMT:
9020         case IN_OMP_FOR:
9021           statement = finish_continue_stmt ();
9022           break;
9023         case IN_OMP_BLOCK:
9024           error_at (token->location, "invalid exit from OpenMP structured block");
9025           break;
9026         default:
9027           gcc_unreachable ();
9028         }
9029       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9030       break;
9031
9032     case RID_RETURN:
9033       {
9034         tree expr;
9035         bool expr_non_constant_p;
9036
9037         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9038           {
9039             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9040             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9041           }
9042         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9043           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9044         else
9045           /* If the next token is a `;', then there is no
9046              expression.  */
9047           expr = NULL_TREE;
9048         /* Build the return-statement.  */
9049         statement = finish_return_stmt (expr);
9050         /* Look for the final `;'.  */
9051         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9052       }
9053       break;
9054
9055     case RID_GOTO:
9056       /* Create the goto-statement.  */
9057       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9058         {
9059           /* Issue a warning about this use of a GNU extension.  */
9060           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9061           /* Consume the '*' token.  */
9062           cp_lexer_consume_token (parser->lexer);
9063           /* Parse the dependent expression.  */
9064           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9065         }
9066       else
9067         finish_goto_stmt (cp_parser_identifier (parser));
9068       /* Look for the final `;'.  */
9069       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9070       break;
9071
9072     default:
9073       cp_parser_error (parser, "expected jump-statement");
9074       break;
9075     }
9076
9077   return statement;
9078 }
9079
9080 /* Parse a declaration-statement.
9081
9082    declaration-statement:
9083      block-declaration  */
9084
9085 static void
9086 cp_parser_declaration_statement (cp_parser* parser)
9087 {
9088   void *p;
9089
9090   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9091   p = obstack_alloc (&declarator_obstack, 0);
9092
9093  /* Parse the block-declaration.  */
9094   cp_parser_block_declaration (parser, /*statement_p=*/true);
9095
9096   /* Free any declarators allocated.  */
9097   obstack_free (&declarator_obstack, p);
9098
9099   /* Finish off the statement.  */
9100   finish_stmt ();
9101 }
9102
9103 /* Some dependent statements (like `if (cond) statement'), are
9104    implicitly in their own scope.  In other words, if the statement is
9105    a single statement (as opposed to a compound-statement), it is
9106    none-the-less treated as if it were enclosed in braces.  Any
9107    declarations appearing in the dependent statement are out of scope
9108    after control passes that point.  This function parses a statement,
9109    but ensures that is in its own scope, even if it is not a
9110    compound-statement.
9111
9112    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9113    is a (possibly labeled) if statement which is not enclosed in
9114    braces and has an else clause.  This is used to implement
9115    -Wparentheses.
9116
9117    Returns the new statement.  */
9118
9119 static tree
9120 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9121 {
9122   tree statement;
9123
9124   if (if_p != NULL)
9125     *if_p = false;
9126
9127   /* Mark if () ; with a special NOP_EXPR.  */
9128   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9129     {
9130       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9131       cp_lexer_consume_token (parser->lexer);
9132       statement = add_stmt (build_empty_stmt (loc));
9133     }
9134   /* if a compound is opened, we simply parse the statement directly.  */
9135   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9136     statement = cp_parser_compound_statement (parser, NULL, false, false);
9137   /* If the token is not a `{', then we must take special action.  */
9138   else
9139     {
9140       /* Create a compound-statement.  */
9141       statement = begin_compound_stmt (0);
9142       /* Parse the dependent-statement.  */
9143       cp_parser_statement (parser, NULL_TREE, false, if_p);
9144       /* Finish the dummy compound-statement.  */
9145       finish_compound_stmt (statement);
9146     }
9147
9148   /* Return the statement.  */
9149   return statement;
9150 }
9151
9152 /* For some dependent statements (like `while (cond) statement'), we
9153    have already created a scope.  Therefore, even if the dependent
9154    statement is a compound-statement, we do not want to create another
9155    scope.  */
9156
9157 static void
9158 cp_parser_already_scoped_statement (cp_parser* parser)
9159 {
9160   /* If the token is a `{', then we must take special action.  */
9161   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9162     cp_parser_statement (parser, NULL_TREE, false, NULL);
9163   else
9164     {
9165       /* Avoid calling cp_parser_compound_statement, so that we
9166          don't create a new scope.  Do everything else by hand.  */
9167       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9168       /* If the next keyword is `__label__' we have a label declaration.  */
9169       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9170         cp_parser_label_declaration (parser);
9171       /* Parse an (optional) statement-seq.  */
9172       cp_parser_statement_seq_opt (parser, NULL_TREE);
9173       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9174     }
9175 }
9176
9177 /* Declarations [gram.dcl.dcl] */
9178
9179 /* Parse an optional declaration-sequence.
9180
9181    declaration-seq:
9182      declaration
9183      declaration-seq declaration  */
9184
9185 static void
9186 cp_parser_declaration_seq_opt (cp_parser* parser)
9187 {
9188   while (true)
9189     {
9190       cp_token *token;
9191
9192       token = cp_lexer_peek_token (parser->lexer);
9193
9194       if (token->type == CPP_CLOSE_BRACE
9195           || token->type == CPP_EOF
9196           || token->type == CPP_PRAGMA_EOL)
9197         break;
9198
9199       if (token->type == CPP_SEMICOLON)
9200         {
9201           /* A declaration consisting of a single semicolon is
9202              invalid.  Allow it unless we're being pedantic.  */
9203           cp_lexer_consume_token (parser->lexer);
9204           if (!in_system_header)
9205             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9206           continue;
9207         }
9208
9209       /* If we're entering or exiting a region that's implicitly
9210          extern "C", modify the lang context appropriately.  */
9211       if (!parser->implicit_extern_c && token->implicit_extern_c)
9212         {
9213           push_lang_context (lang_name_c);
9214           parser->implicit_extern_c = true;
9215         }
9216       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9217         {
9218           pop_lang_context ();
9219           parser->implicit_extern_c = false;
9220         }
9221
9222       if (token->type == CPP_PRAGMA)
9223         {
9224           /* A top-level declaration can consist solely of a #pragma.
9225              A nested declaration cannot, so this is done here and not
9226              in cp_parser_declaration.  (A #pragma at block scope is
9227              handled in cp_parser_statement.)  */
9228           cp_parser_pragma (parser, pragma_external);
9229           continue;
9230         }
9231
9232       /* Parse the declaration itself.  */
9233       cp_parser_declaration (parser);
9234     }
9235 }
9236
9237 /* Parse a declaration.
9238
9239    declaration:
9240      block-declaration
9241      function-definition
9242      template-declaration
9243      explicit-instantiation
9244      explicit-specialization
9245      linkage-specification
9246      namespace-definition
9247
9248    GNU extension:
9249
9250    declaration:
9251       __extension__ declaration */
9252
9253 static void
9254 cp_parser_declaration (cp_parser* parser)
9255 {
9256   cp_token token1;
9257   cp_token token2;
9258   int saved_pedantic;
9259   void *p;
9260   tree attributes = NULL_TREE;
9261
9262   /* Check for the `__extension__' keyword.  */
9263   if (cp_parser_extension_opt (parser, &saved_pedantic))
9264     {
9265       /* Parse the qualified declaration.  */
9266       cp_parser_declaration (parser);
9267       /* Restore the PEDANTIC flag.  */
9268       pedantic = saved_pedantic;
9269
9270       return;
9271     }
9272
9273   /* Try to figure out what kind of declaration is present.  */
9274   token1 = *cp_lexer_peek_token (parser->lexer);
9275
9276   if (token1.type != CPP_EOF)
9277     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9278   else
9279     {
9280       token2.type = CPP_EOF;
9281       token2.keyword = RID_MAX;
9282     }
9283
9284   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9285   p = obstack_alloc (&declarator_obstack, 0);
9286
9287   /* If the next token is `extern' and the following token is a string
9288      literal, then we have a linkage specification.  */
9289   if (token1.keyword == RID_EXTERN
9290       && cp_parser_is_string_literal (&token2))
9291     cp_parser_linkage_specification (parser);
9292   /* If the next token is `template', then we have either a template
9293      declaration, an explicit instantiation, or an explicit
9294      specialization.  */
9295   else if (token1.keyword == RID_TEMPLATE)
9296     {
9297       /* `template <>' indicates a template specialization.  */
9298       if (token2.type == CPP_LESS
9299           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9300         cp_parser_explicit_specialization (parser);
9301       /* `template <' indicates a template declaration.  */
9302       else if (token2.type == CPP_LESS)
9303         cp_parser_template_declaration (parser, /*member_p=*/false);
9304       /* Anything else must be an explicit instantiation.  */
9305       else
9306         cp_parser_explicit_instantiation (parser);
9307     }
9308   /* If the next token is `export', then we have a template
9309      declaration.  */
9310   else if (token1.keyword == RID_EXPORT)
9311     cp_parser_template_declaration (parser, /*member_p=*/false);
9312   /* If the next token is `extern', 'static' or 'inline' and the one
9313      after that is `template', we have a GNU extended explicit
9314      instantiation directive.  */
9315   else if (cp_parser_allow_gnu_extensions_p (parser)
9316            && (token1.keyword == RID_EXTERN
9317                || token1.keyword == RID_STATIC
9318                || token1.keyword == RID_INLINE)
9319            && token2.keyword == RID_TEMPLATE)
9320     cp_parser_explicit_instantiation (parser);
9321   /* If the next token is `namespace', check for a named or unnamed
9322      namespace definition.  */
9323   else if (token1.keyword == RID_NAMESPACE
9324            && (/* A named namespace definition.  */
9325                (token2.type == CPP_NAME
9326                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9327                     != CPP_EQ))
9328                /* An unnamed namespace definition.  */
9329                || token2.type == CPP_OPEN_BRACE
9330                || token2.keyword == RID_ATTRIBUTE))
9331     cp_parser_namespace_definition (parser);
9332   /* An inline (associated) namespace definition.  */
9333   else if (token1.keyword == RID_INLINE
9334            && token2.keyword == RID_NAMESPACE)
9335     cp_parser_namespace_definition (parser);
9336   /* Objective-C++ declaration/definition.  */
9337   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9338     cp_parser_objc_declaration (parser, NULL_TREE);
9339   else if (c_dialect_objc ()
9340            && token1.keyword == RID_ATTRIBUTE
9341            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9342     cp_parser_objc_declaration (parser, attributes);
9343   /* We must have either a block declaration or a function
9344      definition.  */
9345   else
9346     /* Try to parse a block-declaration, or a function-definition.  */
9347     cp_parser_block_declaration (parser, /*statement_p=*/false);
9348
9349   /* Free any declarators allocated.  */
9350   obstack_free (&declarator_obstack, p);
9351 }
9352
9353 /* Parse a block-declaration.
9354
9355    block-declaration:
9356      simple-declaration
9357      asm-definition
9358      namespace-alias-definition
9359      using-declaration
9360      using-directive
9361
9362    GNU Extension:
9363
9364    block-declaration:
9365      __extension__ block-declaration
9366
9367    C++0x Extension:
9368
9369    block-declaration:
9370      static_assert-declaration
9371
9372    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9373    part of a declaration-statement.  */
9374
9375 static void
9376 cp_parser_block_declaration (cp_parser *parser,
9377                              bool      statement_p)
9378 {
9379   cp_token *token1;
9380   int saved_pedantic;
9381
9382   /* Check for the `__extension__' keyword.  */
9383   if (cp_parser_extension_opt (parser, &saved_pedantic))
9384     {
9385       /* Parse the qualified declaration.  */
9386       cp_parser_block_declaration (parser, statement_p);
9387       /* Restore the PEDANTIC flag.  */
9388       pedantic = saved_pedantic;
9389
9390       return;
9391     }
9392
9393   /* Peek at the next token to figure out which kind of declaration is
9394      present.  */
9395   token1 = cp_lexer_peek_token (parser->lexer);
9396
9397   /* If the next keyword is `asm', we have an asm-definition.  */
9398   if (token1->keyword == RID_ASM)
9399     {
9400       if (statement_p)
9401         cp_parser_commit_to_tentative_parse (parser);
9402       cp_parser_asm_definition (parser);
9403     }
9404   /* If the next keyword is `namespace', we have a
9405      namespace-alias-definition.  */
9406   else if (token1->keyword == RID_NAMESPACE)
9407     cp_parser_namespace_alias_definition (parser);
9408   /* If the next keyword is `using', we have either a
9409      using-declaration or a using-directive.  */
9410   else if (token1->keyword == RID_USING)
9411     {
9412       cp_token *token2;
9413
9414       if (statement_p)
9415         cp_parser_commit_to_tentative_parse (parser);
9416       /* If the token after `using' is `namespace', then we have a
9417          using-directive.  */
9418       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9419       if (token2->keyword == RID_NAMESPACE)
9420         cp_parser_using_directive (parser);
9421       /* Otherwise, it's a using-declaration.  */
9422       else
9423         cp_parser_using_declaration (parser,
9424                                      /*access_declaration_p=*/false);
9425     }
9426   /* If the next keyword is `__label__' we have a misplaced label
9427      declaration.  */
9428   else if (token1->keyword == RID_LABEL)
9429     {
9430       cp_lexer_consume_token (parser->lexer);
9431       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9432       cp_parser_skip_to_end_of_statement (parser);
9433       /* If the next token is now a `;', consume it.  */
9434       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9435         cp_lexer_consume_token (parser->lexer);
9436     }
9437   /* If the next token is `static_assert' we have a static assertion.  */
9438   else if (token1->keyword == RID_STATIC_ASSERT)
9439     cp_parser_static_assert (parser, /*member_p=*/false);
9440   /* Anything else must be a simple-declaration.  */
9441   else
9442     cp_parser_simple_declaration (parser, !statement_p,
9443                                   /*maybe_range_for_decl*/NULL);
9444 }
9445
9446 /* Parse a simple-declaration.
9447
9448    simple-declaration:
9449      decl-specifier-seq [opt] init-declarator-list [opt] ;
9450
9451    init-declarator-list:
9452      init-declarator
9453      init-declarator-list , init-declarator
9454
9455    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9456    function-definition as a simple-declaration.
9457
9458    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9459    parsed declaration if it is an uninitialized single declarator not followed
9460    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9461    if present, will not be consumed.  */
9462
9463 static void
9464 cp_parser_simple_declaration (cp_parser* parser,
9465                               bool function_definition_allowed_p,
9466                               tree *maybe_range_for_decl)
9467 {
9468   cp_decl_specifier_seq decl_specifiers;
9469   int declares_class_or_enum;
9470   bool saw_declarator;
9471
9472   if (maybe_range_for_decl)
9473     *maybe_range_for_decl = NULL_TREE;
9474
9475   /* Defer access checks until we know what is being declared; the
9476      checks for names appearing in the decl-specifier-seq should be
9477      done as if we were in the scope of the thing being declared.  */
9478   push_deferring_access_checks (dk_deferred);
9479
9480   /* Parse the decl-specifier-seq.  We have to keep track of whether
9481      or not the decl-specifier-seq declares a named class or
9482      enumeration type, since that is the only case in which the
9483      init-declarator-list is allowed to be empty.
9484
9485      [dcl.dcl]
9486
9487      In a simple-declaration, the optional init-declarator-list can be
9488      omitted only when declaring a class or enumeration, that is when
9489      the decl-specifier-seq contains either a class-specifier, an
9490      elaborated-type-specifier, or an enum-specifier.  */
9491   cp_parser_decl_specifier_seq (parser,
9492                                 CP_PARSER_FLAGS_OPTIONAL,
9493                                 &decl_specifiers,
9494                                 &declares_class_or_enum);
9495   /* We no longer need to defer access checks.  */
9496   stop_deferring_access_checks ();
9497
9498   /* In a block scope, a valid declaration must always have a
9499      decl-specifier-seq.  By not trying to parse declarators, we can
9500      resolve the declaration/expression ambiguity more quickly.  */
9501   if (!function_definition_allowed_p
9502       && !decl_specifiers.any_specifiers_p)
9503     {
9504       cp_parser_error (parser, "expected declaration");
9505       goto done;
9506     }
9507
9508   /* If the next two tokens are both identifiers, the code is
9509      erroneous. The usual cause of this situation is code like:
9510
9511        T t;
9512
9513      where "T" should name a type -- but does not.  */
9514   if (!decl_specifiers.any_type_specifiers_p
9515       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9516     {
9517       /* If parsing tentatively, we should commit; we really are
9518          looking at a declaration.  */
9519       cp_parser_commit_to_tentative_parse (parser);
9520       /* Give up.  */
9521       goto done;
9522     }
9523
9524   /* If we have seen at least one decl-specifier, and the next token
9525      is not a parenthesis, then we must be looking at a declaration.
9526      (After "int (" we might be looking at a functional cast.)  */
9527   if (decl_specifiers.any_specifiers_p
9528       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9529       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9530       && !cp_parser_error_occurred (parser))
9531     cp_parser_commit_to_tentative_parse (parser);
9532
9533   /* Keep going until we hit the `;' at the end of the simple
9534      declaration.  */
9535   saw_declarator = false;
9536   while (cp_lexer_next_token_is_not (parser->lexer,
9537                                      CPP_SEMICOLON))
9538     {
9539       cp_token *token;
9540       bool function_definition_p;
9541       tree decl;
9542
9543       if (saw_declarator)
9544         {
9545           /* If we are processing next declarator, coma is expected */
9546           token = cp_lexer_peek_token (parser->lexer);
9547           gcc_assert (token->type == CPP_COMMA);
9548           cp_lexer_consume_token (parser->lexer);
9549           if (maybe_range_for_decl)
9550             *maybe_range_for_decl = error_mark_node;
9551         }
9552       else
9553         saw_declarator = true;
9554
9555       /* Parse the init-declarator.  */
9556       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9557                                         /*checks=*/NULL,
9558                                         function_definition_allowed_p,
9559                                         /*member_p=*/false,
9560                                         declares_class_or_enum,
9561                                         &function_definition_p,
9562                                         maybe_range_for_decl);
9563       /* If an error occurred while parsing tentatively, exit quickly.
9564          (That usually happens when in the body of a function; each
9565          statement is treated as a declaration-statement until proven
9566          otherwise.)  */
9567       if (cp_parser_error_occurred (parser))
9568         goto done;
9569       /* Handle function definitions specially.  */
9570       if (function_definition_p)
9571         {
9572           /* If the next token is a `,', then we are probably
9573              processing something like:
9574
9575                void f() {}, *p;
9576
9577              which is erroneous.  */
9578           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9579             {
9580               cp_token *token = cp_lexer_peek_token (parser->lexer);
9581               error_at (token->location,
9582                         "mixing"
9583                         " declarations and function-definitions is forbidden");
9584             }
9585           /* Otherwise, we're done with the list of declarators.  */
9586           else
9587             {
9588               pop_deferring_access_checks ();
9589               return;
9590             }
9591         }
9592       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9593         *maybe_range_for_decl = decl;
9594       /* The next token should be either a `,' or a `;'.  */
9595       token = cp_lexer_peek_token (parser->lexer);
9596       /* If it's a `,', there are more declarators to come.  */
9597       if (token->type == CPP_COMMA)
9598         /* will be consumed next time around */;
9599       /* If it's a `;', we are done.  */
9600       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9601         break;
9602       /* Anything else is an error.  */
9603       else
9604         {
9605           /* If we have already issued an error message we don't need
9606              to issue another one.  */
9607           if (decl != error_mark_node
9608               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9609             cp_parser_error (parser, "expected %<,%> or %<;%>");
9610           /* Skip tokens until we reach the end of the statement.  */
9611           cp_parser_skip_to_end_of_statement (parser);
9612           /* If the next token is now a `;', consume it.  */
9613           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9614             cp_lexer_consume_token (parser->lexer);
9615           goto done;
9616         }
9617       /* After the first time around, a function-definition is not
9618          allowed -- even if it was OK at first.  For example:
9619
9620            int i, f() {}
9621
9622          is not valid.  */
9623       function_definition_allowed_p = false;
9624     }
9625
9626   /* Issue an error message if no declarators are present, and the
9627      decl-specifier-seq does not itself declare a class or
9628      enumeration.  */
9629   if (!saw_declarator)
9630     {
9631       if (cp_parser_declares_only_class_p (parser))
9632         shadow_tag (&decl_specifiers);
9633       /* Perform any deferred access checks.  */
9634       perform_deferred_access_checks ();
9635     }
9636
9637   /* Consume the `;'.  */
9638   if (!maybe_range_for_decl)
9639       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9640
9641  done:
9642   pop_deferring_access_checks ();
9643 }
9644
9645 /* Parse a decl-specifier-seq.
9646
9647    decl-specifier-seq:
9648      decl-specifier-seq [opt] decl-specifier
9649
9650    decl-specifier:
9651      storage-class-specifier
9652      type-specifier
9653      function-specifier
9654      friend
9655      typedef
9656
9657    GNU Extension:
9658
9659    decl-specifier:
9660      attributes
9661
9662    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9663
9664    The parser flags FLAGS is used to control type-specifier parsing.
9665
9666    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9667    flags:
9668
9669      1: one of the decl-specifiers is an elaborated-type-specifier
9670         (i.e., a type declaration)
9671      2: one of the decl-specifiers is an enum-specifier or a
9672         class-specifier (i.e., a type definition)
9673
9674    */
9675
9676 static void
9677 cp_parser_decl_specifier_seq (cp_parser* parser,
9678                               cp_parser_flags flags,
9679                               cp_decl_specifier_seq *decl_specs,
9680                               int* declares_class_or_enum)
9681 {
9682   bool constructor_possible_p = !parser->in_declarator_p;
9683   cp_token *start_token = NULL;
9684
9685   /* Clear DECL_SPECS.  */
9686   clear_decl_specs (decl_specs);
9687
9688   /* Assume no class or enumeration type is declared.  */
9689   *declares_class_or_enum = 0;
9690
9691   /* Keep reading specifiers until there are no more to read.  */
9692   while (true)
9693     {
9694       bool constructor_p;
9695       bool found_decl_spec;
9696       cp_token *token;
9697
9698       /* Peek at the next token.  */
9699       token = cp_lexer_peek_token (parser->lexer);
9700
9701       /* Save the first token of the decl spec list for error
9702          reporting.  */
9703       if (!start_token)
9704         start_token = token;
9705       /* Handle attributes.  */
9706       if (token->keyword == RID_ATTRIBUTE)
9707         {
9708           /* Parse the attributes.  */
9709           decl_specs->attributes
9710             = chainon (decl_specs->attributes,
9711                        cp_parser_attributes_opt (parser));
9712           continue;
9713         }
9714       /* Assume we will find a decl-specifier keyword.  */
9715       found_decl_spec = true;
9716       /* If the next token is an appropriate keyword, we can simply
9717          add it to the list.  */
9718       switch (token->keyword)
9719         {
9720           /* decl-specifier:
9721                friend
9722                constexpr */
9723         case RID_FRIEND:
9724           if (!at_class_scope_p ())
9725             {
9726               error_at (token->location, "%<friend%> used outside of class");
9727               cp_lexer_purge_token (parser->lexer);
9728             }
9729           else
9730             {
9731               ++decl_specs->specs[(int) ds_friend];
9732               /* Consume the token.  */
9733               cp_lexer_consume_token (parser->lexer);
9734             }
9735           break;
9736
9737         case RID_CONSTEXPR:
9738           ++decl_specs->specs[(int) ds_constexpr];
9739           cp_lexer_consume_token (parser->lexer);
9740           break;
9741
9742           /* function-specifier:
9743                inline
9744                virtual
9745                explicit  */
9746         case RID_INLINE:
9747         case RID_VIRTUAL:
9748         case RID_EXPLICIT:
9749           cp_parser_function_specifier_opt (parser, decl_specs);
9750           break;
9751
9752           /* decl-specifier:
9753                typedef  */
9754         case RID_TYPEDEF:
9755           ++decl_specs->specs[(int) ds_typedef];
9756           /* Consume the token.  */
9757           cp_lexer_consume_token (parser->lexer);
9758           /* A constructor declarator cannot appear in a typedef.  */
9759           constructor_possible_p = false;
9760           /* The "typedef" keyword can only occur in a declaration; we
9761              may as well commit at this point.  */
9762           cp_parser_commit_to_tentative_parse (parser);
9763
9764           if (decl_specs->storage_class != sc_none)
9765             decl_specs->conflicting_specifiers_p = true;
9766           break;
9767
9768           /* storage-class-specifier:
9769                auto
9770                register
9771                static
9772                extern
9773                mutable
9774
9775              GNU Extension:
9776                thread  */
9777         case RID_AUTO:
9778           if (cxx_dialect == cxx98) 
9779             {
9780               /* Consume the token.  */
9781               cp_lexer_consume_token (parser->lexer);
9782
9783               /* Complain about `auto' as a storage specifier, if
9784                  we're complaining about C++0x compatibility.  */
9785               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9786                           " will change meaning in C++0x; please remove it");
9787
9788               /* Set the storage class anyway.  */
9789               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9790                                            token->location);
9791             }
9792           else
9793             /* C++0x auto type-specifier.  */
9794             found_decl_spec = false;
9795           break;
9796
9797         case RID_REGISTER:
9798         case RID_STATIC:
9799         case RID_EXTERN:
9800         case RID_MUTABLE:
9801           /* Consume the token.  */
9802           cp_lexer_consume_token (parser->lexer);
9803           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9804                                        token->location);
9805           break;
9806         case RID_THREAD:
9807           /* Consume the token.  */
9808           cp_lexer_consume_token (parser->lexer);
9809           ++decl_specs->specs[(int) ds_thread];
9810           break;
9811
9812         default:
9813           /* We did not yet find a decl-specifier yet.  */
9814           found_decl_spec = false;
9815           break;
9816         }
9817
9818       if (found_decl_spec
9819           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9820           && token->keyword != RID_CONSTEXPR)
9821         error ("decl-specifier invalid in condition");
9822
9823       /* Constructors are a special case.  The `S' in `S()' is not a
9824          decl-specifier; it is the beginning of the declarator.  */
9825       constructor_p
9826         = (!found_decl_spec
9827            && constructor_possible_p
9828            && (cp_parser_constructor_declarator_p
9829                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9830
9831       /* If we don't have a DECL_SPEC yet, then we must be looking at
9832          a type-specifier.  */
9833       if (!found_decl_spec && !constructor_p)
9834         {
9835           int decl_spec_declares_class_or_enum;
9836           bool is_cv_qualifier;
9837           tree type_spec;
9838
9839           type_spec
9840             = cp_parser_type_specifier (parser, flags,
9841                                         decl_specs,
9842                                         /*is_declaration=*/true,
9843                                         &decl_spec_declares_class_or_enum,
9844                                         &is_cv_qualifier);
9845           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9846
9847           /* If this type-specifier referenced a user-defined type
9848              (a typedef, class-name, etc.), then we can't allow any
9849              more such type-specifiers henceforth.
9850
9851              [dcl.spec]
9852
9853              The longest sequence of decl-specifiers that could
9854              possibly be a type name is taken as the
9855              decl-specifier-seq of a declaration.  The sequence shall
9856              be self-consistent as described below.
9857
9858              [dcl.type]
9859
9860              As a general rule, at most one type-specifier is allowed
9861              in the complete decl-specifier-seq of a declaration.  The
9862              only exceptions are the following:
9863
9864              -- const or volatile can be combined with any other
9865                 type-specifier.
9866
9867              -- signed or unsigned can be combined with char, long,
9868                 short, or int.
9869
9870              -- ..
9871
9872              Example:
9873
9874                typedef char* Pc;
9875                void g (const int Pc);
9876
9877              Here, Pc is *not* part of the decl-specifier seq; it's
9878              the declarator.  Therefore, once we see a type-specifier
9879              (other than a cv-qualifier), we forbid any additional
9880              user-defined types.  We *do* still allow things like `int
9881              int' to be considered a decl-specifier-seq, and issue the
9882              error message later.  */
9883           if (type_spec && !is_cv_qualifier)
9884             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9885           /* A constructor declarator cannot follow a type-specifier.  */
9886           if (type_spec)
9887             {
9888               constructor_possible_p = false;
9889               found_decl_spec = true;
9890               if (!is_cv_qualifier)
9891                 decl_specs->any_type_specifiers_p = true;
9892             }
9893         }
9894
9895       /* If we still do not have a DECL_SPEC, then there are no more
9896          decl-specifiers.  */
9897       if (!found_decl_spec)
9898         break;
9899
9900       decl_specs->any_specifiers_p = true;
9901       /* After we see one decl-specifier, further decl-specifiers are
9902          always optional.  */
9903       flags |= CP_PARSER_FLAGS_OPTIONAL;
9904     }
9905
9906   cp_parser_check_decl_spec (decl_specs, start_token->location);
9907
9908   /* Don't allow a friend specifier with a class definition.  */
9909   if (decl_specs->specs[(int) ds_friend] != 0
9910       && (*declares_class_or_enum & 2))
9911     error_at (start_token->location,
9912               "class definition may not be declared a friend");
9913 }
9914
9915 /* Parse an (optional) storage-class-specifier.
9916
9917    storage-class-specifier:
9918      auto
9919      register
9920      static
9921      extern
9922      mutable
9923
9924    GNU Extension:
9925
9926    storage-class-specifier:
9927      thread
9928
9929    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9930
9931 static tree
9932 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9933 {
9934   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9935     {
9936     case RID_AUTO:
9937       if (cxx_dialect != cxx98)
9938         return NULL_TREE;
9939       /* Fall through for C++98.  */
9940
9941     case RID_REGISTER:
9942     case RID_STATIC:
9943     case RID_EXTERN:
9944     case RID_MUTABLE:
9945     case RID_THREAD:
9946       /* Consume the token.  */
9947       return cp_lexer_consume_token (parser->lexer)->u.value;
9948
9949     default:
9950       return NULL_TREE;
9951     }
9952 }
9953
9954 /* Parse an (optional) function-specifier.
9955
9956    function-specifier:
9957      inline
9958      virtual
9959      explicit
9960
9961    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9962    Updates DECL_SPECS, if it is non-NULL.  */
9963
9964 static tree
9965 cp_parser_function_specifier_opt (cp_parser* parser,
9966                                   cp_decl_specifier_seq *decl_specs)
9967 {
9968   cp_token *token = cp_lexer_peek_token (parser->lexer);
9969   switch (token->keyword)
9970     {
9971     case RID_INLINE:
9972       if (decl_specs)
9973         ++decl_specs->specs[(int) ds_inline];
9974       break;
9975
9976     case RID_VIRTUAL:
9977       /* 14.5.2.3 [temp.mem]
9978
9979          A member function template shall not be virtual.  */
9980       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9981         error_at (token->location, "templates may not be %<virtual%>");
9982       else if (decl_specs)
9983         ++decl_specs->specs[(int) ds_virtual];
9984       break;
9985
9986     case RID_EXPLICIT:
9987       if (decl_specs)
9988         ++decl_specs->specs[(int) ds_explicit];
9989       break;
9990
9991     default:
9992       return NULL_TREE;
9993     }
9994
9995   /* Consume the token.  */
9996   return cp_lexer_consume_token (parser->lexer)->u.value;
9997 }
9998
9999 /* Parse a linkage-specification.
10000
10001    linkage-specification:
10002      extern string-literal { declaration-seq [opt] }
10003      extern string-literal declaration  */
10004
10005 static void
10006 cp_parser_linkage_specification (cp_parser* parser)
10007 {
10008   tree linkage;
10009
10010   /* Look for the `extern' keyword.  */
10011   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10012
10013   /* Look for the string-literal.  */
10014   linkage = cp_parser_string_literal (parser, false, false);
10015
10016   /* Transform the literal into an identifier.  If the literal is a
10017      wide-character string, or contains embedded NULs, then we can't
10018      handle it as the user wants.  */
10019   if (strlen (TREE_STRING_POINTER (linkage))
10020       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10021     {
10022       cp_parser_error (parser, "invalid linkage-specification");
10023       /* Assume C++ linkage.  */
10024       linkage = lang_name_cplusplus;
10025     }
10026   else
10027     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10028
10029   /* We're now using the new linkage.  */
10030   push_lang_context (linkage);
10031
10032   /* If the next token is a `{', then we're using the first
10033      production.  */
10034   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10035     {
10036       /* Consume the `{' token.  */
10037       cp_lexer_consume_token (parser->lexer);
10038       /* Parse the declarations.  */
10039       cp_parser_declaration_seq_opt (parser);
10040       /* Look for the closing `}'.  */
10041       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10042     }
10043   /* Otherwise, there's just one declaration.  */
10044   else
10045     {
10046       bool saved_in_unbraced_linkage_specification_p;
10047
10048       saved_in_unbraced_linkage_specification_p
10049         = parser->in_unbraced_linkage_specification_p;
10050       parser->in_unbraced_linkage_specification_p = true;
10051       cp_parser_declaration (parser);
10052       parser->in_unbraced_linkage_specification_p
10053         = saved_in_unbraced_linkage_specification_p;
10054     }
10055
10056   /* We're done with the linkage-specification.  */
10057   pop_lang_context ();
10058 }
10059
10060 /* Parse a static_assert-declaration.
10061
10062    static_assert-declaration:
10063      static_assert ( constant-expression , string-literal ) ; 
10064
10065    If MEMBER_P, this static_assert is a class member.  */
10066
10067 static void 
10068 cp_parser_static_assert(cp_parser *parser, bool member_p)
10069 {
10070   tree condition;
10071   tree message;
10072   cp_token *token;
10073   location_t saved_loc;
10074   bool dummy;
10075
10076   /* Peek at the `static_assert' token so we can keep track of exactly
10077      where the static assertion started.  */
10078   token = cp_lexer_peek_token (parser->lexer);
10079   saved_loc = token->location;
10080
10081   /* Look for the `static_assert' keyword.  */
10082   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10083                                   RT_STATIC_ASSERT))
10084     return;
10085
10086   /*  We know we are in a static assertion; commit to any tentative
10087       parse.  */
10088   if (cp_parser_parsing_tentatively (parser))
10089     cp_parser_commit_to_tentative_parse (parser);
10090
10091   /* Parse the `(' starting the static assertion condition.  */
10092   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10093
10094   /* Parse the constant-expression.  Allow a non-constant expression
10095      here in order to give better diagnostics in finish_static_assert.  */
10096   condition = 
10097     cp_parser_constant_expression (parser,
10098                                    /*allow_non_constant_p=*/true,
10099                                    /*non_constant_p=*/&dummy);
10100
10101   /* Parse the separating `,'.  */
10102   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10103
10104   /* Parse the string-literal message.  */
10105   message = cp_parser_string_literal (parser, 
10106                                       /*translate=*/false,
10107                                       /*wide_ok=*/true);
10108
10109   /* A `)' completes the static assertion.  */
10110   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10111     cp_parser_skip_to_closing_parenthesis (parser, 
10112                                            /*recovering=*/true, 
10113                                            /*or_comma=*/false,
10114                                            /*consume_paren=*/true);
10115
10116   /* A semicolon terminates the declaration.  */
10117   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10118
10119   /* Complete the static assertion, which may mean either processing 
10120      the static assert now or saving it for template instantiation.  */
10121   finish_static_assert (condition, message, saved_loc, member_p);
10122 }
10123
10124 /* Parse a `decltype' type. Returns the type. 
10125
10126    simple-type-specifier:
10127      decltype ( expression )  */
10128
10129 static tree
10130 cp_parser_decltype (cp_parser *parser)
10131 {
10132   tree expr;
10133   bool id_expression_or_member_access_p = false;
10134   const char *saved_message;
10135   bool saved_integral_constant_expression_p;
10136   bool saved_non_integral_constant_expression_p;
10137   cp_token *id_expr_start_token;
10138
10139   /* Look for the `decltype' token.  */
10140   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10141     return error_mark_node;
10142
10143   /* Types cannot be defined in a `decltype' expression.  Save away the
10144      old message.  */
10145   saved_message = parser->type_definition_forbidden_message;
10146
10147   /* And create the new one.  */
10148   parser->type_definition_forbidden_message
10149     = G_("types may not be defined in %<decltype%> expressions");
10150
10151   /* The restrictions on constant-expressions do not apply inside
10152      decltype expressions.  */
10153   saved_integral_constant_expression_p
10154     = parser->integral_constant_expression_p;
10155   saved_non_integral_constant_expression_p
10156     = parser->non_integral_constant_expression_p;
10157   parser->integral_constant_expression_p = false;
10158
10159   /* Do not actually evaluate the expression.  */
10160   ++cp_unevaluated_operand;
10161
10162   /* Do not warn about problems with the expression.  */
10163   ++c_inhibit_evaluation_warnings;
10164
10165   /* Parse the opening `('.  */
10166   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10167     return error_mark_node;
10168   
10169   /* First, try parsing an id-expression.  */
10170   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10171   cp_parser_parse_tentatively (parser);
10172   expr = cp_parser_id_expression (parser,
10173                                   /*template_keyword_p=*/false,
10174                                   /*check_dependency_p=*/true,
10175                                   /*template_p=*/NULL,
10176                                   /*declarator_p=*/false,
10177                                   /*optional_p=*/false);
10178
10179   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10180     {
10181       bool non_integral_constant_expression_p = false;
10182       tree id_expression = expr;
10183       cp_id_kind idk;
10184       const char *error_msg;
10185
10186       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10187         /* Lookup the name we got back from the id-expression.  */
10188         expr = cp_parser_lookup_name (parser, expr,
10189                                       none_type,
10190                                       /*is_template=*/false,
10191                                       /*is_namespace=*/false,
10192                                       /*check_dependency=*/true,
10193                                       /*ambiguous_decls=*/NULL,
10194                                       id_expr_start_token->location);
10195
10196       if (expr
10197           && expr != error_mark_node
10198           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10199           && TREE_CODE (expr) != TYPE_DECL
10200           && (TREE_CODE (expr) != BIT_NOT_EXPR
10201               || !TYPE_P (TREE_OPERAND (expr, 0)))
10202           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10203         {
10204           /* Complete lookup of the id-expression.  */
10205           expr = (finish_id_expression
10206                   (id_expression, expr, parser->scope, &idk,
10207                    /*integral_constant_expression_p=*/false,
10208                    /*allow_non_integral_constant_expression_p=*/true,
10209                    &non_integral_constant_expression_p,
10210                    /*template_p=*/false,
10211                    /*done=*/true,
10212                    /*address_p=*/false,
10213                    /*template_arg_p=*/false,
10214                    &error_msg,
10215                    id_expr_start_token->location));
10216
10217           if (expr == error_mark_node)
10218             /* We found an id-expression, but it was something that we
10219                should not have found. This is an error, not something
10220                we can recover from, so note that we found an
10221                id-expression and we'll recover as gracefully as
10222                possible.  */
10223             id_expression_or_member_access_p = true;
10224         }
10225
10226       if (expr 
10227           && expr != error_mark_node
10228           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10229         /* We have an id-expression.  */
10230         id_expression_or_member_access_p = true;
10231     }
10232
10233   if (!id_expression_or_member_access_p)
10234     {
10235       /* Abort the id-expression parse.  */
10236       cp_parser_abort_tentative_parse (parser);
10237
10238       /* Parsing tentatively, again.  */
10239       cp_parser_parse_tentatively (parser);
10240
10241       /* Parse a class member access.  */
10242       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10243                                            /*cast_p=*/false,
10244                                            /*member_access_only_p=*/true, NULL);
10245
10246       if (expr 
10247           && expr != error_mark_node
10248           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10249         /* We have an id-expression.  */
10250         id_expression_or_member_access_p = true;
10251     }
10252
10253   if (id_expression_or_member_access_p)
10254     /* We have parsed the complete id-expression or member access.  */
10255     cp_parser_parse_definitely (parser);
10256   else
10257     {
10258       bool saved_greater_than_is_operator_p;
10259
10260       /* Abort our attempt to parse an id-expression or member access
10261          expression.  */
10262       cp_parser_abort_tentative_parse (parser);
10263
10264       /* Within a parenthesized expression, a `>' token is always
10265          the greater-than operator.  */
10266       saved_greater_than_is_operator_p
10267         = parser->greater_than_is_operator_p;
10268       parser->greater_than_is_operator_p = true;
10269
10270       /* Parse a full expression.  */
10271       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10272
10273       /* The `>' token might be the end of a template-id or
10274          template-parameter-list now.  */
10275       parser->greater_than_is_operator_p
10276         = saved_greater_than_is_operator_p;
10277     }
10278
10279   /* Go back to evaluating expressions.  */
10280   --cp_unevaluated_operand;
10281   --c_inhibit_evaluation_warnings;
10282
10283   /* Restore the old message and the integral constant expression
10284      flags.  */
10285   parser->type_definition_forbidden_message = saved_message;
10286   parser->integral_constant_expression_p
10287     = saved_integral_constant_expression_p;
10288   parser->non_integral_constant_expression_p
10289     = saved_non_integral_constant_expression_p;
10290
10291   if (expr == error_mark_node)
10292     {
10293       /* Skip everything up to the closing `)'.  */
10294       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10295                                              /*consume_paren=*/true);
10296       return error_mark_node;
10297     }
10298   
10299   /* Parse to the closing `)'.  */
10300   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10301     {
10302       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10303                                              /*consume_paren=*/true);
10304       return error_mark_node;
10305     }
10306
10307   return finish_decltype_type (expr, id_expression_or_member_access_p,
10308                                tf_warning_or_error);
10309 }
10310
10311 /* Special member functions [gram.special] */
10312
10313 /* Parse a conversion-function-id.
10314
10315    conversion-function-id:
10316      operator conversion-type-id
10317
10318    Returns an IDENTIFIER_NODE representing the operator.  */
10319
10320 static tree
10321 cp_parser_conversion_function_id (cp_parser* parser)
10322 {
10323   tree type;
10324   tree saved_scope;
10325   tree saved_qualifying_scope;
10326   tree saved_object_scope;
10327   tree pushed_scope = NULL_TREE;
10328
10329   /* Look for the `operator' token.  */
10330   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10331     return error_mark_node;
10332   /* When we parse the conversion-type-id, the current scope will be
10333      reset.  However, we need that information in able to look up the
10334      conversion function later, so we save it here.  */
10335   saved_scope = parser->scope;
10336   saved_qualifying_scope = parser->qualifying_scope;
10337   saved_object_scope = parser->object_scope;
10338   /* We must enter the scope of the class so that the names of
10339      entities declared within the class are available in the
10340      conversion-type-id.  For example, consider:
10341
10342        struct S {
10343          typedef int I;
10344          operator I();
10345        };
10346
10347        S::operator I() { ... }
10348
10349      In order to see that `I' is a type-name in the definition, we
10350      must be in the scope of `S'.  */
10351   if (saved_scope)
10352     pushed_scope = push_scope (saved_scope);
10353   /* Parse the conversion-type-id.  */
10354   type = cp_parser_conversion_type_id (parser);
10355   /* Leave the scope of the class, if any.  */
10356   if (pushed_scope)
10357     pop_scope (pushed_scope);
10358   /* Restore the saved scope.  */
10359   parser->scope = saved_scope;
10360   parser->qualifying_scope = saved_qualifying_scope;
10361   parser->object_scope = saved_object_scope;
10362   /* If the TYPE is invalid, indicate failure.  */
10363   if (type == error_mark_node)
10364     return error_mark_node;
10365   return mangle_conv_op_name_for_type (type);
10366 }
10367
10368 /* Parse a conversion-type-id:
10369
10370    conversion-type-id:
10371      type-specifier-seq conversion-declarator [opt]
10372
10373    Returns the TYPE specified.  */
10374
10375 static tree
10376 cp_parser_conversion_type_id (cp_parser* parser)
10377 {
10378   tree attributes;
10379   cp_decl_specifier_seq type_specifiers;
10380   cp_declarator *declarator;
10381   tree type_specified;
10382
10383   /* Parse the attributes.  */
10384   attributes = cp_parser_attributes_opt (parser);
10385   /* Parse the type-specifiers.  */
10386   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10387                                 /*is_trailing_return=*/false,
10388                                 &type_specifiers);
10389   /* If that didn't work, stop.  */
10390   if (type_specifiers.type == error_mark_node)
10391     return error_mark_node;
10392   /* Parse the conversion-declarator.  */
10393   declarator = cp_parser_conversion_declarator_opt (parser);
10394
10395   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10396                                     /*initialized=*/0, &attributes);
10397   if (attributes)
10398     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10399
10400   /* Don't give this error when parsing tentatively.  This happens to
10401      work because we always parse this definitively once.  */
10402   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10403       && type_uses_auto (type_specified))
10404     {
10405       error ("invalid use of %<auto%> in conversion operator");
10406       return error_mark_node;
10407     }
10408
10409   return type_specified;
10410 }
10411
10412 /* Parse an (optional) conversion-declarator.
10413
10414    conversion-declarator:
10415      ptr-operator conversion-declarator [opt]
10416
10417    */
10418
10419 static cp_declarator *
10420 cp_parser_conversion_declarator_opt (cp_parser* parser)
10421 {
10422   enum tree_code code;
10423   tree class_type;
10424   cp_cv_quals cv_quals;
10425
10426   /* We don't know if there's a ptr-operator next, or not.  */
10427   cp_parser_parse_tentatively (parser);
10428   /* Try the ptr-operator.  */
10429   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10430   /* If it worked, look for more conversion-declarators.  */
10431   if (cp_parser_parse_definitely (parser))
10432     {
10433       cp_declarator *declarator;
10434
10435       /* Parse another optional declarator.  */
10436       declarator = cp_parser_conversion_declarator_opt (parser);
10437
10438       return cp_parser_make_indirect_declarator
10439         (code, class_type, cv_quals, declarator);
10440    }
10441
10442   return NULL;
10443 }
10444
10445 /* Parse an (optional) ctor-initializer.
10446
10447    ctor-initializer:
10448      : mem-initializer-list
10449
10450    Returns TRUE iff the ctor-initializer was actually present.  */
10451
10452 static bool
10453 cp_parser_ctor_initializer_opt (cp_parser* parser)
10454 {
10455   /* If the next token is not a `:', then there is no
10456      ctor-initializer.  */
10457   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10458     {
10459       /* Do default initialization of any bases and members.  */
10460       if (DECL_CONSTRUCTOR_P (current_function_decl))
10461         finish_mem_initializers (NULL_TREE);
10462
10463       return false;
10464     }
10465
10466   /* Consume the `:' token.  */
10467   cp_lexer_consume_token (parser->lexer);
10468   /* And the mem-initializer-list.  */
10469   cp_parser_mem_initializer_list (parser);
10470
10471   return true;
10472 }
10473
10474 /* Parse a mem-initializer-list.
10475
10476    mem-initializer-list:
10477      mem-initializer ... [opt]
10478      mem-initializer ... [opt] , mem-initializer-list  */
10479
10480 static void
10481 cp_parser_mem_initializer_list (cp_parser* parser)
10482 {
10483   tree mem_initializer_list = NULL_TREE;
10484   cp_token *token = cp_lexer_peek_token (parser->lexer);
10485
10486   /* Let the semantic analysis code know that we are starting the
10487      mem-initializer-list.  */
10488   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10489     error_at (token->location,
10490               "only constructors take member initializers");
10491
10492   /* Loop through the list.  */
10493   while (true)
10494     {
10495       tree mem_initializer;
10496
10497       token = cp_lexer_peek_token (parser->lexer);
10498       /* Parse the mem-initializer.  */
10499       mem_initializer = cp_parser_mem_initializer (parser);
10500       /* If the next token is a `...', we're expanding member initializers. */
10501       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10502         {
10503           /* Consume the `...'. */
10504           cp_lexer_consume_token (parser->lexer);
10505
10506           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10507              can be expanded but members cannot. */
10508           if (mem_initializer != error_mark_node
10509               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10510             {
10511               error_at (token->location,
10512                         "cannot expand initializer for member %<%D%>",
10513                         TREE_PURPOSE (mem_initializer));
10514               mem_initializer = error_mark_node;
10515             }
10516
10517           /* Construct the pack expansion type. */
10518           if (mem_initializer != error_mark_node)
10519             mem_initializer = make_pack_expansion (mem_initializer);
10520         }
10521       /* Add it to the list, unless it was erroneous.  */
10522       if (mem_initializer != error_mark_node)
10523         {
10524           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10525           mem_initializer_list = mem_initializer;
10526         }
10527       /* If the next token is not a `,', we're done.  */
10528       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10529         break;
10530       /* Consume the `,' token.  */
10531       cp_lexer_consume_token (parser->lexer);
10532     }
10533
10534   /* Perform semantic analysis.  */
10535   if (DECL_CONSTRUCTOR_P (current_function_decl))
10536     finish_mem_initializers (mem_initializer_list);
10537 }
10538
10539 /* Parse a mem-initializer.
10540
10541    mem-initializer:
10542      mem-initializer-id ( expression-list [opt] )
10543      mem-initializer-id braced-init-list
10544
10545    GNU extension:
10546
10547    mem-initializer:
10548      ( expression-list [opt] )
10549
10550    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10551    class) or FIELD_DECL (for a non-static data member) to initialize;
10552    the TREE_VALUE is the expression-list.  An empty initialization
10553    list is represented by void_list_node.  */
10554
10555 static tree
10556 cp_parser_mem_initializer (cp_parser* parser)
10557 {
10558   tree mem_initializer_id;
10559   tree expression_list;
10560   tree member;
10561   cp_token *token = cp_lexer_peek_token (parser->lexer);
10562
10563   /* Find out what is being initialized.  */
10564   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10565     {
10566       permerror (token->location,
10567                  "anachronistic old-style base class initializer");
10568       mem_initializer_id = NULL_TREE;
10569     }
10570   else
10571     {
10572       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10573       if (mem_initializer_id == error_mark_node)
10574         return mem_initializer_id;
10575     }
10576   member = expand_member_init (mem_initializer_id);
10577   if (member && !DECL_P (member))
10578     in_base_initializer = 1;
10579
10580   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10581     {
10582       bool expr_non_constant_p;
10583       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10584       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10585       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10586       expression_list = build_tree_list (NULL_TREE, expression_list);
10587     }
10588   else
10589     {
10590       VEC(tree,gc)* vec;
10591       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10592                                                      /*cast_p=*/false,
10593                                                      /*allow_expansion_p=*/true,
10594                                                      /*non_constant_p=*/NULL);
10595       if (vec == NULL)
10596         return error_mark_node;
10597       expression_list = build_tree_list_vec (vec);
10598       release_tree_vector (vec);
10599     }
10600
10601   if (expression_list == error_mark_node)
10602     return error_mark_node;
10603   if (!expression_list)
10604     expression_list = void_type_node;
10605
10606   in_base_initializer = 0;
10607
10608   return member ? build_tree_list (member, expression_list) : error_mark_node;
10609 }
10610
10611 /* Parse a mem-initializer-id.
10612
10613    mem-initializer-id:
10614      :: [opt] nested-name-specifier [opt] class-name
10615      identifier
10616
10617    Returns a TYPE indicating the class to be initializer for the first
10618    production.  Returns an IDENTIFIER_NODE indicating the data member
10619    to be initialized for the second production.  */
10620
10621 static tree
10622 cp_parser_mem_initializer_id (cp_parser* parser)
10623 {
10624   bool global_scope_p;
10625   bool nested_name_specifier_p;
10626   bool template_p = false;
10627   tree id;
10628
10629   cp_token *token = cp_lexer_peek_token (parser->lexer);
10630
10631   /* `typename' is not allowed in this context ([temp.res]).  */
10632   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10633     {
10634       error_at (token->location, 
10635                 "keyword %<typename%> not allowed in this context (a qualified "
10636                 "member initializer is implicitly a type)");
10637       cp_lexer_consume_token (parser->lexer);
10638     }
10639   /* Look for the optional `::' operator.  */
10640   global_scope_p
10641     = (cp_parser_global_scope_opt (parser,
10642                                    /*current_scope_valid_p=*/false)
10643        != NULL_TREE);
10644   /* Look for the optional nested-name-specifier.  The simplest way to
10645      implement:
10646
10647        [temp.res]
10648
10649        The keyword `typename' is not permitted in a base-specifier or
10650        mem-initializer; in these contexts a qualified name that
10651        depends on a template-parameter is implicitly assumed to be a
10652        type name.
10653
10654      is to assume that we have seen the `typename' keyword at this
10655      point.  */
10656   nested_name_specifier_p
10657     = (cp_parser_nested_name_specifier_opt (parser,
10658                                             /*typename_keyword_p=*/true,
10659                                             /*check_dependency_p=*/true,
10660                                             /*type_p=*/true,
10661                                             /*is_declaration=*/true)
10662        != NULL_TREE);
10663   if (nested_name_specifier_p)
10664     template_p = cp_parser_optional_template_keyword (parser);
10665   /* If there is a `::' operator or a nested-name-specifier, then we
10666      are definitely looking for a class-name.  */
10667   if (global_scope_p || nested_name_specifier_p)
10668     return cp_parser_class_name (parser,
10669                                  /*typename_keyword_p=*/true,
10670                                  /*template_keyword_p=*/template_p,
10671                                  typename_type,
10672                                  /*check_dependency_p=*/true,
10673                                  /*class_head_p=*/false,
10674                                  /*is_declaration=*/true);
10675   /* Otherwise, we could also be looking for an ordinary identifier.  */
10676   cp_parser_parse_tentatively (parser);
10677   /* Try a class-name.  */
10678   id = cp_parser_class_name (parser,
10679                              /*typename_keyword_p=*/true,
10680                              /*template_keyword_p=*/false,
10681                              none_type,
10682                              /*check_dependency_p=*/true,
10683                              /*class_head_p=*/false,
10684                              /*is_declaration=*/true);
10685   /* If we found one, we're done.  */
10686   if (cp_parser_parse_definitely (parser))
10687     return id;
10688   /* Otherwise, look for an ordinary identifier.  */
10689   return cp_parser_identifier (parser);
10690 }
10691
10692 /* Overloading [gram.over] */
10693
10694 /* Parse an operator-function-id.
10695
10696    operator-function-id:
10697      operator operator
10698
10699    Returns an IDENTIFIER_NODE for the operator which is a
10700    human-readable spelling of the identifier, e.g., `operator +'.  */
10701
10702 static tree
10703 cp_parser_operator_function_id (cp_parser* parser)
10704 {
10705   /* Look for the `operator' keyword.  */
10706   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10707     return error_mark_node;
10708   /* And then the name of the operator itself.  */
10709   return cp_parser_operator (parser);
10710 }
10711
10712 /* Parse an operator.
10713
10714    operator:
10715      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10716      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10717      || ++ -- , ->* -> () []
10718
10719    GNU Extensions:
10720
10721    operator:
10722      <? >? <?= >?=
10723
10724    Returns an IDENTIFIER_NODE for the operator which is a
10725    human-readable spelling of the identifier, e.g., `operator +'.  */
10726
10727 static tree
10728 cp_parser_operator (cp_parser* parser)
10729 {
10730   tree id = NULL_TREE;
10731   cp_token *token;
10732
10733   /* Peek at the next token.  */
10734   token = cp_lexer_peek_token (parser->lexer);
10735   /* Figure out which operator we have.  */
10736   switch (token->type)
10737     {
10738     case CPP_KEYWORD:
10739       {
10740         enum tree_code op;
10741
10742         /* The keyword should be either `new' or `delete'.  */
10743         if (token->keyword == RID_NEW)
10744           op = NEW_EXPR;
10745         else if (token->keyword == RID_DELETE)
10746           op = DELETE_EXPR;
10747         else
10748           break;
10749
10750         /* Consume the `new' or `delete' token.  */
10751         cp_lexer_consume_token (parser->lexer);
10752
10753         /* Peek at the next token.  */
10754         token = cp_lexer_peek_token (parser->lexer);
10755         /* If it's a `[' token then this is the array variant of the
10756            operator.  */
10757         if (token->type == CPP_OPEN_SQUARE)
10758           {
10759             /* Consume the `[' token.  */
10760             cp_lexer_consume_token (parser->lexer);
10761             /* Look for the `]' token.  */
10762             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10763             id = ansi_opname (op == NEW_EXPR
10764                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10765           }
10766         /* Otherwise, we have the non-array variant.  */
10767         else
10768           id = ansi_opname (op);
10769
10770         return id;
10771       }
10772
10773     case CPP_PLUS:
10774       id = ansi_opname (PLUS_EXPR);
10775       break;
10776
10777     case CPP_MINUS:
10778       id = ansi_opname (MINUS_EXPR);
10779       break;
10780
10781     case CPP_MULT:
10782       id = ansi_opname (MULT_EXPR);
10783       break;
10784
10785     case CPP_DIV:
10786       id = ansi_opname (TRUNC_DIV_EXPR);
10787       break;
10788
10789     case CPP_MOD:
10790       id = ansi_opname (TRUNC_MOD_EXPR);
10791       break;
10792
10793     case CPP_XOR:
10794       id = ansi_opname (BIT_XOR_EXPR);
10795       break;
10796
10797     case CPP_AND:
10798       id = ansi_opname (BIT_AND_EXPR);
10799       break;
10800
10801     case CPP_OR:
10802       id = ansi_opname (BIT_IOR_EXPR);
10803       break;
10804
10805     case CPP_COMPL:
10806       id = ansi_opname (BIT_NOT_EXPR);
10807       break;
10808
10809     case CPP_NOT:
10810       id = ansi_opname (TRUTH_NOT_EXPR);
10811       break;
10812
10813     case CPP_EQ:
10814       id = ansi_assopname (NOP_EXPR);
10815       break;
10816
10817     case CPP_LESS:
10818       id = ansi_opname (LT_EXPR);
10819       break;
10820
10821     case CPP_GREATER:
10822       id = ansi_opname (GT_EXPR);
10823       break;
10824
10825     case CPP_PLUS_EQ:
10826       id = ansi_assopname (PLUS_EXPR);
10827       break;
10828
10829     case CPP_MINUS_EQ:
10830       id = ansi_assopname (MINUS_EXPR);
10831       break;
10832
10833     case CPP_MULT_EQ:
10834       id = ansi_assopname (MULT_EXPR);
10835       break;
10836
10837     case CPP_DIV_EQ:
10838       id = ansi_assopname (TRUNC_DIV_EXPR);
10839       break;
10840
10841     case CPP_MOD_EQ:
10842       id = ansi_assopname (TRUNC_MOD_EXPR);
10843       break;
10844
10845     case CPP_XOR_EQ:
10846       id = ansi_assopname (BIT_XOR_EXPR);
10847       break;
10848
10849     case CPP_AND_EQ:
10850       id = ansi_assopname (BIT_AND_EXPR);
10851       break;
10852
10853     case CPP_OR_EQ:
10854       id = ansi_assopname (BIT_IOR_EXPR);
10855       break;
10856
10857     case CPP_LSHIFT:
10858       id = ansi_opname (LSHIFT_EXPR);
10859       break;
10860
10861     case CPP_RSHIFT:
10862       id = ansi_opname (RSHIFT_EXPR);
10863       break;
10864
10865     case CPP_LSHIFT_EQ:
10866       id = ansi_assopname (LSHIFT_EXPR);
10867       break;
10868
10869     case CPP_RSHIFT_EQ:
10870       id = ansi_assopname (RSHIFT_EXPR);
10871       break;
10872
10873     case CPP_EQ_EQ:
10874       id = ansi_opname (EQ_EXPR);
10875       break;
10876
10877     case CPP_NOT_EQ:
10878       id = ansi_opname (NE_EXPR);
10879       break;
10880
10881     case CPP_LESS_EQ:
10882       id = ansi_opname (LE_EXPR);
10883       break;
10884
10885     case CPP_GREATER_EQ:
10886       id = ansi_opname (GE_EXPR);
10887       break;
10888
10889     case CPP_AND_AND:
10890       id = ansi_opname (TRUTH_ANDIF_EXPR);
10891       break;
10892
10893     case CPP_OR_OR:
10894       id = ansi_opname (TRUTH_ORIF_EXPR);
10895       break;
10896
10897     case CPP_PLUS_PLUS:
10898       id = ansi_opname (POSTINCREMENT_EXPR);
10899       break;
10900
10901     case CPP_MINUS_MINUS:
10902       id = ansi_opname (PREDECREMENT_EXPR);
10903       break;
10904
10905     case CPP_COMMA:
10906       id = ansi_opname (COMPOUND_EXPR);
10907       break;
10908
10909     case CPP_DEREF_STAR:
10910       id = ansi_opname (MEMBER_REF);
10911       break;
10912
10913     case CPP_DEREF:
10914       id = ansi_opname (COMPONENT_REF);
10915       break;
10916
10917     case CPP_OPEN_PAREN:
10918       /* Consume the `('.  */
10919       cp_lexer_consume_token (parser->lexer);
10920       /* Look for the matching `)'.  */
10921       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10922       return ansi_opname (CALL_EXPR);
10923
10924     case CPP_OPEN_SQUARE:
10925       /* Consume the `['.  */
10926       cp_lexer_consume_token (parser->lexer);
10927       /* Look for the matching `]'.  */
10928       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10929       return ansi_opname (ARRAY_REF);
10930
10931     default:
10932       /* Anything else is an error.  */
10933       break;
10934     }
10935
10936   /* If we have selected an identifier, we need to consume the
10937      operator token.  */
10938   if (id)
10939     cp_lexer_consume_token (parser->lexer);
10940   /* Otherwise, no valid operator name was present.  */
10941   else
10942     {
10943       cp_parser_error (parser, "expected operator");
10944       id = error_mark_node;
10945     }
10946
10947   return id;
10948 }
10949
10950 /* Parse a template-declaration.
10951
10952    template-declaration:
10953      export [opt] template < template-parameter-list > declaration
10954
10955    If MEMBER_P is TRUE, this template-declaration occurs within a
10956    class-specifier.
10957
10958    The grammar rule given by the standard isn't correct.  What
10959    is really meant is:
10960
10961    template-declaration:
10962      export [opt] template-parameter-list-seq
10963        decl-specifier-seq [opt] init-declarator [opt] ;
10964      export [opt] template-parameter-list-seq
10965        function-definition
10966
10967    template-parameter-list-seq:
10968      template-parameter-list-seq [opt]
10969      template < template-parameter-list >  */
10970
10971 static void
10972 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10973 {
10974   /* Check for `export'.  */
10975   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10976     {
10977       /* Consume the `export' token.  */
10978       cp_lexer_consume_token (parser->lexer);
10979       /* Warn that we do not support `export'.  */
10980       warning (0, "keyword %<export%> not implemented, and will be ignored");
10981     }
10982
10983   cp_parser_template_declaration_after_export (parser, member_p);
10984 }
10985
10986 /* Parse a template-parameter-list.
10987
10988    template-parameter-list:
10989      template-parameter
10990      template-parameter-list , template-parameter
10991
10992    Returns a TREE_LIST.  Each node represents a template parameter.
10993    The nodes are connected via their TREE_CHAINs.  */
10994
10995 static tree
10996 cp_parser_template_parameter_list (cp_parser* parser)
10997 {
10998   tree parameter_list = NULL_TREE;
10999
11000   begin_template_parm_list ();
11001
11002   /* The loop below parses the template parms.  We first need to know
11003      the total number of template parms to be able to compute proper
11004      canonical types of each dependent type. So after the loop, when
11005      we know the total number of template parms,
11006      end_template_parm_list computes the proper canonical types and
11007      fixes up the dependent types accordingly.  */
11008   while (true)
11009     {
11010       tree parameter;
11011       bool is_non_type;
11012       bool is_parameter_pack;
11013       location_t parm_loc;
11014
11015       /* Parse the template-parameter.  */
11016       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11017       parameter = cp_parser_template_parameter (parser, 
11018                                                 &is_non_type,
11019                                                 &is_parameter_pack);
11020       /* Add it to the list.  */
11021       if (parameter != error_mark_node)
11022         parameter_list = process_template_parm (parameter_list,
11023                                                 parm_loc,
11024                                                 parameter,
11025                                                 is_non_type,
11026                                                 is_parameter_pack,
11027                                                 0);
11028       else
11029        {
11030          tree err_parm = build_tree_list (parameter, parameter);
11031          parameter_list = chainon (parameter_list, err_parm);
11032        }
11033
11034       /* If the next token is not a `,', we're done.  */
11035       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11036         break;
11037       /* Otherwise, consume the `,' token.  */
11038       cp_lexer_consume_token (parser->lexer);
11039     }
11040
11041   return end_template_parm_list (parameter_list);
11042 }
11043
11044 /* Parse a template-parameter.
11045
11046    template-parameter:
11047      type-parameter
11048      parameter-declaration
11049
11050    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11051    the parameter.  The TREE_PURPOSE is the default value, if any.
11052    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11053    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11054    set to true iff this parameter is a parameter pack. */
11055
11056 static tree
11057 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11058                               bool *is_parameter_pack)
11059 {
11060   cp_token *token;
11061   cp_parameter_declarator *parameter_declarator;
11062   cp_declarator *id_declarator;
11063   tree parm;
11064
11065   /* Assume it is a type parameter or a template parameter.  */
11066   *is_non_type = false;
11067   /* Assume it not a parameter pack. */
11068   *is_parameter_pack = false;
11069   /* Peek at the next token.  */
11070   token = cp_lexer_peek_token (parser->lexer);
11071   /* If it is `class' or `template', we have a type-parameter.  */
11072   if (token->keyword == RID_TEMPLATE)
11073     return cp_parser_type_parameter (parser, is_parameter_pack);
11074   /* If it is `class' or `typename' we do not know yet whether it is a
11075      type parameter or a non-type parameter.  Consider:
11076
11077        template <typename T, typename T::X X> ...
11078
11079      or:
11080
11081        template <class C, class D*> ...
11082
11083      Here, the first parameter is a type parameter, and the second is
11084      a non-type parameter.  We can tell by looking at the token after
11085      the identifier -- if it is a `,', `=', or `>' then we have a type
11086      parameter.  */
11087   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11088     {
11089       /* Peek at the token after `class' or `typename'.  */
11090       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11091       /* If it's an ellipsis, we have a template type parameter
11092          pack. */
11093       if (token->type == CPP_ELLIPSIS)
11094         return cp_parser_type_parameter (parser, is_parameter_pack);
11095       /* If it's an identifier, skip it.  */
11096       if (token->type == CPP_NAME)
11097         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11098       /* Now, see if the token looks like the end of a template
11099          parameter.  */
11100       if (token->type == CPP_COMMA
11101           || token->type == CPP_EQ
11102           || token->type == CPP_GREATER)
11103         return cp_parser_type_parameter (parser, is_parameter_pack);
11104     }
11105
11106   /* Otherwise, it is a non-type parameter.
11107
11108      [temp.param]
11109
11110      When parsing a default template-argument for a non-type
11111      template-parameter, the first non-nested `>' is taken as the end
11112      of the template parameter-list rather than a greater-than
11113      operator.  */
11114   *is_non_type = true;
11115   parameter_declarator
11116      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11117                                         /*parenthesized_p=*/NULL);
11118
11119   /* If the parameter declaration is marked as a parameter pack, set
11120      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11121      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11122      grokdeclarator. */
11123   if (parameter_declarator
11124       && parameter_declarator->declarator
11125       && parameter_declarator->declarator->parameter_pack_p)
11126     {
11127       *is_parameter_pack = true;
11128       parameter_declarator->declarator->parameter_pack_p = false;
11129     }
11130
11131   /* If the next token is an ellipsis, and we don't already have it
11132      marked as a parameter pack, then we have a parameter pack (that
11133      has no declarator).  */
11134   if (!*is_parameter_pack
11135       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11136       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11137     {
11138       /* Consume the `...'.  */
11139       cp_lexer_consume_token (parser->lexer);
11140       maybe_warn_variadic_templates ();
11141       
11142       *is_parameter_pack = true;
11143     }
11144   /* We might end up with a pack expansion as the type of the non-type
11145      template parameter, in which case this is a non-type template
11146      parameter pack.  */
11147   else if (parameter_declarator
11148            && parameter_declarator->decl_specifiers.type
11149            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11150     {
11151       *is_parameter_pack = true;
11152       parameter_declarator->decl_specifiers.type = 
11153         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11154     }
11155
11156   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11157     {
11158       /* Parameter packs cannot have default arguments.  However, a
11159          user may try to do so, so we'll parse them and give an
11160          appropriate diagnostic here.  */
11161
11162       /* Consume the `='.  */
11163       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11164       cp_lexer_consume_token (parser->lexer);
11165       
11166       /* Find the name of the parameter pack.  */     
11167       id_declarator = parameter_declarator->declarator;
11168       while (id_declarator && id_declarator->kind != cdk_id)
11169         id_declarator = id_declarator->declarator;
11170       
11171       if (id_declarator && id_declarator->kind == cdk_id)
11172         error_at (start_token->location,
11173                   "template parameter pack %qD cannot have a default argument",
11174                   id_declarator->u.id.unqualified_name);
11175       else
11176         error_at (start_token->location,
11177                   "template parameter pack cannot have a default argument");
11178       
11179       /* Parse the default argument, but throw away the result.  */
11180       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11181     }
11182
11183   parm = grokdeclarator (parameter_declarator->declarator,
11184                          &parameter_declarator->decl_specifiers,
11185                          TPARM, /*initialized=*/0,
11186                          /*attrlist=*/NULL);
11187   if (parm == error_mark_node)
11188     return error_mark_node;
11189
11190   return build_tree_list (parameter_declarator->default_argument, parm);
11191 }
11192
11193 /* Parse a type-parameter.
11194
11195    type-parameter:
11196      class identifier [opt]
11197      class identifier [opt] = type-id
11198      typename identifier [opt]
11199      typename identifier [opt] = type-id
11200      template < template-parameter-list > class identifier [opt]
11201      template < template-parameter-list > class identifier [opt]
11202        = id-expression
11203
11204    GNU Extension (variadic templates):
11205
11206    type-parameter:
11207      class ... identifier [opt]
11208      typename ... identifier [opt]
11209
11210    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11211    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11212    the declaration of the parameter.
11213
11214    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11215
11216 static tree
11217 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11218 {
11219   cp_token *token;
11220   tree parameter;
11221
11222   /* Look for a keyword to tell us what kind of parameter this is.  */
11223   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11224   if (!token)
11225     return error_mark_node;
11226
11227   switch (token->keyword)
11228     {
11229     case RID_CLASS:
11230     case RID_TYPENAME:
11231       {
11232         tree identifier;
11233         tree default_argument;
11234
11235         /* If the next token is an ellipsis, we have a template
11236            argument pack. */
11237         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11238           {
11239             /* Consume the `...' token. */
11240             cp_lexer_consume_token (parser->lexer);
11241             maybe_warn_variadic_templates ();
11242
11243             *is_parameter_pack = true;
11244           }
11245
11246         /* If the next token is an identifier, then it names the
11247            parameter.  */
11248         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11249           identifier = cp_parser_identifier (parser);
11250         else
11251           identifier = NULL_TREE;
11252
11253         /* Create the parameter.  */
11254         parameter = finish_template_type_parm (class_type_node, identifier);
11255
11256         /* If the next token is an `=', we have a default argument.  */
11257         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11258           {
11259             /* Consume the `=' token.  */
11260             cp_lexer_consume_token (parser->lexer);
11261             /* Parse the default-argument.  */
11262             push_deferring_access_checks (dk_no_deferred);
11263             default_argument = cp_parser_type_id (parser);
11264
11265             /* Template parameter packs cannot have default
11266                arguments. */
11267             if (*is_parameter_pack)
11268               {
11269                 if (identifier)
11270                   error_at (token->location,
11271                             "template parameter pack %qD cannot have a "
11272                             "default argument", identifier);
11273                 else
11274                   error_at (token->location,
11275                             "template parameter packs cannot have "
11276                             "default arguments");
11277                 default_argument = NULL_TREE;
11278               }
11279             pop_deferring_access_checks ();
11280           }
11281         else
11282           default_argument = NULL_TREE;
11283
11284         /* Create the combined representation of the parameter and the
11285            default argument.  */
11286         parameter = build_tree_list (default_argument, parameter);
11287       }
11288       break;
11289
11290     case RID_TEMPLATE:
11291       {
11292         tree identifier;
11293         tree default_argument;
11294
11295         /* Look for the `<'.  */
11296         cp_parser_require (parser, CPP_LESS, RT_LESS);
11297         /* Parse the template-parameter-list.  */
11298         cp_parser_template_parameter_list (parser);
11299         /* Look for the `>'.  */
11300         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11301         /* Look for the `class' keyword.  */
11302         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11303         /* If the next token is an ellipsis, we have a template
11304            argument pack. */
11305         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11306           {
11307             /* Consume the `...' token. */
11308             cp_lexer_consume_token (parser->lexer);
11309             maybe_warn_variadic_templates ();
11310
11311             *is_parameter_pack = true;
11312           }
11313         /* If the next token is an `=', then there is a
11314            default-argument.  If the next token is a `>', we are at
11315            the end of the parameter-list.  If the next token is a `,',
11316            then we are at the end of this parameter.  */
11317         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11318             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11319             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11320           {
11321             identifier = cp_parser_identifier (parser);
11322             /* Treat invalid names as if the parameter were nameless.  */
11323             if (identifier == error_mark_node)
11324               identifier = NULL_TREE;
11325           }
11326         else
11327           identifier = NULL_TREE;
11328
11329         /* Create the template parameter.  */
11330         parameter = finish_template_template_parm (class_type_node,
11331                                                    identifier);
11332
11333         /* If the next token is an `=', then there is a
11334            default-argument.  */
11335         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11336           {
11337             bool is_template;
11338
11339             /* Consume the `='.  */
11340             cp_lexer_consume_token (parser->lexer);
11341             /* Parse the id-expression.  */
11342             push_deferring_access_checks (dk_no_deferred);
11343             /* save token before parsing the id-expression, for error
11344                reporting */
11345             token = cp_lexer_peek_token (parser->lexer);
11346             default_argument
11347               = cp_parser_id_expression (parser,
11348                                          /*template_keyword_p=*/false,
11349                                          /*check_dependency_p=*/true,
11350                                          /*template_p=*/&is_template,
11351                                          /*declarator_p=*/false,
11352                                          /*optional_p=*/false);
11353             if (TREE_CODE (default_argument) == TYPE_DECL)
11354               /* If the id-expression was a template-id that refers to
11355                  a template-class, we already have the declaration here,
11356                  so no further lookup is needed.  */
11357                  ;
11358             else
11359               /* Look up the name.  */
11360               default_argument
11361                 = cp_parser_lookup_name (parser, default_argument,
11362                                          none_type,
11363                                          /*is_template=*/is_template,
11364                                          /*is_namespace=*/false,
11365                                          /*check_dependency=*/true,
11366                                          /*ambiguous_decls=*/NULL,
11367                                          token->location);
11368             /* See if the default argument is valid.  */
11369             default_argument
11370               = check_template_template_default_arg (default_argument);
11371
11372             /* Template parameter packs cannot have default
11373                arguments. */
11374             if (*is_parameter_pack)
11375               {
11376                 if (identifier)
11377                   error_at (token->location,
11378                             "template parameter pack %qD cannot "
11379                             "have a default argument",
11380                             identifier);
11381                 else
11382                   error_at (token->location, "template parameter packs cannot "
11383                             "have default arguments");
11384                 default_argument = NULL_TREE;
11385               }
11386             pop_deferring_access_checks ();
11387           }
11388         else
11389           default_argument = NULL_TREE;
11390
11391         /* Create the combined representation of the parameter and the
11392            default argument.  */
11393         parameter = build_tree_list (default_argument, parameter);
11394       }
11395       break;
11396
11397     default:
11398       gcc_unreachable ();
11399       break;
11400     }
11401
11402   return parameter;
11403 }
11404
11405 /* Parse a template-id.
11406
11407    template-id:
11408      template-name < template-argument-list [opt] >
11409
11410    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11411    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11412    returned.  Otherwise, if the template-name names a function, or set
11413    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11414    names a class, returns a TYPE_DECL for the specialization.
11415
11416    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11417    uninstantiated templates.  */
11418
11419 static tree
11420 cp_parser_template_id (cp_parser *parser,
11421                        bool template_keyword_p,
11422                        bool check_dependency_p,
11423                        bool is_declaration)
11424 {
11425   int i;
11426   tree templ;
11427   tree arguments;
11428   tree template_id;
11429   cp_token_position start_of_id = 0;
11430   deferred_access_check *chk;
11431   VEC (deferred_access_check,gc) *access_check;
11432   cp_token *next_token = NULL, *next_token_2 = NULL;
11433   bool is_identifier;
11434
11435   /* If the next token corresponds to a template-id, there is no need
11436      to reparse it.  */
11437   next_token = cp_lexer_peek_token (parser->lexer);
11438   if (next_token->type == CPP_TEMPLATE_ID)
11439     {
11440       struct tree_check *check_value;
11441
11442       /* Get the stored value.  */
11443       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11444       /* Perform any access checks that were deferred.  */
11445       access_check = check_value->checks;
11446       if (access_check)
11447         {
11448           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11449             perform_or_defer_access_check (chk->binfo,
11450                                            chk->decl,
11451                                            chk->diag_decl);
11452         }
11453       /* Return the stored value.  */
11454       return check_value->value;
11455     }
11456
11457   /* Avoid performing name lookup if there is no possibility of
11458      finding a template-id.  */
11459   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11460       || (next_token->type == CPP_NAME
11461           && !cp_parser_nth_token_starts_template_argument_list_p
11462                (parser, 2)))
11463     {
11464       cp_parser_error (parser, "expected template-id");
11465       return error_mark_node;
11466     }
11467
11468   /* Remember where the template-id starts.  */
11469   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11470     start_of_id = cp_lexer_token_position (parser->lexer, false);
11471
11472   push_deferring_access_checks (dk_deferred);
11473
11474   /* Parse the template-name.  */
11475   is_identifier = false;
11476   templ = cp_parser_template_name (parser, template_keyword_p,
11477                                    check_dependency_p,
11478                                    is_declaration,
11479                                    &is_identifier);
11480   if (templ == error_mark_node || is_identifier)
11481     {
11482       pop_deferring_access_checks ();
11483       return templ;
11484     }
11485
11486   /* If we find the sequence `[:' after a template-name, it's probably
11487      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11488      parse correctly the argument list.  */
11489   next_token = cp_lexer_peek_token (parser->lexer);
11490   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11491   if (next_token->type == CPP_OPEN_SQUARE
11492       && next_token->flags & DIGRAPH
11493       && next_token_2->type == CPP_COLON
11494       && !(next_token_2->flags & PREV_WHITE))
11495     {
11496       cp_parser_parse_tentatively (parser);
11497       /* Change `:' into `::'.  */
11498       next_token_2->type = CPP_SCOPE;
11499       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11500          CPP_LESS.  */
11501       cp_lexer_consume_token (parser->lexer);
11502
11503       /* Parse the arguments.  */
11504       arguments = cp_parser_enclosed_template_argument_list (parser);
11505       if (!cp_parser_parse_definitely (parser))
11506         {
11507           /* If we couldn't parse an argument list, then we revert our changes
11508              and return simply an error. Maybe this is not a template-id
11509              after all.  */
11510           next_token_2->type = CPP_COLON;
11511           cp_parser_error (parser, "expected %<<%>");
11512           pop_deferring_access_checks ();
11513           return error_mark_node;
11514         }
11515       /* Otherwise, emit an error about the invalid digraph, but continue
11516          parsing because we got our argument list.  */
11517       if (permerror (next_token->location,
11518                      "%<<::%> cannot begin a template-argument list"))
11519         {
11520           static bool hint = false;
11521           inform (next_token->location,
11522                   "%<<:%> is an alternate spelling for %<[%>."
11523                   " Insert whitespace between %<<%> and %<::%>");
11524           if (!hint && !flag_permissive)
11525             {
11526               inform (next_token->location, "(if you use %<-fpermissive%>"
11527                       " G++ will accept your code)");
11528               hint = true;
11529             }
11530         }
11531     }
11532   else
11533     {
11534       /* Look for the `<' that starts the template-argument-list.  */
11535       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11536         {
11537           pop_deferring_access_checks ();
11538           return error_mark_node;
11539         }
11540       /* Parse the arguments.  */
11541       arguments = cp_parser_enclosed_template_argument_list (parser);
11542     }
11543
11544   /* Build a representation of the specialization.  */
11545   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11546     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11547   else if (DECL_CLASS_TEMPLATE_P (templ)
11548            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11549     {
11550       bool entering_scope;
11551       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11552          template (rather than some instantiation thereof) only if
11553          is not nested within some other construct.  For example, in
11554          "template <typename T> void f(T) { A<T>::", A<T> is just an
11555          instantiation of A.  */
11556       entering_scope = (template_parm_scope_p ()
11557                         && cp_lexer_next_token_is (parser->lexer,
11558                                                    CPP_SCOPE));
11559       template_id
11560         = finish_template_type (templ, arguments, entering_scope);
11561     }
11562   else
11563     {
11564       /* If it's not a class-template or a template-template, it should be
11565          a function-template.  */
11566       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11567                    || TREE_CODE (templ) == OVERLOAD
11568                    || BASELINK_P (templ)));
11569
11570       template_id = lookup_template_function (templ, arguments);
11571     }
11572
11573   /* If parsing tentatively, replace the sequence of tokens that makes
11574      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11575      should we re-parse the token stream, we will not have to repeat
11576      the effort required to do the parse, nor will we issue duplicate
11577      error messages about problems during instantiation of the
11578      template.  */
11579   if (start_of_id)
11580     {
11581       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11582
11583       /* Reset the contents of the START_OF_ID token.  */
11584       token->type = CPP_TEMPLATE_ID;
11585       /* Retrieve any deferred checks.  Do not pop this access checks yet
11586          so the memory will not be reclaimed during token replacing below.  */
11587       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11588       token->u.tree_check_value->value = template_id;
11589       token->u.tree_check_value->checks = get_deferred_access_checks ();
11590       token->keyword = RID_MAX;
11591
11592       /* Purge all subsequent tokens.  */
11593       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11594
11595       /* ??? Can we actually assume that, if template_id ==
11596          error_mark_node, we will have issued a diagnostic to the
11597          user, as opposed to simply marking the tentative parse as
11598          failed?  */
11599       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11600         error_at (token->location, "parse error in template argument list");
11601     }
11602
11603   pop_deferring_access_checks ();
11604   return template_id;
11605 }
11606
11607 /* Parse a template-name.
11608
11609    template-name:
11610      identifier
11611
11612    The standard should actually say:
11613
11614    template-name:
11615      identifier
11616      operator-function-id
11617
11618    A defect report has been filed about this issue.
11619
11620    A conversion-function-id cannot be a template name because they cannot
11621    be part of a template-id. In fact, looking at this code:
11622
11623    a.operator K<int>()
11624
11625    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11626    It is impossible to call a templated conversion-function-id with an
11627    explicit argument list, since the only allowed template parameter is
11628    the type to which it is converting.
11629
11630    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11631    `template' keyword, in a construction like:
11632
11633      T::template f<3>()
11634
11635    In that case `f' is taken to be a template-name, even though there
11636    is no way of knowing for sure.
11637
11638    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11639    name refers to a set of overloaded functions, at least one of which
11640    is a template, or an IDENTIFIER_NODE with the name of the template,
11641    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11642    names are looked up inside uninstantiated templates.  */
11643
11644 static tree
11645 cp_parser_template_name (cp_parser* parser,
11646                          bool template_keyword_p,
11647                          bool check_dependency_p,
11648                          bool is_declaration,
11649                          bool *is_identifier)
11650 {
11651   tree identifier;
11652   tree decl;
11653   tree fns;
11654   cp_token *token = cp_lexer_peek_token (parser->lexer);
11655
11656   /* If the next token is `operator', then we have either an
11657      operator-function-id or a conversion-function-id.  */
11658   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11659     {
11660       /* We don't know whether we're looking at an
11661          operator-function-id or a conversion-function-id.  */
11662       cp_parser_parse_tentatively (parser);
11663       /* Try an operator-function-id.  */
11664       identifier = cp_parser_operator_function_id (parser);
11665       /* If that didn't work, try a conversion-function-id.  */
11666       if (!cp_parser_parse_definitely (parser))
11667         {
11668           cp_parser_error (parser, "expected template-name");
11669           return error_mark_node;
11670         }
11671     }
11672   /* Look for the identifier.  */
11673   else
11674     identifier = cp_parser_identifier (parser);
11675
11676   /* If we didn't find an identifier, we don't have a template-id.  */
11677   if (identifier == error_mark_node)
11678     return error_mark_node;
11679
11680   /* If the name immediately followed the `template' keyword, then it
11681      is a template-name.  However, if the next token is not `<', then
11682      we do not treat it as a template-name, since it is not being used
11683      as part of a template-id.  This enables us to handle constructs
11684      like:
11685
11686        template <typename T> struct S { S(); };
11687        template <typename T> S<T>::S();
11688
11689      correctly.  We would treat `S' as a template -- if it were `S<T>'
11690      -- but we do not if there is no `<'.  */
11691
11692   if (processing_template_decl
11693       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11694     {
11695       /* In a declaration, in a dependent context, we pretend that the
11696          "template" keyword was present in order to improve error
11697          recovery.  For example, given:
11698
11699            template <typename T> void f(T::X<int>);
11700
11701          we want to treat "X<int>" as a template-id.  */
11702       if (is_declaration
11703           && !template_keyword_p
11704           && parser->scope && TYPE_P (parser->scope)
11705           && check_dependency_p
11706           && dependent_scope_p (parser->scope)
11707           /* Do not do this for dtors (or ctors), since they never
11708              need the template keyword before their name.  */
11709           && !constructor_name_p (identifier, parser->scope))
11710         {
11711           cp_token_position start = 0;
11712
11713           /* Explain what went wrong.  */
11714           error_at (token->location, "non-template %qD used as template",
11715                     identifier);
11716           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11717                   parser->scope, identifier);
11718           /* If parsing tentatively, find the location of the "<" token.  */
11719           if (cp_parser_simulate_error (parser))
11720             start = cp_lexer_token_position (parser->lexer, true);
11721           /* Parse the template arguments so that we can issue error
11722              messages about them.  */
11723           cp_lexer_consume_token (parser->lexer);
11724           cp_parser_enclosed_template_argument_list (parser);
11725           /* Skip tokens until we find a good place from which to
11726              continue parsing.  */
11727           cp_parser_skip_to_closing_parenthesis (parser,
11728                                                  /*recovering=*/true,
11729                                                  /*or_comma=*/true,
11730                                                  /*consume_paren=*/false);
11731           /* If parsing tentatively, permanently remove the
11732              template argument list.  That will prevent duplicate
11733              error messages from being issued about the missing
11734              "template" keyword.  */
11735           if (start)
11736             cp_lexer_purge_tokens_after (parser->lexer, start);
11737           if (is_identifier)
11738             *is_identifier = true;
11739           return identifier;
11740         }
11741
11742       /* If the "template" keyword is present, then there is generally
11743          no point in doing name-lookup, so we just return IDENTIFIER.
11744          But, if the qualifying scope is non-dependent then we can
11745          (and must) do name-lookup normally.  */
11746       if (template_keyword_p
11747           && (!parser->scope
11748               || (TYPE_P (parser->scope)
11749                   && dependent_type_p (parser->scope))))
11750         return identifier;
11751     }
11752
11753   /* Look up the name.  */
11754   decl = cp_parser_lookup_name (parser, identifier,
11755                                 none_type,
11756                                 /*is_template=*/true,
11757                                 /*is_namespace=*/false,
11758                                 check_dependency_p,
11759                                 /*ambiguous_decls=*/NULL,
11760                                 token->location);
11761
11762   /* If DECL is a template, then the name was a template-name.  */
11763   if (TREE_CODE (decl) == TEMPLATE_DECL)
11764     ;
11765   else
11766     {
11767       tree fn = NULL_TREE;
11768
11769       /* The standard does not explicitly indicate whether a name that
11770          names a set of overloaded declarations, some of which are
11771          templates, is a template-name.  However, such a name should
11772          be a template-name; otherwise, there is no way to form a
11773          template-id for the overloaded templates.  */
11774       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11775       if (TREE_CODE (fns) == OVERLOAD)
11776         for (fn = fns; fn; fn = OVL_NEXT (fn))
11777           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11778             break;
11779
11780       if (!fn)
11781         {
11782           /* The name does not name a template.  */
11783           cp_parser_error (parser, "expected template-name");
11784           return error_mark_node;
11785         }
11786     }
11787
11788   /* If DECL is dependent, and refers to a function, then just return
11789      its name; we will look it up again during template instantiation.  */
11790   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11791     {
11792       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11793       if (TYPE_P (scope) && dependent_type_p (scope))
11794         return identifier;
11795     }
11796
11797   return decl;
11798 }
11799
11800 /* Parse a template-argument-list.
11801
11802    template-argument-list:
11803      template-argument ... [opt]
11804      template-argument-list , template-argument ... [opt]
11805
11806    Returns a TREE_VEC containing the arguments.  */
11807
11808 static tree
11809 cp_parser_template_argument_list (cp_parser* parser)
11810 {
11811   tree fixed_args[10];
11812   unsigned n_args = 0;
11813   unsigned alloced = 10;
11814   tree *arg_ary = fixed_args;
11815   tree vec;
11816   bool saved_in_template_argument_list_p;
11817   bool saved_ice_p;
11818   bool saved_non_ice_p;
11819
11820   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11821   parser->in_template_argument_list_p = true;
11822   /* Even if the template-id appears in an integral
11823      constant-expression, the contents of the argument list do
11824      not.  */
11825   saved_ice_p = parser->integral_constant_expression_p;
11826   parser->integral_constant_expression_p = false;
11827   saved_non_ice_p = parser->non_integral_constant_expression_p;
11828   parser->non_integral_constant_expression_p = false;
11829   /* Parse the arguments.  */
11830   do
11831     {
11832       tree argument;
11833
11834       if (n_args)
11835         /* Consume the comma.  */
11836         cp_lexer_consume_token (parser->lexer);
11837
11838       /* Parse the template-argument.  */
11839       argument = cp_parser_template_argument (parser);
11840
11841       /* If the next token is an ellipsis, we're expanding a template
11842          argument pack. */
11843       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11844         {
11845           if (argument == error_mark_node)
11846             {
11847               cp_token *token = cp_lexer_peek_token (parser->lexer);
11848               error_at (token->location,
11849                         "expected parameter pack before %<...%>");
11850             }
11851           /* Consume the `...' token. */
11852           cp_lexer_consume_token (parser->lexer);
11853
11854           /* Make the argument into a TYPE_PACK_EXPANSION or
11855              EXPR_PACK_EXPANSION. */
11856           argument = make_pack_expansion (argument);
11857         }
11858
11859       if (n_args == alloced)
11860         {
11861           alloced *= 2;
11862
11863           if (arg_ary == fixed_args)
11864             {
11865               arg_ary = XNEWVEC (tree, alloced);
11866               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11867             }
11868           else
11869             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11870         }
11871       arg_ary[n_args++] = argument;
11872     }
11873   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11874
11875   vec = make_tree_vec (n_args);
11876
11877   while (n_args--)
11878     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11879
11880   if (arg_ary != fixed_args)
11881     free (arg_ary);
11882   parser->non_integral_constant_expression_p = saved_non_ice_p;
11883   parser->integral_constant_expression_p = saved_ice_p;
11884   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11885 #ifdef ENABLE_CHECKING
11886   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11887 #endif
11888   return vec;
11889 }
11890
11891 /* Parse a template-argument.
11892
11893    template-argument:
11894      assignment-expression
11895      type-id
11896      id-expression
11897
11898    The representation is that of an assignment-expression, type-id, or
11899    id-expression -- except that the qualified id-expression is
11900    evaluated, so that the value returned is either a DECL or an
11901    OVERLOAD.
11902
11903    Although the standard says "assignment-expression", it forbids
11904    throw-expressions or assignments in the template argument.
11905    Therefore, we use "conditional-expression" instead.  */
11906
11907 static tree
11908 cp_parser_template_argument (cp_parser* parser)
11909 {
11910   tree argument;
11911   bool template_p;
11912   bool address_p;
11913   bool maybe_type_id = false;
11914   cp_token *token = NULL, *argument_start_token = NULL;
11915   cp_id_kind idk;
11916
11917   /* There's really no way to know what we're looking at, so we just
11918      try each alternative in order.
11919
11920        [temp.arg]
11921
11922        In a template-argument, an ambiguity between a type-id and an
11923        expression is resolved to a type-id, regardless of the form of
11924        the corresponding template-parameter.
11925
11926      Therefore, we try a type-id first.  */
11927   cp_parser_parse_tentatively (parser);
11928   argument = cp_parser_template_type_arg (parser);
11929   /* If there was no error parsing the type-id but the next token is a
11930      '>>', our behavior depends on which dialect of C++ we're
11931      parsing. In C++98, we probably found a typo for '> >'. But there
11932      are type-id which are also valid expressions. For instance:
11933
11934      struct X { int operator >> (int); };
11935      template <int V> struct Foo {};
11936      Foo<X () >> 5> r;
11937
11938      Here 'X()' is a valid type-id of a function type, but the user just
11939      wanted to write the expression "X() >> 5". Thus, we remember that we
11940      found a valid type-id, but we still try to parse the argument as an
11941      expression to see what happens. 
11942
11943      In C++0x, the '>>' will be considered two separate '>'
11944      tokens.  */
11945   if (!cp_parser_error_occurred (parser)
11946       && cxx_dialect == cxx98
11947       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11948     {
11949       maybe_type_id = true;
11950       cp_parser_abort_tentative_parse (parser);
11951     }
11952   else
11953     {
11954       /* If the next token isn't a `,' or a `>', then this argument wasn't
11955       really finished. This means that the argument is not a valid
11956       type-id.  */
11957       if (!cp_parser_next_token_ends_template_argument_p (parser))
11958         cp_parser_error (parser, "expected template-argument");
11959       /* If that worked, we're done.  */
11960       if (cp_parser_parse_definitely (parser))
11961         return argument;
11962     }
11963   /* We're still not sure what the argument will be.  */
11964   cp_parser_parse_tentatively (parser);
11965   /* Try a template.  */
11966   argument_start_token = cp_lexer_peek_token (parser->lexer);
11967   argument = cp_parser_id_expression (parser,
11968                                       /*template_keyword_p=*/false,
11969                                       /*check_dependency_p=*/true,
11970                                       &template_p,
11971                                       /*declarator_p=*/false,
11972                                       /*optional_p=*/false);
11973   /* If the next token isn't a `,' or a `>', then this argument wasn't
11974      really finished.  */
11975   if (!cp_parser_next_token_ends_template_argument_p (parser))
11976     cp_parser_error (parser, "expected template-argument");
11977   if (!cp_parser_error_occurred (parser))
11978     {
11979       /* Figure out what is being referred to.  If the id-expression
11980          was for a class template specialization, then we will have a
11981          TYPE_DECL at this point.  There is no need to do name lookup
11982          at this point in that case.  */
11983       if (TREE_CODE (argument) != TYPE_DECL)
11984         argument = cp_parser_lookup_name (parser, argument,
11985                                           none_type,
11986                                           /*is_template=*/template_p,
11987                                           /*is_namespace=*/false,
11988                                           /*check_dependency=*/true,
11989                                           /*ambiguous_decls=*/NULL,
11990                                           argument_start_token->location);
11991       if (TREE_CODE (argument) != TEMPLATE_DECL
11992           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11993         cp_parser_error (parser, "expected template-name");
11994     }
11995   if (cp_parser_parse_definitely (parser))
11996     return argument;
11997   /* It must be a non-type argument.  There permitted cases are given
11998      in [temp.arg.nontype]:
11999
12000      -- an integral constant-expression of integral or enumeration
12001         type; or
12002
12003      -- the name of a non-type template-parameter; or
12004
12005      -- the name of an object or function with external linkage...
12006
12007      -- the address of an object or function with external linkage...
12008
12009      -- a pointer to member...  */
12010   /* Look for a non-type template parameter.  */
12011   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12012     {
12013       cp_parser_parse_tentatively (parser);
12014       argument = cp_parser_primary_expression (parser,
12015                                                /*address_p=*/false,
12016                                                /*cast_p=*/false,
12017                                                /*template_arg_p=*/true,
12018                                                &idk);
12019       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12020           || !cp_parser_next_token_ends_template_argument_p (parser))
12021         cp_parser_simulate_error (parser);
12022       if (cp_parser_parse_definitely (parser))
12023         return argument;
12024     }
12025
12026   /* If the next token is "&", the argument must be the address of an
12027      object or function with external linkage.  */
12028   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12029   if (address_p)
12030     cp_lexer_consume_token (parser->lexer);
12031   /* See if we might have an id-expression.  */
12032   token = cp_lexer_peek_token (parser->lexer);
12033   if (token->type == CPP_NAME
12034       || token->keyword == RID_OPERATOR
12035       || token->type == CPP_SCOPE
12036       || token->type == CPP_TEMPLATE_ID
12037       || token->type == CPP_NESTED_NAME_SPECIFIER)
12038     {
12039       cp_parser_parse_tentatively (parser);
12040       argument = cp_parser_primary_expression (parser,
12041                                                address_p,
12042                                                /*cast_p=*/false,
12043                                                /*template_arg_p=*/true,
12044                                                &idk);
12045       if (cp_parser_error_occurred (parser)
12046           || !cp_parser_next_token_ends_template_argument_p (parser))
12047         cp_parser_abort_tentative_parse (parser);
12048       else
12049         {
12050           tree probe;
12051
12052           if (TREE_CODE (argument) == INDIRECT_REF)
12053             {
12054               gcc_assert (REFERENCE_REF_P (argument));
12055               argument = TREE_OPERAND (argument, 0);
12056             }
12057
12058           /* If we're in a template, we represent a qualified-id referring
12059              to a static data member as a SCOPE_REF even if the scope isn't
12060              dependent so that we can check access control later.  */
12061           probe = argument;
12062           if (TREE_CODE (probe) == SCOPE_REF)
12063             probe = TREE_OPERAND (probe, 1);
12064           if (TREE_CODE (probe) == VAR_DECL)
12065             {
12066               /* A variable without external linkage might still be a
12067                  valid constant-expression, so no error is issued here
12068                  if the external-linkage check fails.  */
12069               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12070                 cp_parser_simulate_error (parser);
12071             }
12072           else if (is_overloaded_fn (argument))
12073             /* All overloaded functions are allowed; if the external
12074                linkage test does not pass, an error will be issued
12075                later.  */
12076             ;
12077           else if (address_p
12078                    && (TREE_CODE (argument) == OFFSET_REF
12079                        || TREE_CODE (argument) == SCOPE_REF))
12080             /* A pointer-to-member.  */
12081             ;
12082           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12083             ;
12084           else
12085             cp_parser_simulate_error (parser);
12086
12087           if (cp_parser_parse_definitely (parser))
12088             {
12089               if (address_p)
12090                 argument = build_x_unary_op (ADDR_EXPR, argument,
12091                                              tf_warning_or_error);
12092               return argument;
12093             }
12094         }
12095     }
12096   /* If the argument started with "&", there are no other valid
12097      alternatives at this point.  */
12098   if (address_p)
12099     {
12100       cp_parser_error (parser, "invalid non-type template argument");
12101       return error_mark_node;
12102     }
12103
12104   /* If the argument wasn't successfully parsed as a type-id followed
12105      by '>>', the argument can only be a constant expression now.
12106      Otherwise, we try parsing the constant-expression tentatively,
12107      because the argument could really be a type-id.  */
12108   if (maybe_type_id)
12109     cp_parser_parse_tentatively (parser);
12110   argument = cp_parser_constant_expression (parser,
12111                                             /*allow_non_constant_p=*/false,
12112                                             /*non_constant_p=*/NULL);
12113   argument = fold_non_dependent_expr (argument);
12114   if (!maybe_type_id)
12115     return argument;
12116   if (!cp_parser_next_token_ends_template_argument_p (parser))
12117     cp_parser_error (parser, "expected template-argument");
12118   if (cp_parser_parse_definitely (parser))
12119     return argument;
12120   /* We did our best to parse the argument as a non type-id, but that
12121      was the only alternative that matched (albeit with a '>' after
12122      it). We can assume it's just a typo from the user, and a
12123      diagnostic will then be issued.  */
12124   return cp_parser_template_type_arg (parser);
12125 }
12126
12127 /* Parse an explicit-instantiation.
12128
12129    explicit-instantiation:
12130      template declaration
12131
12132    Although the standard says `declaration', what it really means is:
12133
12134    explicit-instantiation:
12135      template decl-specifier-seq [opt] declarator [opt] ;
12136
12137    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12138    supposed to be allowed.  A defect report has been filed about this
12139    issue.
12140
12141    GNU Extension:
12142
12143    explicit-instantiation:
12144      storage-class-specifier template
12145        decl-specifier-seq [opt] declarator [opt] ;
12146      function-specifier template
12147        decl-specifier-seq [opt] declarator [opt] ;  */
12148
12149 static void
12150 cp_parser_explicit_instantiation (cp_parser* parser)
12151 {
12152   int declares_class_or_enum;
12153   cp_decl_specifier_seq decl_specifiers;
12154   tree extension_specifier = NULL_TREE;
12155
12156   timevar_push (TV_TEMPLATE_INST);
12157
12158   /* Look for an (optional) storage-class-specifier or
12159      function-specifier.  */
12160   if (cp_parser_allow_gnu_extensions_p (parser))
12161     {
12162       extension_specifier
12163         = cp_parser_storage_class_specifier_opt (parser);
12164       if (!extension_specifier)
12165         extension_specifier
12166           = cp_parser_function_specifier_opt (parser,
12167                                               /*decl_specs=*/NULL);
12168     }
12169
12170   /* Look for the `template' keyword.  */
12171   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12172   /* Let the front end know that we are processing an explicit
12173      instantiation.  */
12174   begin_explicit_instantiation ();
12175   /* [temp.explicit] says that we are supposed to ignore access
12176      control while processing explicit instantiation directives.  */
12177   push_deferring_access_checks (dk_no_check);
12178   /* Parse a decl-specifier-seq.  */
12179   cp_parser_decl_specifier_seq (parser,
12180                                 CP_PARSER_FLAGS_OPTIONAL,
12181                                 &decl_specifiers,
12182                                 &declares_class_or_enum);
12183   /* If there was exactly one decl-specifier, and it declared a class,
12184      and there's no declarator, then we have an explicit type
12185      instantiation.  */
12186   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12187     {
12188       tree type;
12189
12190       type = check_tag_decl (&decl_specifiers);
12191       /* Turn access control back on for names used during
12192          template instantiation.  */
12193       pop_deferring_access_checks ();
12194       if (type)
12195         do_type_instantiation (type, extension_specifier,
12196                                /*complain=*/tf_error);
12197     }
12198   else
12199     {
12200       cp_declarator *declarator;
12201       tree decl;
12202
12203       /* Parse the declarator.  */
12204       declarator
12205         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12206                                 /*ctor_dtor_or_conv_p=*/NULL,
12207                                 /*parenthesized_p=*/NULL,
12208                                 /*member_p=*/false);
12209       if (declares_class_or_enum & 2)
12210         cp_parser_check_for_definition_in_return_type (declarator,
12211                                                        decl_specifiers.type,
12212                                                        decl_specifiers.type_location);
12213       if (declarator != cp_error_declarator)
12214         {
12215           if (decl_specifiers.specs[(int)ds_inline])
12216             permerror (input_location, "explicit instantiation shall not use"
12217                        " %<inline%> specifier");
12218           if (decl_specifiers.specs[(int)ds_constexpr])
12219             permerror (input_location, "explicit instantiation shall not use"
12220                        " %<constexpr%> specifier");
12221
12222           decl = grokdeclarator (declarator, &decl_specifiers,
12223                                  NORMAL, 0, &decl_specifiers.attributes);
12224           /* Turn access control back on for names used during
12225              template instantiation.  */
12226           pop_deferring_access_checks ();
12227           /* Do the explicit instantiation.  */
12228           do_decl_instantiation (decl, extension_specifier);
12229         }
12230       else
12231         {
12232           pop_deferring_access_checks ();
12233           /* Skip the body of the explicit instantiation.  */
12234           cp_parser_skip_to_end_of_statement (parser);
12235         }
12236     }
12237   /* We're done with the instantiation.  */
12238   end_explicit_instantiation ();
12239
12240   cp_parser_consume_semicolon_at_end_of_statement (parser);
12241
12242   timevar_pop (TV_TEMPLATE_INST);
12243 }
12244
12245 /* Parse an explicit-specialization.
12246
12247    explicit-specialization:
12248      template < > declaration
12249
12250    Although the standard says `declaration', what it really means is:
12251
12252    explicit-specialization:
12253      template <> decl-specifier [opt] init-declarator [opt] ;
12254      template <> function-definition
12255      template <> explicit-specialization
12256      template <> template-declaration  */
12257
12258 static void
12259 cp_parser_explicit_specialization (cp_parser* parser)
12260 {
12261   bool need_lang_pop;
12262   cp_token *token = cp_lexer_peek_token (parser->lexer);
12263
12264   /* Look for the `template' keyword.  */
12265   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12266   /* Look for the `<'.  */
12267   cp_parser_require (parser, CPP_LESS, RT_LESS);
12268   /* Look for the `>'.  */
12269   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12270   /* We have processed another parameter list.  */
12271   ++parser->num_template_parameter_lists;
12272   /* [temp]
12273
12274      A template ... explicit specialization ... shall not have C
12275      linkage.  */
12276   if (current_lang_name == lang_name_c)
12277     {
12278       error_at (token->location, "template specialization with C linkage");
12279       /* Give it C++ linkage to avoid confusing other parts of the
12280          front end.  */
12281       push_lang_context (lang_name_cplusplus);
12282       need_lang_pop = true;
12283     }
12284   else
12285     need_lang_pop = false;
12286   /* Let the front end know that we are beginning a specialization.  */
12287   if (!begin_specialization ())
12288     {
12289       end_specialization ();
12290       return;
12291     }
12292
12293   /* If the next keyword is `template', we need to figure out whether
12294      or not we're looking a template-declaration.  */
12295   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12296     {
12297       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12298           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12299         cp_parser_template_declaration_after_export (parser,
12300                                                      /*member_p=*/false);
12301       else
12302         cp_parser_explicit_specialization (parser);
12303     }
12304   else
12305     /* Parse the dependent declaration.  */
12306     cp_parser_single_declaration (parser,
12307                                   /*checks=*/NULL,
12308                                   /*member_p=*/false,
12309                                   /*explicit_specialization_p=*/true,
12310                                   /*friend_p=*/NULL);
12311   /* We're done with the specialization.  */
12312   end_specialization ();
12313   /* For the erroneous case of a template with C linkage, we pushed an
12314      implicit C++ linkage scope; exit that scope now.  */
12315   if (need_lang_pop)
12316     pop_lang_context ();
12317   /* We're done with this parameter list.  */
12318   --parser->num_template_parameter_lists;
12319 }
12320
12321 /* Parse a type-specifier.
12322
12323    type-specifier:
12324      simple-type-specifier
12325      class-specifier
12326      enum-specifier
12327      elaborated-type-specifier
12328      cv-qualifier
12329
12330    GNU Extension:
12331
12332    type-specifier:
12333      __complex__
12334
12335    Returns a representation of the type-specifier.  For a
12336    class-specifier, enum-specifier, or elaborated-type-specifier, a
12337    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12338
12339    The parser flags FLAGS is used to control type-specifier parsing.
12340
12341    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12342    in a decl-specifier-seq.
12343
12344    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12345    class-specifier, enum-specifier, or elaborated-type-specifier, then
12346    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12347    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12348    zero.
12349
12350    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12351    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12352    is set to FALSE.  */
12353
12354 static tree
12355 cp_parser_type_specifier (cp_parser* parser,
12356                           cp_parser_flags flags,
12357                           cp_decl_specifier_seq *decl_specs,
12358                           bool is_declaration,
12359                           int* declares_class_or_enum,
12360                           bool* is_cv_qualifier)
12361 {
12362   tree type_spec = NULL_TREE;
12363   cp_token *token;
12364   enum rid keyword;
12365   cp_decl_spec ds = ds_last;
12366
12367   /* Assume this type-specifier does not declare a new type.  */
12368   if (declares_class_or_enum)
12369     *declares_class_or_enum = 0;
12370   /* And that it does not specify a cv-qualifier.  */
12371   if (is_cv_qualifier)
12372     *is_cv_qualifier = false;
12373   /* Peek at the next token.  */
12374   token = cp_lexer_peek_token (parser->lexer);
12375
12376   /* If we're looking at a keyword, we can use that to guide the
12377      production we choose.  */
12378   keyword = token->keyword;
12379   switch (keyword)
12380     {
12381     case RID_ENUM:
12382       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12383         goto elaborated_type_specifier;
12384
12385       /* Look for the enum-specifier.  */
12386       type_spec = cp_parser_enum_specifier (parser);
12387       /* If that worked, we're done.  */
12388       if (type_spec)
12389         {
12390           if (declares_class_or_enum)
12391             *declares_class_or_enum = 2;
12392           if (decl_specs)
12393             cp_parser_set_decl_spec_type (decl_specs,
12394                                           type_spec,
12395                                           token->location,
12396                                           /*user_defined_p=*/true);
12397           return type_spec;
12398         }
12399       else
12400         goto elaborated_type_specifier;
12401
12402       /* Any of these indicate either a class-specifier, or an
12403          elaborated-type-specifier.  */
12404     case RID_CLASS:
12405     case RID_STRUCT:
12406     case RID_UNION:
12407       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12408         goto elaborated_type_specifier;
12409
12410       /* Parse tentatively so that we can back up if we don't find a
12411          class-specifier.  */
12412       cp_parser_parse_tentatively (parser);
12413       /* Look for the class-specifier.  */
12414       type_spec = cp_parser_class_specifier (parser);
12415       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12416       /* If that worked, we're done.  */
12417       if (cp_parser_parse_definitely (parser))
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
12429       /* Fall through.  */
12430     elaborated_type_specifier:
12431       /* We're declaring (not defining) a class or enum.  */
12432       if (declares_class_or_enum)
12433         *declares_class_or_enum = 1;
12434
12435       /* Fall through.  */
12436     case RID_TYPENAME:
12437       /* Look for an elaborated-type-specifier.  */
12438       type_spec
12439         = (cp_parser_elaborated_type_specifier
12440            (parser,
12441             decl_specs && decl_specs->specs[(int) ds_friend],
12442             is_declaration));
12443       if (decl_specs)
12444         cp_parser_set_decl_spec_type (decl_specs,
12445                                       type_spec,
12446                                       token->location,
12447                                       /*user_defined_p=*/true);
12448       return type_spec;
12449
12450     case RID_CONST:
12451       ds = ds_const;
12452       if (is_cv_qualifier)
12453         *is_cv_qualifier = true;
12454       break;
12455
12456     case RID_VOLATILE:
12457       ds = ds_volatile;
12458       if (is_cv_qualifier)
12459         *is_cv_qualifier = true;
12460       break;
12461
12462     case RID_RESTRICT:
12463       ds = ds_restrict;
12464       if (is_cv_qualifier)
12465         *is_cv_qualifier = true;
12466       break;
12467
12468     case RID_COMPLEX:
12469       /* The `__complex__' keyword is a GNU extension.  */
12470       ds = ds_complex;
12471       break;
12472
12473     default:
12474       break;
12475     }
12476
12477   /* Handle simple keywords.  */
12478   if (ds != ds_last)
12479     {
12480       if (decl_specs)
12481         {
12482           ++decl_specs->specs[(int)ds];
12483           decl_specs->any_specifiers_p = true;
12484         }
12485       return cp_lexer_consume_token (parser->lexer)->u.value;
12486     }
12487
12488   /* If we do not already have a type-specifier, assume we are looking
12489      at a simple-type-specifier.  */
12490   type_spec = cp_parser_simple_type_specifier (parser,
12491                                                decl_specs,
12492                                                flags);
12493
12494   /* If we didn't find a type-specifier, and a type-specifier was not
12495      optional in this context, issue an error message.  */
12496   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12497     {
12498       cp_parser_error (parser, "expected type specifier");
12499       return error_mark_node;
12500     }
12501
12502   return type_spec;
12503 }
12504
12505 /* Parse a simple-type-specifier.
12506
12507    simple-type-specifier:
12508      :: [opt] nested-name-specifier [opt] type-name
12509      :: [opt] nested-name-specifier template template-id
12510      char
12511      wchar_t
12512      bool
12513      short
12514      int
12515      long
12516      signed
12517      unsigned
12518      float
12519      double
12520      void
12521
12522    C++0x Extension:
12523
12524    simple-type-specifier:
12525      auto
12526      decltype ( expression )   
12527      char16_t
12528      char32_t
12529      __underlying_type ( type-id )
12530
12531    GNU Extension:
12532
12533    simple-type-specifier:
12534      __int128
12535      __typeof__ unary-expression
12536      __typeof__ ( type-id )
12537
12538    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12539    appropriately updated.  */
12540
12541 static tree
12542 cp_parser_simple_type_specifier (cp_parser* parser,
12543                                  cp_decl_specifier_seq *decl_specs,
12544                                  cp_parser_flags flags)
12545 {
12546   tree type = NULL_TREE;
12547   cp_token *token;
12548
12549   /* Peek at the next token.  */
12550   token = cp_lexer_peek_token (parser->lexer);
12551
12552   /* If we're looking at a keyword, things are easy.  */
12553   switch (token->keyword)
12554     {
12555     case RID_CHAR:
12556       if (decl_specs)
12557         decl_specs->explicit_char_p = true;
12558       type = char_type_node;
12559       break;
12560     case RID_CHAR16:
12561       type = char16_type_node;
12562       break;
12563     case RID_CHAR32:
12564       type = char32_type_node;
12565       break;
12566     case RID_WCHAR:
12567       type = wchar_type_node;
12568       break;
12569     case RID_BOOL:
12570       type = boolean_type_node;
12571       break;
12572     case RID_SHORT:
12573       if (decl_specs)
12574         ++decl_specs->specs[(int) ds_short];
12575       type = short_integer_type_node;
12576       break;
12577     case RID_INT:
12578       if (decl_specs)
12579         decl_specs->explicit_int_p = true;
12580       type = integer_type_node;
12581       break;
12582     case RID_INT128:
12583       if (!int128_integer_type_node)
12584         break;
12585       if (decl_specs)
12586         decl_specs->explicit_int128_p = true;
12587       type = int128_integer_type_node;
12588       break;
12589     case RID_LONG:
12590       if (decl_specs)
12591         ++decl_specs->specs[(int) ds_long];
12592       type = long_integer_type_node;
12593       break;
12594     case RID_SIGNED:
12595       if (decl_specs)
12596         ++decl_specs->specs[(int) ds_signed];
12597       type = integer_type_node;
12598       break;
12599     case RID_UNSIGNED:
12600       if (decl_specs)
12601         ++decl_specs->specs[(int) ds_unsigned];
12602       type = unsigned_type_node;
12603       break;
12604     case RID_FLOAT:
12605       type = float_type_node;
12606       break;
12607     case RID_DOUBLE:
12608       type = double_type_node;
12609       break;
12610     case RID_VOID:
12611       type = void_type_node;
12612       break;
12613       
12614     case RID_AUTO:
12615       maybe_warn_cpp0x (CPP0X_AUTO);
12616       type = make_auto ();
12617       break;
12618
12619     case RID_DECLTYPE:
12620       /* Parse the `decltype' type.  */
12621       type = cp_parser_decltype (parser);
12622
12623       if (decl_specs)
12624         cp_parser_set_decl_spec_type (decl_specs, type,
12625                                       token->location,
12626                                       /*user_defined_p=*/true);
12627
12628       return type;
12629
12630     case RID_TYPEOF:
12631       /* Consume the `typeof' token.  */
12632       cp_lexer_consume_token (parser->lexer);
12633       /* Parse the operand to `typeof'.  */
12634       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12635       /* If it is not already a TYPE, take its type.  */
12636       if (!TYPE_P (type))
12637         type = finish_typeof (type);
12638
12639       if (decl_specs)
12640         cp_parser_set_decl_spec_type (decl_specs, type,
12641                                       token->location,
12642                                       /*user_defined_p=*/true);
12643
12644       return type;
12645
12646     case RID_UNDERLYING_TYPE:
12647       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12648
12649       if (decl_specs)
12650         cp_parser_set_decl_spec_type (decl_specs, type,
12651                                       token->location,
12652                                       /*user_defined_p=*/true);
12653
12654       return type;
12655
12656     default:
12657       break;
12658     }
12659
12660   /* If the type-specifier was for a built-in type, we're done.  */
12661   if (type)
12662     {
12663       /* Record the type.  */
12664       if (decl_specs
12665           && (token->keyword != RID_SIGNED
12666               && token->keyword != RID_UNSIGNED
12667               && token->keyword != RID_SHORT
12668               && token->keyword != RID_LONG))
12669         cp_parser_set_decl_spec_type (decl_specs,
12670                                       type,
12671                                       token->location,
12672                                       /*user_defined=*/false);
12673       if (decl_specs)
12674         decl_specs->any_specifiers_p = true;
12675
12676       /* Consume the token.  */
12677       cp_lexer_consume_token (parser->lexer);
12678
12679       /* There is no valid C++ program where a non-template type is
12680          followed by a "<".  That usually indicates that the user thought
12681          that the type was a template.  */
12682       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12683
12684       return TYPE_NAME (type);
12685     }
12686
12687   /* The type-specifier must be a user-defined type.  */
12688   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12689     {
12690       bool qualified_p;
12691       bool global_p;
12692
12693       /* Don't gobble tokens or issue error messages if this is an
12694          optional type-specifier.  */
12695       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12696         cp_parser_parse_tentatively (parser);
12697
12698       /* Look for the optional `::' operator.  */
12699       global_p
12700         = (cp_parser_global_scope_opt (parser,
12701                                        /*current_scope_valid_p=*/false)
12702            != NULL_TREE);
12703       /* Look for the nested-name specifier.  */
12704       qualified_p
12705         = (cp_parser_nested_name_specifier_opt (parser,
12706                                                 /*typename_keyword_p=*/false,
12707                                                 /*check_dependency_p=*/true,
12708                                                 /*type_p=*/false,
12709                                                 /*is_declaration=*/false)
12710            != NULL_TREE);
12711       token = cp_lexer_peek_token (parser->lexer);
12712       /* If we have seen a nested-name-specifier, and the next token
12713          is `template', then we are using the template-id production.  */
12714       if (parser->scope
12715           && cp_parser_optional_template_keyword (parser))
12716         {
12717           /* Look for the template-id.  */
12718           type = cp_parser_template_id (parser,
12719                                         /*template_keyword_p=*/true,
12720                                         /*check_dependency_p=*/true,
12721                                         /*is_declaration=*/false);
12722           /* If the template-id did not name a type, we are out of
12723              luck.  */
12724           if (TREE_CODE (type) != TYPE_DECL)
12725             {
12726               cp_parser_error (parser, "expected template-id for type");
12727               type = NULL_TREE;
12728             }
12729         }
12730       /* Otherwise, look for a type-name.  */
12731       else
12732         type = cp_parser_type_name (parser);
12733       /* Keep track of all name-lookups performed in class scopes.  */
12734       if (type
12735           && !global_p
12736           && !qualified_p
12737           && TREE_CODE (type) == TYPE_DECL
12738           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12739         maybe_note_name_used_in_class (DECL_NAME (type), type);
12740       /* If it didn't work out, we don't have a TYPE.  */
12741       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12742           && !cp_parser_parse_definitely (parser))
12743         type = NULL_TREE;
12744       if (type && decl_specs)
12745         cp_parser_set_decl_spec_type (decl_specs, type,
12746                                       token->location,
12747                                       /*user_defined=*/true);
12748     }
12749
12750   /* If we didn't get a type-name, issue an error message.  */
12751   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12752     {
12753       cp_parser_error (parser, "expected type-name");
12754       return error_mark_node;
12755     }
12756
12757   if (type && type != error_mark_node)
12758     {
12759       /* See if TYPE is an Objective-C type, and if so, parse and
12760          accept any protocol references following it.  Do this before
12761          the cp_parser_check_for_invalid_template_id() call, because
12762          Objective-C types can be followed by '<...>' which would
12763          enclose protocol names rather than template arguments, and so
12764          everything is fine.  */
12765       if (c_dialect_objc () && !parser->scope
12766           && (objc_is_id (type) || objc_is_class_name (type)))
12767         {
12768           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12769           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12770
12771           /* Clobber the "unqualified" type previously entered into
12772              DECL_SPECS with the new, improved protocol-qualified version.  */
12773           if (decl_specs)
12774             decl_specs->type = qual_type;
12775
12776           return qual_type;
12777         }
12778
12779       /* There is no valid C++ program where a non-template type is
12780          followed by a "<".  That usually indicates that the user
12781          thought that the type was a template.  */
12782       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12783                                                token->location);
12784     }
12785
12786   return type;
12787 }
12788
12789 /* Parse a type-name.
12790
12791    type-name:
12792      class-name
12793      enum-name
12794      typedef-name
12795
12796    enum-name:
12797      identifier
12798
12799    typedef-name:
12800      identifier
12801
12802    Returns a TYPE_DECL for the type.  */
12803
12804 static tree
12805 cp_parser_type_name (cp_parser* parser)
12806 {
12807   tree type_decl;
12808
12809   /* We can't know yet whether it is a class-name or not.  */
12810   cp_parser_parse_tentatively (parser);
12811   /* Try a class-name.  */
12812   type_decl = cp_parser_class_name (parser,
12813                                     /*typename_keyword_p=*/false,
12814                                     /*template_keyword_p=*/false,
12815                                     none_type,
12816                                     /*check_dependency_p=*/true,
12817                                     /*class_head_p=*/false,
12818                                     /*is_declaration=*/false);
12819   /* If it's not a class-name, keep looking.  */
12820   if (!cp_parser_parse_definitely (parser))
12821     {
12822       /* It must be a typedef-name or an enum-name.  */
12823       return cp_parser_nonclass_name (parser);
12824     }
12825
12826   return type_decl;
12827 }
12828
12829 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12830
12831    enum-name:
12832      identifier
12833
12834    typedef-name:
12835      identifier
12836
12837    Returns a TYPE_DECL for the type.  */
12838
12839 static tree
12840 cp_parser_nonclass_name (cp_parser* parser)
12841 {
12842   tree type_decl;
12843   tree identifier;
12844
12845   cp_token *token = cp_lexer_peek_token (parser->lexer);
12846   identifier = cp_parser_identifier (parser);
12847   if (identifier == error_mark_node)
12848     return error_mark_node;
12849
12850   /* Look up the type-name.  */
12851   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12852
12853   if (TREE_CODE (type_decl) != TYPE_DECL
12854       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12855     {
12856       /* See if this is an Objective-C type.  */
12857       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12858       tree type = objc_get_protocol_qualified_type (identifier, protos);
12859       if (type)
12860         type_decl = TYPE_NAME (type);
12861     }
12862
12863   /* Issue an error if we did not find a type-name.  */
12864   if (TREE_CODE (type_decl) != TYPE_DECL
12865       /* In Objective-C, we have the complication that class names are
12866          normally type names and start declarations (eg, the
12867          "NSObject" in "NSObject *object;"), but can be used in an
12868          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12869          is an expression.  So, a classname followed by a dot is not a
12870          valid type-name.  */
12871       || (objc_is_class_name (TREE_TYPE (type_decl))
12872           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12873     {
12874       if (!cp_parser_simulate_error (parser))
12875         cp_parser_name_lookup_error (parser, identifier, type_decl,
12876                                      NLE_TYPE, token->location);
12877       return error_mark_node;
12878     }
12879   /* Remember that the name was used in the definition of the
12880      current class so that we can check later to see if the
12881      meaning would have been different after the class was
12882      entirely defined.  */
12883   else if (type_decl != error_mark_node
12884            && !parser->scope)
12885     maybe_note_name_used_in_class (identifier, type_decl);
12886   
12887   return type_decl;
12888 }
12889
12890 /* Parse an elaborated-type-specifier.  Note that the grammar given
12891    here incorporates the resolution to DR68.
12892
12893    elaborated-type-specifier:
12894      class-key :: [opt] nested-name-specifier [opt] identifier
12895      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12896      enum-key :: [opt] nested-name-specifier [opt] identifier
12897      typename :: [opt] nested-name-specifier identifier
12898      typename :: [opt] nested-name-specifier template [opt]
12899        template-id
12900
12901    GNU extension:
12902
12903    elaborated-type-specifier:
12904      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12905      class-key attributes :: [opt] nested-name-specifier [opt]
12906                template [opt] template-id
12907      enum attributes :: [opt] nested-name-specifier [opt] identifier
12908
12909    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12910    declared `friend'.  If IS_DECLARATION is TRUE, then this
12911    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12912    something is being declared.
12913
12914    Returns the TYPE specified.  */
12915
12916 static tree
12917 cp_parser_elaborated_type_specifier (cp_parser* parser,
12918                                      bool is_friend,
12919                                      bool is_declaration)
12920 {
12921   enum tag_types tag_type;
12922   tree identifier;
12923   tree type = NULL_TREE;
12924   tree attributes = NULL_TREE;
12925   tree globalscope;
12926   cp_token *token = NULL;
12927
12928   /* See if we're looking at the `enum' keyword.  */
12929   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12930     {
12931       /* Consume the `enum' token.  */
12932       cp_lexer_consume_token (parser->lexer);
12933       /* Remember that it's an enumeration type.  */
12934       tag_type = enum_type;
12935       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12936          enums) is used here.  */
12937       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12938           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12939         {
12940             pedwarn (input_location, 0, "elaborated-type-specifier "
12941                       "for a scoped enum must not use the %<%D%> keyword",
12942                       cp_lexer_peek_token (parser->lexer)->u.value);
12943           /* Consume the `struct' or `class' and parse it anyway.  */
12944           cp_lexer_consume_token (parser->lexer);
12945         }
12946       /* Parse the attributes.  */
12947       attributes = cp_parser_attributes_opt (parser);
12948     }
12949   /* Or, it might be `typename'.  */
12950   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12951                                            RID_TYPENAME))
12952     {
12953       /* Consume the `typename' token.  */
12954       cp_lexer_consume_token (parser->lexer);
12955       /* Remember that it's a `typename' type.  */
12956       tag_type = typename_type;
12957     }
12958   /* Otherwise it must be a class-key.  */
12959   else
12960     {
12961       tag_type = cp_parser_class_key (parser);
12962       if (tag_type == none_type)
12963         return error_mark_node;
12964       /* Parse the attributes.  */
12965       attributes = cp_parser_attributes_opt (parser);
12966     }
12967
12968   /* Look for the `::' operator.  */
12969   globalscope =  cp_parser_global_scope_opt (parser,
12970                                              /*current_scope_valid_p=*/false);
12971   /* Look for the nested-name-specifier.  */
12972   if (tag_type == typename_type && !globalscope)
12973     {
12974       if (!cp_parser_nested_name_specifier (parser,
12975                                            /*typename_keyword_p=*/true,
12976                                            /*check_dependency_p=*/true,
12977                                            /*type_p=*/true,
12978                                             is_declaration))
12979         return error_mark_node;
12980     }
12981   else
12982     /* Even though `typename' is not present, the proposed resolution
12983        to Core Issue 180 says that in `class A<T>::B', `B' should be
12984        considered a type-name, even if `A<T>' is dependent.  */
12985     cp_parser_nested_name_specifier_opt (parser,
12986                                          /*typename_keyword_p=*/true,
12987                                          /*check_dependency_p=*/true,
12988                                          /*type_p=*/true,
12989                                          is_declaration);
12990  /* For everything but enumeration types, consider a template-id.
12991     For an enumeration type, consider only a plain identifier.  */
12992   if (tag_type != enum_type)
12993     {
12994       bool template_p = false;
12995       tree decl;
12996
12997       /* Allow the `template' keyword.  */
12998       template_p = cp_parser_optional_template_keyword (parser);
12999       /* If we didn't see `template', we don't know if there's a
13000          template-id or not.  */
13001       if (!template_p)
13002         cp_parser_parse_tentatively (parser);
13003       /* Parse the template-id.  */
13004       token = cp_lexer_peek_token (parser->lexer);
13005       decl = cp_parser_template_id (parser, template_p,
13006                                     /*check_dependency_p=*/true,
13007                                     is_declaration);
13008       /* If we didn't find a template-id, look for an ordinary
13009          identifier.  */
13010       if (!template_p && !cp_parser_parse_definitely (parser))
13011         ;
13012       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13013          in effect, then we must assume that, upon instantiation, the
13014          template will correspond to a class.  */
13015       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13016                && tag_type == typename_type)
13017         type = make_typename_type (parser->scope, decl,
13018                                    typename_type,
13019                                    /*complain=*/tf_error);
13020       /* If the `typename' keyword is in effect and DECL is not a type
13021          decl. Then type is non existant.   */
13022       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13023         type = NULL_TREE; 
13024       else 
13025         type = TREE_TYPE (decl);
13026     }
13027
13028   if (!type)
13029     {
13030       token = cp_lexer_peek_token (parser->lexer);
13031       identifier = cp_parser_identifier (parser);
13032
13033       if (identifier == error_mark_node)
13034         {
13035           parser->scope = NULL_TREE;
13036           return error_mark_node;
13037         }
13038
13039       /* For a `typename', we needn't call xref_tag.  */
13040       if (tag_type == typename_type
13041           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13042         return cp_parser_make_typename_type (parser, parser->scope,
13043                                              identifier,
13044                                              token->location);
13045       /* Look up a qualified name in the usual way.  */
13046       if (parser->scope)
13047         {
13048           tree decl;
13049           tree ambiguous_decls;
13050
13051           decl = cp_parser_lookup_name (parser, identifier,
13052                                         tag_type,
13053                                         /*is_template=*/false,
13054                                         /*is_namespace=*/false,
13055                                         /*check_dependency=*/true,
13056                                         &ambiguous_decls,
13057                                         token->location);
13058
13059           /* If the lookup was ambiguous, an error will already have been
13060              issued.  */
13061           if (ambiguous_decls)
13062             return error_mark_node;
13063
13064           /* If we are parsing friend declaration, DECL may be a
13065              TEMPLATE_DECL tree node here.  However, we need to check
13066              whether this TEMPLATE_DECL results in valid code.  Consider
13067              the following example:
13068
13069                namespace N {
13070                  template <class T> class C {};
13071                }
13072                class X {
13073                  template <class T> friend class N::C; // #1, valid code
13074                };
13075                template <class T> class Y {
13076                  friend class N::C;                    // #2, invalid code
13077                };
13078
13079              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13080              name lookup of `N::C'.  We see that friend declaration must
13081              be template for the code to be valid.  Note that
13082              processing_template_decl does not work here since it is
13083              always 1 for the above two cases.  */
13084
13085           decl = (cp_parser_maybe_treat_template_as_class
13086                   (decl, /*tag_name_p=*/is_friend
13087                          && parser->num_template_parameter_lists));
13088
13089           if (TREE_CODE (decl) != TYPE_DECL)
13090             {
13091               cp_parser_diagnose_invalid_type_name (parser,
13092                                                     parser->scope,
13093                                                     identifier,
13094                                                     token->location);
13095               return error_mark_node;
13096             }
13097
13098           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13099             {
13100               bool allow_template = (parser->num_template_parameter_lists
13101                                       || DECL_SELF_REFERENCE_P (decl));
13102               type = check_elaborated_type_specifier (tag_type, decl, 
13103                                                       allow_template);
13104
13105               if (type == error_mark_node)
13106                 return error_mark_node;
13107             }
13108
13109           /* Forward declarations of nested types, such as
13110
13111                class C1::C2;
13112                class C1::C2::C3;
13113
13114              are invalid unless all components preceding the final '::'
13115              are complete.  If all enclosing types are complete, these
13116              declarations become merely pointless.
13117
13118              Invalid forward declarations of nested types are errors
13119              caught elsewhere in parsing.  Those that are pointless arrive
13120              here.  */
13121
13122           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13123               && !is_friend && !processing_explicit_instantiation)
13124             warning (0, "declaration %qD does not declare anything", decl);
13125
13126           type = TREE_TYPE (decl);
13127         }
13128       else
13129         {
13130           /* An elaborated-type-specifier sometimes introduces a new type and
13131              sometimes names an existing type.  Normally, the rule is that it
13132              introduces a new type only if there is not an existing type of
13133              the same name already in scope.  For example, given:
13134
13135                struct S {};
13136                void f() { struct S s; }
13137
13138              the `struct S' in the body of `f' is the same `struct S' as in
13139              the global scope; the existing definition is used.  However, if
13140              there were no global declaration, this would introduce a new
13141              local class named `S'.
13142
13143              An exception to this rule applies to the following code:
13144
13145                namespace N { struct S; }
13146
13147              Here, the elaborated-type-specifier names a new type
13148              unconditionally; even if there is already an `S' in the
13149              containing scope this declaration names a new type.
13150              This exception only applies if the elaborated-type-specifier
13151              forms the complete declaration:
13152
13153                [class.name]
13154
13155                A declaration consisting solely of `class-key identifier ;' is
13156                either a redeclaration of the name in the current scope or a
13157                forward declaration of the identifier as a class name.  It
13158                introduces the name into the current scope.
13159
13160              We are in this situation precisely when the next token is a `;'.
13161
13162              An exception to the exception is that a `friend' declaration does
13163              *not* name a new type; i.e., given:
13164
13165                struct S { friend struct T; };
13166
13167              `T' is not a new type in the scope of `S'.
13168
13169              Also, `new struct S' or `sizeof (struct S)' never results in the
13170              definition of a new type; a new type can only be declared in a
13171              declaration context.  */
13172
13173           tag_scope ts;
13174           bool template_p;
13175
13176           if (is_friend)
13177             /* Friends have special name lookup rules.  */
13178             ts = ts_within_enclosing_non_class;
13179           else if (is_declaration
13180                    && cp_lexer_next_token_is (parser->lexer,
13181                                               CPP_SEMICOLON))
13182             /* This is a `class-key identifier ;' */
13183             ts = ts_current;
13184           else
13185             ts = ts_global;
13186
13187           template_p =
13188             (parser->num_template_parameter_lists
13189              && (cp_parser_next_token_starts_class_definition_p (parser)
13190                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13191           /* An unqualified name was used to reference this type, so
13192              there were no qualifying templates.  */
13193           if (!cp_parser_check_template_parameters (parser,
13194                                                     /*num_templates=*/0,
13195                                                     token->location,
13196                                                     /*declarator=*/NULL))
13197             return error_mark_node;
13198           type = xref_tag (tag_type, identifier, ts, template_p);
13199         }
13200     }
13201
13202   if (type == error_mark_node)
13203     return error_mark_node;
13204
13205   /* Allow attributes on forward declarations of classes.  */
13206   if (attributes)
13207     {
13208       if (TREE_CODE (type) == TYPENAME_TYPE)
13209         warning (OPT_Wattributes,
13210                  "attributes ignored on uninstantiated type");
13211       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13212                && ! processing_explicit_instantiation)
13213         warning (OPT_Wattributes,
13214                  "attributes ignored on template instantiation");
13215       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13216         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13217       else
13218         warning (OPT_Wattributes,
13219                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13220     }
13221
13222   if (tag_type != enum_type)
13223     cp_parser_check_class_key (tag_type, type);
13224
13225   /* A "<" cannot follow an elaborated type specifier.  If that
13226      happens, the user was probably trying to form a template-id.  */
13227   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13228
13229   return type;
13230 }
13231
13232 /* Parse an enum-specifier.
13233
13234    enum-specifier:
13235      enum-head { enumerator-list [opt] }
13236
13237    enum-head:
13238      enum-key identifier [opt] enum-base [opt]
13239      enum-key nested-name-specifier identifier enum-base [opt]
13240
13241    enum-key:
13242      enum
13243      enum class   [C++0x]
13244      enum struct  [C++0x]
13245
13246    enum-base:   [C++0x]
13247      : type-specifier-seq
13248
13249    opaque-enum-specifier:
13250      enum-key identifier enum-base [opt] ;
13251
13252    GNU Extensions:
13253      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13254        { enumerator-list [opt] }attributes[opt]
13255
13256    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13257    if the token stream isn't an enum-specifier after all.  */
13258
13259 static tree
13260 cp_parser_enum_specifier (cp_parser* parser)
13261 {
13262   tree identifier;
13263   tree type = NULL_TREE;
13264   tree prev_scope;
13265   tree nested_name_specifier = NULL_TREE;
13266   tree attributes;
13267   bool scoped_enum_p = false;
13268   bool has_underlying_type = false;
13269   bool nested_being_defined = false;
13270   bool new_value_list = false;
13271   bool is_new_type = false;
13272   bool is_anonymous = false;
13273   tree underlying_type = NULL_TREE;
13274   cp_token *type_start_token = NULL;
13275   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13276
13277   parser->colon_corrects_to_scope_p = false;
13278
13279   /* Parse tentatively so that we can back up if we don't find a
13280      enum-specifier.  */
13281   cp_parser_parse_tentatively (parser);
13282
13283   /* Caller guarantees that the current token is 'enum', an identifier
13284      possibly follows, and the token after that is an opening brace.
13285      If we don't have an identifier, fabricate an anonymous name for
13286      the enumeration being defined.  */
13287   cp_lexer_consume_token (parser->lexer);
13288
13289   /* Parse the "class" or "struct", which indicates a scoped
13290      enumeration type in C++0x.  */
13291   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13292       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13293     {
13294       if (cxx_dialect < cxx0x)
13295         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13296
13297       /* Consume the `struct' or `class' token.  */
13298       cp_lexer_consume_token (parser->lexer);
13299
13300       scoped_enum_p = true;
13301     }
13302
13303   attributes = cp_parser_attributes_opt (parser);
13304
13305   /* Clear the qualification.  */
13306   parser->scope = NULL_TREE;
13307   parser->qualifying_scope = NULL_TREE;
13308   parser->object_scope = NULL_TREE;
13309
13310   /* Figure out in what scope the declaration is being placed.  */
13311   prev_scope = current_scope ();
13312
13313   type_start_token = cp_lexer_peek_token (parser->lexer);
13314
13315   push_deferring_access_checks (dk_no_check);
13316   nested_name_specifier
13317       = cp_parser_nested_name_specifier_opt (parser,
13318                                              /*typename_keyword_p=*/true,
13319                                              /*check_dependency_p=*/false,
13320                                              /*type_p=*/false,
13321                                              /*is_declaration=*/false);
13322
13323   if (nested_name_specifier)
13324     {
13325       tree name;
13326
13327       identifier = cp_parser_identifier (parser);
13328       name =  cp_parser_lookup_name (parser, identifier,
13329                                      enum_type,
13330                                      /*is_template=*/false,
13331                                      /*is_namespace=*/false,
13332                                      /*check_dependency=*/true,
13333                                      /*ambiguous_decls=*/NULL,
13334                                      input_location);
13335       if (name)
13336         {
13337           type = TREE_TYPE (name);
13338           if (TREE_CODE (type) == TYPENAME_TYPE)
13339             {
13340               /* Are template enums allowed in ISO? */
13341               if (template_parm_scope_p ())
13342                 pedwarn (type_start_token->location, OPT_pedantic,
13343                          "%qD is an enumeration template", name);
13344               /* ignore a typename reference, for it will be solved by name
13345                  in start_enum.  */
13346               type = NULL_TREE;
13347             }
13348         }
13349       else
13350         error_at (type_start_token->location,
13351                   "%qD is not an enumerator-name", identifier);
13352     }
13353   else
13354     {
13355       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13356         identifier = cp_parser_identifier (parser);
13357       else
13358         {
13359           identifier = make_anon_name ();
13360           is_anonymous = true;
13361         }
13362     }
13363   pop_deferring_access_checks ();
13364
13365   /* Check for the `:' that denotes a specified underlying type in C++0x.
13366      Note that a ':' could also indicate a bitfield width, however.  */
13367   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13368     {
13369       cp_decl_specifier_seq type_specifiers;
13370
13371       /* Consume the `:'.  */
13372       cp_lexer_consume_token (parser->lexer);
13373
13374       /* Parse the type-specifier-seq.  */
13375       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13376                                     /*is_trailing_return=*/false,
13377                                     &type_specifiers);
13378
13379       /* At this point this is surely not elaborated type specifier.  */
13380       if (!cp_parser_parse_definitely (parser))
13381         return NULL_TREE;
13382
13383       if (cxx_dialect < cxx0x)
13384         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13385
13386       has_underlying_type = true;
13387
13388       /* If that didn't work, stop.  */
13389       if (type_specifiers.type != error_mark_node)
13390         {
13391           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13392                                             /*initialized=*/0, NULL);
13393           if (underlying_type == error_mark_node)
13394             underlying_type = NULL_TREE;
13395         }
13396     }
13397
13398   /* Look for the `{' but don't consume it yet.  */
13399   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13400     {
13401       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13402         {
13403           cp_parser_error (parser, "expected %<{%>");
13404           if (has_underlying_type)
13405             {
13406               type = NULL_TREE;
13407               goto out;
13408             }
13409         }
13410       /* An opaque-enum-specifier must have a ';' here.  */
13411       if ((scoped_enum_p || underlying_type)
13412           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13413         {
13414           cp_parser_error (parser, "expected %<;%> or %<{%>");
13415           if (has_underlying_type)
13416             {
13417               type = NULL_TREE;
13418               goto out;
13419             }
13420         }
13421     }
13422
13423   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13424     return NULL_TREE;
13425
13426   if (nested_name_specifier)
13427     {
13428       if (CLASS_TYPE_P (nested_name_specifier))
13429         {
13430           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13431           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13432           push_scope (nested_name_specifier);
13433         }
13434       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13435         {
13436           push_nested_namespace (nested_name_specifier);
13437         }
13438     }
13439
13440   /* Issue an error message if type-definitions are forbidden here.  */
13441   if (!cp_parser_check_type_definition (parser))
13442     type = error_mark_node;
13443   else
13444     /* Create the new type.  We do this before consuming the opening
13445        brace so the enum will be recorded as being on the line of its
13446        tag (or the 'enum' keyword, if there is no tag).  */
13447     type = start_enum (identifier, type, underlying_type,
13448                        scoped_enum_p, &is_new_type);
13449
13450   /* If the next token is not '{' it is an opaque-enum-specifier or an
13451      elaborated-type-specifier.  */
13452   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13453     {
13454       timevar_push (TV_PARSE_ENUM);
13455       if (nested_name_specifier)
13456         {
13457           /* The following catches invalid code such as:
13458              enum class S<int>::E { A, B, C }; */
13459           if (!processing_specialization
13460               && CLASS_TYPE_P (nested_name_specifier)
13461               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13462             error_at (type_start_token->location, "cannot add an enumerator "
13463                       "list to a template instantiation");
13464
13465           /* If that scope does not contain the scope in which the
13466              class was originally declared, the program is invalid.  */
13467           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13468             {
13469               if (at_namespace_scope_p ())
13470                 error_at (type_start_token->location,
13471                           "declaration of %qD in namespace %qD which does not "
13472                           "enclose %qD",
13473                           type, prev_scope, nested_name_specifier);
13474               else
13475                 error_at (type_start_token->location,
13476                           "declaration of %qD in %qD which does not enclose %qD",
13477                           type, prev_scope, nested_name_specifier);
13478               type = error_mark_node;
13479             }
13480         }
13481
13482       if (scoped_enum_p)
13483         begin_scope (sk_scoped_enum, type);
13484
13485       /* Consume the opening brace.  */
13486       cp_lexer_consume_token (parser->lexer);
13487
13488       if (type == error_mark_node)
13489         ; /* Nothing to add */
13490       else if (OPAQUE_ENUM_P (type)
13491                || (cxx_dialect > cxx98 && processing_specialization))
13492         {
13493           new_value_list = true;
13494           SET_OPAQUE_ENUM_P (type, false);
13495           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13496         }
13497       else
13498         {
13499           error_at (type_start_token->location, "multiple definition of %q#T", type);
13500           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13501                     "previous definition here");
13502           type = error_mark_node;
13503         }
13504
13505       if (type == error_mark_node)
13506         cp_parser_skip_to_end_of_block_or_statement (parser);
13507       /* If the next token is not '}', then there are some enumerators.  */
13508       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13509         cp_parser_enumerator_list (parser, type);
13510
13511       /* Consume the final '}'.  */
13512       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13513
13514       if (scoped_enum_p)
13515         finish_scope ();
13516       timevar_pop (TV_PARSE_ENUM);
13517     }
13518   else
13519     {
13520       /* If a ';' follows, then it is an opaque-enum-specifier
13521         and additional restrictions apply.  */
13522       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13523         {
13524           if (is_anonymous)
13525             error_at (type_start_token->location,
13526                       "opaque-enum-specifier without name");
13527           else if (nested_name_specifier)
13528             error_at (type_start_token->location,
13529                       "opaque-enum-specifier must use a simple identifier");
13530         }
13531     }
13532
13533   /* Look for trailing attributes to apply to this enumeration, and
13534      apply them if appropriate.  */
13535   if (cp_parser_allow_gnu_extensions_p (parser))
13536     {
13537       tree trailing_attr = cp_parser_attributes_opt (parser);
13538       trailing_attr = chainon (trailing_attr, attributes);
13539       cplus_decl_attributes (&type,
13540                              trailing_attr,
13541                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13542     }
13543
13544   /* Finish up the enumeration.  */
13545   if (type != error_mark_node)
13546     {
13547       if (new_value_list)
13548         finish_enum_value_list (type);
13549       if (is_new_type)
13550         finish_enum (type);
13551     }
13552
13553   if (nested_name_specifier)
13554     {
13555       if (CLASS_TYPE_P (nested_name_specifier))
13556         {
13557           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13558           pop_scope (nested_name_specifier);
13559         }
13560       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13561         {
13562           pop_nested_namespace (nested_name_specifier);
13563         }
13564     }
13565  out:
13566   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13567   return type;
13568 }
13569
13570 /* Parse an enumerator-list.  The enumerators all have the indicated
13571    TYPE.
13572
13573    enumerator-list:
13574      enumerator-definition
13575      enumerator-list , enumerator-definition  */
13576
13577 static void
13578 cp_parser_enumerator_list (cp_parser* parser, tree type)
13579 {
13580   while (true)
13581     {
13582       /* Parse an enumerator-definition.  */
13583       cp_parser_enumerator_definition (parser, type);
13584
13585       /* If the next token is not a ',', we've reached the end of
13586          the list.  */
13587       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13588         break;
13589       /* Otherwise, consume the `,' and keep going.  */
13590       cp_lexer_consume_token (parser->lexer);
13591       /* If the next token is a `}', there is a trailing comma.  */
13592       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13593         {
13594           if (!in_system_header)
13595             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13596           break;
13597         }
13598     }
13599 }
13600
13601 /* Parse an enumerator-definition.  The enumerator has the indicated
13602    TYPE.
13603
13604    enumerator-definition:
13605      enumerator
13606      enumerator = constant-expression
13607
13608    enumerator:
13609      identifier  */
13610
13611 static void
13612 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13613 {
13614   tree identifier;
13615   tree value;
13616   location_t loc;
13617
13618   /* Save the input location because we are interested in the location
13619      of the identifier and not the location of the explicit value.  */
13620   loc = cp_lexer_peek_token (parser->lexer)->location;
13621
13622   /* Look for the identifier.  */
13623   identifier = cp_parser_identifier (parser);
13624   if (identifier == error_mark_node)
13625     return;
13626
13627   /* If the next token is an '=', then there is an explicit value.  */
13628   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13629     {
13630       /* Consume the `=' token.  */
13631       cp_lexer_consume_token (parser->lexer);
13632       /* Parse the value.  */
13633       value = cp_parser_constant_expression (parser,
13634                                              /*allow_non_constant_p=*/false,
13635                                              NULL);
13636     }
13637   else
13638     value = NULL_TREE;
13639
13640   /* If we are processing a template, make sure the initializer of the
13641      enumerator doesn't contain any bare template parameter pack.  */
13642   if (check_for_bare_parameter_packs (value))
13643     value = error_mark_node;
13644
13645   /* integral_constant_value will pull out this expression, so make sure
13646      it's folded as appropriate.  */
13647   value = fold_non_dependent_expr (value);
13648
13649   /* Create the enumerator.  */
13650   build_enumerator (identifier, value, type, loc);
13651 }
13652
13653 /* Parse a namespace-name.
13654
13655    namespace-name:
13656      original-namespace-name
13657      namespace-alias
13658
13659    Returns the NAMESPACE_DECL for the namespace.  */
13660
13661 static tree
13662 cp_parser_namespace_name (cp_parser* parser)
13663 {
13664   tree identifier;
13665   tree namespace_decl;
13666
13667   cp_token *token = cp_lexer_peek_token (parser->lexer);
13668
13669   /* Get the name of the namespace.  */
13670   identifier = cp_parser_identifier (parser);
13671   if (identifier == error_mark_node)
13672     return error_mark_node;
13673
13674   /* Look up the identifier in the currently active scope.  Look only
13675      for namespaces, due to:
13676
13677        [basic.lookup.udir]
13678
13679        When looking up a namespace-name in a using-directive or alias
13680        definition, only namespace names are considered.
13681
13682      And:
13683
13684        [basic.lookup.qual]
13685
13686        During the lookup of a name preceding the :: scope resolution
13687        operator, object, function, and enumerator names are ignored.
13688
13689      (Note that cp_parser_qualifying_entity only calls this
13690      function if the token after the name is the scope resolution
13691      operator.)  */
13692   namespace_decl = cp_parser_lookup_name (parser, identifier,
13693                                           none_type,
13694                                           /*is_template=*/false,
13695                                           /*is_namespace=*/true,
13696                                           /*check_dependency=*/true,
13697                                           /*ambiguous_decls=*/NULL,
13698                                           token->location);
13699   /* If it's not a namespace, issue an error.  */
13700   if (namespace_decl == error_mark_node
13701       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13702     {
13703       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13704         error_at (token->location, "%qD is not a namespace-name", identifier);
13705       cp_parser_error (parser, "expected namespace-name");
13706       namespace_decl = error_mark_node;
13707     }
13708
13709   return namespace_decl;
13710 }
13711
13712 /* Parse a namespace-definition.
13713
13714    namespace-definition:
13715      named-namespace-definition
13716      unnamed-namespace-definition
13717
13718    named-namespace-definition:
13719      original-namespace-definition
13720      extension-namespace-definition
13721
13722    original-namespace-definition:
13723      namespace identifier { namespace-body }
13724
13725    extension-namespace-definition:
13726      namespace original-namespace-name { namespace-body }
13727
13728    unnamed-namespace-definition:
13729      namespace { namespace-body } */
13730
13731 static void
13732 cp_parser_namespace_definition (cp_parser* parser)
13733 {
13734   tree identifier, attribs;
13735   bool has_visibility;
13736   bool is_inline;
13737
13738   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13739     {
13740       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13741       is_inline = true;
13742       cp_lexer_consume_token (parser->lexer);
13743     }
13744   else
13745     is_inline = false;
13746
13747   /* Look for the `namespace' keyword.  */
13748   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13749
13750   /* Get the name of the namespace.  We do not attempt to distinguish
13751      between an original-namespace-definition and an
13752      extension-namespace-definition at this point.  The semantic
13753      analysis routines are responsible for that.  */
13754   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13755     identifier = cp_parser_identifier (parser);
13756   else
13757     identifier = NULL_TREE;
13758
13759   /* Parse any specified attributes.  */
13760   attribs = cp_parser_attributes_opt (parser);
13761
13762   /* Look for the `{' to start the namespace.  */
13763   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13764   /* Start the namespace.  */
13765   push_namespace (identifier);
13766
13767   /* "inline namespace" is equivalent to a stub namespace definition
13768      followed by a strong using directive.  */
13769   if (is_inline)
13770     {
13771       tree name_space = current_namespace;
13772       /* Set up namespace association.  */
13773       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13774         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13775                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13776       /* Import the contents of the inline namespace.  */
13777       pop_namespace ();
13778       do_using_directive (name_space);
13779       push_namespace (identifier);
13780     }
13781
13782   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13783
13784   /* Parse the body of the namespace.  */
13785   cp_parser_namespace_body (parser);
13786
13787   if (has_visibility)
13788     pop_visibility (1);
13789
13790   /* Finish the namespace.  */
13791   pop_namespace ();
13792   /* Look for the final `}'.  */
13793   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13794 }
13795
13796 /* Parse a namespace-body.
13797
13798    namespace-body:
13799      declaration-seq [opt]  */
13800
13801 static void
13802 cp_parser_namespace_body (cp_parser* parser)
13803 {
13804   cp_parser_declaration_seq_opt (parser);
13805 }
13806
13807 /* Parse a namespace-alias-definition.
13808
13809    namespace-alias-definition:
13810      namespace identifier = qualified-namespace-specifier ;  */
13811
13812 static void
13813 cp_parser_namespace_alias_definition (cp_parser* parser)
13814 {
13815   tree identifier;
13816   tree namespace_specifier;
13817
13818   cp_token *token = cp_lexer_peek_token (parser->lexer);
13819
13820   /* Look for the `namespace' keyword.  */
13821   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13822   /* Look for the identifier.  */
13823   identifier = cp_parser_identifier (parser);
13824   if (identifier == error_mark_node)
13825     return;
13826   /* Look for the `=' token.  */
13827   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13828       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13829     {
13830       error_at (token->location, "%<namespace%> definition is not allowed here");
13831       /* Skip the definition.  */
13832       cp_lexer_consume_token (parser->lexer);
13833       if (cp_parser_skip_to_closing_brace (parser))
13834         cp_lexer_consume_token (parser->lexer);
13835       return;
13836     }
13837   cp_parser_require (parser, CPP_EQ, RT_EQ);
13838   /* Look for the qualified-namespace-specifier.  */
13839   namespace_specifier
13840     = cp_parser_qualified_namespace_specifier (parser);
13841   /* Look for the `;' token.  */
13842   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13843
13844   /* Register the alias in the symbol table.  */
13845   do_namespace_alias (identifier, namespace_specifier);
13846 }
13847
13848 /* Parse a qualified-namespace-specifier.
13849
13850    qualified-namespace-specifier:
13851      :: [opt] nested-name-specifier [opt] namespace-name
13852
13853    Returns a NAMESPACE_DECL corresponding to the specified
13854    namespace.  */
13855
13856 static tree
13857 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13858 {
13859   /* Look for the optional `::'.  */
13860   cp_parser_global_scope_opt (parser,
13861                               /*current_scope_valid_p=*/false);
13862
13863   /* Look for the optional nested-name-specifier.  */
13864   cp_parser_nested_name_specifier_opt (parser,
13865                                        /*typename_keyword_p=*/false,
13866                                        /*check_dependency_p=*/true,
13867                                        /*type_p=*/false,
13868                                        /*is_declaration=*/true);
13869
13870   return cp_parser_namespace_name (parser);
13871 }
13872
13873 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13874    access declaration.
13875
13876    using-declaration:
13877      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13878      using :: unqualified-id ;  
13879
13880    access-declaration:
13881      qualified-id ;  
13882
13883    */
13884
13885 static bool
13886 cp_parser_using_declaration (cp_parser* parser, 
13887                              bool access_declaration_p)
13888 {
13889   cp_token *token;
13890   bool typename_p = false;
13891   bool global_scope_p;
13892   tree decl;
13893   tree identifier;
13894   tree qscope;
13895
13896   if (access_declaration_p)
13897     cp_parser_parse_tentatively (parser);
13898   else
13899     {
13900       /* Look for the `using' keyword.  */
13901       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13902       
13903       /* Peek at the next token.  */
13904       token = cp_lexer_peek_token (parser->lexer);
13905       /* See if it's `typename'.  */
13906       if (token->keyword == RID_TYPENAME)
13907         {
13908           /* Remember that we've seen it.  */
13909           typename_p = true;
13910           /* Consume the `typename' token.  */
13911           cp_lexer_consume_token (parser->lexer);
13912         }
13913     }
13914
13915   /* Look for the optional global scope qualification.  */
13916   global_scope_p
13917     = (cp_parser_global_scope_opt (parser,
13918                                    /*current_scope_valid_p=*/false)
13919        != NULL_TREE);
13920
13921   /* If we saw `typename', or didn't see `::', then there must be a
13922      nested-name-specifier present.  */
13923   if (typename_p || !global_scope_p)
13924     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13925                                               /*check_dependency_p=*/true,
13926                                               /*type_p=*/false,
13927                                               /*is_declaration=*/true);
13928   /* Otherwise, we could be in either of the two productions.  In that
13929      case, treat the nested-name-specifier as optional.  */
13930   else
13931     qscope = cp_parser_nested_name_specifier_opt (parser,
13932                                                   /*typename_keyword_p=*/false,
13933                                                   /*check_dependency_p=*/true,
13934                                                   /*type_p=*/false,
13935                                                   /*is_declaration=*/true);
13936   if (!qscope)
13937     qscope = global_namespace;
13938
13939   if (access_declaration_p && cp_parser_error_occurred (parser))
13940     /* Something has already gone wrong; there's no need to parse
13941        further.  Since an error has occurred, the return value of
13942        cp_parser_parse_definitely will be false, as required.  */
13943     return cp_parser_parse_definitely (parser);
13944
13945   token = cp_lexer_peek_token (parser->lexer);
13946   /* Parse the unqualified-id.  */
13947   identifier = cp_parser_unqualified_id (parser,
13948                                          /*template_keyword_p=*/false,
13949                                          /*check_dependency_p=*/true,
13950                                          /*declarator_p=*/true,
13951                                          /*optional_p=*/false);
13952
13953   if (access_declaration_p)
13954     {
13955       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13956         cp_parser_simulate_error (parser);
13957       if (!cp_parser_parse_definitely (parser))
13958         return false;
13959     }
13960
13961   /* The function we call to handle a using-declaration is different
13962      depending on what scope we are in.  */
13963   if (qscope == error_mark_node || identifier == error_mark_node)
13964     ;
13965   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13966            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13967     /* [namespace.udecl]
13968
13969        A using declaration shall not name a template-id.  */
13970     error_at (token->location,
13971               "a template-id may not appear in a using-declaration");
13972   else
13973     {
13974       if (at_class_scope_p ())
13975         {
13976           /* Create the USING_DECL.  */
13977           decl = do_class_using_decl (parser->scope, identifier);
13978
13979           if (check_for_bare_parameter_packs (decl))
13980             return false;
13981           else
13982             /* Add it to the list of members in this class.  */
13983             finish_member_declaration (decl);
13984         }
13985       else
13986         {
13987           decl = cp_parser_lookup_name_simple (parser,
13988                                                identifier,
13989                                                token->location);
13990           if (decl == error_mark_node)
13991             cp_parser_name_lookup_error (parser, identifier,
13992                                          decl, NLE_NULL,
13993                                          token->location);
13994           else if (check_for_bare_parameter_packs (decl))
13995             return false;
13996           else if (!at_namespace_scope_p ())
13997             do_local_using_decl (decl, qscope, identifier);
13998           else
13999             do_toplevel_using_decl (decl, qscope, identifier);
14000         }
14001     }
14002
14003   /* Look for the final `;'.  */
14004   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14005   
14006   return true;
14007 }
14008
14009 /* Parse a using-directive.
14010
14011    using-directive:
14012      using namespace :: [opt] nested-name-specifier [opt]
14013        namespace-name ;  */
14014
14015 static void
14016 cp_parser_using_directive (cp_parser* parser)
14017 {
14018   tree namespace_decl;
14019   tree attribs;
14020
14021   /* Look for the `using' keyword.  */
14022   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14023   /* And the `namespace' keyword.  */
14024   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14025   /* Look for the optional `::' operator.  */
14026   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14027   /* And the optional nested-name-specifier.  */
14028   cp_parser_nested_name_specifier_opt (parser,
14029                                        /*typename_keyword_p=*/false,
14030                                        /*check_dependency_p=*/true,
14031                                        /*type_p=*/false,
14032                                        /*is_declaration=*/true);
14033   /* Get the namespace being used.  */
14034   namespace_decl = cp_parser_namespace_name (parser);
14035   /* And any specified attributes.  */
14036   attribs = cp_parser_attributes_opt (parser);
14037   /* Update the symbol table.  */
14038   parse_using_directive (namespace_decl, attribs);
14039   /* Look for the final `;'.  */
14040   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14041 }
14042
14043 /* Parse an asm-definition.
14044
14045    asm-definition:
14046      asm ( string-literal ) ;
14047
14048    GNU Extension:
14049
14050    asm-definition:
14051      asm volatile [opt] ( string-literal ) ;
14052      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14053      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14054                           : asm-operand-list [opt] ) ;
14055      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14056                           : asm-operand-list [opt]
14057                           : asm-clobber-list [opt] ) ;
14058      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14059                                : asm-clobber-list [opt]
14060                                : asm-goto-list ) ;  */
14061
14062 static void
14063 cp_parser_asm_definition (cp_parser* parser)
14064 {
14065   tree string;
14066   tree outputs = NULL_TREE;
14067   tree inputs = NULL_TREE;
14068   tree clobbers = NULL_TREE;
14069   tree labels = NULL_TREE;
14070   tree asm_stmt;
14071   bool volatile_p = false;
14072   bool extended_p = false;
14073   bool invalid_inputs_p = false;
14074   bool invalid_outputs_p = false;
14075   bool goto_p = false;
14076   required_token missing = RT_NONE;
14077
14078   /* Look for the `asm' keyword.  */
14079   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14080   /* See if the next token is `volatile'.  */
14081   if (cp_parser_allow_gnu_extensions_p (parser)
14082       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14083     {
14084       /* Remember that we saw the `volatile' keyword.  */
14085       volatile_p = true;
14086       /* Consume the token.  */
14087       cp_lexer_consume_token (parser->lexer);
14088     }
14089   if (cp_parser_allow_gnu_extensions_p (parser)
14090       && parser->in_function_body
14091       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14092     {
14093       /* Remember that we saw the `goto' keyword.  */
14094       goto_p = true;
14095       /* Consume the token.  */
14096       cp_lexer_consume_token (parser->lexer);
14097     }
14098   /* Look for the opening `('.  */
14099   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14100     return;
14101   /* Look for the string.  */
14102   string = cp_parser_string_literal (parser, false, false);
14103   if (string == error_mark_node)
14104     {
14105       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14106                                              /*consume_paren=*/true);
14107       return;
14108     }
14109
14110   /* If we're allowing GNU extensions, check for the extended assembly
14111      syntax.  Unfortunately, the `:' tokens need not be separated by
14112      a space in C, and so, for compatibility, we tolerate that here
14113      too.  Doing that means that we have to treat the `::' operator as
14114      two `:' tokens.  */
14115   if (cp_parser_allow_gnu_extensions_p (parser)
14116       && parser->in_function_body
14117       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14118           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14119     {
14120       bool inputs_p = false;
14121       bool clobbers_p = false;
14122       bool labels_p = false;
14123
14124       /* The extended syntax was used.  */
14125       extended_p = true;
14126
14127       /* Look for outputs.  */
14128       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14129         {
14130           /* Consume the `:'.  */
14131           cp_lexer_consume_token (parser->lexer);
14132           /* Parse the output-operands.  */
14133           if (cp_lexer_next_token_is_not (parser->lexer,
14134                                           CPP_COLON)
14135               && cp_lexer_next_token_is_not (parser->lexer,
14136                                              CPP_SCOPE)
14137               && cp_lexer_next_token_is_not (parser->lexer,
14138                                              CPP_CLOSE_PAREN)
14139               && !goto_p)
14140             outputs = cp_parser_asm_operand_list (parser);
14141
14142             if (outputs == error_mark_node)
14143               invalid_outputs_p = true;
14144         }
14145       /* If the next token is `::', there are no outputs, and the
14146          next token is the beginning of the inputs.  */
14147       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14148         /* The inputs are coming next.  */
14149         inputs_p = true;
14150
14151       /* Look for inputs.  */
14152       if (inputs_p
14153           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14154         {
14155           /* Consume the `:' or `::'.  */
14156           cp_lexer_consume_token (parser->lexer);
14157           /* Parse the output-operands.  */
14158           if (cp_lexer_next_token_is_not (parser->lexer,
14159                                           CPP_COLON)
14160               && cp_lexer_next_token_is_not (parser->lexer,
14161                                              CPP_SCOPE)
14162               && cp_lexer_next_token_is_not (parser->lexer,
14163                                              CPP_CLOSE_PAREN))
14164             inputs = cp_parser_asm_operand_list (parser);
14165
14166             if (inputs == error_mark_node)
14167               invalid_inputs_p = true;
14168         }
14169       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14170         /* The clobbers are coming next.  */
14171         clobbers_p = true;
14172
14173       /* Look for clobbers.  */
14174       if (clobbers_p
14175           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14176         {
14177           clobbers_p = true;
14178           /* Consume the `:' or `::'.  */
14179           cp_lexer_consume_token (parser->lexer);
14180           /* Parse the clobbers.  */
14181           if (cp_lexer_next_token_is_not (parser->lexer,
14182                                           CPP_COLON)
14183               && cp_lexer_next_token_is_not (parser->lexer,
14184                                              CPP_CLOSE_PAREN))
14185             clobbers = cp_parser_asm_clobber_list (parser);
14186         }
14187       else if (goto_p
14188                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14189         /* The labels are coming next.  */
14190         labels_p = true;
14191
14192       /* Look for labels.  */
14193       if (labels_p
14194           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14195         {
14196           labels_p = true;
14197           /* Consume the `:' or `::'.  */
14198           cp_lexer_consume_token (parser->lexer);
14199           /* Parse the labels.  */
14200           labels = cp_parser_asm_label_list (parser);
14201         }
14202
14203       if (goto_p && !labels_p)
14204         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14205     }
14206   else if (goto_p)
14207     missing = RT_COLON_SCOPE;
14208
14209   /* Look for the closing `)'.  */
14210   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14211                           missing ? missing : RT_CLOSE_PAREN))
14212     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14213                                            /*consume_paren=*/true);
14214   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14215
14216   if (!invalid_inputs_p && !invalid_outputs_p)
14217     {
14218       /* Create the ASM_EXPR.  */
14219       if (parser->in_function_body)
14220         {
14221           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14222                                       inputs, clobbers, labels);
14223           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14224           if (!extended_p)
14225             {
14226               tree temp = asm_stmt;
14227               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14228                 temp = TREE_OPERAND (temp, 0);
14229
14230               ASM_INPUT_P (temp) = 1;
14231             }
14232         }
14233       else
14234         cgraph_add_asm_node (string);
14235     }
14236 }
14237
14238 /* Declarators [gram.dcl.decl] */
14239
14240 /* Parse an init-declarator.
14241
14242    init-declarator:
14243      declarator initializer [opt]
14244
14245    GNU Extension:
14246
14247    init-declarator:
14248      declarator asm-specification [opt] attributes [opt] initializer [opt]
14249
14250    function-definition:
14251      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14252        function-body
14253      decl-specifier-seq [opt] declarator function-try-block
14254
14255    GNU Extension:
14256
14257    function-definition:
14258      __extension__ function-definition
14259
14260    The DECL_SPECIFIERS apply to this declarator.  Returns a
14261    representation of the entity declared.  If MEMBER_P is TRUE, then
14262    this declarator appears in a class scope.  The new DECL created by
14263    this declarator is returned.
14264
14265    The CHECKS are access checks that should be performed once we know
14266    what entity is being declared (and, therefore, what classes have
14267    befriended it).
14268
14269    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14270    for a function-definition here as well.  If the declarator is a
14271    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14272    be TRUE upon return.  By that point, the function-definition will
14273    have been completely parsed.
14274
14275    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14276    is FALSE.
14277
14278    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14279    parsed declaration if it is an uninitialized single declarator not followed
14280    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14281    if present, will not be consumed.  If returned, this declarator will be
14282    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14283
14284 static tree
14285 cp_parser_init_declarator (cp_parser* parser,
14286                            cp_decl_specifier_seq *decl_specifiers,
14287                            VEC (deferred_access_check,gc)* checks,
14288                            bool function_definition_allowed_p,
14289                            bool member_p,
14290                            int declares_class_or_enum,
14291                            bool* function_definition_p,
14292                            tree* maybe_range_for_decl)
14293 {
14294   cp_token *token = NULL, *asm_spec_start_token = NULL,
14295            *attributes_start_token = NULL;
14296   cp_declarator *declarator;
14297   tree prefix_attributes;
14298   tree attributes;
14299   tree asm_specification;
14300   tree initializer;
14301   tree decl = NULL_TREE;
14302   tree scope;
14303   int is_initialized;
14304   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14305      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14306      "(...)".  */
14307   enum cpp_ttype initialization_kind;
14308   bool is_direct_init = false;
14309   bool is_non_constant_init;
14310   int ctor_dtor_or_conv_p;
14311   bool friend_p;
14312   tree pushed_scope = NULL_TREE;
14313   bool range_for_decl_p = false;
14314
14315   /* Gather the attributes that were provided with the
14316      decl-specifiers.  */
14317   prefix_attributes = decl_specifiers->attributes;
14318
14319   /* Assume that this is not the declarator for a function
14320      definition.  */
14321   if (function_definition_p)
14322     *function_definition_p = false;
14323
14324   /* Defer access checks while parsing the declarator; we cannot know
14325      what names are accessible until we know what is being
14326      declared.  */
14327   resume_deferring_access_checks ();
14328
14329   /* Parse the declarator.  */
14330   token = cp_lexer_peek_token (parser->lexer);
14331   declarator
14332     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14333                             &ctor_dtor_or_conv_p,
14334                             /*parenthesized_p=*/NULL,
14335                             /*member_p=*/false);
14336   /* Gather up the deferred checks.  */
14337   stop_deferring_access_checks ();
14338
14339   /* If the DECLARATOR was erroneous, there's no need to go
14340      further.  */
14341   if (declarator == cp_error_declarator)
14342     return error_mark_node;
14343
14344   /* Check that the number of template-parameter-lists is OK.  */
14345   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14346                                                        token->location))
14347     return error_mark_node;
14348
14349   if (declares_class_or_enum & 2)
14350     cp_parser_check_for_definition_in_return_type (declarator,
14351                                                    decl_specifiers->type,
14352                                                    decl_specifiers->type_location);
14353
14354   /* Figure out what scope the entity declared by the DECLARATOR is
14355      located in.  `grokdeclarator' sometimes changes the scope, so
14356      we compute it now.  */
14357   scope = get_scope_of_declarator (declarator);
14358
14359   /* Perform any lookups in the declared type which were thought to be
14360      dependent, but are not in the scope of the declarator.  */
14361   decl_specifiers->type
14362     = maybe_update_decl_type (decl_specifiers->type, scope);
14363
14364   /* If we're allowing GNU extensions, look for an asm-specification
14365      and attributes.  */
14366   if (cp_parser_allow_gnu_extensions_p (parser))
14367     {
14368       /* Look for an asm-specification.  */
14369       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14370       asm_specification = cp_parser_asm_specification_opt (parser);
14371       /* And attributes.  */
14372       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14373       attributes = cp_parser_attributes_opt (parser);
14374     }
14375   else
14376     {
14377       asm_specification = NULL_TREE;
14378       attributes = NULL_TREE;
14379     }
14380
14381   /* Peek at the next token.  */
14382   token = cp_lexer_peek_token (parser->lexer);
14383   /* Check to see if the token indicates the start of a
14384      function-definition.  */
14385   if (function_declarator_p (declarator)
14386       && cp_parser_token_starts_function_definition_p (token))
14387     {
14388       if (!function_definition_allowed_p)
14389         {
14390           /* If a function-definition should not appear here, issue an
14391              error message.  */
14392           cp_parser_error (parser,
14393                            "a function-definition is not allowed here");
14394           return error_mark_node;
14395         }
14396       else
14397         {
14398           location_t func_brace_location
14399             = cp_lexer_peek_token (parser->lexer)->location;
14400
14401           /* Neither attributes nor an asm-specification are allowed
14402              on a function-definition.  */
14403           if (asm_specification)
14404             error_at (asm_spec_start_token->location,
14405                       "an asm-specification is not allowed "
14406                       "on a function-definition");
14407           if (attributes)
14408             error_at (attributes_start_token->location,
14409                       "attributes are not allowed on a function-definition");
14410           /* This is a function-definition.  */
14411           *function_definition_p = true;
14412
14413           /* Parse the function definition.  */
14414           if (member_p)
14415             decl = cp_parser_save_member_function_body (parser,
14416                                                         decl_specifiers,
14417                                                         declarator,
14418                                                         prefix_attributes);
14419           else
14420             decl
14421               = (cp_parser_function_definition_from_specifiers_and_declarator
14422                  (parser, decl_specifiers, prefix_attributes, declarator));
14423
14424           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14425             {
14426               /* This is where the prologue starts...  */
14427               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14428                 = func_brace_location;
14429             }
14430
14431           return decl;
14432         }
14433     }
14434
14435   /* [dcl.dcl]
14436
14437      Only in function declarations for constructors, destructors, and
14438      type conversions can the decl-specifier-seq be omitted.
14439
14440      We explicitly postpone this check past the point where we handle
14441      function-definitions because we tolerate function-definitions
14442      that are missing their return types in some modes.  */
14443   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14444     {
14445       cp_parser_error (parser,
14446                        "expected constructor, destructor, or type conversion");
14447       return error_mark_node;
14448     }
14449
14450   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14451   if (token->type == CPP_EQ
14452       || token->type == CPP_OPEN_PAREN
14453       || token->type == CPP_OPEN_BRACE)
14454     {
14455       is_initialized = SD_INITIALIZED;
14456       initialization_kind = token->type;
14457       if (maybe_range_for_decl)
14458         *maybe_range_for_decl = error_mark_node;
14459
14460       if (token->type == CPP_EQ
14461           && function_declarator_p (declarator))
14462         {
14463           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14464           if (t2->keyword == RID_DEFAULT)
14465             is_initialized = SD_DEFAULTED;
14466           else if (t2->keyword == RID_DELETE)
14467             is_initialized = SD_DELETED;
14468         }
14469     }
14470   else
14471     {
14472       /* If the init-declarator isn't initialized and isn't followed by a
14473          `,' or `;', it's not a valid init-declarator.  */
14474       if (token->type != CPP_COMMA
14475           && token->type != CPP_SEMICOLON)
14476         {
14477           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14478             range_for_decl_p = true;
14479           else
14480             {
14481               cp_parser_error (parser, "expected initializer");
14482               return error_mark_node;
14483             }
14484         }
14485       is_initialized = SD_UNINITIALIZED;
14486       initialization_kind = CPP_EOF;
14487     }
14488
14489   /* Because start_decl has side-effects, we should only call it if we
14490      know we're going ahead.  By this point, we know that we cannot
14491      possibly be looking at any other construct.  */
14492   cp_parser_commit_to_tentative_parse (parser);
14493
14494   /* If the decl specifiers were bad, issue an error now that we're
14495      sure this was intended to be a declarator.  Then continue
14496      declaring the variable(s), as int, to try to cut down on further
14497      errors.  */
14498   if (decl_specifiers->any_specifiers_p
14499       && decl_specifiers->type == error_mark_node)
14500     {
14501       cp_parser_error (parser, "invalid type in declaration");
14502       decl_specifiers->type = integer_type_node;
14503     }
14504
14505   /* Check to see whether or not this declaration is a friend.  */
14506   friend_p = cp_parser_friend_p (decl_specifiers);
14507
14508   /* Enter the newly declared entry in the symbol table.  If we're
14509      processing a declaration in a class-specifier, we wait until
14510      after processing the initializer.  */
14511   if (!member_p)
14512     {
14513       if (parser->in_unbraced_linkage_specification_p)
14514         decl_specifiers->storage_class = sc_extern;
14515       decl = start_decl (declarator, decl_specifiers,
14516                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14517                          attributes, prefix_attributes,
14518                          &pushed_scope);
14519       /* Adjust location of decl if declarator->id_loc is more appropriate:
14520          set, and decl wasn't merged with another decl, in which case its
14521          location would be different from input_location, and more accurate.  */
14522       if (DECL_P (decl)
14523           && declarator->id_loc != UNKNOWN_LOCATION
14524           && DECL_SOURCE_LOCATION (decl) == input_location)
14525         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14526     }
14527   else if (scope)
14528     /* Enter the SCOPE.  That way unqualified names appearing in the
14529        initializer will be looked up in SCOPE.  */
14530     pushed_scope = push_scope (scope);
14531
14532   /* Perform deferred access control checks, now that we know in which
14533      SCOPE the declared entity resides.  */
14534   if (!member_p && decl)
14535     {
14536       tree saved_current_function_decl = NULL_TREE;
14537
14538       /* If the entity being declared is a function, pretend that we
14539          are in its scope.  If it is a `friend', it may have access to
14540          things that would not otherwise be accessible.  */
14541       if (TREE_CODE (decl) == FUNCTION_DECL)
14542         {
14543           saved_current_function_decl = current_function_decl;
14544           current_function_decl = decl;
14545         }
14546
14547       /* Perform access checks for template parameters.  */
14548       cp_parser_perform_template_parameter_access_checks (checks);
14549
14550       /* Perform the access control checks for the declarator and the
14551          decl-specifiers.  */
14552       perform_deferred_access_checks ();
14553
14554       /* Restore the saved value.  */
14555       if (TREE_CODE (decl) == FUNCTION_DECL)
14556         current_function_decl = saved_current_function_decl;
14557     }
14558
14559   /* Parse the initializer.  */
14560   initializer = NULL_TREE;
14561   is_direct_init = false;
14562   is_non_constant_init = true;
14563   if (is_initialized)
14564     {
14565       if (function_declarator_p (declarator))
14566         {
14567           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14568            if (initialization_kind == CPP_EQ)
14569              initializer = cp_parser_pure_specifier (parser);
14570            else
14571              {
14572                /* If the declaration was erroneous, we don't really
14573                   know what the user intended, so just silently
14574                   consume the initializer.  */
14575                if (decl != error_mark_node)
14576                  error_at (initializer_start_token->location,
14577                            "initializer provided for function");
14578                cp_parser_skip_to_closing_parenthesis (parser,
14579                                                       /*recovering=*/true,
14580                                                       /*or_comma=*/false,
14581                                                       /*consume_paren=*/true);
14582              }
14583         }
14584       else
14585         {
14586           /* We want to record the extra mangling scope for in-class
14587              initializers of class members and initializers of static data
14588              member templates.  The former is a C++0x feature which isn't
14589              implemented yet, and I expect it will involve deferring
14590              parsing of the initializer until end of class as with default
14591              arguments.  So right here we only handle the latter.  */
14592           if (!member_p && processing_template_decl)
14593             start_lambda_scope (decl);
14594           initializer = cp_parser_initializer (parser,
14595                                                &is_direct_init,
14596                                                &is_non_constant_init);
14597           if (!member_p && processing_template_decl)
14598             finish_lambda_scope ();
14599         }
14600     }
14601
14602   /* The old parser allows attributes to appear after a parenthesized
14603      initializer.  Mark Mitchell proposed removing this functionality
14604      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14605      attributes -- but ignores them.  */
14606   if (cp_parser_allow_gnu_extensions_p (parser)
14607       && initialization_kind == CPP_OPEN_PAREN)
14608     if (cp_parser_attributes_opt (parser))
14609       warning (OPT_Wattributes,
14610                "attributes after parenthesized initializer ignored");
14611
14612   /* For an in-class declaration, use `grokfield' to create the
14613      declaration.  */
14614   if (member_p)
14615     {
14616       if (pushed_scope)
14617         {
14618           pop_scope (pushed_scope);
14619           pushed_scope = NULL_TREE;
14620         }
14621       decl = grokfield (declarator, decl_specifiers,
14622                         initializer, !is_non_constant_init,
14623                         /*asmspec=*/NULL_TREE,
14624                         prefix_attributes);
14625       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14626         cp_parser_save_default_args (parser, decl);
14627     }
14628
14629   /* Finish processing the declaration.  But, skip member
14630      declarations.  */
14631   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14632     {
14633       cp_finish_decl (decl,
14634                       initializer, !is_non_constant_init,
14635                       asm_specification,
14636                       /* If the initializer is in parentheses, then this is
14637                          a direct-initialization, which means that an
14638                          `explicit' constructor is OK.  Otherwise, an
14639                          `explicit' constructor cannot be used.  */
14640                       ((is_direct_init || !is_initialized)
14641                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14642     }
14643   else if ((cxx_dialect != cxx98) && friend_p
14644            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14645     /* Core issue #226 (C++0x only): A default template-argument
14646        shall not be specified in a friend class template
14647        declaration. */
14648     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14649                              /*is_partial=*/0, /*is_friend_decl=*/1);
14650
14651   if (!friend_p && pushed_scope)
14652     pop_scope (pushed_scope);
14653
14654   return decl;
14655 }
14656
14657 /* Parse a declarator.
14658
14659    declarator:
14660      direct-declarator
14661      ptr-operator declarator
14662
14663    abstract-declarator:
14664      ptr-operator abstract-declarator [opt]
14665      direct-abstract-declarator
14666
14667    GNU Extensions:
14668
14669    declarator:
14670      attributes [opt] direct-declarator
14671      attributes [opt] ptr-operator declarator
14672
14673    abstract-declarator:
14674      attributes [opt] ptr-operator abstract-declarator [opt]
14675      attributes [opt] direct-abstract-declarator
14676
14677    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14678    detect constructor, destructor or conversion operators. It is set
14679    to -1 if the declarator is a name, and +1 if it is a
14680    function. Otherwise it is set to zero. Usually you just want to
14681    test for >0, but internally the negative value is used.
14682
14683    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14684    a decl-specifier-seq unless it declares a constructor, destructor,
14685    or conversion.  It might seem that we could check this condition in
14686    semantic analysis, rather than parsing, but that makes it difficult
14687    to handle something like `f()'.  We want to notice that there are
14688    no decl-specifiers, and therefore realize that this is an
14689    expression, not a declaration.)
14690
14691    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14692    the declarator is a direct-declarator of the form "(...)".
14693
14694    MEMBER_P is true iff this declarator is a member-declarator.  */
14695
14696 static cp_declarator *
14697 cp_parser_declarator (cp_parser* parser,
14698                       cp_parser_declarator_kind dcl_kind,
14699                       int* ctor_dtor_or_conv_p,
14700                       bool* parenthesized_p,
14701                       bool member_p)
14702 {
14703   cp_declarator *declarator;
14704   enum tree_code code;
14705   cp_cv_quals cv_quals;
14706   tree class_type;
14707   tree attributes = NULL_TREE;
14708
14709   /* Assume this is not a constructor, destructor, or type-conversion
14710      operator.  */
14711   if (ctor_dtor_or_conv_p)
14712     *ctor_dtor_or_conv_p = 0;
14713
14714   if (cp_parser_allow_gnu_extensions_p (parser))
14715     attributes = cp_parser_attributes_opt (parser);
14716
14717   /* Check for the ptr-operator production.  */
14718   cp_parser_parse_tentatively (parser);
14719   /* Parse the ptr-operator.  */
14720   code = cp_parser_ptr_operator (parser,
14721                                  &class_type,
14722                                  &cv_quals);
14723   /* If that worked, then we have a ptr-operator.  */
14724   if (cp_parser_parse_definitely (parser))
14725     {
14726       /* If a ptr-operator was found, then this declarator was not
14727          parenthesized.  */
14728       if (parenthesized_p)
14729         *parenthesized_p = true;
14730       /* The dependent declarator is optional if we are parsing an
14731          abstract-declarator.  */
14732       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14733         cp_parser_parse_tentatively (parser);
14734
14735       /* Parse the dependent declarator.  */
14736       declarator = cp_parser_declarator (parser, dcl_kind,
14737                                          /*ctor_dtor_or_conv_p=*/NULL,
14738                                          /*parenthesized_p=*/NULL,
14739                                          /*member_p=*/false);
14740
14741       /* If we are parsing an abstract-declarator, we must handle the
14742          case where the dependent declarator is absent.  */
14743       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14744           && !cp_parser_parse_definitely (parser))
14745         declarator = NULL;
14746
14747       declarator = cp_parser_make_indirect_declarator
14748         (code, class_type, cv_quals, declarator);
14749     }
14750   /* Everything else is a direct-declarator.  */
14751   else
14752     {
14753       if (parenthesized_p)
14754         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14755                                                    CPP_OPEN_PAREN);
14756       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14757                                                 ctor_dtor_or_conv_p,
14758                                                 member_p);
14759     }
14760
14761   if (attributes && declarator && declarator != cp_error_declarator)
14762     declarator->attributes = attributes;
14763
14764   return declarator;
14765 }
14766
14767 /* Parse a direct-declarator or direct-abstract-declarator.
14768
14769    direct-declarator:
14770      declarator-id
14771      direct-declarator ( parameter-declaration-clause )
14772        cv-qualifier-seq [opt]
14773        exception-specification [opt]
14774      direct-declarator [ constant-expression [opt] ]
14775      ( declarator )
14776
14777    direct-abstract-declarator:
14778      direct-abstract-declarator [opt]
14779        ( parameter-declaration-clause )
14780        cv-qualifier-seq [opt]
14781        exception-specification [opt]
14782      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14783      ( abstract-declarator )
14784
14785    Returns a representation of the declarator.  DCL_KIND is
14786    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14787    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14788    we are parsing a direct-declarator.  It is
14789    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14790    of ambiguity we prefer an abstract declarator, as per
14791    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14792    cp_parser_declarator.  */
14793
14794 static cp_declarator *
14795 cp_parser_direct_declarator (cp_parser* parser,
14796                              cp_parser_declarator_kind dcl_kind,
14797                              int* ctor_dtor_or_conv_p,
14798                              bool member_p)
14799 {
14800   cp_token *token;
14801   cp_declarator *declarator = NULL;
14802   tree scope = NULL_TREE;
14803   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14804   bool saved_in_declarator_p = parser->in_declarator_p;
14805   bool first = true;
14806   tree pushed_scope = NULL_TREE;
14807
14808   while (true)
14809     {
14810       /* Peek at the next token.  */
14811       token = cp_lexer_peek_token (parser->lexer);
14812       if (token->type == CPP_OPEN_PAREN)
14813         {
14814           /* This is either a parameter-declaration-clause, or a
14815              parenthesized declarator. When we know we are parsing a
14816              named declarator, it must be a parenthesized declarator
14817              if FIRST is true. For instance, `(int)' is a
14818              parameter-declaration-clause, with an omitted
14819              direct-abstract-declarator. But `((*))', is a
14820              parenthesized abstract declarator. Finally, when T is a
14821              template parameter `(T)' is a
14822              parameter-declaration-clause, and not a parenthesized
14823              named declarator.
14824
14825              We first try and parse a parameter-declaration-clause,
14826              and then try a nested declarator (if FIRST is true).
14827
14828              It is not an error for it not to be a
14829              parameter-declaration-clause, even when FIRST is
14830              false. Consider,
14831
14832                int i (int);
14833                int i (3);
14834
14835              The first is the declaration of a function while the
14836              second is the definition of a variable, including its
14837              initializer.
14838
14839              Having seen only the parenthesis, we cannot know which of
14840              these two alternatives should be selected.  Even more
14841              complex are examples like:
14842
14843                int i (int (a));
14844                int i (int (3));
14845
14846              The former is a function-declaration; the latter is a
14847              variable initialization.
14848
14849              Thus again, we try a parameter-declaration-clause, and if
14850              that fails, we back out and return.  */
14851
14852           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14853             {
14854               tree params;
14855               unsigned saved_num_template_parameter_lists;
14856               bool is_declarator = false;
14857               tree t;
14858
14859               /* In a member-declarator, the only valid interpretation
14860                  of a parenthesis is the start of a
14861                  parameter-declaration-clause.  (It is invalid to
14862                  initialize a static data member with a parenthesized
14863                  initializer; only the "=" form of initialization is
14864                  permitted.)  */
14865               if (!member_p)
14866                 cp_parser_parse_tentatively (parser);
14867
14868               /* Consume the `('.  */
14869               cp_lexer_consume_token (parser->lexer);
14870               if (first)
14871                 {
14872                   /* If this is going to be an abstract declarator, we're
14873                      in a declarator and we can't have default args.  */
14874                   parser->default_arg_ok_p = false;
14875                   parser->in_declarator_p = true;
14876                 }
14877
14878               /* Inside the function parameter list, surrounding
14879                  template-parameter-lists do not apply.  */
14880               saved_num_template_parameter_lists
14881                 = parser->num_template_parameter_lists;
14882               parser->num_template_parameter_lists = 0;
14883
14884               begin_scope (sk_function_parms, NULL_TREE);
14885
14886               /* Parse the parameter-declaration-clause.  */
14887               params = cp_parser_parameter_declaration_clause (parser);
14888
14889               parser->num_template_parameter_lists
14890                 = saved_num_template_parameter_lists;
14891
14892               /* If all went well, parse the cv-qualifier-seq and the
14893                  exception-specification.  */
14894               if (member_p || cp_parser_parse_definitely (parser))
14895                 {
14896                   cp_cv_quals cv_quals;
14897                   tree exception_specification;
14898                   tree late_return;
14899
14900                   is_declarator = true;
14901
14902                   if (ctor_dtor_or_conv_p)
14903                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14904                   first = false;
14905                   /* Consume the `)'.  */
14906                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14907
14908                   /* Parse the cv-qualifier-seq.  */
14909                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14910                   /* And the exception-specification.  */
14911                   exception_specification
14912                     = cp_parser_exception_specification_opt (parser);
14913
14914                   late_return
14915                     = cp_parser_late_return_type_opt (parser);
14916
14917                   /* Create the function-declarator.  */
14918                   declarator = make_call_declarator (declarator,
14919                                                      params,
14920                                                      cv_quals,
14921                                                      exception_specification,
14922                                                      late_return);
14923                   /* Any subsequent parameter lists are to do with
14924                      return type, so are not those of the declared
14925                      function.  */
14926                   parser->default_arg_ok_p = false;
14927                 }
14928
14929               /* Remove the function parms from scope.  */
14930               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14931                 pop_binding (DECL_NAME (t), t);
14932               leave_scope();
14933
14934               if (is_declarator)
14935                 /* Repeat the main loop.  */
14936                 continue;
14937             }
14938
14939           /* If this is the first, we can try a parenthesized
14940              declarator.  */
14941           if (first)
14942             {
14943               bool saved_in_type_id_in_expr_p;
14944
14945               parser->default_arg_ok_p = saved_default_arg_ok_p;
14946               parser->in_declarator_p = saved_in_declarator_p;
14947
14948               /* Consume the `('.  */
14949               cp_lexer_consume_token (parser->lexer);
14950               /* Parse the nested declarator.  */
14951               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14952               parser->in_type_id_in_expr_p = true;
14953               declarator
14954                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14955                                         /*parenthesized_p=*/NULL,
14956                                         member_p);
14957               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14958               first = false;
14959               /* Expect a `)'.  */
14960               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14961                 declarator = cp_error_declarator;
14962               if (declarator == cp_error_declarator)
14963                 break;
14964
14965               goto handle_declarator;
14966             }
14967           /* Otherwise, we must be done.  */
14968           else
14969             break;
14970         }
14971       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14972                && token->type == CPP_OPEN_SQUARE)
14973         {
14974           /* Parse an array-declarator.  */
14975           tree bounds;
14976
14977           if (ctor_dtor_or_conv_p)
14978             *ctor_dtor_or_conv_p = 0;
14979
14980           first = false;
14981           parser->default_arg_ok_p = false;
14982           parser->in_declarator_p = true;
14983           /* Consume the `['.  */
14984           cp_lexer_consume_token (parser->lexer);
14985           /* Peek at the next token.  */
14986           token = cp_lexer_peek_token (parser->lexer);
14987           /* If the next token is `]', then there is no
14988              constant-expression.  */
14989           if (token->type != CPP_CLOSE_SQUARE)
14990             {
14991               bool non_constant_p;
14992
14993               bounds
14994                 = cp_parser_constant_expression (parser,
14995                                                  /*allow_non_constant=*/true,
14996                                                  &non_constant_p);
14997               if (!non_constant_p)
14998                 /* OK */;
14999               /* Normally, the array bound must be an integral constant
15000                  expression.  However, as an extension, we allow VLAs
15001                  in function scopes as long as they aren't part of a
15002                  parameter declaration.  */
15003               else if (!parser->in_function_body
15004                        || current_binding_level->kind == sk_function_parms)
15005                 {
15006                   cp_parser_error (parser,
15007                                    "array bound is not an integer constant");
15008                   bounds = error_mark_node;
15009                 }
15010               else if (processing_template_decl && !error_operand_p (bounds))
15011                 {
15012                   /* Remember this wasn't a constant-expression.  */
15013                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15014                   TREE_SIDE_EFFECTS (bounds) = 1;
15015                 }
15016             }
15017           else
15018             bounds = NULL_TREE;
15019           /* Look for the closing `]'.  */
15020           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15021             {
15022               declarator = cp_error_declarator;
15023               break;
15024             }
15025
15026           declarator = make_array_declarator (declarator, bounds);
15027         }
15028       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15029         {
15030           {
15031             tree qualifying_scope;
15032             tree unqualified_name;
15033             special_function_kind sfk;
15034             bool abstract_ok;
15035             bool pack_expansion_p = false;
15036             cp_token *declarator_id_start_token;
15037
15038             /* Parse a declarator-id */
15039             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15040             if (abstract_ok)
15041               {
15042                 cp_parser_parse_tentatively (parser);
15043
15044                 /* If we see an ellipsis, we should be looking at a
15045                    parameter pack. */
15046                 if (token->type == CPP_ELLIPSIS)
15047                   {
15048                     /* Consume the `...' */
15049                     cp_lexer_consume_token (parser->lexer);
15050
15051                     pack_expansion_p = true;
15052                   }
15053               }
15054
15055             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15056             unqualified_name
15057               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15058             qualifying_scope = parser->scope;
15059             if (abstract_ok)
15060               {
15061                 bool okay = false;
15062
15063                 if (!unqualified_name && pack_expansion_p)
15064                   {
15065                     /* Check whether an error occurred. */
15066                     okay = !cp_parser_error_occurred (parser);
15067
15068                     /* We already consumed the ellipsis to mark a
15069                        parameter pack, but we have no way to report it,
15070                        so abort the tentative parse. We will be exiting
15071                        immediately anyway. */
15072                     cp_parser_abort_tentative_parse (parser);
15073                   }
15074                 else
15075                   okay = cp_parser_parse_definitely (parser);
15076
15077                 if (!okay)
15078                   unqualified_name = error_mark_node;
15079                 else if (unqualified_name
15080                          && (qualifying_scope
15081                              || (TREE_CODE (unqualified_name)
15082                                  != IDENTIFIER_NODE)))
15083                   {
15084                     cp_parser_error (parser, "expected unqualified-id");
15085                     unqualified_name = error_mark_node;
15086                   }
15087               }
15088
15089             if (!unqualified_name)
15090               return NULL;
15091             if (unqualified_name == error_mark_node)
15092               {
15093                 declarator = cp_error_declarator;
15094                 pack_expansion_p = false;
15095                 declarator->parameter_pack_p = false;
15096                 break;
15097               }
15098
15099             if (qualifying_scope && at_namespace_scope_p ()
15100                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15101               {
15102                 /* In the declaration of a member of a template class
15103                    outside of the class itself, the SCOPE will sometimes
15104                    be a TYPENAME_TYPE.  For example, given:
15105
15106                    template <typename T>
15107                    int S<T>::R::i = 3;
15108
15109                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15110                    this context, we must resolve S<T>::R to an ordinary
15111                    type, rather than a typename type.
15112
15113                    The reason we normally avoid resolving TYPENAME_TYPEs
15114                    is that a specialization of `S' might render
15115                    `S<T>::R' not a type.  However, if `S' is
15116                    specialized, then this `i' will not be used, so there
15117                    is no harm in resolving the types here.  */
15118                 tree type;
15119
15120                 /* Resolve the TYPENAME_TYPE.  */
15121                 type = resolve_typename_type (qualifying_scope,
15122                                               /*only_current_p=*/false);
15123                 /* If that failed, the declarator is invalid.  */
15124                 if (TREE_CODE (type) == TYPENAME_TYPE)
15125                   {
15126                     if (typedef_variant_p (type))
15127                       error_at (declarator_id_start_token->location,
15128                                 "cannot define member of dependent typedef "
15129                                 "%qT", type);
15130                     else
15131                       error_at (declarator_id_start_token->location,
15132                                 "%<%T::%E%> is not a type",
15133                                 TYPE_CONTEXT (qualifying_scope),
15134                                 TYPE_IDENTIFIER (qualifying_scope));
15135                   }
15136                 qualifying_scope = type;
15137               }
15138
15139             sfk = sfk_none;
15140
15141             if (unqualified_name)
15142               {
15143                 tree class_type;
15144
15145                 if (qualifying_scope
15146                     && CLASS_TYPE_P (qualifying_scope))
15147                   class_type = qualifying_scope;
15148                 else
15149                   class_type = current_class_type;
15150
15151                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15152                   {
15153                     tree name_type = TREE_TYPE (unqualified_name);
15154                     if (class_type && same_type_p (name_type, class_type))
15155                       {
15156                         if (qualifying_scope
15157                             && CLASSTYPE_USE_TEMPLATE (name_type))
15158                           {
15159                             error_at (declarator_id_start_token->location,
15160                                       "invalid use of constructor as a template");
15161                             inform (declarator_id_start_token->location,
15162                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15163                                     "name the constructor in a qualified name",
15164                                     class_type,
15165                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15166                                     class_type, name_type);
15167                             declarator = cp_error_declarator;
15168                             break;
15169                           }
15170                         else
15171                           unqualified_name = constructor_name (class_type);
15172                       }
15173                     else
15174                       {
15175                         /* We do not attempt to print the declarator
15176                            here because we do not have enough
15177                            information about its original syntactic
15178                            form.  */
15179                         cp_parser_error (parser, "invalid declarator");
15180                         declarator = cp_error_declarator;
15181                         break;
15182                       }
15183                   }
15184
15185                 if (class_type)
15186                   {
15187                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15188                       sfk = sfk_destructor;
15189                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15190                       sfk = sfk_conversion;
15191                     else if (/* There's no way to declare a constructor
15192                                 for an anonymous type, even if the type
15193                                 got a name for linkage purposes.  */
15194                              !TYPE_WAS_ANONYMOUS (class_type)
15195                              && constructor_name_p (unqualified_name,
15196                                                     class_type))
15197                       {
15198                         unqualified_name = constructor_name (class_type);
15199                         sfk = sfk_constructor;
15200                       }
15201                     else if (is_overloaded_fn (unqualified_name)
15202                              && DECL_CONSTRUCTOR_P (get_first_fn
15203                                                     (unqualified_name)))
15204                       sfk = sfk_constructor;
15205
15206                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15207                       *ctor_dtor_or_conv_p = -1;
15208                   }
15209               }
15210             declarator = make_id_declarator (qualifying_scope,
15211                                              unqualified_name,
15212                                              sfk);
15213             declarator->id_loc = token->location;
15214             declarator->parameter_pack_p = pack_expansion_p;
15215
15216             if (pack_expansion_p)
15217               maybe_warn_variadic_templates ();
15218           }
15219
15220         handle_declarator:;
15221           scope = get_scope_of_declarator (declarator);
15222           if (scope)
15223             /* Any names that appear after the declarator-id for a
15224                member are looked up in the containing scope.  */
15225             pushed_scope = push_scope (scope);
15226           parser->in_declarator_p = true;
15227           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15228               || (declarator && declarator->kind == cdk_id))
15229             /* Default args are only allowed on function
15230                declarations.  */
15231             parser->default_arg_ok_p = saved_default_arg_ok_p;
15232           else
15233             parser->default_arg_ok_p = false;
15234
15235           first = false;
15236         }
15237       /* We're done.  */
15238       else
15239         break;
15240     }
15241
15242   /* For an abstract declarator, we might wind up with nothing at this
15243      point.  That's an error; the declarator is not optional.  */
15244   if (!declarator)
15245     cp_parser_error (parser, "expected declarator");
15246
15247   /* If we entered a scope, we must exit it now.  */
15248   if (pushed_scope)
15249     pop_scope (pushed_scope);
15250
15251   parser->default_arg_ok_p = saved_default_arg_ok_p;
15252   parser->in_declarator_p = saved_in_declarator_p;
15253
15254   return declarator;
15255 }
15256
15257 /* Parse a ptr-operator.
15258
15259    ptr-operator:
15260      * cv-qualifier-seq [opt]
15261      &
15262      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15263
15264    GNU Extension:
15265
15266    ptr-operator:
15267      & cv-qualifier-seq [opt]
15268
15269    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15270    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15271    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15272    filled in with the TYPE containing the member.  *CV_QUALS is
15273    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15274    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15275    Note that the tree codes returned by this function have nothing
15276    to do with the types of trees that will be eventually be created
15277    to represent the pointer or reference type being parsed. They are
15278    just constants with suggestive names. */
15279 static enum tree_code
15280 cp_parser_ptr_operator (cp_parser* parser,
15281                         tree* type,
15282                         cp_cv_quals *cv_quals)
15283 {
15284   enum tree_code code = ERROR_MARK;
15285   cp_token *token;
15286
15287   /* Assume that it's not a pointer-to-member.  */
15288   *type = NULL_TREE;
15289   /* And that there are no cv-qualifiers.  */
15290   *cv_quals = TYPE_UNQUALIFIED;
15291
15292   /* Peek at the next token.  */
15293   token = cp_lexer_peek_token (parser->lexer);
15294
15295   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15296   if (token->type == CPP_MULT)
15297     code = INDIRECT_REF;
15298   else if (token->type == CPP_AND)
15299     code = ADDR_EXPR;
15300   else if ((cxx_dialect != cxx98) &&
15301            token->type == CPP_AND_AND) /* C++0x only */
15302     code = NON_LVALUE_EXPR;
15303
15304   if (code != ERROR_MARK)
15305     {
15306       /* Consume the `*', `&' or `&&'.  */
15307       cp_lexer_consume_token (parser->lexer);
15308
15309       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15310          `&', if we are allowing GNU extensions.  (The only qualifier
15311          that can legally appear after `&' is `restrict', but that is
15312          enforced during semantic analysis.  */
15313       if (code == INDIRECT_REF
15314           || cp_parser_allow_gnu_extensions_p (parser))
15315         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15316     }
15317   else
15318     {
15319       /* Try the pointer-to-member case.  */
15320       cp_parser_parse_tentatively (parser);
15321       /* Look for the optional `::' operator.  */
15322       cp_parser_global_scope_opt (parser,
15323                                   /*current_scope_valid_p=*/false);
15324       /* Look for the nested-name specifier.  */
15325       token = cp_lexer_peek_token (parser->lexer);
15326       cp_parser_nested_name_specifier (parser,
15327                                        /*typename_keyword_p=*/false,
15328                                        /*check_dependency_p=*/true,
15329                                        /*type_p=*/false,
15330                                        /*is_declaration=*/false);
15331       /* If we found it, and the next token is a `*', then we are
15332          indeed looking at a pointer-to-member operator.  */
15333       if (!cp_parser_error_occurred (parser)
15334           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15335         {
15336           /* Indicate that the `*' operator was used.  */
15337           code = INDIRECT_REF;
15338
15339           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15340             error_at (token->location, "%qD is a namespace", parser->scope);
15341           else
15342             {
15343               /* The type of which the member is a member is given by the
15344                  current SCOPE.  */
15345               *type = parser->scope;
15346               /* The next name will not be qualified.  */
15347               parser->scope = NULL_TREE;
15348               parser->qualifying_scope = NULL_TREE;
15349               parser->object_scope = NULL_TREE;
15350               /* Look for the optional cv-qualifier-seq.  */
15351               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15352             }
15353         }
15354       /* If that didn't work we don't have a ptr-operator.  */
15355       if (!cp_parser_parse_definitely (parser))
15356         cp_parser_error (parser, "expected ptr-operator");
15357     }
15358
15359   return code;
15360 }
15361
15362 /* Parse an (optional) cv-qualifier-seq.
15363
15364    cv-qualifier-seq:
15365      cv-qualifier cv-qualifier-seq [opt]
15366
15367    cv-qualifier:
15368      const
15369      volatile
15370
15371    GNU Extension:
15372
15373    cv-qualifier:
15374      __restrict__
15375
15376    Returns a bitmask representing the cv-qualifiers.  */
15377
15378 static cp_cv_quals
15379 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15380 {
15381   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15382
15383   while (true)
15384     {
15385       cp_token *token;
15386       cp_cv_quals cv_qualifier;
15387
15388       /* Peek at the next token.  */
15389       token = cp_lexer_peek_token (parser->lexer);
15390       /* See if it's a cv-qualifier.  */
15391       switch (token->keyword)
15392         {
15393         case RID_CONST:
15394           cv_qualifier = TYPE_QUAL_CONST;
15395           break;
15396
15397         case RID_VOLATILE:
15398           cv_qualifier = TYPE_QUAL_VOLATILE;
15399           break;
15400
15401         case RID_RESTRICT:
15402           cv_qualifier = TYPE_QUAL_RESTRICT;
15403           break;
15404
15405         default:
15406           cv_qualifier = TYPE_UNQUALIFIED;
15407           break;
15408         }
15409
15410       if (!cv_qualifier)
15411         break;
15412
15413       if (cv_quals & cv_qualifier)
15414         {
15415           error_at (token->location, "duplicate cv-qualifier");
15416           cp_lexer_purge_token (parser->lexer);
15417         }
15418       else
15419         {
15420           cp_lexer_consume_token (parser->lexer);
15421           cv_quals |= cv_qualifier;
15422         }
15423     }
15424
15425   return cv_quals;
15426 }
15427
15428 /* Parse a late-specified return type, if any.  This is not a separate
15429    non-terminal, but part of a function declarator, which looks like
15430
15431    -> trailing-type-specifier-seq abstract-declarator(opt)
15432
15433    Returns the type indicated by the type-id.  */
15434
15435 static tree
15436 cp_parser_late_return_type_opt (cp_parser* parser)
15437 {
15438   cp_token *token;
15439
15440   /* Peek at the next token.  */
15441   token = cp_lexer_peek_token (parser->lexer);
15442   /* A late-specified return type is indicated by an initial '->'. */
15443   if (token->type != CPP_DEREF)
15444     return NULL_TREE;
15445
15446   /* Consume the ->.  */
15447   cp_lexer_consume_token (parser->lexer);
15448
15449   return cp_parser_trailing_type_id (parser);
15450 }
15451
15452 /* Parse a declarator-id.
15453
15454    declarator-id:
15455      id-expression
15456      :: [opt] nested-name-specifier [opt] type-name
15457
15458    In the `id-expression' case, the value returned is as for
15459    cp_parser_id_expression if the id-expression was an unqualified-id.
15460    If the id-expression was a qualified-id, then a SCOPE_REF is
15461    returned.  The first operand is the scope (either a NAMESPACE_DECL
15462    or TREE_TYPE), but the second is still just a representation of an
15463    unqualified-id.  */
15464
15465 static tree
15466 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15467 {
15468   tree id;
15469   /* The expression must be an id-expression.  Assume that qualified
15470      names are the names of types so that:
15471
15472        template <class T>
15473        int S<T>::R::i = 3;
15474
15475      will work; we must treat `S<T>::R' as the name of a type.
15476      Similarly, assume that qualified names are templates, where
15477      required, so that:
15478
15479        template <class T>
15480        int S<T>::R<T>::i = 3;
15481
15482      will work, too.  */
15483   id = cp_parser_id_expression (parser,
15484                                 /*template_keyword_p=*/false,
15485                                 /*check_dependency_p=*/false,
15486                                 /*template_p=*/NULL,
15487                                 /*declarator_p=*/true,
15488                                 optional_p);
15489   if (id && BASELINK_P (id))
15490     id = BASELINK_FUNCTIONS (id);
15491   return id;
15492 }
15493
15494 /* Parse a type-id.
15495
15496    type-id:
15497      type-specifier-seq abstract-declarator [opt]
15498
15499    Returns the TYPE specified.  */
15500
15501 static tree
15502 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15503                      bool is_trailing_return)
15504 {
15505   cp_decl_specifier_seq type_specifier_seq;
15506   cp_declarator *abstract_declarator;
15507
15508   /* Parse the type-specifier-seq.  */
15509   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15510                                 is_trailing_return,
15511                                 &type_specifier_seq);
15512   if (type_specifier_seq.type == error_mark_node)
15513     return error_mark_node;
15514
15515   /* There might or might not be an abstract declarator.  */
15516   cp_parser_parse_tentatively (parser);
15517   /* Look for the declarator.  */
15518   abstract_declarator
15519     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15520                             /*parenthesized_p=*/NULL,
15521                             /*member_p=*/false);
15522   /* Check to see if there really was a declarator.  */
15523   if (!cp_parser_parse_definitely (parser))
15524     abstract_declarator = NULL;
15525
15526   if (type_specifier_seq.type
15527       && type_uses_auto (type_specifier_seq.type))
15528     {
15529       /* A type-id with type 'auto' is only ok if the abstract declarator
15530          is a function declarator with a late-specified return type.  */
15531       if (abstract_declarator
15532           && abstract_declarator->kind == cdk_function
15533           && abstract_declarator->u.function.late_return_type)
15534         /* OK */;
15535       else
15536         {
15537           error ("invalid use of %<auto%>");
15538           return error_mark_node;
15539         }
15540     }
15541   
15542   return groktypename (&type_specifier_seq, abstract_declarator,
15543                        is_template_arg);
15544 }
15545
15546 static tree cp_parser_type_id (cp_parser *parser)
15547 {
15548   return cp_parser_type_id_1 (parser, false, false);
15549 }
15550
15551 static tree cp_parser_template_type_arg (cp_parser *parser)
15552 {
15553   tree r;
15554   const char *saved_message = parser->type_definition_forbidden_message;
15555   parser->type_definition_forbidden_message
15556     = G_("types may not be defined in template arguments");
15557   r = cp_parser_type_id_1 (parser, true, false);
15558   parser->type_definition_forbidden_message = saved_message;
15559   return r;
15560 }
15561
15562 static tree cp_parser_trailing_type_id (cp_parser *parser)
15563 {
15564   return cp_parser_type_id_1 (parser, false, true);
15565 }
15566
15567 /* Parse a type-specifier-seq.
15568
15569    type-specifier-seq:
15570      type-specifier type-specifier-seq [opt]
15571
15572    GNU extension:
15573
15574    type-specifier-seq:
15575      attributes type-specifier-seq [opt]
15576
15577    If IS_DECLARATION is true, we are at the start of a "condition" or
15578    exception-declaration, so we might be followed by a declarator-id.
15579
15580    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15581    i.e. we've just seen "->".
15582
15583    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15584
15585 static void
15586 cp_parser_type_specifier_seq (cp_parser* parser,
15587                               bool is_declaration,
15588                               bool is_trailing_return,
15589                               cp_decl_specifier_seq *type_specifier_seq)
15590 {
15591   bool seen_type_specifier = false;
15592   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15593   cp_token *start_token = NULL;
15594
15595   /* Clear the TYPE_SPECIFIER_SEQ.  */
15596   clear_decl_specs (type_specifier_seq);
15597
15598   /* In the context of a trailing return type, enum E { } is an
15599      elaborated-type-specifier followed by a function-body, not an
15600      enum-specifier.  */
15601   if (is_trailing_return)
15602     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15603
15604   /* Parse the type-specifiers and attributes.  */
15605   while (true)
15606     {
15607       tree type_specifier;
15608       bool is_cv_qualifier;
15609
15610       /* Check for attributes first.  */
15611       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15612         {
15613           type_specifier_seq->attributes =
15614             chainon (type_specifier_seq->attributes,
15615                      cp_parser_attributes_opt (parser));
15616           continue;
15617         }
15618
15619       /* record the token of the beginning of the type specifier seq,
15620          for error reporting purposes*/
15621      if (!start_token)
15622        start_token = cp_lexer_peek_token (parser->lexer);
15623
15624       /* Look for the type-specifier.  */
15625       type_specifier = cp_parser_type_specifier (parser,
15626                                                  flags,
15627                                                  type_specifier_seq,
15628                                                  /*is_declaration=*/false,
15629                                                  NULL,
15630                                                  &is_cv_qualifier);
15631       if (!type_specifier)
15632         {
15633           /* If the first type-specifier could not be found, this is not a
15634              type-specifier-seq at all.  */
15635           if (!seen_type_specifier)
15636             {
15637               cp_parser_error (parser, "expected type-specifier");
15638               type_specifier_seq->type = error_mark_node;
15639               return;
15640             }
15641           /* If subsequent type-specifiers could not be found, the
15642              type-specifier-seq is complete.  */
15643           break;
15644         }
15645
15646       seen_type_specifier = true;
15647       /* The standard says that a condition can be:
15648
15649             type-specifier-seq declarator = assignment-expression
15650
15651          However, given:
15652
15653            struct S {};
15654            if (int S = ...)
15655
15656          we should treat the "S" as a declarator, not as a
15657          type-specifier.  The standard doesn't say that explicitly for
15658          type-specifier-seq, but it does say that for
15659          decl-specifier-seq in an ordinary declaration.  Perhaps it
15660          would be clearer just to allow a decl-specifier-seq here, and
15661          then add a semantic restriction that if any decl-specifiers
15662          that are not type-specifiers appear, the program is invalid.  */
15663       if (is_declaration && !is_cv_qualifier)
15664         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15665     }
15666
15667   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15668 }
15669
15670 /* Parse a parameter-declaration-clause.
15671
15672    parameter-declaration-clause:
15673      parameter-declaration-list [opt] ... [opt]
15674      parameter-declaration-list , ...
15675
15676    Returns a representation for the parameter declarations.  A return
15677    value of NULL indicates a parameter-declaration-clause consisting
15678    only of an ellipsis.  */
15679
15680 static tree
15681 cp_parser_parameter_declaration_clause (cp_parser* parser)
15682 {
15683   tree parameters;
15684   cp_token *token;
15685   bool ellipsis_p;
15686   bool is_error;
15687
15688   /* Peek at the next token.  */
15689   token = cp_lexer_peek_token (parser->lexer);
15690   /* Check for trivial parameter-declaration-clauses.  */
15691   if (token->type == CPP_ELLIPSIS)
15692     {
15693       /* Consume the `...' token.  */
15694       cp_lexer_consume_token (parser->lexer);
15695       return NULL_TREE;
15696     }
15697   else if (token->type == CPP_CLOSE_PAREN)
15698     /* There are no parameters.  */
15699     {
15700 #ifndef NO_IMPLICIT_EXTERN_C
15701       if (in_system_header && current_class_type == NULL
15702           && current_lang_name == lang_name_c)
15703         return NULL_TREE;
15704       else
15705 #endif
15706         return void_list_node;
15707     }
15708   /* Check for `(void)', too, which is a special case.  */
15709   else if (token->keyword == RID_VOID
15710            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15711                == CPP_CLOSE_PAREN))
15712     {
15713       /* Consume the `void' token.  */
15714       cp_lexer_consume_token (parser->lexer);
15715       /* There are no parameters.  */
15716       return void_list_node;
15717     }
15718
15719   /* Parse the parameter-declaration-list.  */
15720   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15721   /* If a parse error occurred while parsing the
15722      parameter-declaration-list, then the entire
15723      parameter-declaration-clause is erroneous.  */
15724   if (is_error)
15725     return NULL;
15726
15727   /* Peek at the next token.  */
15728   token = cp_lexer_peek_token (parser->lexer);
15729   /* If it's a `,', the clause should terminate with an ellipsis.  */
15730   if (token->type == CPP_COMMA)
15731     {
15732       /* Consume the `,'.  */
15733       cp_lexer_consume_token (parser->lexer);
15734       /* Expect an ellipsis.  */
15735       ellipsis_p
15736         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15737     }
15738   /* It might also be `...' if the optional trailing `,' was
15739      omitted.  */
15740   else if (token->type == CPP_ELLIPSIS)
15741     {
15742       /* Consume the `...' token.  */
15743       cp_lexer_consume_token (parser->lexer);
15744       /* And remember that we saw it.  */
15745       ellipsis_p = true;
15746     }
15747   else
15748     ellipsis_p = false;
15749
15750   /* Finish the parameter list.  */
15751   if (!ellipsis_p)
15752     parameters = chainon (parameters, void_list_node);
15753
15754   return parameters;
15755 }
15756
15757 /* Parse a parameter-declaration-list.
15758
15759    parameter-declaration-list:
15760      parameter-declaration
15761      parameter-declaration-list , parameter-declaration
15762
15763    Returns a representation of the parameter-declaration-list, as for
15764    cp_parser_parameter_declaration_clause.  However, the
15765    `void_list_node' is never appended to the list.  Upon return,
15766    *IS_ERROR will be true iff an error occurred.  */
15767
15768 static tree
15769 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15770 {
15771   tree parameters = NULL_TREE;
15772   tree *tail = &parameters; 
15773   bool saved_in_unbraced_linkage_specification_p;
15774   int index = 0;
15775
15776   /* Assume all will go well.  */
15777   *is_error = false;
15778   /* The special considerations that apply to a function within an
15779      unbraced linkage specifications do not apply to the parameters
15780      to the function.  */
15781   saved_in_unbraced_linkage_specification_p 
15782     = parser->in_unbraced_linkage_specification_p;
15783   parser->in_unbraced_linkage_specification_p = false;
15784
15785   /* Look for more parameters.  */
15786   while (true)
15787     {
15788       cp_parameter_declarator *parameter;
15789       tree decl = error_mark_node;
15790       bool parenthesized_p;
15791       /* Parse the parameter.  */
15792       parameter
15793         = cp_parser_parameter_declaration (parser,
15794                                            /*template_parm_p=*/false,
15795                                            &parenthesized_p);
15796
15797       /* We don't know yet if the enclosing context is deprecated, so wait
15798          and warn in grokparms if appropriate.  */
15799       deprecated_state = DEPRECATED_SUPPRESS;
15800
15801       if (parameter)
15802         decl = grokdeclarator (parameter->declarator,
15803                                &parameter->decl_specifiers,
15804                                PARM,
15805                                parameter->default_argument != NULL_TREE,
15806                                &parameter->decl_specifiers.attributes);
15807
15808       deprecated_state = DEPRECATED_NORMAL;
15809
15810       /* If a parse error occurred parsing the parameter declaration,
15811          then the entire parameter-declaration-list is erroneous.  */
15812       if (decl == error_mark_node)
15813         {
15814           *is_error = true;
15815           parameters = error_mark_node;
15816           break;
15817         }
15818
15819       if (parameter->decl_specifiers.attributes)
15820         cplus_decl_attributes (&decl,
15821                                parameter->decl_specifiers.attributes,
15822                                0);
15823       if (DECL_NAME (decl))
15824         decl = pushdecl (decl);
15825
15826       if (decl != error_mark_node)
15827         {
15828           retrofit_lang_decl (decl);
15829           DECL_PARM_INDEX (decl) = ++index;
15830           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15831         }
15832
15833       /* Add the new parameter to the list.  */
15834       *tail = build_tree_list (parameter->default_argument, decl);
15835       tail = &TREE_CHAIN (*tail);
15836
15837       /* Peek at the next token.  */
15838       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15839           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15840           /* These are for Objective-C++ */
15841           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15842           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15843         /* The parameter-declaration-list is complete.  */
15844         break;
15845       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15846         {
15847           cp_token *token;
15848
15849           /* Peek at the next token.  */
15850           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15851           /* If it's an ellipsis, then the list is complete.  */
15852           if (token->type == CPP_ELLIPSIS)
15853             break;
15854           /* Otherwise, there must be more parameters.  Consume the
15855              `,'.  */
15856           cp_lexer_consume_token (parser->lexer);
15857           /* When parsing something like:
15858
15859                 int i(float f, double d)
15860
15861              we can tell after seeing the declaration for "f" that we
15862              are not looking at an initialization of a variable "i",
15863              but rather at the declaration of a function "i".
15864
15865              Due to the fact that the parsing of template arguments
15866              (as specified to a template-id) requires backtracking we
15867              cannot use this technique when inside a template argument
15868              list.  */
15869           if (!parser->in_template_argument_list_p
15870               && !parser->in_type_id_in_expr_p
15871               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15872               /* However, a parameter-declaration of the form
15873                  "foat(f)" (which is a valid declaration of a
15874                  parameter "f") can also be interpreted as an
15875                  expression (the conversion of "f" to "float").  */
15876               && !parenthesized_p)
15877             cp_parser_commit_to_tentative_parse (parser);
15878         }
15879       else
15880         {
15881           cp_parser_error (parser, "expected %<,%> or %<...%>");
15882           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15883             cp_parser_skip_to_closing_parenthesis (parser,
15884                                                    /*recovering=*/true,
15885                                                    /*or_comma=*/false,
15886                                                    /*consume_paren=*/false);
15887           break;
15888         }
15889     }
15890
15891   parser->in_unbraced_linkage_specification_p
15892     = saved_in_unbraced_linkage_specification_p;
15893
15894   return parameters;
15895 }
15896
15897 /* Parse a parameter declaration.
15898
15899    parameter-declaration:
15900      decl-specifier-seq ... [opt] declarator
15901      decl-specifier-seq declarator = assignment-expression
15902      decl-specifier-seq ... [opt] abstract-declarator [opt]
15903      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15904
15905    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15906    declares a template parameter.  (In that case, a non-nested `>'
15907    token encountered during the parsing of the assignment-expression
15908    is not interpreted as a greater-than operator.)
15909
15910    Returns a representation of the parameter, or NULL if an error
15911    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15912    true iff the declarator is of the form "(p)".  */
15913
15914 static cp_parameter_declarator *
15915 cp_parser_parameter_declaration (cp_parser *parser,
15916                                  bool template_parm_p,
15917                                  bool *parenthesized_p)
15918 {
15919   int declares_class_or_enum;
15920   cp_decl_specifier_seq decl_specifiers;
15921   cp_declarator *declarator;
15922   tree default_argument;
15923   cp_token *token = NULL, *declarator_token_start = NULL;
15924   const char *saved_message;
15925
15926   /* In a template parameter, `>' is not an operator.
15927
15928      [temp.param]
15929
15930      When parsing a default template-argument for a non-type
15931      template-parameter, the first non-nested `>' is taken as the end
15932      of the template parameter-list rather than a greater-than
15933      operator.  */
15934
15935   /* Type definitions may not appear in parameter types.  */
15936   saved_message = parser->type_definition_forbidden_message;
15937   parser->type_definition_forbidden_message
15938     = G_("types may not be defined in parameter types");
15939
15940   /* Parse the declaration-specifiers.  */
15941   cp_parser_decl_specifier_seq (parser,
15942                                 CP_PARSER_FLAGS_NONE,
15943                                 &decl_specifiers,
15944                                 &declares_class_or_enum);
15945
15946   /* Complain about missing 'typename' or other invalid type names.  */
15947   if (!decl_specifiers.any_type_specifiers_p)
15948     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15949
15950   /* If an error occurred, there's no reason to attempt to parse the
15951      rest of the declaration.  */
15952   if (cp_parser_error_occurred (parser))
15953     {
15954       parser->type_definition_forbidden_message = saved_message;
15955       return NULL;
15956     }
15957
15958   /* Peek at the next token.  */
15959   token = cp_lexer_peek_token (parser->lexer);
15960
15961   /* If the next token is a `)', `,', `=', `>', or `...', then there
15962      is no declarator. However, when variadic templates are enabled,
15963      there may be a declarator following `...'.  */
15964   if (token->type == CPP_CLOSE_PAREN
15965       || token->type == CPP_COMMA
15966       || token->type == CPP_EQ
15967       || token->type == CPP_GREATER)
15968     {
15969       declarator = NULL;
15970       if (parenthesized_p)
15971         *parenthesized_p = false;
15972     }
15973   /* Otherwise, there should be a declarator.  */
15974   else
15975     {
15976       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15977       parser->default_arg_ok_p = false;
15978
15979       /* After seeing a decl-specifier-seq, if the next token is not a
15980          "(", there is no possibility that the code is a valid
15981          expression.  Therefore, if parsing tentatively, we commit at
15982          this point.  */
15983       if (!parser->in_template_argument_list_p
15984           /* In an expression context, having seen:
15985
15986                (int((char ...
15987
15988              we cannot be sure whether we are looking at a
15989              function-type (taking a "char" as a parameter) or a cast
15990              of some object of type "char" to "int".  */
15991           && !parser->in_type_id_in_expr_p
15992           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15993           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15994         cp_parser_commit_to_tentative_parse (parser);
15995       /* Parse the declarator.  */
15996       declarator_token_start = token;
15997       declarator = cp_parser_declarator (parser,
15998                                          CP_PARSER_DECLARATOR_EITHER,
15999                                          /*ctor_dtor_or_conv_p=*/NULL,
16000                                          parenthesized_p,
16001                                          /*member_p=*/false);
16002       parser->default_arg_ok_p = saved_default_arg_ok_p;
16003       /* After the declarator, allow more attributes.  */
16004       decl_specifiers.attributes
16005         = chainon (decl_specifiers.attributes,
16006                    cp_parser_attributes_opt (parser));
16007     }
16008
16009   /* If the next token is an ellipsis, and we have not seen a
16010      declarator name, and the type of the declarator contains parameter
16011      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16012      a parameter pack expansion expression. Otherwise, leave the
16013      ellipsis for a C-style variadic function. */
16014   token = cp_lexer_peek_token (parser->lexer);
16015   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16016     {
16017       tree type = decl_specifiers.type;
16018
16019       if (type && DECL_P (type))
16020         type = TREE_TYPE (type);
16021
16022       if (type
16023           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16024           && declarator_can_be_parameter_pack (declarator)
16025           && (!declarator || !declarator->parameter_pack_p)
16026           && uses_parameter_packs (type))
16027         {
16028           /* Consume the `...'. */
16029           cp_lexer_consume_token (parser->lexer);
16030           maybe_warn_variadic_templates ();
16031           
16032           /* Build a pack expansion type */
16033           if (declarator)
16034             declarator->parameter_pack_p = true;
16035           else
16036             decl_specifiers.type = make_pack_expansion (type);
16037         }
16038     }
16039
16040   /* The restriction on defining new types applies only to the type
16041      of the parameter, not to the default argument.  */
16042   parser->type_definition_forbidden_message = saved_message;
16043
16044   /* If the next token is `=', then process a default argument.  */
16045   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16046     {
16047       /* Consume the `='.  */
16048       cp_lexer_consume_token (parser->lexer);
16049
16050       /* If we are defining a class, then the tokens that make up the
16051          default argument must be saved and processed later.  */
16052       if (!template_parm_p && at_class_scope_p ()
16053           && TYPE_BEING_DEFINED (current_class_type)
16054           && !LAMBDA_TYPE_P (current_class_type))
16055         {
16056           unsigned depth = 0;
16057           int maybe_template_id = 0;
16058           cp_token *first_token;
16059           cp_token *token;
16060
16061           /* Add tokens until we have processed the entire default
16062              argument.  We add the range [first_token, token).  */
16063           first_token = cp_lexer_peek_token (parser->lexer);
16064           while (true)
16065             {
16066               bool done = false;
16067
16068               /* Peek at the next token.  */
16069               token = cp_lexer_peek_token (parser->lexer);
16070               /* What we do depends on what token we have.  */
16071               switch (token->type)
16072                 {
16073                   /* In valid code, a default argument must be
16074                      immediately followed by a `,' `)', or `...'.  */
16075                 case CPP_COMMA:
16076                   if (depth == 0 && maybe_template_id)
16077                     {
16078                       /* If we've seen a '<', we might be in a
16079                          template-argument-list.  Until Core issue 325 is
16080                          resolved, we don't know how this situation ought
16081                          to be handled, so try to DTRT.  We check whether
16082                          what comes after the comma is a valid parameter
16083                          declaration list.  If it is, then the comma ends
16084                          the default argument; otherwise the default
16085                          argument continues.  */
16086                       bool error = false;
16087                       tree t;
16088
16089                       /* Set ITALP so cp_parser_parameter_declaration_list
16090                          doesn't decide to commit to this parse.  */
16091                       bool saved_italp = parser->in_template_argument_list_p;
16092                       parser->in_template_argument_list_p = true;
16093
16094                       cp_parser_parse_tentatively (parser);
16095                       cp_lexer_consume_token (parser->lexer);
16096                       begin_scope (sk_function_parms, NULL_TREE);
16097                       cp_parser_parameter_declaration_list (parser, &error);
16098                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16099                         pop_binding (DECL_NAME (t), t);
16100                       leave_scope ();
16101                       if (!cp_parser_error_occurred (parser) && !error)
16102                         done = true;
16103                       cp_parser_abort_tentative_parse (parser);
16104
16105                       parser->in_template_argument_list_p = saved_italp;
16106                       break;
16107                     }
16108                 case CPP_CLOSE_PAREN:
16109                 case CPP_ELLIPSIS:
16110                   /* If we run into a non-nested `;', `}', or `]',
16111                      then the code is invalid -- but the default
16112                      argument is certainly over.  */
16113                 case CPP_SEMICOLON:
16114                 case CPP_CLOSE_BRACE:
16115                 case CPP_CLOSE_SQUARE:
16116                   if (depth == 0)
16117                     done = true;
16118                   /* Update DEPTH, if necessary.  */
16119                   else if (token->type == CPP_CLOSE_PAREN
16120                            || token->type == CPP_CLOSE_BRACE
16121                            || token->type == CPP_CLOSE_SQUARE)
16122                     --depth;
16123                   break;
16124
16125                 case CPP_OPEN_PAREN:
16126                 case CPP_OPEN_SQUARE:
16127                 case CPP_OPEN_BRACE:
16128                   ++depth;
16129                   break;
16130
16131                 case CPP_LESS:
16132                   if (depth == 0)
16133                     /* This might be the comparison operator, or it might
16134                        start a template argument list.  */
16135                     ++maybe_template_id;
16136                   break;
16137
16138                 case CPP_RSHIFT:
16139                   if (cxx_dialect == cxx98)
16140                     break;
16141                   /* Fall through for C++0x, which treats the `>>'
16142                      operator like two `>' tokens in certain
16143                      cases.  */
16144
16145                 case CPP_GREATER:
16146                   if (depth == 0)
16147                     {
16148                       /* This might be an operator, or it might close a
16149                          template argument list.  But if a previous '<'
16150                          started a template argument list, this will have
16151                          closed it, so we can't be in one anymore.  */
16152                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16153                       if (maybe_template_id < 0)
16154                         maybe_template_id = 0;
16155                     }
16156                   break;
16157
16158                   /* If we run out of tokens, issue an error message.  */
16159                 case CPP_EOF:
16160                 case CPP_PRAGMA_EOL:
16161                   error_at (token->location, "file ends in default argument");
16162                   done = true;
16163                   break;
16164
16165                 case CPP_NAME:
16166                 case CPP_SCOPE:
16167                   /* In these cases, we should look for template-ids.
16168                      For example, if the default argument is
16169                      `X<int, double>()', we need to do name lookup to
16170                      figure out whether or not `X' is a template; if
16171                      so, the `,' does not end the default argument.
16172
16173                      That is not yet done.  */
16174                   break;
16175
16176                 default:
16177                   break;
16178                 }
16179
16180               /* If we've reached the end, stop.  */
16181               if (done)
16182                 break;
16183
16184               /* Add the token to the token block.  */
16185               token = cp_lexer_consume_token (parser->lexer);
16186             }
16187
16188           /* Create a DEFAULT_ARG to represent the unparsed default
16189              argument.  */
16190           default_argument = make_node (DEFAULT_ARG);
16191           DEFARG_TOKENS (default_argument)
16192             = cp_token_cache_new (first_token, token);
16193           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16194         }
16195       /* Outside of a class definition, we can just parse the
16196          assignment-expression.  */
16197       else
16198         {
16199           token = cp_lexer_peek_token (parser->lexer);
16200           default_argument 
16201             = cp_parser_default_argument (parser, template_parm_p);
16202         }
16203
16204       if (!parser->default_arg_ok_p)
16205         {
16206           if (flag_permissive)
16207             warning (0, "deprecated use of default argument for parameter of non-function");
16208           else
16209             {
16210               error_at (token->location,
16211                         "default arguments are only "
16212                         "permitted for function parameters");
16213               default_argument = NULL_TREE;
16214             }
16215         }
16216       else if ((declarator && declarator->parameter_pack_p)
16217                || (decl_specifiers.type
16218                    && PACK_EXPANSION_P (decl_specifiers.type)))
16219         {
16220           /* Find the name of the parameter pack.  */     
16221           cp_declarator *id_declarator = declarator;
16222           while (id_declarator && id_declarator->kind != cdk_id)
16223             id_declarator = id_declarator->declarator;
16224           
16225           if (id_declarator && id_declarator->kind == cdk_id)
16226             error_at (declarator_token_start->location,
16227                       template_parm_p 
16228                       ? "template parameter pack %qD"
16229                       " cannot have a default argument"
16230                       : "parameter pack %qD cannot have a default argument",
16231                       id_declarator->u.id.unqualified_name);
16232           else
16233             error_at (declarator_token_start->location,
16234                       template_parm_p 
16235                       ? "template parameter pack cannot have a default argument"
16236                       : "parameter pack cannot have a default argument");
16237           
16238           default_argument = NULL_TREE;
16239         }
16240     }
16241   else
16242     default_argument = NULL_TREE;
16243
16244   return make_parameter_declarator (&decl_specifiers,
16245                                     declarator,
16246                                     default_argument);
16247 }
16248
16249 /* Parse a default argument and return it.
16250
16251    TEMPLATE_PARM_P is true if this is a default argument for a
16252    non-type template parameter.  */
16253 static tree
16254 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16255 {
16256   tree default_argument = NULL_TREE;
16257   bool saved_greater_than_is_operator_p;
16258   bool saved_local_variables_forbidden_p;
16259
16260   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16261      set correctly.  */
16262   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16263   parser->greater_than_is_operator_p = !template_parm_p;
16264   /* Local variable names (and the `this' keyword) may not
16265      appear in a default argument.  */
16266   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16267   parser->local_variables_forbidden_p = true;
16268   /* Parse the assignment-expression.  */
16269   if (template_parm_p)
16270     push_deferring_access_checks (dk_no_deferred);
16271   default_argument
16272     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16273   if (template_parm_p)
16274     pop_deferring_access_checks ();
16275   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16276   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16277
16278   return default_argument;
16279 }
16280
16281 /* Parse a function-body.
16282
16283    function-body:
16284      compound_statement  */
16285
16286 static void
16287 cp_parser_function_body (cp_parser *parser)
16288 {
16289   cp_parser_compound_statement (parser, NULL, false, true);
16290 }
16291
16292 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16293    true if a ctor-initializer was present.  */
16294
16295 static bool
16296 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16297 {
16298   tree body, list;
16299   bool ctor_initializer_p;
16300   const bool check_body_p =
16301      DECL_CONSTRUCTOR_P (current_function_decl)
16302      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16303   tree last = NULL;
16304
16305   /* Begin the function body.  */
16306   body = begin_function_body ();
16307   /* Parse the optional ctor-initializer.  */
16308   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16309
16310   /* If we're parsing a constexpr constructor definition, we need
16311      to check that the constructor body is indeed empty.  However,
16312      before we get to cp_parser_function_body lot of junk has been
16313      generated, so we can't just check that we have an empty block.
16314      Rather we take a snapshot of the outermost block, and check whether
16315      cp_parser_function_body changed its state.  */
16316   if (check_body_p)
16317     {
16318       list = body;
16319       if (TREE_CODE (list) == BIND_EXPR)
16320         list = BIND_EXPR_BODY (list);
16321       if (TREE_CODE (list) == STATEMENT_LIST
16322           && STATEMENT_LIST_TAIL (list) != NULL)
16323         last = STATEMENT_LIST_TAIL (list)->stmt;
16324     }
16325   /* Parse the function-body.  */
16326   cp_parser_function_body (parser);
16327   if (check_body_p)
16328     check_constexpr_ctor_body (last, list);
16329   /* Finish the function body.  */
16330   finish_function_body (body);
16331
16332   return ctor_initializer_p;
16333 }
16334
16335 /* Parse an initializer.
16336
16337    initializer:
16338      = initializer-clause
16339      ( expression-list )
16340
16341    Returns an expression representing the initializer.  If no
16342    initializer is present, NULL_TREE is returned.
16343
16344    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16345    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16346    set to TRUE if there is no initializer present.  If there is an
16347    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16348    is set to true; otherwise it is set to false.  */
16349
16350 static tree
16351 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16352                        bool* non_constant_p)
16353 {
16354   cp_token *token;
16355   tree init;
16356
16357   /* Peek at the next token.  */
16358   token = cp_lexer_peek_token (parser->lexer);
16359
16360   /* Let our caller know whether or not this initializer was
16361      parenthesized.  */
16362   *is_direct_init = (token->type != CPP_EQ);
16363   /* Assume that the initializer is constant.  */
16364   *non_constant_p = false;
16365
16366   if (token->type == CPP_EQ)
16367     {
16368       /* Consume the `='.  */
16369       cp_lexer_consume_token (parser->lexer);
16370       /* Parse the initializer-clause.  */
16371       init = cp_parser_initializer_clause (parser, non_constant_p);
16372     }
16373   else if (token->type == CPP_OPEN_PAREN)
16374     {
16375       VEC(tree,gc) *vec;
16376       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16377                                                      /*cast_p=*/false,
16378                                                      /*allow_expansion_p=*/true,
16379                                                      non_constant_p);
16380       if (vec == NULL)
16381         return error_mark_node;
16382       init = build_tree_list_vec (vec);
16383       release_tree_vector (vec);
16384     }
16385   else if (token->type == CPP_OPEN_BRACE)
16386     {
16387       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16388       init = cp_parser_braced_list (parser, non_constant_p);
16389       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16390     }
16391   else
16392     {
16393       /* Anything else is an error.  */
16394       cp_parser_error (parser, "expected initializer");
16395       init = error_mark_node;
16396     }
16397
16398   return init;
16399 }
16400
16401 /* Parse an initializer-clause.
16402
16403    initializer-clause:
16404      assignment-expression
16405      braced-init-list
16406
16407    Returns an expression representing the initializer.
16408
16409    If the `assignment-expression' production is used the value
16410    returned is simply a representation for the expression.
16411
16412    Otherwise, calls cp_parser_braced_list.  */
16413
16414 static tree
16415 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16416 {
16417   tree initializer;
16418
16419   /* Assume the expression is constant.  */
16420   *non_constant_p = false;
16421
16422   /* If it is not a `{', then we are looking at an
16423      assignment-expression.  */
16424   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16425     {
16426       initializer
16427         = cp_parser_constant_expression (parser,
16428                                         /*allow_non_constant_p=*/true,
16429                                         non_constant_p);
16430       if (!*non_constant_p)
16431         {
16432           /* We only want to fold if this is really a constant
16433              expression.  FIXME Actually, we don't want to fold here, but in
16434              cp_finish_decl.  */
16435           tree folded = fold_non_dependent_expr (initializer);
16436           folded = maybe_constant_value (folded);
16437           if (TREE_CONSTANT (folded))
16438             initializer = folded;
16439         }
16440     }
16441   else
16442     initializer = cp_parser_braced_list (parser, non_constant_p);
16443
16444   return initializer;
16445 }
16446
16447 /* Parse a brace-enclosed initializer list.
16448
16449    braced-init-list:
16450      { initializer-list , [opt] }
16451      { }
16452
16453    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16454    the elements of the initializer-list (or NULL, if the last
16455    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16456    NULL_TREE.  There is no way to detect whether or not the optional
16457    trailing `,' was provided.  NON_CONSTANT_P is as for
16458    cp_parser_initializer.  */     
16459
16460 static tree
16461 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16462 {
16463   tree initializer;
16464
16465   /* Consume the `{' token.  */
16466   cp_lexer_consume_token (parser->lexer);
16467   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16468   initializer = make_node (CONSTRUCTOR);
16469   /* If it's not a `}', then there is a non-trivial initializer.  */
16470   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16471     {
16472       /* Parse the initializer list.  */
16473       CONSTRUCTOR_ELTS (initializer)
16474         = cp_parser_initializer_list (parser, non_constant_p);
16475       /* A trailing `,' token is allowed.  */
16476       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16477         cp_lexer_consume_token (parser->lexer);
16478     }
16479   /* Now, there should be a trailing `}'.  */
16480   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16481   TREE_TYPE (initializer) = init_list_type_node;
16482   return initializer;
16483 }
16484
16485 /* Parse an initializer-list.
16486
16487    initializer-list:
16488      initializer-clause ... [opt]
16489      initializer-list , initializer-clause ... [opt]
16490
16491    GNU Extension:
16492
16493    initializer-list:
16494      identifier : initializer-clause
16495      initializer-list, identifier : initializer-clause
16496
16497    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16498    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16499    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16500    as for cp_parser_initializer.  */
16501
16502 static VEC(constructor_elt,gc) *
16503 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16504 {
16505   VEC(constructor_elt,gc) *v = NULL;
16506
16507   /* Assume all of the expressions are constant.  */
16508   *non_constant_p = false;
16509
16510   /* Parse the rest of the list.  */
16511   while (true)
16512     {
16513       cp_token *token;
16514       tree identifier;
16515       tree initializer;
16516       bool clause_non_constant_p;
16517
16518       /* If the next token is an identifier and the following one is a
16519          colon, we are looking at the GNU designated-initializer
16520          syntax.  */
16521       if (cp_parser_allow_gnu_extensions_p (parser)
16522           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16523           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16524         {
16525           /* Warn the user that they are using an extension.  */
16526           pedwarn (input_location, OPT_pedantic, 
16527                    "ISO C++ does not allow designated initializers");
16528           /* Consume the identifier.  */
16529           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16530           /* Consume the `:'.  */
16531           cp_lexer_consume_token (parser->lexer);
16532         }
16533       else
16534         identifier = NULL_TREE;
16535
16536       /* Parse the initializer.  */
16537       initializer = cp_parser_initializer_clause (parser,
16538                                                   &clause_non_constant_p);
16539       /* If any clause is non-constant, so is the entire initializer.  */
16540       if (clause_non_constant_p)
16541         *non_constant_p = true;
16542
16543       /* If we have an ellipsis, this is an initializer pack
16544          expansion.  */
16545       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16546         {
16547           /* Consume the `...'.  */
16548           cp_lexer_consume_token (parser->lexer);
16549
16550           /* Turn the initializer into an initializer expansion.  */
16551           initializer = make_pack_expansion (initializer);
16552         }
16553
16554       /* Add it to the vector.  */
16555       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16556
16557       /* If the next token is not a comma, we have reached the end of
16558          the list.  */
16559       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16560         break;
16561
16562       /* Peek at the next token.  */
16563       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16564       /* If the next token is a `}', then we're still done.  An
16565          initializer-clause can have a trailing `,' after the
16566          initializer-list and before the closing `}'.  */
16567       if (token->type == CPP_CLOSE_BRACE)
16568         break;
16569
16570       /* Consume the `,' token.  */
16571       cp_lexer_consume_token (parser->lexer);
16572     }
16573
16574   return v;
16575 }
16576
16577 /* Classes [gram.class] */
16578
16579 /* Parse a class-name.
16580
16581    class-name:
16582      identifier
16583      template-id
16584
16585    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16586    to indicate that names looked up in dependent types should be
16587    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16588    keyword has been used to indicate that the name that appears next
16589    is a template.  TAG_TYPE indicates the explicit tag given before
16590    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16591    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16592    is the class being defined in a class-head.
16593
16594    Returns the TYPE_DECL representing the class.  */
16595
16596 static tree
16597 cp_parser_class_name (cp_parser *parser,
16598                       bool typename_keyword_p,
16599                       bool template_keyword_p,
16600                       enum tag_types tag_type,
16601                       bool check_dependency_p,
16602                       bool class_head_p,
16603                       bool is_declaration)
16604 {
16605   tree decl;
16606   tree scope;
16607   bool typename_p;
16608   cp_token *token;
16609   tree identifier = NULL_TREE;
16610
16611   /* All class-names start with an identifier.  */
16612   token = cp_lexer_peek_token (parser->lexer);
16613   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16614     {
16615       cp_parser_error (parser, "expected class-name");
16616       return error_mark_node;
16617     }
16618
16619   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16620      to a template-id, so we save it here.  */
16621   scope = parser->scope;
16622   if (scope == error_mark_node)
16623     return error_mark_node;
16624
16625   /* Any name names a type if we're following the `typename' keyword
16626      in a qualified name where the enclosing scope is type-dependent.  */
16627   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16628                 && dependent_type_p (scope));
16629   /* Handle the common case (an identifier, but not a template-id)
16630      efficiently.  */
16631   if (token->type == CPP_NAME
16632       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16633     {
16634       cp_token *identifier_token;
16635       bool ambiguous_p;
16636
16637       /* Look for the identifier.  */
16638       identifier_token = cp_lexer_peek_token (parser->lexer);
16639       ambiguous_p = identifier_token->ambiguous_p;
16640       identifier = cp_parser_identifier (parser);
16641       /* If the next token isn't an identifier, we are certainly not
16642          looking at a class-name.  */
16643       if (identifier == error_mark_node)
16644         decl = error_mark_node;
16645       /* If we know this is a type-name, there's no need to look it
16646          up.  */
16647       else if (typename_p)
16648         decl = identifier;
16649       else
16650         {
16651           tree ambiguous_decls;
16652           /* If we already know that this lookup is ambiguous, then
16653              we've already issued an error message; there's no reason
16654              to check again.  */
16655           if (ambiguous_p)
16656             {
16657               cp_parser_simulate_error (parser);
16658               return error_mark_node;
16659             }
16660           /* If the next token is a `::', then the name must be a type
16661              name.
16662
16663              [basic.lookup.qual]
16664
16665              During the lookup for a name preceding the :: scope
16666              resolution operator, object, function, and enumerator
16667              names are ignored.  */
16668           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16669             tag_type = typename_type;
16670           /* Look up the name.  */
16671           decl = cp_parser_lookup_name (parser, identifier,
16672                                         tag_type,
16673                                         /*is_template=*/false,
16674                                         /*is_namespace=*/false,
16675                                         check_dependency_p,
16676                                         &ambiguous_decls,
16677                                         identifier_token->location);
16678           if (ambiguous_decls)
16679             {
16680               if (cp_parser_parsing_tentatively (parser))
16681                 cp_parser_simulate_error (parser);
16682               return error_mark_node;
16683             }
16684         }
16685     }
16686   else
16687     {
16688       /* Try a template-id.  */
16689       decl = cp_parser_template_id (parser, template_keyword_p,
16690                                     check_dependency_p,
16691                                     is_declaration);
16692       if (decl == error_mark_node)
16693         return error_mark_node;
16694     }
16695
16696   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16697
16698   /* If this is a typename, create a TYPENAME_TYPE.  */
16699   if (typename_p && decl != error_mark_node)
16700     {
16701       decl = make_typename_type (scope, decl, typename_type,
16702                                  /*complain=*/tf_error);
16703       if (decl != error_mark_node)
16704         decl = TYPE_NAME (decl);
16705     }
16706
16707   /* Check to see that it is really the name of a class.  */
16708   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16709       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16710       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16711     /* Situations like this:
16712
16713          template <typename T> struct A {
16714            typename T::template X<int>::I i;
16715          };
16716
16717        are problematic.  Is `T::template X<int>' a class-name?  The
16718        standard does not seem to be definitive, but there is no other
16719        valid interpretation of the following `::'.  Therefore, those
16720        names are considered class-names.  */
16721     {
16722       decl = make_typename_type (scope, decl, tag_type, tf_error);
16723       if (decl != error_mark_node)
16724         decl = TYPE_NAME (decl);
16725     }
16726   else if (TREE_CODE (decl) != TYPE_DECL
16727            || TREE_TYPE (decl) == error_mark_node
16728            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16729            /* In Objective-C 2.0, a classname followed by '.' starts a
16730               dot-syntax expression, and it's not a type-name.  */
16731            || (c_dialect_objc ()
16732                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16733                && objc_is_class_name (decl)))
16734     decl = error_mark_node;
16735
16736   if (decl == error_mark_node)
16737     cp_parser_error (parser, "expected class-name");
16738   else if (identifier && !parser->scope)
16739     maybe_note_name_used_in_class (identifier, decl);
16740
16741   return decl;
16742 }
16743
16744 /* Parse a class-specifier.
16745
16746    class-specifier:
16747      class-head { member-specification [opt] }
16748
16749    Returns the TREE_TYPE representing the class.  */
16750
16751 static tree
16752 cp_parser_class_specifier_1 (cp_parser* parser)
16753 {
16754   tree type;
16755   tree attributes = NULL_TREE;
16756   bool nested_name_specifier_p;
16757   unsigned saved_num_template_parameter_lists;
16758   bool saved_in_function_body;
16759   bool saved_in_unbraced_linkage_specification_p;
16760   tree old_scope = NULL_TREE;
16761   tree scope = NULL_TREE;
16762   tree bases;
16763   cp_token *closing_brace;
16764
16765   push_deferring_access_checks (dk_no_deferred);
16766
16767   /* Parse the class-head.  */
16768   type = cp_parser_class_head (parser,
16769                                &nested_name_specifier_p,
16770                                &attributes,
16771                                &bases);
16772   /* If the class-head was a semantic disaster, skip the entire body
16773      of the class.  */
16774   if (!type)
16775     {
16776       cp_parser_skip_to_end_of_block_or_statement (parser);
16777       pop_deferring_access_checks ();
16778       return error_mark_node;
16779     }
16780
16781   /* Look for the `{'.  */
16782   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16783     {
16784       pop_deferring_access_checks ();
16785       return error_mark_node;
16786     }
16787
16788   /* Process the base classes. If they're invalid, skip the 
16789      entire class body.  */
16790   if (!xref_basetypes (type, bases))
16791     {
16792       /* Consuming the closing brace yields better error messages
16793          later on.  */
16794       if (cp_parser_skip_to_closing_brace (parser))
16795         cp_lexer_consume_token (parser->lexer);
16796       pop_deferring_access_checks ();
16797       return error_mark_node;
16798     }
16799
16800   /* Issue an error message if type-definitions are forbidden here.  */
16801   cp_parser_check_type_definition (parser);
16802   /* Remember that we are defining one more class.  */
16803   ++parser->num_classes_being_defined;
16804   /* Inside the class, surrounding template-parameter-lists do not
16805      apply.  */
16806   saved_num_template_parameter_lists
16807     = parser->num_template_parameter_lists;
16808   parser->num_template_parameter_lists = 0;
16809   /* We are not in a function body.  */
16810   saved_in_function_body = parser->in_function_body;
16811   parser->in_function_body = false;
16812   /* We are not immediately inside an extern "lang" block.  */
16813   saved_in_unbraced_linkage_specification_p
16814     = parser->in_unbraced_linkage_specification_p;
16815   parser->in_unbraced_linkage_specification_p = false;
16816
16817   /* Start the class.  */
16818   if (nested_name_specifier_p)
16819     {
16820       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16821       old_scope = push_inner_scope (scope);
16822     }
16823   type = begin_class_definition (type, attributes);
16824
16825   if (type == error_mark_node)
16826     /* If the type is erroneous, skip the entire body of the class.  */
16827     cp_parser_skip_to_closing_brace (parser);
16828   else
16829     /* Parse the member-specification.  */
16830     cp_parser_member_specification_opt (parser);
16831
16832   /* Look for the trailing `}'.  */
16833   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16834   /* Look for trailing attributes to apply to this class.  */
16835   if (cp_parser_allow_gnu_extensions_p (parser))
16836     attributes = cp_parser_attributes_opt (parser);
16837   if (type != error_mark_node)
16838     type = finish_struct (type, attributes);
16839   if (nested_name_specifier_p)
16840     pop_inner_scope (old_scope, scope);
16841
16842   /* We've finished a type definition.  Check for the common syntax
16843      error of forgetting a semicolon after the definition.  We need to
16844      be careful, as we can't just check for not-a-semicolon and be done
16845      with it; the user might have typed:
16846
16847      class X { } c = ...;
16848      class X { } *p = ...;
16849
16850      and so forth.  Instead, enumerate all the possible tokens that
16851      might follow this production; if we don't see one of them, then
16852      complain and silently insert the semicolon.  */
16853   {
16854     cp_token *token = cp_lexer_peek_token (parser->lexer);
16855     bool want_semicolon = true;
16856
16857     switch (token->type)
16858       {
16859       case CPP_NAME:
16860       case CPP_SEMICOLON:
16861       case CPP_MULT:
16862       case CPP_AND:
16863       case CPP_OPEN_PAREN:
16864       case CPP_CLOSE_PAREN:
16865       case CPP_COMMA:
16866         want_semicolon = false;
16867         break;
16868
16869         /* While it's legal for type qualifiers and storage class
16870            specifiers to follow type definitions in the grammar, only
16871            compiler testsuites contain code like that.  Assume that if
16872            we see such code, then what we're really seeing is a case
16873            like:
16874
16875            class X { }
16876            const <type> var = ...;
16877
16878            or
16879
16880            class Y { }
16881            static <type> func (...) ...
16882
16883            i.e. the qualifier or specifier applies to the next
16884            declaration.  To do so, however, we need to look ahead one
16885            more token to see if *that* token is a type specifier.
16886
16887            This code could be improved to handle:
16888
16889            class Z { }
16890            static const <type> var = ...;  */
16891       case CPP_KEYWORD:
16892         if (keyword_is_decl_specifier (token->keyword))
16893           {
16894             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16895
16896             /* Handling user-defined types here would be nice, but very
16897                tricky.  */
16898             want_semicolon
16899               = (lookahead->type == CPP_KEYWORD
16900                  && keyword_begins_type_specifier (lookahead->keyword));
16901           }
16902         break;
16903       default:
16904         break;
16905       }
16906
16907     /* If we don't have a type, then something is very wrong and we
16908        shouldn't try to do anything clever.  Likewise for not seeing the
16909        closing brace.  */
16910     if (closing_brace && TYPE_P (type) && want_semicolon)
16911       {
16912         cp_token_position prev
16913           = cp_lexer_previous_token_position (parser->lexer);
16914         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16915         location_t loc = prev_token->location;
16916
16917         if (CLASSTYPE_DECLARED_CLASS (type))
16918           error_at (loc, "expected %<;%> after class definition");
16919         else if (TREE_CODE (type) == RECORD_TYPE)
16920           error_at (loc, "expected %<;%> after struct definition");
16921         else if (TREE_CODE (type) == UNION_TYPE)
16922           error_at (loc, "expected %<;%> after union definition");
16923         else
16924           gcc_unreachable ();
16925
16926         /* Unget one token and smash it to look as though we encountered
16927            a semicolon in the input stream.  */
16928         cp_lexer_set_token_position (parser->lexer, prev);
16929         token = cp_lexer_peek_token (parser->lexer);
16930         token->type = CPP_SEMICOLON;
16931         token->keyword = RID_MAX;
16932       }
16933   }
16934
16935   /* If this class is not itself within the scope of another class,
16936      then we need to parse the bodies of all of the queued function
16937      definitions.  Note that the queued functions defined in a class
16938      are not always processed immediately following the
16939      class-specifier for that class.  Consider:
16940
16941        struct A {
16942          struct B { void f() { sizeof (A); } };
16943        };
16944
16945      If `f' were processed before the processing of `A' were
16946      completed, there would be no way to compute the size of `A'.
16947      Note that the nesting we are interested in here is lexical --
16948      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16949      for:
16950
16951        struct A { struct B; };
16952        struct A::B { void f() { } };
16953
16954      there is no need to delay the parsing of `A::B::f'.  */
16955   if (--parser->num_classes_being_defined == 0)
16956     {
16957       tree fn;
16958       tree class_type = NULL_TREE;
16959       tree pushed_scope = NULL_TREE;
16960       unsigned ix;
16961       cp_default_arg_entry *e;
16962
16963       /* In a first pass, parse default arguments to the functions.
16964          Then, in a second pass, parse the bodies of the functions.
16965          This two-phased approach handles cases like:
16966
16967             struct S {
16968               void f() { g(); }
16969               void g(int i = 3);
16970             };
16971
16972          */
16973       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16974                         ix, e)
16975         {
16976           fn = e->decl;
16977           /* If there are default arguments that have not yet been processed,
16978              take care of them now.  */
16979           if (class_type != e->class_type)
16980             {
16981               if (pushed_scope)
16982                 pop_scope (pushed_scope);
16983               class_type = e->class_type;
16984               pushed_scope = push_scope (class_type);
16985             }
16986           /* Make sure that any template parameters are in scope.  */
16987           maybe_begin_member_template_processing (fn);
16988           /* Parse the default argument expressions.  */
16989           cp_parser_late_parsing_default_args (parser, fn);
16990           /* Remove any template parameters from the symbol table.  */
16991           maybe_end_member_template_processing ();
16992         }
16993       if (pushed_scope)
16994         pop_scope (pushed_scope);
16995       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16996       /* Now parse the body of the functions.  */
16997       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16998         cp_parser_late_parsing_for_member (parser, fn);
16999       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17000     }
17001
17002   /* Put back any saved access checks.  */
17003   pop_deferring_access_checks ();
17004
17005   /* Restore saved state.  */
17006   parser->in_function_body = saved_in_function_body;
17007   parser->num_template_parameter_lists
17008     = saved_num_template_parameter_lists;
17009   parser->in_unbraced_linkage_specification_p
17010     = saved_in_unbraced_linkage_specification_p;
17011
17012   return type;
17013 }
17014
17015 static tree
17016 cp_parser_class_specifier (cp_parser* parser)
17017 {
17018   tree ret;
17019   timevar_push (TV_PARSE_STRUCT);
17020   ret = cp_parser_class_specifier_1 (parser);
17021   timevar_pop (TV_PARSE_STRUCT);
17022   return ret;
17023 }
17024
17025 /* Parse a class-head.
17026
17027    class-head:
17028      class-key identifier [opt] base-clause [opt]
17029      class-key nested-name-specifier identifier base-clause [opt]
17030      class-key nested-name-specifier [opt] template-id
17031        base-clause [opt]
17032
17033    GNU Extensions:
17034      class-key attributes identifier [opt] base-clause [opt]
17035      class-key attributes nested-name-specifier identifier base-clause [opt]
17036      class-key attributes nested-name-specifier [opt] template-id
17037        base-clause [opt]
17038
17039    Upon return BASES is initialized to the list of base classes (or
17040    NULL, if there are none) in the same form returned by
17041    cp_parser_base_clause.
17042
17043    Returns the TYPE of the indicated class.  Sets
17044    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17045    involving a nested-name-specifier was used, and FALSE otherwise.
17046
17047    Returns error_mark_node if this is not a class-head.
17048
17049    Returns NULL_TREE if the class-head is syntactically valid, but
17050    semantically invalid in a way that means we should skip the entire
17051    body of the class.  */
17052
17053 static tree
17054 cp_parser_class_head (cp_parser* parser,
17055                       bool* nested_name_specifier_p,
17056                       tree *attributes_p,
17057                       tree *bases)
17058 {
17059   tree nested_name_specifier;
17060   enum tag_types class_key;
17061   tree id = NULL_TREE;
17062   tree type = NULL_TREE;
17063   tree attributes;
17064   bool template_id_p = false;
17065   bool qualified_p = false;
17066   bool invalid_nested_name_p = false;
17067   bool invalid_explicit_specialization_p = false;
17068   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17069   tree pushed_scope = NULL_TREE;
17070   unsigned num_templates;
17071   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17072   /* Assume no nested-name-specifier will be present.  */
17073   *nested_name_specifier_p = false;
17074   /* Assume no template parameter lists will be used in defining the
17075      type.  */
17076   num_templates = 0;
17077   parser->colon_corrects_to_scope_p = false;
17078
17079   *bases = NULL_TREE;
17080
17081   /* Look for the class-key.  */
17082   class_key = cp_parser_class_key (parser);
17083   if (class_key == none_type)
17084     return error_mark_node;
17085
17086   /* Parse the attributes.  */
17087   attributes = cp_parser_attributes_opt (parser);
17088
17089   /* If the next token is `::', that is invalid -- but sometimes
17090      people do try to write:
17091
17092        struct ::S {};
17093
17094      Handle this gracefully by accepting the extra qualifier, and then
17095      issuing an error about it later if this really is a
17096      class-head.  If it turns out just to be an elaborated type
17097      specifier, remain silent.  */
17098   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17099     qualified_p = true;
17100
17101   push_deferring_access_checks (dk_no_check);
17102
17103   /* Determine the name of the class.  Begin by looking for an
17104      optional nested-name-specifier.  */
17105   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17106   nested_name_specifier
17107     = cp_parser_nested_name_specifier_opt (parser,
17108                                            /*typename_keyword_p=*/false,
17109                                            /*check_dependency_p=*/false,
17110                                            /*type_p=*/false,
17111                                            /*is_declaration=*/false);
17112   /* If there was a nested-name-specifier, then there *must* be an
17113      identifier.  */
17114   if (nested_name_specifier)
17115     {
17116       type_start_token = cp_lexer_peek_token (parser->lexer);
17117       /* Although the grammar says `identifier', it really means
17118          `class-name' or `template-name'.  You are only allowed to
17119          define a class that has already been declared with this
17120          syntax.
17121
17122          The proposed resolution for Core Issue 180 says that wherever
17123          you see `class T::X' you should treat `X' as a type-name.
17124
17125          It is OK to define an inaccessible class; for example:
17126
17127            class A { class B; };
17128            class A::B {};
17129
17130          We do not know if we will see a class-name, or a
17131          template-name.  We look for a class-name first, in case the
17132          class-name is a template-id; if we looked for the
17133          template-name first we would stop after the template-name.  */
17134       cp_parser_parse_tentatively (parser);
17135       type = cp_parser_class_name (parser,
17136                                    /*typename_keyword_p=*/false,
17137                                    /*template_keyword_p=*/false,
17138                                    class_type,
17139                                    /*check_dependency_p=*/false,
17140                                    /*class_head_p=*/true,
17141                                    /*is_declaration=*/false);
17142       /* If that didn't work, ignore the nested-name-specifier.  */
17143       if (!cp_parser_parse_definitely (parser))
17144         {
17145           invalid_nested_name_p = true;
17146           type_start_token = cp_lexer_peek_token (parser->lexer);
17147           id = cp_parser_identifier (parser);
17148           if (id == error_mark_node)
17149             id = NULL_TREE;
17150         }
17151       /* If we could not find a corresponding TYPE, treat this
17152          declaration like an unqualified declaration.  */
17153       if (type == error_mark_node)
17154         nested_name_specifier = NULL_TREE;
17155       /* Otherwise, count the number of templates used in TYPE and its
17156          containing scopes.  */
17157       else
17158         {
17159           tree scope;
17160
17161           for (scope = TREE_TYPE (type);
17162                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17163                scope = (TYPE_P (scope)
17164                         ? TYPE_CONTEXT (scope)
17165                         : DECL_CONTEXT (scope)))
17166             if (TYPE_P (scope)
17167                 && CLASS_TYPE_P (scope)
17168                 && CLASSTYPE_TEMPLATE_INFO (scope)
17169                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17170                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17171               ++num_templates;
17172         }
17173     }
17174   /* Otherwise, the identifier is optional.  */
17175   else
17176     {
17177       /* We don't know whether what comes next is a template-id,
17178          an identifier, or nothing at all.  */
17179       cp_parser_parse_tentatively (parser);
17180       /* Check for a template-id.  */
17181       type_start_token = cp_lexer_peek_token (parser->lexer);
17182       id = cp_parser_template_id (parser,
17183                                   /*template_keyword_p=*/false,
17184                                   /*check_dependency_p=*/true,
17185                                   /*is_declaration=*/true);
17186       /* If that didn't work, it could still be an identifier.  */
17187       if (!cp_parser_parse_definitely (parser))
17188         {
17189           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17190             {
17191               type_start_token = cp_lexer_peek_token (parser->lexer);
17192               id = cp_parser_identifier (parser);
17193             }
17194           else
17195             id = NULL_TREE;
17196         }
17197       else
17198         {
17199           template_id_p = true;
17200           ++num_templates;
17201         }
17202     }
17203
17204   pop_deferring_access_checks ();
17205
17206   if (id)
17207     cp_parser_check_for_invalid_template_id (parser, id,
17208                                              type_start_token->location);
17209
17210   /* If it's not a `:' or a `{' then we can't really be looking at a
17211      class-head, since a class-head only appears as part of a
17212      class-specifier.  We have to detect this situation before calling
17213      xref_tag, since that has irreversible side-effects.  */
17214   if (!cp_parser_next_token_starts_class_definition_p (parser))
17215     {
17216       cp_parser_error (parser, "expected %<{%> or %<:%>");
17217       type = error_mark_node;
17218       goto out;
17219     }
17220
17221   /* At this point, we're going ahead with the class-specifier, even
17222      if some other problem occurs.  */
17223   cp_parser_commit_to_tentative_parse (parser);
17224   /* Issue the error about the overly-qualified name now.  */
17225   if (qualified_p)
17226     {
17227       cp_parser_error (parser,
17228                        "global qualification of class name is invalid");
17229       type = error_mark_node;
17230       goto out;
17231     }
17232   else if (invalid_nested_name_p)
17233     {
17234       cp_parser_error (parser,
17235                        "qualified name does not name a class");
17236       type = error_mark_node;
17237       goto out;
17238     }
17239   else if (nested_name_specifier)
17240     {
17241       tree scope;
17242
17243       /* Reject typedef-names in class heads.  */
17244       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17245         {
17246           error_at (type_start_token->location,
17247                     "invalid class name in declaration of %qD",
17248                     type);
17249           type = NULL_TREE;
17250           goto done;
17251         }
17252
17253       /* Figure out in what scope the declaration is being placed.  */
17254       scope = current_scope ();
17255       /* If that scope does not contain the scope in which the
17256          class was originally declared, the program is invalid.  */
17257       if (scope && !is_ancestor (scope, nested_name_specifier))
17258         {
17259           if (at_namespace_scope_p ())
17260             error_at (type_start_token->location,
17261                       "declaration of %qD in namespace %qD which does not "
17262                       "enclose %qD",
17263                       type, scope, nested_name_specifier);
17264           else
17265             error_at (type_start_token->location,
17266                       "declaration of %qD in %qD which does not enclose %qD",
17267                       type, scope, nested_name_specifier);
17268           type = NULL_TREE;
17269           goto done;
17270         }
17271       /* [dcl.meaning]
17272
17273          A declarator-id shall not be qualified except for the
17274          definition of a ... nested class outside of its class
17275          ... [or] the definition or explicit instantiation of a
17276          class member of a namespace outside of its namespace.  */
17277       if (scope == nested_name_specifier)
17278         {
17279           permerror (nested_name_specifier_token_start->location,
17280                      "extra qualification not allowed");
17281           nested_name_specifier = NULL_TREE;
17282           num_templates = 0;
17283         }
17284     }
17285   /* An explicit-specialization must be preceded by "template <>".  If
17286      it is not, try to recover gracefully.  */
17287   if (at_namespace_scope_p ()
17288       && parser->num_template_parameter_lists == 0
17289       && template_id_p)
17290     {
17291       error_at (type_start_token->location,
17292                 "an explicit specialization must be preceded by %<template <>%>");
17293       invalid_explicit_specialization_p = true;
17294       /* Take the same action that would have been taken by
17295          cp_parser_explicit_specialization.  */
17296       ++parser->num_template_parameter_lists;
17297       begin_specialization ();
17298     }
17299   /* There must be no "return" statements between this point and the
17300      end of this function; set "type "to the correct return value and
17301      use "goto done;" to return.  */
17302   /* Make sure that the right number of template parameters were
17303      present.  */
17304   if (!cp_parser_check_template_parameters (parser, num_templates,
17305                                             type_start_token->location,
17306                                             /*declarator=*/NULL))
17307     {
17308       /* If something went wrong, there is no point in even trying to
17309          process the class-definition.  */
17310       type = NULL_TREE;
17311       goto done;
17312     }
17313
17314   /* Look up the type.  */
17315   if (template_id_p)
17316     {
17317       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17318           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17319               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17320         {
17321           error_at (type_start_token->location,
17322                     "function template %qD redeclared as a class template", id);
17323           type = error_mark_node;
17324         }
17325       else
17326         {
17327           type = TREE_TYPE (id);
17328           type = maybe_process_partial_specialization (type);
17329         }
17330       if (nested_name_specifier)
17331         pushed_scope = push_scope (nested_name_specifier);
17332     }
17333   else if (nested_name_specifier)
17334     {
17335       tree class_type;
17336
17337       /* Given:
17338
17339             template <typename T> struct S { struct T };
17340             template <typename T> struct S<T>::T { };
17341
17342          we will get a TYPENAME_TYPE when processing the definition of
17343          `S::T'.  We need to resolve it to the actual type before we
17344          try to define it.  */
17345       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17346         {
17347           class_type = resolve_typename_type (TREE_TYPE (type),
17348                                               /*only_current_p=*/false);
17349           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17350             type = TYPE_NAME (class_type);
17351           else
17352             {
17353               cp_parser_error (parser, "could not resolve typename type");
17354               type = error_mark_node;
17355             }
17356         }
17357
17358       if (maybe_process_partial_specialization (TREE_TYPE (type))
17359           == error_mark_node)
17360         {
17361           type = NULL_TREE;
17362           goto done;
17363         }
17364
17365       class_type = current_class_type;
17366       /* Enter the scope indicated by the nested-name-specifier.  */
17367       pushed_scope = push_scope (nested_name_specifier);
17368       /* Get the canonical version of this type.  */
17369       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17370       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17371           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17372         {
17373           type = push_template_decl (type);
17374           if (type == error_mark_node)
17375             {
17376               type = NULL_TREE;
17377               goto done;
17378             }
17379         }
17380
17381       type = TREE_TYPE (type);
17382       *nested_name_specifier_p = true;
17383     }
17384   else      /* The name is not a nested name.  */
17385     {
17386       /* If the class was unnamed, create a dummy name.  */
17387       if (!id)
17388         id = make_anon_name ();
17389       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17390                        parser->num_template_parameter_lists);
17391     }
17392
17393   /* Indicate whether this class was declared as a `class' or as a
17394      `struct'.  */
17395   if (TREE_CODE (type) == RECORD_TYPE)
17396     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17397   cp_parser_check_class_key (class_key, type);
17398
17399   /* If this type was already complete, and we see another definition,
17400      that's an error.  */
17401   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17402     {
17403       error_at (type_start_token->location, "redefinition of %q#T",
17404                 type);
17405       error_at (type_start_token->location, "previous definition of %q+#T",
17406                 type);
17407       type = NULL_TREE;
17408       goto done;
17409     }
17410   else if (type == error_mark_node)
17411     type = NULL_TREE;
17412
17413   /* We will have entered the scope containing the class; the names of
17414      base classes should be looked up in that context.  For example:
17415
17416        struct A { struct B {}; struct C; };
17417        struct A::C : B {};
17418
17419      is valid.  */
17420
17421   /* Get the list of base-classes, if there is one.  */
17422   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17423     *bases = cp_parser_base_clause (parser);
17424
17425  done:
17426   /* Leave the scope given by the nested-name-specifier.  We will
17427      enter the class scope itself while processing the members.  */
17428   if (pushed_scope)
17429     pop_scope (pushed_scope);
17430
17431   if (invalid_explicit_specialization_p)
17432     {
17433       end_specialization ();
17434       --parser->num_template_parameter_lists;
17435     }
17436
17437   if (type)
17438     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17439   *attributes_p = attributes;
17440  out:
17441   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17442   return type;
17443 }
17444
17445 /* Parse a class-key.
17446
17447    class-key:
17448      class
17449      struct
17450      union
17451
17452    Returns the kind of class-key specified, or none_type to indicate
17453    error.  */
17454
17455 static enum tag_types
17456 cp_parser_class_key (cp_parser* parser)
17457 {
17458   cp_token *token;
17459   enum tag_types tag_type;
17460
17461   /* Look for the class-key.  */
17462   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17463   if (!token)
17464     return none_type;
17465
17466   /* Check to see if the TOKEN is a class-key.  */
17467   tag_type = cp_parser_token_is_class_key (token);
17468   if (!tag_type)
17469     cp_parser_error (parser, "expected class-key");
17470   return tag_type;
17471 }
17472
17473 /* Parse an (optional) member-specification.
17474
17475    member-specification:
17476      member-declaration member-specification [opt]
17477      access-specifier : member-specification [opt]  */
17478
17479 static void
17480 cp_parser_member_specification_opt (cp_parser* parser)
17481 {
17482   while (true)
17483     {
17484       cp_token *token;
17485       enum rid keyword;
17486
17487       /* Peek at the next token.  */
17488       token = cp_lexer_peek_token (parser->lexer);
17489       /* If it's a `}', or EOF then we've seen all the members.  */
17490       if (token->type == CPP_CLOSE_BRACE
17491           || token->type == CPP_EOF
17492           || token->type == CPP_PRAGMA_EOL)
17493         break;
17494
17495       /* See if this token is a keyword.  */
17496       keyword = token->keyword;
17497       switch (keyword)
17498         {
17499         case RID_PUBLIC:
17500         case RID_PROTECTED:
17501         case RID_PRIVATE:
17502           /* Consume the access-specifier.  */
17503           cp_lexer_consume_token (parser->lexer);
17504           /* Remember which access-specifier is active.  */
17505           current_access_specifier = token->u.value;
17506           /* Look for the `:'.  */
17507           cp_parser_require (parser, CPP_COLON, RT_COLON);
17508           break;
17509
17510         default:
17511           /* Accept #pragmas at class scope.  */
17512           if (token->type == CPP_PRAGMA)
17513             {
17514               cp_parser_pragma (parser, pragma_external);
17515               break;
17516             }
17517
17518           /* Otherwise, the next construction must be a
17519              member-declaration.  */
17520           cp_parser_member_declaration (parser);
17521         }
17522     }
17523 }
17524
17525 /* Parse a member-declaration.
17526
17527    member-declaration:
17528      decl-specifier-seq [opt] member-declarator-list [opt] ;
17529      function-definition ; [opt]
17530      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17531      using-declaration
17532      template-declaration
17533
17534    member-declarator-list:
17535      member-declarator
17536      member-declarator-list , member-declarator
17537
17538    member-declarator:
17539      declarator pure-specifier [opt]
17540      declarator constant-initializer [opt]
17541      identifier [opt] : constant-expression
17542
17543    GNU Extensions:
17544
17545    member-declaration:
17546      __extension__ member-declaration
17547
17548    member-declarator:
17549      declarator attributes [opt] pure-specifier [opt]
17550      declarator attributes [opt] constant-initializer [opt]
17551      identifier [opt] attributes [opt] : constant-expression  
17552
17553    C++0x Extensions:
17554
17555    member-declaration:
17556      static_assert-declaration  */
17557
17558 static void
17559 cp_parser_member_declaration (cp_parser* parser)
17560 {
17561   cp_decl_specifier_seq decl_specifiers;
17562   tree prefix_attributes;
17563   tree decl;
17564   int declares_class_or_enum;
17565   bool friend_p;
17566   cp_token *token = NULL;
17567   cp_token *decl_spec_token_start = NULL;
17568   cp_token *initializer_token_start = NULL;
17569   int saved_pedantic;
17570   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17571
17572   /* Check for the `__extension__' keyword.  */
17573   if (cp_parser_extension_opt (parser, &saved_pedantic))
17574     {
17575       /* Recurse.  */
17576       cp_parser_member_declaration (parser);
17577       /* Restore the old value of the PEDANTIC flag.  */
17578       pedantic = saved_pedantic;
17579
17580       return;
17581     }
17582
17583   /* Check for a template-declaration.  */
17584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17585     {
17586       /* An explicit specialization here is an error condition, and we
17587          expect the specialization handler to detect and report this.  */
17588       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17589           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17590         cp_parser_explicit_specialization (parser);
17591       else
17592         cp_parser_template_declaration (parser, /*member_p=*/true);
17593
17594       return;
17595     }
17596
17597   /* Check for a using-declaration.  */
17598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17599     {
17600       /* Parse the using-declaration.  */
17601       cp_parser_using_declaration (parser,
17602                                    /*access_declaration_p=*/false);
17603       return;
17604     }
17605
17606   /* Check for @defs.  */
17607   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17608     {
17609       tree ivar, member;
17610       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17611       ivar = ivar_chains;
17612       while (ivar)
17613         {
17614           member = ivar;
17615           ivar = TREE_CHAIN (member);
17616           TREE_CHAIN (member) = NULL_TREE;
17617           finish_member_declaration (member);
17618         }
17619       return;
17620     }
17621
17622   /* If the next token is `static_assert' we have a static assertion.  */
17623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17624     {
17625       cp_parser_static_assert (parser, /*member_p=*/true);
17626       return;
17627     }
17628
17629   parser->colon_corrects_to_scope_p = false;
17630
17631   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17632     goto out;
17633
17634   /* Parse the decl-specifier-seq.  */
17635   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17636   cp_parser_decl_specifier_seq (parser,
17637                                 CP_PARSER_FLAGS_OPTIONAL,
17638                                 &decl_specifiers,
17639                                 &declares_class_or_enum);
17640   prefix_attributes = decl_specifiers.attributes;
17641   decl_specifiers.attributes = NULL_TREE;
17642   /* Check for an invalid type-name.  */
17643   if (!decl_specifiers.any_type_specifiers_p
17644       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17645     goto out;
17646   /* If there is no declarator, then the decl-specifier-seq should
17647      specify a type.  */
17648   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17649     {
17650       /* If there was no decl-specifier-seq, and the next token is a
17651          `;', then we have something like:
17652
17653            struct S { ; };
17654
17655          [class.mem]
17656
17657          Each member-declaration shall declare at least one member
17658          name of the class.  */
17659       if (!decl_specifiers.any_specifiers_p)
17660         {
17661           cp_token *token = cp_lexer_peek_token (parser->lexer);
17662           if (!in_system_header_at (token->location))
17663             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17664         }
17665       else
17666         {
17667           tree type;
17668
17669           /* See if this declaration is a friend.  */
17670           friend_p = cp_parser_friend_p (&decl_specifiers);
17671           /* If there were decl-specifiers, check to see if there was
17672              a class-declaration.  */
17673           type = check_tag_decl (&decl_specifiers);
17674           /* Nested classes have already been added to the class, but
17675              a `friend' needs to be explicitly registered.  */
17676           if (friend_p)
17677             {
17678               /* If the `friend' keyword was present, the friend must
17679                  be introduced with a class-key.  */
17680                if (!declares_class_or_enum)
17681                  error_at (decl_spec_token_start->location,
17682                            "a class-key must be used when declaring a friend");
17683                /* In this case:
17684
17685                     template <typename T> struct A {
17686                       friend struct A<T>::B;
17687                     };
17688
17689                   A<T>::B will be represented by a TYPENAME_TYPE, and
17690                   therefore not recognized by check_tag_decl.  */
17691                if (!type
17692                    && decl_specifiers.type
17693                    && TYPE_P (decl_specifiers.type))
17694                  type = decl_specifiers.type;
17695                if (!type || !TYPE_P (type))
17696                  error_at (decl_spec_token_start->location,
17697                            "friend declaration does not name a class or "
17698                            "function");
17699                else
17700                  make_friend_class (current_class_type, type,
17701                                     /*complain=*/true);
17702             }
17703           /* If there is no TYPE, an error message will already have
17704              been issued.  */
17705           else if (!type || type == error_mark_node)
17706             ;
17707           /* An anonymous aggregate has to be handled specially; such
17708              a declaration really declares a data member (with a
17709              particular type), as opposed to a nested class.  */
17710           else if (ANON_AGGR_TYPE_P (type))
17711             {
17712               /* Remove constructors and such from TYPE, now that we
17713                  know it is an anonymous aggregate.  */
17714               fixup_anonymous_aggr (type);
17715               /* And make the corresponding data member.  */
17716               decl = build_decl (decl_spec_token_start->location,
17717                                  FIELD_DECL, NULL_TREE, type);
17718               /* Add it to the class.  */
17719               finish_member_declaration (decl);
17720             }
17721           else
17722             cp_parser_check_access_in_redeclaration
17723                                               (TYPE_NAME (type),
17724                                                decl_spec_token_start->location);
17725         }
17726     }
17727   else
17728     {
17729       bool assume_semicolon = false;
17730
17731       /* See if these declarations will be friends.  */
17732       friend_p = cp_parser_friend_p (&decl_specifiers);
17733
17734       /* Keep going until we hit the `;' at the end of the
17735          declaration.  */
17736       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17737         {
17738           tree attributes = NULL_TREE;
17739           tree first_attribute;
17740
17741           /* Peek at the next token.  */
17742           token = cp_lexer_peek_token (parser->lexer);
17743
17744           /* Check for a bitfield declaration.  */
17745           if (token->type == CPP_COLON
17746               || (token->type == CPP_NAME
17747                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17748                   == CPP_COLON))
17749             {
17750               tree identifier;
17751               tree width;
17752
17753               /* Get the name of the bitfield.  Note that we cannot just
17754                  check TOKEN here because it may have been invalidated by
17755                  the call to cp_lexer_peek_nth_token above.  */
17756               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17757                 identifier = cp_parser_identifier (parser);
17758               else
17759                 identifier = NULL_TREE;
17760
17761               /* Consume the `:' token.  */
17762               cp_lexer_consume_token (parser->lexer);
17763               /* Get the width of the bitfield.  */
17764               width
17765                 = cp_parser_constant_expression (parser,
17766                                                  /*allow_non_constant=*/false,
17767                                                  NULL);
17768
17769               /* Look for attributes that apply to the bitfield.  */
17770               attributes = cp_parser_attributes_opt (parser);
17771               /* Remember which attributes are prefix attributes and
17772                  which are not.  */
17773               first_attribute = attributes;
17774               /* Combine the attributes.  */
17775               attributes = chainon (prefix_attributes, attributes);
17776
17777               /* Create the bitfield declaration.  */
17778               decl = grokbitfield (identifier
17779                                    ? make_id_declarator (NULL_TREE,
17780                                                          identifier,
17781                                                          sfk_none)
17782                                    : NULL,
17783                                    &decl_specifiers,
17784                                    width,
17785                                    attributes);
17786             }
17787           else
17788             {
17789               cp_declarator *declarator;
17790               tree initializer;
17791               tree asm_specification;
17792               int ctor_dtor_or_conv_p;
17793
17794               /* Parse the declarator.  */
17795               declarator
17796                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17797                                         &ctor_dtor_or_conv_p,
17798                                         /*parenthesized_p=*/NULL,
17799                                         /*member_p=*/true);
17800
17801               /* If something went wrong parsing the declarator, make sure
17802                  that we at least consume some tokens.  */
17803               if (declarator == cp_error_declarator)
17804                 {
17805                   /* Skip to the end of the statement.  */
17806                   cp_parser_skip_to_end_of_statement (parser);
17807                   /* If the next token is not a semicolon, that is
17808                      probably because we just skipped over the body of
17809                      a function.  So, we consume a semicolon if
17810                      present, but do not issue an error message if it
17811                      is not present.  */
17812                   if (cp_lexer_next_token_is (parser->lexer,
17813                                               CPP_SEMICOLON))
17814                     cp_lexer_consume_token (parser->lexer);
17815                   goto out;
17816                 }
17817
17818               if (declares_class_or_enum & 2)
17819                 cp_parser_check_for_definition_in_return_type
17820                                             (declarator, decl_specifiers.type,
17821                                              decl_specifiers.type_location);
17822
17823               /* Look for an asm-specification.  */
17824               asm_specification = cp_parser_asm_specification_opt (parser);
17825               /* Look for attributes that apply to the declaration.  */
17826               attributes = cp_parser_attributes_opt (parser);
17827               /* Remember which attributes are prefix attributes and
17828                  which are not.  */
17829               first_attribute = attributes;
17830               /* Combine the attributes.  */
17831               attributes = chainon (prefix_attributes, attributes);
17832
17833               /* If it's an `=', then we have a constant-initializer or a
17834                  pure-specifier.  It is not correct to parse the
17835                  initializer before registering the member declaration
17836                  since the member declaration should be in scope while
17837                  its initializer is processed.  However, the rest of the
17838                  front end does not yet provide an interface that allows
17839                  us to handle this correctly.  */
17840               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17841                 {
17842                   /* In [class.mem]:
17843
17844                      A pure-specifier shall be used only in the declaration of
17845                      a virtual function.
17846
17847                      A member-declarator can contain a constant-initializer
17848                      only if it declares a static member of integral or
17849                      enumeration type.
17850
17851                      Therefore, if the DECLARATOR is for a function, we look
17852                      for a pure-specifier; otherwise, we look for a
17853                      constant-initializer.  When we call `grokfield', it will
17854                      perform more stringent semantics checks.  */
17855                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17856                   if (function_declarator_p (declarator))
17857                     initializer = cp_parser_pure_specifier (parser);
17858                   else
17859                     /* Parse the initializer.  */
17860                     initializer = cp_parser_constant_initializer (parser);
17861                 }
17862               /* Otherwise, there is no initializer.  */
17863               else
17864                 initializer = NULL_TREE;
17865
17866               /* See if we are probably looking at a function
17867                  definition.  We are certainly not looking at a
17868                  member-declarator.  Calling `grokfield' has
17869                  side-effects, so we must not do it unless we are sure
17870                  that we are looking at a member-declarator.  */
17871               if (cp_parser_token_starts_function_definition_p
17872                   (cp_lexer_peek_token (parser->lexer)))
17873                 {
17874                   /* The grammar does not allow a pure-specifier to be
17875                      used when a member function is defined.  (It is
17876                      possible that this fact is an oversight in the
17877                      standard, since a pure function may be defined
17878                      outside of the class-specifier.  */
17879                   if (initializer)
17880                     error_at (initializer_token_start->location,
17881                               "pure-specifier on function-definition");
17882                   decl = cp_parser_save_member_function_body (parser,
17883                                                               &decl_specifiers,
17884                                                               declarator,
17885                                                               attributes);
17886                   /* If the member was not a friend, declare it here.  */
17887                   if (!friend_p)
17888                     finish_member_declaration (decl);
17889                   /* Peek at the next token.  */
17890                   token = cp_lexer_peek_token (parser->lexer);
17891                   /* If the next token is a semicolon, consume it.  */
17892                   if (token->type == CPP_SEMICOLON)
17893                     cp_lexer_consume_token (parser->lexer);
17894                   goto out;
17895                 }
17896               else
17897                 if (declarator->kind == cdk_function)
17898                   declarator->id_loc = token->location;
17899                 /* Create the declaration.  */
17900                 decl = grokfield (declarator, &decl_specifiers,
17901                                   initializer, /*init_const_expr_p=*/true,
17902                                   asm_specification,
17903                                   attributes);
17904             }
17905
17906           /* Reset PREFIX_ATTRIBUTES.  */
17907           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17908             attributes = TREE_CHAIN (attributes);
17909           if (attributes)
17910             TREE_CHAIN (attributes) = NULL_TREE;
17911
17912           /* If there is any qualification still in effect, clear it
17913              now; we will be starting fresh with the next declarator.  */
17914           parser->scope = NULL_TREE;
17915           parser->qualifying_scope = NULL_TREE;
17916           parser->object_scope = NULL_TREE;
17917           /* If it's a `,', then there are more declarators.  */
17918           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17919             cp_lexer_consume_token (parser->lexer);
17920           /* If the next token isn't a `;', then we have a parse error.  */
17921           else if (cp_lexer_next_token_is_not (parser->lexer,
17922                                                CPP_SEMICOLON))
17923             {
17924               /* The next token might be a ways away from where the
17925                  actual semicolon is missing.  Find the previous token
17926                  and use that for our error position.  */
17927               cp_token *token = cp_lexer_previous_token (parser->lexer);
17928               error_at (token->location,
17929                         "expected %<;%> at end of member declaration");
17930
17931               /* Assume that the user meant to provide a semicolon.  If
17932                  we were to cp_parser_skip_to_end_of_statement, we might
17933                  skip to a semicolon inside a member function definition
17934                  and issue nonsensical error messages.  */
17935               assume_semicolon = true;
17936             }
17937
17938           if (decl)
17939             {
17940               /* Add DECL to the list of members.  */
17941               if (!friend_p)
17942                 finish_member_declaration (decl);
17943
17944               if (TREE_CODE (decl) == FUNCTION_DECL)
17945                 cp_parser_save_default_args (parser, decl);
17946             }
17947
17948           if (assume_semicolon)
17949             goto out;
17950         }
17951     }
17952
17953   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17954  out:
17955   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17956 }
17957
17958 /* Parse a pure-specifier.
17959
17960    pure-specifier:
17961      = 0
17962
17963    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17964    Otherwise, ERROR_MARK_NODE is returned.  */
17965
17966 static tree
17967 cp_parser_pure_specifier (cp_parser* parser)
17968 {
17969   cp_token *token;
17970
17971   /* Look for the `=' token.  */
17972   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17973     return error_mark_node;
17974   /* Look for the `0' token.  */
17975   token = cp_lexer_peek_token (parser->lexer);
17976
17977   if (token->type == CPP_EOF
17978       || token->type == CPP_PRAGMA_EOL)
17979     return error_mark_node;
17980
17981   cp_lexer_consume_token (parser->lexer);
17982
17983   /* Accept = default or = delete in c++0x mode.  */
17984   if (token->keyword == RID_DEFAULT
17985       || token->keyword == RID_DELETE)
17986     {
17987       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17988       return token->u.value;
17989     }
17990
17991   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
17992   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17993     {
17994       cp_parser_error (parser,
17995                        "invalid pure specifier (only %<= 0%> is allowed)");
17996       cp_parser_skip_to_end_of_statement (parser);
17997       return error_mark_node;
17998     }
17999   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18000     {
18001       error_at (token->location, "templates may not be %<virtual%>");
18002       return error_mark_node;
18003     }
18004
18005   return integer_zero_node;
18006 }
18007
18008 /* Parse a constant-initializer.
18009
18010    constant-initializer:
18011      = constant-expression
18012
18013    Returns a representation of the constant-expression.  */
18014
18015 static tree
18016 cp_parser_constant_initializer (cp_parser* parser)
18017 {
18018   /* Look for the `=' token.  */
18019   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18020     return error_mark_node;
18021
18022   /* It is invalid to write:
18023
18024        struct S { static const int i = { 7 }; };
18025
18026      */
18027   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18028     {
18029       cp_parser_error (parser,
18030                        "a brace-enclosed initializer is not allowed here");
18031       /* Consume the opening brace.  */
18032       cp_lexer_consume_token (parser->lexer);
18033       /* Skip the initializer.  */
18034       cp_parser_skip_to_closing_brace (parser);
18035       /* Look for the trailing `}'.  */
18036       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18037
18038       return error_mark_node;
18039     }
18040
18041   return cp_parser_constant_expression (parser,
18042                                         /*allow_non_constant=*/false,
18043                                         NULL);
18044 }
18045
18046 /* Derived classes [gram.class.derived] */
18047
18048 /* Parse a base-clause.
18049
18050    base-clause:
18051      : base-specifier-list
18052
18053    base-specifier-list:
18054      base-specifier ... [opt]
18055      base-specifier-list , base-specifier ... [opt]
18056
18057    Returns a TREE_LIST representing the base-classes, in the order in
18058    which they were declared.  The representation of each node is as
18059    described by cp_parser_base_specifier.
18060
18061    In the case that no bases are specified, this function will return
18062    NULL_TREE, not ERROR_MARK_NODE.  */
18063
18064 static tree
18065 cp_parser_base_clause (cp_parser* parser)
18066 {
18067   tree bases = NULL_TREE;
18068
18069   /* Look for the `:' that begins the list.  */
18070   cp_parser_require (parser, CPP_COLON, RT_COLON);
18071
18072   /* Scan the base-specifier-list.  */
18073   while (true)
18074     {
18075       cp_token *token;
18076       tree base;
18077       bool pack_expansion_p = false;
18078
18079       /* Look for the base-specifier.  */
18080       base = cp_parser_base_specifier (parser);
18081       /* Look for the (optional) ellipsis. */
18082       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18083         {
18084           /* Consume the `...'. */
18085           cp_lexer_consume_token (parser->lexer);
18086
18087           pack_expansion_p = true;
18088         }
18089
18090       /* Add BASE to the front of the list.  */
18091       if (base != error_mark_node)
18092         {
18093           if (pack_expansion_p)
18094             /* Make this a pack expansion type. */
18095             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18096           
18097
18098           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18099             {
18100               TREE_CHAIN (base) = bases;
18101               bases = base;
18102             }
18103         }
18104       /* Peek at the next token.  */
18105       token = cp_lexer_peek_token (parser->lexer);
18106       /* If it's not a comma, then the list is complete.  */
18107       if (token->type != CPP_COMMA)
18108         break;
18109       /* Consume the `,'.  */
18110       cp_lexer_consume_token (parser->lexer);
18111     }
18112
18113   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18114      base class had a qualified name.  However, the next name that
18115      appears is certainly not qualified.  */
18116   parser->scope = NULL_TREE;
18117   parser->qualifying_scope = NULL_TREE;
18118   parser->object_scope = NULL_TREE;
18119
18120   return nreverse (bases);
18121 }
18122
18123 /* Parse a base-specifier.
18124
18125    base-specifier:
18126      :: [opt] nested-name-specifier [opt] class-name
18127      virtual access-specifier [opt] :: [opt] nested-name-specifier
18128        [opt] class-name
18129      access-specifier virtual [opt] :: [opt] nested-name-specifier
18130        [opt] class-name
18131
18132    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18133    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18134    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18135    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18136
18137 static tree
18138 cp_parser_base_specifier (cp_parser* parser)
18139 {
18140   cp_token *token;
18141   bool done = false;
18142   bool virtual_p = false;
18143   bool duplicate_virtual_error_issued_p = false;
18144   bool duplicate_access_error_issued_p = false;
18145   bool class_scope_p, template_p;
18146   tree access = access_default_node;
18147   tree type;
18148
18149   /* Process the optional `virtual' and `access-specifier'.  */
18150   while (!done)
18151     {
18152       /* Peek at the next token.  */
18153       token = cp_lexer_peek_token (parser->lexer);
18154       /* Process `virtual'.  */
18155       switch (token->keyword)
18156         {
18157         case RID_VIRTUAL:
18158           /* If `virtual' appears more than once, issue an error.  */
18159           if (virtual_p && !duplicate_virtual_error_issued_p)
18160             {
18161               cp_parser_error (parser,
18162                                "%<virtual%> specified more than once in base-specified");
18163               duplicate_virtual_error_issued_p = true;
18164             }
18165
18166           virtual_p = true;
18167
18168           /* Consume the `virtual' token.  */
18169           cp_lexer_consume_token (parser->lexer);
18170
18171           break;
18172
18173         case RID_PUBLIC:
18174         case RID_PROTECTED:
18175         case RID_PRIVATE:
18176           /* If more than one access specifier appears, issue an
18177              error.  */
18178           if (access != access_default_node
18179               && !duplicate_access_error_issued_p)
18180             {
18181               cp_parser_error (parser,
18182                                "more than one access specifier in base-specified");
18183               duplicate_access_error_issued_p = true;
18184             }
18185
18186           access = ridpointers[(int) token->keyword];
18187
18188           /* Consume the access-specifier.  */
18189           cp_lexer_consume_token (parser->lexer);
18190
18191           break;
18192
18193         default:
18194           done = true;
18195           break;
18196         }
18197     }
18198   /* It is not uncommon to see programs mechanically, erroneously, use
18199      the 'typename' keyword to denote (dependent) qualified types
18200      as base classes.  */
18201   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18202     {
18203       token = cp_lexer_peek_token (parser->lexer);
18204       if (!processing_template_decl)
18205         error_at (token->location,
18206                   "keyword %<typename%> not allowed outside of templates");
18207       else
18208         error_at (token->location,
18209                   "keyword %<typename%> not allowed in this context "
18210                   "(the base class is implicitly a type)");
18211       cp_lexer_consume_token (parser->lexer);
18212     }
18213
18214   /* Look for the optional `::' operator.  */
18215   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18216   /* Look for the nested-name-specifier.  The simplest way to
18217      implement:
18218
18219        [temp.res]
18220
18221        The keyword `typename' is not permitted in a base-specifier or
18222        mem-initializer; in these contexts a qualified name that
18223        depends on a template-parameter is implicitly assumed to be a
18224        type name.
18225
18226      is to pretend that we have seen the `typename' keyword at this
18227      point.  */
18228   cp_parser_nested_name_specifier_opt (parser,
18229                                        /*typename_keyword_p=*/true,
18230                                        /*check_dependency_p=*/true,
18231                                        typename_type,
18232                                        /*is_declaration=*/true);
18233   /* If the base class is given by a qualified name, assume that names
18234      we see are type names or templates, as appropriate.  */
18235   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18236   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18237
18238   /* Finally, look for the class-name.  */
18239   type = cp_parser_class_name (parser,
18240                                class_scope_p,
18241                                template_p,
18242                                typename_type,
18243                                /*check_dependency_p=*/true,
18244                                /*class_head_p=*/false,
18245                                /*is_declaration=*/true);
18246
18247   if (type == error_mark_node)
18248     return error_mark_node;
18249
18250   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18251 }
18252
18253 /* Exception handling [gram.exception] */
18254
18255 /* Parse an (optional) exception-specification.
18256
18257    exception-specification:
18258      throw ( type-id-list [opt] )
18259
18260    Returns a TREE_LIST representing the exception-specification.  The
18261    TREE_VALUE of each node is a type.  */
18262
18263 static tree
18264 cp_parser_exception_specification_opt (cp_parser* parser)
18265 {
18266   cp_token *token;
18267   tree type_id_list;
18268   const char *saved_message;
18269
18270   /* Peek at the next token.  */
18271   token = cp_lexer_peek_token (parser->lexer);
18272
18273   /* Is it a noexcept-specification?  */
18274   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18275     {
18276       tree expr;
18277       cp_lexer_consume_token (parser->lexer);
18278
18279       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18280         {
18281           cp_lexer_consume_token (parser->lexer);
18282
18283           /* Types may not be defined in an exception-specification.  */
18284           saved_message = parser->type_definition_forbidden_message;
18285           parser->type_definition_forbidden_message
18286             = G_("types may not be defined in an exception-specification");
18287
18288           expr = cp_parser_constant_expression (parser, false, NULL);
18289
18290           /* Restore the saved message.  */
18291           parser->type_definition_forbidden_message = saved_message;
18292
18293           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18294         }
18295       else
18296         expr = boolean_true_node;
18297
18298       return build_noexcept_spec (expr, tf_warning_or_error);
18299     }
18300
18301   /* If it's not `throw', then there's no exception-specification.  */
18302   if (!cp_parser_is_keyword (token, RID_THROW))
18303     return NULL_TREE;
18304
18305 #if 0
18306   /* Enable this once a lot of code has transitioned to noexcept?  */
18307   if (cxx_dialect == cxx0x && !in_system_header)
18308     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18309              "deprecated in C++0x; use %<noexcept%> instead");
18310 #endif
18311
18312   /* Consume the `throw'.  */
18313   cp_lexer_consume_token (parser->lexer);
18314
18315   /* Look for the `('.  */
18316   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18317
18318   /* Peek at the next token.  */
18319   token = cp_lexer_peek_token (parser->lexer);
18320   /* If it's not a `)', then there is a type-id-list.  */
18321   if (token->type != CPP_CLOSE_PAREN)
18322     {
18323       /* Types may not be defined in an exception-specification.  */
18324       saved_message = parser->type_definition_forbidden_message;
18325       parser->type_definition_forbidden_message
18326         = G_("types may not be defined in an exception-specification");
18327       /* Parse the type-id-list.  */
18328       type_id_list = cp_parser_type_id_list (parser);
18329       /* Restore the saved message.  */
18330       parser->type_definition_forbidden_message = saved_message;
18331     }
18332   else
18333     type_id_list = empty_except_spec;
18334
18335   /* Look for the `)'.  */
18336   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18337
18338   return type_id_list;
18339 }
18340
18341 /* Parse an (optional) type-id-list.
18342
18343    type-id-list:
18344      type-id ... [opt]
18345      type-id-list , type-id ... [opt]
18346
18347    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18348    in the order that the types were presented.  */
18349
18350 static tree
18351 cp_parser_type_id_list (cp_parser* parser)
18352 {
18353   tree types = NULL_TREE;
18354
18355   while (true)
18356     {
18357       cp_token *token;
18358       tree type;
18359
18360       /* Get the next type-id.  */
18361       type = cp_parser_type_id (parser);
18362       /* Parse the optional ellipsis. */
18363       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18364         {
18365           /* Consume the `...'. */
18366           cp_lexer_consume_token (parser->lexer);
18367
18368           /* Turn the type into a pack expansion expression. */
18369           type = make_pack_expansion (type);
18370         }
18371       /* Add it to the list.  */
18372       types = add_exception_specifier (types, type, /*complain=*/1);
18373       /* Peek at the next token.  */
18374       token = cp_lexer_peek_token (parser->lexer);
18375       /* If it is not a `,', we are done.  */
18376       if (token->type != CPP_COMMA)
18377         break;
18378       /* Consume the `,'.  */
18379       cp_lexer_consume_token (parser->lexer);
18380     }
18381
18382   return nreverse (types);
18383 }
18384
18385 /* Parse a try-block.
18386
18387    try-block:
18388      try compound-statement handler-seq  */
18389
18390 static tree
18391 cp_parser_try_block (cp_parser* parser)
18392 {
18393   tree try_block;
18394
18395   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18396   try_block = begin_try_block ();
18397   cp_parser_compound_statement (parser, NULL, true, false);
18398   finish_try_block (try_block);
18399   cp_parser_handler_seq (parser);
18400   finish_handler_sequence (try_block);
18401
18402   return try_block;
18403 }
18404
18405 /* Parse a function-try-block.
18406
18407    function-try-block:
18408      try ctor-initializer [opt] function-body handler-seq  */
18409
18410 static bool
18411 cp_parser_function_try_block (cp_parser* parser)
18412 {
18413   tree compound_stmt;
18414   tree try_block;
18415   bool ctor_initializer_p;
18416
18417   /* Look for the `try' keyword.  */
18418   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18419     return false;
18420   /* Let the rest of the front end know where we are.  */
18421   try_block = begin_function_try_block (&compound_stmt);
18422   /* Parse the function-body.  */
18423   ctor_initializer_p
18424     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18425   /* We're done with the `try' part.  */
18426   finish_function_try_block (try_block);
18427   /* Parse the handlers.  */
18428   cp_parser_handler_seq (parser);
18429   /* We're done with the handlers.  */
18430   finish_function_handler_sequence (try_block, compound_stmt);
18431
18432   return ctor_initializer_p;
18433 }
18434
18435 /* Parse a handler-seq.
18436
18437    handler-seq:
18438      handler handler-seq [opt]  */
18439
18440 static void
18441 cp_parser_handler_seq (cp_parser* parser)
18442 {
18443   while (true)
18444     {
18445       cp_token *token;
18446
18447       /* Parse the handler.  */
18448       cp_parser_handler (parser);
18449       /* Peek at the next token.  */
18450       token = cp_lexer_peek_token (parser->lexer);
18451       /* If it's not `catch' then there are no more handlers.  */
18452       if (!cp_parser_is_keyword (token, RID_CATCH))
18453         break;
18454     }
18455 }
18456
18457 /* Parse a handler.
18458
18459    handler:
18460      catch ( exception-declaration ) compound-statement  */
18461
18462 static void
18463 cp_parser_handler (cp_parser* parser)
18464 {
18465   tree handler;
18466   tree declaration;
18467
18468   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18469   handler = begin_handler ();
18470   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18471   declaration = cp_parser_exception_declaration (parser);
18472   finish_handler_parms (declaration, handler);
18473   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18474   cp_parser_compound_statement (parser, NULL, false, false);
18475   finish_handler (handler);
18476 }
18477
18478 /* Parse an exception-declaration.
18479
18480    exception-declaration:
18481      type-specifier-seq declarator
18482      type-specifier-seq abstract-declarator
18483      type-specifier-seq
18484      ...
18485
18486    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18487    ellipsis variant is used.  */
18488
18489 static tree
18490 cp_parser_exception_declaration (cp_parser* parser)
18491 {
18492   cp_decl_specifier_seq type_specifiers;
18493   cp_declarator *declarator;
18494   const char *saved_message;
18495
18496   /* If it's an ellipsis, it's easy to handle.  */
18497   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18498     {
18499       /* Consume the `...' token.  */
18500       cp_lexer_consume_token (parser->lexer);
18501       return NULL_TREE;
18502     }
18503
18504   /* Types may not be defined in exception-declarations.  */
18505   saved_message = parser->type_definition_forbidden_message;
18506   parser->type_definition_forbidden_message
18507     = G_("types may not be defined in exception-declarations");
18508
18509   /* Parse the type-specifier-seq.  */
18510   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18511                                 /*is_trailing_return=*/false,
18512                                 &type_specifiers);
18513   /* If it's a `)', then there is no declarator.  */
18514   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18515     declarator = NULL;
18516   else
18517     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18518                                        /*ctor_dtor_or_conv_p=*/NULL,
18519                                        /*parenthesized_p=*/NULL,
18520                                        /*member_p=*/false);
18521
18522   /* Restore the saved message.  */
18523   parser->type_definition_forbidden_message = saved_message;
18524
18525   if (!type_specifiers.any_specifiers_p)
18526     return error_mark_node;
18527
18528   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18529 }
18530
18531 /* Parse a throw-expression.
18532
18533    throw-expression:
18534      throw assignment-expression [opt]
18535
18536    Returns a THROW_EXPR representing the throw-expression.  */
18537
18538 static tree
18539 cp_parser_throw_expression (cp_parser* parser)
18540 {
18541   tree expression;
18542   cp_token* token;
18543
18544   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18545   token = cp_lexer_peek_token (parser->lexer);
18546   /* Figure out whether or not there is an assignment-expression
18547      following the "throw" keyword.  */
18548   if (token->type == CPP_COMMA
18549       || token->type == CPP_SEMICOLON
18550       || token->type == CPP_CLOSE_PAREN
18551       || token->type == CPP_CLOSE_SQUARE
18552       || token->type == CPP_CLOSE_BRACE
18553       || token->type == CPP_COLON)
18554     expression = NULL_TREE;
18555   else
18556     expression = cp_parser_assignment_expression (parser,
18557                                                   /*cast_p=*/false, NULL);
18558
18559   return build_throw (expression);
18560 }
18561
18562 /* GNU Extensions */
18563
18564 /* Parse an (optional) asm-specification.
18565
18566    asm-specification:
18567      asm ( string-literal )
18568
18569    If the asm-specification is present, returns a STRING_CST
18570    corresponding to the string-literal.  Otherwise, returns
18571    NULL_TREE.  */
18572
18573 static tree
18574 cp_parser_asm_specification_opt (cp_parser* parser)
18575 {
18576   cp_token *token;
18577   tree asm_specification;
18578
18579   /* Peek at the next token.  */
18580   token = cp_lexer_peek_token (parser->lexer);
18581   /* If the next token isn't the `asm' keyword, then there's no
18582      asm-specification.  */
18583   if (!cp_parser_is_keyword (token, RID_ASM))
18584     return NULL_TREE;
18585
18586   /* Consume the `asm' token.  */
18587   cp_lexer_consume_token (parser->lexer);
18588   /* Look for the `('.  */
18589   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18590
18591   /* Look for the string-literal.  */
18592   asm_specification = cp_parser_string_literal (parser, false, false);
18593
18594   /* Look for the `)'.  */
18595   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18596
18597   return asm_specification;
18598 }
18599
18600 /* Parse an asm-operand-list.
18601
18602    asm-operand-list:
18603      asm-operand
18604      asm-operand-list , asm-operand
18605
18606    asm-operand:
18607      string-literal ( expression )
18608      [ string-literal ] string-literal ( expression )
18609
18610    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18611    each node is the expression.  The TREE_PURPOSE is itself a
18612    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18613    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18614    is a STRING_CST for the string literal before the parenthesis. Returns
18615    ERROR_MARK_NODE if any of the operands are invalid.  */
18616
18617 static tree
18618 cp_parser_asm_operand_list (cp_parser* parser)
18619 {
18620   tree asm_operands = NULL_TREE;
18621   bool invalid_operands = false;
18622
18623   while (true)
18624     {
18625       tree string_literal;
18626       tree expression;
18627       tree name;
18628
18629       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18630         {
18631           /* Consume the `[' token.  */
18632           cp_lexer_consume_token (parser->lexer);
18633           /* Read the operand name.  */
18634           name = cp_parser_identifier (parser);
18635           if (name != error_mark_node)
18636             name = build_string (IDENTIFIER_LENGTH (name),
18637                                  IDENTIFIER_POINTER (name));
18638           /* Look for the closing `]'.  */
18639           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18640         }
18641       else
18642         name = NULL_TREE;
18643       /* Look for the string-literal.  */
18644       string_literal = cp_parser_string_literal (parser, false, false);
18645
18646       /* Look for the `('.  */
18647       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18648       /* Parse the expression.  */
18649       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18650       /* Look for the `)'.  */
18651       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18652
18653       if (name == error_mark_node 
18654           || string_literal == error_mark_node 
18655           || expression == error_mark_node)
18656         invalid_operands = true;
18657
18658       /* Add this operand to the list.  */
18659       asm_operands = tree_cons (build_tree_list (name, string_literal),
18660                                 expression,
18661                                 asm_operands);
18662       /* If the next token is not a `,', there are no more
18663          operands.  */
18664       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18665         break;
18666       /* Consume the `,'.  */
18667       cp_lexer_consume_token (parser->lexer);
18668     }
18669
18670   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18671 }
18672
18673 /* Parse an asm-clobber-list.
18674
18675    asm-clobber-list:
18676      string-literal
18677      asm-clobber-list , string-literal
18678
18679    Returns a TREE_LIST, indicating the clobbers in the order that they
18680    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18681
18682 static tree
18683 cp_parser_asm_clobber_list (cp_parser* parser)
18684 {
18685   tree clobbers = NULL_TREE;
18686
18687   while (true)
18688     {
18689       tree string_literal;
18690
18691       /* Look for the string literal.  */
18692       string_literal = cp_parser_string_literal (parser, false, false);
18693       /* Add it to the list.  */
18694       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18695       /* If the next token is not a `,', then the list is
18696          complete.  */
18697       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18698         break;
18699       /* Consume the `,' token.  */
18700       cp_lexer_consume_token (parser->lexer);
18701     }
18702
18703   return clobbers;
18704 }
18705
18706 /* Parse an asm-label-list.
18707
18708    asm-label-list:
18709      identifier
18710      asm-label-list , identifier
18711
18712    Returns a TREE_LIST, indicating the labels in the order that they
18713    appeared.  The TREE_VALUE of each node is a label.  */
18714
18715 static tree
18716 cp_parser_asm_label_list (cp_parser* parser)
18717 {
18718   tree labels = NULL_TREE;
18719
18720   while (true)
18721     {
18722       tree identifier, label, name;
18723
18724       /* Look for the identifier.  */
18725       identifier = cp_parser_identifier (parser);
18726       if (!error_operand_p (identifier))
18727         {
18728           label = lookup_label (identifier);
18729           if (TREE_CODE (label) == LABEL_DECL)
18730             {
18731               TREE_USED (label) = 1;
18732               check_goto (label);
18733               name = build_string (IDENTIFIER_LENGTH (identifier),
18734                                    IDENTIFIER_POINTER (identifier));
18735               labels = tree_cons (name, label, labels);
18736             }
18737         }
18738       /* If the next token is not a `,', then the list is
18739          complete.  */
18740       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18741         break;
18742       /* Consume the `,' token.  */
18743       cp_lexer_consume_token (parser->lexer);
18744     }
18745
18746   return nreverse (labels);
18747 }
18748
18749 /* Parse an (optional) series of attributes.
18750
18751    attributes:
18752      attributes attribute
18753
18754    attribute:
18755      __attribute__ (( attribute-list [opt] ))
18756
18757    The return value is as for cp_parser_attribute_list.  */
18758
18759 static tree
18760 cp_parser_attributes_opt (cp_parser* parser)
18761 {
18762   tree attributes = NULL_TREE;
18763
18764   while (true)
18765     {
18766       cp_token *token;
18767       tree attribute_list;
18768
18769       /* Peek at the next token.  */
18770       token = cp_lexer_peek_token (parser->lexer);
18771       /* If it's not `__attribute__', then we're done.  */
18772       if (token->keyword != RID_ATTRIBUTE)
18773         break;
18774
18775       /* Consume the `__attribute__' keyword.  */
18776       cp_lexer_consume_token (parser->lexer);
18777       /* Look for the two `(' tokens.  */
18778       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18779       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18780
18781       /* Peek at the next token.  */
18782       token = cp_lexer_peek_token (parser->lexer);
18783       if (token->type != CPP_CLOSE_PAREN)
18784         /* Parse the attribute-list.  */
18785         attribute_list = cp_parser_attribute_list (parser);
18786       else
18787         /* If the next token is a `)', then there is no attribute
18788            list.  */
18789         attribute_list = NULL;
18790
18791       /* Look for the two `)' tokens.  */
18792       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18793       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18794
18795       /* Add these new attributes to the list.  */
18796       attributes = chainon (attributes, attribute_list);
18797     }
18798
18799   return attributes;
18800 }
18801
18802 /* Parse an attribute-list.
18803
18804    attribute-list:
18805      attribute
18806      attribute-list , attribute
18807
18808    attribute:
18809      identifier
18810      identifier ( identifier )
18811      identifier ( identifier , expression-list )
18812      identifier ( expression-list )
18813
18814    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18815    to an attribute.  The TREE_PURPOSE of each node is the identifier
18816    indicating which attribute is in use.  The TREE_VALUE represents
18817    the arguments, if any.  */
18818
18819 static tree
18820 cp_parser_attribute_list (cp_parser* parser)
18821 {
18822   tree attribute_list = NULL_TREE;
18823   bool save_translate_strings_p = parser->translate_strings_p;
18824
18825   parser->translate_strings_p = false;
18826   while (true)
18827     {
18828       cp_token *token;
18829       tree identifier;
18830       tree attribute;
18831
18832       /* Look for the identifier.  We also allow keywords here; for
18833          example `__attribute__ ((const))' is legal.  */
18834       token = cp_lexer_peek_token (parser->lexer);
18835       if (token->type == CPP_NAME
18836           || token->type == CPP_KEYWORD)
18837         {
18838           tree arguments = NULL_TREE;
18839
18840           /* Consume the token.  */
18841           token = cp_lexer_consume_token (parser->lexer);
18842
18843           /* Save away the identifier that indicates which attribute
18844              this is.  */
18845           identifier = (token->type == CPP_KEYWORD) 
18846             /* For keywords, use the canonical spelling, not the
18847                parsed identifier.  */
18848             ? ridpointers[(int) token->keyword]
18849             : token->u.value;
18850           
18851           attribute = build_tree_list (identifier, NULL_TREE);
18852
18853           /* Peek at the next token.  */
18854           token = cp_lexer_peek_token (parser->lexer);
18855           /* If it's an `(', then parse the attribute arguments.  */
18856           if (token->type == CPP_OPEN_PAREN)
18857             {
18858               VEC(tree,gc) *vec;
18859               int attr_flag = (attribute_takes_identifier_p (identifier)
18860                                ? id_attr : normal_attr);
18861               vec = cp_parser_parenthesized_expression_list
18862                     (parser, attr_flag, /*cast_p=*/false,
18863                      /*allow_expansion_p=*/false,
18864                      /*non_constant_p=*/NULL);
18865               if (vec == NULL)
18866                 arguments = error_mark_node;
18867               else
18868                 {
18869                   arguments = build_tree_list_vec (vec);
18870                   release_tree_vector (vec);
18871                 }
18872               /* Save the arguments away.  */
18873               TREE_VALUE (attribute) = arguments;
18874             }
18875
18876           if (arguments != error_mark_node)
18877             {
18878               /* Add this attribute to the list.  */
18879               TREE_CHAIN (attribute) = attribute_list;
18880               attribute_list = attribute;
18881             }
18882
18883           token = cp_lexer_peek_token (parser->lexer);
18884         }
18885       /* Now, look for more attributes.  If the next token isn't a
18886          `,', we're done.  */
18887       if (token->type != CPP_COMMA)
18888         break;
18889
18890       /* Consume the comma and keep going.  */
18891       cp_lexer_consume_token (parser->lexer);
18892     }
18893   parser->translate_strings_p = save_translate_strings_p;
18894
18895   /* We built up the list in reverse order.  */
18896   return nreverse (attribute_list);
18897 }
18898
18899 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18900    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18901    current value of the PEDANTIC flag, regardless of whether or not
18902    the `__extension__' keyword is present.  The caller is responsible
18903    for restoring the value of the PEDANTIC flag.  */
18904
18905 static bool
18906 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18907 {
18908   /* Save the old value of the PEDANTIC flag.  */
18909   *saved_pedantic = pedantic;
18910
18911   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18912     {
18913       /* Consume the `__extension__' token.  */
18914       cp_lexer_consume_token (parser->lexer);
18915       /* We're not being pedantic while the `__extension__' keyword is
18916          in effect.  */
18917       pedantic = 0;
18918
18919       return true;
18920     }
18921
18922   return false;
18923 }
18924
18925 /* Parse a label declaration.
18926
18927    label-declaration:
18928      __label__ label-declarator-seq ;
18929
18930    label-declarator-seq:
18931      identifier , label-declarator-seq
18932      identifier  */
18933
18934 static void
18935 cp_parser_label_declaration (cp_parser* parser)
18936 {
18937   /* Look for the `__label__' keyword.  */
18938   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18939
18940   while (true)
18941     {
18942       tree identifier;
18943
18944       /* Look for an identifier.  */
18945       identifier = cp_parser_identifier (parser);
18946       /* If we failed, stop.  */
18947       if (identifier == error_mark_node)
18948         break;
18949       /* Declare it as a label.  */
18950       finish_label_decl (identifier);
18951       /* If the next token is a `;', stop.  */
18952       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18953         break;
18954       /* Look for the `,' separating the label declarations.  */
18955       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18956     }
18957
18958   /* Look for the final `;'.  */
18959   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18960 }
18961
18962 /* Support Functions */
18963
18964 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18965    NAME should have one of the representations used for an
18966    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18967    is returned.  If PARSER->SCOPE is a dependent type, then a
18968    SCOPE_REF is returned.
18969
18970    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18971    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18972    was formed.  Abstractly, such entities should not be passed to this
18973    function, because they do not need to be looked up, but it is
18974    simpler to check for this special case here, rather than at the
18975    call-sites.
18976
18977    In cases not explicitly covered above, this function returns a
18978    DECL, OVERLOAD, or baselink representing the result of the lookup.
18979    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18980    is returned.
18981
18982    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18983    (e.g., "struct") that was used.  In that case bindings that do not
18984    refer to types are ignored.
18985
18986    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18987    ignored.
18988
18989    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18990    are ignored.
18991
18992    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18993    types.
18994
18995    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18996    TREE_LIST of candidates if name-lookup results in an ambiguity, and
18997    NULL_TREE otherwise.  */
18998
18999 static tree
19000 cp_parser_lookup_name (cp_parser *parser, tree name,
19001                        enum tag_types tag_type,
19002                        bool is_template,
19003                        bool is_namespace,
19004                        bool check_dependency,
19005                        tree *ambiguous_decls,
19006                        location_t name_location)
19007 {
19008   int flags = 0;
19009   tree decl;
19010   tree object_type = parser->context->object_type;
19011
19012   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19013     flags |= LOOKUP_COMPLAIN;
19014
19015   /* Assume that the lookup will be unambiguous.  */
19016   if (ambiguous_decls)
19017     *ambiguous_decls = NULL_TREE;
19018
19019   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19020      no longer valid.  Note that if we are parsing tentatively, and
19021      the parse fails, OBJECT_TYPE will be automatically restored.  */
19022   parser->context->object_type = NULL_TREE;
19023
19024   if (name == error_mark_node)
19025     return error_mark_node;
19026
19027   /* A template-id has already been resolved; there is no lookup to
19028      do.  */
19029   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19030     return name;
19031   if (BASELINK_P (name))
19032     {
19033       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19034                   == TEMPLATE_ID_EXPR);
19035       return name;
19036     }
19037
19038   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19039      it should already have been checked to make sure that the name
19040      used matches the type being destroyed.  */
19041   if (TREE_CODE (name) == BIT_NOT_EXPR)
19042     {
19043       tree type;
19044
19045       /* Figure out to which type this destructor applies.  */
19046       if (parser->scope)
19047         type = parser->scope;
19048       else if (object_type)
19049         type = object_type;
19050       else
19051         type = current_class_type;
19052       /* If that's not a class type, there is no destructor.  */
19053       if (!type || !CLASS_TYPE_P (type))
19054         return error_mark_node;
19055       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19056         lazily_declare_fn (sfk_destructor, type);
19057       if (!CLASSTYPE_DESTRUCTORS (type))
19058           return error_mark_node;
19059       /* If it was a class type, return the destructor.  */
19060       return CLASSTYPE_DESTRUCTORS (type);
19061     }
19062
19063   /* By this point, the NAME should be an ordinary identifier.  If
19064      the id-expression was a qualified name, the qualifying scope is
19065      stored in PARSER->SCOPE at this point.  */
19066   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19067
19068   /* Perform the lookup.  */
19069   if (parser->scope)
19070     {
19071       bool dependent_p;
19072
19073       if (parser->scope == error_mark_node)
19074         return error_mark_node;
19075
19076       /* If the SCOPE is dependent, the lookup must be deferred until
19077          the template is instantiated -- unless we are explicitly
19078          looking up names in uninstantiated templates.  Even then, we
19079          cannot look up the name if the scope is not a class type; it
19080          might, for example, be a template type parameter.  */
19081       dependent_p = (TYPE_P (parser->scope)
19082                      && dependent_scope_p (parser->scope));
19083       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19084           && dependent_p)
19085         /* Defer lookup.  */
19086         decl = error_mark_node;
19087       else
19088         {
19089           tree pushed_scope = NULL_TREE;
19090
19091           /* If PARSER->SCOPE is a dependent type, then it must be a
19092              class type, and we must not be checking dependencies;
19093              otherwise, we would have processed this lookup above.  So
19094              that PARSER->SCOPE is not considered a dependent base by
19095              lookup_member, we must enter the scope here.  */
19096           if (dependent_p)
19097             pushed_scope = push_scope (parser->scope);
19098
19099           /* If the PARSER->SCOPE is a template specialization, it
19100              may be instantiated during name lookup.  In that case,
19101              errors may be issued.  Even if we rollback the current
19102              tentative parse, those errors are valid.  */
19103           decl = lookup_qualified_name (parser->scope, name,
19104                                         tag_type != none_type,
19105                                         /*complain=*/true);
19106
19107           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19108              lookup result and the nested-name-specifier nominates a class C:
19109                * if the name specified after the nested-name-specifier, when
19110                looked up in C, is the injected-class-name of C (Clause 9), or
19111                * if the name specified after the nested-name-specifier is the
19112                same as the identifier or the simple-template-id's template-
19113                name in the last component of the nested-name-specifier,
19114              the name is instead considered to name the constructor of
19115              class C. [ Note: for example, the constructor is not an
19116              acceptable lookup result in an elaborated-type-specifier so
19117              the constructor would not be used in place of the
19118              injected-class-name. --end note ] Such a constructor name
19119              shall be used only in the declarator-id of a declaration that
19120              names a constructor or in a using-declaration.  */
19121           if (tag_type == none_type
19122               && DECL_SELF_REFERENCE_P (decl)
19123               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19124             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19125                                           tag_type != none_type,
19126                                           /*complain=*/true);
19127
19128           /* If we have a single function from a using decl, pull it out.  */
19129           if (TREE_CODE (decl) == OVERLOAD
19130               && !really_overloaded_fn (decl))
19131             decl = OVL_FUNCTION (decl);
19132
19133           if (pushed_scope)
19134             pop_scope (pushed_scope);
19135         }
19136
19137       /* If the scope is a dependent type and either we deferred lookup or
19138          we did lookup but didn't find the name, rememeber the name.  */
19139       if (decl == error_mark_node && TYPE_P (parser->scope)
19140           && dependent_type_p (parser->scope))
19141         {
19142           if (tag_type)
19143             {
19144               tree type;
19145
19146               /* The resolution to Core Issue 180 says that `struct
19147                  A::B' should be considered a type-name, even if `A'
19148                  is dependent.  */
19149               type = make_typename_type (parser->scope, name, tag_type,
19150                                          /*complain=*/tf_error);
19151               decl = TYPE_NAME (type);
19152             }
19153           else if (is_template
19154                    && (cp_parser_next_token_ends_template_argument_p (parser)
19155                        || cp_lexer_next_token_is (parser->lexer,
19156                                                   CPP_CLOSE_PAREN)))
19157             decl = make_unbound_class_template (parser->scope,
19158                                                 name, NULL_TREE,
19159                                                 /*complain=*/tf_error);
19160           else
19161             decl = build_qualified_name (/*type=*/NULL_TREE,
19162                                          parser->scope, name,
19163                                          is_template);
19164         }
19165       parser->qualifying_scope = parser->scope;
19166       parser->object_scope = NULL_TREE;
19167     }
19168   else if (object_type)
19169     {
19170       tree object_decl = NULL_TREE;
19171       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19172          OBJECT_TYPE is not a class.  */
19173       if (CLASS_TYPE_P (object_type))
19174         /* If the OBJECT_TYPE is a template specialization, it may
19175            be instantiated during name lookup.  In that case, errors
19176            may be issued.  Even if we rollback the current tentative
19177            parse, those errors are valid.  */
19178         object_decl = lookup_member (object_type,
19179                                      name,
19180                                      /*protect=*/0,
19181                                      tag_type != none_type);
19182       /* Look it up in the enclosing context, too.  */
19183       decl = lookup_name_real (name, tag_type != none_type,
19184                                /*nonclass=*/0,
19185                                /*block_p=*/true, is_namespace, flags);
19186       parser->object_scope = object_type;
19187       parser->qualifying_scope = NULL_TREE;
19188       if (object_decl)
19189         decl = object_decl;
19190     }
19191   else
19192     {
19193       decl = lookup_name_real (name, tag_type != none_type,
19194                                /*nonclass=*/0,
19195                                /*block_p=*/true, is_namespace, flags);
19196       parser->qualifying_scope = NULL_TREE;
19197       parser->object_scope = NULL_TREE;
19198     }
19199
19200   /* If the lookup failed, let our caller know.  */
19201   if (!decl || decl == error_mark_node)
19202     return error_mark_node;
19203
19204   /* Pull out the template from an injected-class-name (or multiple).  */
19205   if (is_template)
19206     decl = maybe_get_template_decl_from_type_decl (decl);
19207
19208   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19209   if (TREE_CODE (decl) == TREE_LIST)
19210     {
19211       if (ambiguous_decls)
19212         *ambiguous_decls = decl;
19213       /* The error message we have to print is too complicated for
19214          cp_parser_error, so we incorporate its actions directly.  */
19215       if (!cp_parser_simulate_error (parser))
19216         {
19217           error_at (name_location, "reference to %qD is ambiguous",
19218                     name);
19219           print_candidates (decl);
19220         }
19221       return error_mark_node;
19222     }
19223
19224   gcc_assert (DECL_P (decl)
19225               || TREE_CODE (decl) == OVERLOAD
19226               || TREE_CODE (decl) == SCOPE_REF
19227               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19228               || BASELINK_P (decl));
19229
19230   /* If we have resolved the name of a member declaration, check to
19231      see if the declaration is accessible.  When the name resolves to
19232      set of overloaded functions, accessibility is checked when
19233      overload resolution is done.
19234
19235      During an explicit instantiation, access is not checked at all,
19236      as per [temp.explicit].  */
19237   if (DECL_P (decl))
19238     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19239
19240   return decl;
19241 }
19242
19243 /* Like cp_parser_lookup_name, but for use in the typical case where
19244    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19245    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19246
19247 static tree
19248 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19249 {
19250   return cp_parser_lookup_name (parser, name,
19251                                 none_type,
19252                                 /*is_template=*/false,
19253                                 /*is_namespace=*/false,
19254                                 /*check_dependency=*/true,
19255                                 /*ambiguous_decls=*/NULL,
19256                                 location);
19257 }
19258
19259 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19260    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19261    true, the DECL indicates the class being defined in a class-head,
19262    or declared in an elaborated-type-specifier.
19263
19264    Otherwise, return DECL.  */
19265
19266 static tree
19267 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19268 {
19269   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19270      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19271
19272        struct A {
19273          template <typename T> struct B;
19274        };
19275
19276        template <typename T> struct A::B {};
19277
19278      Similarly, in an elaborated-type-specifier:
19279
19280        namespace N { struct X{}; }
19281
19282        struct A {
19283          template <typename T> friend struct N::X;
19284        };
19285
19286      However, if the DECL refers to a class type, and we are in
19287      the scope of the class, then the name lookup automatically
19288      finds the TYPE_DECL created by build_self_reference rather
19289      than a TEMPLATE_DECL.  For example, in:
19290
19291        template <class T> struct S {
19292          S s;
19293        };
19294
19295      there is no need to handle such case.  */
19296
19297   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19298     return DECL_TEMPLATE_RESULT (decl);
19299
19300   return decl;
19301 }
19302
19303 /* If too many, or too few, template-parameter lists apply to the
19304    declarator, issue an error message.  Returns TRUE if all went well,
19305    and FALSE otherwise.  */
19306
19307 static bool
19308 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19309                                                 cp_declarator *declarator,
19310                                                 location_t declarator_location)
19311 {
19312   unsigned num_templates;
19313
19314   /* We haven't seen any classes that involve template parameters yet.  */
19315   num_templates = 0;
19316
19317   switch (declarator->kind)
19318     {
19319     case cdk_id:
19320       if (declarator->u.id.qualifying_scope)
19321         {
19322           tree scope;
19323
19324           scope = declarator->u.id.qualifying_scope;
19325
19326           while (scope && CLASS_TYPE_P (scope))
19327             {
19328               /* You're supposed to have one `template <...>'
19329                  for every template class, but you don't need one
19330                  for a full specialization.  For example:
19331
19332                  template <class T> struct S{};
19333                  template <> struct S<int> { void f(); };
19334                  void S<int>::f () {}
19335
19336                  is correct; there shouldn't be a `template <>' for
19337                  the definition of `S<int>::f'.  */
19338               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19339                 /* If SCOPE does not have template information of any
19340                    kind, then it is not a template, nor is it nested
19341                    within a template.  */
19342                 break;
19343               if (explicit_class_specialization_p (scope))
19344                 break;
19345               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19346                 ++num_templates;
19347
19348               scope = TYPE_CONTEXT (scope);
19349             }
19350         }
19351       else if (TREE_CODE (declarator->u.id.unqualified_name)
19352                == TEMPLATE_ID_EXPR)
19353         /* If the DECLARATOR has the form `X<y>' then it uses one
19354            additional level of template parameters.  */
19355         ++num_templates;
19356
19357       return cp_parser_check_template_parameters 
19358         (parser, num_templates, declarator_location, declarator);
19359
19360
19361     case cdk_function:
19362     case cdk_array:
19363     case cdk_pointer:
19364     case cdk_reference:
19365     case cdk_ptrmem:
19366       return (cp_parser_check_declarator_template_parameters
19367               (parser, declarator->declarator, declarator_location));
19368
19369     case cdk_error:
19370       return true;
19371
19372     default:
19373       gcc_unreachable ();
19374     }
19375   return false;
19376 }
19377
19378 /* NUM_TEMPLATES were used in the current declaration.  If that is
19379    invalid, return FALSE and issue an error messages.  Otherwise,
19380    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19381    declarator and we can print more accurate diagnostics.  */
19382
19383 static bool
19384 cp_parser_check_template_parameters (cp_parser* parser,
19385                                      unsigned num_templates,
19386                                      location_t location,
19387                                      cp_declarator *declarator)
19388 {
19389   /* If there are the same number of template classes and parameter
19390      lists, that's OK.  */
19391   if (parser->num_template_parameter_lists == num_templates)
19392     return true;
19393   /* If there are more, but only one more, then we are referring to a
19394      member template.  That's OK too.  */
19395   if (parser->num_template_parameter_lists == num_templates + 1)
19396     return true;
19397   /* If there are more template classes than parameter lists, we have
19398      something like:
19399
19400        template <class T> void S<T>::R<T>::f ();  */
19401   if (parser->num_template_parameter_lists < num_templates)
19402     {
19403       if (declarator && !current_function_decl)
19404         error_at (location, "specializing member %<%T::%E%> "
19405                   "requires %<template<>%> syntax", 
19406                   declarator->u.id.qualifying_scope,
19407                   declarator->u.id.unqualified_name);
19408       else if (declarator)
19409         error_at (location, "invalid declaration of %<%T::%E%>",
19410                   declarator->u.id.qualifying_scope,
19411                   declarator->u.id.unqualified_name);
19412       else 
19413         error_at (location, "too few template-parameter-lists");
19414       return false;
19415     }
19416   /* Otherwise, there are too many template parameter lists.  We have
19417      something like:
19418
19419      template <class T> template <class U> void S::f();  */
19420   error_at (location, "too many template-parameter-lists");
19421   return false;
19422 }
19423
19424 /* Parse an optional `::' token indicating that the following name is
19425    from the global namespace.  If so, PARSER->SCOPE is set to the
19426    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19427    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19428    Returns the new value of PARSER->SCOPE, if the `::' token is
19429    present, and NULL_TREE otherwise.  */
19430
19431 static tree
19432 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19433 {
19434   cp_token *token;
19435
19436   /* Peek at the next token.  */
19437   token = cp_lexer_peek_token (parser->lexer);
19438   /* If we're looking at a `::' token then we're starting from the
19439      global namespace, not our current location.  */
19440   if (token->type == CPP_SCOPE)
19441     {
19442       /* Consume the `::' token.  */
19443       cp_lexer_consume_token (parser->lexer);
19444       /* Set the SCOPE so that we know where to start the lookup.  */
19445       parser->scope = global_namespace;
19446       parser->qualifying_scope = global_namespace;
19447       parser->object_scope = NULL_TREE;
19448
19449       return parser->scope;
19450     }
19451   else if (!current_scope_valid_p)
19452     {
19453       parser->scope = NULL_TREE;
19454       parser->qualifying_scope = NULL_TREE;
19455       parser->object_scope = NULL_TREE;
19456     }
19457
19458   return NULL_TREE;
19459 }
19460
19461 /* Returns TRUE if the upcoming token sequence is the start of a
19462    constructor declarator.  If FRIEND_P is true, the declarator is
19463    preceded by the `friend' specifier.  */
19464
19465 static bool
19466 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19467 {
19468   bool constructor_p;
19469   tree nested_name_specifier;
19470   cp_token *next_token;
19471
19472   /* The common case is that this is not a constructor declarator, so
19473      try to avoid doing lots of work if at all possible.  It's not
19474      valid declare a constructor at function scope.  */
19475   if (parser->in_function_body)
19476     return false;
19477   /* And only certain tokens can begin a constructor declarator.  */
19478   next_token = cp_lexer_peek_token (parser->lexer);
19479   if (next_token->type != CPP_NAME
19480       && next_token->type != CPP_SCOPE
19481       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19482       && next_token->type != CPP_TEMPLATE_ID)
19483     return false;
19484
19485   /* Parse tentatively; we are going to roll back all of the tokens
19486      consumed here.  */
19487   cp_parser_parse_tentatively (parser);
19488   /* Assume that we are looking at a constructor declarator.  */
19489   constructor_p = true;
19490
19491   /* Look for the optional `::' operator.  */
19492   cp_parser_global_scope_opt (parser,
19493                               /*current_scope_valid_p=*/false);
19494   /* Look for the nested-name-specifier.  */
19495   nested_name_specifier
19496     = (cp_parser_nested_name_specifier_opt (parser,
19497                                             /*typename_keyword_p=*/false,
19498                                             /*check_dependency_p=*/false,
19499                                             /*type_p=*/false,
19500                                             /*is_declaration=*/false));
19501   /* Outside of a class-specifier, there must be a
19502      nested-name-specifier.  */
19503   if (!nested_name_specifier &&
19504       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19505        || friend_p))
19506     constructor_p = false;
19507   else if (nested_name_specifier == error_mark_node)
19508     constructor_p = false;
19509
19510   /* If we have a class scope, this is easy; DR 147 says that S::S always
19511      names the constructor, and no other qualified name could.  */
19512   if (constructor_p && nested_name_specifier
19513       && TYPE_P (nested_name_specifier))
19514     {
19515       tree id = cp_parser_unqualified_id (parser,
19516                                           /*template_keyword_p=*/false,
19517                                           /*check_dependency_p=*/false,
19518                                           /*declarator_p=*/true,
19519                                           /*optional_p=*/false);
19520       if (is_overloaded_fn (id))
19521         id = DECL_NAME (get_first_fn (id));
19522       if (!constructor_name_p (id, nested_name_specifier))
19523         constructor_p = false;
19524     }
19525   /* If we still think that this might be a constructor-declarator,
19526      look for a class-name.  */
19527   else if (constructor_p)
19528     {
19529       /* If we have:
19530
19531            template <typename T> struct S {
19532              S();
19533            };
19534
19535          we must recognize that the nested `S' names a class.  */
19536       tree type_decl;
19537       type_decl = cp_parser_class_name (parser,
19538                                         /*typename_keyword_p=*/false,
19539                                         /*template_keyword_p=*/false,
19540                                         none_type,
19541                                         /*check_dependency_p=*/false,
19542                                         /*class_head_p=*/false,
19543                                         /*is_declaration=*/false);
19544       /* If there was no class-name, then this is not a constructor.  */
19545       constructor_p = !cp_parser_error_occurred (parser);
19546
19547       /* If we're still considering a constructor, we have to see a `(',
19548          to begin the parameter-declaration-clause, followed by either a
19549          `)', an `...', or a decl-specifier.  We need to check for a
19550          type-specifier to avoid being fooled into thinking that:
19551
19552            S (f) (int);
19553
19554          is a constructor.  (It is actually a function named `f' that
19555          takes one parameter (of type `int') and returns a value of type
19556          `S'.  */
19557       if (constructor_p
19558           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19559         constructor_p = false;
19560
19561       if (constructor_p
19562           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19563           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19564           /* A parameter declaration begins with a decl-specifier,
19565              which is either the "attribute" keyword, a storage class
19566              specifier, or (usually) a type-specifier.  */
19567           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19568         {
19569           tree type;
19570           tree pushed_scope = NULL_TREE;
19571           unsigned saved_num_template_parameter_lists;
19572
19573           /* Names appearing in the type-specifier should be looked up
19574              in the scope of the class.  */
19575           if (current_class_type)
19576             type = NULL_TREE;
19577           else
19578             {
19579               type = TREE_TYPE (type_decl);
19580               if (TREE_CODE (type) == TYPENAME_TYPE)
19581                 {
19582                   type = resolve_typename_type (type,
19583                                                 /*only_current_p=*/false);
19584                   if (TREE_CODE (type) == TYPENAME_TYPE)
19585                     {
19586                       cp_parser_abort_tentative_parse (parser);
19587                       return false;
19588                     }
19589                 }
19590               pushed_scope = push_scope (type);
19591             }
19592
19593           /* Inside the constructor parameter list, surrounding
19594              template-parameter-lists do not apply.  */
19595           saved_num_template_parameter_lists
19596             = parser->num_template_parameter_lists;
19597           parser->num_template_parameter_lists = 0;
19598
19599           /* Look for the type-specifier.  */
19600           cp_parser_type_specifier (parser,
19601                                     CP_PARSER_FLAGS_NONE,
19602                                     /*decl_specs=*/NULL,
19603                                     /*is_declarator=*/true,
19604                                     /*declares_class_or_enum=*/NULL,
19605                                     /*is_cv_qualifier=*/NULL);
19606
19607           parser->num_template_parameter_lists
19608             = saved_num_template_parameter_lists;
19609
19610           /* Leave the scope of the class.  */
19611           if (pushed_scope)
19612             pop_scope (pushed_scope);
19613
19614           constructor_p = !cp_parser_error_occurred (parser);
19615         }
19616     }
19617
19618   /* We did not really want to consume any tokens.  */
19619   cp_parser_abort_tentative_parse (parser);
19620
19621   return constructor_p;
19622 }
19623
19624 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19625    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19626    they must be performed once we are in the scope of the function.
19627
19628    Returns the function defined.  */
19629
19630 static tree
19631 cp_parser_function_definition_from_specifiers_and_declarator
19632   (cp_parser* parser,
19633    cp_decl_specifier_seq *decl_specifiers,
19634    tree attributes,
19635    const cp_declarator *declarator)
19636 {
19637   tree fn;
19638   bool success_p;
19639
19640   /* Begin the function-definition.  */
19641   success_p = start_function (decl_specifiers, declarator, attributes);
19642
19643   /* The things we're about to see are not directly qualified by any
19644      template headers we've seen thus far.  */
19645   reset_specialization ();
19646
19647   /* If there were names looked up in the decl-specifier-seq that we
19648      did not check, check them now.  We must wait until we are in the
19649      scope of the function to perform the checks, since the function
19650      might be a friend.  */
19651   perform_deferred_access_checks ();
19652
19653   if (!success_p)
19654     {
19655       /* Skip the entire function.  */
19656       cp_parser_skip_to_end_of_block_or_statement (parser);
19657       fn = error_mark_node;
19658     }
19659   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19660     {
19661       /* Seen already, skip it.  An error message has already been output.  */
19662       cp_parser_skip_to_end_of_block_or_statement (parser);
19663       fn = current_function_decl;
19664       current_function_decl = NULL_TREE;
19665       /* If this is a function from a class, pop the nested class.  */
19666       if (current_class_name)
19667         pop_nested_class ();
19668     }
19669   else
19670     {
19671       timevar_id_t tv;
19672       if (DECL_DECLARED_INLINE_P (current_function_decl))
19673         tv = TV_PARSE_INLINE;
19674       else
19675         tv = TV_PARSE_FUNC;
19676       timevar_push (tv);
19677       fn = cp_parser_function_definition_after_declarator (parser,
19678                                                          /*inline_p=*/false);
19679       timevar_pop (tv);
19680     }
19681
19682   return fn;
19683 }
19684
19685 /* Parse the part of a function-definition that follows the
19686    declarator.  INLINE_P is TRUE iff this function is an inline
19687    function defined within a class-specifier.
19688
19689    Returns the function defined.  */
19690
19691 static tree
19692 cp_parser_function_definition_after_declarator (cp_parser* parser,
19693                                                 bool inline_p)
19694 {
19695   tree fn;
19696   bool ctor_initializer_p = false;
19697   bool saved_in_unbraced_linkage_specification_p;
19698   bool saved_in_function_body;
19699   unsigned saved_num_template_parameter_lists;
19700   cp_token *token;
19701
19702   saved_in_function_body = parser->in_function_body;
19703   parser->in_function_body = true;
19704   /* If the next token is `return', then the code may be trying to
19705      make use of the "named return value" extension that G++ used to
19706      support.  */
19707   token = cp_lexer_peek_token (parser->lexer);
19708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19709     {
19710       /* Consume the `return' keyword.  */
19711       cp_lexer_consume_token (parser->lexer);
19712       /* Look for the identifier that indicates what value is to be
19713          returned.  */
19714       cp_parser_identifier (parser);
19715       /* Issue an error message.  */
19716       error_at (token->location,
19717                 "named return values are no longer supported");
19718       /* Skip tokens until we reach the start of the function body.  */
19719       while (true)
19720         {
19721           cp_token *token = cp_lexer_peek_token (parser->lexer);
19722           if (token->type == CPP_OPEN_BRACE
19723               || token->type == CPP_EOF
19724               || token->type == CPP_PRAGMA_EOL)
19725             break;
19726           cp_lexer_consume_token (parser->lexer);
19727         }
19728     }
19729   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19730      anything declared inside `f'.  */
19731   saved_in_unbraced_linkage_specification_p
19732     = parser->in_unbraced_linkage_specification_p;
19733   parser->in_unbraced_linkage_specification_p = false;
19734   /* Inside the function, surrounding template-parameter-lists do not
19735      apply.  */
19736   saved_num_template_parameter_lists
19737     = parser->num_template_parameter_lists;
19738   parser->num_template_parameter_lists = 0;
19739
19740   start_lambda_scope (current_function_decl);
19741
19742   /* If the next token is `try', then we are looking at a
19743      function-try-block.  */
19744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19745     ctor_initializer_p = cp_parser_function_try_block (parser);
19746   /* A function-try-block includes the function-body, so we only do
19747      this next part if we're not processing a function-try-block.  */
19748   else
19749     ctor_initializer_p
19750       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19751
19752   finish_lambda_scope ();
19753
19754   /* Finish the function.  */
19755   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19756                         (inline_p ? 2 : 0));
19757   /* Generate code for it, if necessary.  */
19758   expand_or_defer_fn (fn);
19759   /* Restore the saved values.  */
19760   parser->in_unbraced_linkage_specification_p
19761     = saved_in_unbraced_linkage_specification_p;
19762   parser->num_template_parameter_lists
19763     = saved_num_template_parameter_lists;
19764   parser->in_function_body = saved_in_function_body;
19765
19766   return fn;
19767 }
19768
19769 /* Parse a template-declaration, assuming that the `export' (and
19770    `extern') keywords, if present, has already been scanned.  MEMBER_P
19771    is as for cp_parser_template_declaration.  */
19772
19773 static void
19774 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19775 {
19776   tree decl = NULL_TREE;
19777   VEC (deferred_access_check,gc) *checks;
19778   tree parameter_list;
19779   bool friend_p = false;
19780   bool need_lang_pop;
19781   cp_token *token;
19782
19783   /* Look for the `template' keyword.  */
19784   token = cp_lexer_peek_token (parser->lexer);
19785   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19786     return;
19787
19788   /* And the `<'.  */
19789   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19790     return;
19791   if (at_class_scope_p () && current_function_decl)
19792     {
19793       /* 14.5.2.2 [temp.mem]
19794
19795          A local class shall not have member templates.  */
19796       error_at (token->location,
19797                 "invalid declaration of member template in local class");
19798       cp_parser_skip_to_end_of_block_or_statement (parser);
19799       return;
19800     }
19801   /* [temp]
19802
19803      A template ... shall not have C linkage.  */
19804   if (current_lang_name == lang_name_c)
19805     {
19806       error_at (token->location, "template with C linkage");
19807       /* Give it C++ linkage to avoid confusing other parts of the
19808          front end.  */
19809       push_lang_context (lang_name_cplusplus);
19810       need_lang_pop = true;
19811     }
19812   else
19813     need_lang_pop = false;
19814
19815   /* We cannot perform access checks on the template parameter
19816      declarations until we know what is being declared, just as we
19817      cannot check the decl-specifier list.  */
19818   push_deferring_access_checks (dk_deferred);
19819
19820   /* If the next token is `>', then we have an invalid
19821      specialization.  Rather than complain about an invalid template
19822      parameter, issue an error message here.  */
19823   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19824     {
19825       cp_parser_error (parser, "invalid explicit specialization");
19826       begin_specialization ();
19827       parameter_list = NULL_TREE;
19828     }
19829   else
19830     {
19831       /* Parse the template parameters.  */
19832       parameter_list = cp_parser_template_parameter_list (parser);
19833       fixup_template_parms ();
19834     }
19835
19836   /* Get the deferred access checks from the parameter list.  These
19837      will be checked once we know what is being declared, as for a
19838      member template the checks must be performed in the scope of the
19839      class containing the member.  */
19840   checks = get_deferred_access_checks ();
19841
19842   /* Look for the `>'.  */
19843   cp_parser_skip_to_end_of_template_parameter_list (parser);
19844   /* We just processed one more parameter list.  */
19845   ++parser->num_template_parameter_lists;
19846   /* If the next token is `template', there are more template
19847      parameters.  */
19848   if (cp_lexer_next_token_is_keyword (parser->lexer,
19849                                       RID_TEMPLATE))
19850     cp_parser_template_declaration_after_export (parser, member_p);
19851   else
19852     {
19853       /* There are no access checks when parsing a template, as we do not
19854          know if a specialization will be a friend.  */
19855       push_deferring_access_checks (dk_no_check);
19856       token = cp_lexer_peek_token (parser->lexer);
19857       decl = cp_parser_single_declaration (parser,
19858                                            checks,
19859                                            member_p,
19860                                            /*explicit_specialization_p=*/false,
19861                                            &friend_p);
19862       pop_deferring_access_checks ();
19863
19864       /* If this is a member template declaration, let the front
19865          end know.  */
19866       if (member_p && !friend_p && decl)
19867         {
19868           if (TREE_CODE (decl) == TYPE_DECL)
19869             cp_parser_check_access_in_redeclaration (decl, token->location);
19870
19871           decl = finish_member_template_decl (decl);
19872         }
19873       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19874         make_friend_class (current_class_type, TREE_TYPE (decl),
19875                            /*complain=*/true);
19876     }
19877   /* We are done with the current parameter list.  */
19878   --parser->num_template_parameter_lists;
19879
19880   pop_deferring_access_checks ();
19881
19882   /* Finish up.  */
19883   finish_template_decl (parameter_list);
19884
19885   /* Register member declarations.  */
19886   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19887     finish_member_declaration (decl);
19888   /* For the erroneous case of a template with C linkage, we pushed an
19889      implicit C++ linkage scope; exit that scope now.  */
19890   if (need_lang_pop)
19891     pop_lang_context ();
19892   /* If DECL is a function template, we must return to parse it later.
19893      (Even though there is no definition, there might be default
19894      arguments that need handling.)  */
19895   if (member_p && decl
19896       && (TREE_CODE (decl) == FUNCTION_DECL
19897           || DECL_FUNCTION_TEMPLATE_P (decl)))
19898     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19899 }
19900
19901 /* Perform the deferred access checks from a template-parameter-list.
19902    CHECKS is a TREE_LIST of access checks, as returned by
19903    get_deferred_access_checks.  */
19904
19905 static void
19906 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19907 {
19908   ++processing_template_parmlist;
19909   perform_access_checks (checks);
19910   --processing_template_parmlist;
19911 }
19912
19913 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19914    `function-definition' sequence.  MEMBER_P is true, this declaration
19915    appears in a class scope.
19916
19917    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19918    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19919
19920 static tree
19921 cp_parser_single_declaration (cp_parser* parser,
19922                               VEC (deferred_access_check,gc)* checks,
19923                               bool member_p,
19924                               bool explicit_specialization_p,
19925                               bool* friend_p)
19926 {
19927   int declares_class_or_enum;
19928   tree decl = NULL_TREE;
19929   cp_decl_specifier_seq decl_specifiers;
19930   bool function_definition_p = false;
19931   cp_token *decl_spec_token_start;
19932
19933   /* This function is only used when processing a template
19934      declaration.  */
19935   gcc_assert (innermost_scope_kind () == sk_template_parms
19936               || innermost_scope_kind () == sk_template_spec);
19937
19938   /* Defer access checks until we know what is being declared.  */
19939   push_deferring_access_checks (dk_deferred);
19940
19941   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19942      alternative.  */
19943   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19944   cp_parser_decl_specifier_seq (parser,
19945                                 CP_PARSER_FLAGS_OPTIONAL,
19946                                 &decl_specifiers,
19947                                 &declares_class_or_enum);
19948   if (friend_p)
19949     *friend_p = cp_parser_friend_p (&decl_specifiers);
19950
19951   /* There are no template typedefs.  */
19952   if (decl_specifiers.specs[(int) ds_typedef])
19953     {
19954       error_at (decl_spec_token_start->location,
19955                 "template declaration of %<typedef%>");
19956       decl = error_mark_node;
19957     }
19958
19959   /* Gather up the access checks that occurred the
19960      decl-specifier-seq.  */
19961   stop_deferring_access_checks ();
19962
19963   /* Check for the declaration of a template class.  */
19964   if (declares_class_or_enum)
19965     {
19966       if (cp_parser_declares_only_class_p (parser))
19967         {
19968           decl = shadow_tag (&decl_specifiers);
19969
19970           /* In this case:
19971
19972                struct C {
19973                  friend template <typename T> struct A<T>::B;
19974                };
19975
19976              A<T>::B will be represented by a TYPENAME_TYPE, and
19977              therefore not recognized by shadow_tag.  */
19978           if (friend_p && *friend_p
19979               && !decl
19980               && decl_specifiers.type
19981               && TYPE_P (decl_specifiers.type))
19982             decl = decl_specifiers.type;
19983
19984           if (decl && decl != error_mark_node)
19985             decl = TYPE_NAME (decl);
19986           else
19987             decl = error_mark_node;
19988
19989           /* Perform access checks for template parameters.  */
19990           cp_parser_perform_template_parameter_access_checks (checks);
19991         }
19992     }
19993
19994   /* Complain about missing 'typename' or other invalid type names.  */
19995   if (!decl_specifiers.any_type_specifiers_p
19996       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19997     {
19998       /* cp_parser_parse_and_diagnose_invalid_type_name calls
19999          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20000          the rest of this declaration.  */
20001       decl = error_mark_node;
20002       goto out;
20003     }
20004
20005   /* If it's not a template class, try for a template function.  If
20006      the next token is a `;', then this declaration does not declare
20007      anything.  But, if there were errors in the decl-specifiers, then
20008      the error might well have come from an attempted class-specifier.
20009      In that case, there's no need to warn about a missing declarator.  */
20010   if (!decl
20011       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20012           || decl_specifiers.type != error_mark_node))
20013     {
20014       decl = cp_parser_init_declarator (parser,
20015                                         &decl_specifiers,
20016                                         checks,
20017                                         /*function_definition_allowed_p=*/true,
20018                                         member_p,
20019                                         declares_class_or_enum,
20020                                         &function_definition_p,
20021                                         NULL);
20022
20023     /* 7.1.1-1 [dcl.stc]
20024
20025        A storage-class-specifier shall not be specified in an explicit
20026        specialization...  */
20027     if (decl
20028         && explicit_specialization_p
20029         && decl_specifiers.storage_class != sc_none)
20030       {
20031         error_at (decl_spec_token_start->location,
20032                   "explicit template specialization cannot have a storage class");
20033         decl = error_mark_node;
20034       }
20035     }
20036
20037   /* Look for a trailing `;' after the declaration.  */
20038   if (!function_definition_p
20039       && (decl == error_mark_node
20040           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20041     cp_parser_skip_to_end_of_block_or_statement (parser);
20042
20043  out:
20044   pop_deferring_access_checks ();
20045
20046   /* Clear any current qualification; whatever comes next is the start
20047      of something new.  */
20048   parser->scope = NULL_TREE;
20049   parser->qualifying_scope = NULL_TREE;
20050   parser->object_scope = NULL_TREE;
20051
20052   return decl;
20053 }
20054
20055 /* Parse a cast-expression that is not the operand of a unary "&".  */
20056
20057 static tree
20058 cp_parser_simple_cast_expression (cp_parser *parser)
20059 {
20060   return cp_parser_cast_expression (parser, /*address_p=*/false,
20061                                     /*cast_p=*/false, NULL);
20062 }
20063
20064 /* Parse a functional cast to TYPE.  Returns an expression
20065    representing the cast.  */
20066
20067 static tree
20068 cp_parser_functional_cast (cp_parser* parser, tree type)
20069 {
20070   VEC(tree,gc) *vec;
20071   tree expression_list;
20072   tree cast;
20073   bool nonconst_p;
20074
20075   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20076     {
20077       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20078       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20079       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20080       if (TREE_CODE (type) == TYPE_DECL)
20081         type = TREE_TYPE (type);
20082       return finish_compound_literal (type, expression_list,
20083                                       tf_warning_or_error);
20084     }
20085
20086
20087   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20088                                                  /*cast_p=*/true,
20089                                                  /*allow_expansion_p=*/true,
20090                                                  /*non_constant_p=*/NULL);
20091   if (vec == NULL)
20092     expression_list = error_mark_node;
20093   else
20094     {
20095       expression_list = build_tree_list_vec (vec);
20096       release_tree_vector (vec);
20097     }
20098
20099   cast = build_functional_cast (type, expression_list,
20100                                 tf_warning_or_error);
20101   /* [expr.const]/1: In an integral constant expression "only type
20102      conversions to integral or enumeration type can be used".  */
20103   if (TREE_CODE (type) == TYPE_DECL)
20104     type = TREE_TYPE (type);
20105   if (cast != error_mark_node
20106       && !cast_valid_in_integral_constant_expression_p (type)
20107       && cp_parser_non_integral_constant_expression (parser,
20108                                                      NIC_CONSTRUCTOR))
20109     return error_mark_node;
20110   return cast;
20111 }
20112
20113 /* Save the tokens that make up the body of a member function defined
20114    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20115    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20116    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20117    for the member function.  */
20118
20119 static tree
20120 cp_parser_save_member_function_body (cp_parser* parser,
20121                                      cp_decl_specifier_seq *decl_specifiers,
20122                                      cp_declarator *declarator,
20123                                      tree attributes)
20124 {
20125   cp_token *first;
20126   cp_token *last;
20127   tree fn;
20128
20129   /* Create the FUNCTION_DECL.  */
20130   fn = grokmethod (decl_specifiers, declarator, attributes);
20131   /* If something went badly wrong, bail out now.  */
20132   if (fn == error_mark_node)
20133     {
20134       /* If there's a function-body, skip it.  */
20135       if (cp_parser_token_starts_function_definition_p
20136           (cp_lexer_peek_token (parser->lexer)))
20137         cp_parser_skip_to_end_of_block_or_statement (parser);
20138       return error_mark_node;
20139     }
20140
20141   /* Remember it, if there default args to post process.  */
20142   cp_parser_save_default_args (parser, fn);
20143
20144   /* Save away the tokens that make up the body of the
20145      function.  */
20146   first = parser->lexer->next_token;
20147   /* We can have braced-init-list mem-initializers before the fn body.  */
20148   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20149     {
20150       cp_lexer_consume_token (parser->lexer);
20151       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20152              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20153         {
20154           /* cache_group will stop after an un-nested { } pair, too.  */
20155           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20156             break;
20157
20158           /* variadic mem-inits have ... after the ')'.  */
20159           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20160             cp_lexer_consume_token (parser->lexer);
20161         }
20162     }
20163   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20164   /* Handle function try blocks.  */
20165   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20166     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20167   last = parser->lexer->next_token;
20168
20169   /* Save away the inline definition; we will process it when the
20170      class is complete.  */
20171   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20172   DECL_PENDING_INLINE_P (fn) = 1;
20173
20174   /* We need to know that this was defined in the class, so that
20175      friend templates are handled correctly.  */
20176   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20177
20178   /* Add FN to the queue of functions to be parsed later.  */
20179   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20180
20181   return fn;
20182 }
20183
20184 /* Parse a template-argument-list, as well as the trailing ">" (but
20185    not the opening ">").  See cp_parser_template_argument_list for the
20186    return value.  */
20187
20188 static tree
20189 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20190 {
20191   tree arguments;
20192   tree saved_scope;
20193   tree saved_qualifying_scope;
20194   tree saved_object_scope;
20195   bool saved_greater_than_is_operator_p;
20196   int saved_unevaluated_operand;
20197   int saved_inhibit_evaluation_warnings;
20198
20199   /* [temp.names]
20200
20201      When parsing a template-id, the first non-nested `>' is taken as
20202      the end of the template-argument-list rather than a greater-than
20203      operator.  */
20204   saved_greater_than_is_operator_p
20205     = parser->greater_than_is_operator_p;
20206   parser->greater_than_is_operator_p = false;
20207   /* Parsing the argument list may modify SCOPE, so we save it
20208      here.  */
20209   saved_scope = parser->scope;
20210   saved_qualifying_scope = parser->qualifying_scope;
20211   saved_object_scope = parser->object_scope;
20212   /* We need to evaluate the template arguments, even though this
20213      template-id may be nested within a "sizeof".  */
20214   saved_unevaluated_operand = cp_unevaluated_operand;
20215   cp_unevaluated_operand = 0;
20216   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20217   c_inhibit_evaluation_warnings = 0;
20218   /* Parse the template-argument-list itself.  */
20219   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20220       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20221     arguments = NULL_TREE;
20222   else
20223     arguments = cp_parser_template_argument_list (parser);
20224   /* Look for the `>' that ends the template-argument-list. If we find
20225      a '>>' instead, it's probably just a typo.  */
20226   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20227     {
20228       if (cxx_dialect != cxx98)
20229         {
20230           /* In C++0x, a `>>' in a template argument list or cast
20231              expression is considered to be two separate `>'
20232              tokens. So, change the current token to a `>', but don't
20233              consume it: it will be consumed later when the outer
20234              template argument list (or cast expression) is parsed.
20235              Note that this replacement of `>' for `>>' is necessary
20236              even if we are parsing tentatively: in the tentative
20237              case, after calling
20238              cp_parser_enclosed_template_argument_list we will always
20239              throw away all of the template arguments and the first
20240              closing `>', either because the template argument list
20241              was erroneous or because we are replacing those tokens
20242              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20243              not have been thrown away) is needed either to close an
20244              outer template argument list or to complete a new-style
20245              cast.  */
20246           cp_token *token = cp_lexer_peek_token (parser->lexer);
20247           token->type = CPP_GREATER;
20248         }
20249       else if (!saved_greater_than_is_operator_p)
20250         {
20251           /* If we're in a nested template argument list, the '>>' has
20252             to be a typo for '> >'. We emit the error message, but we
20253             continue parsing and we push a '>' as next token, so that
20254             the argument list will be parsed correctly.  Note that the
20255             global source location is still on the token before the
20256             '>>', so we need to say explicitly where we want it.  */
20257           cp_token *token = cp_lexer_peek_token (parser->lexer);
20258           error_at (token->location, "%<>>%> should be %<> >%> "
20259                     "within a nested template argument list");
20260
20261           token->type = CPP_GREATER;
20262         }
20263       else
20264         {
20265           /* If this is not a nested template argument list, the '>>'
20266             is a typo for '>'. Emit an error message and continue.
20267             Same deal about the token location, but here we can get it
20268             right by consuming the '>>' before issuing the diagnostic.  */
20269           cp_token *token = cp_lexer_consume_token (parser->lexer);
20270           error_at (token->location,
20271                     "spurious %<>>%>, use %<>%> to terminate "
20272                     "a template argument list");
20273         }
20274     }
20275   else
20276     cp_parser_skip_to_end_of_template_parameter_list (parser);
20277   /* The `>' token might be a greater-than operator again now.  */
20278   parser->greater_than_is_operator_p
20279     = saved_greater_than_is_operator_p;
20280   /* Restore the SAVED_SCOPE.  */
20281   parser->scope = saved_scope;
20282   parser->qualifying_scope = saved_qualifying_scope;
20283   parser->object_scope = saved_object_scope;
20284   cp_unevaluated_operand = saved_unevaluated_operand;
20285   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20286
20287   return arguments;
20288 }
20289
20290 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20291    arguments, or the body of the function have not yet been parsed,
20292    parse them now.  */
20293
20294 static void
20295 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20296 {
20297   timevar_push (TV_PARSE_INMETH);
20298   /* If this member is a template, get the underlying
20299      FUNCTION_DECL.  */
20300   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20301     member_function = DECL_TEMPLATE_RESULT (member_function);
20302
20303   /* There should not be any class definitions in progress at this
20304      point; the bodies of members are only parsed outside of all class
20305      definitions.  */
20306   gcc_assert (parser->num_classes_being_defined == 0);
20307   /* While we're parsing the member functions we might encounter more
20308      classes.  We want to handle them right away, but we don't want
20309      them getting mixed up with functions that are currently in the
20310      queue.  */
20311   push_unparsed_function_queues (parser);
20312
20313   /* Make sure that any template parameters are in scope.  */
20314   maybe_begin_member_template_processing (member_function);
20315
20316   /* If the body of the function has not yet been parsed, parse it
20317      now.  */
20318   if (DECL_PENDING_INLINE_P (member_function))
20319     {
20320       tree function_scope;
20321       cp_token_cache *tokens;
20322
20323       /* The function is no longer pending; we are processing it.  */
20324       tokens = DECL_PENDING_INLINE_INFO (member_function);
20325       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20326       DECL_PENDING_INLINE_P (member_function) = 0;
20327
20328       /* If this is a local class, enter the scope of the containing
20329          function.  */
20330       function_scope = current_function_decl;
20331       if (function_scope)
20332         push_function_context ();
20333
20334       /* Push the body of the function onto the lexer stack.  */
20335       cp_parser_push_lexer_for_tokens (parser, tokens);
20336
20337       /* Let the front end know that we going to be defining this
20338          function.  */
20339       start_preparsed_function (member_function, NULL_TREE,
20340                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20341
20342       /* Don't do access checking if it is a templated function.  */
20343       if (processing_template_decl)
20344         push_deferring_access_checks (dk_no_check);
20345
20346       /* Now, parse the body of the function.  */
20347       cp_parser_function_definition_after_declarator (parser,
20348                                                       /*inline_p=*/true);
20349
20350       if (processing_template_decl)
20351         pop_deferring_access_checks ();
20352
20353       /* Leave the scope of the containing function.  */
20354       if (function_scope)
20355         pop_function_context ();
20356       cp_parser_pop_lexer (parser);
20357     }
20358
20359   /* Remove any template parameters from the symbol table.  */
20360   maybe_end_member_template_processing ();
20361
20362   /* Restore the queue.  */
20363   pop_unparsed_function_queues (parser);
20364   timevar_pop (TV_PARSE_INMETH);
20365 }
20366
20367 /* If DECL contains any default args, remember it on the unparsed
20368    functions queue.  */
20369
20370 static void
20371 cp_parser_save_default_args (cp_parser* parser, tree decl)
20372 {
20373   tree probe;
20374
20375   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20376        probe;
20377        probe = TREE_CHAIN (probe))
20378     if (TREE_PURPOSE (probe))
20379       {
20380         cp_default_arg_entry *entry
20381           = VEC_safe_push (cp_default_arg_entry, gc,
20382                            unparsed_funs_with_default_args, NULL);
20383         entry->class_type = current_class_type;
20384         entry->decl = decl;
20385         break;
20386       }
20387 }
20388
20389 /* FN is a FUNCTION_DECL which may contains a parameter with an
20390    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20391    assumes that the current scope is the scope in which the default
20392    argument should be processed.  */
20393
20394 static void
20395 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20396 {
20397   bool saved_local_variables_forbidden_p;
20398   tree parm, parmdecl;
20399
20400   /* While we're parsing the default args, we might (due to the
20401      statement expression extension) encounter more classes.  We want
20402      to handle them right away, but we don't want them getting mixed
20403      up with default args that are currently in the queue.  */
20404   push_unparsed_function_queues (parser);
20405
20406   /* Local variable names (and the `this' keyword) may not appear
20407      in a default argument.  */
20408   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20409   parser->local_variables_forbidden_p = true;
20410
20411   push_defarg_context (fn);
20412
20413   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20414          parmdecl = DECL_ARGUMENTS (fn);
20415        parm && parm != void_list_node;
20416        parm = TREE_CHAIN (parm),
20417          parmdecl = DECL_CHAIN (parmdecl))
20418     {
20419       cp_token_cache *tokens;
20420       tree default_arg = TREE_PURPOSE (parm);
20421       tree parsed_arg;
20422       VEC(tree,gc) *insts;
20423       tree copy;
20424       unsigned ix;
20425
20426       if (!default_arg)
20427         continue;
20428
20429       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20430         /* This can happen for a friend declaration for a function
20431            already declared with default arguments.  */
20432         continue;
20433
20434        /* Push the saved tokens for the default argument onto the parser's
20435           lexer stack.  */
20436       tokens = DEFARG_TOKENS (default_arg);
20437       cp_parser_push_lexer_for_tokens (parser, tokens);
20438
20439       start_lambda_scope (parmdecl);
20440
20441       /* Parse the assignment-expression.  */
20442       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20443       if (parsed_arg == error_mark_node)
20444         {
20445           cp_parser_pop_lexer (parser);
20446           continue;
20447         }
20448
20449       if (!processing_template_decl)
20450         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20451
20452       TREE_PURPOSE (parm) = parsed_arg;
20453
20454       /* Update any instantiations we've already created.  */
20455       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20456            VEC_iterate (tree, insts, ix, copy); ix++)
20457         TREE_PURPOSE (copy) = parsed_arg;
20458
20459       finish_lambda_scope ();
20460
20461       /* If the token stream has not been completely used up, then
20462          there was extra junk after the end of the default
20463          argument.  */
20464       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20465         cp_parser_error (parser, "expected %<,%>");
20466
20467       /* Revert to the main lexer.  */
20468       cp_parser_pop_lexer (parser);
20469     }
20470
20471   pop_defarg_context ();
20472
20473   /* Make sure no default arg is missing.  */
20474   check_default_args (fn);
20475
20476   /* Restore the state of local_variables_forbidden_p.  */
20477   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20478
20479   /* Restore the queue.  */
20480   pop_unparsed_function_queues (parser);
20481 }
20482
20483 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20484    either a TYPE or an expression, depending on the form of the
20485    input.  The KEYWORD indicates which kind of expression we have
20486    encountered.  */
20487
20488 static tree
20489 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20490 {
20491   tree expr = NULL_TREE;
20492   const char *saved_message;
20493   char *tmp;
20494   bool saved_integral_constant_expression_p;
20495   bool saved_non_integral_constant_expression_p;
20496   bool pack_expansion_p = false;
20497
20498   /* Types cannot be defined in a `sizeof' expression.  Save away the
20499      old message.  */
20500   saved_message = parser->type_definition_forbidden_message;
20501   /* And create the new one.  */
20502   tmp = concat ("types may not be defined in %<",
20503                 IDENTIFIER_POINTER (ridpointers[keyword]),
20504                 "%> expressions", NULL);
20505   parser->type_definition_forbidden_message = tmp;
20506
20507   /* The restrictions on constant-expressions do not apply inside
20508      sizeof expressions.  */
20509   saved_integral_constant_expression_p
20510     = parser->integral_constant_expression_p;
20511   saved_non_integral_constant_expression_p
20512     = parser->non_integral_constant_expression_p;
20513   parser->integral_constant_expression_p = false;
20514
20515   /* If it's a `...', then we are computing the length of a parameter
20516      pack.  */
20517   if (keyword == RID_SIZEOF
20518       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20519     {
20520       /* Consume the `...'.  */
20521       cp_lexer_consume_token (parser->lexer);
20522       maybe_warn_variadic_templates ();
20523
20524       /* Note that this is an expansion.  */
20525       pack_expansion_p = true;
20526     }
20527
20528   /* Do not actually evaluate the expression.  */
20529   ++cp_unevaluated_operand;
20530   ++c_inhibit_evaluation_warnings;
20531   /* If it's a `(', then we might be looking at the type-id
20532      construction.  */
20533   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20534     {
20535       tree type;
20536       bool saved_in_type_id_in_expr_p;
20537
20538       /* We can't be sure yet whether we're looking at a type-id or an
20539          expression.  */
20540       cp_parser_parse_tentatively (parser);
20541       /* Consume the `('.  */
20542       cp_lexer_consume_token (parser->lexer);
20543       /* Parse the type-id.  */
20544       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20545       parser->in_type_id_in_expr_p = true;
20546       type = cp_parser_type_id (parser);
20547       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20548       /* Now, look for the trailing `)'.  */
20549       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20550       /* If all went well, then we're done.  */
20551       if (cp_parser_parse_definitely (parser))
20552         {
20553           cp_decl_specifier_seq decl_specs;
20554
20555           /* Build a trivial decl-specifier-seq.  */
20556           clear_decl_specs (&decl_specs);
20557           decl_specs.type = type;
20558
20559           /* Call grokdeclarator to figure out what type this is.  */
20560           expr = grokdeclarator (NULL,
20561                                  &decl_specs,
20562                                  TYPENAME,
20563                                  /*initialized=*/0,
20564                                  /*attrlist=*/NULL);
20565         }
20566     }
20567
20568   /* If the type-id production did not work out, then we must be
20569      looking at the unary-expression production.  */
20570   if (!expr)
20571     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20572                                        /*cast_p=*/false, NULL);
20573
20574   if (pack_expansion_p)
20575     /* Build a pack expansion. */
20576     expr = make_pack_expansion (expr);
20577
20578   /* Go back to evaluating expressions.  */
20579   --cp_unevaluated_operand;
20580   --c_inhibit_evaluation_warnings;
20581
20582   /* Free the message we created.  */
20583   free (tmp);
20584   /* And restore the old one.  */
20585   parser->type_definition_forbidden_message = saved_message;
20586   parser->integral_constant_expression_p
20587     = saved_integral_constant_expression_p;
20588   parser->non_integral_constant_expression_p
20589     = saved_non_integral_constant_expression_p;
20590
20591   return expr;
20592 }
20593
20594 /* If the current declaration has no declarator, return true.  */
20595
20596 static bool
20597 cp_parser_declares_only_class_p (cp_parser *parser)
20598 {
20599   /* If the next token is a `;' or a `,' then there is no
20600      declarator.  */
20601   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20602           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20603 }
20604
20605 /* Update the DECL_SPECS to reflect the storage class indicated by
20606    KEYWORD.  */
20607
20608 static void
20609 cp_parser_set_storage_class (cp_parser *parser,
20610                              cp_decl_specifier_seq *decl_specs,
20611                              enum rid keyword,
20612                              location_t location)
20613 {
20614   cp_storage_class storage_class;
20615
20616   if (parser->in_unbraced_linkage_specification_p)
20617     {
20618       error_at (location, "invalid use of %qD in linkage specification",
20619                 ridpointers[keyword]);
20620       return;
20621     }
20622   else if (decl_specs->storage_class != sc_none)
20623     {
20624       decl_specs->conflicting_specifiers_p = true;
20625       return;
20626     }
20627
20628   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20629       && decl_specs->specs[(int) ds_thread])
20630     {
20631       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20632       decl_specs->specs[(int) ds_thread] = 0;
20633     }
20634
20635   switch (keyword)
20636     {
20637     case RID_AUTO:
20638       storage_class = sc_auto;
20639       break;
20640     case RID_REGISTER:
20641       storage_class = sc_register;
20642       break;
20643     case RID_STATIC:
20644       storage_class = sc_static;
20645       break;
20646     case RID_EXTERN:
20647       storage_class = sc_extern;
20648       break;
20649     case RID_MUTABLE:
20650       storage_class = sc_mutable;
20651       break;
20652     default:
20653       gcc_unreachable ();
20654     }
20655   decl_specs->storage_class = storage_class;
20656
20657   /* A storage class specifier cannot be applied alongside a typedef 
20658      specifier. If there is a typedef specifier present then set 
20659      conflicting_specifiers_p which will trigger an error later
20660      on in grokdeclarator. */
20661   if (decl_specs->specs[(int)ds_typedef])
20662     decl_specs->conflicting_specifiers_p = true;
20663 }
20664
20665 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20666    is true, the type is a user-defined type; otherwise it is a
20667    built-in type specified by a keyword.  */
20668
20669 static void
20670 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20671                               tree type_spec,
20672                               location_t location,
20673                               bool user_defined_p)
20674 {
20675   decl_specs->any_specifiers_p = true;
20676
20677   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20678      (with, for example, in "typedef int wchar_t;") we remember that
20679      this is what happened.  In system headers, we ignore these
20680      declarations so that G++ can work with system headers that are not
20681      C++-safe.  */
20682   if (decl_specs->specs[(int) ds_typedef]
20683       && !user_defined_p
20684       && (type_spec == boolean_type_node
20685           || type_spec == char16_type_node
20686           || type_spec == char32_type_node
20687           || type_spec == wchar_type_node)
20688       && (decl_specs->type
20689           || decl_specs->specs[(int) ds_long]
20690           || decl_specs->specs[(int) ds_short]
20691           || decl_specs->specs[(int) ds_unsigned]
20692           || decl_specs->specs[(int) ds_signed]))
20693     {
20694       decl_specs->redefined_builtin_type = type_spec;
20695       if (!decl_specs->type)
20696         {
20697           decl_specs->type = type_spec;
20698           decl_specs->user_defined_type_p = false;
20699           decl_specs->type_location = location;
20700         }
20701     }
20702   else if (decl_specs->type)
20703     decl_specs->multiple_types_p = true;
20704   else
20705     {
20706       decl_specs->type = type_spec;
20707       decl_specs->user_defined_type_p = user_defined_p;
20708       decl_specs->redefined_builtin_type = NULL_TREE;
20709       decl_specs->type_location = location;
20710     }
20711 }
20712
20713 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20714    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20715
20716 static bool
20717 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20718 {
20719   return decl_specifiers->specs[(int) ds_friend] != 0;
20720 }
20721
20722 /* Issue an error message indicating that TOKEN_DESC was expected.
20723    If KEYWORD is true, it indicated this function is called by
20724    cp_parser_require_keword and the required token can only be
20725    a indicated keyword. */
20726
20727 static void
20728 cp_parser_required_error (cp_parser *parser,
20729                           required_token token_desc,
20730                           bool keyword)
20731 {
20732   switch (token_desc)
20733     {
20734       case RT_NEW:
20735         cp_parser_error (parser, "expected %<new%>");
20736         return;
20737       case RT_DELETE:
20738         cp_parser_error (parser, "expected %<delete%>");
20739         return;
20740       case RT_RETURN:
20741         cp_parser_error (parser, "expected %<return%>");
20742         return;
20743       case RT_WHILE:
20744         cp_parser_error (parser, "expected %<while%>");
20745         return;
20746       case RT_EXTERN:
20747         cp_parser_error (parser, "expected %<extern%>");
20748         return;
20749       case RT_STATIC_ASSERT:
20750         cp_parser_error (parser, "expected %<static_assert%>");
20751         return;
20752       case RT_DECLTYPE:
20753         cp_parser_error (parser, "expected %<decltype%>");
20754         return;
20755       case RT_OPERATOR:
20756         cp_parser_error (parser, "expected %<operator%>");
20757         return;
20758       case RT_CLASS:
20759         cp_parser_error (parser, "expected %<class%>");
20760         return;
20761       case RT_TEMPLATE:
20762         cp_parser_error (parser, "expected %<template%>");
20763         return;
20764       case RT_NAMESPACE:
20765         cp_parser_error (parser, "expected %<namespace%>");
20766         return;
20767       case RT_USING:
20768         cp_parser_error (parser, "expected %<using%>");
20769         return;
20770       case RT_ASM:
20771         cp_parser_error (parser, "expected %<asm%>");
20772         return;
20773       case RT_TRY:
20774         cp_parser_error (parser, "expected %<try%>");
20775         return;
20776       case RT_CATCH:
20777         cp_parser_error (parser, "expected %<catch%>");
20778         return;
20779       case RT_THROW:
20780         cp_parser_error (parser, "expected %<throw%>");
20781         return;
20782       case RT_LABEL:
20783         cp_parser_error (parser, "expected %<__label__%>");
20784         return;
20785       case RT_AT_TRY:
20786         cp_parser_error (parser, "expected %<@try%>");
20787         return;
20788       case RT_AT_SYNCHRONIZED:
20789         cp_parser_error (parser, "expected %<@synchronized%>");
20790         return;
20791       case RT_AT_THROW:
20792         cp_parser_error (parser, "expected %<@throw%>");
20793         return;
20794       default:
20795         break;
20796     }
20797   if (!keyword)
20798     {
20799       switch (token_desc)
20800         {
20801           case RT_SEMICOLON:
20802             cp_parser_error (parser, "expected %<;%>");
20803             return;
20804           case RT_OPEN_PAREN:
20805             cp_parser_error (parser, "expected %<(%>");
20806             return;
20807           case RT_CLOSE_BRACE:
20808             cp_parser_error (parser, "expected %<}%>");
20809             return;
20810           case RT_OPEN_BRACE:
20811             cp_parser_error (parser, "expected %<{%>");
20812             return;
20813           case RT_CLOSE_SQUARE:
20814             cp_parser_error (parser, "expected %<]%>");
20815             return;
20816           case RT_OPEN_SQUARE:
20817             cp_parser_error (parser, "expected %<[%>");
20818             return;
20819           case RT_COMMA:
20820             cp_parser_error (parser, "expected %<,%>");
20821             return;
20822           case RT_SCOPE:
20823             cp_parser_error (parser, "expected %<::%>");
20824             return;
20825           case RT_LESS:
20826             cp_parser_error (parser, "expected %<<%>");
20827             return;
20828           case RT_GREATER:
20829             cp_parser_error (parser, "expected %<>%>");
20830             return;
20831           case RT_EQ:
20832             cp_parser_error (parser, "expected %<=%>");
20833             return;
20834           case RT_ELLIPSIS:
20835             cp_parser_error (parser, "expected %<...%>");
20836             return;
20837           case RT_MULT:
20838             cp_parser_error (parser, "expected %<*%>");
20839             return;
20840           case RT_COMPL:
20841             cp_parser_error (parser, "expected %<~%>");
20842             return;
20843           case RT_COLON:
20844             cp_parser_error (parser, "expected %<:%>");
20845             return;
20846           case RT_COLON_SCOPE:
20847             cp_parser_error (parser, "expected %<:%> or %<::%>");
20848             return;
20849           case RT_CLOSE_PAREN:
20850             cp_parser_error (parser, "expected %<)%>");
20851             return;
20852           case RT_COMMA_CLOSE_PAREN:
20853             cp_parser_error (parser, "expected %<,%> or %<)%>");
20854             return;
20855           case RT_PRAGMA_EOL:
20856             cp_parser_error (parser, "expected end of line");
20857             return;
20858           case RT_NAME:
20859             cp_parser_error (parser, "expected identifier");
20860             return;
20861           case RT_SELECT:
20862             cp_parser_error (parser, "expected selection-statement");
20863             return;
20864           case RT_INTERATION:
20865             cp_parser_error (parser, "expected iteration-statement");
20866             return;
20867           case RT_JUMP:
20868             cp_parser_error (parser, "expected jump-statement");
20869             return;
20870           case RT_CLASS_KEY:
20871             cp_parser_error (parser, "expected class-key");
20872             return;
20873           case RT_CLASS_TYPENAME_TEMPLATE:
20874             cp_parser_error (parser,
20875                  "expected %<class%>, %<typename%>, or %<template%>");
20876             return;
20877           default:
20878             gcc_unreachable ();
20879         }
20880     }
20881   else
20882     gcc_unreachable ();
20883 }
20884
20885
20886
20887 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20888    issue an error message indicating that TOKEN_DESC was expected.
20889
20890    Returns the token consumed, if the token had the appropriate type.
20891    Otherwise, returns NULL.  */
20892
20893 static cp_token *
20894 cp_parser_require (cp_parser* parser,
20895                    enum cpp_ttype type,
20896                    required_token token_desc)
20897 {
20898   if (cp_lexer_next_token_is (parser->lexer, type))
20899     return cp_lexer_consume_token (parser->lexer);
20900   else
20901     {
20902       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20903       if (!cp_parser_simulate_error (parser))
20904         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20905       return NULL;
20906     }
20907 }
20908
20909 /* An error message is produced if the next token is not '>'.
20910    All further tokens are skipped until the desired token is
20911    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20912
20913 static void
20914 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20915 {
20916   /* Current level of '< ... >'.  */
20917   unsigned level = 0;
20918   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20919   unsigned nesting_depth = 0;
20920
20921   /* Are we ready, yet?  If not, issue error message.  */
20922   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20923     return;
20924
20925   /* Skip tokens until the desired token is found.  */
20926   while (true)
20927     {
20928       /* Peek at the next token.  */
20929       switch (cp_lexer_peek_token (parser->lexer)->type)
20930         {
20931         case CPP_LESS:
20932           if (!nesting_depth)
20933             ++level;
20934           break;
20935
20936         case CPP_RSHIFT:
20937           if (cxx_dialect == cxx98)
20938             /* C++0x views the `>>' operator as two `>' tokens, but
20939                C++98 does not. */
20940             break;
20941           else if (!nesting_depth && level-- == 0)
20942             {
20943               /* We've hit a `>>' where the first `>' closes the
20944                  template argument list, and the second `>' is
20945                  spurious.  Just consume the `>>' and stop; we've
20946                  already produced at least one error.  */
20947               cp_lexer_consume_token (parser->lexer);
20948               return;
20949             }
20950           /* Fall through for C++0x, so we handle the second `>' in
20951              the `>>'.  */
20952
20953         case CPP_GREATER:
20954           if (!nesting_depth && level-- == 0)
20955             {
20956               /* We've reached the token we want, consume it and stop.  */
20957               cp_lexer_consume_token (parser->lexer);
20958               return;
20959             }
20960           break;
20961
20962         case CPP_OPEN_PAREN:
20963         case CPP_OPEN_SQUARE:
20964           ++nesting_depth;
20965           break;
20966
20967         case CPP_CLOSE_PAREN:
20968         case CPP_CLOSE_SQUARE:
20969           if (nesting_depth-- == 0)
20970             return;
20971           break;
20972
20973         case CPP_EOF:
20974         case CPP_PRAGMA_EOL:
20975         case CPP_SEMICOLON:
20976         case CPP_OPEN_BRACE:
20977         case CPP_CLOSE_BRACE:
20978           /* The '>' was probably forgotten, don't look further.  */
20979           return;
20980
20981         default:
20982           break;
20983         }
20984
20985       /* Consume this token.  */
20986       cp_lexer_consume_token (parser->lexer);
20987     }
20988 }
20989
20990 /* If the next token is the indicated keyword, consume it.  Otherwise,
20991    issue an error message indicating that TOKEN_DESC was expected.
20992
20993    Returns the token consumed, if the token had the appropriate type.
20994    Otherwise, returns NULL.  */
20995
20996 static cp_token *
20997 cp_parser_require_keyword (cp_parser* parser,
20998                            enum rid keyword,
20999                            required_token token_desc)
21000 {
21001   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21002
21003   if (token && token->keyword != keyword)
21004     {
21005       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21006       return NULL;
21007     }
21008
21009   return token;
21010 }
21011
21012 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21013    function-definition.  */
21014
21015 static bool
21016 cp_parser_token_starts_function_definition_p (cp_token* token)
21017 {
21018   return (/* An ordinary function-body begins with an `{'.  */
21019           token->type == CPP_OPEN_BRACE
21020           /* A ctor-initializer begins with a `:'.  */
21021           || token->type == CPP_COLON
21022           /* A function-try-block begins with `try'.  */
21023           || token->keyword == RID_TRY
21024           /* The named return value extension begins with `return'.  */
21025           || token->keyword == RID_RETURN);
21026 }
21027
21028 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21029    definition.  */
21030
21031 static bool
21032 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21033 {
21034   cp_token *token;
21035
21036   token = cp_lexer_peek_token (parser->lexer);
21037   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21038 }
21039
21040 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21041    C++0x) ending a template-argument.  */
21042
21043 static bool
21044 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21045 {
21046   cp_token *token;
21047
21048   token = cp_lexer_peek_token (parser->lexer);
21049   return (token->type == CPP_COMMA 
21050           || token->type == CPP_GREATER
21051           || token->type == CPP_ELLIPSIS
21052           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21053 }
21054
21055 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21056    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21057
21058 static bool
21059 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21060                                                      size_t n)
21061 {
21062   cp_token *token;
21063
21064   token = cp_lexer_peek_nth_token (parser->lexer, n);
21065   if (token->type == CPP_LESS)
21066     return true;
21067   /* Check for the sequence `<::' in the original code. It would be lexed as
21068      `[:', where `[' is a digraph, and there is no whitespace before
21069      `:'.  */
21070   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21071     {
21072       cp_token *token2;
21073       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21074       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21075         return true;
21076     }
21077   return false;
21078 }
21079
21080 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21081    or none_type otherwise.  */
21082
21083 static enum tag_types
21084 cp_parser_token_is_class_key (cp_token* token)
21085 {
21086   switch (token->keyword)
21087     {
21088     case RID_CLASS:
21089       return class_type;
21090     case RID_STRUCT:
21091       return record_type;
21092     case RID_UNION:
21093       return union_type;
21094
21095     default:
21096       return none_type;
21097     }
21098 }
21099
21100 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21101
21102 static void
21103 cp_parser_check_class_key (enum tag_types class_key, tree type)
21104 {
21105   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21106     permerror (input_location, "%qs tag used in naming %q#T",
21107             class_key == union_type ? "union"
21108              : class_key == record_type ? "struct" : "class",
21109              type);
21110 }
21111
21112 /* Issue an error message if DECL is redeclared with different
21113    access than its original declaration [class.access.spec/3].
21114    This applies to nested classes and nested class templates.
21115    [class.mem/1].  */
21116
21117 static void
21118 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21119 {
21120   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21121     return;
21122
21123   if ((TREE_PRIVATE (decl)
21124        != (current_access_specifier == access_private_node))
21125       || (TREE_PROTECTED (decl)
21126           != (current_access_specifier == access_protected_node)))
21127     error_at (location, "%qD redeclared with different access", decl);
21128 }
21129
21130 /* Look for the `template' keyword, as a syntactic disambiguator.
21131    Return TRUE iff it is present, in which case it will be
21132    consumed.  */
21133
21134 static bool
21135 cp_parser_optional_template_keyword (cp_parser *parser)
21136 {
21137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21138     {
21139       /* The `template' keyword can only be used within templates;
21140          outside templates the parser can always figure out what is a
21141          template and what is not.  */
21142       if (!processing_template_decl)
21143         {
21144           cp_token *token = cp_lexer_peek_token (parser->lexer);
21145           error_at (token->location,
21146                     "%<template%> (as a disambiguator) is only allowed "
21147                     "within templates");
21148           /* If this part of the token stream is rescanned, the same
21149              error message would be generated.  So, we purge the token
21150              from the stream.  */
21151           cp_lexer_purge_token (parser->lexer);
21152           return false;
21153         }
21154       else
21155         {
21156           /* Consume the `template' keyword.  */
21157           cp_lexer_consume_token (parser->lexer);
21158           return true;
21159         }
21160     }
21161
21162   return false;
21163 }
21164
21165 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21166    set PARSER->SCOPE, and perform other related actions.  */
21167
21168 static void
21169 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21170 {
21171   int i;
21172   struct tree_check *check_value;
21173   deferred_access_check *chk;
21174   VEC (deferred_access_check,gc) *checks;
21175
21176   /* Get the stored value.  */
21177   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21178   /* Perform any access checks that were deferred.  */
21179   checks = check_value->checks;
21180   if (checks)
21181     {
21182       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21183         perform_or_defer_access_check (chk->binfo,
21184                                        chk->decl,
21185                                        chk->diag_decl);
21186     }
21187   /* Set the scope from the stored value.  */
21188   parser->scope = check_value->value;
21189   parser->qualifying_scope = check_value->qualifying_scope;
21190   parser->object_scope = NULL_TREE;
21191 }
21192
21193 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21194    encounter the end of a block before what we were looking for.  */
21195
21196 static bool
21197 cp_parser_cache_group (cp_parser *parser,
21198                        enum cpp_ttype end,
21199                        unsigned depth)
21200 {
21201   while (true)
21202     {
21203       cp_token *token = cp_lexer_peek_token (parser->lexer);
21204
21205       /* Abort a parenthesized expression if we encounter a semicolon.  */
21206       if ((end == CPP_CLOSE_PAREN || depth == 0)
21207           && token->type == CPP_SEMICOLON)
21208         return true;
21209       /* If we've reached the end of the file, stop.  */
21210       if (token->type == CPP_EOF
21211           || (end != CPP_PRAGMA_EOL
21212               && token->type == CPP_PRAGMA_EOL))
21213         return true;
21214       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21215         /* We've hit the end of an enclosing block, so there's been some
21216            kind of syntax error.  */
21217         return true;
21218
21219       /* Consume the token.  */
21220       cp_lexer_consume_token (parser->lexer);
21221       /* See if it starts a new group.  */
21222       if (token->type == CPP_OPEN_BRACE)
21223         {
21224           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21225           /* In theory this should probably check end == '}', but
21226              cp_parser_save_member_function_body needs it to exit
21227              after either '}' or ')' when called with ')'.  */
21228           if (depth == 0)
21229             return false;
21230         }
21231       else if (token->type == CPP_OPEN_PAREN)
21232         {
21233           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21234           if (depth == 0 && end == CPP_CLOSE_PAREN)
21235             return false;
21236         }
21237       else if (token->type == CPP_PRAGMA)
21238         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21239       else if (token->type == end)
21240         return false;
21241     }
21242 }
21243
21244 /* Begin parsing tentatively.  We always save tokens while parsing
21245    tentatively so that if the tentative parsing fails we can restore the
21246    tokens.  */
21247
21248 static void
21249 cp_parser_parse_tentatively (cp_parser* parser)
21250 {
21251   /* Enter a new parsing context.  */
21252   parser->context = cp_parser_context_new (parser->context);
21253   /* Begin saving tokens.  */
21254   cp_lexer_save_tokens (parser->lexer);
21255   /* In order to avoid repetitive access control error messages,
21256      access checks are queued up until we are no longer parsing
21257      tentatively.  */
21258   push_deferring_access_checks (dk_deferred);
21259 }
21260
21261 /* Commit to the currently active tentative parse.  */
21262
21263 static void
21264 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21265 {
21266   cp_parser_context *context;
21267   cp_lexer *lexer;
21268
21269   /* Mark all of the levels as committed.  */
21270   lexer = parser->lexer;
21271   for (context = parser->context; context->next; context = context->next)
21272     {
21273       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21274         break;
21275       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21276       while (!cp_lexer_saving_tokens (lexer))
21277         lexer = lexer->next;
21278       cp_lexer_commit_tokens (lexer);
21279     }
21280 }
21281
21282 /* Abort the currently active tentative parse.  All consumed tokens
21283    will be rolled back, and no diagnostics will be issued.  */
21284
21285 static void
21286 cp_parser_abort_tentative_parse (cp_parser* parser)
21287 {
21288   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21289               || errorcount > 0);
21290   cp_parser_simulate_error (parser);
21291   /* Now, pretend that we want to see if the construct was
21292      successfully parsed.  */
21293   cp_parser_parse_definitely (parser);
21294 }
21295
21296 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21297    token stream.  Otherwise, commit to the tokens we have consumed.
21298    Returns true if no error occurred; false otherwise.  */
21299
21300 static bool
21301 cp_parser_parse_definitely (cp_parser* parser)
21302 {
21303   bool error_occurred;
21304   cp_parser_context *context;
21305
21306   /* Remember whether or not an error occurred, since we are about to
21307      destroy that information.  */
21308   error_occurred = cp_parser_error_occurred (parser);
21309   /* Remove the topmost context from the stack.  */
21310   context = parser->context;
21311   parser->context = context->next;
21312   /* If no parse errors occurred, commit to the tentative parse.  */
21313   if (!error_occurred)
21314     {
21315       /* Commit to the tokens read tentatively, unless that was
21316          already done.  */
21317       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21318         cp_lexer_commit_tokens (parser->lexer);
21319
21320       pop_to_parent_deferring_access_checks ();
21321     }
21322   /* Otherwise, if errors occurred, roll back our state so that things
21323      are just as they were before we began the tentative parse.  */
21324   else
21325     {
21326       cp_lexer_rollback_tokens (parser->lexer);
21327       pop_deferring_access_checks ();
21328     }
21329   /* Add the context to the front of the free list.  */
21330   context->next = cp_parser_context_free_list;
21331   cp_parser_context_free_list = context;
21332
21333   return !error_occurred;
21334 }
21335
21336 /* Returns true if we are parsing tentatively and are not committed to
21337    this tentative parse.  */
21338
21339 static bool
21340 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21341 {
21342   return (cp_parser_parsing_tentatively (parser)
21343           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21344 }
21345
21346 /* Returns nonzero iff an error has occurred during the most recent
21347    tentative parse.  */
21348
21349 static bool
21350 cp_parser_error_occurred (cp_parser* parser)
21351 {
21352   return (cp_parser_parsing_tentatively (parser)
21353           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21354 }
21355
21356 /* Returns nonzero if GNU extensions are allowed.  */
21357
21358 static bool
21359 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21360 {
21361   return parser->allow_gnu_extensions_p;
21362 }
21363 \f
21364 /* Objective-C++ Productions */
21365
21366
21367 /* Parse an Objective-C expression, which feeds into a primary-expression
21368    above.
21369
21370    objc-expression:
21371      objc-message-expression
21372      objc-string-literal
21373      objc-encode-expression
21374      objc-protocol-expression
21375      objc-selector-expression
21376
21377   Returns a tree representation of the expression.  */
21378
21379 static tree
21380 cp_parser_objc_expression (cp_parser* parser)
21381 {
21382   /* Try to figure out what kind of declaration is present.  */
21383   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21384
21385   switch (kwd->type)
21386     {
21387     case CPP_OPEN_SQUARE:
21388       return cp_parser_objc_message_expression (parser);
21389
21390     case CPP_OBJC_STRING:
21391       kwd = cp_lexer_consume_token (parser->lexer);
21392       return objc_build_string_object (kwd->u.value);
21393
21394     case CPP_KEYWORD:
21395       switch (kwd->keyword)
21396         {
21397         case RID_AT_ENCODE:
21398           return cp_parser_objc_encode_expression (parser);
21399
21400         case RID_AT_PROTOCOL:
21401           return cp_parser_objc_protocol_expression (parser);
21402
21403         case RID_AT_SELECTOR:
21404           return cp_parser_objc_selector_expression (parser);
21405
21406         default:
21407           break;
21408         }
21409     default:
21410       error_at (kwd->location,
21411                 "misplaced %<@%D%> Objective-C++ construct",
21412                 kwd->u.value);
21413       cp_parser_skip_to_end_of_block_or_statement (parser);
21414     }
21415
21416   return error_mark_node;
21417 }
21418
21419 /* Parse an Objective-C message expression.
21420
21421    objc-message-expression:
21422      [ objc-message-receiver objc-message-args ]
21423
21424    Returns a representation of an Objective-C message.  */
21425
21426 static tree
21427 cp_parser_objc_message_expression (cp_parser* parser)
21428 {
21429   tree receiver, messageargs;
21430
21431   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21432   receiver = cp_parser_objc_message_receiver (parser);
21433   messageargs = cp_parser_objc_message_args (parser);
21434   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21435
21436   return objc_build_message_expr (receiver, messageargs);
21437 }
21438
21439 /* Parse an objc-message-receiver.
21440
21441    objc-message-receiver:
21442      expression
21443      simple-type-specifier
21444
21445   Returns a representation of the type or expression.  */
21446
21447 static tree
21448 cp_parser_objc_message_receiver (cp_parser* parser)
21449 {
21450   tree rcv;
21451
21452   /* An Objective-C message receiver may be either (1) a type
21453      or (2) an expression.  */
21454   cp_parser_parse_tentatively (parser);
21455   rcv = cp_parser_expression (parser, false, NULL);
21456
21457   if (cp_parser_parse_definitely (parser))
21458     return rcv;
21459
21460   rcv = cp_parser_simple_type_specifier (parser,
21461                                          /*decl_specs=*/NULL,
21462                                          CP_PARSER_FLAGS_NONE);
21463
21464   return objc_get_class_reference (rcv);
21465 }
21466
21467 /* Parse the arguments and selectors comprising an Objective-C message.
21468
21469    objc-message-args:
21470      objc-selector
21471      objc-selector-args
21472      objc-selector-args , objc-comma-args
21473
21474    objc-selector-args:
21475      objc-selector [opt] : assignment-expression
21476      objc-selector-args objc-selector [opt] : assignment-expression
21477
21478    objc-comma-args:
21479      assignment-expression
21480      objc-comma-args , assignment-expression
21481
21482    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21483    selector arguments and TREE_VALUE containing a list of comma
21484    arguments.  */
21485
21486 static tree
21487 cp_parser_objc_message_args (cp_parser* parser)
21488 {
21489   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21490   bool maybe_unary_selector_p = true;
21491   cp_token *token = cp_lexer_peek_token (parser->lexer);
21492
21493   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21494     {
21495       tree selector = NULL_TREE, arg;
21496
21497       if (token->type != CPP_COLON)
21498         selector = cp_parser_objc_selector (parser);
21499
21500       /* Detect if we have a unary selector.  */
21501       if (maybe_unary_selector_p
21502           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21503         return build_tree_list (selector, NULL_TREE);
21504
21505       maybe_unary_selector_p = false;
21506       cp_parser_require (parser, CPP_COLON, RT_COLON);
21507       arg = cp_parser_assignment_expression (parser, false, NULL);
21508
21509       sel_args
21510         = chainon (sel_args,
21511                    build_tree_list (selector, arg));
21512
21513       token = cp_lexer_peek_token (parser->lexer);
21514     }
21515
21516   /* Handle non-selector arguments, if any. */
21517   while (token->type == CPP_COMMA)
21518     {
21519       tree arg;
21520
21521       cp_lexer_consume_token (parser->lexer);
21522       arg = cp_parser_assignment_expression (parser, false, NULL);
21523
21524       addl_args
21525         = chainon (addl_args,
21526                    build_tree_list (NULL_TREE, arg));
21527
21528       token = cp_lexer_peek_token (parser->lexer);
21529     }
21530
21531   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21532     {
21533       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21534       return build_tree_list (error_mark_node, error_mark_node);
21535     }
21536
21537   return build_tree_list (sel_args, addl_args);
21538 }
21539
21540 /* Parse an Objective-C encode expression.
21541
21542    objc-encode-expression:
21543      @encode objc-typename
21544
21545    Returns an encoded representation of the type argument.  */
21546
21547 static tree
21548 cp_parser_objc_encode_expression (cp_parser* parser)
21549 {
21550   tree type;
21551   cp_token *token;
21552
21553   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21554   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21555   token = cp_lexer_peek_token (parser->lexer);
21556   type = complete_type (cp_parser_type_id (parser));
21557   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21558
21559   if (!type)
21560     {
21561       error_at (token->location, 
21562                 "%<@encode%> must specify a type as an argument");
21563       return error_mark_node;
21564     }
21565
21566   /* This happens if we find @encode(T) (where T is a template
21567      typename or something dependent on a template typename) when
21568      parsing a template.  In that case, we can't compile it
21569      immediately, but we rather create an AT_ENCODE_EXPR which will
21570      need to be instantiated when the template is used.
21571   */
21572   if (dependent_type_p (type))
21573     {
21574       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21575       TREE_READONLY (value) = 1;
21576       return value;
21577     }
21578
21579   return objc_build_encode_expr (type);
21580 }
21581
21582 /* Parse an Objective-C @defs expression.  */
21583
21584 static tree
21585 cp_parser_objc_defs_expression (cp_parser *parser)
21586 {
21587   tree name;
21588
21589   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21590   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21591   name = cp_parser_identifier (parser);
21592   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21593
21594   return objc_get_class_ivars (name);
21595 }
21596
21597 /* Parse an Objective-C protocol expression.
21598
21599   objc-protocol-expression:
21600     @protocol ( identifier )
21601
21602   Returns a representation of the protocol expression.  */
21603
21604 static tree
21605 cp_parser_objc_protocol_expression (cp_parser* parser)
21606 {
21607   tree proto;
21608
21609   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21610   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21611   proto = cp_parser_identifier (parser);
21612   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21613
21614   return objc_build_protocol_expr (proto);
21615 }
21616
21617 /* Parse an Objective-C selector expression.
21618
21619    objc-selector-expression:
21620      @selector ( objc-method-signature )
21621
21622    objc-method-signature:
21623      objc-selector
21624      objc-selector-seq
21625
21626    objc-selector-seq:
21627      objc-selector :
21628      objc-selector-seq objc-selector :
21629
21630   Returns a representation of the method selector.  */
21631
21632 static tree
21633 cp_parser_objc_selector_expression (cp_parser* parser)
21634 {
21635   tree sel_seq = NULL_TREE;
21636   bool maybe_unary_selector_p = true;
21637   cp_token *token;
21638   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21639
21640   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21641   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21642   token = cp_lexer_peek_token (parser->lexer);
21643
21644   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21645          || token->type == CPP_SCOPE)
21646     {
21647       tree selector = NULL_TREE;
21648
21649       if (token->type != CPP_COLON
21650           || token->type == CPP_SCOPE)
21651         selector = cp_parser_objc_selector (parser);
21652
21653       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21654           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21655         {
21656           /* Detect if we have a unary selector.  */
21657           if (maybe_unary_selector_p)
21658             {
21659               sel_seq = selector;
21660               goto finish_selector;
21661             }
21662           else
21663             {
21664               cp_parser_error (parser, "expected %<:%>");
21665             }
21666         }
21667       maybe_unary_selector_p = false;
21668       token = cp_lexer_consume_token (parser->lexer);
21669
21670       if (token->type == CPP_SCOPE)
21671         {
21672           sel_seq
21673             = chainon (sel_seq,
21674                        build_tree_list (selector, NULL_TREE));
21675           sel_seq
21676             = chainon (sel_seq,
21677                        build_tree_list (NULL_TREE, NULL_TREE));
21678         }
21679       else
21680         sel_seq
21681           = chainon (sel_seq,
21682                      build_tree_list (selector, NULL_TREE));
21683
21684       token = cp_lexer_peek_token (parser->lexer);
21685     }
21686
21687  finish_selector:
21688   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21689
21690   return objc_build_selector_expr (loc, sel_seq);
21691 }
21692
21693 /* Parse a list of identifiers.
21694
21695    objc-identifier-list:
21696      identifier
21697      objc-identifier-list , identifier
21698
21699    Returns a TREE_LIST of identifier nodes.  */
21700
21701 static tree
21702 cp_parser_objc_identifier_list (cp_parser* parser)
21703 {
21704   tree identifier;
21705   tree list;
21706   cp_token *sep;
21707
21708   identifier = cp_parser_identifier (parser);
21709   if (identifier == error_mark_node)
21710     return error_mark_node;      
21711
21712   list = build_tree_list (NULL_TREE, identifier);
21713   sep = cp_lexer_peek_token (parser->lexer);
21714
21715   while (sep->type == CPP_COMMA)
21716     {
21717       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21718       identifier = cp_parser_identifier (parser);
21719       if (identifier == error_mark_node)
21720         return list;
21721
21722       list = chainon (list, build_tree_list (NULL_TREE,
21723                                              identifier));
21724       sep = cp_lexer_peek_token (parser->lexer);
21725     }
21726   
21727   return list;
21728 }
21729
21730 /* Parse an Objective-C alias declaration.
21731
21732    objc-alias-declaration:
21733      @compatibility_alias identifier identifier ;
21734
21735    This function registers the alias mapping with the Objective-C front end.
21736    It returns nothing.  */
21737
21738 static void
21739 cp_parser_objc_alias_declaration (cp_parser* parser)
21740 {
21741   tree alias, orig;
21742
21743   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21744   alias = cp_parser_identifier (parser);
21745   orig = cp_parser_identifier (parser);
21746   objc_declare_alias (alias, orig);
21747   cp_parser_consume_semicolon_at_end_of_statement (parser);
21748 }
21749
21750 /* Parse an Objective-C class forward-declaration.
21751
21752    objc-class-declaration:
21753      @class objc-identifier-list ;
21754
21755    The function registers the forward declarations with the Objective-C
21756    front end.  It returns nothing.  */
21757
21758 static void
21759 cp_parser_objc_class_declaration (cp_parser* parser)
21760 {
21761   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21762   while (true)
21763     {
21764       tree id;
21765       
21766       id = cp_parser_identifier (parser);
21767       if (id == error_mark_node)
21768         break;
21769       
21770       objc_declare_class (id);
21771
21772       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21773         cp_lexer_consume_token (parser->lexer);
21774       else
21775         break;
21776     }
21777   cp_parser_consume_semicolon_at_end_of_statement (parser);
21778 }
21779
21780 /* Parse a list of Objective-C protocol references.
21781
21782    objc-protocol-refs-opt:
21783      objc-protocol-refs [opt]
21784
21785    objc-protocol-refs:
21786      < objc-identifier-list >
21787
21788    Returns a TREE_LIST of identifiers, if any.  */
21789
21790 static tree
21791 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21792 {
21793   tree protorefs = NULL_TREE;
21794
21795   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21796     {
21797       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21798       protorefs = cp_parser_objc_identifier_list (parser);
21799       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21800     }
21801
21802   return protorefs;
21803 }
21804
21805 /* Parse a Objective-C visibility specification.  */
21806
21807 static void
21808 cp_parser_objc_visibility_spec (cp_parser* parser)
21809 {
21810   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21811
21812   switch (vis->keyword)
21813     {
21814     case RID_AT_PRIVATE:
21815       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21816       break;
21817     case RID_AT_PROTECTED:
21818       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21819       break;
21820     case RID_AT_PUBLIC:
21821       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21822       break;
21823     case RID_AT_PACKAGE:
21824       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21825       break;
21826     default:
21827       return;
21828     }
21829
21830   /* Eat '@private'/'@protected'/'@public'.  */
21831   cp_lexer_consume_token (parser->lexer);
21832 }
21833
21834 /* Parse an Objective-C method type.  Return 'true' if it is a class
21835    (+) method, and 'false' if it is an instance (-) method.  */
21836
21837 static inline bool
21838 cp_parser_objc_method_type (cp_parser* parser)
21839 {
21840   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21841     return true;
21842   else
21843     return false;
21844 }
21845
21846 /* Parse an Objective-C protocol qualifier.  */
21847
21848 static tree
21849 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21850 {
21851   tree quals = NULL_TREE, node;
21852   cp_token *token = cp_lexer_peek_token (parser->lexer);
21853
21854   node = token->u.value;
21855
21856   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21857          && (node == ridpointers [(int) RID_IN]
21858              || node == ridpointers [(int) RID_OUT]
21859              || node == ridpointers [(int) RID_INOUT]
21860              || node == ridpointers [(int) RID_BYCOPY]
21861              || node == ridpointers [(int) RID_BYREF]
21862              || node == ridpointers [(int) RID_ONEWAY]))
21863     {
21864       quals = tree_cons (NULL_TREE, node, quals);
21865       cp_lexer_consume_token (parser->lexer);
21866       token = cp_lexer_peek_token (parser->lexer);
21867       node = token->u.value;
21868     }
21869
21870   return quals;
21871 }
21872
21873 /* Parse an Objective-C typename.  */
21874
21875 static tree
21876 cp_parser_objc_typename (cp_parser* parser)
21877 {
21878   tree type_name = NULL_TREE;
21879
21880   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21881     {
21882       tree proto_quals, cp_type = NULL_TREE;
21883
21884       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21885       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21886
21887       /* An ObjC type name may consist of just protocol qualifiers, in which
21888          case the type shall default to 'id'.  */
21889       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21890         {
21891           cp_type = cp_parser_type_id (parser);
21892           
21893           /* If the type could not be parsed, an error has already
21894              been produced.  For error recovery, behave as if it had
21895              not been specified, which will use the default type
21896              'id'.  */
21897           if (cp_type == error_mark_node)
21898             {
21899               cp_type = NULL_TREE;
21900               /* We need to skip to the closing parenthesis as
21901                  cp_parser_type_id() does not seem to do it for
21902                  us.  */
21903               cp_parser_skip_to_closing_parenthesis (parser,
21904                                                      /*recovering=*/true,
21905                                                      /*or_comma=*/false,
21906                                                      /*consume_paren=*/false);
21907             }
21908         }
21909
21910       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21911       type_name = build_tree_list (proto_quals, cp_type);
21912     }
21913
21914   return type_name;
21915 }
21916
21917 /* Check to see if TYPE refers to an Objective-C selector name.  */
21918
21919 static bool
21920 cp_parser_objc_selector_p (enum cpp_ttype type)
21921 {
21922   return (type == CPP_NAME || type == CPP_KEYWORD
21923           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21924           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21925           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21926           || type == CPP_XOR || type == CPP_XOR_EQ);
21927 }
21928
21929 /* Parse an Objective-C selector.  */
21930
21931 static tree
21932 cp_parser_objc_selector (cp_parser* parser)
21933 {
21934   cp_token *token = cp_lexer_consume_token (parser->lexer);
21935
21936   if (!cp_parser_objc_selector_p (token->type))
21937     {
21938       error_at (token->location, "invalid Objective-C++ selector name");
21939       return error_mark_node;
21940     }
21941
21942   /* C++ operator names are allowed to appear in ObjC selectors.  */
21943   switch (token->type)
21944     {
21945     case CPP_AND_AND: return get_identifier ("and");
21946     case CPP_AND_EQ: return get_identifier ("and_eq");
21947     case CPP_AND: return get_identifier ("bitand");
21948     case CPP_OR: return get_identifier ("bitor");
21949     case CPP_COMPL: return get_identifier ("compl");
21950     case CPP_NOT: return get_identifier ("not");
21951     case CPP_NOT_EQ: return get_identifier ("not_eq");
21952     case CPP_OR_OR: return get_identifier ("or");
21953     case CPP_OR_EQ: return get_identifier ("or_eq");
21954     case CPP_XOR: return get_identifier ("xor");
21955     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21956     default: return token->u.value;
21957     }
21958 }
21959
21960 /* Parse an Objective-C params list.  */
21961
21962 static tree
21963 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21964 {
21965   tree params = NULL_TREE;
21966   bool maybe_unary_selector_p = true;
21967   cp_token *token = cp_lexer_peek_token (parser->lexer);
21968
21969   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21970     {
21971       tree selector = NULL_TREE, type_name, identifier;
21972       tree parm_attr = NULL_TREE;
21973
21974       if (token->keyword == RID_ATTRIBUTE)
21975         break;
21976
21977       if (token->type != CPP_COLON)
21978         selector = cp_parser_objc_selector (parser);
21979
21980       /* Detect if we have a unary selector.  */
21981       if (maybe_unary_selector_p
21982           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21983         {
21984           params = selector; /* Might be followed by attributes.  */
21985           break;
21986         }
21987
21988       maybe_unary_selector_p = false;
21989       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21990         {
21991           /* Something went quite wrong.  There should be a colon
21992              here, but there is not.  Stop parsing parameters.  */
21993           break;
21994         }
21995       type_name = cp_parser_objc_typename (parser);
21996       /* New ObjC allows attributes on parameters too.  */
21997       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21998         parm_attr = cp_parser_attributes_opt (parser);
21999       identifier = cp_parser_identifier (parser);
22000
22001       params
22002         = chainon (params,
22003                    objc_build_keyword_decl (selector,
22004                                             type_name,
22005                                             identifier,
22006                                             parm_attr));
22007
22008       token = cp_lexer_peek_token (parser->lexer);
22009     }
22010
22011   if (params == NULL_TREE)
22012     {
22013       cp_parser_error (parser, "objective-c++ method declaration is expected");
22014       return error_mark_node;
22015     }
22016
22017   /* We allow tail attributes for the method.  */
22018   if (token->keyword == RID_ATTRIBUTE)
22019     {
22020       *attributes = cp_parser_attributes_opt (parser);
22021       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22022           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22023         return params;
22024       cp_parser_error (parser, 
22025                        "method attributes must be specified at the end");
22026       return error_mark_node;
22027     }
22028
22029   if (params == NULL_TREE)
22030     {
22031       cp_parser_error (parser, "objective-c++ method declaration is expected");
22032       return error_mark_node;
22033     }
22034   return params;
22035 }
22036
22037 /* Parse the non-keyword Objective-C params.  */
22038
22039 static tree
22040 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22041                                        tree* attributes)
22042 {
22043   tree params = make_node (TREE_LIST);
22044   cp_token *token = cp_lexer_peek_token (parser->lexer);
22045   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22046
22047   while (token->type == CPP_COMMA)
22048     {
22049       cp_parameter_declarator *parmdecl;
22050       tree parm;
22051
22052       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22053       token = cp_lexer_peek_token (parser->lexer);
22054
22055       if (token->type == CPP_ELLIPSIS)
22056         {
22057           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22058           *ellipsisp = true;
22059           token = cp_lexer_peek_token (parser->lexer);
22060           break;
22061         }
22062
22063       /* TODO: parse attributes for tail parameters.  */
22064       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22065       parm = grokdeclarator (parmdecl->declarator,
22066                              &parmdecl->decl_specifiers,
22067                              PARM, /*initialized=*/0,
22068                              /*attrlist=*/NULL);
22069
22070       chainon (params, build_tree_list (NULL_TREE, parm));
22071       token = cp_lexer_peek_token (parser->lexer);
22072     }
22073
22074   /* We allow tail attributes for the method.  */
22075   if (token->keyword == RID_ATTRIBUTE)
22076     {
22077       if (*attributes == NULL_TREE)
22078         {
22079           *attributes = cp_parser_attributes_opt (parser);
22080           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22081               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22082             return params;
22083         }
22084       else        
22085         /* We have an error, but parse the attributes, so that we can 
22086            carry on.  */
22087         *attributes = cp_parser_attributes_opt (parser);
22088
22089       cp_parser_error (parser, 
22090                        "method attributes must be specified at the end");
22091       return error_mark_node;
22092     }
22093
22094   return params;
22095 }
22096
22097 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22098
22099 static void
22100 cp_parser_objc_interstitial_code (cp_parser* parser)
22101 {
22102   cp_token *token = cp_lexer_peek_token (parser->lexer);
22103
22104   /* If the next token is `extern' and the following token is a string
22105      literal, then we have a linkage specification.  */
22106   if (token->keyword == RID_EXTERN
22107       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22108     cp_parser_linkage_specification (parser);
22109   /* Handle #pragma, if any.  */
22110   else if (token->type == CPP_PRAGMA)
22111     cp_parser_pragma (parser, pragma_external);
22112   /* Allow stray semicolons.  */
22113   else if (token->type == CPP_SEMICOLON)
22114     cp_lexer_consume_token (parser->lexer);
22115   /* Mark methods as optional or required, when building protocols.  */
22116   else if (token->keyword == RID_AT_OPTIONAL)
22117     {
22118       cp_lexer_consume_token (parser->lexer);
22119       objc_set_method_opt (true);
22120     }
22121   else if (token->keyword == RID_AT_REQUIRED)
22122     {
22123       cp_lexer_consume_token (parser->lexer);
22124       objc_set_method_opt (false);
22125     }
22126   else if (token->keyword == RID_NAMESPACE)
22127     cp_parser_namespace_definition (parser);
22128   /* Other stray characters must generate errors.  */
22129   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22130     {
22131       cp_lexer_consume_token (parser->lexer);
22132       error ("stray %qs between Objective-C++ methods",
22133              token->type == CPP_OPEN_BRACE ? "{" : "}");
22134     }
22135   /* Finally, try to parse a block-declaration, or a function-definition.  */
22136   else
22137     cp_parser_block_declaration (parser, /*statement_p=*/false);
22138 }
22139
22140 /* Parse a method signature.  */
22141
22142 static tree
22143 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22144 {
22145   tree rettype, kwdparms, optparms;
22146   bool ellipsis = false;
22147   bool is_class_method;
22148
22149   is_class_method = cp_parser_objc_method_type (parser);
22150   rettype = cp_parser_objc_typename (parser);
22151   *attributes = NULL_TREE;
22152   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22153   if (kwdparms == error_mark_node)
22154     return error_mark_node;
22155   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22156   if (optparms == error_mark_node)
22157     return error_mark_node;
22158
22159   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22160 }
22161
22162 static bool
22163 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22164 {
22165   tree tattr;  
22166   cp_lexer_save_tokens (parser->lexer);
22167   tattr = cp_parser_attributes_opt (parser);
22168   gcc_assert (tattr) ;
22169   
22170   /* If the attributes are followed by a method introducer, this is not allowed.
22171      Dump the attributes and flag the situation.  */
22172   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22173       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22174     return true;
22175
22176   /* Otherwise, the attributes introduce some interstitial code, possibly so
22177      rewind to allow that check.  */
22178   cp_lexer_rollback_tokens (parser->lexer);
22179   return false;  
22180 }
22181
22182 /* Parse an Objective-C method prototype list.  */
22183
22184 static void
22185 cp_parser_objc_method_prototype_list (cp_parser* parser)
22186 {
22187   cp_token *token = cp_lexer_peek_token (parser->lexer);
22188
22189   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22190     {
22191       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22192         {
22193           tree attributes, sig;
22194           bool is_class_method;
22195           if (token->type == CPP_PLUS)
22196             is_class_method = true;
22197           else
22198             is_class_method = false;
22199           sig = cp_parser_objc_method_signature (parser, &attributes);
22200           if (sig == error_mark_node)
22201             {
22202               cp_parser_skip_to_end_of_block_or_statement (parser);
22203               token = cp_lexer_peek_token (parser->lexer);
22204               continue;
22205             }
22206           objc_add_method_declaration (is_class_method, sig, attributes);
22207           cp_parser_consume_semicolon_at_end_of_statement (parser);
22208         }
22209       else if (token->keyword == RID_AT_PROPERTY)
22210         cp_parser_objc_at_property_declaration (parser);
22211       else if (token->keyword == RID_ATTRIBUTE 
22212                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22213         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22214                     OPT_Wattributes, 
22215                     "prefix attributes are ignored for methods");
22216       else
22217         /* Allow for interspersed non-ObjC++ code.  */
22218         cp_parser_objc_interstitial_code (parser);
22219
22220       token = cp_lexer_peek_token (parser->lexer);
22221     }
22222
22223   if (token->type != CPP_EOF)
22224     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22225   else
22226     cp_parser_error (parser, "expected %<@end%>");
22227
22228   objc_finish_interface ();
22229 }
22230
22231 /* Parse an Objective-C method definition list.  */
22232
22233 static void
22234 cp_parser_objc_method_definition_list (cp_parser* parser)
22235 {
22236   cp_token *token = cp_lexer_peek_token (parser->lexer);
22237
22238   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22239     {
22240       tree meth;
22241
22242       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22243         {
22244           cp_token *ptk;
22245           tree sig, attribute;
22246           bool is_class_method;
22247           if (token->type == CPP_PLUS)
22248             is_class_method = true;
22249           else
22250             is_class_method = false;
22251           push_deferring_access_checks (dk_deferred);
22252           sig = cp_parser_objc_method_signature (parser, &attribute);
22253           if (sig == error_mark_node)
22254             {
22255               cp_parser_skip_to_end_of_block_or_statement (parser);
22256               token = cp_lexer_peek_token (parser->lexer);
22257               continue;
22258             }
22259           objc_start_method_definition (is_class_method, sig, attribute,
22260                                         NULL_TREE);
22261
22262           /* For historical reasons, we accept an optional semicolon.  */
22263           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22264             cp_lexer_consume_token (parser->lexer);
22265
22266           ptk = cp_lexer_peek_token (parser->lexer);
22267           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22268                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22269             {
22270               perform_deferred_access_checks ();
22271               stop_deferring_access_checks ();
22272               meth = cp_parser_function_definition_after_declarator (parser,
22273                                                                      false);
22274               pop_deferring_access_checks ();
22275               objc_finish_method_definition (meth);
22276             }
22277         }
22278       /* The following case will be removed once @synthesize is
22279          completely implemented.  */
22280       else if (token->keyword == RID_AT_PROPERTY)
22281         cp_parser_objc_at_property_declaration (parser);
22282       else if (token->keyword == RID_AT_SYNTHESIZE)
22283         cp_parser_objc_at_synthesize_declaration (parser);
22284       else if (token->keyword == RID_AT_DYNAMIC)
22285         cp_parser_objc_at_dynamic_declaration (parser);
22286       else if (token->keyword == RID_ATTRIBUTE 
22287                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22288         warning_at (token->location, OPT_Wattributes,
22289                     "prefix attributes are ignored for methods");
22290       else
22291         /* Allow for interspersed non-ObjC++ code.  */
22292         cp_parser_objc_interstitial_code (parser);
22293
22294       token = cp_lexer_peek_token (parser->lexer);
22295     }
22296
22297   if (token->type != CPP_EOF)
22298     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22299   else
22300     cp_parser_error (parser, "expected %<@end%>");
22301
22302   objc_finish_implementation ();
22303 }
22304
22305 /* Parse Objective-C ivars.  */
22306
22307 static void
22308 cp_parser_objc_class_ivars (cp_parser* parser)
22309 {
22310   cp_token *token = cp_lexer_peek_token (parser->lexer);
22311
22312   if (token->type != CPP_OPEN_BRACE)
22313     return;     /* No ivars specified.  */
22314
22315   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22316   token = cp_lexer_peek_token (parser->lexer);
22317
22318   while (token->type != CPP_CLOSE_BRACE 
22319         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22320     {
22321       cp_decl_specifier_seq declspecs;
22322       int decl_class_or_enum_p;
22323       tree prefix_attributes;
22324
22325       cp_parser_objc_visibility_spec (parser);
22326
22327       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22328         break;
22329
22330       cp_parser_decl_specifier_seq (parser,
22331                                     CP_PARSER_FLAGS_OPTIONAL,
22332                                     &declspecs,
22333                                     &decl_class_or_enum_p);
22334
22335       /* auto, register, static, extern, mutable.  */
22336       if (declspecs.storage_class != sc_none)
22337         {
22338           cp_parser_error (parser, "invalid type for instance variable");         
22339           declspecs.storage_class = sc_none;
22340         }
22341
22342       /* __thread.  */
22343       if (declspecs.specs[(int) ds_thread])
22344         {
22345           cp_parser_error (parser, "invalid type for instance variable");
22346           declspecs.specs[(int) ds_thread] = 0;
22347         }
22348       
22349       /* typedef.  */
22350       if (declspecs.specs[(int) ds_typedef])
22351         {
22352           cp_parser_error (parser, "invalid type for instance variable");
22353           declspecs.specs[(int) ds_typedef] = 0;
22354         }
22355
22356       prefix_attributes = declspecs.attributes;
22357       declspecs.attributes = NULL_TREE;
22358
22359       /* Keep going until we hit the `;' at the end of the
22360          declaration.  */
22361       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22362         {
22363           tree width = NULL_TREE, attributes, first_attribute, decl;
22364           cp_declarator *declarator = NULL;
22365           int ctor_dtor_or_conv_p;
22366
22367           /* Check for a (possibly unnamed) bitfield declaration.  */
22368           token = cp_lexer_peek_token (parser->lexer);
22369           if (token->type == CPP_COLON)
22370             goto eat_colon;
22371
22372           if (token->type == CPP_NAME
22373               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22374                   == CPP_COLON))
22375             {
22376               /* Get the name of the bitfield.  */
22377               declarator = make_id_declarator (NULL_TREE,
22378                                                cp_parser_identifier (parser),
22379                                                sfk_none);
22380
22381              eat_colon:
22382               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22383               /* Get the width of the bitfield.  */
22384               width
22385                 = cp_parser_constant_expression (parser,
22386                                                  /*allow_non_constant=*/false,
22387                                                  NULL);
22388             }
22389           else
22390             {
22391               /* Parse the declarator.  */
22392               declarator
22393                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22394                                         &ctor_dtor_or_conv_p,
22395                                         /*parenthesized_p=*/NULL,
22396                                         /*member_p=*/false);
22397             }
22398
22399           /* Look for attributes that apply to the ivar.  */
22400           attributes = cp_parser_attributes_opt (parser);
22401           /* Remember which attributes are prefix attributes and
22402              which are not.  */
22403           first_attribute = attributes;
22404           /* Combine the attributes.  */
22405           attributes = chainon (prefix_attributes, attributes);
22406
22407           if (width)
22408               /* Create the bitfield declaration.  */
22409               decl = grokbitfield (declarator, &declspecs,
22410                                    width,
22411                                    attributes);
22412           else
22413             decl = grokfield (declarator, &declspecs,
22414                               NULL_TREE, /*init_const_expr_p=*/false,
22415                               NULL_TREE, attributes);
22416
22417           /* Add the instance variable.  */
22418           objc_add_instance_variable (decl);
22419
22420           /* Reset PREFIX_ATTRIBUTES.  */
22421           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22422             attributes = TREE_CHAIN (attributes);
22423           if (attributes)
22424             TREE_CHAIN (attributes) = NULL_TREE;
22425
22426           token = cp_lexer_peek_token (parser->lexer);
22427
22428           if (token->type == CPP_COMMA)
22429             {
22430               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22431               continue;
22432             }
22433           break;
22434         }
22435
22436       cp_parser_consume_semicolon_at_end_of_statement (parser);
22437       token = cp_lexer_peek_token (parser->lexer);
22438     }
22439
22440   if (token->keyword == RID_AT_END)
22441     cp_parser_error (parser, "expected %<}%>");
22442
22443   /* Do not consume the RID_AT_END, so it will be read again as terminating
22444      the @interface of @implementation.  */ 
22445   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22446     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22447     
22448   /* For historical reasons, we accept an optional semicolon.  */
22449   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22450     cp_lexer_consume_token (parser->lexer);
22451 }
22452
22453 /* Parse an Objective-C protocol declaration.  */
22454
22455 static void
22456 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22457 {
22458   tree proto, protorefs;
22459   cp_token *tok;
22460
22461   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22462   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22463     {
22464       tok = cp_lexer_peek_token (parser->lexer);
22465       error_at (tok->location, "identifier expected after %<@protocol%>");
22466       cp_parser_consume_semicolon_at_end_of_statement (parser);
22467       return;
22468     }
22469
22470   /* See if we have a forward declaration or a definition.  */
22471   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22472
22473   /* Try a forward declaration first.  */
22474   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22475     {
22476       while (true)
22477         {
22478           tree id;
22479           
22480           id = cp_parser_identifier (parser);
22481           if (id == error_mark_node)
22482             break;
22483           
22484           objc_declare_protocol (id, attributes);
22485           
22486           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22487             cp_lexer_consume_token (parser->lexer);
22488           else
22489             break;
22490         }
22491       cp_parser_consume_semicolon_at_end_of_statement (parser);
22492     }
22493
22494   /* Ok, we got a full-fledged definition (or at least should).  */
22495   else
22496     {
22497       proto = cp_parser_identifier (parser);
22498       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22499       objc_start_protocol (proto, protorefs, attributes);
22500       cp_parser_objc_method_prototype_list (parser);
22501     }
22502 }
22503
22504 /* Parse an Objective-C superclass or category.  */
22505
22506 static void
22507 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22508                                        bool iface_p,
22509                                        tree *super,
22510                                        tree *categ, bool *is_class_extension)
22511 {
22512   cp_token *next = cp_lexer_peek_token (parser->lexer);
22513
22514   *super = *categ = NULL_TREE;
22515   *is_class_extension = false;
22516   if (next->type == CPP_COLON)
22517     {
22518       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22519       *super = cp_parser_identifier (parser);
22520     }
22521   else if (next->type == CPP_OPEN_PAREN)
22522     {
22523       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22524
22525       /* If there is no category name, and this is an @interface, we
22526          have a class extension.  */
22527       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22528         {
22529           *categ = NULL_TREE;
22530           *is_class_extension = true;
22531         }
22532       else
22533         *categ = cp_parser_identifier (parser);
22534
22535       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22536     }
22537 }
22538
22539 /* Parse an Objective-C class interface.  */
22540
22541 static void
22542 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22543 {
22544   tree name, super, categ, protos;
22545   bool is_class_extension;
22546
22547   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22548   name = cp_parser_identifier (parser);
22549   if (name == error_mark_node)
22550     {
22551       /* It's hard to recover because even if valid @interface stuff
22552          is to follow, we can't compile it (or validate it) if we
22553          don't even know which class it refers to.  Let's assume this
22554          was a stray '@interface' token in the stream and skip it.
22555       */
22556       return;
22557     }
22558   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22559                                          &is_class_extension);
22560   protos = cp_parser_objc_protocol_refs_opt (parser);
22561
22562   /* We have either a class or a category on our hands.  */
22563   if (categ || is_class_extension)
22564     objc_start_category_interface (name, categ, protos, attributes);
22565   else
22566     {
22567       objc_start_class_interface (name, super, protos, attributes);
22568       /* Handle instance variable declarations, if any.  */
22569       cp_parser_objc_class_ivars (parser);
22570       objc_continue_interface ();
22571     }
22572
22573   cp_parser_objc_method_prototype_list (parser);
22574 }
22575
22576 /* Parse an Objective-C class implementation.  */
22577
22578 static void
22579 cp_parser_objc_class_implementation (cp_parser* parser)
22580 {
22581   tree name, super, categ;
22582   bool is_class_extension;
22583
22584   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22585   name = cp_parser_identifier (parser);
22586   if (name == error_mark_node)
22587     {
22588       /* It's hard to recover because even if valid @implementation
22589          stuff is to follow, we can't compile it (or validate it) if
22590          we don't even know which class it refers to.  Let's assume
22591          this was a stray '@implementation' token in the stream and
22592          skip it.
22593       */
22594       return;
22595     }
22596   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22597                                          &is_class_extension);
22598
22599   /* We have either a class or a category on our hands.  */
22600   if (categ)
22601     objc_start_category_implementation (name, categ);
22602   else
22603     {
22604       objc_start_class_implementation (name, super);
22605       /* Handle instance variable declarations, if any.  */
22606       cp_parser_objc_class_ivars (parser);
22607       objc_continue_implementation ();
22608     }
22609
22610   cp_parser_objc_method_definition_list (parser);
22611 }
22612
22613 /* Consume the @end token and finish off the implementation.  */
22614
22615 static void
22616 cp_parser_objc_end_implementation (cp_parser* parser)
22617 {
22618   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22619   objc_finish_implementation ();
22620 }
22621
22622 /* Parse an Objective-C declaration.  */
22623
22624 static void
22625 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22626 {
22627   /* Try to figure out what kind of declaration is present.  */
22628   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22629
22630   if (attributes)
22631     switch (kwd->keyword)
22632       {
22633         case RID_AT_ALIAS:
22634         case RID_AT_CLASS:
22635         case RID_AT_END:
22636           error_at (kwd->location, "attributes may not be specified before"
22637                     " the %<@%D%> Objective-C++ keyword",
22638                     kwd->u.value);
22639           attributes = NULL;
22640           break;
22641         case RID_AT_IMPLEMENTATION:
22642           warning_at (kwd->location, OPT_Wattributes,
22643                       "prefix attributes are ignored before %<@%D%>",
22644                       kwd->u.value);
22645           attributes = NULL;
22646         default:
22647           break;
22648       }
22649
22650   switch (kwd->keyword)
22651     {
22652     case RID_AT_ALIAS:
22653       cp_parser_objc_alias_declaration (parser);
22654       break;
22655     case RID_AT_CLASS:
22656       cp_parser_objc_class_declaration (parser);
22657       break;
22658     case RID_AT_PROTOCOL:
22659       cp_parser_objc_protocol_declaration (parser, attributes);
22660       break;
22661     case RID_AT_INTERFACE:
22662       cp_parser_objc_class_interface (parser, attributes);
22663       break;
22664     case RID_AT_IMPLEMENTATION:
22665       cp_parser_objc_class_implementation (parser);
22666       break;
22667     case RID_AT_END:
22668       cp_parser_objc_end_implementation (parser);
22669       break;
22670     default:
22671       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22672                 kwd->u.value);
22673       cp_parser_skip_to_end_of_block_or_statement (parser);
22674     }
22675 }
22676
22677 /* Parse an Objective-C try-catch-finally statement.
22678
22679    objc-try-catch-finally-stmt:
22680      @try compound-statement objc-catch-clause-seq [opt]
22681        objc-finally-clause [opt]
22682
22683    objc-catch-clause-seq:
22684      objc-catch-clause objc-catch-clause-seq [opt]
22685
22686    objc-catch-clause:
22687      @catch ( objc-exception-declaration ) compound-statement
22688
22689    objc-finally-clause:
22690      @finally compound-statement
22691
22692    objc-exception-declaration:
22693      parameter-declaration
22694      '...'
22695
22696    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22697
22698    Returns NULL_TREE.
22699
22700    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22701    for C.  Keep them in sync.  */   
22702
22703 static tree
22704 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22705 {
22706   location_t location;
22707   tree stmt;
22708
22709   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22710   location = cp_lexer_peek_token (parser->lexer)->location;
22711   objc_maybe_warn_exceptions (location);
22712   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22713      node, lest it get absorbed into the surrounding block.  */
22714   stmt = push_stmt_list ();
22715   cp_parser_compound_statement (parser, NULL, false, false);
22716   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22717
22718   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22719     {
22720       cp_parameter_declarator *parm;
22721       tree parameter_declaration = error_mark_node;
22722       bool seen_open_paren = false;
22723
22724       cp_lexer_consume_token (parser->lexer);
22725       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22726         seen_open_paren = true;
22727       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22728         {
22729           /* We have "@catch (...)" (where the '...' are literally
22730              what is in the code).  Skip the '...'.
22731              parameter_declaration is set to NULL_TREE, and
22732              objc_being_catch_clauses() knows that that means
22733              '...'.  */
22734           cp_lexer_consume_token (parser->lexer);
22735           parameter_declaration = NULL_TREE;
22736         }
22737       else
22738         {
22739           /* We have "@catch (NSException *exception)" or something
22740              like that.  Parse the parameter declaration.  */
22741           parm = cp_parser_parameter_declaration (parser, false, NULL);
22742           if (parm == NULL)
22743             parameter_declaration = error_mark_node;
22744           else
22745             parameter_declaration = grokdeclarator (parm->declarator,
22746                                                     &parm->decl_specifiers,
22747                                                     PARM, /*initialized=*/0,
22748                                                     /*attrlist=*/NULL);
22749         }
22750       if (seen_open_paren)
22751         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22752       else
22753         {
22754           /* If there was no open parenthesis, we are recovering from
22755              an error, and we are trying to figure out what mistake
22756              the user has made.  */
22757
22758           /* If there is an immediate closing parenthesis, the user
22759              probably forgot the opening one (ie, they typed "@catch
22760              NSException *e)".  Parse the closing parenthesis and keep
22761              going.  */
22762           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22763             cp_lexer_consume_token (parser->lexer);
22764           
22765           /* If these is no immediate closing parenthesis, the user
22766              probably doesn't know that parenthesis are required at
22767              all (ie, they typed "@catch NSException *e").  So, just
22768              forget about the closing parenthesis and keep going.  */
22769         }
22770       objc_begin_catch_clause (parameter_declaration);
22771       cp_parser_compound_statement (parser, NULL, false, false);
22772       objc_finish_catch_clause ();
22773     }
22774   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22775     {
22776       cp_lexer_consume_token (parser->lexer);
22777       location = cp_lexer_peek_token (parser->lexer)->location;
22778       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22779          node, lest it get absorbed into the surrounding block.  */
22780       stmt = push_stmt_list ();
22781       cp_parser_compound_statement (parser, NULL, false, false);
22782       objc_build_finally_clause (location, pop_stmt_list (stmt));
22783     }
22784
22785   return objc_finish_try_stmt ();
22786 }
22787
22788 /* Parse an Objective-C synchronized statement.
22789
22790    objc-synchronized-stmt:
22791      @synchronized ( expression ) compound-statement
22792
22793    Returns NULL_TREE.  */
22794
22795 static tree
22796 cp_parser_objc_synchronized_statement (cp_parser *parser)
22797 {
22798   location_t location;
22799   tree lock, stmt;
22800
22801   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22802
22803   location = cp_lexer_peek_token (parser->lexer)->location;
22804   objc_maybe_warn_exceptions (location);
22805   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22806   lock = cp_parser_expression (parser, false, NULL);
22807   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22808
22809   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22810      node, lest it get absorbed into the surrounding block.  */
22811   stmt = push_stmt_list ();
22812   cp_parser_compound_statement (parser, NULL, false, false);
22813
22814   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22815 }
22816
22817 /* Parse an Objective-C throw statement.
22818
22819    objc-throw-stmt:
22820      @throw assignment-expression [opt] ;
22821
22822    Returns a constructed '@throw' statement.  */
22823
22824 static tree
22825 cp_parser_objc_throw_statement (cp_parser *parser)
22826 {
22827   tree expr = NULL_TREE;
22828   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22829
22830   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22831
22832   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22833     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22834
22835   cp_parser_consume_semicolon_at_end_of_statement (parser);
22836
22837   return objc_build_throw_stmt (loc, expr);
22838 }
22839
22840 /* Parse an Objective-C statement.  */
22841
22842 static tree
22843 cp_parser_objc_statement (cp_parser * parser)
22844 {
22845   /* Try to figure out what kind of declaration is present.  */
22846   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22847
22848   switch (kwd->keyword)
22849     {
22850     case RID_AT_TRY:
22851       return cp_parser_objc_try_catch_finally_statement (parser);
22852     case RID_AT_SYNCHRONIZED:
22853       return cp_parser_objc_synchronized_statement (parser);
22854     case RID_AT_THROW:
22855       return cp_parser_objc_throw_statement (parser);
22856     default:
22857       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22858                kwd->u.value);
22859       cp_parser_skip_to_end_of_block_or_statement (parser);
22860     }
22861
22862   return error_mark_node;
22863 }
22864
22865 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22866    look ahead to see if an objc keyword follows the attributes.  This
22867    is to detect the use of prefix attributes on ObjC @interface and 
22868    @protocol.  */
22869
22870 static bool
22871 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22872 {
22873   cp_lexer_save_tokens (parser->lexer);
22874   *attrib = cp_parser_attributes_opt (parser);
22875   gcc_assert (*attrib);
22876   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22877     {
22878       cp_lexer_commit_tokens (parser->lexer);
22879       return true;
22880     }
22881   cp_lexer_rollback_tokens (parser->lexer);
22882   return false;  
22883 }
22884
22885 /* This routine is a minimal replacement for
22886    c_parser_struct_declaration () used when parsing the list of
22887    types/names or ObjC++ properties.  For example, when parsing the
22888    code
22889
22890    @property (readonly) int a, b, c;
22891
22892    this function is responsible for parsing "int a, int b, int c" and
22893    returning the declarations as CHAIN of DECLs.
22894
22895    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22896    similar parsing.  */
22897 static tree
22898 cp_parser_objc_struct_declaration (cp_parser *parser)
22899 {
22900   tree decls = NULL_TREE;
22901   cp_decl_specifier_seq declspecs;
22902   int decl_class_or_enum_p;
22903   tree prefix_attributes;
22904
22905   cp_parser_decl_specifier_seq (parser,
22906                                 CP_PARSER_FLAGS_NONE,
22907                                 &declspecs,
22908                                 &decl_class_or_enum_p);
22909
22910   if (declspecs.type == error_mark_node)
22911     return error_mark_node;
22912
22913   /* auto, register, static, extern, mutable.  */
22914   if (declspecs.storage_class != sc_none)
22915     {
22916       cp_parser_error (parser, "invalid type for property");
22917       declspecs.storage_class = sc_none;
22918     }
22919   
22920   /* __thread.  */
22921   if (declspecs.specs[(int) ds_thread])
22922     {
22923       cp_parser_error (parser, "invalid type for property");
22924       declspecs.specs[(int) ds_thread] = 0;
22925     }
22926   
22927   /* typedef.  */
22928   if (declspecs.specs[(int) ds_typedef])
22929     {
22930       cp_parser_error (parser, "invalid type for property");
22931       declspecs.specs[(int) ds_typedef] = 0;
22932     }
22933
22934   prefix_attributes = declspecs.attributes;
22935   declspecs.attributes = NULL_TREE;
22936
22937   /* Keep going until we hit the `;' at the end of the declaration. */
22938   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22939     {
22940       tree attributes, first_attribute, decl;
22941       cp_declarator *declarator;
22942       cp_token *token;
22943
22944       /* Parse the declarator.  */
22945       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22946                                          NULL, NULL, false);
22947
22948       /* Look for attributes that apply to the ivar.  */
22949       attributes = cp_parser_attributes_opt (parser);
22950       /* Remember which attributes are prefix attributes and
22951          which are not.  */
22952       first_attribute = attributes;
22953       /* Combine the attributes.  */
22954       attributes = chainon (prefix_attributes, attributes);
22955       
22956       decl = grokfield (declarator, &declspecs,
22957                         NULL_TREE, /*init_const_expr_p=*/false,
22958                         NULL_TREE, attributes);
22959
22960       if (decl == error_mark_node || decl == NULL_TREE)
22961         return error_mark_node;
22962       
22963       /* Reset PREFIX_ATTRIBUTES.  */
22964       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22965         attributes = TREE_CHAIN (attributes);
22966       if (attributes)
22967         TREE_CHAIN (attributes) = NULL_TREE;
22968
22969       DECL_CHAIN (decl) = decls;
22970       decls = decl;
22971
22972       token = cp_lexer_peek_token (parser->lexer);
22973       if (token->type == CPP_COMMA)
22974         {
22975           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22976           continue;
22977         }
22978       else
22979         break;
22980     }
22981   return decls;
22982 }
22983
22984 /* Parse an Objective-C @property declaration.  The syntax is:
22985
22986    objc-property-declaration:
22987      '@property' objc-property-attributes[opt] struct-declaration ;
22988
22989    objc-property-attributes:
22990     '(' objc-property-attribute-list ')'
22991
22992    objc-property-attribute-list:
22993      objc-property-attribute
22994      objc-property-attribute-list, objc-property-attribute
22995
22996    objc-property-attribute
22997      'getter' = identifier
22998      'setter' = identifier
22999      'readonly'
23000      'readwrite'
23001      'assign'
23002      'retain'
23003      'copy'
23004      'nonatomic'
23005
23006   For example:
23007     @property NSString *name;
23008     @property (readonly) id object;
23009     @property (retain, nonatomic, getter=getTheName) id name;
23010     @property int a, b, c;
23011
23012    PS: This function is identical to
23013    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23014 static void 
23015 cp_parser_objc_at_property_declaration (cp_parser *parser)
23016 {
23017   /* The following variables hold the attributes of the properties as
23018      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23019      seen.  When we see an attribute, we set them to 'true' (if they
23020      are boolean properties) or to the identifier (if they have an
23021      argument, ie, for getter and setter).  Note that here we only
23022      parse the list of attributes, check the syntax and accumulate the
23023      attributes that we find.  objc_add_property_declaration() will
23024      then process the information.  */
23025   bool property_assign = false;
23026   bool property_copy = false;
23027   tree property_getter_ident = NULL_TREE;
23028   bool property_nonatomic = false;
23029   bool property_readonly = false;
23030   bool property_readwrite = false;
23031   bool property_retain = false;
23032   tree property_setter_ident = NULL_TREE;
23033
23034   /* 'properties' is the list of properties that we read.  Usually a
23035      single one, but maybe more (eg, in "@property int a, b, c;" there
23036      are three).  */
23037   tree properties;
23038   location_t loc;
23039
23040   loc = cp_lexer_peek_token (parser->lexer)->location;
23041
23042   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23043
23044   /* Parse the optional attribute list...  */
23045   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23046     {
23047       /* Eat the '('.  */
23048       cp_lexer_consume_token (parser->lexer);
23049
23050       while (true)
23051         {
23052           bool syntax_error = false;
23053           cp_token *token = cp_lexer_peek_token (parser->lexer);
23054           enum rid keyword;
23055
23056           if (token->type != CPP_NAME)
23057             {
23058               cp_parser_error (parser, "expected identifier");
23059               break;
23060             }
23061           keyword = C_RID_CODE (token->u.value);
23062           cp_lexer_consume_token (parser->lexer);
23063           switch (keyword)
23064             {
23065             case RID_ASSIGN:    property_assign = true;    break;
23066             case RID_COPY:      property_copy = true;      break;
23067             case RID_NONATOMIC: property_nonatomic = true; break;
23068             case RID_READONLY:  property_readonly = true;  break;
23069             case RID_READWRITE: property_readwrite = true; break;
23070             case RID_RETAIN:    property_retain = true;    break;
23071
23072             case RID_GETTER:
23073             case RID_SETTER:
23074               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23075                 {
23076                   if (keyword == RID_GETTER)
23077                     cp_parser_error (parser,
23078                                      "missing %<=%> (after %<getter%> attribute)");
23079                   else
23080                     cp_parser_error (parser,
23081                                      "missing %<=%> (after %<setter%> attribute)");
23082                   syntax_error = true;
23083                   break;
23084                 }
23085               cp_lexer_consume_token (parser->lexer); /* eat the = */
23086               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23087                 {
23088                   cp_parser_error (parser, "expected identifier");
23089                   syntax_error = true;
23090                   break;
23091                 }
23092               if (keyword == RID_SETTER)
23093                 {
23094                   if (property_setter_ident != NULL_TREE)
23095                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23096                   else
23097                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23098                   cp_lexer_consume_token (parser->lexer);
23099                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23100                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23101                   else
23102                     cp_lexer_consume_token (parser->lexer);
23103                 }
23104               else
23105                 {
23106                   if (property_getter_ident != NULL_TREE)
23107                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23108                   else
23109                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23110                   cp_lexer_consume_token (parser->lexer);
23111                 }
23112               break;
23113             default:
23114               cp_parser_error (parser, "unknown property attribute");
23115               syntax_error = true;
23116               break;
23117             }
23118
23119           if (syntax_error)
23120             break;
23121
23122           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23123             cp_lexer_consume_token (parser->lexer);
23124           else
23125             break;
23126         }
23127
23128       /* FIXME: "@property (setter, assign);" will generate a spurious
23129          "error: expected â€˜)’ before â€˜,’ token".  This is because
23130          cp_parser_require, unlike the C counterpart, will produce an
23131          error even if we are in error recovery.  */
23132       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23133         {
23134           cp_parser_skip_to_closing_parenthesis (parser,
23135                                                  /*recovering=*/true,
23136                                                  /*or_comma=*/false,
23137                                                  /*consume_paren=*/true);
23138         }
23139     }
23140
23141   /* ... and the property declaration(s).  */
23142   properties = cp_parser_objc_struct_declaration (parser);
23143
23144   if (properties == error_mark_node)
23145     {
23146       cp_parser_skip_to_end_of_statement (parser);
23147       /* If the next token is now a `;', consume it.  */
23148       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23149         cp_lexer_consume_token (parser->lexer);
23150       return;
23151     }
23152
23153   if (properties == NULL_TREE)
23154     cp_parser_error (parser, "expected identifier");
23155   else
23156     {
23157       /* Comma-separated properties are chained together in
23158          reverse order; add them one by one.  */
23159       properties = nreverse (properties);
23160       
23161       for (; properties; properties = TREE_CHAIN (properties))
23162         objc_add_property_declaration (loc, copy_node (properties),
23163                                        property_readonly, property_readwrite,
23164                                        property_assign, property_retain,
23165                                        property_copy, property_nonatomic,
23166                                        property_getter_ident, property_setter_ident);
23167     }
23168   
23169   cp_parser_consume_semicolon_at_end_of_statement (parser);
23170 }
23171
23172 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23173
23174    objc-synthesize-declaration:
23175      @synthesize objc-synthesize-identifier-list ;
23176
23177    objc-synthesize-identifier-list:
23178      objc-synthesize-identifier
23179      objc-synthesize-identifier-list, objc-synthesize-identifier
23180
23181    objc-synthesize-identifier
23182      identifier
23183      identifier = identifier
23184
23185   For example:
23186     @synthesize MyProperty;
23187     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23188
23189   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23190   for C.  Keep them in sync.
23191 */
23192 static void 
23193 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23194 {
23195   tree list = NULL_TREE;
23196   location_t loc;
23197   loc = cp_lexer_peek_token (parser->lexer)->location;
23198
23199   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23200   while (true)
23201     {
23202       tree property, ivar;
23203       property = cp_parser_identifier (parser);
23204       if (property == error_mark_node)
23205         {
23206           cp_parser_consume_semicolon_at_end_of_statement (parser);
23207           return;
23208         }
23209       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23210         {
23211           cp_lexer_consume_token (parser->lexer);
23212           ivar = cp_parser_identifier (parser);
23213           if (ivar == error_mark_node)
23214             {
23215               cp_parser_consume_semicolon_at_end_of_statement (parser);
23216               return;
23217             }
23218         }
23219       else
23220         ivar = NULL_TREE;
23221       list = chainon (list, build_tree_list (ivar, property));
23222       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23223         cp_lexer_consume_token (parser->lexer);
23224       else
23225         break;
23226     }
23227   cp_parser_consume_semicolon_at_end_of_statement (parser);
23228   objc_add_synthesize_declaration (loc, list);
23229 }
23230
23231 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23232
23233    objc-dynamic-declaration:
23234      @dynamic identifier-list ;
23235
23236    For example:
23237      @dynamic MyProperty;
23238      @dynamic MyProperty, AnotherProperty;
23239
23240   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23241   for C.  Keep them in sync.
23242 */
23243 static void 
23244 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23245 {
23246   tree list = NULL_TREE;
23247   location_t loc;
23248   loc = cp_lexer_peek_token (parser->lexer)->location;
23249
23250   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23251   while (true)
23252     {
23253       tree property;
23254       property = cp_parser_identifier (parser);
23255       if (property == error_mark_node)
23256         {
23257           cp_parser_consume_semicolon_at_end_of_statement (parser);
23258           return;
23259         }
23260       list = chainon (list, build_tree_list (NULL, property));
23261       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23262         cp_lexer_consume_token (parser->lexer);
23263       else
23264         break;
23265     }
23266   cp_parser_consume_semicolon_at_end_of_statement (parser);
23267   objc_add_dynamic_declaration (loc, list);
23268 }
23269
23270 \f
23271 /* OpenMP 2.5 parsing routines.  */
23272
23273 /* Returns name of the next clause.
23274    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23275    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23276    returned and the token is consumed.  */
23277
23278 static pragma_omp_clause
23279 cp_parser_omp_clause_name (cp_parser *parser)
23280 {
23281   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23282
23283   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23284     result = PRAGMA_OMP_CLAUSE_IF;
23285   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23286     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23287   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23288     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23289   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23290     {
23291       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23292       const char *p = IDENTIFIER_POINTER (id);
23293
23294       switch (p[0])
23295         {
23296         case 'c':
23297           if (!strcmp ("collapse", p))
23298             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23299           else if (!strcmp ("copyin", p))
23300             result = PRAGMA_OMP_CLAUSE_COPYIN;
23301           else if (!strcmp ("copyprivate", p))
23302             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23303           break;
23304         case 'f':
23305           if (!strcmp ("firstprivate", p))
23306             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23307           break;
23308         case 'l':
23309           if (!strcmp ("lastprivate", p))
23310             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23311           break;
23312         case 'n':
23313           if (!strcmp ("nowait", p))
23314             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23315           else if (!strcmp ("num_threads", p))
23316             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23317           break;
23318         case 'o':
23319           if (!strcmp ("ordered", p))
23320             result = PRAGMA_OMP_CLAUSE_ORDERED;
23321           break;
23322         case 'r':
23323           if (!strcmp ("reduction", p))
23324             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23325           break;
23326         case 's':
23327           if (!strcmp ("schedule", p))
23328             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23329           else if (!strcmp ("shared", p))
23330             result = PRAGMA_OMP_CLAUSE_SHARED;
23331           break;
23332         case 'u':
23333           if (!strcmp ("untied", p))
23334             result = PRAGMA_OMP_CLAUSE_UNTIED;
23335           break;
23336         }
23337     }
23338
23339   if (result != PRAGMA_OMP_CLAUSE_NONE)
23340     cp_lexer_consume_token (parser->lexer);
23341
23342   return result;
23343 }
23344
23345 /* Validate that a clause of the given type does not already exist.  */
23346
23347 static void
23348 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23349                            const char *name, location_t location)
23350 {
23351   tree c;
23352
23353   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23354     if (OMP_CLAUSE_CODE (c) == code)
23355       {
23356         error_at (location, "too many %qs clauses", name);
23357         break;
23358       }
23359 }
23360
23361 /* OpenMP 2.5:
23362    variable-list:
23363      identifier
23364      variable-list , identifier
23365
23366    In addition, we match a closing parenthesis.  An opening parenthesis
23367    will have been consumed by the caller.
23368
23369    If KIND is nonzero, create the appropriate node and install the decl
23370    in OMP_CLAUSE_DECL and add the node to the head of the list.
23371
23372    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23373    return the list created.  */
23374
23375 static tree
23376 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23377                                 tree list)
23378 {
23379   cp_token *token;
23380   while (1)
23381     {
23382       tree name, decl;
23383
23384       token = cp_lexer_peek_token (parser->lexer);
23385       name = cp_parser_id_expression (parser, /*template_p=*/false,
23386                                       /*check_dependency_p=*/true,
23387                                       /*template_p=*/NULL,
23388                                       /*declarator_p=*/false,
23389                                       /*optional_p=*/false);
23390       if (name == error_mark_node)
23391         goto skip_comma;
23392
23393       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23394       if (decl == error_mark_node)
23395         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23396                                      token->location);
23397       else if (kind != 0)
23398         {
23399           tree u = build_omp_clause (token->location, kind);
23400           OMP_CLAUSE_DECL (u) = decl;
23401           OMP_CLAUSE_CHAIN (u) = list;
23402           list = u;
23403         }
23404       else
23405         list = tree_cons (decl, NULL_TREE, list);
23406
23407     get_comma:
23408       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23409         break;
23410       cp_lexer_consume_token (parser->lexer);
23411     }
23412
23413   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23414     {
23415       int ending;
23416
23417       /* Try to resync to an unnested comma.  Copied from
23418          cp_parser_parenthesized_expression_list.  */
23419     skip_comma:
23420       ending = cp_parser_skip_to_closing_parenthesis (parser,
23421                                                       /*recovering=*/true,
23422                                                       /*or_comma=*/true,
23423                                                       /*consume_paren=*/true);
23424       if (ending < 0)
23425         goto get_comma;
23426     }
23427
23428   return list;
23429 }
23430
23431 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23432    common case for omp clauses.  */
23433
23434 static tree
23435 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23436 {
23437   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23438     return cp_parser_omp_var_list_no_open (parser, kind, list);
23439   return list;
23440 }
23441
23442 /* OpenMP 3.0:
23443    collapse ( constant-expression ) */
23444
23445 static tree
23446 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23447 {
23448   tree c, num;
23449   location_t loc;
23450   HOST_WIDE_INT n;
23451
23452   loc = cp_lexer_peek_token (parser->lexer)->location;
23453   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23454     return list;
23455
23456   num = cp_parser_constant_expression (parser, false, NULL);
23457
23458   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23459     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23460                                            /*or_comma=*/false,
23461                                            /*consume_paren=*/true);
23462
23463   if (num == error_mark_node)
23464     return list;
23465   num = fold_non_dependent_expr (num);
23466   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23467       || !host_integerp (num, 0)
23468       || (n = tree_low_cst (num, 0)) <= 0
23469       || (int) n != n)
23470     {
23471       error_at (loc, "collapse argument needs positive constant integer expression");
23472       return list;
23473     }
23474
23475   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23476   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23477   OMP_CLAUSE_CHAIN (c) = list;
23478   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23479
23480   return c;
23481 }
23482
23483 /* OpenMP 2.5:
23484    default ( shared | none ) */
23485
23486 static tree
23487 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23488 {
23489   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23490   tree c;
23491
23492   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23493     return list;
23494   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23495     {
23496       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23497       const char *p = IDENTIFIER_POINTER (id);
23498
23499       switch (p[0])
23500         {
23501         case 'n':
23502           if (strcmp ("none", p) != 0)
23503             goto invalid_kind;
23504           kind = OMP_CLAUSE_DEFAULT_NONE;
23505           break;
23506
23507         case 's':
23508           if (strcmp ("shared", p) != 0)
23509             goto invalid_kind;
23510           kind = OMP_CLAUSE_DEFAULT_SHARED;
23511           break;
23512
23513         default:
23514           goto invalid_kind;
23515         }
23516
23517       cp_lexer_consume_token (parser->lexer);
23518     }
23519   else
23520     {
23521     invalid_kind:
23522       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23523     }
23524
23525   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23526     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23527                                            /*or_comma=*/false,
23528                                            /*consume_paren=*/true);
23529
23530   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23531     return list;
23532
23533   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23534   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23535   OMP_CLAUSE_CHAIN (c) = list;
23536   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23537
23538   return c;
23539 }
23540
23541 /* OpenMP 2.5:
23542    if ( expression ) */
23543
23544 static tree
23545 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23546 {
23547   tree t, c;
23548
23549   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23550     return list;
23551
23552   t = cp_parser_condition (parser);
23553
23554   if (t == error_mark_node
23555       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23556     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23557                                            /*or_comma=*/false,
23558                                            /*consume_paren=*/true);
23559
23560   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23561
23562   c = build_omp_clause (location, OMP_CLAUSE_IF);
23563   OMP_CLAUSE_IF_EXPR (c) = t;
23564   OMP_CLAUSE_CHAIN (c) = list;
23565
23566   return c;
23567 }
23568
23569 /* OpenMP 2.5:
23570    nowait */
23571
23572 static tree
23573 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23574                              tree list, location_t location)
23575 {
23576   tree c;
23577
23578   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23579
23580   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23581   OMP_CLAUSE_CHAIN (c) = list;
23582   return c;
23583 }
23584
23585 /* OpenMP 2.5:
23586    num_threads ( expression ) */
23587
23588 static tree
23589 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23590                                   location_t location)
23591 {
23592   tree t, c;
23593
23594   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23595     return list;
23596
23597   t = cp_parser_expression (parser, false, NULL);
23598
23599   if (t == error_mark_node
23600       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23601     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23602                                            /*or_comma=*/false,
23603                                            /*consume_paren=*/true);
23604
23605   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23606                              "num_threads", location);
23607
23608   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23609   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23610   OMP_CLAUSE_CHAIN (c) = list;
23611
23612   return c;
23613 }
23614
23615 /* OpenMP 2.5:
23616    ordered */
23617
23618 static tree
23619 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23620                               tree list, location_t location)
23621 {
23622   tree c;
23623
23624   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23625                              "ordered", location);
23626
23627   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23628   OMP_CLAUSE_CHAIN (c) = list;
23629   return c;
23630 }
23631
23632 /* OpenMP 2.5:
23633    reduction ( reduction-operator : variable-list )
23634
23635    reduction-operator:
23636      One of: + * - & ^ | && || */
23637
23638 static tree
23639 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23640 {
23641   enum tree_code code;
23642   tree nlist, c;
23643
23644   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23645     return list;
23646
23647   switch (cp_lexer_peek_token (parser->lexer)->type)
23648     {
23649     case CPP_PLUS:
23650       code = PLUS_EXPR;
23651       break;
23652     case CPP_MULT:
23653       code = MULT_EXPR;
23654       break;
23655     case CPP_MINUS:
23656       code = MINUS_EXPR;
23657       break;
23658     case CPP_AND:
23659       code = BIT_AND_EXPR;
23660       break;
23661     case CPP_XOR:
23662       code = BIT_XOR_EXPR;
23663       break;
23664     case CPP_OR:
23665       code = BIT_IOR_EXPR;
23666       break;
23667     case CPP_AND_AND:
23668       code = TRUTH_ANDIF_EXPR;
23669       break;
23670     case CPP_OR_OR:
23671       code = TRUTH_ORIF_EXPR;
23672       break;
23673     default:
23674       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23675                                "%<|%>, %<&&%>, or %<||%>");
23676     resync_fail:
23677       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23678                                              /*or_comma=*/false,
23679                                              /*consume_paren=*/true);
23680       return list;
23681     }
23682   cp_lexer_consume_token (parser->lexer);
23683
23684   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23685     goto resync_fail;
23686
23687   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23688   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23689     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23690
23691   return nlist;
23692 }
23693
23694 /* OpenMP 2.5:
23695    schedule ( schedule-kind )
23696    schedule ( schedule-kind , expression )
23697
23698    schedule-kind:
23699      static | dynamic | guided | runtime | auto  */
23700
23701 static tree
23702 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23703 {
23704   tree c, t;
23705
23706   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23707     return list;
23708
23709   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23710
23711   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23712     {
23713       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23714       const char *p = IDENTIFIER_POINTER (id);
23715
23716       switch (p[0])
23717         {
23718         case 'd':
23719           if (strcmp ("dynamic", p) != 0)
23720             goto invalid_kind;
23721           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23722           break;
23723
23724         case 'g':
23725           if (strcmp ("guided", p) != 0)
23726             goto invalid_kind;
23727           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23728           break;
23729
23730         case 'r':
23731           if (strcmp ("runtime", p) != 0)
23732             goto invalid_kind;
23733           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23734           break;
23735
23736         default:
23737           goto invalid_kind;
23738         }
23739     }
23740   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23741     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23742   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23743     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23744   else
23745     goto invalid_kind;
23746   cp_lexer_consume_token (parser->lexer);
23747
23748   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23749     {
23750       cp_token *token;
23751       cp_lexer_consume_token (parser->lexer);
23752
23753       token = cp_lexer_peek_token (parser->lexer);
23754       t = cp_parser_assignment_expression (parser, false, NULL);
23755
23756       if (t == error_mark_node)
23757         goto resync_fail;
23758       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23759         error_at (token->location, "schedule %<runtime%> does not take "
23760                   "a %<chunk_size%> parameter");
23761       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23762         error_at (token->location, "schedule %<auto%> does not take "
23763                   "a %<chunk_size%> parameter");
23764       else
23765         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23766
23767       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23768         goto resync_fail;
23769     }
23770   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23771     goto resync_fail;
23772
23773   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23774   OMP_CLAUSE_CHAIN (c) = list;
23775   return c;
23776
23777  invalid_kind:
23778   cp_parser_error (parser, "invalid schedule kind");
23779  resync_fail:
23780   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23781                                          /*or_comma=*/false,
23782                                          /*consume_paren=*/true);
23783   return list;
23784 }
23785
23786 /* OpenMP 3.0:
23787    untied */
23788
23789 static tree
23790 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23791                              tree list, location_t location)
23792 {
23793   tree c;
23794
23795   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23796
23797   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23798   OMP_CLAUSE_CHAIN (c) = list;
23799   return c;
23800 }
23801
23802 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23803    is a bitmask in MASK.  Return the list of clauses found; the result
23804    of clause default goes in *pdefault.  */
23805
23806 static tree
23807 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23808                            const char *where, cp_token *pragma_tok)
23809 {
23810   tree clauses = NULL;
23811   bool first = true;
23812   cp_token *token = NULL;
23813
23814   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23815     {
23816       pragma_omp_clause c_kind;
23817       const char *c_name;
23818       tree prev = clauses;
23819
23820       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23821         cp_lexer_consume_token (parser->lexer);
23822
23823       token = cp_lexer_peek_token (parser->lexer);
23824       c_kind = cp_parser_omp_clause_name (parser);
23825       first = false;
23826
23827       switch (c_kind)
23828         {
23829         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23830           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23831                                                    token->location);
23832           c_name = "collapse";
23833           break;
23834         case PRAGMA_OMP_CLAUSE_COPYIN:
23835           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23836           c_name = "copyin";
23837           break;
23838         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23839           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23840                                             clauses);
23841           c_name = "copyprivate";
23842           break;
23843         case PRAGMA_OMP_CLAUSE_DEFAULT:
23844           clauses = cp_parser_omp_clause_default (parser, clauses,
23845                                                   token->location);
23846           c_name = "default";
23847           break;
23848         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23849           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23850                                             clauses);
23851           c_name = "firstprivate";
23852           break;
23853         case PRAGMA_OMP_CLAUSE_IF:
23854           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23855           c_name = "if";
23856           break;
23857         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23858           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23859                                             clauses);
23860           c_name = "lastprivate";
23861           break;
23862         case PRAGMA_OMP_CLAUSE_NOWAIT:
23863           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23864           c_name = "nowait";
23865           break;
23866         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23867           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23868                                                       token->location);
23869           c_name = "num_threads";
23870           break;
23871         case PRAGMA_OMP_CLAUSE_ORDERED:
23872           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23873                                                   token->location);
23874           c_name = "ordered";
23875           break;
23876         case PRAGMA_OMP_CLAUSE_PRIVATE:
23877           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23878                                             clauses);
23879           c_name = "private";
23880           break;
23881         case PRAGMA_OMP_CLAUSE_REDUCTION:
23882           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23883           c_name = "reduction";
23884           break;
23885         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23886           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23887                                                    token->location);
23888           c_name = "schedule";
23889           break;
23890         case PRAGMA_OMP_CLAUSE_SHARED:
23891           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23892                                             clauses);
23893           c_name = "shared";
23894           break;
23895         case PRAGMA_OMP_CLAUSE_UNTIED:
23896           clauses = cp_parser_omp_clause_untied (parser, clauses,
23897                                                  token->location);
23898           c_name = "nowait";
23899           break;
23900         default:
23901           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23902           goto saw_error;
23903         }
23904
23905       if (((mask >> c_kind) & 1) == 0)
23906         {
23907           /* Remove the invalid clause(s) from the list to avoid
23908              confusing the rest of the compiler.  */
23909           clauses = prev;
23910           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23911         }
23912     }
23913  saw_error:
23914   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23915   return finish_omp_clauses (clauses);
23916 }
23917
23918 /* OpenMP 2.5:
23919    structured-block:
23920      statement
23921
23922    In practice, we're also interested in adding the statement to an
23923    outer node.  So it is convenient if we work around the fact that
23924    cp_parser_statement calls add_stmt.  */
23925
23926 static unsigned
23927 cp_parser_begin_omp_structured_block (cp_parser *parser)
23928 {
23929   unsigned save = parser->in_statement;
23930
23931   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23932      This preserves the "not within loop or switch" style error messages
23933      for nonsense cases like
23934         void foo() {
23935         #pragma omp single
23936           break;
23937         }
23938   */
23939   if (parser->in_statement)
23940     parser->in_statement = IN_OMP_BLOCK;
23941
23942   return save;
23943 }
23944
23945 static void
23946 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23947 {
23948   parser->in_statement = save;
23949 }
23950
23951 static tree
23952 cp_parser_omp_structured_block (cp_parser *parser)
23953 {
23954   tree stmt = begin_omp_structured_block ();
23955   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23956
23957   cp_parser_statement (parser, NULL_TREE, false, NULL);
23958
23959   cp_parser_end_omp_structured_block (parser, save);
23960   return finish_omp_structured_block (stmt);
23961 }
23962
23963 /* OpenMP 2.5:
23964    # pragma omp atomic new-line
23965      expression-stmt
23966
23967    expression-stmt:
23968      x binop= expr | x++ | ++x | x-- | --x
23969    binop:
23970      +, *, -, /, &, ^, |, <<, >>
23971
23972   where x is an lvalue expression with scalar type.  */
23973
23974 static void
23975 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23976 {
23977   tree lhs, rhs;
23978   enum tree_code code;
23979
23980   cp_parser_require_pragma_eol (parser, pragma_tok);
23981
23982   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23983                                     /*cast_p=*/false, NULL);
23984   switch (TREE_CODE (lhs))
23985     {
23986     case ERROR_MARK:
23987       goto saw_error;
23988
23989     case PREINCREMENT_EXPR:
23990     case POSTINCREMENT_EXPR:
23991       lhs = TREE_OPERAND (lhs, 0);
23992       code = PLUS_EXPR;
23993       rhs = integer_one_node;
23994       break;
23995
23996     case PREDECREMENT_EXPR:
23997     case POSTDECREMENT_EXPR:
23998       lhs = TREE_OPERAND (lhs, 0);
23999       code = MINUS_EXPR;
24000       rhs = integer_one_node;
24001       break;
24002
24003     case COMPOUND_EXPR:
24004       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24005          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24006          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24007          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24008          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24009                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24010             == BOOLEAN_TYPE)
24011        /* Undo effects of boolean_increment for post {in,de}crement.  */
24012        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24013       /* FALLTHRU */
24014     case MODIFY_EXPR:
24015       if (TREE_CODE (lhs) == MODIFY_EXPR
24016          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24017        {
24018          /* Undo effects of boolean_increment.  */
24019          if (integer_onep (TREE_OPERAND (lhs, 1)))
24020            {
24021              /* This is pre or post increment.  */
24022              rhs = TREE_OPERAND (lhs, 1);
24023              lhs = TREE_OPERAND (lhs, 0);
24024              code = NOP_EXPR;
24025              break;
24026            }
24027        }
24028       /* FALLTHRU */
24029     default:
24030       switch (cp_lexer_peek_token (parser->lexer)->type)
24031         {
24032         case CPP_MULT_EQ:
24033           code = MULT_EXPR;
24034           break;
24035         case CPP_DIV_EQ:
24036           code = TRUNC_DIV_EXPR;
24037           break;
24038         case CPP_PLUS_EQ:
24039           code = PLUS_EXPR;
24040           break;
24041         case CPP_MINUS_EQ:
24042           code = MINUS_EXPR;
24043           break;
24044         case CPP_LSHIFT_EQ:
24045           code = LSHIFT_EXPR;
24046           break;
24047         case CPP_RSHIFT_EQ:
24048           code = RSHIFT_EXPR;
24049           break;
24050         case CPP_AND_EQ:
24051           code = BIT_AND_EXPR;
24052           break;
24053         case CPP_OR_EQ:
24054           code = BIT_IOR_EXPR;
24055           break;
24056         case CPP_XOR_EQ:
24057           code = BIT_XOR_EXPR;
24058           break;
24059         default:
24060           cp_parser_error (parser,
24061                            "invalid operator for %<#pragma omp atomic%>");
24062           goto saw_error;
24063         }
24064       cp_lexer_consume_token (parser->lexer);
24065
24066       rhs = cp_parser_expression (parser, false, NULL);
24067       if (rhs == error_mark_node)
24068         goto saw_error;
24069       break;
24070     }
24071   finish_omp_atomic (code, lhs, rhs);
24072   cp_parser_consume_semicolon_at_end_of_statement (parser);
24073   return;
24074
24075  saw_error:
24076   cp_parser_skip_to_end_of_block_or_statement (parser);
24077 }
24078
24079
24080 /* OpenMP 2.5:
24081    # pragma omp barrier new-line  */
24082
24083 static void
24084 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24085 {
24086   cp_parser_require_pragma_eol (parser, pragma_tok);
24087   finish_omp_barrier ();
24088 }
24089
24090 /* OpenMP 2.5:
24091    # pragma omp critical [(name)] new-line
24092      structured-block  */
24093
24094 static tree
24095 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24096 {
24097   tree stmt, name = NULL;
24098
24099   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24100     {
24101       cp_lexer_consume_token (parser->lexer);
24102
24103       name = cp_parser_identifier (parser);
24104
24105       if (name == error_mark_node
24106           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24107         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24108                                                /*or_comma=*/false,
24109                                                /*consume_paren=*/true);
24110       if (name == error_mark_node)
24111         name = NULL;
24112     }
24113   cp_parser_require_pragma_eol (parser, pragma_tok);
24114
24115   stmt = cp_parser_omp_structured_block (parser);
24116   return c_finish_omp_critical (input_location, stmt, name);
24117 }
24118
24119 /* OpenMP 2.5:
24120    # pragma omp flush flush-vars[opt] new-line
24121
24122    flush-vars:
24123      ( variable-list ) */
24124
24125 static void
24126 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24127 {
24128   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24129     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24130   cp_parser_require_pragma_eol (parser, pragma_tok);
24131
24132   finish_omp_flush ();
24133 }
24134
24135 /* Helper function, to parse omp for increment expression.  */
24136
24137 static tree
24138 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24139 {
24140   tree cond = cp_parser_binary_expression (parser, false, true,
24141                                            PREC_NOT_OPERATOR, NULL);
24142   bool overloaded_p;
24143
24144   if (cond == error_mark_node
24145       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24146     {
24147       cp_parser_skip_to_end_of_statement (parser);
24148       return error_mark_node;
24149     }
24150
24151   switch (TREE_CODE (cond))
24152     {
24153     case GT_EXPR:
24154     case GE_EXPR:
24155     case LT_EXPR:
24156     case LE_EXPR:
24157       break;
24158     default:
24159       return error_mark_node;
24160     }
24161
24162   /* If decl is an iterator, preserve LHS and RHS of the relational
24163      expr until finish_omp_for.  */
24164   if (decl
24165       && (type_dependent_expression_p (decl)
24166           || CLASS_TYPE_P (TREE_TYPE (decl))))
24167     return cond;
24168
24169   return build_x_binary_op (TREE_CODE (cond),
24170                             TREE_OPERAND (cond, 0), ERROR_MARK,
24171                             TREE_OPERAND (cond, 1), ERROR_MARK,
24172                             &overloaded_p, tf_warning_or_error);
24173 }
24174
24175 /* Helper function, to parse omp for increment expression.  */
24176
24177 static tree
24178 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24179 {
24180   cp_token *token = cp_lexer_peek_token (parser->lexer);
24181   enum tree_code op;
24182   tree lhs, rhs;
24183   cp_id_kind idk;
24184   bool decl_first;
24185
24186   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24187     {
24188       op = (token->type == CPP_PLUS_PLUS
24189             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24190       cp_lexer_consume_token (parser->lexer);
24191       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24192       if (lhs != decl)
24193         return error_mark_node;
24194       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24195     }
24196
24197   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24198   if (lhs != decl)
24199     return error_mark_node;
24200
24201   token = cp_lexer_peek_token (parser->lexer);
24202   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24203     {
24204       op = (token->type == CPP_PLUS_PLUS
24205             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24206       cp_lexer_consume_token (parser->lexer);
24207       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24208     }
24209
24210   op = cp_parser_assignment_operator_opt (parser);
24211   if (op == ERROR_MARK)
24212     return error_mark_node;
24213
24214   if (op != NOP_EXPR)
24215     {
24216       rhs = cp_parser_assignment_expression (parser, false, NULL);
24217       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24218       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24219     }
24220
24221   lhs = cp_parser_binary_expression (parser, false, false,
24222                                      PREC_ADDITIVE_EXPRESSION, NULL);
24223   token = cp_lexer_peek_token (parser->lexer);
24224   decl_first = lhs == decl;
24225   if (decl_first)
24226     lhs = NULL_TREE;
24227   if (token->type != CPP_PLUS
24228       && token->type != CPP_MINUS)
24229     return error_mark_node;
24230
24231   do
24232     {
24233       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24234       cp_lexer_consume_token (parser->lexer);
24235       rhs = cp_parser_binary_expression (parser, false, false,
24236                                          PREC_ADDITIVE_EXPRESSION, NULL);
24237       token = cp_lexer_peek_token (parser->lexer);
24238       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24239         {
24240           if (lhs == NULL_TREE)
24241             {
24242               if (op == PLUS_EXPR)
24243                 lhs = rhs;
24244               else
24245                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24246             }
24247           else
24248             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24249                                      NULL, tf_warning_or_error);
24250         }
24251     }
24252   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24253
24254   if (!decl_first)
24255     {
24256       if (rhs != decl || op == MINUS_EXPR)
24257         return error_mark_node;
24258       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24259     }
24260   else
24261     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24262
24263   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24264 }
24265
24266 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24267
24268 static tree
24269 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24270 {
24271   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24272   tree real_decl, initv, condv, incrv, declv;
24273   tree this_pre_body, cl;
24274   location_t loc_first;
24275   bool collapse_err = false;
24276   int i, collapse = 1, nbraces = 0;
24277   VEC(tree,gc) *for_block = make_tree_vector ();
24278
24279   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24280     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24281       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24282
24283   gcc_assert (collapse >= 1);
24284
24285   declv = make_tree_vec (collapse);
24286   initv = make_tree_vec (collapse);
24287   condv = make_tree_vec (collapse);
24288   incrv = make_tree_vec (collapse);
24289
24290   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24291
24292   for (i = 0; i < collapse; i++)
24293     {
24294       int bracecount = 0;
24295       bool add_private_clause = false;
24296       location_t loc;
24297
24298       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24299         {
24300           cp_parser_error (parser, "for statement expected");
24301           return NULL;
24302         }
24303       loc = cp_lexer_consume_token (parser->lexer)->location;
24304
24305       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24306         return NULL;
24307
24308       init = decl = real_decl = NULL;
24309       this_pre_body = push_stmt_list ();
24310       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24311         {
24312           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24313
24314              init-expr:
24315                        var = lb
24316                        integer-type var = lb
24317                        random-access-iterator-type var = lb
24318                        pointer-type var = lb
24319           */
24320           cp_decl_specifier_seq type_specifiers;
24321
24322           /* First, try to parse as an initialized declaration.  See
24323              cp_parser_condition, from whence the bulk of this is copied.  */
24324
24325           cp_parser_parse_tentatively (parser);
24326           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24327                                         /*is_trailing_return=*/false,
24328                                         &type_specifiers);
24329           if (cp_parser_parse_definitely (parser))
24330             {
24331               /* If parsing a type specifier seq succeeded, then this
24332                  MUST be a initialized declaration.  */
24333               tree asm_specification, attributes;
24334               cp_declarator *declarator;
24335
24336               declarator = cp_parser_declarator (parser,
24337                                                  CP_PARSER_DECLARATOR_NAMED,
24338                                                  /*ctor_dtor_or_conv_p=*/NULL,
24339                                                  /*parenthesized_p=*/NULL,
24340                                                  /*member_p=*/false);
24341               attributes = cp_parser_attributes_opt (parser);
24342               asm_specification = cp_parser_asm_specification_opt (parser);
24343
24344               if (declarator == cp_error_declarator) 
24345                 cp_parser_skip_to_end_of_statement (parser);
24346
24347               else 
24348                 {
24349                   tree pushed_scope, auto_node;
24350
24351                   decl = start_decl (declarator, &type_specifiers,
24352                                      SD_INITIALIZED, attributes,
24353                                      /*prefix_attributes=*/NULL_TREE,
24354                                      &pushed_scope);
24355
24356                   auto_node = type_uses_auto (TREE_TYPE (decl));
24357                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24358                     {
24359                       if (cp_lexer_next_token_is (parser->lexer, 
24360                                                   CPP_OPEN_PAREN))
24361                         error ("parenthesized initialization is not allowed in "
24362                                "OpenMP %<for%> loop");
24363                       else
24364                         /* Trigger an error.  */
24365                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24366
24367                       init = error_mark_node;
24368                       cp_parser_skip_to_end_of_statement (parser);
24369                     }
24370                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24371                            || type_dependent_expression_p (decl)
24372                            || auto_node)
24373                     {
24374                       bool is_direct_init, is_non_constant_init;
24375
24376                       init = cp_parser_initializer (parser,
24377                                                     &is_direct_init,
24378                                                     &is_non_constant_init);
24379
24380                       if (auto_node && describable_type (init))
24381                         {
24382                           TREE_TYPE (decl)
24383                             = do_auto_deduction (TREE_TYPE (decl), init,
24384                                                  auto_node);
24385
24386                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24387                               && !type_dependent_expression_p (decl))
24388                             goto non_class;
24389                         }
24390                       
24391                       cp_finish_decl (decl, init, !is_non_constant_init,
24392                                       asm_specification,
24393                                       LOOKUP_ONLYCONVERTING);
24394                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24395                         {
24396                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24397                           init = NULL_TREE;
24398                         }
24399                       else
24400                         init = pop_stmt_list (this_pre_body);
24401                       this_pre_body = NULL_TREE;
24402                     }
24403                   else
24404                     {
24405                       /* Consume '='.  */
24406                       cp_lexer_consume_token (parser->lexer);
24407                       init = cp_parser_assignment_expression (parser, false, NULL);
24408
24409                     non_class:
24410                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24411                         init = error_mark_node;
24412                       else
24413                         cp_finish_decl (decl, NULL_TREE,
24414                                         /*init_const_expr_p=*/false,
24415                                         asm_specification,
24416                                         LOOKUP_ONLYCONVERTING);
24417                     }
24418
24419                   if (pushed_scope)
24420                     pop_scope (pushed_scope);
24421                 }
24422             }
24423           else 
24424             {
24425               cp_id_kind idk;
24426               /* If parsing a type specifier sequence failed, then
24427                  this MUST be a simple expression.  */
24428               cp_parser_parse_tentatively (parser);
24429               decl = cp_parser_primary_expression (parser, false, false,
24430                                                    false, &idk);
24431               if (!cp_parser_error_occurred (parser)
24432                   && decl
24433                   && DECL_P (decl)
24434                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24435                 {
24436                   tree rhs;
24437
24438                   cp_parser_parse_definitely (parser);
24439                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24440                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24441                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24442                                                          rhs,
24443                                                          tf_warning_or_error));
24444                   add_private_clause = true;
24445                 }
24446               else
24447                 {
24448                   decl = NULL;
24449                   cp_parser_abort_tentative_parse (parser);
24450                   init = cp_parser_expression (parser, false, NULL);
24451                   if (init)
24452                     {
24453                       if (TREE_CODE (init) == MODIFY_EXPR
24454                           || TREE_CODE (init) == MODOP_EXPR)
24455                         real_decl = TREE_OPERAND (init, 0);
24456                     }
24457                 }
24458             }
24459         }
24460       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24461       if (this_pre_body)
24462         {
24463           this_pre_body = pop_stmt_list (this_pre_body);
24464           if (pre_body)
24465             {
24466               tree t = pre_body;
24467               pre_body = push_stmt_list ();
24468               add_stmt (t);
24469               add_stmt (this_pre_body);
24470               pre_body = pop_stmt_list (pre_body);
24471             }
24472           else
24473             pre_body = this_pre_body;
24474         }
24475
24476       if (decl)
24477         real_decl = decl;
24478       if (par_clauses != NULL && real_decl != NULL_TREE)
24479         {
24480           tree *c;
24481           for (c = par_clauses; *c ; )
24482             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24483                 && OMP_CLAUSE_DECL (*c) == real_decl)
24484               {
24485                 error_at (loc, "iteration variable %qD"
24486                           " should not be firstprivate", real_decl);
24487                 *c = OMP_CLAUSE_CHAIN (*c);
24488               }
24489             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24490                      && OMP_CLAUSE_DECL (*c) == real_decl)
24491               {
24492                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24493                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24494                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24495                 OMP_CLAUSE_DECL (l) = real_decl;
24496                 OMP_CLAUSE_CHAIN (l) = clauses;
24497                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24498                 clauses = l;
24499                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24500                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24501                 add_private_clause = false;
24502               }
24503             else
24504               {
24505                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24506                     && OMP_CLAUSE_DECL (*c) == real_decl)
24507                   add_private_clause = false;
24508                 c = &OMP_CLAUSE_CHAIN (*c);
24509               }
24510         }
24511
24512       if (add_private_clause)
24513         {
24514           tree c;
24515           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24516             {
24517               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24518                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24519                   && OMP_CLAUSE_DECL (c) == decl)
24520                 break;
24521               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24522                        && OMP_CLAUSE_DECL (c) == decl)
24523                 error_at (loc, "iteration variable %qD "
24524                           "should not be firstprivate",
24525                           decl);
24526               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24527                        && OMP_CLAUSE_DECL (c) == decl)
24528                 error_at (loc, "iteration variable %qD should not be reduction",
24529                           decl);
24530             }
24531           if (c == NULL)
24532             {
24533               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24534               OMP_CLAUSE_DECL (c) = decl;
24535               c = finish_omp_clauses (c);
24536               if (c)
24537                 {
24538                   OMP_CLAUSE_CHAIN (c) = clauses;
24539                   clauses = c;
24540                 }
24541             }
24542         }
24543
24544       cond = NULL;
24545       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24546         cond = cp_parser_omp_for_cond (parser, decl);
24547       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24548
24549       incr = NULL;
24550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24551         {
24552           /* If decl is an iterator, preserve the operator on decl
24553              until finish_omp_for.  */
24554           if (decl
24555               && ((type_dependent_expression_p (decl)
24556                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24557                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24558             incr = cp_parser_omp_for_incr (parser, decl);
24559           else
24560             incr = cp_parser_expression (parser, false, NULL);
24561         }
24562
24563       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24564         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24565                                                /*or_comma=*/false,
24566                                                /*consume_paren=*/true);
24567
24568       TREE_VEC_ELT (declv, i) = decl;
24569       TREE_VEC_ELT (initv, i) = init;
24570       TREE_VEC_ELT (condv, i) = cond;
24571       TREE_VEC_ELT (incrv, i) = incr;
24572
24573       if (i == collapse - 1)
24574         break;
24575
24576       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24577          in between the collapsed for loops to be still considered perfectly
24578          nested.  Hopefully the final version clarifies this.
24579          For now handle (multiple) {'s and empty statements.  */
24580       cp_parser_parse_tentatively (parser);
24581       do
24582         {
24583           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24584             break;
24585           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24586             {
24587               cp_lexer_consume_token (parser->lexer);
24588               bracecount++;
24589             }
24590           else if (bracecount
24591                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24592             cp_lexer_consume_token (parser->lexer);
24593           else
24594             {
24595               loc = cp_lexer_peek_token (parser->lexer)->location;
24596               error_at (loc, "not enough collapsed for loops");
24597               collapse_err = true;
24598               cp_parser_abort_tentative_parse (parser);
24599               declv = NULL_TREE;
24600               break;
24601             }
24602         }
24603       while (1);
24604
24605       if (declv)
24606         {
24607           cp_parser_parse_definitely (parser);
24608           nbraces += bracecount;
24609         }
24610     }
24611
24612   /* Note that we saved the original contents of this flag when we entered
24613      the structured block, and so we don't need to re-save it here.  */
24614   parser->in_statement = IN_OMP_FOR;
24615
24616   /* Note that the grammar doesn't call for a structured block here,
24617      though the loop as a whole is a structured block.  */
24618   body = push_stmt_list ();
24619   cp_parser_statement (parser, NULL_TREE, false, NULL);
24620   body = pop_stmt_list (body);
24621
24622   if (declv == NULL_TREE)
24623     ret = NULL_TREE;
24624   else
24625     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24626                           pre_body, clauses);
24627
24628   while (nbraces)
24629     {
24630       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24631         {
24632           cp_lexer_consume_token (parser->lexer);
24633           nbraces--;
24634         }
24635       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24636         cp_lexer_consume_token (parser->lexer);
24637       else
24638         {
24639           if (!collapse_err)
24640             {
24641               error_at (cp_lexer_peek_token (parser->lexer)->location,
24642                         "collapsed loops not perfectly nested");
24643             }
24644           collapse_err = true;
24645           cp_parser_statement_seq_opt (parser, NULL);
24646           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24647             break;
24648         }
24649     }
24650
24651   while (!VEC_empty (tree, for_block))
24652     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24653   release_tree_vector (for_block);
24654
24655   return ret;
24656 }
24657
24658 /* OpenMP 2.5:
24659    #pragma omp for for-clause[optseq] new-line
24660      for-loop  */
24661
24662 #define OMP_FOR_CLAUSE_MASK                             \
24663         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24664         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24665         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24666         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24667         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24668         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24669         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24670         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24671
24672 static tree
24673 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24674 {
24675   tree clauses, sb, ret;
24676   unsigned int save;
24677
24678   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24679                                        "#pragma omp for", pragma_tok);
24680
24681   sb = begin_omp_structured_block ();
24682   save = cp_parser_begin_omp_structured_block (parser);
24683
24684   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24685
24686   cp_parser_end_omp_structured_block (parser, save);
24687   add_stmt (finish_omp_structured_block (sb));
24688
24689   return ret;
24690 }
24691
24692 /* OpenMP 2.5:
24693    # pragma omp master new-line
24694      structured-block  */
24695
24696 static tree
24697 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24698 {
24699   cp_parser_require_pragma_eol (parser, pragma_tok);
24700   return c_finish_omp_master (input_location,
24701                               cp_parser_omp_structured_block (parser));
24702 }
24703
24704 /* OpenMP 2.5:
24705    # pragma omp ordered new-line
24706      structured-block  */
24707
24708 static tree
24709 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24710 {
24711   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24712   cp_parser_require_pragma_eol (parser, pragma_tok);
24713   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24714 }
24715
24716 /* OpenMP 2.5:
24717
24718    section-scope:
24719      { section-sequence }
24720
24721    section-sequence:
24722      section-directive[opt] structured-block
24723      section-sequence section-directive structured-block  */
24724
24725 static tree
24726 cp_parser_omp_sections_scope (cp_parser *parser)
24727 {
24728   tree stmt, substmt;
24729   bool error_suppress = false;
24730   cp_token *tok;
24731
24732   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24733     return NULL_TREE;
24734
24735   stmt = push_stmt_list ();
24736
24737   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24738     {
24739       unsigned save;
24740
24741       substmt = begin_omp_structured_block ();
24742       save = cp_parser_begin_omp_structured_block (parser);
24743
24744       while (1)
24745         {
24746           cp_parser_statement (parser, NULL_TREE, false, NULL);
24747
24748           tok = cp_lexer_peek_token (parser->lexer);
24749           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24750             break;
24751           if (tok->type == CPP_CLOSE_BRACE)
24752             break;
24753           if (tok->type == CPP_EOF)
24754             break;
24755         }
24756
24757       cp_parser_end_omp_structured_block (parser, save);
24758       substmt = finish_omp_structured_block (substmt);
24759       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24760       add_stmt (substmt);
24761     }
24762
24763   while (1)
24764     {
24765       tok = cp_lexer_peek_token (parser->lexer);
24766       if (tok->type == CPP_CLOSE_BRACE)
24767         break;
24768       if (tok->type == CPP_EOF)
24769         break;
24770
24771       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24772         {
24773           cp_lexer_consume_token (parser->lexer);
24774           cp_parser_require_pragma_eol (parser, tok);
24775           error_suppress = false;
24776         }
24777       else if (!error_suppress)
24778         {
24779           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24780           error_suppress = true;
24781         }
24782
24783       substmt = cp_parser_omp_structured_block (parser);
24784       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24785       add_stmt (substmt);
24786     }
24787   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24788
24789   substmt = pop_stmt_list (stmt);
24790
24791   stmt = make_node (OMP_SECTIONS);
24792   TREE_TYPE (stmt) = void_type_node;
24793   OMP_SECTIONS_BODY (stmt) = substmt;
24794
24795   add_stmt (stmt);
24796   return stmt;
24797 }
24798
24799 /* OpenMP 2.5:
24800    # pragma omp sections sections-clause[optseq] newline
24801      sections-scope  */
24802
24803 #define OMP_SECTIONS_CLAUSE_MASK                        \
24804         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24805         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24806         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24807         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24808         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24809
24810 static tree
24811 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24812 {
24813   tree clauses, ret;
24814
24815   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24816                                        "#pragma omp sections", pragma_tok);
24817
24818   ret = cp_parser_omp_sections_scope (parser);
24819   if (ret)
24820     OMP_SECTIONS_CLAUSES (ret) = clauses;
24821
24822   return ret;
24823 }
24824
24825 /* OpenMP 2.5:
24826    # pragma parallel parallel-clause new-line
24827    # pragma parallel for parallel-for-clause new-line
24828    # pragma parallel sections parallel-sections-clause new-line  */
24829
24830 #define OMP_PARALLEL_CLAUSE_MASK                        \
24831         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24832         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24833         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24834         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24835         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24836         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24837         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24838         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24839
24840 static tree
24841 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24842 {
24843   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24844   const char *p_name = "#pragma omp parallel";
24845   tree stmt, clauses, par_clause, ws_clause, block;
24846   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24847   unsigned int save;
24848   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24849
24850   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24851     {
24852       cp_lexer_consume_token (parser->lexer);
24853       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24854       p_name = "#pragma omp parallel for";
24855       mask |= OMP_FOR_CLAUSE_MASK;
24856       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24857     }
24858   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24859     {
24860       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24861       const char *p = IDENTIFIER_POINTER (id);
24862       if (strcmp (p, "sections") == 0)
24863         {
24864           cp_lexer_consume_token (parser->lexer);
24865           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24866           p_name = "#pragma omp parallel sections";
24867           mask |= OMP_SECTIONS_CLAUSE_MASK;
24868           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24869         }
24870     }
24871
24872   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24873   block = begin_omp_parallel ();
24874   save = cp_parser_begin_omp_structured_block (parser);
24875
24876   switch (p_kind)
24877     {
24878     case PRAGMA_OMP_PARALLEL:
24879       cp_parser_statement (parser, NULL_TREE, false, NULL);
24880       par_clause = clauses;
24881       break;
24882
24883     case PRAGMA_OMP_PARALLEL_FOR:
24884       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24885       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24886       break;
24887
24888     case PRAGMA_OMP_PARALLEL_SECTIONS:
24889       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24890       stmt = cp_parser_omp_sections_scope (parser);
24891       if (stmt)
24892         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24893       break;
24894
24895     default:
24896       gcc_unreachable ();
24897     }
24898
24899   cp_parser_end_omp_structured_block (parser, save);
24900   stmt = finish_omp_parallel (par_clause, block);
24901   if (p_kind != PRAGMA_OMP_PARALLEL)
24902     OMP_PARALLEL_COMBINED (stmt) = 1;
24903   return stmt;
24904 }
24905
24906 /* OpenMP 2.5:
24907    # pragma omp single single-clause[optseq] new-line
24908      structured-block  */
24909
24910 #define OMP_SINGLE_CLAUSE_MASK                          \
24911         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24912         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24913         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24914         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24915
24916 static tree
24917 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24918 {
24919   tree stmt = make_node (OMP_SINGLE);
24920   TREE_TYPE (stmt) = void_type_node;
24921
24922   OMP_SINGLE_CLAUSES (stmt)
24923     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24924                                  "#pragma omp single", pragma_tok);
24925   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24926
24927   return add_stmt (stmt);
24928 }
24929
24930 /* OpenMP 3.0:
24931    # pragma omp task task-clause[optseq] new-line
24932      structured-block  */
24933
24934 #define OMP_TASK_CLAUSE_MASK                            \
24935         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24936         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24937         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24938         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24939         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24940         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24941
24942 static tree
24943 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24944 {
24945   tree clauses, block;
24946   unsigned int save;
24947
24948   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24949                                        "#pragma omp task", pragma_tok);
24950   block = begin_omp_task ();
24951   save = cp_parser_begin_omp_structured_block (parser);
24952   cp_parser_statement (parser, NULL_TREE, false, NULL);
24953   cp_parser_end_omp_structured_block (parser, save);
24954   return finish_omp_task (clauses, block);
24955 }
24956
24957 /* OpenMP 3.0:
24958    # pragma omp taskwait new-line  */
24959
24960 static void
24961 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24962 {
24963   cp_parser_require_pragma_eol (parser, pragma_tok);
24964   finish_omp_taskwait ();
24965 }
24966
24967 /* OpenMP 2.5:
24968    # pragma omp threadprivate (variable-list) */
24969
24970 static void
24971 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24972 {
24973   tree vars;
24974
24975   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24976   cp_parser_require_pragma_eol (parser, pragma_tok);
24977
24978   finish_omp_threadprivate (vars);
24979 }
24980
24981 /* Main entry point to OpenMP statement pragmas.  */
24982
24983 static void
24984 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24985 {
24986   tree stmt;
24987
24988   switch (pragma_tok->pragma_kind)
24989     {
24990     case PRAGMA_OMP_ATOMIC:
24991       cp_parser_omp_atomic (parser, pragma_tok);
24992       return;
24993     case PRAGMA_OMP_CRITICAL:
24994       stmt = cp_parser_omp_critical (parser, pragma_tok);
24995       break;
24996     case PRAGMA_OMP_FOR:
24997       stmt = cp_parser_omp_for (parser, pragma_tok);
24998       break;
24999     case PRAGMA_OMP_MASTER:
25000       stmt = cp_parser_omp_master (parser, pragma_tok);
25001       break;
25002     case PRAGMA_OMP_ORDERED:
25003       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25004       break;
25005     case PRAGMA_OMP_PARALLEL:
25006       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25007       break;
25008     case PRAGMA_OMP_SECTIONS:
25009       stmt = cp_parser_omp_sections (parser, pragma_tok);
25010       break;
25011     case PRAGMA_OMP_SINGLE:
25012       stmt = cp_parser_omp_single (parser, pragma_tok);
25013       break;
25014     case PRAGMA_OMP_TASK:
25015       stmt = cp_parser_omp_task (parser, pragma_tok);
25016       break;
25017     default:
25018       gcc_unreachable ();
25019     }
25020
25021   if (stmt)
25022     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25023 }
25024 \f
25025 /* The parser.  */
25026
25027 static GTY (()) cp_parser *the_parser;
25028
25029 \f
25030 /* Special handling for the first token or line in the file.  The first
25031    thing in the file might be #pragma GCC pch_preprocess, which loads a
25032    PCH file, which is a GC collection point.  So we need to handle this
25033    first pragma without benefit of an existing lexer structure.
25034
25035    Always returns one token to the caller in *FIRST_TOKEN.  This is
25036    either the true first token of the file, or the first token after
25037    the initial pragma.  */
25038
25039 static void
25040 cp_parser_initial_pragma (cp_token *first_token)
25041 {
25042   tree name = NULL;
25043
25044   cp_lexer_get_preprocessor_token (NULL, first_token);
25045   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25046     return;
25047
25048   cp_lexer_get_preprocessor_token (NULL, first_token);
25049   if (first_token->type == CPP_STRING)
25050     {
25051       name = first_token->u.value;
25052
25053       cp_lexer_get_preprocessor_token (NULL, first_token);
25054       if (first_token->type != CPP_PRAGMA_EOL)
25055         error_at (first_token->location,
25056                   "junk at end of %<#pragma GCC pch_preprocess%>");
25057     }
25058   else
25059     error_at (first_token->location, "expected string literal");
25060
25061   /* Skip to the end of the pragma.  */
25062   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25063     cp_lexer_get_preprocessor_token (NULL, first_token);
25064
25065   /* Now actually load the PCH file.  */
25066   if (name)
25067     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25068
25069   /* Read one more token to return to our caller.  We have to do this
25070      after reading the PCH file in, since its pointers have to be
25071      live.  */
25072   cp_lexer_get_preprocessor_token (NULL, first_token);
25073 }
25074
25075 /* Normal parsing of a pragma token.  Here we can (and must) use the
25076    regular lexer.  */
25077
25078 static bool
25079 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25080 {
25081   cp_token *pragma_tok;
25082   unsigned int id;
25083
25084   pragma_tok = cp_lexer_consume_token (parser->lexer);
25085   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25086   parser->lexer->in_pragma = true;
25087
25088   id = pragma_tok->pragma_kind;
25089   switch (id)
25090     {
25091     case PRAGMA_GCC_PCH_PREPROCESS:
25092       error_at (pragma_tok->location,
25093                 "%<#pragma GCC pch_preprocess%> must be first");
25094       break;
25095
25096     case PRAGMA_OMP_BARRIER:
25097       switch (context)
25098         {
25099         case pragma_compound:
25100           cp_parser_omp_barrier (parser, pragma_tok);
25101           return false;
25102         case pragma_stmt:
25103           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25104                     "used in compound statements");
25105           break;
25106         default:
25107           goto bad_stmt;
25108         }
25109       break;
25110
25111     case PRAGMA_OMP_FLUSH:
25112       switch (context)
25113         {
25114         case pragma_compound:
25115           cp_parser_omp_flush (parser, pragma_tok);
25116           return false;
25117         case pragma_stmt:
25118           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25119                     "used in compound statements");
25120           break;
25121         default:
25122           goto bad_stmt;
25123         }
25124       break;
25125
25126     case PRAGMA_OMP_TASKWAIT:
25127       switch (context)
25128         {
25129         case pragma_compound:
25130           cp_parser_omp_taskwait (parser, pragma_tok);
25131           return false;
25132         case pragma_stmt:
25133           error_at (pragma_tok->location,
25134                     "%<#pragma omp taskwait%> may only be "
25135                     "used in compound statements");
25136           break;
25137         default:
25138           goto bad_stmt;
25139         }
25140       break;
25141
25142     case PRAGMA_OMP_THREADPRIVATE:
25143       cp_parser_omp_threadprivate (parser, pragma_tok);
25144       return false;
25145
25146     case PRAGMA_OMP_ATOMIC:
25147     case PRAGMA_OMP_CRITICAL:
25148     case PRAGMA_OMP_FOR:
25149     case PRAGMA_OMP_MASTER:
25150     case PRAGMA_OMP_ORDERED:
25151     case PRAGMA_OMP_PARALLEL:
25152     case PRAGMA_OMP_SECTIONS:
25153     case PRAGMA_OMP_SINGLE:
25154     case PRAGMA_OMP_TASK:
25155       if (context == pragma_external)
25156         goto bad_stmt;
25157       cp_parser_omp_construct (parser, pragma_tok);
25158       return true;
25159
25160     case PRAGMA_OMP_SECTION:
25161       error_at (pragma_tok->location, 
25162                 "%<#pragma omp section%> may only be used in "
25163                 "%<#pragma omp sections%> construct");
25164       break;
25165
25166     default:
25167       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25168       c_invoke_pragma_handler (id);
25169       break;
25170
25171     bad_stmt:
25172       cp_parser_error (parser, "expected declaration specifiers");
25173       break;
25174     }
25175
25176   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25177   return false;
25178 }
25179
25180 /* The interface the pragma parsers have to the lexer.  */
25181
25182 enum cpp_ttype
25183 pragma_lex (tree *value)
25184 {
25185   cp_token *tok;
25186   enum cpp_ttype ret;
25187
25188   tok = cp_lexer_peek_token (the_parser->lexer);
25189
25190   ret = tok->type;
25191   *value = tok->u.value;
25192
25193   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25194     ret = CPP_EOF;
25195   else if (ret == CPP_STRING)
25196     *value = cp_parser_string_literal (the_parser, false, false);
25197   else
25198     {
25199       cp_lexer_consume_token (the_parser->lexer);
25200       if (ret == CPP_KEYWORD)
25201         ret = CPP_NAME;
25202     }
25203
25204   return ret;
25205 }
25206
25207 \f
25208 /* External interface.  */
25209
25210 /* Parse one entire translation unit.  */
25211
25212 void
25213 c_parse_file (void)
25214 {
25215   static bool already_called = false;
25216
25217   if (already_called)
25218     {
25219       sorry ("inter-module optimizations not implemented for C++");
25220       return;
25221     }
25222   already_called = true;
25223
25224   the_parser = cp_parser_new ();
25225   push_deferring_access_checks (flag_access_control
25226                                 ? dk_no_deferred : dk_no_check);
25227   cp_parser_translation_unit (the_parser);
25228   the_parser = NULL;
25229 }
25230
25231 #include "gt-cp-parser.h"