OSDN Git Service

* cp-tree.h (struct tinst_level): Add chain_next GTY
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215   (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217   (cp_lexer *);
218 static void cp_lexer_start_debugging
219   (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224    about passing NULL to functions that require non-NULL arguments
225    (fputs, fprintf).  It will never be used, so all we need is a value
226    of the right type that's guaranteed not to be NULL.  */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233   (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236   (cp_token *);
237
238 /* Manifest constants.  */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables.  */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written.  */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250    sizeof, typeof, or alignof.  */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE.  If NUM is 0, dump all the
255    tokens.  */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260   unsigned i;
261   cp_token *token;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (num == 0)
266     num = VEC_length (cp_token, buffer);
267
268   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269     {
270       cp_lexer_print_token (file, token);
271       switch (token->type)
272         {
273           case CPP_SEMICOLON:
274           case CPP_OPEN_BRACE:
275           case CPP_CLOSE_BRACE:
276           case CPP_EOF:
277             fputc ('\n', file);
278             break;
279
280           default:
281             fputc (' ', file);
282         }
283     }
284
285   if (i == num && i < VEC_length (cp_token, buffer))
286     {
287       fprintf (file, " ... ");
288       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289                             VEC_length (cp_token, buffer) - 1));
290     }
291
292   fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr.  */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301   cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it.  */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311   cp_lexer *lexer;
312
313   c_common_no_more_pch ();
314
315   /* Allocate the memory.  */
316   lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319   /* Initially we are not debugging.  */
320   lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323                                    CP_SAVED_TOKEN_STACK);
324
325   /* Create the buffer.  */
326   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328   return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333    preprocessor.  */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338   cp_lexer *lexer;
339   cp_token token;
340
341   /* It's possible that parsing the first pragma will load a PCH file,
342      which is a GC collection point.  So we have to do that before
343      allocating any memory.  */
344   cp_parser_initial_pragma (&token);
345
346   lexer = cp_lexer_alloc ();
347
348   /* Put the first token in the buffer.  */
349   VEC_quick_push (cp_token, lexer->buffer, &token);
350
351   /* Get the remaining tokens from the preprocessor.  */
352   while (token.type != CPP_EOF)
353     {
354       cp_lexer_get_preprocessor_token (lexer, &token);
355       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356     }
357
358   lexer->last_token = VEC_address (cp_token, lexer->buffer)
359                       + VEC_length (cp_token, lexer->buffer)
360                       - 1;
361   lexer->next_token = VEC_length (cp_token, lexer->buffer)
362                       ? VEC_address (cp_token, lexer->buffer)
363                       : &eof_token;
364
365   /* Subsequent preprocessor diagnostics should use compiler
366      diagnostic functions to get the compiler source location.  */
367   done_lexing = true;
368
369   gcc_assert (!lexer->next_token->purged_p);
370   return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379   cp_token *first = cache->first;
380   cp_token *last = cache->last;
381   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383   /* We do not own the buffer.  */
384   lexer->buffer = NULL;
385   lexer->next_token = first == last ? &eof_token : first;
386   lexer->last_token = last;
387
388   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389                                    CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392   /* Initially we are not debugging.  */
393   lexer->debugging_p = false;
394 #endif
395
396   gcc_assert (!lexer->next_token->purged_p);
397   return lexer;
398 }
399
400 /* Frees all resources associated with LEXER.  */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405   VEC_free (cp_token, gc, lexer->buffer);
406   VEC_free (cp_token_position, heap, lexer->saved_tokens);
407   ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output.  */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417   return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425   gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427   return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433   return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439   lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445   if (lexer->next_token == &eof_token)
446     return lexer->last_token - 1;
447   else
448     return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454   cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456   return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens.  */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN.  Return true
468    if we reach EOF.  If LEXER is NULL, assume we are handling an
469    initial #pragma pch_preprocess, and thus want the lexer to return
470    processed strings.  */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475   static int is_extern_c = 0;
476
477    /* Get a new token from the preprocessor.  */
478   token->type
479     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481   token->keyword = RID_MAX;
482   token->pragma_kind = PRAGMA_NONE;
483   token->purged_p = false;
484
485   /* On some systems, some header files are surrounded by an
486      implicit extern "C" block.  Set a flag in the token if it
487      comes from such a header.  */
488   is_extern_c += pending_lang_change;
489   pending_lang_change = 0;
490   token->implicit_extern_c = is_extern_c > 0;
491
492   /* Check to see if this token is a keyword.  */
493   if (token->type == CPP_NAME)
494     {
495       if (C_IS_RESERVED_WORD (token->u.value))
496         {
497           /* Mark this token as a keyword.  */
498           token->type = CPP_KEYWORD;
499           /* Record which keyword.  */
500           token->keyword = C_RID_CODE (token->u.value);
501         }
502       else
503         {
504           if (warn_cxx0x_compat
505               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507             {
508               /* Warn about the C++0x keyword (but still treat it as
509                  an identifier).  */
510               warning (OPT_Wc__0x_compat, 
511                        "identifier %qE will become a keyword in C++0x",
512                        token->u.value);
513
514               /* Clear out the C_RID_CODE so we don't warn about this
515                  particular identifier-turned-keyword again.  */
516               C_SET_RID_CODE (token->u.value, RID_MAX);
517             }
518
519           token->ambiguous_p = false;
520           token->keyword = RID_MAX;
521         }
522     }
523   else if (token->type == CPP_AT_NAME)
524     {
525       /* This only happens in Objective-C++; it must be a keyword.  */
526       token->type = CPP_KEYWORD;
527       switch (C_RID_CODE (token->u.value))
528         {
529           /* Replace 'class' with '@class', 'private' with '@private',
530              etc.  This prevents confusion with the C++ keyword
531              'class', and makes the tokens consistent with other
532              Objective-C 'AT' keywords.  For example '@class' is
533              reported as RID_AT_CLASS which is consistent with
534              '@synchronized', which is reported as
535              RID_AT_SYNCHRONIZED.
536           */
537         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
538         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
539         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
541         case RID_THROW:     token->keyword = RID_AT_THROW; break;
542         case RID_TRY:       token->keyword = RID_AT_TRY; break;
543         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
544         default:            token->keyword = C_RID_CODE (token->u.value);
545         }
546     }
547   else if (token->type == CPP_PRAGMA)
548     {
549       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
550       token->pragma_kind = ((enum pragma_kind)
551                             TREE_INT_CST_LOW (token->u.value));
552       token->u.value = NULL_TREE;
553     }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN.  */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560   if (token->type != CPP_EOF)
561     {
562       input_location = token->location;
563     }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567    consume it.  */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578   return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE.  */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586   return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE.  */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594   return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD.  */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602   return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD.  */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610   return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier.  */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618   cp_token *token;
619
620   token = cp_lexer_peek_token (lexer);
621   switch (token->keyword) 
622     {
623       /* auto specifier: storage-class-specifier in C++,
624          simple-type-specifier in C++0x.  */
625     case RID_AUTO:
626       /* Storage classes.  */
627     case RID_REGISTER:
628     case RID_STATIC:
629     case RID_EXTERN:
630     case RID_MUTABLE:
631     case RID_THREAD:
632       /* Elaborated type specifiers.  */
633     case RID_ENUM:
634     case RID_CLASS:
635     case RID_STRUCT:
636     case RID_UNION:
637     case RID_TYPENAME:
638       /* Simple type specifiers.  */
639     case RID_CHAR:
640     case RID_CHAR16:
641     case RID_CHAR32:
642     case RID_WCHAR:
643     case RID_BOOL:
644     case RID_SHORT:
645     case RID_INT:
646     case RID_LONG:
647     case RID_INT128:
648     case RID_SIGNED:
649     case RID_UNSIGNED:
650     case RID_FLOAT:
651     case RID_DOUBLE:
652     case RID_VOID:
653       /* GNU extensions.  */ 
654     case RID_ATTRIBUTE:
655     case RID_TYPEOF:
656       /* C++0x extensions.  */
657     case RID_DECLTYPE:
658     case RID_UNDERLYING_TYPE:
659       return true;
660
661     default:
662       return false;
663     }
664 }
665
666 /* Return a pointer to the Nth token in the token stream.  If N is 1,
667    then this is precisely equivalent to cp_lexer_peek_token (except
668    that it is not inline).  One would like to disallow that case, but
669    there is one case (cp_parser_nth_token_starts_template_id) where
670    the caller passes a variable for N and it might be 1.  */
671
672 static cp_token *
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
674 {
675   cp_token *token;
676
677   /* N is 1-based, not zero-based.  */
678   gcc_assert (n > 0);
679
680   if (cp_lexer_debugging_p (lexer))
681     fprintf (cp_lexer_debug_stream,
682              "cp_lexer: peeking ahead %ld at token: ", (long)n);
683
684   --n;
685   token = lexer->next_token;
686   gcc_assert (!n || token != &eof_token);
687   while (n != 0)
688     {
689       ++token;
690       if (token == lexer->last_token)
691         {
692           token = &eof_token;
693           break;
694         }
695
696       if (!token->purged_p)
697         --n;
698     }
699
700   if (cp_lexer_debugging_p (lexer))
701     {
702       cp_lexer_print_token (cp_lexer_debug_stream, token);
703       putc ('\n', cp_lexer_debug_stream);
704     }
705
706   return token;
707 }
708
709 /* Return the next token, and advance the lexer's next_token pointer
710    to point to the next non-purged token.  */
711
712 static cp_token *
713 cp_lexer_consume_token (cp_lexer* lexer)
714 {
715   cp_token *token = lexer->next_token;
716
717   gcc_assert (token != &eof_token);
718   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
719
720   do
721     {
722       lexer->next_token++;
723       if (lexer->next_token == lexer->last_token)
724         {
725           lexer->next_token = &eof_token;
726           break;
727         }
728
729     }
730   while (lexer->next_token->purged_p);
731
732   cp_lexer_set_source_position_from_token (token);
733
734   /* Provide debugging output.  */
735   if (cp_lexer_debugging_p (lexer))
736     {
737       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738       cp_lexer_print_token (cp_lexer_debug_stream, token);
739       putc ('\n', cp_lexer_debug_stream);
740     }
741
742   return token;
743 }
744
745 /* Permanently remove the next token from the token stream, and
746    advance the next_token pointer to refer to the next non-purged
747    token.  */
748
749 static void
750 cp_lexer_purge_token (cp_lexer *lexer)
751 {
752   cp_token *tok = lexer->next_token;
753
754   gcc_assert (tok != &eof_token);
755   tok->purged_p = true;
756   tok->location = UNKNOWN_LOCATION;
757   tok->u.value = NULL_TREE;
758   tok->keyword = RID_MAX;
759
760   do
761     {
762       tok++;
763       if (tok == lexer->last_token)
764         {
765           tok = &eof_token;
766           break;
767         }
768     }
769   while (tok->purged_p);
770   lexer->next_token = tok;
771 }
772
773 /* Permanently remove all tokens after TOK, up to, but not
774    including, the token that will be returned next by
775    cp_lexer_peek_token.  */
776
777 static void
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
779 {
780   cp_token *peek = lexer->next_token;
781
782   if (peek == &eof_token)
783     peek = lexer->last_token;
784
785   gcc_assert (tok < peek);
786
787   for ( tok += 1; tok != peek; tok += 1)
788     {
789       tok->purged_p = true;
790       tok->location = UNKNOWN_LOCATION;
791       tok->u.value = NULL_TREE;
792       tok->keyword = RID_MAX;
793     }
794 }
795
796 /* Begin saving tokens.  All tokens consumed after this point will be
797    preserved.  */
798
799 static void
800 cp_lexer_save_tokens (cp_lexer* lexer)
801 {
802   /* Provide debugging output.  */
803   if (cp_lexer_debugging_p (lexer))
804     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
805
806   VEC_safe_push (cp_token_position, heap,
807                  lexer->saved_tokens, lexer->next_token);
808 }
809
810 /* Commit to the portion of the token stream most recently saved.  */
811
812 static void
813 cp_lexer_commit_tokens (cp_lexer* lexer)
814 {
815   /* Provide debugging output.  */
816   if (cp_lexer_debugging_p (lexer))
817     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
818
819   VEC_pop (cp_token_position, lexer->saved_tokens);
820 }
821
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823    to the token stream.  Stop saving tokens.  */
824
825 static void
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
827 {
828   /* Provide debugging output.  */
829   if (cp_lexer_debugging_p (lexer))
830     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
831
832   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
833 }
834
835 /* Print a representation of the TOKEN on the STREAM.  */
836
837 #ifdef ENABLE_CHECKING
838
839 static void
840 cp_lexer_print_token (FILE * stream, cp_token *token)
841 {
842   /* We don't use cpp_type2name here because the parser defines
843      a few tokens of its own.  */
844   static const char *const token_names[] = {
845     /* cpplib-defined token types */
846 #define OP(e, s) #e,
847 #define TK(e, s) #e,
848     TTYPE_TABLE
849 #undef OP
850 #undef TK
851     /* C++ parser token types - see "Manifest constants", above.  */
852     "KEYWORD",
853     "TEMPLATE_ID",
854     "NESTED_NAME_SPECIFIER",
855   };
856
857   /* For some tokens, print the associated data.  */
858   switch (token->type)
859     {
860     case CPP_KEYWORD:
861       /* Some keywords have a value that is not an IDENTIFIER_NODE.
862          For example, `struct' is mapped to an INTEGER_CST.  */
863       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
864         break;
865       /* else fall through */
866     case CPP_NAME:
867       fputs (IDENTIFIER_POINTER (token->u.value), stream);
868       break;
869
870     case CPP_STRING:
871     case CPP_STRING16:
872     case CPP_STRING32:
873     case CPP_WSTRING:
874     case CPP_UTF8STRING:
875       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
876       break;
877
878     case CPP_NUMBER:
879       print_generic_expr (stream, token->u.value, 0);
880       break;
881
882     default:
883       /* If we have a name for the token, print it out.  Otherwise, we
884          simply give the numeric code.  */
885       if (token->type < ARRAY_SIZE(token_names))
886         fputs (token_names[token->type], stream);
887       else
888         fprintf (stream, "[%d]", token->type);
889       break;
890     }
891 }
892
893 /* Start emitting debugging information.  */
894
895 static void
896 cp_lexer_start_debugging (cp_lexer* lexer)
897 {
898   lexer->debugging_p = true;
899 }
900
901 /* Stop emitting debugging information.  */
902
903 static void
904 cp_lexer_stop_debugging (cp_lexer* lexer)
905 {
906   lexer->debugging_p = false;
907 }
908
909 #endif /* ENABLE_CHECKING */
910
911 /* Create a new cp_token_cache, representing a range of tokens.  */
912
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
915 {
916   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917   cache->first = first;
918   cache->last = last;
919   return cache;
920 }
921
922 \f
923 /* Decl-specifiers.  */
924
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
926
927 static void
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
929 {
930   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
931 }
932
933 /* Declarators.  */
934
935 /* Nothing other than the parser should be creating declarators;
936    declarators are a semi-syntactic representation of C++ entities.
937    Other parts of the front end that need to create entities (like
938    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
939
940 static cp_declarator *make_call_declarator
941   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
942 static cp_declarator *make_array_declarator
943   (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945   (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947   (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949   (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951   (cp_cv_quals, tree, cp_declarator *);
952
953 /* An erroneous declarator.  */
954 static cp_declarator *cp_error_declarator;
955
956 /* The obstack on which declarators and related data structures are
957    allocated.  */
958 static struct obstack declarator_obstack;
959
960 /* Alloc BYTES from the declarator memory pool.  */
961
962 static inline void *
963 alloc_declarator (size_t bytes)
964 {
965   return obstack_alloc (&declarator_obstack, bytes);
966 }
967
968 /* Allocate a declarator of the indicated KIND.  Clear fields that are
969    common to all declarators.  */
970
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
973 {
974   cp_declarator *declarator;
975
976   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977   declarator->kind = kind;
978   declarator->attributes = NULL_TREE;
979   declarator->declarator = NULL;
980   declarator->parameter_pack_p = false;
981   declarator->id_loc = UNKNOWN_LOCATION;
982
983   return declarator;
984 }
985
986 /* Make a declarator for a generalized identifier.  If
987    QUALIFYING_SCOPE is non-NULL, the identifier is
988    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
990    is, if any.   */
991
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994                     special_function_kind sfk)
995 {
996   cp_declarator *declarator;
997
998   /* It is valid to write:
999
1000        class C { void f(); };
1001        typedef C D;
1002        void D::f();
1003
1004      The standard is not clear about whether `typedef const C D' is
1005      legal; as of 2002-09-15 the committee is considering that
1006      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1007      well.  */
1008   if (qualifying_scope && TYPE_P (qualifying_scope))
1009     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1010
1011   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1014
1015   declarator = make_declarator (cdk_id);
1016   declarator->u.id.qualifying_scope = qualifying_scope;
1017   declarator->u.id.unqualified_name = unqualified_name;
1018   declarator->u.id.sfk = sfk;
1019   
1020   return declarator;
1021 }
1022
1023 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1024    of modifiers such as const or volatile to apply to the pointer
1025    type, represented as identifiers.  */
1026
1027 cp_declarator *
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1029 {
1030   cp_declarator *declarator;
1031
1032   declarator = make_declarator (cdk_pointer);
1033   declarator->declarator = target;
1034   declarator->u.pointer.qualifiers = cv_qualifiers;
1035   declarator->u.pointer.class_type = NULL_TREE;
1036   if (target)
1037     {
1038       declarator->id_loc = target->id_loc;
1039       declarator->parameter_pack_p = target->parameter_pack_p;
1040       target->parameter_pack_p = false;
1041     }
1042   else
1043     declarator->parameter_pack_p = false;
1044
1045   return declarator;
1046 }
1047
1048 /* Like make_pointer_declarator -- but for references.  */
1049
1050 cp_declarator *
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1052                            bool rvalue_ref)
1053 {
1054   cp_declarator *declarator;
1055
1056   declarator = make_declarator (cdk_reference);
1057   declarator->declarator = target;
1058   declarator->u.reference.qualifiers = cv_qualifiers;
1059   declarator->u.reference.rvalue_ref = rvalue_ref;
1060   if (target)
1061     {
1062       declarator->id_loc = target->id_loc;
1063       declarator->parameter_pack_p = target->parameter_pack_p;
1064       target->parameter_pack_p = false;
1065     }
1066   else
1067     declarator->parameter_pack_p = false;
1068
1069   return declarator;
1070 }
1071
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073    member of CLASS_TYPE.  */
1074
1075 cp_declarator *
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077                         cp_declarator *pointee)
1078 {
1079   cp_declarator *declarator;
1080
1081   declarator = make_declarator (cdk_ptrmem);
1082   declarator->declarator = pointee;
1083   declarator->u.pointer.qualifiers = cv_qualifiers;
1084   declarator->u.pointer.class_type = class_type;
1085
1086   if (pointee)
1087     {
1088       declarator->parameter_pack_p = pointee->parameter_pack_p;
1089       pointee->parameter_pack_p = false;
1090     }
1091   else
1092     declarator->parameter_pack_p = false;
1093
1094   return declarator;
1095 }
1096
1097 /* Make a declarator for the function given by TARGET, with the
1098    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1099    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1100    indicates what exceptions can be thrown.  */
1101
1102 cp_declarator *
1103 make_call_declarator (cp_declarator *target,
1104                       tree parms,
1105                       cp_cv_quals cv_qualifiers,
1106                       cp_virt_specifiers virt_specifiers,
1107                       tree exception_specification,
1108                       tree late_return_type)
1109 {
1110   cp_declarator *declarator;
1111
1112   declarator = make_declarator (cdk_function);
1113   declarator->declarator = target;
1114   declarator->u.function.parameters = parms;
1115   declarator->u.function.qualifiers = cv_qualifiers;
1116   declarator->u.function.virt_specifiers = virt_specifiers;
1117   declarator->u.function.exception_specification = exception_specification;
1118   declarator->u.function.late_return_type = late_return_type;
1119   if (target)
1120     {
1121       declarator->id_loc = target->id_loc;
1122       declarator->parameter_pack_p = target->parameter_pack_p;
1123       target->parameter_pack_p = false;
1124     }
1125   else
1126     declarator->parameter_pack_p = false;
1127
1128   return declarator;
1129 }
1130
1131 /* Make a declarator for an array of BOUNDS elements, each of which is
1132    defined by ELEMENT.  */
1133
1134 cp_declarator *
1135 make_array_declarator (cp_declarator *element, tree bounds)
1136 {
1137   cp_declarator *declarator;
1138
1139   declarator = make_declarator (cdk_array);
1140   declarator->declarator = element;
1141   declarator->u.array.bounds = bounds;
1142   if (element)
1143     {
1144       declarator->id_loc = element->id_loc;
1145       declarator->parameter_pack_p = element->parameter_pack_p;
1146       element->parameter_pack_p = false;
1147     }
1148   else
1149     declarator->parameter_pack_p = false;
1150
1151   return declarator;
1152 }
1153
1154 /* Determine whether the declarator we've seen so far can be a
1155    parameter pack, when followed by an ellipsis.  */
1156 static bool 
1157 declarator_can_be_parameter_pack (cp_declarator *declarator)
1158 {
1159   /* Search for a declarator name, or any other declarator that goes
1160      after the point where the ellipsis could appear in a parameter
1161      pack. If we find any of these, then this declarator can not be
1162      made into a parameter pack.  */
1163   bool found = false;
1164   while (declarator && !found)
1165     {
1166       switch ((int)declarator->kind)
1167         {
1168         case cdk_id:
1169         case cdk_array:
1170           found = true;
1171           break;
1172
1173         case cdk_error:
1174           return true;
1175
1176         default:
1177           declarator = declarator->declarator;
1178           break;
1179         }
1180     }
1181
1182   return !found;
1183 }
1184
1185 cp_parameter_declarator *no_parameters;
1186
1187 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1188    DECLARATOR and DEFAULT_ARGUMENT.  */
1189
1190 cp_parameter_declarator *
1191 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1192                            cp_declarator *declarator,
1193                            tree default_argument)
1194 {
1195   cp_parameter_declarator *parameter;
1196
1197   parameter = ((cp_parameter_declarator *)
1198                alloc_declarator (sizeof (cp_parameter_declarator)));
1199   parameter->next = NULL;
1200   if (decl_specifiers)
1201     parameter->decl_specifiers = *decl_specifiers;
1202   else
1203     clear_decl_specs (&parameter->decl_specifiers);
1204   parameter->declarator = declarator;
1205   parameter->default_argument = default_argument;
1206   parameter->ellipsis_p = false;
1207
1208   return parameter;
1209 }
1210
1211 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1212
1213 static bool
1214 function_declarator_p (const cp_declarator *declarator)
1215 {
1216   while (declarator)
1217     {
1218       if (declarator->kind == cdk_function
1219           && declarator->declarator->kind == cdk_id)
1220         return true;
1221       if (declarator->kind == cdk_id
1222           || declarator->kind == cdk_error)
1223         return false;
1224       declarator = declarator->declarator;
1225     }
1226   return false;
1227 }
1228  
1229 /* The parser.  */
1230
1231 /* Overview
1232    --------
1233
1234    A cp_parser parses the token stream as specified by the C++
1235    grammar.  Its job is purely parsing, not semantic analysis.  For
1236    example, the parser breaks the token stream into declarators,
1237    expressions, statements, and other similar syntactic constructs.
1238    It does not check that the types of the expressions on either side
1239    of an assignment-statement are compatible, or that a function is
1240    not declared with a parameter of type `void'.
1241
1242    The parser invokes routines elsewhere in the compiler to perform
1243    semantic analysis and to build up the abstract syntax tree for the
1244    code processed.
1245
1246    The parser (and the template instantiation code, which is, in a
1247    way, a close relative of parsing) are the only parts of the
1248    compiler that should be calling push_scope and pop_scope, or
1249    related functions.  The parser (and template instantiation code)
1250    keeps track of what scope is presently active; everything else
1251    should simply honor that.  (The code that generates static
1252    initializers may also need to set the scope, in order to check
1253    access control correctly when emitting the initializers.)
1254
1255    Methodology
1256    -----------
1257
1258    The parser is of the standard recursive-descent variety.  Upcoming
1259    tokens in the token stream are examined in order to determine which
1260    production to use when parsing a non-terminal.  Some C++ constructs
1261    require arbitrary look ahead to disambiguate.  For example, it is
1262    impossible, in the general case, to tell whether a statement is an
1263    expression or declaration without scanning the entire statement.
1264    Therefore, the parser is capable of "parsing tentatively."  When the
1265    parser is not sure what construct comes next, it enters this mode.
1266    Then, while we attempt to parse the construct, the parser queues up
1267    error messages, rather than issuing them immediately, and saves the
1268    tokens it consumes.  If the construct is parsed successfully, the
1269    parser "commits", i.e., it issues any queued error messages and
1270    the tokens that were being preserved are permanently discarded.
1271    If, however, the construct is not parsed successfully, the parser
1272    rolls back its state completely so that it can resume parsing using
1273    a different alternative.
1274
1275    Future Improvements
1276    -------------------
1277
1278    The performance of the parser could probably be improved substantially.
1279    We could often eliminate the need to parse tentatively by looking ahead
1280    a little bit.  In some places, this approach might not entirely eliminate
1281    the need to parse tentatively, but it might still speed up the average
1282    case.  */
1283
1284 /* Flags that are passed to some parsing functions.  These values can
1285    be bitwise-ored together.  */
1286
1287 enum
1288 {
1289   /* No flags.  */
1290   CP_PARSER_FLAGS_NONE = 0x0,
1291   /* The construct is optional.  If it is not present, then no error
1292      should be issued.  */
1293   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1294   /* When parsing a type-specifier, treat user-defined type-names
1295      as non-type identifiers.  */
1296   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1297   /* When parsing a type-specifier, do not try to parse a class-specifier
1298      or enum-specifier.  */
1299   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1300   /* When parsing a decl-specifier-seq, only allow type-specifier or
1301      constexpr.  */
1302   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1303 };
1304
1305 /* This type is used for parameters and variables which hold
1306    combinations of the above flags.  */
1307 typedef int cp_parser_flags;
1308
1309 /* The different kinds of declarators we want to parse.  */
1310
1311 typedef enum cp_parser_declarator_kind
1312 {
1313   /* We want an abstract declarator.  */
1314   CP_PARSER_DECLARATOR_ABSTRACT,
1315   /* We want a named declarator.  */
1316   CP_PARSER_DECLARATOR_NAMED,
1317   /* We don't mind, but the name must be an unqualified-id.  */
1318   CP_PARSER_DECLARATOR_EITHER
1319 } cp_parser_declarator_kind;
1320
1321 /* The precedence values used to parse binary expressions.  The minimum value
1322    of PREC must be 1, because zero is reserved to quickly discriminate
1323    binary operators from other tokens.  */
1324
1325 enum cp_parser_prec
1326 {
1327   PREC_NOT_OPERATOR,
1328   PREC_LOGICAL_OR_EXPRESSION,
1329   PREC_LOGICAL_AND_EXPRESSION,
1330   PREC_INCLUSIVE_OR_EXPRESSION,
1331   PREC_EXCLUSIVE_OR_EXPRESSION,
1332   PREC_AND_EXPRESSION,
1333   PREC_EQUALITY_EXPRESSION,
1334   PREC_RELATIONAL_EXPRESSION,
1335   PREC_SHIFT_EXPRESSION,
1336   PREC_ADDITIVE_EXPRESSION,
1337   PREC_MULTIPLICATIVE_EXPRESSION,
1338   PREC_PM_EXPRESSION,
1339   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1340 };
1341
1342 /* A mapping from a token type to a corresponding tree node type, with a
1343    precedence value.  */
1344
1345 typedef struct cp_parser_binary_operations_map_node
1346 {
1347   /* The token type.  */
1348   enum cpp_ttype token_type;
1349   /* The corresponding tree code.  */
1350   enum tree_code tree_type;
1351   /* The precedence of this operator.  */
1352   enum cp_parser_prec prec;
1353 } cp_parser_binary_operations_map_node;
1354
1355 typedef struct cp_parser_expression_stack_entry
1356 {
1357   /* Left hand side of the binary operation we are currently
1358      parsing.  */
1359   tree lhs;
1360   /* Original tree code for left hand side, if it was a binary
1361      expression itself (used for -Wparentheses).  */
1362   enum tree_code lhs_type;
1363   /* Tree code for the binary operation we are parsing.  */
1364   enum tree_code tree_type;
1365   /* Precedence of the binary operation we are parsing.  */
1366   enum cp_parser_prec prec;
1367 } cp_parser_expression_stack_entry;
1368
1369 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1370    entries because precedence levels on the stack are monotonically
1371    increasing.  */
1372 typedef struct cp_parser_expression_stack_entry
1373   cp_parser_expression_stack[NUM_PREC_VALUES];
1374
1375 /* Prototypes.  */
1376
1377 /* Constructors and destructors.  */
1378
1379 static cp_parser_context *cp_parser_context_new
1380   (cp_parser_context *);
1381
1382 /* Class variables.  */
1383
1384 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1385
1386 /* The operator-precedence table used by cp_parser_binary_expression.
1387    Transformed into an associative array (binops_by_token) by
1388    cp_parser_new.  */
1389
1390 static const cp_parser_binary_operations_map_node binops[] = {
1391   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1392   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1393
1394   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1397
1398   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1400
1401   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1403
1404   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1405   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1406   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1408
1409   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1410   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1411
1412   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1413
1414   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1415
1416   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1417
1418   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1419
1420   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1421 };
1422
1423 /* The same as binops, but initialized by cp_parser_new so that
1424    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1425    for speed.  */
1426 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1427
1428 /* Constructors and destructors.  */
1429
1430 /* Construct a new context.  The context below this one on the stack
1431    is given by NEXT.  */
1432
1433 static cp_parser_context *
1434 cp_parser_context_new (cp_parser_context* next)
1435 {
1436   cp_parser_context *context;
1437
1438   /* Allocate the storage.  */
1439   if (cp_parser_context_free_list != NULL)
1440     {
1441       /* Pull the first entry from the free list.  */
1442       context = cp_parser_context_free_list;
1443       cp_parser_context_free_list = context->next;
1444       memset (context, 0, sizeof (*context));
1445     }
1446   else
1447     context = ggc_alloc_cleared_cp_parser_context ();
1448
1449   /* No errors have occurred yet in this context.  */
1450   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1451   /* If this is not the bottommost context, copy information that we
1452      need from the previous context.  */
1453   if (next)
1454     {
1455       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1456          expression, then we are parsing one in this context, too.  */
1457       context->object_type = next->object_type;
1458       /* Thread the stack.  */
1459       context->next = next;
1460     }
1461
1462   return context;
1463 }
1464
1465 /* Managing the unparsed function queues.  */
1466
1467 #define unparsed_funs_with_default_args \
1468   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1469 #define unparsed_funs_with_definitions \
1470   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1471
1472 static void
1473 push_unparsed_function_queues (cp_parser *parser)
1474 {
1475   VEC_safe_push (cp_unparsed_functions_entry, gc,
1476                  parser->unparsed_queues, NULL);
1477   unparsed_funs_with_default_args = NULL;
1478   unparsed_funs_with_definitions = make_tree_vector ();
1479 }
1480
1481 static void
1482 pop_unparsed_function_queues (cp_parser *parser)
1483 {
1484   release_tree_vector (unparsed_funs_with_definitions);
1485   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1486 }
1487
1488 /* Prototypes.  */
1489
1490 /* Constructors and destructors.  */
1491
1492 static cp_parser *cp_parser_new
1493   (void);
1494
1495 /* Routines to parse various constructs.
1496
1497    Those that return `tree' will return the error_mark_node (rather
1498    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1499    Sometimes, they will return an ordinary node if error-recovery was
1500    attempted, even though a parse error occurred.  So, to check
1501    whether or not a parse error occurred, you should always use
1502    cp_parser_error_occurred.  If the construct is optional (indicated
1503    either by an `_opt' in the name of the function that does the
1504    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1505    the construct is not present.  */
1506
1507 /* Lexical conventions [gram.lex]  */
1508
1509 static tree cp_parser_identifier
1510   (cp_parser *);
1511 static tree cp_parser_string_literal
1512   (cp_parser *, bool, bool);
1513
1514 /* Basic concepts [gram.basic]  */
1515
1516 static bool cp_parser_translation_unit
1517   (cp_parser *);
1518
1519 /* Expressions [gram.expr]  */
1520
1521 static tree cp_parser_primary_expression
1522   (cp_parser *, bool, bool, bool, cp_id_kind *);
1523 static tree cp_parser_id_expression
1524   (cp_parser *, bool, bool, bool *, bool, bool);
1525 static tree cp_parser_unqualified_id
1526   (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier_opt
1528   (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_nested_name_specifier
1530   (cp_parser *, bool, bool, bool, bool);
1531 static tree cp_parser_qualifying_entity
1532   (cp_parser *, bool, bool, bool, bool, bool);
1533 static tree cp_parser_postfix_expression
1534   (cp_parser *, bool, bool, bool, cp_id_kind *);
1535 static tree cp_parser_postfix_open_square_expression
1536   (cp_parser *, tree, bool);
1537 static tree cp_parser_postfix_dot_deref_expression
1538   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1539 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1540   (cp_parser *, int, bool, bool, bool *);
1541 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1542 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1543 static void cp_parser_pseudo_destructor_name
1544   (cp_parser *, tree *, tree *);
1545 static tree cp_parser_unary_expression
1546   (cp_parser *, bool, bool, cp_id_kind *);
1547 static enum tree_code cp_parser_unary_operator
1548   (cp_token *);
1549 static tree cp_parser_new_expression
1550   (cp_parser *);
1551 static VEC(tree,gc) *cp_parser_new_placement
1552   (cp_parser *);
1553 static tree cp_parser_new_type_id
1554   (cp_parser *, tree *);
1555 static cp_declarator *cp_parser_new_declarator_opt
1556   (cp_parser *);
1557 static cp_declarator *cp_parser_direct_new_declarator
1558   (cp_parser *);
1559 static VEC(tree,gc) *cp_parser_new_initializer
1560   (cp_parser *);
1561 static tree cp_parser_delete_expression
1562   (cp_parser *);
1563 static tree cp_parser_cast_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static tree cp_parser_binary_expression
1566   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1567 static tree cp_parser_question_colon_clause
1568   (cp_parser *, tree);
1569 static tree cp_parser_assignment_expression
1570   (cp_parser *, bool, cp_id_kind *);
1571 static enum tree_code cp_parser_assignment_operator_opt
1572   (cp_parser *);
1573 static tree cp_parser_expression
1574   (cp_parser *, bool, cp_id_kind *);
1575 static tree cp_parser_constant_expression
1576   (cp_parser *, bool, bool *);
1577 static tree cp_parser_builtin_offsetof
1578   (cp_parser *);
1579 static tree cp_parser_lambda_expression
1580   (cp_parser *);
1581 static void cp_parser_lambda_introducer
1582   (cp_parser *, tree);
1583 static bool cp_parser_lambda_declarator_opt
1584   (cp_parser *, tree);
1585 static void cp_parser_lambda_body
1586   (cp_parser *, tree);
1587
1588 /* Statements [gram.stmt.stmt]  */
1589
1590 static void cp_parser_statement
1591   (cp_parser *, tree, bool, bool *);
1592 static void cp_parser_label_for_labeled_statement
1593   (cp_parser *);
1594 static tree cp_parser_expression_statement
1595   (cp_parser *, tree);
1596 static tree cp_parser_compound_statement
1597   (cp_parser *, tree, bool, bool);
1598 static void cp_parser_statement_seq_opt
1599   (cp_parser *, tree);
1600 static tree cp_parser_selection_statement
1601   (cp_parser *, bool *);
1602 static tree cp_parser_condition
1603   (cp_parser *);
1604 static tree cp_parser_iteration_statement
1605   (cp_parser *);
1606 static bool cp_parser_for_init_statement
1607   (cp_parser *, tree *decl);
1608 static tree cp_parser_for
1609   (cp_parser *);
1610 static tree cp_parser_c_for
1611   (cp_parser *, tree, tree);
1612 static tree cp_parser_range_for
1613   (cp_parser *, tree, tree, tree);
1614 static tree cp_parser_perform_range_for_lookup
1615   (tree, tree *, tree *);
1616 static tree cp_parser_range_for_member_function
1617   (tree, tree);
1618 static tree cp_parser_jump_statement
1619   (cp_parser *);
1620 static void cp_parser_declaration_statement
1621   (cp_parser *);
1622
1623 static tree cp_parser_implicitly_scoped_statement
1624   (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1626   (cp_parser *);
1627
1628 /* Declarations [gram.dcl.dcl] */
1629
1630 static void cp_parser_declaration_seq_opt
1631   (cp_parser *);
1632 static void cp_parser_declaration
1633   (cp_parser *);
1634 static void cp_parser_block_declaration
1635   (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637   (cp_parser *, bool, tree *);
1638 static void cp_parser_decl_specifier_seq
1639   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1641   (cp_parser *);
1642 static tree cp_parser_function_specifier_opt
1643   (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1646    int *, bool *);
1647 static tree cp_parser_simple_type_specifier
1648   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1650   (cp_parser *);
1651 static tree cp_parser_nonclass_name 
1652   (cp_parser* parser);
1653 static tree cp_parser_elaborated_type_specifier
1654   (cp_parser *, bool, bool);
1655 static tree cp_parser_enum_specifier
1656   (cp_parser *);
1657 static void cp_parser_enumerator_list
1658   (cp_parser *, tree);
1659 static void cp_parser_enumerator_definition
1660   (cp_parser *, tree);
1661 static tree cp_parser_namespace_name
1662   (cp_parser *);
1663 static void cp_parser_namespace_definition
1664   (cp_parser *);
1665 static void cp_parser_namespace_body
1666   (cp_parser *);
1667 static tree cp_parser_qualified_namespace_specifier
1668   (cp_parser *);
1669 static void cp_parser_namespace_alias_definition
1670   (cp_parser *);
1671 static bool cp_parser_using_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_using_directive
1674   (cp_parser *);
1675 static void cp_parser_asm_definition
1676   (cp_parser *);
1677 static void cp_parser_linkage_specification
1678   (cp_parser *);
1679 static void cp_parser_static_assert
1680   (cp_parser *, bool);
1681 static tree cp_parser_decltype
1682   (cp_parser *);
1683
1684 /* Declarators [gram.dcl.decl] */
1685
1686 static tree cp_parser_init_declarator
1687   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1688 static cp_declarator *cp_parser_declarator
1689   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1690 static cp_declarator *cp_parser_direct_declarator
1691   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1692 static enum tree_code cp_parser_ptr_operator
1693   (cp_parser *, tree *, cp_cv_quals *);
1694 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1695   (cp_parser *);
1696 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1697   (cp_parser *);
1698 static tree cp_parser_late_return_type_opt
1699   (cp_parser *);
1700 static tree cp_parser_declarator_id
1701   (cp_parser *, bool);
1702 static tree cp_parser_type_id
1703   (cp_parser *);
1704 static tree cp_parser_template_type_arg
1705   (cp_parser *);
1706 static tree cp_parser_trailing_type_id (cp_parser *);
1707 static tree cp_parser_type_id_1
1708   (cp_parser *, bool, bool);
1709 static void cp_parser_type_specifier_seq
1710   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1711 static tree cp_parser_parameter_declaration_clause
1712   (cp_parser *);
1713 static tree cp_parser_parameter_declaration_list
1714   (cp_parser *, bool *);
1715 static cp_parameter_declarator *cp_parser_parameter_declaration
1716   (cp_parser *, bool, bool *);
1717 static tree cp_parser_default_argument 
1718   (cp_parser *, bool);
1719 static void cp_parser_function_body
1720   (cp_parser *);
1721 static tree cp_parser_initializer
1722   (cp_parser *, bool *, bool *);
1723 static tree cp_parser_initializer_clause
1724   (cp_parser *, bool *);
1725 static tree cp_parser_braced_list
1726   (cp_parser*, bool*);
1727 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1728   (cp_parser *, bool *);
1729
1730 static bool cp_parser_ctor_initializer_opt_and_function_body
1731   (cp_parser *);
1732
1733 /* Classes [gram.class] */
1734
1735 static tree cp_parser_class_name
1736   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1737 static tree cp_parser_class_specifier
1738   (cp_parser *);
1739 static tree cp_parser_class_head
1740   (cp_parser *, bool *, tree *, tree *);
1741 static enum tag_types cp_parser_class_key
1742   (cp_parser *);
1743 static void cp_parser_member_specification_opt
1744   (cp_parser *);
1745 static void cp_parser_member_declaration
1746   (cp_parser *);
1747 static tree cp_parser_pure_specifier
1748   (cp_parser *);
1749 static tree cp_parser_constant_initializer
1750   (cp_parser *);
1751
1752 /* Derived classes [gram.class.derived] */
1753
1754 static tree cp_parser_base_clause
1755   (cp_parser *);
1756 static tree cp_parser_base_specifier
1757   (cp_parser *);
1758
1759 /* Special member functions [gram.special] */
1760
1761 static tree cp_parser_conversion_function_id
1762   (cp_parser *);
1763 static tree cp_parser_conversion_type_id
1764   (cp_parser *);
1765 static cp_declarator *cp_parser_conversion_declarator_opt
1766   (cp_parser *);
1767 static bool cp_parser_ctor_initializer_opt
1768   (cp_parser *);
1769 static void cp_parser_mem_initializer_list
1770   (cp_parser *);
1771 static tree cp_parser_mem_initializer
1772   (cp_parser *);
1773 static tree cp_parser_mem_initializer_id
1774   (cp_parser *);
1775
1776 /* Overloading [gram.over] */
1777
1778 static tree cp_parser_operator_function_id
1779   (cp_parser *);
1780 static tree cp_parser_operator
1781   (cp_parser *);
1782
1783 /* Templates [gram.temp] */
1784
1785 static void cp_parser_template_declaration
1786   (cp_parser *, bool);
1787 static tree cp_parser_template_parameter_list
1788   (cp_parser *);
1789 static tree cp_parser_template_parameter
1790   (cp_parser *, bool *, bool *);
1791 static tree cp_parser_type_parameter
1792   (cp_parser *, bool *);
1793 static tree cp_parser_template_id
1794   (cp_parser *, bool, bool, bool);
1795 static tree cp_parser_template_name
1796   (cp_parser *, bool, bool, bool, bool *);
1797 static tree cp_parser_template_argument_list
1798   (cp_parser *);
1799 static tree cp_parser_template_argument
1800   (cp_parser *);
1801 static void cp_parser_explicit_instantiation
1802   (cp_parser *);
1803 static void cp_parser_explicit_specialization
1804   (cp_parser *);
1805
1806 /* Exception handling [gram.exception] */
1807
1808 static tree cp_parser_try_block
1809   (cp_parser *);
1810 static bool cp_parser_function_try_block
1811   (cp_parser *);
1812 static void cp_parser_handler_seq
1813   (cp_parser *);
1814 static void cp_parser_handler
1815   (cp_parser *);
1816 static tree cp_parser_exception_declaration
1817   (cp_parser *);
1818 static tree cp_parser_throw_expression
1819   (cp_parser *);
1820 static tree cp_parser_exception_specification_opt
1821   (cp_parser *);
1822 static tree cp_parser_type_id_list
1823   (cp_parser *);
1824
1825 /* GNU Extensions */
1826
1827 static tree cp_parser_asm_specification_opt
1828   (cp_parser *);
1829 static tree cp_parser_asm_operand_list
1830   (cp_parser *);
1831 static tree cp_parser_asm_clobber_list
1832   (cp_parser *);
1833 static tree cp_parser_asm_label_list
1834   (cp_parser *);
1835 static tree cp_parser_attributes_opt
1836   (cp_parser *);
1837 static tree cp_parser_attribute_list
1838   (cp_parser *);
1839 static bool cp_parser_extension_opt
1840   (cp_parser *, int *);
1841 static void cp_parser_label_declaration
1842   (cp_parser *);
1843
1844 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1845 static bool cp_parser_pragma
1846   (cp_parser *, enum pragma_context);
1847
1848 /* Objective-C++ Productions */
1849
1850 static tree cp_parser_objc_message_receiver
1851   (cp_parser *);
1852 static tree cp_parser_objc_message_args
1853   (cp_parser *);
1854 static tree cp_parser_objc_message_expression
1855   (cp_parser *);
1856 static tree cp_parser_objc_encode_expression
1857   (cp_parser *);
1858 static tree cp_parser_objc_defs_expression
1859   (cp_parser *);
1860 static tree cp_parser_objc_protocol_expression
1861   (cp_parser *);
1862 static tree cp_parser_objc_selector_expression
1863   (cp_parser *);
1864 static tree cp_parser_objc_expression
1865   (cp_parser *);
1866 static bool cp_parser_objc_selector_p
1867   (enum cpp_ttype);
1868 static tree cp_parser_objc_selector
1869   (cp_parser *);
1870 static tree cp_parser_objc_protocol_refs_opt
1871   (cp_parser *);
1872 static void cp_parser_objc_declaration
1873   (cp_parser *, tree);
1874 static tree cp_parser_objc_statement
1875   (cp_parser *);
1876 static bool cp_parser_objc_valid_prefix_attributes
1877   (cp_parser *, tree *);
1878 static void cp_parser_objc_at_property_declaration 
1879   (cp_parser *) ;
1880 static void cp_parser_objc_at_synthesize_declaration 
1881   (cp_parser *) ;
1882 static void cp_parser_objc_at_dynamic_declaration
1883   (cp_parser *) ;
1884 static tree cp_parser_objc_struct_declaration
1885   (cp_parser *) ;
1886
1887 /* Utility Routines */
1888
1889 static tree cp_parser_lookup_name
1890   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1891 static tree cp_parser_lookup_name_simple
1892   (cp_parser *, tree, location_t);
1893 static tree cp_parser_maybe_treat_template_as_class
1894   (tree, bool);
1895 static bool cp_parser_check_declarator_template_parameters
1896   (cp_parser *, cp_declarator *, location_t);
1897 static bool cp_parser_check_template_parameters
1898   (cp_parser *, unsigned, location_t, cp_declarator *);
1899 static tree cp_parser_simple_cast_expression
1900   (cp_parser *);
1901 static tree cp_parser_global_scope_opt
1902   (cp_parser *, bool);
1903 static bool cp_parser_constructor_declarator_p
1904   (cp_parser *, bool);
1905 static tree cp_parser_function_definition_from_specifiers_and_declarator
1906   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1907 static tree cp_parser_function_definition_after_declarator
1908   (cp_parser *, bool);
1909 static void cp_parser_template_declaration_after_export
1910   (cp_parser *, bool);
1911 static void cp_parser_perform_template_parameter_access_checks
1912   (VEC (deferred_access_check,gc)*);
1913 static tree cp_parser_single_declaration
1914   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1915 static tree cp_parser_functional_cast
1916   (cp_parser *, tree);
1917 static tree cp_parser_save_member_function_body
1918   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1919 static tree cp_parser_enclosed_template_argument_list
1920   (cp_parser *);
1921 static void cp_parser_save_default_args
1922   (cp_parser *, tree);
1923 static void cp_parser_late_parsing_for_member
1924   (cp_parser *, tree);
1925 static void cp_parser_late_parsing_default_args
1926   (cp_parser *, tree);
1927 static tree cp_parser_sizeof_operand
1928   (cp_parser *, enum rid);
1929 static tree cp_parser_trait_expr
1930   (cp_parser *, enum rid);
1931 static bool cp_parser_declares_only_class_p
1932   (cp_parser *);
1933 static void cp_parser_set_storage_class
1934   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1935 static void cp_parser_set_decl_spec_type
1936   (cp_decl_specifier_seq *, tree, location_t, bool);
1937 static bool cp_parser_friend_p
1938   (const cp_decl_specifier_seq *);
1939 static void cp_parser_required_error
1940   (cp_parser *, required_token, bool);
1941 static cp_token *cp_parser_require
1942   (cp_parser *, enum cpp_ttype, required_token);
1943 static cp_token *cp_parser_require_keyword
1944   (cp_parser *, enum rid, required_token);
1945 static bool cp_parser_token_starts_function_definition_p
1946   (cp_token *);
1947 static bool cp_parser_next_token_starts_class_definition_p
1948   (cp_parser *);
1949 static bool cp_parser_next_token_ends_template_argument_p
1950   (cp_parser *);
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952   (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1954   (cp_token *);
1955 static void cp_parser_check_class_key
1956   (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958   (tree type, location_t location);
1959 static bool cp_parser_optional_template_keyword
1960   (cp_parser *);
1961 static void cp_parser_pre_parsed_nested_name_specifier
1962   (cp_parser *);
1963 static bool cp_parser_cache_group
1964   (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1966   (cp_parser *);
1967 static void cp_parser_commit_to_tentative_parse
1968   (cp_parser *);
1969 static void cp_parser_abort_tentative_parse
1970   (cp_parser *);
1971 static bool cp_parser_parse_definitely
1972   (cp_parser *);
1973 static inline bool cp_parser_parsing_tentatively
1974   (cp_parser *);
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1976   (cp_parser *);
1977 static void cp_parser_error
1978   (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980   (cp_parser *, tree, tree, name_lookup_error, location_t);
1981 static bool cp_parser_simulate_error
1982   (cp_parser *);
1983 static bool cp_parser_check_type_definition
1984   (cp_parser *);
1985 static void cp_parser_check_for_definition_in_return_type
1986   (cp_declarator *, tree, location_t type_location);
1987 static void cp_parser_check_for_invalid_template_id
1988   (cp_parser *, tree, location_t location);
1989 static bool cp_parser_non_integral_constant_expression
1990   (cp_parser *, non_integral_constant);
1991 static void cp_parser_diagnose_invalid_type_name
1992   (cp_parser *, tree, tree, location_t);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994   (cp_parser *);
1995 static int cp_parser_skip_to_closing_parenthesis
1996   (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1998   (cp_parser *);
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2000   (cp_parser *);
2001 static void cp_parser_skip_to_end_of_block_or_statement
2002   (cp_parser *);
2003 static bool cp_parser_skip_to_closing_brace
2004   (cp_parser *);
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2006   (cp_parser *);
2007 static void cp_parser_skip_to_pragma_eol
2008   (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2010   (cp_parser *);
2011 static bool cp_parser_allow_gnu_extensions_p
2012   (cp_parser *);
2013 static bool cp_parser_is_string_literal
2014   (cp_token *);
2015 static bool cp_parser_is_keyword
2016   (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018   (cp_parser *, tree, tree, location_t location);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021
2022 /* Returns nonzero if we are parsing tentatively.  */
2023
2024 static inline bool
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2026 {
2027   return parser->context->next != NULL;
2028 }
2029
2030 /* Returns nonzero if TOKEN is a string literal.  */
2031
2032 static bool
2033 cp_parser_is_string_literal (cp_token* token)
2034 {
2035   return (token->type == CPP_STRING ||
2036           token->type == CPP_STRING16 ||
2037           token->type == CPP_STRING32 ||
2038           token->type == CPP_WSTRING ||
2039           token->type == CPP_UTF8STRING);
2040 }
2041
2042 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2043
2044 static bool
2045 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2046 {
2047   return token->keyword == keyword;
2048 }
2049
2050 /* If not parsing tentatively, issue a diagnostic of the form
2051       FILE:LINE: MESSAGE before TOKEN
2052    where TOKEN is the next token in the input stream.  MESSAGE
2053    (specified by the caller) is usually of the form "expected
2054    OTHER-TOKEN".  */
2055
2056 static void
2057 cp_parser_error (cp_parser* parser, const char* gmsgid)
2058 {
2059   if (!cp_parser_simulate_error (parser))
2060     {
2061       cp_token *token = cp_lexer_peek_token (parser->lexer);
2062       /* This diagnostic makes more sense if it is tagged to the line
2063          of the token we just peeked at.  */
2064       cp_lexer_set_source_position_from_token (token);
2065
2066       if (token->type == CPP_PRAGMA)
2067         {
2068           error_at (token->location,
2069                     "%<#pragma%> is not allowed here");
2070           cp_parser_skip_to_pragma_eol (parser, token);
2071           return;
2072         }
2073
2074       c_parse_error (gmsgid,
2075                      /* Because c_parser_error does not understand
2076                         CPP_KEYWORD, keywords are treated like
2077                         identifiers.  */
2078                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079                      token->u.value, token->flags);
2080     }
2081 }
2082
2083 /* Issue an error about name-lookup failing.  NAME is the
2084    IDENTIFIER_NODE DECL is the result of
2085    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2086    the thing that we hoped to find.  */
2087
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090                              tree name,
2091                              tree decl,
2092                              name_lookup_error desired,
2093                              location_t location)
2094 {
2095   /* If name lookup completely failed, tell the user that NAME was not
2096      declared.  */
2097   if (decl == error_mark_node)
2098     {
2099       if (parser->scope && parser->scope != global_namespace)
2100         error_at (location, "%<%E::%E%> has not been declared",
2101                   parser->scope, name);
2102       else if (parser->scope == global_namespace)
2103         error_at (location, "%<::%E%> has not been declared", name);
2104       else if (parser->object_scope
2105                && !CLASS_TYPE_P (parser->object_scope))
2106         error_at (location, "request for member %qE in non-class type %qT",
2107                   name, parser->object_scope);
2108       else if (parser->object_scope)
2109         error_at (location, "%<%T::%E%> has not been declared",
2110                   parser->object_scope, name);
2111       else
2112         error_at (location, "%qE has not been declared", name);
2113     }
2114   else if (parser->scope && parser->scope != global_namespace)
2115     {
2116       switch (desired)
2117         {
2118           case NLE_TYPE:
2119             error_at (location, "%<%E::%E%> is not a type",
2120                                 parser->scope, name);
2121             break;
2122           case NLE_CXX98:
2123             error_at (location, "%<%E::%E%> is not a class or namespace",
2124                                 parser->scope, name);
2125             break;
2126           case NLE_NOT_CXX98:
2127             error_at (location,
2128                       "%<%E::%E%> is not a class, namespace, or enumeration",
2129                       parser->scope, name);
2130             break;
2131           default:
2132             gcc_unreachable ();
2133             
2134         }
2135     }
2136   else if (parser->scope == global_namespace)
2137     {
2138       switch (desired)
2139         {
2140           case NLE_TYPE:
2141             error_at (location, "%<::%E%> is not a type", name);
2142             break;
2143           case NLE_CXX98:
2144             error_at (location, "%<::%E%> is not a class or namespace", name);
2145             break;
2146           case NLE_NOT_CXX98:
2147             error_at (location,
2148                       "%<::%E%> is not a class, namespace, or enumeration",
2149                       name);
2150             break;
2151           default:
2152             gcc_unreachable ();
2153         }
2154     }
2155   else
2156     {
2157       switch (desired)
2158         {
2159           case NLE_TYPE:
2160             error_at (location, "%qE is not a type", name);
2161             break;
2162           case NLE_CXX98:
2163             error_at (location, "%qE is not a class or namespace", name);
2164             break;
2165           case NLE_NOT_CXX98:
2166             error_at (location,
2167                       "%qE is not a class, namespace, or enumeration", name);
2168             break;
2169           default:
2170             gcc_unreachable ();
2171         }
2172     }
2173 }
2174
2175 /* If we are parsing tentatively, remember that an error has occurred
2176    during this tentative parse.  Returns true if the error was
2177    simulated; false if a message should be issued by the caller.  */
2178
2179 static bool
2180 cp_parser_simulate_error (cp_parser* parser)
2181 {
2182   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2183     {
2184       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2185       return true;
2186     }
2187   return false;
2188 }
2189
2190 /* Check for repeated decl-specifiers.  */
2191
2192 static void
2193 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2194                            location_t location)
2195 {
2196   int ds;
2197
2198   for (ds = ds_first; ds != ds_last; ++ds)
2199     {
2200       unsigned count = decl_specs->specs[ds];
2201       if (count < 2)
2202         continue;
2203       /* The "long" specifier is a special case because of "long long".  */
2204       if (ds == ds_long)
2205         {
2206           if (count > 2)
2207             error_at (location, "%<long long long%> is too long for GCC");
2208           else 
2209             pedwarn_cxx98 (location, OPT_Wlong_long, 
2210                            "ISO C++ 1998 does not support %<long long%>");
2211         }
2212       else if (count > 1)
2213         {
2214           static const char *const decl_spec_names[] = {
2215             "signed",
2216             "unsigned",
2217             "short",
2218             "long",
2219             "const",
2220             "volatile",
2221             "restrict",
2222             "inline",
2223             "virtual",
2224             "explicit",
2225             "friend",
2226             "typedef",
2227             "constexpr",
2228             "__complex",
2229             "__thread"
2230           };
2231           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2232         }
2233     }
2234 }
2235
2236 /* This function is called when a type is defined.  If type
2237    definitions are forbidden at this point, an error message is
2238    issued.  */
2239
2240 static bool
2241 cp_parser_check_type_definition (cp_parser* parser)
2242 {
2243   /* If types are forbidden here, issue a message.  */
2244   if (parser->type_definition_forbidden_message)
2245     {
2246       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2247          in the message need to be interpreted.  */
2248       error (parser->type_definition_forbidden_message);
2249       return false;
2250     }
2251   return true;
2252 }
2253
2254 /* This function is called when the DECLARATOR is processed.  The TYPE
2255    was a type defined in the decl-specifiers.  If it is invalid to
2256    define a type in the decl-specifiers for DECLARATOR, an error is
2257    issued. TYPE_LOCATION is the location of TYPE and is used
2258    for error reporting.  */
2259
2260 static void
2261 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2262                                                tree type, location_t type_location)
2263 {
2264   /* [dcl.fct] forbids type definitions in return types.
2265      Unfortunately, it's not easy to know whether or not we are
2266      processing a return type until after the fact.  */
2267   while (declarator
2268          && (declarator->kind == cdk_pointer
2269              || declarator->kind == cdk_reference
2270              || declarator->kind == cdk_ptrmem))
2271     declarator = declarator->declarator;
2272   if (declarator
2273       && declarator->kind == cdk_function)
2274     {
2275       error_at (type_location,
2276                 "new types may not be defined in a return type");
2277       inform (type_location, 
2278               "(perhaps a semicolon is missing after the definition of %qT)",
2279               type);
2280     }
2281 }
2282
2283 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2284    "<" in any valid C++ program.  If the next token is indeed "<",
2285    issue a message warning the user about what appears to be an
2286    invalid attempt to form a template-id. LOCATION is the location
2287    of the type-specifier (TYPE) */
2288
2289 static void
2290 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2291                                          tree type, location_t location)
2292 {
2293   cp_token_position start = 0;
2294
2295   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2296     {
2297       if (TYPE_P (type))
2298         error_at (location, "%qT is not a template", type);
2299       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2300         error_at (location, "%qE is not a template", type);
2301       else
2302         error_at (location, "invalid template-id");
2303       /* Remember the location of the invalid "<".  */
2304       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2305         start = cp_lexer_token_position (parser->lexer, true);
2306       /* Consume the "<".  */
2307       cp_lexer_consume_token (parser->lexer);
2308       /* Parse the template arguments.  */
2309       cp_parser_enclosed_template_argument_list (parser);
2310       /* Permanently remove the invalid template arguments so that
2311          this error message is not issued again.  */
2312       if (start)
2313         cp_lexer_purge_tokens_after (parser->lexer, start);
2314     }
2315 }
2316
2317 /* If parsing an integral constant-expression, issue an error message
2318    about the fact that THING appeared and return true.  Otherwise,
2319    return false.  In either case, set
2320    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2321
2322 static bool
2323 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2324                                             non_integral_constant thing)
2325 {
2326   parser->non_integral_constant_expression_p = true;
2327   if (parser->integral_constant_expression_p)
2328     {
2329       if (!parser->allow_non_integral_constant_expression_p)
2330         {
2331           const char *msg = NULL;
2332           switch (thing)
2333             {
2334               case NIC_FLOAT:
2335                 error ("floating-point literal "
2336                        "cannot appear in a constant-expression");
2337                 return true;
2338               case NIC_CAST:
2339                 error ("a cast to a type other than an integral or "
2340                        "enumeration type cannot appear in a "
2341                        "constant-expression");
2342                 return true;
2343               case NIC_TYPEID:
2344                 error ("%<typeid%> operator "
2345                        "cannot appear in a constant-expression");
2346                 return true;
2347               case NIC_NCC:
2348                 error ("non-constant compound literals "
2349                        "cannot appear in a constant-expression");
2350                 return true;
2351               case NIC_FUNC_CALL:
2352                 error ("a function call "
2353                        "cannot appear in a constant-expression");
2354                 return true;
2355               case NIC_INC:
2356                 error ("an increment "
2357                        "cannot appear in a constant-expression");
2358                 return true;
2359               case NIC_DEC:
2360                 error ("an decrement "
2361                        "cannot appear in a constant-expression");
2362                 return true;
2363               case NIC_ARRAY_REF:
2364                 error ("an array reference "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_ADDR_LABEL:
2368                 error ("the address of a label "
2369                        "cannot appear in a constant-expression");
2370                 return true;
2371               case NIC_OVERLOADED:
2372                 error ("calls to overloaded operators "
2373                        "cannot appear in a constant-expression");
2374                 return true;
2375               case NIC_ASSIGNMENT:
2376                 error ("an assignment cannot appear in a constant-expression");
2377                 return true;
2378               case NIC_COMMA:
2379                 error ("a comma operator "
2380                        "cannot appear in a constant-expression");
2381                 return true;
2382               case NIC_CONSTRUCTOR:
2383                 error ("a call to a constructor "
2384                        "cannot appear in a constant-expression");
2385                 return true;
2386               case NIC_THIS:
2387                 msg = "this";
2388                 break;
2389               case NIC_FUNC_NAME:
2390                 msg = "__FUNCTION__";
2391                 break;
2392               case NIC_PRETTY_FUNC:
2393                 msg = "__PRETTY_FUNCTION__";
2394                 break;
2395               case NIC_C99_FUNC:
2396                 msg = "__func__";
2397                 break;
2398               case NIC_VA_ARG:
2399                 msg = "va_arg";
2400                 break;
2401               case NIC_ARROW:
2402                 msg = "->";
2403                 break;
2404               case NIC_POINT:
2405                 msg = ".";
2406                 break;
2407               case NIC_STAR:
2408                 msg = "*";
2409                 break;
2410               case NIC_ADDR:
2411                 msg = "&";
2412                 break;
2413               case NIC_PREINCREMENT:
2414                 msg = "++";
2415                 break;
2416               case NIC_PREDECREMENT:
2417                 msg = "--";
2418                 break;
2419               case NIC_NEW:
2420                 msg = "new";
2421                 break;
2422               case NIC_DEL:
2423                 msg = "delete";
2424                 break;
2425               default:
2426                 gcc_unreachable ();
2427             }
2428           if (msg)
2429             error ("%qs cannot appear in a constant-expression", msg);
2430           return true;
2431         }
2432     }
2433   return false;
2434 }
2435
2436 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2437    qualifying scope (or NULL, if none) for ID.  This function commits
2438    to the current active tentative parse, if any.  (Otherwise, the
2439    problematic construct might be encountered again later, resulting
2440    in duplicate error messages.) LOCATION is the location of ID.  */
2441
2442 static void
2443 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2444                                       tree scope, tree id,
2445                                       location_t location)
2446 {
2447   tree decl, old_scope;
2448   cp_parser_commit_to_tentative_parse (parser);
2449   /* Try to lookup the identifier.  */
2450   old_scope = parser->scope;
2451   parser->scope = scope;
2452   decl = cp_parser_lookup_name_simple (parser, id, location);
2453   parser->scope = old_scope;
2454   /* If the lookup found a template-name, it means that the user forgot
2455   to specify an argument list. Emit a useful error message.  */
2456   if (TREE_CODE (decl) == TEMPLATE_DECL)
2457     error_at (location,
2458               "invalid use of template-name %qE without an argument list",
2459               decl);
2460   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2461     error_at (location, "invalid use of destructor %qD as a type", id);
2462   else if (TREE_CODE (decl) == TYPE_DECL)
2463     /* Something like 'unsigned A a;'  */
2464     error_at (location, "invalid combination of multiple type-specifiers");
2465   else if (!parser->scope)
2466     {
2467       /* Issue an error message.  */
2468       error_at (location, "%qE does not name a type", id);
2469       /* If we're in a template class, it's possible that the user was
2470          referring to a type from a base class.  For example:
2471
2472            template <typename T> struct A { typedef T X; };
2473            template <typename T> struct B : public A<T> { X x; };
2474
2475          The user should have said "typename A<T>::X".  */
2476       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2477         inform (location, "C++0x %<constexpr%> only available with "
2478                 "-std=c++0x or -std=gnu++0x");
2479       else if (processing_template_decl && current_class_type
2480                && TYPE_BINFO (current_class_type))
2481         {
2482           tree b;
2483
2484           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2485                b;
2486                b = TREE_CHAIN (b))
2487             {
2488               tree base_type = BINFO_TYPE (b);
2489               if (CLASS_TYPE_P (base_type)
2490                   && dependent_type_p (base_type))
2491                 {
2492                   tree field;
2493                   /* Go from a particular instantiation of the
2494                      template (which will have an empty TYPE_FIELDs),
2495                      to the main version.  */
2496                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2497                   for (field = TYPE_FIELDS (base_type);
2498                        field;
2499                        field = DECL_CHAIN (field))
2500                     if (TREE_CODE (field) == TYPE_DECL
2501                         && DECL_NAME (field) == id)
2502                       {
2503                         inform (location, 
2504                                 "(perhaps %<typename %T::%E%> was intended)",
2505                                 BINFO_TYPE (b), id);
2506                         break;
2507                       }
2508                   if (field)
2509                     break;
2510                 }
2511             }
2512         }
2513     }
2514   /* Here we diagnose qualified-ids where the scope is actually correct,
2515      but the identifier does not resolve to a valid type name.  */
2516   else if (parser->scope != error_mark_node)
2517     {
2518       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2519         error_at (location, "%qE in namespace %qE does not name a type",
2520                   id, parser->scope);
2521       else if (CLASS_TYPE_P (parser->scope)
2522                && constructor_name_p (id, parser->scope))
2523         {
2524           /* A<T>::A<T>() */
2525           error_at (location, "%<%T::%E%> names the constructor, not"
2526                     " the type", parser->scope, id);
2527           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2528             error_at (location, "and %qT has no template constructors",
2529                       parser->scope);
2530         }
2531       else if (TYPE_P (parser->scope)
2532                && dependent_scope_p (parser->scope))
2533         error_at (location, "need %<typename%> before %<%T::%E%> because "
2534                   "%qT is a dependent scope",
2535                   parser->scope, id, parser->scope);
2536       else if (TYPE_P (parser->scope))
2537         error_at (location, "%qE in %q#T does not name a type",
2538                   id, parser->scope);
2539       else
2540         gcc_unreachable ();
2541     }
2542 }
2543
2544 /* Check for a common situation where a type-name should be present,
2545    but is not, and issue a sensible error message.  Returns true if an
2546    invalid type-name was detected.
2547
2548    The situation handled by this function are variable declarations of the
2549    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2550    Usually, `ID' should name a type, but if we got here it means that it
2551    does not. We try to emit the best possible error message depending on
2552    how exactly the id-expression looks like.  */
2553
2554 static bool
2555 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2556 {
2557   tree id;
2558   cp_token *token = cp_lexer_peek_token (parser->lexer);
2559
2560   /* Avoid duplicate error about ambiguous lookup.  */
2561   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2562     {
2563       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2564       if (next->type == CPP_NAME && next->ambiguous_p)
2565         goto out;
2566     }
2567
2568   cp_parser_parse_tentatively (parser);
2569   id = cp_parser_id_expression (parser,
2570                                 /*template_keyword_p=*/false,
2571                                 /*check_dependency_p=*/true,
2572                                 /*template_p=*/NULL,
2573                                 /*declarator_p=*/true,
2574                                 /*optional_p=*/false);
2575   /* If the next token is a (, this is a function with no explicit return
2576      type, i.e. constructor, destructor or conversion op.  */
2577   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2578       || TREE_CODE (id) == TYPE_DECL)
2579     {
2580       cp_parser_abort_tentative_parse (parser);
2581       return false;
2582     }
2583   if (!cp_parser_parse_definitely (parser))
2584     return false;
2585
2586   /* Emit a diagnostic for the invalid type.  */
2587   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2588                                         id, token->location);
2589  out:
2590   /* If we aren't in the middle of a declarator (i.e. in a
2591      parameter-declaration-clause), skip to the end of the declaration;
2592      there's no point in trying to process it.  */
2593   if (!parser->in_declarator_p)
2594     cp_parser_skip_to_end_of_block_or_statement (parser);
2595   return true;
2596 }
2597
2598 /* Consume tokens up to, and including, the next non-nested closing `)'.
2599    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2600    are doing error recovery. Returns -1 if OR_COMMA is true and we
2601    found an unnested comma.  */
2602
2603 static int
2604 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2605                                        bool recovering,
2606                                        bool or_comma,
2607                                        bool consume_paren)
2608 {
2609   unsigned paren_depth = 0;
2610   unsigned brace_depth = 0;
2611   unsigned square_depth = 0;
2612
2613   if (recovering && !or_comma
2614       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2615     return 0;
2616
2617   while (true)
2618     {
2619       cp_token * token = cp_lexer_peek_token (parser->lexer);
2620
2621       switch (token->type)
2622         {
2623         case CPP_EOF:
2624         case CPP_PRAGMA_EOL:
2625           /* If we've run out of tokens, then there is no closing `)'.  */
2626           return 0;
2627
2628         /* This is good for lambda expression capture-lists.  */
2629         case CPP_OPEN_SQUARE:
2630           ++square_depth;
2631           break;
2632         case CPP_CLOSE_SQUARE:
2633           if (!square_depth--)
2634             return 0;
2635           break;
2636
2637         case CPP_SEMICOLON:
2638           /* This matches the processing in skip_to_end_of_statement.  */
2639           if (!brace_depth)
2640             return 0;
2641           break;
2642
2643         case CPP_OPEN_BRACE:
2644           ++brace_depth;
2645           break;
2646         case CPP_CLOSE_BRACE:
2647           if (!brace_depth--)
2648             return 0;
2649           break;
2650
2651         case CPP_COMMA:
2652           if (recovering && or_comma && !brace_depth && !paren_depth
2653               && !square_depth)
2654             return -1;
2655           break;
2656
2657         case CPP_OPEN_PAREN:
2658           if (!brace_depth)
2659             ++paren_depth;
2660           break;
2661
2662         case CPP_CLOSE_PAREN:
2663           if (!brace_depth && !paren_depth--)
2664             {
2665               if (consume_paren)
2666                 cp_lexer_consume_token (parser->lexer);
2667               return 1;
2668             }
2669           break;
2670
2671         default:
2672           break;
2673         }
2674
2675       /* Consume the token.  */
2676       cp_lexer_consume_token (parser->lexer);
2677     }
2678 }
2679
2680 /* Consume tokens until we reach the end of the current statement.
2681    Normally, that will be just before consuming a `;'.  However, if a
2682    non-nested `}' comes first, then we stop before consuming that.  */
2683
2684 static void
2685 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2686 {
2687   unsigned nesting_depth = 0;
2688
2689   while (true)
2690     {
2691       cp_token *token = cp_lexer_peek_token (parser->lexer);
2692
2693       switch (token->type)
2694         {
2695         case CPP_EOF:
2696         case CPP_PRAGMA_EOL:
2697           /* If we've run out of tokens, stop.  */
2698           return;
2699
2700         case CPP_SEMICOLON:
2701           /* If the next token is a `;', we have reached the end of the
2702              statement.  */
2703           if (!nesting_depth)
2704             return;
2705           break;
2706
2707         case CPP_CLOSE_BRACE:
2708           /* If this is a non-nested '}', stop before consuming it.
2709              That way, when confronted with something like:
2710
2711                { 3 + }
2712
2713              we stop before consuming the closing '}', even though we
2714              have not yet reached a `;'.  */
2715           if (nesting_depth == 0)
2716             return;
2717
2718           /* If it is the closing '}' for a block that we have
2719              scanned, stop -- but only after consuming the token.
2720              That way given:
2721
2722                 void f g () { ... }
2723                 typedef int I;
2724
2725              we will stop after the body of the erroneously declared
2726              function, but before consuming the following `typedef'
2727              declaration.  */
2728           if (--nesting_depth == 0)
2729             {
2730               cp_lexer_consume_token (parser->lexer);
2731               return;
2732             }
2733
2734         case CPP_OPEN_BRACE:
2735           ++nesting_depth;
2736           break;
2737
2738         default:
2739           break;
2740         }
2741
2742       /* Consume the token.  */
2743       cp_lexer_consume_token (parser->lexer);
2744     }
2745 }
2746
2747 /* This function is called at the end of a statement or declaration.
2748    If the next token is a semicolon, it is consumed; otherwise, error
2749    recovery is attempted.  */
2750
2751 static void
2752 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2753 {
2754   /* Look for the trailing `;'.  */
2755   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2756     {
2757       /* If there is additional (erroneous) input, skip to the end of
2758          the statement.  */
2759       cp_parser_skip_to_end_of_statement (parser);
2760       /* If the next token is now a `;', consume it.  */
2761       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2762         cp_lexer_consume_token (parser->lexer);
2763     }
2764 }
2765
2766 /* Skip tokens until we have consumed an entire block, or until we
2767    have consumed a non-nested `;'.  */
2768
2769 static void
2770 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2771 {
2772   int nesting_depth = 0;
2773
2774   while (nesting_depth >= 0)
2775     {
2776       cp_token *token = cp_lexer_peek_token (parser->lexer);
2777
2778       switch (token->type)
2779         {
2780         case CPP_EOF:
2781         case CPP_PRAGMA_EOL:
2782           /* If we've run out of tokens, stop.  */
2783           return;
2784
2785         case CPP_SEMICOLON:
2786           /* Stop if this is an unnested ';'. */
2787           if (!nesting_depth)
2788             nesting_depth = -1;
2789           break;
2790
2791         case CPP_CLOSE_BRACE:
2792           /* Stop if this is an unnested '}', or closes the outermost
2793              nesting level.  */
2794           nesting_depth--;
2795           if (nesting_depth < 0)
2796             return;
2797           if (!nesting_depth)
2798             nesting_depth = -1;
2799           break;
2800
2801         case CPP_OPEN_BRACE:
2802           /* Nest. */
2803           nesting_depth++;
2804           break;
2805
2806         default:
2807           break;
2808         }
2809
2810       /* Consume the token.  */
2811       cp_lexer_consume_token (parser->lexer);
2812     }
2813 }
2814
2815 /* Skip tokens until a non-nested closing curly brace is the next
2816    token, or there are no more tokens. Return true in the first case,
2817    false otherwise.  */
2818
2819 static bool
2820 cp_parser_skip_to_closing_brace (cp_parser *parser)
2821 {
2822   unsigned nesting_depth = 0;
2823
2824   while (true)
2825     {
2826       cp_token *token = cp_lexer_peek_token (parser->lexer);
2827
2828       switch (token->type)
2829         {
2830         case CPP_EOF:
2831         case CPP_PRAGMA_EOL:
2832           /* If we've run out of tokens, stop.  */
2833           return false;
2834
2835         case CPP_CLOSE_BRACE:
2836           /* If the next token is a non-nested `}', then we have reached
2837              the end of the current block.  */
2838           if (nesting_depth-- == 0)
2839             return true;
2840           break;
2841
2842         case CPP_OPEN_BRACE:
2843           /* If it the next token is a `{', then we are entering a new
2844              block.  Consume the entire block.  */
2845           ++nesting_depth;
2846           break;
2847
2848         default:
2849           break;
2850         }
2851
2852       /* Consume the token.  */
2853       cp_lexer_consume_token (parser->lexer);
2854     }
2855 }
2856
2857 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2858    parameter is the PRAGMA token, allowing us to purge the entire pragma
2859    sequence.  */
2860
2861 static void
2862 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2863 {
2864   cp_token *token;
2865
2866   parser->lexer->in_pragma = false;
2867
2868   do
2869     token = cp_lexer_consume_token (parser->lexer);
2870   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2871
2872   /* Ensure that the pragma is not parsed again.  */
2873   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2874 }
2875
2876 /* Require pragma end of line, resyncing with it as necessary.  The
2877    arguments are as for cp_parser_skip_to_pragma_eol.  */
2878
2879 static void
2880 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2881 {
2882   parser->lexer->in_pragma = false;
2883   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2884     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2885 }
2886
2887 /* This is a simple wrapper around make_typename_type. When the id is
2888    an unresolved identifier node, we can provide a superior diagnostic
2889    using cp_parser_diagnose_invalid_type_name.  */
2890
2891 static tree
2892 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2893                               tree id, location_t id_location)
2894 {
2895   tree result;
2896   if (TREE_CODE (id) == IDENTIFIER_NODE)
2897     {
2898       result = make_typename_type (scope, id, typename_type,
2899                                    /*complain=*/tf_none);
2900       if (result == error_mark_node)
2901         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2902       return result;
2903     }
2904   return make_typename_type (scope, id, typename_type, tf_error);
2905 }
2906
2907 /* This is a wrapper around the
2908    make_{pointer,ptrmem,reference}_declarator functions that decides
2909    which one to call based on the CODE and CLASS_TYPE arguments. The
2910    CODE argument should be one of the values returned by
2911    cp_parser_ptr_operator. */
2912 static cp_declarator *
2913 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2914                                     cp_cv_quals cv_qualifiers,
2915                                     cp_declarator *target)
2916 {
2917   if (code == ERROR_MARK)
2918     return cp_error_declarator;
2919
2920   if (code == INDIRECT_REF)
2921     if (class_type == NULL_TREE)
2922       return make_pointer_declarator (cv_qualifiers, target);
2923     else
2924       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2925   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2926     return make_reference_declarator (cv_qualifiers, target, false);
2927   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2928     return make_reference_declarator (cv_qualifiers, target, true);
2929   gcc_unreachable ();
2930 }
2931
2932 /* Create a new C++ parser.  */
2933
2934 static cp_parser *
2935 cp_parser_new (void)
2936 {
2937   cp_parser *parser;
2938   cp_lexer *lexer;
2939   unsigned i;
2940
2941   /* cp_lexer_new_main is called before doing GC allocation because
2942      cp_lexer_new_main might load a PCH file.  */
2943   lexer = cp_lexer_new_main ();
2944
2945   /* Initialize the binops_by_token so that we can get the tree
2946      directly from the token.  */
2947   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2948     binops_by_token[binops[i].token_type] = binops[i];
2949
2950   parser = ggc_alloc_cleared_cp_parser ();
2951   parser->lexer = lexer;
2952   parser->context = cp_parser_context_new (NULL);
2953
2954   /* For now, we always accept GNU extensions.  */
2955   parser->allow_gnu_extensions_p = 1;
2956
2957   /* The `>' token is a greater-than operator, not the end of a
2958      template-id.  */
2959   parser->greater_than_is_operator_p = true;
2960
2961   parser->default_arg_ok_p = true;
2962
2963   /* We are not parsing a constant-expression.  */
2964   parser->integral_constant_expression_p = false;
2965   parser->allow_non_integral_constant_expression_p = false;
2966   parser->non_integral_constant_expression_p = false;
2967
2968   /* Local variable names are not forbidden.  */
2969   parser->local_variables_forbidden_p = false;
2970
2971   /* We are not processing an `extern "C"' declaration.  */
2972   parser->in_unbraced_linkage_specification_p = false;
2973
2974   /* We are not processing a declarator.  */
2975   parser->in_declarator_p = false;
2976
2977   /* We are not processing a template-argument-list.  */
2978   parser->in_template_argument_list_p = false;
2979
2980   /* We are not in an iteration statement.  */
2981   parser->in_statement = 0;
2982
2983   /* We are not in a switch statement.  */
2984   parser->in_switch_statement_p = false;
2985
2986   /* We are not parsing a type-id inside an expression.  */
2987   parser->in_type_id_in_expr_p = false;
2988
2989   /* Declarations aren't implicitly extern "C".  */
2990   parser->implicit_extern_c = false;
2991
2992   /* String literals should be translated to the execution character set.  */
2993   parser->translate_strings_p = true;
2994
2995   /* We are not parsing a function body.  */
2996   parser->in_function_body = false;
2997
2998   /* We can correct until told otherwise.  */
2999   parser->colon_corrects_to_scope_p = true;
3000
3001   /* The unparsed function queue is empty.  */
3002   push_unparsed_function_queues (parser);
3003
3004   /* There are no classes being defined.  */
3005   parser->num_classes_being_defined = 0;
3006
3007   /* No template parameters apply.  */
3008   parser->num_template_parameter_lists = 0;
3009
3010   return parser;
3011 }
3012
3013 /* Create a cp_lexer structure which will emit the tokens in CACHE
3014    and push it onto the parser's lexer stack.  This is used for delayed
3015    parsing of in-class method bodies and default arguments, and should
3016    not be confused with tentative parsing.  */
3017 static void
3018 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3019 {
3020   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3021   lexer->next = parser->lexer;
3022   parser->lexer = lexer;
3023
3024   /* Move the current source position to that of the first token in the
3025      new lexer.  */
3026   cp_lexer_set_source_position_from_token (lexer->next_token);
3027 }
3028
3029 /* Pop the top lexer off the parser stack.  This is never used for the
3030    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3031 static void
3032 cp_parser_pop_lexer (cp_parser *parser)
3033 {
3034   cp_lexer *lexer = parser->lexer;
3035   parser->lexer = lexer->next;
3036   cp_lexer_destroy (lexer);
3037
3038   /* Put the current source position back where it was before this
3039      lexer was pushed.  */
3040   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3041 }
3042
3043 /* Lexical conventions [gram.lex]  */
3044
3045 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3046    identifier.  */
3047
3048 static tree
3049 cp_parser_identifier (cp_parser* parser)
3050 {
3051   cp_token *token;
3052
3053   /* Look for the identifier.  */
3054   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3055   /* Return the value.  */
3056   return token ? token->u.value : error_mark_node;
3057 }
3058
3059 /* Parse a sequence of adjacent string constants.  Returns a
3060    TREE_STRING representing the combined, nul-terminated string
3061    constant.  If TRANSLATE is true, translate the string to the
3062    execution character set.  If WIDE_OK is true, a wide string is
3063    invalid here.
3064
3065    C++98 [lex.string] says that if a narrow string literal token is
3066    adjacent to a wide string literal token, the behavior is undefined.
3067    However, C99 6.4.5p4 says that this results in a wide string literal.
3068    We follow C99 here, for consistency with the C front end.
3069
3070    This code is largely lifted from lex_string() in c-lex.c.
3071
3072    FUTURE: ObjC++ will need to handle @-strings here.  */
3073 static tree
3074 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3075 {
3076   tree value;
3077   size_t count;
3078   struct obstack str_ob;
3079   cpp_string str, istr, *strs;
3080   cp_token *tok;
3081   enum cpp_ttype type;
3082
3083   tok = cp_lexer_peek_token (parser->lexer);
3084   if (!cp_parser_is_string_literal (tok))
3085     {
3086       cp_parser_error (parser, "expected string-literal");
3087       return error_mark_node;
3088     }
3089
3090   type = tok->type;
3091
3092   /* Try to avoid the overhead of creating and destroying an obstack
3093      for the common case of just one string.  */
3094   if (!cp_parser_is_string_literal
3095       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3096     {
3097       cp_lexer_consume_token (parser->lexer);
3098
3099       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3100       str.len = TREE_STRING_LENGTH (tok->u.value);
3101       count = 1;
3102
3103       strs = &str;
3104     }
3105   else
3106     {
3107       gcc_obstack_init (&str_ob);
3108       count = 0;
3109
3110       do
3111         {
3112           cp_lexer_consume_token (parser->lexer);
3113           count++;
3114           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3115           str.len = TREE_STRING_LENGTH (tok->u.value);
3116
3117           if (type != tok->type)
3118             {
3119               if (type == CPP_STRING)
3120                 type = tok->type;
3121               else if (tok->type != CPP_STRING)
3122                 error_at (tok->location,
3123                           "unsupported non-standard concatenation "
3124                           "of string literals");
3125             }
3126
3127           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3128
3129           tok = cp_lexer_peek_token (parser->lexer);
3130         }
3131       while (cp_parser_is_string_literal (tok));
3132
3133       strs = (cpp_string *) obstack_finish (&str_ob);
3134     }
3135
3136   if (type != CPP_STRING && !wide_ok)
3137     {
3138       cp_parser_error (parser, "a wide string is invalid in this context");
3139       type = CPP_STRING;
3140     }
3141
3142   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3143       (parse_in, strs, count, &istr, type))
3144     {
3145       value = build_string (istr.len, (const char *)istr.text);
3146       free (CONST_CAST (unsigned char *, istr.text));
3147
3148       switch (type)
3149         {
3150         default:
3151         case CPP_STRING:
3152         case CPP_UTF8STRING:
3153           TREE_TYPE (value) = char_array_type_node;
3154           break;
3155         case CPP_STRING16:
3156           TREE_TYPE (value) = char16_array_type_node;
3157           break;
3158         case CPP_STRING32:
3159           TREE_TYPE (value) = char32_array_type_node;
3160           break;
3161         case CPP_WSTRING:
3162           TREE_TYPE (value) = wchar_array_type_node;
3163           break;
3164         }
3165
3166       value = fix_string_type (value);
3167     }
3168   else
3169     /* cpp_interpret_string has issued an error.  */
3170     value = error_mark_node;
3171
3172   if (count > 1)
3173     obstack_free (&str_ob, 0);
3174
3175   return value;
3176 }
3177
3178
3179 /* Basic concepts [gram.basic]  */
3180
3181 /* Parse a translation-unit.
3182
3183    translation-unit:
3184      declaration-seq [opt]
3185
3186    Returns TRUE if all went well.  */
3187
3188 static bool
3189 cp_parser_translation_unit (cp_parser* parser)
3190 {
3191   /* The address of the first non-permanent object on the declarator
3192      obstack.  */
3193   static void *declarator_obstack_base;
3194
3195   bool success;
3196
3197   /* Create the declarator obstack, if necessary.  */
3198   if (!cp_error_declarator)
3199     {
3200       gcc_obstack_init (&declarator_obstack);
3201       /* Create the error declarator.  */
3202       cp_error_declarator = make_declarator (cdk_error);
3203       /* Create the empty parameter list.  */
3204       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3205       /* Remember where the base of the declarator obstack lies.  */
3206       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3207     }
3208
3209   cp_parser_declaration_seq_opt (parser);
3210
3211   /* If there are no tokens left then all went well.  */
3212   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3213     {
3214       /* Get rid of the token array; we don't need it any more.  */
3215       cp_lexer_destroy (parser->lexer);
3216       parser->lexer = NULL;
3217
3218       /* This file might have been a context that's implicitly extern
3219          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3220       if (parser->implicit_extern_c)
3221         {
3222           pop_lang_context ();
3223           parser->implicit_extern_c = false;
3224         }
3225
3226       /* Finish up.  */
3227       finish_translation_unit ();
3228
3229       success = true;
3230     }
3231   else
3232     {
3233       cp_parser_error (parser, "expected declaration");
3234       success = false;
3235     }
3236
3237   /* Make sure the declarator obstack was fully cleaned up.  */
3238   gcc_assert (obstack_next_free (&declarator_obstack)
3239               == declarator_obstack_base);
3240
3241   /* All went well.  */
3242   return success;
3243 }
3244
3245 /* Expressions [gram.expr] */
3246
3247 /* Parse a primary-expression.
3248
3249    primary-expression:
3250      literal
3251      this
3252      ( expression )
3253      id-expression
3254
3255    GNU Extensions:
3256
3257    primary-expression:
3258      ( compound-statement )
3259      __builtin_va_arg ( assignment-expression , type-id )
3260      __builtin_offsetof ( type-id , offsetof-expression )
3261
3262    C++ Extensions:
3263      __has_nothrow_assign ( type-id )   
3264      __has_nothrow_constructor ( type-id )
3265      __has_nothrow_copy ( type-id )
3266      __has_trivial_assign ( type-id )   
3267      __has_trivial_constructor ( type-id )
3268      __has_trivial_copy ( type-id )
3269      __has_trivial_destructor ( type-id )
3270      __has_virtual_destructor ( type-id )     
3271      __is_abstract ( type-id )
3272      __is_base_of ( type-id , type-id )
3273      __is_class ( type-id )
3274      __is_convertible_to ( type-id , type-id )     
3275      __is_empty ( type-id )
3276      __is_enum ( type-id )
3277      __is_literal_type ( type-id )
3278      __is_pod ( type-id )
3279      __is_polymorphic ( type-id )
3280      __is_std_layout ( type-id )
3281      __is_trivial ( type-id )
3282      __is_union ( type-id )
3283
3284    Objective-C++ Extension:
3285
3286    primary-expression:
3287      objc-expression
3288
3289    literal:
3290      __null
3291
3292    ADDRESS_P is true iff this expression was immediately preceded by
3293    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3294    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3295    true iff this expression is a template argument.
3296
3297    Returns a representation of the expression.  Upon return, *IDK
3298    indicates what kind of id-expression (if any) was present.  */
3299
3300 static tree
3301 cp_parser_primary_expression (cp_parser *parser,
3302                               bool address_p,
3303                               bool cast_p,
3304                               bool template_arg_p,
3305                               cp_id_kind *idk)
3306 {
3307   cp_token *token = NULL;
3308
3309   /* Assume the primary expression is not an id-expression.  */
3310   *idk = CP_ID_KIND_NONE;
3311
3312   /* Peek at the next token.  */
3313   token = cp_lexer_peek_token (parser->lexer);
3314   switch (token->type)
3315     {
3316       /* literal:
3317            integer-literal
3318            character-literal
3319            floating-literal
3320            string-literal
3321            boolean-literal  */
3322     case CPP_CHAR:
3323     case CPP_CHAR16:
3324     case CPP_CHAR32:
3325     case CPP_WCHAR:
3326     case CPP_NUMBER:
3327       token = cp_lexer_consume_token (parser->lexer);
3328       if (TREE_CODE (token->u.value) == FIXED_CST)
3329         {
3330           error_at (token->location,
3331                     "fixed-point types not supported in C++");
3332           return error_mark_node;
3333         }
3334       /* Floating-point literals are only allowed in an integral
3335          constant expression if they are cast to an integral or
3336          enumeration type.  */
3337       if (TREE_CODE (token->u.value) == REAL_CST
3338           && parser->integral_constant_expression_p
3339           && pedantic)
3340         {
3341           /* CAST_P will be set even in invalid code like "int(2.7 +
3342              ...)".   Therefore, we have to check that the next token
3343              is sure to end the cast.  */
3344           if (cast_p)
3345             {
3346               cp_token *next_token;
3347
3348               next_token = cp_lexer_peek_token (parser->lexer);
3349               if (/* The comma at the end of an
3350                      enumerator-definition.  */
3351                   next_token->type != CPP_COMMA
3352                   /* The curly brace at the end of an enum-specifier.  */
3353                   && next_token->type != CPP_CLOSE_BRACE
3354                   /* The end of a statement.  */
3355                   && next_token->type != CPP_SEMICOLON
3356                   /* The end of the cast-expression.  */
3357                   && next_token->type != CPP_CLOSE_PAREN
3358                   /* The end of an array bound.  */
3359                   && next_token->type != CPP_CLOSE_SQUARE
3360                   /* The closing ">" in a template-argument-list.  */
3361                   && (next_token->type != CPP_GREATER
3362                       || parser->greater_than_is_operator_p)
3363                   /* C++0x only: A ">>" treated like two ">" tokens,
3364                      in a template-argument-list.  */
3365                   && (next_token->type != CPP_RSHIFT
3366                       || (cxx_dialect == cxx98)
3367                       || parser->greater_than_is_operator_p))
3368                 cast_p = false;
3369             }
3370
3371           /* If we are within a cast, then the constraint that the
3372              cast is to an integral or enumeration type will be
3373              checked at that point.  If we are not within a cast, then
3374              this code is invalid.  */
3375           if (!cast_p)
3376             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3377         }
3378       return token->u.value;
3379
3380     case CPP_STRING:
3381     case CPP_STRING16:
3382     case CPP_STRING32:
3383     case CPP_WSTRING:
3384     case CPP_UTF8STRING:
3385       /* ??? Should wide strings be allowed when parser->translate_strings_p
3386          is false (i.e. in attributes)?  If not, we can kill the third
3387          argument to cp_parser_string_literal.  */
3388       return cp_parser_string_literal (parser,
3389                                        parser->translate_strings_p,
3390                                        true);
3391
3392     case CPP_OPEN_PAREN:
3393       {
3394         tree expr;
3395         bool saved_greater_than_is_operator_p;
3396
3397         /* Consume the `('.  */
3398         cp_lexer_consume_token (parser->lexer);
3399         /* Within a parenthesized expression, a `>' token is always
3400            the greater-than operator.  */
3401         saved_greater_than_is_operator_p
3402           = parser->greater_than_is_operator_p;
3403         parser->greater_than_is_operator_p = true;
3404         /* If we see `( { ' then we are looking at the beginning of
3405            a GNU statement-expression.  */
3406         if (cp_parser_allow_gnu_extensions_p (parser)
3407             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3408           {
3409             /* Statement-expressions are not allowed by the standard.  */
3410             pedwarn (token->location, OPT_pedantic, 
3411                      "ISO C++ forbids braced-groups within expressions");
3412
3413             /* And they're not allowed outside of a function-body; you
3414                cannot, for example, write:
3415
3416                  int i = ({ int j = 3; j + 1; });
3417
3418                at class or namespace scope.  */
3419             if (!parser->in_function_body
3420                 || parser->in_template_argument_list_p)
3421               {
3422                 error_at (token->location,
3423                           "statement-expressions are not allowed outside "
3424                           "functions nor in template-argument lists");
3425                 cp_parser_skip_to_end_of_block_or_statement (parser);
3426                 expr = error_mark_node;
3427               }
3428             else
3429               {
3430                 /* Start the statement-expression.  */
3431                 expr = begin_stmt_expr ();
3432                 /* Parse the compound-statement.  */
3433                 cp_parser_compound_statement (parser, expr, false, false);
3434                 /* Finish up.  */
3435                 expr = finish_stmt_expr (expr, false);
3436               }
3437           }
3438         else
3439           {
3440             /* Parse the parenthesized expression.  */
3441             expr = cp_parser_expression (parser, cast_p, idk);
3442             /* Let the front end know that this expression was
3443                enclosed in parentheses. This matters in case, for
3444                example, the expression is of the form `A::B', since
3445                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3446                not.  */
3447             finish_parenthesized_expr (expr);
3448             /* DR 705: Wrapping an unqualified name in parentheses
3449                suppresses arg-dependent lookup.  We want to pass back
3450                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3451                (c++/37862), but none of the others.  */
3452             if (*idk != CP_ID_KIND_QUALIFIED)
3453               *idk = CP_ID_KIND_NONE;
3454           }
3455         /* The `>' token might be the end of a template-id or
3456            template-parameter-list now.  */
3457         parser->greater_than_is_operator_p
3458           = saved_greater_than_is_operator_p;
3459         /* Consume the `)'.  */
3460         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3461           cp_parser_skip_to_end_of_statement (parser);
3462
3463         return expr;
3464       }
3465
3466     case CPP_OPEN_SQUARE:
3467       if (c_dialect_objc ())
3468         /* We have an Objective-C++ message. */
3469         return cp_parser_objc_expression (parser);
3470       {
3471         tree lam = cp_parser_lambda_expression (parser);
3472         /* Don't warn about a failed tentative parse.  */
3473         if (cp_parser_error_occurred (parser))
3474           return error_mark_node;
3475         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3476         return lam;
3477       }
3478
3479     case CPP_OBJC_STRING:
3480       if (c_dialect_objc ())
3481         /* We have an Objective-C++ string literal. */
3482         return cp_parser_objc_expression (parser);
3483       cp_parser_error (parser, "expected primary-expression");
3484       return error_mark_node;
3485
3486     case CPP_KEYWORD:
3487       switch (token->keyword)
3488         {
3489           /* These two are the boolean literals.  */
3490         case RID_TRUE:
3491           cp_lexer_consume_token (parser->lexer);
3492           return boolean_true_node;
3493         case RID_FALSE:
3494           cp_lexer_consume_token (parser->lexer);
3495           return boolean_false_node;
3496
3497           /* The `__null' literal.  */
3498         case RID_NULL:
3499           cp_lexer_consume_token (parser->lexer);
3500           return null_node;
3501
3502           /* The `nullptr' literal.  */
3503         case RID_NULLPTR:
3504           cp_lexer_consume_token (parser->lexer);
3505           return nullptr_node;
3506
3507           /* Recognize the `this' keyword.  */
3508         case RID_THIS:
3509           cp_lexer_consume_token (parser->lexer);
3510           if (parser->local_variables_forbidden_p)
3511             {
3512               error_at (token->location,
3513                         "%<this%> may not be used in this context");
3514               return error_mark_node;
3515             }
3516           /* Pointers cannot appear in constant-expressions.  */
3517           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3518             return error_mark_node;
3519           return finish_this_expr ();
3520
3521           /* The `operator' keyword can be the beginning of an
3522              id-expression.  */
3523         case RID_OPERATOR:
3524           goto id_expression;
3525
3526         case RID_FUNCTION_NAME:
3527         case RID_PRETTY_FUNCTION_NAME:
3528         case RID_C99_FUNCTION_NAME:
3529           {
3530             non_integral_constant name;
3531
3532             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3533                __func__ are the names of variables -- but they are
3534                treated specially.  Therefore, they are handled here,
3535                rather than relying on the generic id-expression logic
3536                below.  Grammatically, these names are id-expressions.
3537
3538                Consume the token.  */
3539             token = cp_lexer_consume_token (parser->lexer);
3540
3541             switch (token->keyword)
3542               {
3543               case RID_FUNCTION_NAME:
3544                 name = NIC_FUNC_NAME;
3545                 break;
3546               case RID_PRETTY_FUNCTION_NAME:
3547                 name = NIC_PRETTY_FUNC;
3548                 break;
3549               case RID_C99_FUNCTION_NAME:
3550                 name = NIC_C99_FUNC;
3551                 break;
3552               default:
3553                 gcc_unreachable ();
3554               }
3555
3556             if (cp_parser_non_integral_constant_expression (parser, name))
3557               return error_mark_node;
3558
3559             /* Look up the name.  */
3560             return finish_fname (token->u.value);
3561           }
3562
3563         case RID_VA_ARG:
3564           {
3565             tree expression;
3566             tree type;
3567
3568             /* The `__builtin_va_arg' construct is used to handle
3569                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3570             cp_lexer_consume_token (parser->lexer);
3571             /* Look for the opening `('.  */
3572             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3573             /* Now, parse the assignment-expression.  */
3574             expression = cp_parser_assignment_expression (parser,
3575                                                           /*cast_p=*/false, NULL);
3576             /* Look for the `,'.  */
3577             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3578             /* Parse the type-id.  */
3579             type = cp_parser_type_id (parser);
3580             /* Look for the closing `)'.  */
3581             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3582             /* Using `va_arg' in a constant-expression is not
3583                allowed.  */
3584             if (cp_parser_non_integral_constant_expression (parser,
3585                                                             NIC_VA_ARG))
3586               return error_mark_node;
3587             return build_x_va_arg (expression, type);
3588           }
3589
3590         case RID_OFFSETOF:
3591           return cp_parser_builtin_offsetof (parser);
3592
3593         case RID_HAS_NOTHROW_ASSIGN:
3594         case RID_HAS_NOTHROW_CONSTRUCTOR:
3595         case RID_HAS_NOTHROW_COPY:        
3596         case RID_HAS_TRIVIAL_ASSIGN:
3597         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3598         case RID_HAS_TRIVIAL_COPY:        
3599         case RID_HAS_TRIVIAL_DESTRUCTOR:
3600         case RID_HAS_VIRTUAL_DESTRUCTOR:
3601         case RID_IS_ABSTRACT:
3602         case RID_IS_BASE_OF:
3603         case RID_IS_CLASS:
3604         case RID_IS_CONVERTIBLE_TO:
3605         case RID_IS_EMPTY:
3606         case RID_IS_ENUM:
3607         case RID_IS_LITERAL_TYPE:
3608         case RID_IS_POD:
3609         case RID_IS_POLYMORPHIC:
3610         case RID_IS_STD_LAYOUT:
3611         case RID_IS_TRIVIAL:
3612         case RID_IS_UNION:
3613           return cp_parser_trait_expr (parser, token->keyword);
3614
3615         /* Objective-C++ expressions.  */
3616         case RID_AT_ENCODE:
3617         case RID_AT_PROTOCOL:
3618         case RID_AT_SELECTOR:
3619           return cp_parser_objc_expression (parser);
3620
3621         case RID_TEMPLATE:
3622           if (parser->in_function_body
3623               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3624                   == CPP_LESS))
3625             {
3626               error_at (token->location,
3627                         "a template declaration cannot appear at block scope");
3628               cp_parser_skip_to_end_of_block_or_statement (parser);
3629               return error_mark_node;
3630             }
3631         default:
3632           cp_parser_error (parser, "expected primary-expression");
3633           return error_mark_node;
3634         }
3635
3636       /* An id-expression can start with either an identifier, a
3637          `::' as the beginning of a qualified-id, or the "operator"
3638          keyword.  */
3639     case CPP_NAME:
3640     case CPP_SCOPE:
3641     case CPP_TEMPLATE_ID:
3642     case CPP_NESTED_NAME_SPECIFIER:
3643       {
3644         tree id_expression;
3645         tree decl;
3646         const char *error_msg;
3647         bool template_p;
3648         bool done;
3649         cp_token *id_expr_token;
3650
3651       id_expression:
3652         /* Parse the id-expression.  */
3653         id_expression
3654           = cp_parser_id_expression (parser,
3655                                      /*template_keyword_p=*/false,
3656                                      /*check_dependency_p=*/true,
3657                                      &template_p,
3658                                      /*declarator_p=*/false,
3659                                      /*optional_p=*/false);
3660         if (id_expression == error_mark_node)
3661           return error_mark_node;
3662         id_expr_token = token;
3663         token = cp_lexer_peek_token (parser->lexer);
3664         done = (token->type != CPP_OPEN_SQUARE
3665                 && token->type != CPP_OPEN_PAREN
3666                 && token->type != CPP_DOT
3667                 && token->type != CPP_DEREF
3668                 && token->type != CPP_PLUS_PLUS
3669                 && token->type != CPP_MINUS_MINUS);
3670         /* If we have a template-id, then no further lookup is
3671            required.  If the template-id was for a template-class, we
3672            will sometimes have a TYPE_DECL at this point.  */
3673         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3674                  || TREE_CODE (id_expression) == TYPE_DECL)
3675           decl = id_expression;
3676         /* Look up the name.  */
3677         else
3678           {
3679             tree ambiguous_decls;
3680
3681             /* If we already know that this lookup is ambiguous, then
3682                we've already issued an error message; there's no reason
3683                to check again.  */
3684             if (id_expr_token->type == CPP_NAME
3685                 && id_expr_token->ambiguous_p)
3686               {
3687                 cp_parser_simulate_error (parser);
3688                 return error_mark_node;
3689               }
3690
3691             decl = cp_parser_lookup_name (parser, id_expression,
3692                                           none_type,
3693                                           template_p,
3694                                           /*is_namespace=*/false,
3695                                           /*check_dependency=*/true,
3696                                           &ambiguous_decls,
3697                                           id_expr_token->location);
3698             /* If the lookup was ambiguous, an error will already have
3699                been issued.  */
3700             if (ambiguous_decls)
3701               return error_mark_node;
3702
3703             /* In Objective-C++, we may have an Objective-C 2.0
3704                dot-syntax for classes here.  */
3705             if (c_dialect_objc ()
3706                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3707                 && TREE_CODE (decl) == TYPE_DECL
3708                 && objc_is_class_name (decl))
3709               {
3710                 tree component;
3711                 cp_lexer_consume_token (parser->lexer);
3712                 component = cp_parser_identifier (parser);
3713                 if (component == error_mark_node)
3714                   return error_mark_node;
3715
3716                 return objc_build_class_component_ref (id_expression, component);
3717               }
3718
3719             /* In Objective-C++, an instance variable (ivar) may be preferred
3720                to whatever cp_parser_lookup_name() found.  */
3721             decl = objc_lookup_ivar (decl, id_expression);
3722
3723             /* If name lookup gives us a SCOPE_REF, then the
3724                qualifying scope was dependent.  */
3725             if (TREE_CODE (decl) == SCOPE_REF)
3726               {
3727                 /* At this point, we do not know if DECL is a valid
3728                    integral constant expression.  We assume that it is
3729                    in fact such an expression, so that code like:
3730
3731                       template <int N> struct A {
3732                         int a[B<N>::i];
3733                       };
3734                      
3735                    is accepted.  At template-instantiation time, we
3736                    will check that B<N>::i is actually a constant.  */
3737                 return decl;
3738               }
3739             /* Check to see if DECL is a local variable in a context
3740                where that is forbidden.  */
3741             if (parser->local_variables_forbidden_p
3742                 && local_variable_p (decl))
3743               {
3744                 /* It might be that we only found DECL because we are
3745                    trying to be generous with pre-ISO scoping rules.
3746                    For example, consider:
3747
3748                      int i;
3749                      void g() {
3750                        for (int i = 0; i < 10; ++i) {}
3751                        extern void f(int j = i);
3752                      }
3753
3754                    Here, name look up will originally find the out
3755                    of scope `i'.  We need to issue a warning message,
3756                    but then use the global `i'.  */
3757                 decl = check_for_out_of_scope_variable (decl);
3758                 if (local_variable_p (decl))
3759                   {
3760                     error_at (id_expr_token->location,
3761                               "local variable %qD may not appear in this context",
3762                               decl);
3763                     return error_mark_node;
3764                   }
3765               }
3766           }
3767
3768         decl = (finish_id_expression
3769                 (id_expression, decl, parser->scope,
3770                  idk,
3771                  parser->integral_constant_expression_p,
3772                  parser->allow_non_integral_constant_expression_p,
3773                  &parser->non_integral_constant_expression_p,
3774                  template_p, done, address_p,
3775                  template_arg_p,
3776                  &error_msg,
3777                  id_expr_token->location));
3778         if (error_msg)
3779           cp_parser_error (parser, error_msg);
3780         return decl;
3781       }
3782
3783       /* Anything else is an error.  */
3784     default:
3785       cp_parser_error (parser, "expected primary-expression");
3786       return error_mark_node;
3787     }
3788 }
3789
3790 /* Parse an id-expression.
3791
3792    id-expression:
3793      unqualified-id
3794      qualified-id
3795
3796    qualified-id:
3797      :: [opt] nested-name-specifier template [opt] unqualified-id
3798      :: identifier
3799      :: operator-function-id
3800      :: template-id
3801
3802    Return a representation of the unqualified portion of the
3803    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3804    a `::' or nested-name-specifier.
3805
3806    Often, if the id-expression was a qualified-id, the caller will
3807    want to make a SCOPE_REF to represent the qualified-id.  This
3808    function does not do this in order to avoid wastefully creating
3809    SCOPE_REFs when they are not required.
3810
3811    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3812    `template' keyword.
3813
3814    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3815    uninstantiated templates.
3816
3817    If *TEMPLATE_P is non-NULL, it is set to true iff the
3818    `template' keyword is used to explicitly indicate that the entity
3819    named is a template.
3820
3821    If DECLARATOR_P is true, the id-expression is appearing as part of
3822    a declarator, rather than as part of an expression.  */
3823
3824 static tree
3825 cp_parser_id_expression (cp_parser *parser,
3826                          bool template_keyword_p,
3827                          bool check_dependency_p,
3828                          bool *template_p,
3829                          bool declarator_p,
3830                          bool optional_p)
3831 {
3832   bool global_scope_p;
3833   bool nested_name_specifier_p;
3834
3835   /* Assume the `template' keyword was not used.  */
3836   if (template_p)
3837     *template_p = template_keyword_p;
3838
3839   /* Look for the optional `::' operator.  */
3840   global_scope_p
3841     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3842        != NULL_TREE);
3843   /* Look for the optional nested-name-specifier.  */
3844   nested_name_specifier_p
3845     = (cp_parser_nested_name_specifier_opt (parser,
3846                                             /*typename_keyword_p=*/false,
3847                                             check_dependency_p,
3848                                             /*type_p=*/false,
3849                                             declarator_p)
3850        != NULL_TREE);
3851   /* If there is a nested-name-specifier, then we are looking at
3852      the first qualified-id production.  */
3853   if (nested_name_specifier_p)
3854     {
3855       tree saved_scope;
3856       tree saved_object_scope;
3857       tree saved_qualifying_scope;
3858       tree unqualified_id;
3859       bool is_template;
3860
3861       /* See if the next token is the `template' keyword.  */
3862       if (!template_p)
3863         template_p = &is_template;
3864       *template_p = cp_parser_optional_template_keyword (parser);
3865       /* Name lookup we do during the processing of the
3866          unqualified-id might obliterate SCOPE.  */
3867       saved_scope = parser->scope;
3868       saved_object_scope = parser->object_scope;
3869       saved_qualifying_scope = parser->qualifying_scope;
3870       /* Process the final unqualified-id.  */
3871       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3872                                                  check_dependency_p,
3873                                                  declarator_p,
3874                                                  /*optional_p=*/false);
3875       /* Restore the SAVED_SCOPE for our caller.  */
3876       parser->scope = saved_scope;
3877       parser->object_scope = saved_object_scope;
3878       parser->qualifying_scope = saved_qualifying_scope;
3879
3880       return unqualified_id;
3881     }
3882   /* Otherwise, if we are in global scope, then we are looking at one
3883      of the other qualified-id productions.  */
3884   else if (global_scope_p)
3885     {
3886       cp_token *token;
3887       tree id;
3888
3889       /* Peek at the next token.  */
3890       token = cp_lexer_peek_token (parser->lexer);
3891
3892       /* If it's an identifier, and the next token is not a "<", then
3893          we can avoid the template-id case.  This is an optimization
3894          for this common case.  */
3895       if (token->type == CPP_NAME
3896           && !cp_parser_nth_token_starts_template_argument_list_p
3897                (parser, 2))
3898         return cp_parser_identifier (parser);
3899
3900       cp_parser_parse_tentatively (parser);
3901       /* Try a template-id.  */
3902       id = cp_parser_template_id (parser,
3903                                   /*template_keyword_p=*/false,
3904                                   /*check_dependency_p=*/true,
3905                                   declarator_p);
3906       /* If that worked, we're done.  */
3907       if (cp_parser_parse_definitely (parser))
3908         return id;
3909
3910       /* Peek at the next token.  (Changes in the token buffer may
3911          have invalidated the pointer obtained above.)  */
3912       token = cp_lexer_peek_token (parser->lexer);
3913
3914       switch (token->type)
3915         {
3916         case CPP_NAME:
3917           return cp_parser_identifier (parser);
3918
3919         case CPP_KEYWORD:
3920           if (token->keyword == RID_OPERATOR)
3921             return cp_parser_operator_function_id (parser);
3922           /* Fall through.  */
3923
3924         default:
3925           cp_parser_error (parser, "expected id-expression");
3926           return error_mark_node;
3927         }
3928     }
3929   else
3930     return cp_parser_unqualified_id (parser, template_keyword_p,
3931                                      /*check_dependency_p=*/true,
3932                                      declarator_p,
3933                                      optional_p);
3934 }
3935
3936 /* Parse an unqualified-id.
3937
3938    unqualified-id:
3939      identifier
3940      operator-function-id
3941      conversion-function-id
3942      ~ class-name
3943      template-id
3944
3945    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3946    keyword, in a construct like `A::template ...'.
3947
3948    Returns a representation of unqualified-id.  For the `identifier'
3949    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3950    production a BIT_NOT_EXPR is returned; the operand of the
3951    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3952    other productions, see the documentation accompanying the
3953    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3954    names are looked up in uninstantiated templates.  If DECLARATOR_P
3955    is true, the unqualified-id is appearing as part of a declarator,
3956    rather than as part of an expression.  */
3957
3958 static tree
3959 cp_parser_unqualified_id (cp_parser* parser,
3960                           bool template_keyword_p,
3961                           bool check_dependency_p,
3962                           bool declarator_p,
3963                           bool optional_p)
3964 {
3965   cp_token *token;
3966
3967   /* Peek at the next token.  */
3968   token = cp_lexer_peek_token (parser->lexer);
3969
3970   switch (token->type)
3971     {
3972     case CPP_NAME:
3973       {
3974         tree id;
3975
3976         /* We don't know yet whether or not this will be a
3977            template-id.  */
3978         cp_parser_parse_tentatively (parser);
3979         /* Try a template-id.  */
3980         id = cp_parser_template_id (parser, template_keyword_p,
3981                                     check_dependency_p,
3982                                     declarator_p);
3983         /* If it worked, we're done.  */
3984         if (cp_parser_parse_definitely (parser))
3985           return id;
3986         /* Otherwise, it's an ordinary identifier.  */
3987         return cp_parser_identifier (parser);
3988       }
3989
3990     case CPP_TEMPLATE_ID:
3991       return cp_parser_template_id (parser, template_keyword_p,
3992                                     check_dependency_p,
3993                                     declarator_p);
3994
3995     case CPP_COMPL:
3996       {
3997         tree type_decl;
3998         tree qualifying_scope;
3999         tree object_scope;
4000         tree scope;
4001         bool done;
4002
4003         /* Consume the `~' token.  */
4004         cp_lexer_consume_token (parser->lexer);
4005         /* Parse the class-name.  The standard, as written, seems to
4006            say that:
4007
4008              template <typename T> struct S { ~S (); };
4009              template <typename T> S<T>::~S() {}
4010
4011            is invalid, since `~' must be followed by a class-name, but
4012            `S<T>' is dependent, and so not known to be a class.
4013            That's not right; we need to look in uninstantiated
4014            templates.  A further complication arises from:
4015
4016              template <typename T> void f(T t) {
4017                t.T::~T();
4018              }
4019
4020            Here, it is not possible to look up `T' in the scope of `T'
4021            itself.  We must look in both the current scope, and the
4022            scope of the containing complete expression.
4023
4024            Yet another issue is:
4025
4026              struct S {
4027                int S;
4028                ~S();
4029              };
4030
4031              S::~S() {}
4032
4033            The standard does not seem to say that the `S' in `~S'
4034            should refer to the type `S' and not the data member
4035            `S::S'.  */
4036
4037         /* DR 244 says that we look up the name after the "~" in the
4038            same scope as we looked up the qualifying name.  That idea
4039            isn't fully worked out; it's more complicated than that.  */
4040         scope = parser->scope;
4041         object_scope = parser->object_scope;
4042         qualifying_scope = parser->qualifying_scope;
4043
4044         /* Check for invalid scopes.  */
4045         if (scope == error_mark_node)
4046           {
4047             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4048               cp_lexer_consume_token (parser->lexer);
4049             return error_mark_node;
4050           }
4051         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4052           {
4053             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4054               error_at (token->location,
4055                         "scope %qT before %<~%> is not a class-name",
4056                         scope);
4057             cp_parser_simulate_error (parser);
4058             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4059               cp_lexer_consume_token (parser->lexer);
4060             return error_mark_node;
4061           }
4062         gcc_assert (!scope || TYPE_P (scope));
4063
4064         /* If the name is of the form "X::~X" it's OK even if X is a
4065            typedef.  */
4066         token = cp_lexer_peek_token (parser->lexer);
4067         if (scope
4068             && token->type == CPP_NAME
4069             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4070                 != CPP_LESS)
4071             && (token->u.value == TYPE_IDENTIFIER (scope)
4072                 || (CLASS_TYPE_P (scope)
4073                     && constructor_name_p (token->u.value, scope))))
4074           {
4075             cp_lexer_consume_token (parser->lexer);
4076             return build_nt (BIT_NOT_EXPR, scope);
4077           }
4078
4079         /* If there was an explicit qualification (S::~T), first look
4080            in the scope given by the qualification (i.e., S).
4081
4082            Note: in the calls to cp_parser_class_name below we pass
4083            typename_type so that lookup finds the injected-class-name
4084            rather than the constructor.  */
4085         done = false;
4086         type_decl = NULL_TREE;
4087         if (scope)
4088           {
4089             cp_parser_parse_tentatively (parser);
4090             type_decl = cp_parser_class_name (parser,
4091                                               /*typename_keyword_p=*/false,
4092                                               /*template_keyword_p=*/false,
4093                                               typename_type,
4094                                               /*check_dependency=*/false,
4095                                               /*class_head_p=*/false,
4096                                               declarator_p);
4097             if (cp_parser_parse_definitely (parser))
4098               done = true;
4099           }
4100         /* In "N::S::~S", look in "N" as well.  */
4101         if (!done && scope && qualifying_scope)
4102           {
4103             cp_parser_parse_tentatively (parser);
4104             parser->scope = qualifying_scope;
4105             parser->object_scope = NULL_TREE;
4106             parser->qualifying_scope = NULL_TREE;
4107             type_decl
4108               = cp_parser_class_name (parser,
4109                                       /*typename_keyword_p=*/false,
4110                                       /*template_keyword_p=*/false,
4111                                       typename_type,
4112                                       /*check_dependency=*/false,
4113                                       /*class_head_p=*/false,
4114                                       declarator_p);
4115             if (cp_parser_parse_definitely (parser))
4116               done = true;
4117           }
4118         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4119         else if (!done && object_scope)
4120           {
4121             cp_parser_parse_tentatively (parser);
4122             parser->scope = object_scope;
4123             parser->object_scope = NULL_TREE;
4124             parser->qualifying_scope = NULL_TREE;
4125             type_decl
4126               = cp_parser_class_name (parser,
4127                                       /*typename_keyword_p=*/false,
4128                                       /*template_keyword_p=*/false,
4129                                       typename_type,
4130                                       /*check_dependency=*/false,
4131                                       /*class_head_p=*/false,
4132                                       declarator_p);
4133             if (cp_parser_parse_definitely (parser))
4134               done = true;
4135           }
4136         /* Look in the surrounding context.  */
4137         if (!done)
4138           {
4139             parser->scope = NULL_TREE;
4140             parser->object_scope = NULL_TREE;
4141             parser->qualifying_scope = NULL_TREE;
4142             if (processing_template_decl)
4143               cp_parser_parse_tentatively (parser);
4144             type_decl
4145               = cp_parser_class_name (parser,
4146                                       /*typename_keyword_p=*/false,
4147                                       /*template_keyword_p=*/false,
4148                                       typename_type,
4149                                       /*check_dependency=*/false,
4150                                       /*class_head_p=*/false,
4151                                       declarator_p);
4152             if (processing_template_decl
4153                 && ! cp_parser_parse_definitely (parser))
4154               {
4155                 /* We couldn't find a type with this name, so just accept
4156                    it and check for a match at instantiation time.  */
4157                 type_decl = cp_parser_identifier (parser);
4158                 if (type_decl != error_mark_node)
4159                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4160                 return type_decl;
4161               }
4162           }
4163         /* If an error occurred, assume that the name of the
4164            destructor is the same as the name of the qualifying
4165            class.  That allows us to keep parsing after running
4166            into ill-formed destructor names.  */
4167         if (type_decl == error_mark_node && scope)
4168           return build_nt (BIT_NOT_EXPR, scope);
4169         else if (type_decl == error_mark_node)
4170           return error_mark_node;
4171
4172         /* Check that destructor name and scope match.  */
4173         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4174           {
4175             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4176               error_at (token->location,
4177                         "declaration of %<~%T%> as member of %qT",
4178                         type_decl, scope);
4179             cp_parser_simulate_error (parser);
4180             return error_mark_node;
4181           }
4182
4183         /* [class.dtor]
4184
4185            A typedef-name that names a class shall not be used as the
4186            identifier in the declarator for a destructor declaration.  */
4187         if (declarator_p
4188             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4189             && !DECL_SELF_REFERENCE_P (type_decl)
4190             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4191           error_at (token->location,
4192                     "typedef-name %qD used as destructor declarator",
4193                     type_decl);
4194
4195         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4196       }
4197
4198     case CPP_KEYWORD:
4199       if (token->keyword == RID_OPERATOR)
4200         {
4201           tree id;
4202
4203           /* This could be a template-id, so we try that first.  */
4204           cp_parser_parse_tentatively (parser);
4205           /* Try a template-id.  */
4206           id = cp_parser_template_id (parser, template_keyword_p,
4207                                       /*check_dependency_p=*/true,
4208                                       declarator_p);
4209           /* If that worked, we're done.  */
4210           if (cp_parser_parse_definitely (parser))
4211             return id;
4212           /* We still don't know whether we're looking at an
4213              operator-function-id or a conversion-function-id.  */
4214           cp_parser_parse_tentatively (parser);
4215           /* Try an operator-function-id.  */
4216           id = cp_parser_operator_function_id (parser);
4217           /* If that didn't work, try a conversion-function-id.  */
4218           if (!cp_parser_parse_definitely (parser))
4219             id = cp_parser_conversion_function_id (parser);
4220
4221           return id;
4222         }
4223       /* Fall through.  */
4224
4225     default:
4226       if (optional_p)
4227         return NULL_TREE;
4228       cp_parser_error (parser, "expected unqualified-id");
4229       return error_mark_node;
4230     }
4231 }
4232
4233 /* Parse an (optional) nested-name-specifier.
4234
4235    nested-name-specifier: [C++98]
4236      class-or-namespace-name :: nested-name-specifier [opt]
4237      class-or-namespace-name :: template nested-name-specifier [opt]
4238
4239    nested-name-specifier: [C++0x]
4240      type-name ::
4241      namespace-name ::
4242      nested-name-specifier identifier ::
4243      nested-name-specifier template [opt] simple-template-id ::
4244
4245    PARSER->SCOPE should be set appropriately before this function is
4246    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4247    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4248    in name lookups.
4249
4250    Sets PARSER->SCOPE to the class (TYPE) or namespace
4251    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4252    it unchanged if there is no nested-name-specifier.  Returns the new
4253    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4254
4255    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4256    part of a declaration and/or decl-specifier.  */
4257
4258 static tree
4259 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4260                                      bool typename_keyword_p,
4261                                      bool check_dependency_p,
4262                                      bool type_p,
4263                                      bool is_declaration)
4264 {
4265   bool success = false;
4266   cp_token_position start = 0;
4267   cp_token *token;
4268
4269   /* Remember where the nested-name-specifier starts.  */
4270   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4271     {
4272       start = cp_lexer_token_position (parser->lexer, false);
4273       push_deferring_access_checks (dk_deferred);
4274     }
4275
4276   while (true)
4277     {
4278       tree new_scope;
4279       tree old_scope;
4280       tree saved_qualifying_scope;
4281       bool template_keyword_p;
4282
4283       /* Spot cases that cannot be the beginning of a
4284          nested-name-specifier.  */
4285       token = cp_lexer_peek_token (parser->lexer);
4286
4287       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4288          the already parsed nested-name-specifier.  */
4289       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4290         {
4291           /* Grab the nested-name-specifier and continue the loop.  */
4292           cp_parser_pre_parsed_nested_name_specifier (parser);
4293           /* If we originally encountered this nested-name-specifier
4294              with IS_DECLARATION set to false, we will not have
4295              resolved TYPENAME_TYPEs, so we must do so here.  */
4296           if (is_declaration
4297               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4298             {
4299               new_scope = resolve_typename_type (parser->scope,
4300                                                  /*only_current_p=*/false);
4301               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4302                 parser->scope = new_scope;
4303             }
4304           success = true;
4305           continue;
4306         }
4307
4308       /* Spot cases that cannot be the beginning of a
4309          nested-name-specifier.  On the second and subsequent times
4310          through the loop, we look for the `template' keyword.  */
4311       if (success && token->keyword == RID_TEMPLATE)
4312         ;
4313       /* A template-id can start a nested-name-specifier.  */
4314       else if (token->type == CPP_TEMPLATE_ID)
4315         ;
4316       else
4317         {
4318           /* If the next token is not an identifier, then it is
4319              definitely not a type-name or namespace-name.  */
4320           if (token->type != CPP_NAME)
4321             break;
4322           /* If the following token is neither a `<' (to begin a
4323              template-id), nor a `::', then we are not looking at a
4324              nested-name-specifier.  */
4325           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4326
4327           if (token->type == CPP_COLON
4328               && parser->colon_corrects_to_scope_p
4329               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4330             {
4331               error_at (token->location,
4332                         "found %<:%> in nested-name-specifier, expected %<::%>");
4333               token->type = CPP_SCOPE;
4334             }
4335
4336           if (token->type != CPP_SCOPE
4337               && !cp_parser_nth_token_starts_template_argument_list_p
4338                   (parser, 2))
4339             break;
4340         }
4341
4342       /* The nested-name-specifier is optional, so we parse
4343          tentatively.  */
4344       cp_parser_parse_tentatively (parser);
4345
4346       /* Look for the optional `template' keyword, if this isn't the
4347          first time through the loop.  */
4348       if (success)
4349         template_keyword_p = cp_parser_optional_template_keyword (parser);
4350       else
4351         template_keyword_p = false;
4352
4353       /* Save the old scope since the name lookup we are about to do
4354          might destroy it.  */
4355       old_scope = parser->scope;
4356       saved_qualifying_scope = parser->qualifying_scope;
4357       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4358          look up names in "X<T>::I" in order to determine that "Y" is
4359          a template.  So, if we have a typename at this point, we make
4360          an effort to look through it.  */
4361       if (is_declaration
4362           && !typename_keyword_p
4363           && parser->scope
4364           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4365         parser->scope = resolve_typename_type (parser->scope,
4366                                                /*only_current_p=*/false);
4367       /* Parse the qualifying entity.  */
4368       new_scope
4369         = cp_parser_qualifying_entity (parser,
4370                                        typename_keyword_p,
4371                                        template_keyword_p,
4372                                        check_dependency_p,
4373                                        type_p,
4374                                        is_declaration);
4375       /* Look for the `::' token.  */
4376       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4377
4378       /* If we found what we wanted, we keep going; otherwise, we're
4379          done.  */
4380       if (!cp_parser_parse_definitely (parser))
4381         {
4382           bool error_p = false;
4383
4384           /* Restore the OLD_SCOPE since it was valid before the
4385              failed attempt at finding the last
4386              class-or-namespace-name.  */
4387           parser->scope = old_scope;
4388           parser->qualifying_scope = saved_qualifying_scope;
4389           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4390             break;
4391           /* If the next token is an identifier, and the one after
4392              that is a `::', then any valid interpretation would have
4393              found a class-or-namespace-name.  */
4394           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4395                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4396                      == CPP_SCOPE)
4397                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4398                      != CPP_COMPL))
4399             {
4400               token = cp_lexer_consume_token (parser->lexer);
4401               if (!error_p)
4402                 {
4403                   if (!token->ambiguous_p)
4404                     {
4405                       tree decl;
4406                       tree ambiguous_decls;
4407
4408                       decl = cp_parser_lookup_name (parser, token->u.value,
4409                                                     none_type,
4410                                                     /*is_template=*/false,
4411                                                     /*is_namespace=*/false,
4412                                                     /*check_dependency=*/true,
4413                                                     &ambiguous_decls,
4414                                                     token->location);
4415                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4416                         error_at (token->location,
4417                                   "%qD used without template parameters",
4418                                   decl);
4419                       else if (ambiguous_decls)
4420                         {
4421                           error_at (token->location,
4422                                     "reference to %qD is ambiguous",
4423                                     token->u.value);
4424                           print_candidates (ambiguous_decls);
4425                           decl = error_mark_node;
4426                         }
4427                       else
4428                         {
4429                           if (cxx_dialect != cxx98)
4430                             cp_parser_name_lookup_error
4431                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4432                              token->location);
4433                           else
4434                             cp_parser_name_lookup_error
4435                             (parser, token->u.value, decl, NLE_CXX98,
4436                              token->location);
4437                         }
4438                     }
4439                   parser->scope = error_mark_node;
4440                   error_p = true;
4441                   /* Treat this as a successful nested-name-specifier
4442                      due to:
4443
4444                      [basic.lookup.qual]
4445
4446                      If the name found is not a class-name (clause
4447                      _class_) or namespace-name (_namespace.def_), the
4448                      program is ill-formed.  */
4449                   success = true;
4450                 }
4451               cp_lexer_consume_token (parser->lexer);
4452             }
4453           break;
4454         }
4455       /* We've found one valid nested-name-specifier.  */
4456       success = true;
4457       /* Name lookup always gives us a DECL.  */
4458       if (TREE_CODE (new_scope) == TYPE_DECL)
4459         new_scope = TREE_TYPE (new_scope);
4460       /* Uses of "template" must be followed by actual templates.  */
4461       if (template_keyword_p
4462           && !(CLASS_TYPE_P (new_scope)
4463                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4464                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4465                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4466           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4467                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4468                    == TEMPLATE_ID_EXPR)))
4469         permerror (input_location, TYPE_P (new_scope)
4470                    ? "%qT is not a template"
4471                    : "%qD is not a template",
4472                    new_scope);
4473       /* If it is a class scope, try to complete it; we are about to
4474          be looking up names inside the class.  */
4475       if (TYPE_P (new_scope)
4476           /* Since checking types for dependency can be expensive,
4477              avoid doing it if the type is already complete.  */
4478           && !COMPLETE_TYPE_P (new_scope)
4479           /* Do not try to complete dependent types.  */
4480           && !dependent_type_p (new_scope))
4481         {
4482           new_scope = complete_type (new_scope);
4483           /* If it is a typedef to current class, use the current
4484              class instead, as the typedef won't have any names inside
4485              it yet.  */
4486           if (!COMPLETE_TYPE_P (new_scope)
4487               && currently_open_class (new_scope))
4488             new_scope = TYPE_MAIN_VARIANT (new_scope);
4489         }
4490       /* Make sure we look in the right scope the next time through
4491          the loop.  */
4492       parser->scope = new_scope;
4493     }
4494
4495   /* If parsing tentatively, replace the sequence of tokens that makes
4496      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4497      token.  That way, should we re-parse the token stream, we will
4498      not have to repeat the effort required to do the parse, nor will
4499      we issue duplicate error messages.  */
4500   if (success && start)
4501     {
4502       cp_token *token;
4503
4504       token = cp_lexer_token_at (parser->lexer, start);
4505       /* Reset the contents of the START token.  */
4506       token->type = CPP_NESTED_NAME_SPECIFIER;
4507       /* Retrieve any deferred checks.  Do not pop this access checks yet
4508          so the memory will not be reclaimed during token replacing below.  */
4509       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4510       token->u.tree_check_value->value = parser->scope;
4511       token->u.tree_check_value->checks = get_deferred_access_checks ();
4512       token->u.tree_check_value->qualifying_scope =
4513         parser->qualifying_scope;
4514       token->keyword = RID_MAX;
4515
4516       /* Purge all subsequent tokens.  */
4517       cp_lexer_purge_tokens_after (parser->lexer, start);
4518     }
4519
4520   if (start)
4521     pop_to_parent_deferring_access_checks ();
4522
4523   return success ? parser->scope : NULL_TREE;
4524 }
4525
4526 /* Parse a nested-name-specifier.  See
4527    cp_parser_nested_name_specifier_opt for details.  This function
4528    behaves identically, except that it will an issue an error if no
4529    nested-name-specifier is present.  */
4530
4531 static tree
4532 cp_parser_nested_name_specifier (cp_parser *parser,
4533                                  bool typename_keyword_p,
4534                                  bool check_dependency_p,
4535                                  bool type_p,
4536                                  bool is_declaration)
4537 {
4538   tree scope;
4539
4540   /* Look for the nested-name-specifier.  */
4541   scope = cp_parser_nested_name_specifier_opt (parser,
4542                                                typename_keyword_p,
4543                                                check_dependency_p,
4544                                                type_p,
4545                                                is_declaration);
4546   /* If it was not present, issue an error message.  */
4547   if (!scope)
4548     {
4549       cp_parser_error (parser, "expected nested-name-specifier");
4550       parser->scope = NULL_TREE;
4551     }
4552
4553   return scope;
4554 }
4555
4556 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4557    this is either a class-name or a namespace-name (which corresponds
4558    to the class-or-namespace-name production in the grammar). For
4559    C++0x, it can also be a type-name that refers to an enumeration
4560    type.
4561
4562    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4563    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4564    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4565    TYPE_P is TRUE iff the next name should be taken as a class-name,
4566    even the same name is declared to be another entity in the same
4567    scope.
4568
4569    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4570    specified by the class-or-namespace-name.  If neither is found the
4571    ERROR_MARK_NODE is returned.  */
4572
4573 static tree
4574 cp_parser_qualifying_entity (cp_parser *parser,
4575                              bool typename_keyword_p,
4576                              bool template_keyword_p,
4577                              bool check_dependency_p,
4578                              bool type_p,
4579                              bool is_declaration)
4580 {
4581   tree saved_scope;
4582   tree saved_qualifying_scope;
4583   tree saved_object_scope;
4584   tree scope;
4585   bool only_class_p;
4586   bool successful_parse_p;
4587
4588   /* Before we try to parse the class-name, we must save away the
4589      current PARSER->SCOPE since cp_parser_class_name will destroy
4590      it.  */
4591   saved_scope = parser->scope;
4592   saved_qualifying_scope = parser->qualifying_scope;
4593   saved_object_scope = parser->object_scope;
4594   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4595      there is no need to look for a namespace-name.  */
4596   only_class_p = template_keyword_p 
4597     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4598   if (!only_class_p)
4599     cp_parser_parse_tentatively (parser);
4600   scope = cp_parser_class_name (parser,
4601                                 typename_keyword_p,
4602                                 template_keyword_p,
4603                                 type_p ? class_type : none_type,
4604                                 check_dependency_p,
4605                                 /*class_head_p=*/false,
4606                                 is_declaration);
4607   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4608   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4609   if (!only_class_p 
4610       && cxx_dialect != cxx98
4611       && !successful_parse_p)
4612     {
4613       /* Restore the saved scope.  */
4614       parser->scope = saved_scope;
4615       parser->qualifying_scope = saved_qualifying_scope;
4616       parser->object_scope = saved_object_scope;
4617
4618       /* Parse tentatively.  */
4619       cp_parser_parse_tentatively (parser);
4620      
4621       /* Parse a typedef-name or enum-name.  */
4622       scope = cp_parser_nonclass_name (parser);
4623
4624       /* "If the name found does not designate a namespace or a class,
4625          enumeration, or dependent type, the program is ill-formed."
4626
4627          We cover classes and dependent types above and namespaces below,
4628          so this code is only looking for enums.  */
4629       if (!scope || TREE_CODE (scope) != TYPE_DECL
4630           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4631         cp_parser_simulate_error (parser);
4632
4633       successful_parse_p = cp_parser_parse_definitely (parser);
4634     }
4635   /* If that didn't work, try for a namespace-name.  */
4636   if (!only_class_p && !successful_parse_p)
4637     {
4638       /* Restore the saved scope.  */
4639       parser->scope = saved_scope;
4640       parser->qualifying_scope = saved_qualifying_scope;
4641       parser->object_scope = saved_object_scope;
4642       /* If we are not looking at an identifier followed by the scope
4643          resolution operator, then this is not part of a
4644          nested-name-specifier.  (Note that this function is only used
4645          to parse the components of a nested-name-specifier.)  */
4646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4647           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4648         return error_mark_node;
4649       scope = cp_parser_namespace_name (parser);
4650     }
4651
4652   return scope;
4653 }
4654
4655 /* Parse a postfix-expression.
4656
4657    postfix-expression:
4658      primary-expression
4659      postfix-expression [ expression ]
4660      postfix-expression ( expression-list [opt] )
4661      simple-type-specifier ( expression-list [opt] )
4662      typename :: [opt] nested-name-specifier identifier
4663        ( expression-list [opt] )
4664      typename :: [opt] nested-name-specifier template [opt] template-id
4665        ( expression-list [opt] )
4666      postfix-expression . template [opt] id-expression
4667      postfix-expression -> template [opt] id-expression
4668      postfix-expression . pseudo-destructor-name
4669      postfix-expression -> pseudo-destructor-name
4670      postfix-expression ++
4671      postfix-expression --
4672      dynamic_cast < type-id > ( expression )
4673      static_cast < type-id > ( expression )
4674      reinterpret_cast < type-id > ( expression )
4675      const_cast < type-id > ( expression )
4676      typeid ( expression )
4677      typeid ( type-id )
4678
4679    GNU Extension:
4680
4681    postfix-expression:
4682      ( type-id ) { initializer-list , [opt] }
4683
4684    This extension is a GNU version of the C99 compound-literal
4685    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4686    but they are essentially the same concept.)
4687
4688    If ADDRESS_P is true, the postfix expression is the operand of the
4689    `&' operator.  CAST_P is true if this expression is the target of a
4690    cast.
4691
4692    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4693    class member access expressions [expr.ref].
4694
4695    Returns a representation of the expression.  */
4696
4697 static tree
4698 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4699                               bool member_access_only_p,
4700                               cp_id_kind * pidk_return)
4701 {
4702   cp_token *token;
4703   enum rid keyword;
4704   cp_id_kind idk = CP_ID_KIND_NONE;
4705   tree postfix_expression = NULL_TREE;
4706   bool is_member_access = false;
4707
4708   /* Peek at the next token.  */
4709   token = cp_lexer_peek_token (parser->lexer);
4710   /* Some of the productions are determined by keywords.  */
4711   keyword = token->keyword;
4712   switch (keyword)
4713     {
4714     case RID_DYNCAST:
4715     case RID_STATCAST:
4716     case RID_REINTCAST:
4717     case RID_CONSTCAST:
4718       {
4719         tree type;
4720         tree expression;
4721         const char *saved_message;
4722
4723         /* All of these can be handled in the same way from the point
4724            of view of parsing.  Begin by consuming the token
4725            identifying the cast.  */
4726         cp_lexer_consume_token (parser->lexer);
4727
4728         /* New types cannot be defined in the cast.  */
4729         saved_message = parser->type_definition_forbidden_message;
4730         parser->type_definition_forbidden_message
4731           = G_("types may not be defined in casts");
4732
4733         /* Look for the opening `<'.  */
4734         cp_parser_require (parser, CPP_LESS, RT_LESS);
4735         /* Parse the type to which we are casting.  */
4736         type = cp_parser_type_id (parser);
4737         /* Look for the closing `>'.  */
4738         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4739         /* Restore the old message.  */
4740         parser->type_definition_forbidden_message = saved_message;
4741
4742         /* And the expression which is being cast.  */
4743         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4744         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4745         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4746
4747         /* Only type conversions to integral or enumeration types
4748            can be used in constant-expressions.  */
4749         if (!cast_valid_in_integral_constant_expression_p (type)
4750             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4751           return error_mark_node;
4752
4753         switch (keyword)
4754           {
4755           case RID_DYNCAST:
4756             postfix_expression
4757               = build_dynamic_cast (type, expression, tf_warning_or_error);
4758             break;
4759           case RID_STATCAST:
4760             postfix_expression
4761               = build_static_cast (type, expression, tf_warning_or_error);
4762             break;
4763           case RID_REINTCAST:
4764             postfix_expression
4765               = build_reinterpret_cast (type, expression, 
4766                                         tf_warning_or_error);
4767             break;
4768           case RID_CONSTCAST:
4769             postfix_expression
4770               = build_const_cast (type, expression, tf_warning_or_error);
4771             break;
4772           default:
4773             gcc_unreachable ();
4774           }
4775       }
4776       break;
4777
4778     case RID_TYPEID:
4779       {
4780         tree type;
4781         const char *saved_message;
4782         bool saved_in_type_id_in_expr_p;
4783
4784         /* Consume the `typeid' token.  */
4785         cp_lexer_consume_token (parser->lexer);
4786         /* Look for the `(' token.  */
4787         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4788         /* Types cannot be defined in a `typeid' expression.  */
4789         saved_message = parser->type_definition_forbidden_message;
4790         parser->type_definition_forbidden_message
4791           = G_("types may not be defined in a %<typeid%> expression");
4792         /* We can't be sure yet whether we're looking at a type-id or an
4793            expression.  */
4794         cp_parser_parse_tentatively (parser);
4795         /* Try a type-id first.  */
4796         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4797         parser->in_type_id_in_expr_p = true;
4798         type = cp_parser_type_id (parser);
4799         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4800         /* Look for the `)' token.  Otherwise, we can't be sure that
4801            we're not looking at an expression: consider `typeid (int
4802            (3))', for example.  */
4803         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4804         /* If all went well, simply lookup the type-id.  */
4805         if (cp_parser_parse_definitely (parser))
4806           postfix_expression = get_typeid (type);
4807         /* Otherwise, fall back to the expression variant.  */
4808         else
4809           {
4810             tree expression;
4811
4812             /* Look for an expression.  */
4813             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4814             /* Compute its typeid.  */
4815             postfix_expression = build_typeid (expression);
4816             /* Look for the `)' token.  */
4817             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4818           }
4819         /* Restore the saved message.  */
4820         parser->type_definition_forbidden_message = saved_message;
4821         /* `typeid' may not appear in an integral constant expression.  */
4822         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4823           return error_mark_node;
4824       }
4825       break;
4826
4827     case RID_TYPENAME:
4828       {
4829         tree type;
4830         /* The syntax permitted here is the same permitted for an
4831            elaborated-type-specifier.  */
4832         type = cp_parser_elaborated_type_specifier (parser,
4833                                                     /*is_friend=*/false,
4834                                                     /*is_declaration=*/false);
4835         postfix_expression = cp_parser_functional_cast (parser, type);
4836       }
4837       break;
4838
4839     default:
4840       {
4841         tree type;
4842
4843         /* If the next thing is a simple-type-specifier, we may be
4844            looking at a functional cast.  We could also be looking at
4845            an id-expression.  So, we try the functional cast, and if
4846            that doesn't work we fall back to the primary-expression.  */
4847         cp_parser_parse_tentatively (parser);
4848         /* Look for the simple-type-specifier.  */
4849         type = cp_parser_simple_type_specifier (parser,
4850                                                 /*decl_specs=*/NULL,
4851                                                 CP_PARSER_FLAGS_NONE);
4852         /* Parse the cast itself.  */
4853         if (!cp_parser_error_occurred (parser))
4854           postfix_expression
4855             = cp_parser_functional_cast (parser, type);
4856         /* If that worked, we're done.  */
4857         if (cp_parser_parse_definitely (parser))
4858           break;
4859
4860         /* If the functional-cast didn't work out, try a
4861            compound-literal.  */
4862         if (cp_parser_allow_gnu_extensions_p (parser)
4863             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4864           {
4865             VEC(constructor_elt,gc) *initializer_list = NULL;
4866             bool saved_in_type_id_in_expr_p;
4867
4868             cp_parser_parse_tentatively (parser);
4869             /* Consume the `('.  */
4870             cp_lexer_consume_token (parser->lexer);
4871             /* Parse the type.  */
4872             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4873             parser->in_type_id_in_expr_p = true;
4874             type = cp_parser_type_id (parser);
4875             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4876             /* Look for the `)'.  */
4877             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4878             /* Look for the `{'.  */
4879             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4880             /* If things aren't going well, there's no need to
4881                keep going.  */
4882             if (!cp_parser_error_occurred (parser))
4883               {
4884                 bool non_constant_p;
4885                 /* Parse the initializer-list.  */
4886                 initializer_list
4887                   = cp_parser_initializer_list (parser, &non_constant_p);
4888                 /* Allow a trailing `,'.  */
4889                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4890                   cp_lexer_consume_token (parser->lexer);
4891                 /* Look for the final `}'.  */
4892                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4893               }
4894             /* If that worked, we're definitely looking at a
4895                compound-literal expression.  */
4896             if (cp_parser_parse_definitely (parser))
4897               {
4898                 /* Warn the user that a compound literal is not
4899                    allowed in standard C++.  */
4900                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4901                 /* For simplicity, we disallow compound literals in
4902                    constant-expressions.  We could
4903                    allow compound literals of integer type, whose
4904                    initializer was a constant, in constant
4905                    expressions.  Permitting that usage, as a further
4906                    extension, would not change the meaning of any
4907                    currently accepted programs.  (Of course, as
4908                    compound literals are not part of ISO C++, the
4909                    standard has nothing to say.)  */
4910                 if (cp_parser_non_integral_constant_expression (parser,
4911                                                                 NIC_NCC))
4912                   {
4913                     postfix_expression = error_mark_node;
4914                     break;
4915                   }
4916                 /* Form the representation of the compound-literal.  */
4917                 postfix_expression
4918                   = (finish_compound_literal
4919                      (type, build_constructor (init_list_type_node,
4920                                                initializer_list),
4921                       tf_warning_or_error));
4922                 break;
4923               }
4924           }
4925
4926         /* It must be a primary-expression.  */
4927         postfix_expression
4928           = cp_parser_primary_expression (parser, address_p, cast_p,
4929                                           /*template_arg_p=*/false,
4930                                           &idk);
4931       }
4932       break;
4933     }
4934
4935   /* Keep looping until the postfix-expression is complete.  */
4936   while (true)
4937     {
4938       if (idk == CP_ID_KIND_UNQUALIFIED
4939           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4940           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4941         /* It is not a Koenig lookup function call.  */
4942         postfix_expression
4943           = unqualified_name_lookup_error (postfix_expression);
4944
4945       /* Peek at the next token.  */
4946       token = cp_lexer_peek_token (parser->lexer);
4947
4948       switch (token->type)
4949         {
4950         case CPP_OPEN_SQUARE:
4951           postfix_expression
4952             = cp_parser_postfix_open_square_expression (parser,
4953                                                         postfix_expression,
4954                                                         false);
4955           idk = CP_ID_KIND_NONE;
4956           is_member_access = false;
4957           break;
4958
4959         case CPP_OPEN_PAREN:
4960           /* postfix-expression ( expression-list [opt] ) */
4961           {
4962             bool koenig_p;
4963             bool is_builtin_constant_p;
4964             bool saved_integral_constant_expression_p = false;
4965             bool saved_non_integral_constant_expression_p = false;
4966             VEC(tree,gc) *args;
4967
4968             is_member_access = false;
4969
4970             is_builtin_constant_p
4971               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4972             if (is_builtin_constant_p)
4973               {
4974                 /* The whole point of __builtin_constant_p is to allow
4975                    non-constant expressions to appear as arguments.  */
4976                 saved_integral_constant_expression_p
4977                   = parser->integral_constant_expression_p;
4978                 saved_non_integral_constant_expression_p
4979                   = parser->non_integral_constant_expression_p;
4980                 parser->integral_constant_expression_p = false;
4981               }
4982             args = (cp_parser_parenthesized_expression_list
4983                     (parser, non_attr,
4984                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4985                      /*non_constant_p=*/NULL));
4986             if (is_builtin_constant_p)
4987               {
4988                 parser->integral_constant_expression_p
4989                   = saved_integral_constant_expression_p;
4990                 parser->non_integral_constant_expression_p
4991                   = saved_non_integral_constant_expression_p;
4992               }
4993
4994             if (args == NULL)
4995               {
4996                 postfix_expression = error_mark_node;
4997                 break;
4998               }
4999
5000             /* Function calls are not permitted in
5001                constant-expressions.  */
5002             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5003                 && cp_parser_non_integral_constant_expression (parser,
5004                                                                NIC_FUNC_CALL))
5005               {
5006                 postfix_expression = error_mark_node;
5007                 release_tree_vector (args);
5008                 break;
5009               }
5010
5011             koenig_p = false;
5012             if (idk == CP_ID_KIND_UNQUALIFIED
5013                 || idk == CP_ID_KIND_TEMPLATE_ID)
5014               {
5015                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5016                   {
5017                     if (!VEC_empty (tree, args))
5018                       {
5019                         koenig_p = true;
5020                         if (!any_type_dependent_arguments_p (args))
5021                           postfix_expression
5022                             = perform_koenig_lookup (postfix_expression, args,
5023                                                      /*include_std=*/false,
5024                                                      tf_warning_or_error);
5025                       }
5026                     else
5027                       postfix_expression
5028                         = unqualified_fn_lookup_error (postfix_expression);
5029                   }
5030                 /* We do not perform argument-dependent lookup if
5031                    normal lookup finds a non-function, in accordance
5032                    with the expected resolution of DR 218.  */
5033                 else if (!VEC_empty (tree, args)
5034                          && is_overloaded_fn (postfix_expression))
5035                   {
5036                     tree fn = get_first_fn (postfix_expression);
5037                     fn = STRIP_TEMPLATE (fn);
5038
5039                     /* Do not do argument dependent lookup if regular
5040                        lookup finds a member function or a block-scope
5041                        function declaration.  [basic.lookup.argdep]/3  */
5042                     if (!DECL_FUNCTION_MEMBER_P (fn)
5043                         && !DECL_LOCAL_FUNCTION_P (fn))
5044                       {
5045                         koenig_p = true;
5046                         if (!any_type_dependent_arguments_p (args))
5047                           postfix_expression
5048                             = perform_koenig_lookup (postfix_expression, args,
5049                                                      /*include_std=*/false,
5050                                                      tf_warning_or_error);
5051                       }
5052                   }
5053               }
5054
5055             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5056               {
5057                 tree instance = TREE_OPERAND (postfix_expression, 0);
5058                 tree fn = TREE_OPERAND (postfix_expression, 1);
5059
5060                 if (processing_template_decl
5061                     && (type_dependent_expression_p (instance)
5062                         || (!BASELINK_P (fn)
5063                             && TREE_CODE (fn) != FIELD_DECL)
5064                         || type_dependent_expression_p (fn)
5065                         || any_type_dependent_arguments_p (args)))
5066                   {
5067                     postfix_expression
5068                       = build_nt_call_vec (postfix_expression, args);
5069                     release_tree_vector (args);
5070                     break;
5071                   }
5072
5073                 if (BASELINK_P (fn))
5074                   {
5075                   postfix_expression
5076                     = (build_new_method_call
5077                        (instance, fn, &args, NULL_TREE,
5078                         (idk == CP_ID_KIND_QUALIFIED
5079                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5080                          : LOOKUP_NORMAL),
5081                         /*fn_p=*/NULL,
5082                         tf_warning_or_error));
5083                   }
5084                 else
5085                   postfix_expression
5086                     = finish_call_expr (postfix_expression, &args,
5087                                         /*disallow_virtual=*/false,
5088                                         /*koenig_p=*/false,
5089                                         tf_warning_or_error);
5090               }
5091             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5092                      || TREE_CODE (postfix_expression) == MEMBER_REF
5093                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5094               postfix_expression = (build_offset_ref_call_from_tree
5095                                     (postfix_expression, &args));
5096             else if (idk == CP_ID_KIND_QUALIFIED)
5097               /* A call to a static class member, or a namespace-scope
5098                  function.  */
5099               postfix_expression
5100                 = finish_call_expr (postfix_expression, &args,
5101                                     /*disallow_virtual=*/true,
5102                                     koenig_p,
5103                                     tf_warning_or_error);
5104             else
5105               /* All other function calls.  */
5106               postfix_expression
5107                 = finish_call_expr (postfix_expression, &args,
5108                                     /*disallow_virtual=*/false,
5109                                     koenig_p,
5110                                     tf_warning_or_error);
5111
5112             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5113             idk = CP_ID_KIND_NONE;
5114
5115             release_tree_vector (args);
5116           }
5117           break;
5118
5119         case CPP_DOT:
5120         case CPP_DEREF:
5121           /* postfix-expression . template [opt] id-expression
5122              postfix-expression . pseudo-destructor-name
5123              postfix-expression -> template [opt] id-expression
5124              postfix-expression -> pseudo-destructor-name */
5125
5126           /* Consume the `.' or `->' operator.  */
5127           cp_lexer_consume_token (parser->lexer);
5128
5129           postfix_expression
5130             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5131                                                       postfix_expression,
5132                                                       false, &idk,
5133                                                       token->location);
5134
5135           is_member_access = true;
5136           break;
5137
5138         case CPP_PLUS_PLUS:
5139           /* postfix-expression ++  */
5140           /* Consume the `++' token.  */
5141           cp_lexer_consume_token (parser->lexer);
5142           /* Generate a representation for the complete expression.  */
5143           postfix_expression
5144             = finish_increment_expr (postfix_expression,
5145                                      POSTINCREMENT_EXPR);
5146           /* Increments may not appear in constant-expressions.  */
5147           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5148             postfix_expression = error_mark_node;
5149           idk = CP_ID_KIND_NONE;
5150           is_member_access = false;
5151           break;
5152
5153         case CPP_MINUS_MINUS:
5154           /* postfix-expression -- */
5155           /* Consume the `--' token.  */
5156           cp_lexer_consume_token (parser->lexer);
5157           /* Generate a representation for the complete expression.  */
5158           postfix_expression
5159             = finish_increment_expr (postfix_expression,
5160                                      POSTDECREMENT_EXPR);
5161           /* Decrements may not appear in constant-expressions.  */
5162           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5163             postfix_expression = error_mark_node;
5164           idk = CP_ID_KIND_NONE;
5165           is_member_access = false;
5166           break;
5167
5168         default:
5169           if (pidk_return != NULL)
5170             * pidk_return = idk;
5171           if (member_access_only_p)
5172             return is_member_access? postfix_expression : error_mark_node;
5173           else
5174             return postfix_expression;
5175         }
5176     }
5177
5178   /* We should never get here.  */
5179   gcc_unreachable ();
5180   return error_mark_node;
5181 }
5182
5183 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5184    by cp_parser_builtin_offsetof.  We're looking for
5185
5186      postfix-expression [ expression ]
5187
5188    FOR_OFFSETOF is set if we're being called in that context, which
5189    changes how we deal with integer constant expressions.  */
5190
5191 static tree
5192 cp_parser_postfix_open_square_expression (cp_parser *parser,
5193                                           tree postfix_expression,
5194                                           bool for_offsetof)
5195 {
5196   tree index;
5197
5198   /* Consume the `[' token.  */
5199   cp_lexer_consume_token (parser->lexer);
5200
5201   /* Parse the index expression.  */
5202   /* ??? For offsetof, there is a question of what to allow here.  If
5203      offsetof is not being used in an integral constant expression context,
5204      then we *could* get the right answer by computing the value at runtime.
5205      If we are in an integral constant expression context, then we might
5206      could accept any constant expression; hard to say without analysis.
5207      Rather than open the barn door too wide right away, allow only integer
5208      constant expressions here.  */
5209   if (for_offsetof)
5210     index = cp_parser_constant_expression (parser, false, NULL);
5211   else
5212     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5213
5214   /* Look for the closing `]'.  */
5215   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5216
5217   /* Build the ARRAY_REF.  */
5218   postfix_expression = grok_array_decl (postfix_expression, index);
5219
5220   /* When not doing offsetof, array references are not permitted in
5221      constant-expressions.  */
5222   if (!for_offsetof
5223       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5224     postfix_expression = error_mark_node;
5225
5226   return postfix_expression;
5227 }
5228
5229 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5230    by cp_parser_builtin_offsetof.  We're looking for
5231
5232      postfix-expression . template [opt] id-expression
5233      postfix-expression . pseudo-destructor-name
5234      postfix-expression -> template [opt] id-expression
5235      postfix-expression -> pseudo-destructor-name
5236
5237    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5238    limits what of the above we'll actually accept, but nevermind.
5239    TOKEN_TYPE is the "." or "->" token, which will already have been
5240    removed from the stream.  */
5241
5242 static tree
5243 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5244                                         enum cpp_ttype token_type,
5245                                         tree postfix_expression,
5246                                         bool for_offsetof, cp_id_kind *idk,
5247                                         location_t location)
5248 {
5249   tree name;
5250   bool dependent_p;
5251   bool pseudo_destructor_p;
5252   tree scope = NULL_TREE;
5253
5254   /* If this is a `->' operator, dereference the pointer.  */
5255   if (token_type == CPP_DEREF)
5256     postfix_expression = build_x_arrow (postfix_expression);
5257   /* Check to see whether or not the expression is type-dependent.  */
5258   dependent_p = type_dependent_expression_p (postfix_expression);
5259   /* The identifier following the `->' or `.' is not qualified.  */
5260   parser->scope = NULL_TREE;
5261   parser->qualifying_scope = NULL_TREE;
5262   parser->object_scope = NULL_TREE;
5263   *idk = CP_ID_KIND_NONE;
5264
5265   /* Enter the scope corresponding to the type of the object
5266      given by the POSTFIX_EXPRESSION.  */
5267   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5268     {
5269       scope = TREE_TYPE (postfix_expression);
5270       /* According to the standard, no expression should ever have
5271          reference type.  Unfortunately, we do not currently match
5272          the standard in this respect in that our internal representation
5273          of an expression may have reference type even when the standard
5274          says it does not.  Therefore, we have to manually obtain the
5275          underlying type here.  */
5276       scope = non_reference (scope);
5277       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5278       if (scope == unknown_type_node)
5279         {
5280           error_at (location, "%qE does not have class type",
5281                     postfix_expression);
5282           scope = NULL_TREE;
5283         }
5284       else
5285         scope = complete_type_or_else (scope, NULL_TREE);
5286       /* Let the name lookup machinery know that we are processing a
5287          class member access expression.  */
5288       parser->context->object_type = scope;
5289       /* If something went wrong, we want to be able to discern that case,
5290          as opposed to the case where there was no SCOPE due to the type
5291          of expression being dependent.  */
5292       if (!scope)
5293         scope = error_mark_node;
5294       /* If the SCOPE was erroneous, make the various semantic analysis
5295          functions exit quickly -- and without issuing additional error
5296          messages.  */
5297       if (scope == error_mark_node)
5298         postfix_expression = error_mark_node;
5299     }
5300
5301   /* Assume this expression is not a pseudo-destructor access.  */
5302   pseudo_destructor_p = false;
5303
5304   /* If the SCOPE is a scalar type, then, if this is a valid program,
5305      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5306      is type dependent, it can be pseudo-destructor-name or something else.
5307      Try to parse it as pseudo-destructor-name first.  */
5308   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5309     {
5310       tree s;
5311       tree type;
5312
5313       cp_parser_parse_tentatively (parser);
5314       /* Parse the pseudo-destructor-name.  */
5315       s = NULL_TREE;
5316       cp_parser_pseudo_destructor_name (parser, &s, &type);
5317       if (dependent_p
5318           && (cp_parser_error_occurred (parser)
5319               || TREE_CODE (type) != TYPE_DECL
5320               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5321         cp_parser_abort_tentative_parse (parser);
5322       else if (cp_parser_parse_definitely (parser))
5323         {
5324           pseudo_destructor_p = true;
5325           postfix_expression
5326             = finish_pseudo_destructor_expr (postfix_expression,
5327                                              s, TREE_TYPE (type));
5328         }
5329     }
5330
5331   if (!pseudo_destructor_p)
5332     {
5333       /* If the SCOPE is not a scalar type, we are looking at an
5334          ordinary class member access expression, rather than a
5335          pseudo-destructor-name.  */
5336       bool template_p;
5337       cp_token *token = cp_lexer_peek_token (parser->lexer);
5338       /* Parse the id-expression.  */
5339       name = (cp_parser_id_expression
5340               (parser,
5341                cp_parser_optional_template_keyword (parser),
5342                /*check_dependency_p=*/true,
5343                &template_p,
5344                /*declarator_p=*/false,
5345                /*optional_p=*/false));
5346       /* In general, build a SCOPE_REF if the member name is qualified.
5347          However, if the name was not dependent and has already been
5348          resolved; there is no need to build the SCOPE_REF.  For example;
5349
5350              struct X { void f(); };
5351              template <typename T> void f(T* t) { t->X::f(); }
5352
5353          Even though "t" is dependent, "X::f" is not and has been resolved
5354          to a BASELINK; there is no need to include scope information.  */
5355
5356       /* But we do need to remember that there was an explicit scope for
5357          virtual function calls.  */
5358       if (parser->scope)
5359         *idk = CP_ID_KIND_QUALIFIED;
5360
5361       /* If the name is a template-id that names a type, we will get a
5362          TYPE_DECL here.  That is invalid code.  */
5363       if (TREE_CODE (name) == TYPE_DECL)
5364         {
5365           error_at (token->location, "invalid use of %qD", name);
5366           postfix_expression = error_mark_node;
5367         }
5368       else
5369         {
5370           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5371             {
5372               name = build_qualified_name (/*type=*/NULL_TREE,
5373                                            parser->scope,
5374                                            name,
5375                                            template_p);
5376               parser->scope = NULL_TREE;
5377               parser->qualifying_scope = NULL_TREE;
5378               parser->object_scope = NULL_TREE;
5379             }
5380           if (scope && name && BASELINK_P (name))
5381             adjust_result_of_qualified_name_lookup
5382               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5383           postfix_expression
5384             = finish_class_member_access_expr (postfix_expression, name,
5385                                                template_p, 
5386                                                tf_warning_or_error);
5387         }
5388     }
5389
5390   /* We no longer need to look up names in the scope of the object on
5391      the left-hand side of the `.' or `->' operator.  */
5392   parser->context->object_type = NULL_TREE;
5393
5394   /* Outside of offsetof, these operators may not appear in
5395      constant-expressions.  */
5396   if (!for_offsetof
5397       && (cp_parser_non_integral_constant_expression
5398           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5399     postfix_expression = error_mark_node;
5400
5401   return postfix_expression;
5402 }
5403
5404 /* Parse a parenthesized expression-list.
5405
5406    expression-list:
5407      assignment-expression
5408      expression-list, assignment-expression
5409
5410    attribute-list:
5411      expression-list
5412      identifier
5413      identifier, expression-list
5414
5415    CAST_P is true if this expression is the target of a cast.
5416
5417    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5418    argument pack.
5419
5420    Returns a vector of trees.  Each element is a representation of an
5421    assignment-expression.  NULL is returned if the ( and or ) are
5422    missing.  An empty, but allocated, vector is returned on no
5423    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5424    if we are parsing an attribute list for an attribute that wants a
5425    plain identifier argument, normal_attr for an attribute that wants
5426    an expression, or non_attr if we aren't parsing an attribute list.  If
5427    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5428    not all of the expressions in the list were constant.  */
5429
5430 static VEC(tree,gc) *
5431 cp_parser_parenthesized_expression_list (cp_parser* parser,
5432                                          int is_attribute_list,
5433                                          bool cast_p,
5434                                          bool allow_expansion_p,
5435                                          bool *non_constant_p)
5436 {
5437   VEC(tree,gc) *expression_list;
5438   bool fold_expr_p = is_attribute_list != non_attr;
5439   tree identifier = NULL_TREE;
5440   bool saved_greater_than_is_operator_p;
5441
5442   /* Assume all the expressions will be constant.  */
5443   if (non_constant_p)
5444     *non_constant_p = false;
5445
5446   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5447     return NULL;
5448
5449   expression_list = make_tree_vector ();
5450
5451   /* Within a parenthesized expression, a `>' token is always
5452      the greater-than operator.  */
5453   saved_greater_than_is_operator_p
5454     = parser->greater_than_is_operator_p;
5455   parser->greater_than_is_operator_p = true;
5456
5457   /* Consume expressions until there are no more.  */
5458   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5459     while (true)
5460       {
5461         tree expr;
5462
5463         /* At the beginning of attribute lists, check to see if the
5464            next token is an identifier.  */
5465         if (is_attribute_list == id_attr
5466             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5467           {
5468             cp_token *token;
5469
5470             /* Consume the identifier.  */
5471             token = cp_lexer_consume_token (parser->lexer);
5472             /* Save the identifier.  */
5473             identifier = token->u.value;
5474           }
5475         else
5476           {
5477             bool expr_non_constant_p;
5478
5479             /* Parse the next assignment-expression.  */
5480             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5481               {
5482                 /* A braced-init-list.  */
5483                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5484                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5485                 if (non_constant_p && expr_non_constant_p)
5486                   *non_constant_p = true;
5487               }
5488             else if (non_constant_p)
5489               {
5490                 expr = (cp_parser_constant_expression
5491                         (parser, /*allow_non_constant_p=*/true,
5492                          &expr_non_constant_p));
5493                 if (expr_non_constant_p)
5494                   *non_constant_p = true;
5495               }
5496             else
5497               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5498
5499             if (fold_expr_p)
5500               expr = fold_non_dependent_expr (expr);
5501
5502             /* If we have an ellipsis, then this is an expression
5503                expansion.  */
5504             if (allow_expansion_p
5505                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5506               {
5507                 /* Consume the `...'.  */
5508                 cp_lexer_consume_token (parser->lexer);
5509
5510                 /* Build the argument pack.  */
5511                 expr = make_pack_expansion (expr);
5512               }
5513
5514              /* Add it to the list.  We add error_mark_node
5515                 expressions to the list, so that we can still tell if
5516                 the correct form for a parenthesized expression-list
5517                 is found. That gives better errors.  */
5518             VEC_safe_push (tree, gc, expression_list, expr);
5519
5520             if (expr == error_mark_node)
5521               goto skip_comma;
5522           }
5523
5524         /* After the first item, attribute lists look the same as
5525            expression lists.  */
5526         is_attribute_list = non_attr;
5527
5528       get_comma:;
5529         /* If the next token isn't a `,', then we are done.  */
5530         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5531           break;
5532
5533         /* Otherwise, consume the `,' and keep going.  */
5534         cp_lexer_consume_token (parser->lexer);
5535       }
5536
5537   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5538     {
5539       int ending;
5540
5541     skip_comma:;
5542       /* We try and resync to an unnested comma, as that will give the
5543          user better diagnostics.  */
5544       ending = cp_parser_skip_to_closing_parenthesis (parser,
5545                                                       /*recovering=*/true,
5546                                                       /*or_comma=*/true,
5547                                                       /*consume_paren=*/true);
5548       if (ending < 0)
5549         goto get_comma;
5550       if (!ending)
5551         {
5552           parser->greater_than_is_operator_p
5553             = saved_greater_than_is_operator_p;
5554           return NULL;
5555         }
5556     }
5557
5558   parser->greater_than_is_operator_p
5559     = saved_greater_than_is_operator_p;
5560
5561   if (identifier)
5562     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5563
5564   return expression_list;
5565 }
5566
5567 /* Parse a pseudo-destructor-name.
5568
5569    pseudo-destructor-name:
5570      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5571      :: [opt] nested-name-specifier template template-id :: ~ type-name
5572      :: [opt] nested-name-specifier [opt] ~ type-name
5573
5574    If either of the first two productions is used, sets *SCOPE to the
5575    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5576    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5577    or ERROR_MARK_NODE if the parse fails.  */
5578
5579 static void
5580 cp_parser_pseudo_destructor_name (cp_parser* parser,
5581                                   tree* scope,
5582                                   tree* type)
5583 {
5584   bool nested_name_specifier_p;
5585
5586   /* Assume that things will not work out.  */
5587   *type = error_mark_node;
5588
5589   /* Look for the optional `::' operator.  */
5590   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5591   /* Look for the optional nested-name-specifier.  */
5592   nested_name_specifier_p
5593     = (cp_parser_nested_name_specifier_opt (parser,
5594                                             /*typename_keyword_p=*/false,
5595                                             /*check_dependency_p=*/true,
5596                                             /*type_p=*/false,
5597                                             /*is_declaration=*/false)
5598        != NULL_TREE);
5599   /* Now, if we saw a nested-name-specifier, we might be doing the
5600      second production.  */
5601   if (nested_name_specifier_p
5602       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5603     {
5604       /* Consume the `template' keyword.  */
5605       cp_lexer_consume_token (parser->lexer);
5606       /* Parse the template-id.  */
5607       cp_parser_template_id (parser,
5608                              /*template_keyword_p=*/true,
5609                              /*check_dependency_p=*/false,
5610                              /*is_declaration=*/true);
5611       /* Look for the `::' token.  */
5612       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5613     }
5614   /* If the next token is not a `~', then there might be some
5615      additional qualification.  */
5616   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5617     {
5618       /* At this point, we're looking for "type-name :: ~".  The type-name
5619          must not be a class-name, since this is a pseudo-destructor.  So,
5620          it must be either an enum-name, or a typedef-name -- both of which
5621          are just identifiers.  So, we peek ahead to check that the "::"
5622          and "~" tokens are present; if they are not, then we can avoid
5623          calling type_name.  */
5624       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5625           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5626           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5627         {
5628           cp_parser_error (parser, "non-scalar type");
5629           return;
5630         }
5631
5632       /* Look for the type-name.  */
5633       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5634       if (*scope == error_mark_node)
5635         return;
5636
5637       /* Look for the `::' token.  */
5638       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5639     }
5640   else
5641     *scope = NULL_TREE;
5642
5643   /* Look for the `~'.  */
5644   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5645
5646   /* Once we see the ~, this has to be a pseudo-destructor.  */
5647   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5648     cp_parser_commit_to_tentative_parse (parser);
5649
5650   /* Look for the type-name again.  We are not responsible for
5651      checking that it matches the first type-name.  */
5652   *type = cp_parser_nonclass_name (parser);
5653 }
5654
5655 /* Parse a unary-expression.
5656
5657    unary-expression:
5658      postfix-expression
5659      ++ cast-expression
5660      -- cast-expression
5661      unary-operator cast-expression
5662      sizeof unary-expression
5663      sizeof ( type-id )
5664      alignof ( type-id )  [C++0x]
5665      new-expression
5666      delete-expression
5667
5668    GNU Extensions:
5669
5670    unary-expression:
5671      __extension__ cast-expression
5672      __alignof__ unary-expression
5673      __alignof__ ( type-id )
5674      alignof unary-expression  [C++0x]
5675      __real__ cast-expression
5676      __imag__ cast-expression
5677      && identifier
5678
5679    ADDRESS_P is true iff the unary-expression is appearing as the
5680    operand of the `&' operator.   CAST_P is true if this expression is
5681    the target of a cast.
5682
5683    Returns a representation of the expression.  */
5684
5685 static tree
5686 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5687                             cp_id_kind * pidk)
5688 {
5689   cp_token *token;
5690   enum tree_code unary_operator;
5691
5692   /* Peek at the next token.  */
5693   token = cp_lexer_peek_token (parser->lexer);
5694   /* Some keywords give away the kind of expression.  */
5695   if (token->type == CPP_KEYWORD)
5696     {
5697       enum rid keyword = token->keyword;
5698
5699       switch (keyword)
5700         {
5701         case RID_ALIGNOF:
5702         case RID_SIZEOF:
5703           {
5704             tree operand;
5705             enum tree_code op;
5706
5707             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5708             /* Consume the token.  */
5709             cp_lexer_consume_token (parser->lexer);
5710             /* Parse the operand.  */
5711             operand = cp_parser_sizeof_operand (parser, keyword);
5712
5713             if (TYPE_P (operand))
5714               return cxx_sizeof_or_alignof_type (operand, op, true);
5715             else
5716               {
5717                 /* ISO C++ defines alignof only with types, not with
5718                    expressions. So pedwarn if alignof is used with a non-
5719                    type expression. However, __alignof__ is ok.  */
5720                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5721                   pedwarn (token->location, OPT_pedantic,
5722                            "ISO C++ does not allow %<alignof%> "
5723                            "with a non-type");
5724
5725                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5726               }
5727           }
5728
5729         case RID_NEW:
5730           return cp_parser_new_expression (parser);
5731
5732         case RID_DELETE:
5733           return cp_parser_delete_expression (parser);
5734
5735         case RID_EXTENSION:
5736           {
5737             /* The saved value of the PEDANTIC flag.  */
5738             int saved_pedantic;
5739             tree expr;
5740
5741             /* Save away the PEDANTIC flag.  */
5742             cp_parser_extension_opt (parser, &saved_pedantic);
5743             /* Parse the cast-expression.  */
5744             expr = cp_parser_simple_cast_expression (parser);
5745             /* Restore the PEDANTIC flag.  */
5746             pedantic = saved_pedantic;
5747
5748             return expr;
5749           }
5750
5751         case RID_REALPART:
5752         case RID_IMAGPART:
5753           {
5754             tree expression;
5755
5756             /* Consume the `__real__' or `__imag__' token.  */
5757             cp_lexer_consume_token (parser->lexer);
5758             /* Parse the cast-expression.  */
5759             expression = cp_parser_simple_cast_expression (parser);
5760             /* Create the complete representation.  */
5761             return build_x_unary_op ((keyword == RID_REALPART
5762                                       ? REALPART_EXPR : IMAGPART_EXPR),
5763                                      expression,
5764                                      tf_warning_or_error);
5765           }
5766           break;
5767
5768         case RID_NOEXCEPT:
5769           {
5770             tree expr;
5771             const char *saved_message;
5772             bool saved_integral_constant_expression_p;
5773             bool saved_non_integral_constant_expression_p;
5774             bool saved_greater_than_is_operator_p;
5775
5776             cp_lexer_consume_token (parser->lexer);
5777             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5778
5779             saved_message = parser->type_definition_forbidden_message;
5780             parser->type_definition_forbidden_message
5781               = G_("types may not be defined in %<noexcept%> expressions");
5782
5783             saved_integral_constant_expression_p
5784               = parser->integral_constant_expression_p;
5785             saved_non_integral_constant_expression_p
5786               = parser->non_integral_constant_expression_p;
5787             parser->integral_constant_expression_p = false;
5788
5789             saved_greater_than_is_operator_p
5790               = parser->greater_than_is_operator_p;
5791             parser->greater_than_is_operator_p = true;
5792
5793             ++cp_unevaluated_operand;
5794             ++c_inhibit_evaluation_warnings;
5795             expr = cp_parser_expression (parser, false, NULL);
5796             --c_inhibit_evaluation_warnings;
5797             --cp_unevaluated_operand;
5798
5799             parser->greater_than_is_operator_p
5800               = saved_greater_than_is_operator_p;
5801
5802             parser->integral_constant_expression_p
5803               = saved_integral_constant_expression_p;
5804             parser->non_integral_constant_expression_p
5805               = saved_non_integral_constant_expression_p;
5806
5807             parser->type_definition_forbidden_message = saved_message;
5808
5809             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5810             return finish_noexcept_expr (expr, tf_warning_or_error);
5811           }
5812
5813         default:
5814           break;
5815         }
5816     }
5817
5818   /* Look for the `:: new' and `:: delete', which also signal the
5819      beginning of a new-expression, or delete-expression,
5820      respectively.  If the next token is `::', then it might be one of
5821      these.  */
5822   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5823     {
5824       enum rid keyword;
5825
5826       /* See if the token after the `::' is one of the keywords in
5827          which we're interested.  */
5828       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5829       /* If it's `new', we have a new-expression.  */
5830       if (keyword == RID_NEW)
5831         return cp_parser_new_expression (parser);
5832       /* Similarly, for `delete'.  */
5833       else if (keyword == RID_DELETE)
5834         return cp_parser_delete_expression (parser);
5835     }
5836
5837   /* Look for a unary operator.  */
5838   unary_operator = cp_parser_unary_operator (token);
5839   /* The `++' and `--' operators can be handled similarly, even though
5840      they are not technically unary-operators in the grammar.  */
5841   if (unary_operator == ERROR_MARK)
5842     {
5843       if (token->type == CPP_PLUS_PLUS)
5844         unary_operator = PREINCREMENT_EXPR;
5845       else if (token->type == CPP_MINUS_MINUS)
5846         unary_operator = PREDECREMENT_EXPR;
5847       /* Handle the GNU address-of-label extension.  */
5848       else if (cp_parser_allow_gnu_extensions_p (parser)
5849                && token->type == CPP_AND_AND)
5850         {
5851           tree identifier;
5852           tree expression;
5853           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5854
5855           /* Consume the '&&' token.  */
5856           cp_lexer_consume_token (parser->lexer);
5857           /* Look for the identifier.  */
5858           identifier = cp_parser_identifier (parser);
5859           /* Create an expression representing the address.  */
5860           expression = finish_label_address_expr (identifier, loc);
5861           if (cp_parser_non_integral_constant_expression (parser,
5862                                                           NIC_ADDR_LABEL))
5863             expression = error_mark_node;
5864           return expression;
5865         }
5866     }
5867   if (unary_operator != ERROR_MARK)
5868     {
5869       tree cast_expression;
5870       tree expression = error_mark_node;
5871       non_integral_constant non_constant_p = NIC_NONE;
5872
5873       /* Consume the operator token.  */
5874       token = cp_lexer_consume_token (parser->lexer);
5875       /* Parse the cast-expression.  */
5876       cast_expression
5877         = cp_parser_cast_expression (parser,
5878                                      unary_operator == ADDR_EXPR,
5879                                      /*cast_p=*/false, pidk);
5880       /* Now, build an appropriate representation.  */
5881       switch (unary_operator)
5882         {
5883         case INDIRECT_REF:
5884           non_constant_p = NIC_STAR;
5885           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5886                                              tf_warning_or_error);
5887           break;
5888
5889         case ADDR_EXPR:
5890            non_constant_p = NIC_ADDR;
5891           /* Fall through.  */
5892         case BIT_NOT_EXPR:
5893           expression = build_x_unary_op (unary_operator, cast_expression,
5894                                          tf_warning_or_error);
5895           break;
5896
5897         case PREINCREMENT_EXPR:
5898         case PREDECREMENT_EXPR:
5899           non_constant_p = unary_operator == PREINCREMENT_EXPR
5900                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5901           /* Fall through.  */
5902         case UNARY_PLUS_EXPR:
5903         case NEGATE_EXPR:
5904         case TRUTH_NOT_EXPR:
5905           expression = finish_unary_op_expr (unary_operator, cast_expression);
5906           break;
5907
5908         default:
5909           gcc_unreachable ();
5910         }
5911
5912       if (non_constant_p != NIC_NONE
5913           && cp_parser_non_integral_constant_expression (parser,
5914                                                          non_constant_p))
5915         expression = error_mark_node;
5916
5917       return expression;
5918     }
5919
5920   return cp_parser_postfix_expression (parser, address_p, cast_p,
5921                                        /*member_access_only_p=*/false,
5922                                        pidk);
5923 }
5924
5925 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5926    unary-operator, the corresponding tree code is returned.  */
5927
5928 static enum tree_code
5929 cp_parser_unary_operator (cp_token* token)
5930 {
5931   switch (token->type)
5932     {
5933     case CPP_MULT:
5934       return INDIRECT_REF;
5935
5936     case CPP_AND:
5937       return ADDR_EXPR;
5938
5939     case CPP_PLUS:
5940       return UNARY_PLUS_EXPR;
5941
5942     case CPP_MINUS:
5943       return NEGATE_EXPR;
5944
5945     case CPP_NOT:
5946       return TRUTH_NOT_EXPR;
5947
5948     case CPP_COMPL:
5949       return BIT_NOT_EXPR;
5950
5951     default:
5952       return ERROR_MARK;
5953     }
5954 }
5955
5956 /* Parse a new-expression.
5957
5958    new-expression:
5959      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5960      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5961
5962    Returns a representation of the expression.  */
5963
5964 static tree
5965 cp_parser_new_expression (cp_parser* parser)
5966 {
5967   bool global_scope_p;
5968   VEC(tree,gc) *placement;
5969   tree type;
5970   VEC(tree,gc) *initializer;
5971   tree nelts;
5972   tree ret;
5973
5974   /* Look for the optional `::' operator.  */
5975   global_scope_p
5976     = (cp_parser_global_scope_opt (parser,
5977                                    /*current_scope_valid_p=*/false)
5978        != NULL_TREE);
5979   /* Look for the `new' operator.  */
5980   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5981   /* There's no easy way to tell a new-placement from the
5982      `( type-id )' construct.  */
5983   cp_parser_parse_tentatively (parser);
5984   /* Look for a new-placement.  */
5985   placement = cp_parser_new_placement (parser);
5986   /* If that didn't work out, there's no new-placement.  */
5987   if (!cp_parser_parse_definitely (parser))
5988     {
5989       if (placement != NULL)
5990         release_tree_vector (placement);
5991       placement = NULL;
5992     }
5993
5994   /* If the next token is a `(', then we have a parenthesized
5995      type-id.  */
5996   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5997     {
5998       cp_token *token;
5999       /* Consume the `('.  */
6000       cp_lexer_consume_token (parser->lexer);
6001       /* Parse the type-id.  */
6002       type = cp_parser_type_id (parser);
6003       /* Look for the closing `)'.  */
6004       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6005       token = cp_lexer_peek_token (parser->lexer);
6006       /* There should not be a direct-new-declarator in this production,
6007          but GCC used to allowed this, so we check and emit a sensible error
6008          message for this case.  */
6009       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6010         {
6011           error_at (token->location,
6012                     "array bound forbidden after parenthesized type-id");
6013           inform (token->location, 
6014                   "try removing the parentheses around the type-id");
6015           cp_parser_direct_new_declarator (parser);
6016         }
6017       nelts = NULL_TREE;
6018     }
6019   /* Otherwise, there must be a new-type-id.  */
6020   else
6021     type = cp_parser_new_type_id (parser, &nelts);
6022
6023   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6024   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6025       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6026     initializer = cp_parser_new_initializer (parser);
6027   else
6028     initializer = NULL;
6029
6030   /* A new-expression may not appear in an integral constant
6031      expression.  */
6032   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6033     ret = error_mark_node;
6034   else
6035     {
6036       /* Create a representation of the new-expression.  */
6037       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6038                        tf_warning_or_error);
6039     }
6040
6041   if (placement != NULL)
6042     release_tree_vector (placement);
6043   if (initializer != NULL)
6044     release_tree_vector (initializer);
6045
6046   return ret;
6047 }
6048
6049 /* Parse a new-placement.
6050
6051    new-placement:
6052      ( expression-list )
6053
6054    Returns the same representation as for an expression-list.  */
6055
6056 static VEC(tree,gc) *
6057 cp_parser_new_placement (cp_parser* parser)
6058 {
6059   VEC(tree,gc) *expression_list;
6060
6061   /* Parse the expression-list.  */
6062   expression_list = (cp_parser_parenthesized_expression_list
6063                      (parser, non_attr, /*cast_p=*/false,
6064                       /*allow_expansion_p=*/true,
6065                       /*non_constant_p=*/NULL));
6066
6067   return expression_list;
6068 }
6069
6070 /* Parse a new-type-id.
6071
6072    new-type-id:
6073      type-specifier-seq new-declarator [opt]
6074
6075    Returns the TYPE allocated.  If the new-type-id indicates an array
6076    type, *NELTS is set to the number of elements in the last array
6077    bound; the TYPE will not include the last array bound.  */
6078
6079 static tree
6080 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6081 {
6082   cp_decl_specifier_seq type_specifier_seq;
6083   cp_declarator *new_declarator;
6084   cp_declarator *declarator;
6085   cp_declarator *outer_declarator;
6086   const char *saved_message;
6087   tree type;
6088
6089   /* The type-specifier sequence must not contain type definitions.
6090      (It cannot contain declarations of new types either, but if they
6091      are not definitions we will catch that because they are not
6092      complete.)  */
6093   saved_message = parser->type_definition_forbidden_message;
6094   parser->type_definition_forbidden_message
6095     = G_("types may not be defined in a new-type-id");
6096   /* Parse the type-specifier-seq.  */
6097   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6098                                 /*is_trailing_return=*/false,
6099                                 &type_specifier_seq);
6100   /* Restore the old message.  */
6101   parser->type_definition_forbidden_message = saved_message;
6102   /* Parse the new-declarator.  */
6103   new_declarator = cp_parser_new_declarator_opt (parser);
6104
6105   /* Determine the number of elements in the last array dimension, if
6106      any.  */
6107   *nelts = NULL_TREE;
6108   /* Skip down to the last array dimension.  */
6109   declarator = new_declarator;
6110   outer_declarator = NULL;
6111   while (declarator && (declarator->kind == cdk_pointer
6112                         || declarator->kind == cdk_ptrmem))
6113     {
6114       outer_declarator = declarator;
6115       declarator = declarator->declarator;
6116     }
6117   while (declarator
6118          && declarator->kind == cdk_array
6119          && declarator->declarator
6120          && declarator->declarator->kind == cdk_array)
6121     {
6122       outer_declarator = declarator;
6123       declarator = declarator->declarator;
6124     }
6125
6126   if (declarator && declarator->kind == cdk_array)
6127     {
6128       *nelts = declarator->u.array.bounds;
6129       if (*nelts == error_mark_node)
6130         *nelts = integer_one_node;
6131
6132       if (outer_declarator)
6133         outer_declarator->declarator = declarator->declarator;
6134       else
6135         new_declarator = NULL;
6136     }
6137
6138   type = groktypename (&type_specifier_seq, new_declarator, false);
6139   return type;
6140 }
6141
6142 /* Parse an (optional) new-declarator.
6143
6144    new-declarator:
6145      ptr-operator new-declarator [opt]
6146      direct-new-declarator
6147
6148    Returns the declarator.  */
6149
6150 static cp_declarator *
6151 cp_parser_new_declarator_opt (cp_parser* parser)
6152 {
6153   enum tree_code code;
6154   tree type;
6155   cp_cv_quals cv_quals;
6156
6157   /* We don't know if there's a ptr-operator next, or not.  */
6158   cp_parser_parse_tentatively (parser);
6159   /* Look for a ptr-operator.  */
6160   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6161   /* If that worked, look for more new-declarators.  */
6162   if (cp_parser_parse_definitely (parser))
6163     {
6164       cp_declarator *declarator;
6165
6166       /* Parse another optional declarator.  */
6167       declarator = cp_parser_new_declarator_opt (parser);
6168
6169       return cp_parser_make_indirect_declarator
6170         (code, type, cv_quals, declarator);
6171     }
6172
6173   /* If the next token is a `[', there is a direct-new-declarator.  */
6174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6175     return cp_parser_direct_new_declarator (parser);
6176
6177   return NULL;
6178 }
6179
6180 /* Parse a direct-new-declarator.
6181
6182    direct-new-declarator:
6183      [ expression ]
6184      direct-new-declarator [constant-expression]
6185
6186    */
6187
6188 static cp_declarator *
6189 cp_parser_direct_new_declarator (cp_parser* parser)
6190 {
6191   cp_declarator *declarator = NULL;
6192
6193   while (true)
6194     {
6195       tree expression;
6196
6197       /* Look for the opening `['.  */
6198       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6199       /* The first expression is not required to be constant.  */
6200       if (!declarator)
6201         {
6202           cp_token *token = cp_lexer_peek_token (parser->lexer);
6203           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6204           /* The standard requires that the expression have integral
6205              type.  DR 74 adds enumeration types.  We believe that the
6206              real intent is that these expressions be handled like the
6207              expression in a `switch' condition, which also allows
6208              classes with a single conversion to integral or
6209              enumeration type.  */
6210           if (!processing_template_decl)
6211             {
6212               expression
6213                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6214                                               expression,
6215                                               /*complain=*/true);
6216               if (!expression)
6217                 {
6218                   error_at (token->location,
6219                             "expression in new-declarator must have integral "
6220                             "or enumeration type");
6221                   expression = error_mark_node;
6222                 }
6223             }
6224         }
6225       /* But all the other expressions must be.  */
6226       else
6227         expression
6228           = cp_parser_constant_expression (parser,
6229                                            /*allow_non_constant=*/false,
6230                                            NULL);
6231       /* Look for the closing `]'.  */
6232       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6233
6234       /* Add this bound to the declarator.  */
6235       declarator = make_array_declarator (declarator, expression);
6236
6237       /* If the next token is not a `[', then there are no more
6238          bounds.  */
6239       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6240         break;
6241     }
6242
6243   return declarator;
6244 }
6245
6246 /* Parse a new-initializer.
6247
6248    new-initializer:
6249      ( expression-list [opt] )
6250      braced-init-list
6251
6252    Returns a representation of the expression-list.  */
6253
6254 static VEC(tree,gc) *
6255 cp_parser_new_initializer (cp_parser* parser)
6256 {
6257   VEC(tree,gc) *expression_list;
6258
6259   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6260     {
6261       tree t;
6262       bool expr_non_constant_p;
6263       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6264       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6265       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6266       expression_list = make_tree_vector_single (t);
6267     }
6268   else
6269     expression_list = (cp_parser_parenthesized_expression_list
6270                        (parser, non_attr, /*cast_p=*/false,
6271                         /*allow_expansion_p=*/true,
6272                         /*non_constant_p=*/NULL));
6273
6274   return expression_list;
6275 }
6276
6277 /* Parse a delete-expression.
6278
6279    delete-expression:
6280      :: [opt] delete cast-expression
6281      :: [opt] delete [ ] cast-expression
6282
6283    Returns a representation of the expression.  */
6284
6285 static tree
6286 cp_parser_delete_expression (cp_parser* parser)
6287 {
6288   bool global_scope_p;
6289   bool array_p;
6290   tree expression;
6291
6292   /* Look for the optional `::' operator.  */
6293   global_scope_p
6294     = (cp_parser_global_scope_opt (parser,
6295                                    /*current_scope_valid_p=*/false)
6296        != NULL_TREE);
6297   /* Look for the `delete' keyword.  */
6298   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6299   /* See if the array syntax is in use.  */
6300   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6301     {
6302       /* Consume the `[' token.  */
6303       cp_lexer_consume_token (parser->lexer);
6304       /* Look for the `]' token.  */
6305       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6306       /* Remember that this is the `[]' construct.  */
6307       array_p = true;
6308     }
6309   else
6310     array_p = false;
6311
6312   /* Parse the cast-expression.  */
6313   expression = cp_parser_simple_cast_expression (parser);
6314
6315   /* A delete-expression may not appear in an integral constant
6316      expression.  */
6317   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6318     return error_mark_node;
6319
6320   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6321                         tf_warning_or_error);
6322 }
6323
6324 /* Returns true if TOKEN may start a cast-expression and false
6325    otherwise.  */
6326
6327 static bool
6328 cp_parser_token_starts_cast_expression (cp_token *token)
6329 {
6330   switch (token->type)
6331     {
6332     case CPP_COMMA:
6333     case CPP_SEMICOLON:
6334     case CPP_QUERY:
6335     case CPP_COLON:
6336     case CPP_CLOSE_SQUARE:
6337     case CPP_CLOSE_PAREN:
6338     case CPP_CLOSE_BRACE:
6339     case CPP_DOT:
6340     case CPP_DOT_STAR:
6341     case CPP_DEREF:
6342     case CPP_DEREF_STAR:
6343     case CPP_DIV:
6344     case CPP_MOD:
6345     case CPP_LSHIFT:
6346     case CPP_RSHIFT:
6347     case CPP_LESS:
6348     case CPP_GREATER:
6349     case CPP_LESS_EQ:
6350     case CPP_GREATER_EQ:
6351     case CPP_EQ_EQ:
6352     case CPP_NOT_EQ:
6353     case CPP_EQ:
6354     case CPP_MULT_EQ:
6355     case CPP_DIV_EQ:
6356     case CPP_MOD_EQ:
6357     case CPP_PLUS_EQ:
6358     case CPP_MINUS_EQ:
6359     case CPP_RSHIFT_EQ:
6360     case CPP_LSHIFT_EQ:
6361     case CPP_AND_EQ:
6362     case CPP_XOR_EQ:
6363     case CPP_OR_EQ:
6364     case CPP_XOR:
6365     case CPP_OR:
6366     case CPP_OR_OR:
6367     case CPP_EOF:
6368       return false;
6369
6370       /* '[' may start a primary-expression in obj-c++.  */
6371     case CPP_OPEN_SQUARE:
6372       return c_dialect_objc ();
6373
6374     default:
6375       return true;
6376     }
6377 }
6378
6379 /* Parse a cast-expression.
6380
6381    cast-expression:
6382      unary-expression
6383      ( type-id ) cast-expression
6384
6385    ADDRESS_P is true iff the unary-expression is appearing as the
6386    operand of the `&' operator.   CAST_P is true if this expression is
6387    the target of a cast.
6388
6389    Returns a representation of the expression.  */
6390
6391 static tree
6392 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6393                            cp_id_kind * pidk)
6394 {
6395   /* If it's a `(', then we might be looking at a cast.  */
6396   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6397     {
6398       tree type = NULL_TREE;
6399       tree expr = NULL_TREE;
6400       bool compound_literal_p;
6401       const char *saved_message;
6402
6403       /* There's no way to know yet whether or not this is a cast.
6404          For example, `(int (3))' is a unary-expression, while `(int)
6405          3' is a cast.  So, we resort to parsing tentatively.  */
6406       cp_parser_parse_tentatively (parser);
6407       /* Types may not be defined in a cast.  */
6408       saved_message = parser->type_definition_forbidden_message;
6409       parser->type_definition_forbidden_message
6410         = G_("types may not be defined in casts");
6411       /* Consume the `('.  */
6412       cp_lexer_consume_token (parser->lexer);
6413       /* A very tricky bit is that `(struct S) { 3 }' is a
6414          compound-literal (which we permit in C++ as an extension).
6415          But, that construct is not a cast-expression -- it is a
6416          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6417          is legal; if the compound-literal were a cast-expression,
6418          you'd need an extra set of parentheses.)  But, if we parse
6419          the type-id, and it happens to be a class-specifier, then we
6420          will commit to the parse at that point, because we cannot
6421          undo the action that is done when creating a new class.  So,
6422          then we cannot back up and do a postfix-expression.
6423
6424          Therefore, we scan ahead to the closing `)', and check to see
6425          if the token after the `)' is a `{'.  If so, we are not
6426          looking at a cast-expression.
6427
6428          Save tokens so that we can put them back.  */
6429       cp_lexer_save_tokens (parser->lexer);
6430       /* Skip tokens until the next token is a closing parenthesis.
6431          If we find the closing `)', and the next token is a `{', then
6432          we are looking at a compound-literal.  */
6433       compound_literal_p
6434         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6435                                                   /*consume_paren=*/true)
6436            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6437       /* Roll back the tokens we skipped.  */
6438       cp_lexer_rollback_tokens (parser->lexer);
6439       /* If we were looking at a compound-literal, simulate an error
6440          so that the call to cp_parser_parse_definitely below will
6441          fail.  */
6442       if (compound_literal_p)
6443         cp_parser_simulate_error (parser);
6444       else
6445         {
6446           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6447           parser->in_type_id_in_expr_p = true;
6448           /* Look for the type-id.  */
6449           type = cp_parser_type_id (parser);
6450           /* Look for the closing `)'.  */
6451           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6452           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6453         }
6454
6455       /* Restore the saved message.  */
6456       parser->type_definition_forbidden_message = saved_message;
6457
6458       /* At this point this can only be either a cast or a
6459          parenthesized ctor such as `(T ())' that looks like a cast to
6460          function returning T.  */
6461       if (!cp_parser_error_occurred (parser)
6462           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6463                                                      (parser->lexer)))
6464         {
6465           cp_parser_parse_definitely (parser);
6466           expr = cp_parser_cast_expression (parser,
6467                                             /*address_p=*/false,
6468                                             /*cast_p=*/true, pidk);
6469
6470           /* Warn about old-style casts, if so requested.  */
6471           if (warn_old_style_cast
6472               && !in_system_header
6473               && !VOID_TYPE_P (type)
6474               && current_lang_name != lang_name_c)
6475             warning (OPT_Wold_style_cast, "use of old-style cast");
6476
6477           /* Only type conversions to integral or enumeration types
6478              can be used in constant-expressions.  */
6479           if (!cast_valid_in_integral_constant_expression_p (type)
6480               && cp_parser_non_integral_constant_expression (parser,
6481                                                              NIC_CAST))
6482             return error_mark_node;
6483
6484           /* Perform the cast.  */
6485           expr = build_c_cast (input_location, type, expr);
6486           return expr;
6487         }
6488       else 
6489         cp_parser_abort_tentative_parse (parser);
6490     }
6491
6492   /* If we get here, then it's not a cast, so it must be a
6493      unary-expression.  */
6494   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6495 }
6496
6497 /* Parse a binary expression of the general form:
6498
6499    pm-expression:
6500      cast-expression
6501      pm-expression .* cast-expression
6502      pm-expression ->* cast-expression
6503
6504    multiplicative-expression:
6505      pm-expression
6506      multiplicative-expression * pm-expression
6507      multiplicative-expression / pm-expression
6508      multiplicative-expression % pm-expression
6509
6510    additive-expression:
6511      multiplicative-expression
6512      additive-expression + multiplicative-expression
6513      additive-expression - multiplicative-expression
6514
6515    shift-expression:
6516      additive-expression
6517      shift-expression << additive-expression
6518      shift-expression >> additive-expression
6519
6520    relational-expression:
6521      shift-expression
6522      relational-expression < shift-expression
6523      relational-expression > shift-expression
6524      relational-expression <= shift-expression
6525      relational-expression >= shift-expression
6526
6527   GNU Extension:
6528
6529    relational-expression:
6530      relational-expression <? shift-expression
6531      relational-expression >? shift-expression
6532
6533    equality-expression:
6534      relational-expression
6535      equality-expression == relational-expression
6536      equality-expression != relational-expression
6537
6538    and-expression:
6539      equality-expression
6540      and-expression & equality-expression
6541
6542    exclusive-or-expression:
6543      and-expression
6544      exclusive-or-expression ^ and-expression
6545
6546    inclusive-or-expression:
6547      exclusive-or-expression
6548      inclusive-or-expression | exclusive-or-expression
6549
6550    logical-and-expression:
6551      inclusive-or-expression
6552      logical-and-expression && inclusive-or-expression
6553
6554    logical-or-expression:
6555      logical-and-expression
6556      logical-or-expression || logical-and-expression
6557
6558    All these are implemented with a single function like:
6559
6560    binary-expression:
6561      simple-cast-expression
6562      binary-expression <token> binary-expression
6563
6564    CAST_P is true if this expression is the target of a cast.
6565
6566    The binops_by_token map is used to get the tree codes for each <token> type.
6567    binary-expressions are associated according to a precedence table.  */
6568
6569 #define TOKEN_PRECEDENCE(token)                              \
6570 (((token->type == CPP_GREATER                                \
6571    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6572   && !parser->greater_than_is_operator_p)                    \
6573  ? PREC_NOT_OPERATOR                                         \
6574  : binops_by_token[token->type].prec)
6575
6576 static tree
6577 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6578                              bool no_toplevel_fold_p,
6579                              enum cp_parser_prec prec,
6580                              cp_id_kind * pidk)
6581 {
6582   cp_parser_expression_stack stack;
6583   cp_parser_expression_stack_entry *sp = &stack[0];
6584   tree lhs, rhs;
6585   cp_token *token;
6586   enum tree_code tree_type, lhs_type, rhs_type;
6587   enum cp_parser_prec new_prec, lookahead_prec;
6588   bool overloaded_p;
6589
6590   /* Parse the first expression.  */
6591   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6592   lhs_type = ERROR_MARK;
6593
6594   for (;;)
6595     {
6596       /* Get an operator token.  */
6597       token = cp_lexer_peek_token (parser->lexer);
6598
6599       if (warn_cxx0x_compat
6600           && token->type == CPP_RSHIFT
6601           && !parser->greater_than_is_operator_p)
6602         {
6603           if (warning_at (token->location, OPT_Wc__0x_compat, 
6604                           "%<>>%> operator will be treated as"
6605                           " two right angle brackets in C++0x"))
6606             inform (token->location,
6607                     "suggest parentheses around %<>>%> expression");
6608         }
6609
6610       new_prec = TOKEN_PRECEDENCE (token);
6611
6612       /* Popping an entry off the stack means we completed a subexpression:
6613          - either we found a token which is not an operator (`>' where it is not
6614            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6615            will happen repeatedly;
6616          - or, we found an operator which has lower priority.  This is the case
6617            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6618            parsing `3 * 4'.  */
6619       if (new_prec <= prec)
6620         {
6621           if (sp == stack)
6622             break;
6623           else
6624             goto pop;
6625         }
6626
6627      get_rhs:
6628       tree_type = binops_by_token[token->type].tree_type;
6629
6630       /* We used the operator token.  */
6631       cp_lexer_consume_token (parser->lexer);
6632
6633       /* For "false && x" or "true || x", x will never be executed;
6634          disable warnings while evaluating it.  */
6635       if (tree_type == TRUTH_ANDIF_EXPR)
6636         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6637       else if (tree_type == TRUTH_ORIF_EXPR)
6638         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6639
6640       /* Extract another operand.  It may be the RHS of this expression
6641          or the LHS of a new, higher priority expression.  */
6642       rhs = cp_parser_simple_cast_expression (parser);
6643       rhs_type = ERROR_MARK;
6644
6645       /* Get another operator token.  Look up its precedence to avoid
6646          building a useless (immediately popped) stack entry for common
6647          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6648       token = cp_lexer_peek_token (parser->lexer);
6649       lookahead_prec = TOKEN_PRECEDENCE (token);
6650       if (lookahead_prec > new_prec)
6651         {
6652           /* ... and prepare to parse the RHS of the new, higher priority
6653              expression.  Since precedence levels on the stack are
6654              monotonically increasing, we do not have to care about
6655              stack overflows.  */
6656           sp->prec = prec;
6657           sp->tree_type = tree_type;
6658           sp->lhs = lhs;
6659           sp->lhs_type = lhs_type;
6660           sp++;
6661           lhs = rhs;
6662           lhs_type = rhs_type;
6663           prec = new_prec;
6664           new_prec = lookahead_prec;
6665           goto get_rhs;
6666
6667          pop:
6668           lookahead_prec = new_prec;
6669           /* If the stack is not empty, we have parsed into LHS the right side
6670              (`4' in the example above) of an expression we had suspended.
6671              We can use the information on the stack to recover the LHS (`3')
6672              from the stack together with the tree code (`MULT_EXPR'), and
6673              the precedence of the higher level subexpression
6674              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6675              which will be used to actually build the additive expression.  */
6676           --sp;
6677           prec = sp->prec;
6678           tree_type = sp->tree_type;
6679           rhs = lhs;
6680           rhs_type = lhs_type;
6681           lhs = sp->lhs;
6682           lhs_type = sp->lhs_type;
6683         }
6684
6685       /* Undo the disabling of warnings done above.  */
6686       if (tree_type == TRUTH_ANDIF_EXPR)
6687         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6688       else if (tree_type == TRUTH_ORIF_EXPR)
6689         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6690
6691       overloaded_p = false;
6692       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6693          ERROR_MARK for everything that is not a binary expression.
6694          This makes warn_about_parentheses miss some warnings that
6695          involve unary operators.  For unary expressions we should
6696          pass the correct tree_code unless the unary expression was
6697          surrounded by parentheses.
6698       */
6699       if (no_toplevel_fold_p
6700           && lookahead_prec <= prec
6701           && sp == stack
6702           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6703         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6704       else
6705         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6706                                  &overloaded_p, tf_warning_or_error);
6707       lhs_type = tree_type;
6708
6709       /* If the binary operator required the use of an overloaded operator,
6710          then this expression cannot be an integral constant-expression.
6711          An overloaded operator can be used even if both operands are
6712          otherwise permissible in an integral constant-expression if at
6713          least one of the operands is of enumeration type.  */
6714
6715       if (overloaded_p
6716           && cp_parser_non_integral_constant_expression (parser,
6717                                                          NIC_OVERLOADED))
6718         return error_mark_node;
6719     }
6720
6721   return lhs;
6722 }
6723
6724
6725 /* Parse the `? expression : assignment-expression' part of a
6726    conditional-expression.  The LOGICAL_OR_EXPR is the
6727    logical-or-expression that started the conditional-expression.
6728    Returns a representation of the entire conditional-expression.
6729
6730    This routine is used by cp_parser_assignment_expression.
6731
6732      ? expression : assignment-expression
6733
6734    GNU Extensions:
6735
6736      ? : assignment-expression */
6737
6738 static tree
6739 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6740 {
6741   tree expr;
6742   tree assignment_expr;
6743   struct cp_token *token;
6744
6745   /* Consume the `?' token.  */
6746   cp_lexer_consume_token (parser->lexer);
6747   token = cp_lexer_peek_token (parser->lexer);
6748   if (cp_parser_allow_gnu_extensions_p (parser)
6749       && token->type == CPP_COLON)
6750     {
6751       pedwarn (token->location, OPT_pedantic, 
6752                "ISO C++ does not allow ?: with omitted middle operand");
6753       /* Implicit true clause.  */
6754       expr = NULL_TREE;
6755       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6756       warn_for_omitted_condop (token->location, logical_or_expr);
6757     }
6758   else
6759     {
6760       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6761       parser->colon_corrects_to_scope_p = false;
6762       /* Parse the expression.  */
6763       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6764       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6765       c_inhibit_evaluation_warnings +=
6766         ((logical_or_expr == truthvalue_true_node)
6767          - (logical_or_expr == truthvalue_false_node));
6768       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6769     }
6770
6771   /* The next token should be a `:'.  */
6772   cp_parser_require (parser, CPP_COLON, RT_COLON);
6773   /* Parse the assignment-expression.  */
6774   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6775   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6776
6777   /* Build the conditional-expression.  */
6778   return build_x_conditional_expr (logical_or_expr,
6779                                    expr,
6780                                    assignment_expr,
6781                                    tf_warning_or_error);
6782 }
6783
6784 /* Parse an assignment-expression.
6785
6786    assignment-expression:
6787      conditional-expression
6788      logical-or-expression assignment-operator assignment_expression
6789      throw-expression
6790
6791    CAST_P is true if this expression is the target of a cast.
6792
6793    Returns a representation for the expression.  */
6794
6795 static tree
6796 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6797                                  cp_id_kind * pidk)
6798 {
6799   tree expr;
6800
6801   /* If the next token is the `throw' keyword, then we're looking at
6802      a throw-expression.  */
6803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6804     expr = cp_parser_throw_expression (parser);
6805   /* Otherwise, it must be that we are looking at a
6806      logical-or-expression.  */
6807   else
6808     {
6809       /* Parse the binary expressions (logical-or-expression).  */
6810       expr = cp_parser_binary_expression (parser, cast_p, false,
6811                                           PREC_NOT_OPERATOR, pidk);
6812       /* If the next token is a `?' then we're actually looking at a
6813          conditional-expression.  */
6814       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6815         return cp_parser_question_colon_clause (parser, expr);
6816       else
6817         {
6818           enum tree_code assignment_operator;
6819
6820           /* If it's an assignment-operator, we're using the second
6821              production.  */
6822           assignment_operator
6823             = cp_parser_assignment_operator_opt (parser);
6824           if (assignment_operator != ERROR_MARK)
6825             {
6826               bool non_constant_p;
6827
6828               /* Parse the right-hand side of the assignment.  */
6829               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6830
6831               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6832                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6833
6834               /* An assignment may not appear in a
6835                  constant-expression.  */
6836               if (cp_parser_non_integral_constant_expression (parser,
6837                                                               NIC_ASSIGNMENT))
6838                 return error_mark_node;
6839               /* Build the assignment expression.  */
6840               expr = build_x_modify_expr (expr,
6841                                           assignment_operator,
6842                                           rhs,
6843                                           tf_warning_or_error);
6844             }
6845         }
6846     }
6847
6848   return expr;
6849 }
6850
6851 /* Parse an (optional) assignment-operator.
6852
6853    assignment-operator: one of
6854      = *= /= %= += -= >>= <<= &= ^= |=
6855
6856    GNU Extension:
6857
6858    assignment-operator: one of
6859      <?= >?=
6860
6861    If the next token is an assignment operator, the corresponding tree
6862    code is returned, and the token is consumed.  For example, for
6863    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6864    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6865    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6866    operator, ERROR_MARK is returned.  */
6867
6868 static enum tree_code
6869 cp_parser_assignment_operator_opt (cp_parser* parser)
6870 {
6871   enum tree_code op;
6872   cp_token *token;
6873
6874   /* Peek at the next token.  */
6875   token = cp_lexer_peek_token (parser->lexer);
6876
6877   switch (token->type)
6878     {
6879     case CPP_EQ:
6880       op = NOP_EXPR;
6881       break;
6882
6883     case CPP_MULT_EQ:
6884       op = MULT_EXPR;
6885       break;
6886
6887     case CPP_DIV_EQ:
6888       op = TRUNC_DIV_EXPR;
6889       break;
6890
6891     case CPP_MOD_EQ:
6892       op = TRUNC_MOD_EXPR;
6893       break;
6894
6895     case CPP_PLUS_EQ:
6896       op = PLUS_EXPR;
6897       break;
6898
6899     case CPP_MINUS_EQ:
6900       op = MINUS_EXPR;
6901       break;
6902
6903     case CPP_RSHIFT_EQ:
6904       op = RSHIFT_EXPR;
6905       break;
6906
6907     case CPP_LSHIFT_EQ:
6908       op = LSHIFT_EXPR;
6909       break;
6910
6911     case CPP_AND_EQ:
6912       op = BIT_AND_EXPR;
6913       break;
6914
6915     case CPP_XOR_EQ:
6916       op = BIT_XOR_EXPR;
6917       break;
6918
6919     case CPP_OR_EQ:
6920       op = BIT_IOR_EXPR;
6921       break;
6922
6923     default:
6924       /* Nothing else is an assignment operator.  */
6925       op = ERROR_MARK;
6926     }
6927
6928   /* If it was an assignment operator, consume it.  */
6929   if (op != ERROR_MARK)
6930     cp_lexer_consume_token (parser->lexer);
6931
6932   return op;
6933 }
6934
6935 /* Parse an expression.
6936
6937    expression:
6938      assignment-expression
6939      expression , assignment-expression
6940
6941    CAST_P is true if this expression is the target of a cast.
6942
6943    Returns a representation of the expression.  */
6944
6945 static tree
6946 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6947 {
6948   tree expression = NULL_TREE;
6949
6950   while (true)
6951     {
6952       tree assignment_expression;
6953
6954       /* Parse the next assignment-expression.  */
6955       assignment_expression
6956         = cp_parser_assignment_expression (parser, cast_p, pidk);
6957       /* If this is the first assignment-expression, we can just
6958          save it away.  */
6959       if (!expression)
6960         expression = assignment_expression;
6961       else
6962         expression = build_x_compound_expr (expression,
6963                                             assignment_expression,
6964                                             tf_warning_or_error);
6965       /* If the next token is not a comma, then we are done with the
6966          expression.  */
6967       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6968         break;
6969       /* Consume the `,'.  */
6970       cp_lexer_consume_token (parser->lexer);
6971       /* A comma operator cannot appear in a constant-expression.  */
6972       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6973         expression = error_mark_node;
6974     }
6975
6976   return expression;
6977 }
6978
6979 /* Parse a constant-expression.
6980
6981    constant-expression:
6982      conditional-expression
6983
6984   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6985   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6986   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6987   is false, NON_CONSTANT_P should be NULL.  */
6988
6989 static tree
6990 cp_parser_constant_expression (cp_parser* parser,
6991                                bool allow_non_constant_p,
6992                                bool *non_constant_p)
6993 {
6994   bool saved_integral_constant_expression_p;
6995   bool saved_allow_non_integral_constant_expression_p;
6996   bool saved_non_integral_constant_expression_p;
6997   tree expression;
6998
6999   /* It might seem that we could simply parse the
7000      conditional-expression, and then check to see if it were
7001      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7002      one that the compiler can figure out is constant, possibly after
7003      doing some simplifications or optimizations.  The standard has a
7004      precise definition of constant-expression, and we must honor
7005      that, even though it is somewhat more restrictive.
7006
7007      For example:
7008
7009        int i[(2, 3)];
7010
7011      is not a legal declaration, because `(2, 3)' is not a
7012      constant-expression.  The `,' operator is forbidden in a
7013      constant-expression.  However, GCC's constant-folding machinery
7014      will fold this operation to an INTEGER_CST for `3'.  */
7015
7016   /* Save the old settings.  */
7017   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7018   saved_allow_non_integral_constant_expression_p
7019     = parser->allow_non_integral_constant_expression_p;
7020   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7021   /* We are now parsing a constant-expression.  */
7022   parser->integral_constant_expression_p = true;
7023   parser->allow_non_integral_constant_expression_p
7024     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7025   parser->non_integral_constant_expression_p = false;
7026   /* Although the grammar says "conditional-expression", we parse an
7027      "assignment-expression", which also permits "throw-expression"
7028      and the use of assignment operators.  In the case that
7029      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7030      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7031      actually essential that we look for an assignment-expression.
7032      For example, cp_parser_initializer_clauses uses this function to
7033      determine whether a particular assignment-expression is in fact
7034      constant.  */
7035   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7036   /* Restore the old settings.  */
7037   parser->integral_constant_expression_p
7038     = saved_integral_constant_expression_p;
7039   parser->allow_non_integral_constant_expression_p
7040     = saved_allow_non_integral_constant_expression_p;
7041   if (cxx_dialect >= cxx0x)
7042     {
7043       /* Require an rvalue constant expression here; that's what our
7044          callers expect.  Reference constant expressions are handled
7045          separately in e.g. cp_parser_template_argument.  */
7046       bool is_const = potential_rvalue_constant_expression (expression);
7047       parser->non_integral_constant_expression_p = !is_const;
7048       if (!is_const && !allow_non_constant_p)
7049         require_potential_rvalue_constant_expression (expression);
7050     }
7051   if (allow_non_constant_p)
7052     *non_constant_p = parser->non_integral_constant_expression_p;
7053   else if (parser->non_integral_constant_expression_p)
7054     expression = error_mark_node;
7055   parser->non_integral_constant_expression_p
7056     = saved_non_integral_constant_expression_p;
7057
7058   return expression;
7059 }
7060
7061 /* Parse __builtin_offsetof.
7062
7063    offsetof-expression:
7064      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7065
7066    offsetof-member-designator:
7067      id-expression
7068      | offsetof-member-designator "." id-expression
7069      | offsetof-member-designator "[" expression "]"
7070      | offsetof-member-designator "->" id-expression  */
7071
7072 static tree
7073 cp_parser_builtin_offsetof (cp_parser *parser)
7074 {
7075   int save_ice_p, save_non_ice_p;
7076   tree type, expr;
7077   cp_id_kind dummy;
7078   cp_token *token;
7079
7080   /* We're about to accept non-integral-constant things, but will
7081      definitely yield an integral constant expression.  Save and
7082      restore these values around our local parsing.  */
7083   save_ice_p = parser->integral_constant_expression_p;
7084   save_non_ice_p = parser->non_integral_constant_expression_p;
7085
7086   /* Consume the "__builtin_offsetof" token.  */
7087   cp_lexer_consume_token (parser->lexer);
7088   /* Consume the opening `('.  */
7089   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7090   /* Parse the type-id.  */
7091   type = cp_parser_type_id (parser);
7092   /* Look for the `,'.  */
7093   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7094   token = cp_lexer_peek_token (parser->lexer);
7095
7096   /* Build the (type *)null that begins the traditional offsetof macro.  */
7097   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7098                             tf_warning_or_error);
7099
7100   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7101   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7102                                                  true, &dummy, token->location);
7103   while (true)
7104     {
7105       token = cp_lexer_peek_token (parser->lexer);
7106       switch (token->type)
7107         {
7108         case CPP_OPEN_SQUARE:
7109           /* offsetof-member-designator "[" expression "]" */
7110           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7111           break;
7112
7113         case CPP_DEREF:
7114           /* offsetof-member-designator "->" identifier */
7115           expr = grok_array_decl (expr, integer_zero_node);
7116           /* FALLTHRU */
7117
7118         case CPP_DOT:
7119           /* offsetof-member-designator "." identifier */
7120           cp_lexer_consume_token (parser->lexer);
7121           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7122                                                          expr, true, &dummy,
7123                                                          token->location);
7124           break;
7125
7126         case CPP_CLOSE_PAREN:
7127           /* Consume the ")" token.  */
7128           cp_lexer_consume_token (parser->lexer);
7129           goto success;
7130
7131         default:
7132           /* Error.  We know the following require will fail, but
7133              that gives the proper error message.  */
7134           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7135           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7136           expr = error_mark_node;
7137           goto failure;
7138         }
7139     }
7140
7141  success:
7142   /* If we're processing a template, we can't finish the semantics yet.
7143      Otherwise we can fold the entire expression now.  */
7144   if (processing_template_decl)
7145     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7146   else
7147     expr = finish_offsetof (expr);
7148
7149  failure:
7150   parser->integral_constant_expression_p = save_ice_p;
7151   parser->non_integral_constant_expression_p = save_non_ice_p;
7152
7153   return expr;
7154 }
7155
7156 /* Parse a trait expression.
7157
7158    Returns a representation of the expression, the underlying type
7159    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7160
7161 static tree
7162 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7163 {
7164   cp_trait_kind kind;
7165   tree type1, type2 = NULL_TREE;
7166   bool binary = false;
7167   cp_decl_specifier_seq decl_specs;
7168
7169   switch (keyword)
7170     {
7171     case RID_HAS_NOTHROW_ASSIGN:
7172       kind = CPTK_HAS_NOTHROW_ASSIGN;
7173       break;
7174     case RID_HAS_NOTHROW_CONSTRUCTOR:
7175       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7176       break;
7177     case RID_HAS_NOTHROW_COPY:
7178       kind = CPTK_HAS_NOTHROW_COPY;
7179       break;
7180     case RID_HAS_TRIVIAL_ASSIGN:
7181       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7182       break;
7183     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7184       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7185       break;
7186     case RID_HAS_TRIVIAL_COPY:
7187       kind = CPTK_HAS_TRIVIAL_COPY;
7188       break;
7189     case RID_HAS_TRIVIAL_DESTRUCTOR:
7190       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7191       break;
7192     case RID_HAS_VIRTUAL_DESTRUCTOR:
7193       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7194       break;
7195     case RID_IS_ABSTRACT:
7196       kind = CPTK_IS_ABSTRACT;
7197       break;
7198     case RID_IS_BASE_OF:
7199       kind = CPTK_IS_BASE_OF;
7200       binary = true;
7201       break;
7202     case RID_IS_CLASS:
7203       kind = CPTK_IS_CLASS;
7204       break;
7205     case RID_IS_CONVERTIBLE_TO:
7206       kind = CPTK_IS_CONVERTIBLE_TO;
7207       binary = true;
7208       break;
7209     case RID_IS_EMPTY:
7210       kind = CPTK_IS_EMPTY;
7211       break;
7212     case RID_IS_ENUM:
7213       kind = CPTK_IS_ENUM;
7214       break;
7215     case RID_IS_LITERAL_TYPE:
7216       kind = CPTK_IS_LITERAL_TYPE;
7217       break;
7218     case RID_IS_POD:
7219       kind = CPTK_IS_POD;
7220       break;
7221     case RID_IS_POLYMORPHIC:
7222       kind = CPTK_IS_POLYMORPHIC;
7223       break;
7224     case RID_IS_STD_LAYOUT:
7225       kind = CPTK_IS_STD_LAYOUT;
7226       break;
7227     case RID_IS_TRIVIAL:
7228       kind = CPTK_IS_TRIVIAL;
7229       break;
7230     case RID_IS_UNION:
7231       kind = CPTK_IS_UNION;
7232       break;
7233     case RID_UNDERLYING_TYPE:
7234       kind = CPTK_UNDERLYING_TYPE;
7235       break;
7236     default:
7237       gcc_unreachable ();
7238     }
7239
7240   /* Consume the token.  */
7241   cp_lexer_consume_token (parser->lexer);
7242
7243   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7244
7245   type1 = cp_parser_type_id (parser);
7246
7247   if (type1 == error_mark_node)
7248     return error_mark_node;
7249
7250   /* Build a trivial decl-specifier-seq.  */
7251   clear_decl_specs (&decl_specs);
7252   decl_specs.type = type1;
7253
7254   /* Call grokdeclarator to figure out what type this is.  */
7255   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7256                           /*initialized=*/0, /*attrlist=*/NULL);
7257
7258   if (binary)
7259     {
7260       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7261  
7262       type2 = cp_parser_type_id (parser);
7263
7264       if (type2 == error_mark_node)
7265         return error_mark_node;
7266
7267       /* Build a trivial decl-specifier-seq.  */
7268       clear_decl_specs (&decl_specs);
7269       decl_specs.type = type2;
7270
7271       /* Call grokdeclarator to figure out what type this is.  */
7272       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7273                               /*initialized=*/0, /*attrlist=*/NULL);
7274     }
7275
7276   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7277
7278   /* Complete the trait expression, which may mean either processing
7279      the trait expr now or saving it for template instantiation.  */
7280   return kind != CPTK_UNDERLYING_TYPE
7281     ? finish_trait_expr (kind, type1, type2)
7282     : finish_underlying_type (type1);
7283 }
7284
7285 /* Lambdas that appear in variable initializer or default argument scope
7286    get that in their mangling, so we need to record it.  We might as well
7287    use the count for function and namespace scopes as well.  */
7288 static GTY(()) tree lambda_scope;
7289 static GTY(()) int lambda_count;
7290 typedef struct GTY(()) tree_int
7291 {
7292   tree t;
7293   int i;
7294 } tree_int;
7295 DEF_VEC_O(tree_int);
7296 DEF_VEC_ALLOC_O(tree_int,gc);
7297 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7298
7299 static void
7300 start_lambda_scope (tree decl)
7301 {
7302   tree_int ti;
7303   gcc_assert (decl);
7304   /* Once we're inside a function, we ignore other scopes and just push
7305      the function again so that popping works properly.  */
7306   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7307     decl = current_function_decl;
7308   ti.t = lambda_scope;
7309   ti.i = lambda_count;
7310   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7311   if (lambda_scope != decl)
7312     {
7313       /* Don't reset the count if we're still in the same function.  */
7314       lambda_scope = decl;
7315       lambda_count = 0;
7316     }
7317 }
7318
7319 static void
7320 record_lambda_scope (tree lambda)
7321 {
7322   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7323   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7324 }
7325
7326 static void
7327 finish_lambda_scope (void)
7328 {
7329   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7330   if (lambda_scope != p->t)
7331     {
7332       lambda_scope = p->t;
7333       lambda_count = p->i;
7334     }
7335   VEC_pop (tree_int, lambda_scope_stack);
7336 }
7337
7338 /* Parse a lambda expression.
7339
7340    lambda-expression:
7341      lambda-introducer lambda-declarator [opt] compound-statement
7342
7343    Returns a representation of the expression.  */
7344
7345 static tree
7346 cp_parser_lambda_expression (cp_parser* parser)
7347 {
7348   tree lambda_expr = build_lambda_expr ();
7349   tree type;
7350   bool ok;
7351
7352   LAMBDA_EXPR_LOCATION (lambda_expr)
7353     = cp_lexer_peek_token (parser->lexer)->location;
7354
7355   if (cp_unevaluated_operand)
7356     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7357               "lambda-expression in unevaluated context");
7358
7359   /* We may be in the middle of deferred access check.  Disable
7360      it now.  */
7361   push_deferring_access_checks (dk_no_deferred);
7362
7363   cp_parser_lambda_introducer (parser, lambda_expr);
7364
7365   type = begin_lambda_type (lambda_expr);
7366
7367   record_lambda_scope (lambda_expr);
7368
7369   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7370   determine_visibility (TYPE_NAME (type));
7371
7372   /* Now that we've started the type, add the capture fields for any
7373      explicit captures.  */
7374   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7375
7376   {
7377     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7378     unsigned int saved_num_template_parameter_lists
7379         = parser->num_template_parameter_lists;
7380
7381     parser->num_template_parameter_lists = 0;
7382
7383     /* By virtue of defining a local class, a lambda expression has access to
7384        the private variables of enclosing classes.  */
7385
7386     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7387
7388     if (ok)
7389       cp_parser_lambda_body (parser, lambda_expr);
7390     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7391       cp_parser_skip_to_end_of_block_or_statement (parser);
7392
7393     /* The capture list was built up in reverse order; fix that now.  */
7394     {
7395       tree newlist = NULL_TREE;
7396       tree elt, next;
7397
7398       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7399            elt; elt = next)
7400         {
7401           tree field = TREE_PURPOSE (elt);
7402           char *buf;
7403
7404           next = TREE_CHAIN (elt);
7405           TREE_CHAIN (elt) = newlist;
7406           newlist = elt;
7407
7408           /* Also add __ to the beginning of the field name so that code
7409              outside the lambda body can't see the captured name.  We could
7410              just remove the name entirely, but this is more useful for
7411              debugging.  */
7412           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7413             /* The 'this' capture already starts with __.  */
7414             continue;
7415
7416           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7417           buf[1] = buf[0] = '_';
7418           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7419                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7420           DECL_NAME (field) = get_identifier (buf);
7421         }
7422       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7423     }
7424
7425     if (ok)
7426       maybe_add_lambda_conv_op (type);
7427
7428     type = finish_struct (type, /*attributes=*/NULL_TREE);
7429
7430     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7431   }
7432
7433   pop_deferring_access_checks ();
7434
7435   if (ok)
7436     return build_lambda_object (lambda_expr);
7437   else
7438     return error_mark_node;
7439 }
7440
7441 /* Parse the beginning of a lambda expression.
7442
7443    lambda-introducer:
7444      [ lambda-capture [opt] ]
7445
7446    LAMBDA_EXPR is the current representation of the lambda expression.  */
7447
7448 static void
7449 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7450 {
7451   /* Need commas after the first capture.  */
7452   bool first = true;
7453
7454   /* Eat the leading `['.  */
7455   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7456
7457   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7458   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7459       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7460     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7461   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7462     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7463
7464   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7465     {
7466       cp_lexer_consume_token (parser->lexer);
7467       first = false;
7468     }
7469
7470   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7471     {
7472       cp_token* capture_token;
7473       tree capture_id;
7474       tree capture_init_expr;
7475       cp_id_kind idk = CP_ID_KIND_NONE;
7476       bool explicit_init_p = false;
7477
7478       enum capture_kind_type
7479       {
7480         BY_COPY,
7481         BY_REFERENCE
7482       };
7483       enum capture_kind_type capture_kind = BY_COPY;
7484
7485       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7486         {
7487           error ("expected end of capture-list");
7488           return;
7489         }
7490
7491       if (first)
7492         first = false;
7493       else
7494         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7495
7496       /* Possibly capture `this'.  */
7497       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7498         {
7499           cp_lexer_consume_token (parser->lexer);
7500           add_capture (lambda_expr,
7501                        /*id=*/get_identifier ("__this"),
7502                        /*initializer=*/finish_this_expr(),
7503                        /*by_reference_p=*/false,
7504                        explicit_init_p);
7505           continue;
7506         }
7507
7508       /* Remember whether we want to capture as a reference or not.  */
7509       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7510         {
7511           capture_kind = BY_REFERENCE;
7512           cp_lexer_consume_token (parser->lexer);
7513         }
7514
7515       /* Get the identifier.  */
7516       capture_token = cp_lexer_peek_token (parser->lexer);
7517       capture_id = cp_parser_identifier (parser);
7518
7519       if (capture_id == error_mark_node)
7520         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7521            delimiters, but I modified this to stop on unnested ']' as well.  It
7522            was already changed to stop on unnested '}', so the
7523            "closing_parenthesis" name is no more misleading with my change.  */
7524         {
7525           cp_parser_skip_to_closing_parenthesis (parser,
7526                                                  /*recovering=*/true,
7527                                                  /*or_comma=*/true,
7528                                                  /*consume_paren=*/true);
7529           break;
7530         }
7531
7532       /* Find the initializer for this capture.  */
7533       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7534         {
7535           /* An explicit expression exists.  */
7536           cp_lexer_consume_token (parser->lexer);
7537           pedwarn (input_location, OPT_pedantic,
7538                    "ISO C++ does not allow initializers "
7539                    "in lambda expression capture lists");
7540           capture_init_expr = cp_parser_assignment_expression (parser,
7541                                                                /*cast_p=*/true,
7542                                                                &idk);
7543           explicit_init_p = true;
7544         }
7545       else
7546         {
7547           const char* error_msg;
7548
7549           /* Turn the identifier into an id-expression.  */
7550           capture_init_expr
7551             = cp_parser_lookup_name
7552                 (parser,
7553                  capture_id,
7554                  none_type,
7555                  /*is_template=*/false,
7556                  /*is_namespace=*/false,
7557                  /*check_dependency=*/true,
7558                  /*ambiguous_decls=*/NULL,
7559                  capture_token->location);
7560
7561           capture_init_expr
7562             = finish_id_expression
7563                 (capture_id,
7564                  capture_init_expr,
7565                  parser->scope,
7566                  &idk,
7567                  /*integral_constant_expression_p=*/false,
7568                  /*allow_non_integral_constant_expression_p=*/false,
7569                  /*non_integral_constant_expression_p=*/NULL,
7570                  /*template_p=*/false,
7571                  /*done=*/true,
7572                  /*address_p=*/false,
7573                  /*template_arg_p=*/false,
7574                  &error_msg,
7575                  capture_token->location);
7576         }
7577
7578       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7579         capture_init_expr
7580           = unqualified_name_lookup_error (capture_init_expr);
7581
7582       add_capture (lambda_expr,
7583                    capture_id,
7584                    capture_init_expr,
7585                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7586                    explicit_init_p);
7587     }
7588
7589   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7590 }
7591
7592 /* Parse the (optional) middle of a lambda expression.
7593
7594    lambda-declarator:
7595      ( parameter-declaration-clause [opt] )
7596        attribute-specifier [opt]
7597        mutable [opt]
7598        exception-specification [opt]
7599        lambda-return-type-clause [opt]
7600
7601    LAMBDA_EXPR is the current representation of the lambda expression.  */
7602
7603 static bool
7604 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7605 {
7606   /* 5.1.1.4 of the standard says:
7607        If a lambda-expression does not include a lambda-declarator, it is as if
7608        the lambda-declarator were ().
7609      This means an empty parameter list, no attributes, and no exception
7610      specification.  */
7611   tree param_list = void_list_node;
7612   tree attributes = NULL_TREE;
7613   tree exception_spec = NULL_TREE;
7614   tree t;
7615
7616   /* The lambda-declarator is optional, but must begin with an opening
7617      parenthesis if present.  */
7618   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7619     {
7620       cp_lexer_consume_token (parser->lexer);
7621
7622       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7623
7624       /* Parse parameters.  */
7625       param_list = cp_parser_parameter_declaration_clause (parser);
7626
7627       /* Default arguments shall not be specified in the
7628          parameter-declaration-clause of a lambda-declarator.  */
7629       for (t = param_list; t; t = TREE_CHAIN (t))
7630         if (TREE_PURPOSE (t))
7631           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7632                    "default argument specified for lambda parameter");
7633
7634       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7635
7636       attributes = cp_parser_attributes_opt (parser);
7637
7638       /* Parse optional `mutable' keyword.  */
7639       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7640         {
7641           cp_lexer_consume_token (parser->lexer);
7642           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7643         }
7644
7645       /* Parse optional exception specification.  */
7646       exception_spec = cp_parser_exception_specification_opt (parser);
7647
7648       /* Parse optional trailing return type.  */
7649       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7650         {
7651           cp_lexer_consume_token (parser->lexer);
7652           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7653         }
7654
7655       /* The function parameters must be in scope all the way until after the
7656          trailing-return-type in case of decltype.  */
7657       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7658         pop_binding (DECL_NAME (t), t);
7659
7660       leave_scope ();
7661     }
7662
7663   /* Create the function call operator.
7664
7665      Messing with declarators like this is no uglier than building up the
7666      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7667      other code.  */
7668   {
7669     cp_decl_specifier_seq return_type_specs;
7670     cp_declarator* declarator;
7671     tree fco;
7672     int quals;
7673     void *p;
7674
7675     clear_decl_specs (&return_type_specs);
7676     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7677       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7678     else
7679       /* Maybe we will deduce the return type later, but we can use void
7680          as a placeholder return type anyways.  */
7681       return_type_specs.type = void_type_node;
7682
7683     p = obstack_alloc (&declarator_obstack, 0);
7684
7685     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7686                                      sfk_none);
7687
7688     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7689              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7690     declarator = make_call_declarator (declarator, param_list, quals,
7691                                        VIRT_SPEC_UNSPECIFIED,
7692                                        exception_spec,
7693                                        /*late_return_type=*/NULL_TREE);
7694     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7695
7696     fco = grokmethod (&return_type_specs,
7697                       declarator,
7698                       attributes);
7699     if (fco != error_mark_node)
7700       {
7701         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7702         DECL_ARTIFICIAL (fco) = 1;
7703       }
7704
7705     finish_member_declaration (fco);
7706
7707     obstack_free (&declarator_obstack, p);
7708
7709     return (fco != error_mark_node);
7710   }
7711 }
7712
7713 /* Parse the body of a lambda expression, which is simply
7714
7715    compound-statement
7716
7717    but which requires special handling.
7718    LAMBDA_EXPR is the current representation of the lambda expression.  */
7719
7720 static void
7721 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7722 {
7723   bool nested = (current_function_decl != NULL_TREE);
7724   if (nested)
7725     push_function_context ();
7726
7727   /* Finish the function call operator
7728      - class_specifier
7729      + late_parsing_for_member
7730      + function_definition_after_declarator
7731      + ctor_initializer_opt_and_function_body  */
7732   {
7733     tree fco = lambda_function (lambda_expr);
7734     tree body;
7735     bool done = false;
7736
7737     /* Let the front end know that we are going to be defining this
7738        function.  */
7739     start_preparsed_function (fco,
7740                               NULL_TREE,
7741                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7742
7743     start_lambda_scope (fco);
7744     body = begin_function_body ();
7745
7746     /* 5.1.1.4 of the standard says:
7747          If a lambda-expression does not include a trailing-return-type, it
7748          is as if the trailing-return-type denotes the following type:
7749           * if the compound-statement is of the form
7750                { return attribute-specifier [opt] expression ; }
7751              the type of the returned expression after lvalue-to-rvalue
7752              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7753              (_conv.array_ 4.2), and function-to-pointer conversion
7754              (_conv.func_ 4.3);
7755           * otherwise, void.  */
7756
7757     /* In a lambda that has neither a lambda-return-type-clause
7758        nor a deducible form, errors should be reported for return statements
7759        in the body.  Since we used void as the placeholder return type, parsing
7760        the body as usual will give such desired behavior.  */
7761     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7762         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7763         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7764         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7765       {
7766         tree compound_stmt;
7767         tree expr = NULL_TREE;
7768         cp_id_kind idk = CP_ID_KIND_NONE;
7769
7770         /* Parse tentatively in case there's more after the initial return
7771            statement.  */
7772         cp_parser_parse_tentatively (parser);
7773
7774         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7775         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7776
7777         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7778
7779         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7780         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7781
7782         if (cp_parser_parse_definitely (parser))
7783           {
7784             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7785
7786             compound_stmt = begin_compound_stmt (0);
7787             /* Will get error here if type not deduced yet.  */
7788             finish_return_stmt (expr);
7789             finish_compound_stmt (compound_stmt);
7790
7791             done = true;
7792           }
7793       }
7794
7795     if (!done)
7796       {
7797         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7798           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7799         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7800            cp_parser_compound_stmt does not pass it.  */
7801         cp_parser_function_body (parser);
7802         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7803       }
7804
7805     finish_function_body (body);
7806     finish_lambda_scope ();
7807
7808     /* Finish the function and generate code for it if necessary.  */
7809     expand_or_defer_fn (finish_function (/*inline*/2));
7810   }
7811
7812   if (nested)
7813     pop_function_context();
7814 }
7815
7816 /* Statements [gram.stmt.stmt]  */
7817
7818 /* Parse a statement.
7819
7820    statement:
7821      labeled-statement
7822      expression-statement
7823      compound-statement
7824      selection-statement
7825      iteration-statement
7826      jump-statement
7827      declaration-statement
7828      try-block
7829
7830   IN_COMPOUND is true when the statement is nested inside a
7831   cp_parser_compound_statement; this matters for certain pragmas.
7832
7833   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7834   is a (possibly labeled) if statement which is not enclosed in braces
7835   and has an else clause.  This is used to implement -Wparentheses.  */
7836
7837 static void
7838 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7839                      bool in_compound, bool *if_p)
7840 {
7841   tree statement;
7842   cp_token *token;
7843   location_t statement_location;
7844
7845  restart:
7846   if (if_p != NULL)
7847     *if_p = false;
7848   /* There is no statement yet.  */
7849   statement = NULL_TREE;
7850   /* Peek at the next token.  */
7851   token = cp_lexer_peek_token (parser->lexer);
7852   /* Remember the location of the first token in the statement.  */
7853   statement_location = token->location;
7854   /* If this is a keyword, then that will often determine what kind of
7855      statement we have.  */
7856   if (token->type == CPP_KEYWORD)
7857     {
7858       enum rid keyword = token->keyword;
7859
7860       switch (keyword)
7861         {
7862         case RID_CASE:
7863         case RID_DEFAULT:
7864           /* Looks like a labeled-statement with a case label.
7865              Parse the label, and then use tail recursion to parse
7866              the statement.  */
7867           cp_parser_label_for_labeled_statement (parser);
7868           goto restart;
7869
7870         case RID_IF:
7871         case RID_SWITCH:
7872           statement = cp_parser_selection_statement (parser, if_p);
7873           break;
7874
7875         case RID_WHILE:
7876         case RID_DO:
7877         case RID_FOR:
7878           statement = cp_parser_iteration_statement (parser);
7879           break;
7880
7881         case RID_BREAK:
7882         case RID_CONTINUE:
7883         case RID_RETURN:
7884         case RID_GOTO:
7885           statement = cp_parser_jump_statement (parser);
7886           break;
7887
7888           /* Objective-C++ exception-handling constructs.  */
7889         case RID_AT_TRY:
7890         case RID_AT_CATCH:
7891         case RID_AT_FINALLY:
7892         case RID_AT_SYNCHRONIZED:
7893         case RID_AT_THROW:
7894           statement = cp_parser_objc_statement (parser);
7895           break;
7896
7897         case RID_TRY:
7898           statement = cp_parser_try_block (parser);
7899           break;
7900
7901         case RID_NAMESPACE:
7902           /* This must be a namespace alias definition.  */
7903           cp_parser_declaration_statement (parser);
7904           return;
7905           
7906         default:
7907           /* It might be a keyword like `int' that can start a
7908              declaration-statement.  */
7909           break;
7910         }
7911     }
7912   else if (token->type == CPP_NAME)
7913     {
7914       /* If the next token is a `:', then we are looking at a
7915          labeled-statement.  */
7916       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7917       if (token->type == CPP_COLON)
7918         {
7919           /* Looks like a labeled-statement with an ordinary label.
7920              Parse the label, and then use tail recursion to parse
7921              the statement.  */
7922           cp_parser_label_for_labeled_statement (parser);
7923           goto restart;
7924         }
7925     }
7926   /* Anything that starts with a `{' must be a compound-statement.  */
7927   else if (token->type == CPP_OPEN_BRACE)
7928     statement = cp_parser_compound_statement (parser, NULL, false, false);
7929   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7930      a statement all its own.  */
7931   else if (token->type == CPP_PRAGMA)
7932     {
7933       /* Only certain OpenMP pragmas are attached to statements, and thus
7934          are considered statements themselves.  All others are not.  In
7935          the context of a compound, accept the pragma as a "statement" and
7936          return so that we can check for a close brace.  Otherwise we
7937          require a real statement and must go back and read one.  */
7938       if (in_compound)
7939         cp_parser_pragma (parser, pragma_compound);
7940       else if (!cp_parser_pragma (parser, pragma_stmt))
7941         goto restart;
7942       return;
7943     }
7944   else if (token->type == CPP_EOF)
7945     {
7946       cp_parser_error (parser, "expected statement");
7947       return;
7948     }
7949
7950   /* Everything else must be a declaration-statement or an
7951      expression-statement.  Try for the declaration-statement
7952      first, unless we are looking at a `;', in which case we know that
7953      we have an expression-statement.  */
7954   if (!statement)
7955     {
7956       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7957         {
7958           cp_parser_parse_tentatively (parser);
7959           /* Try to parse the declaration-statement.  */
7960           cp_parser_declaration_statement (parser);
7961           /* If that worked, we're done.  */
7962           if (cp_parser_parse_definitely (parser))
7963             return;
7964         }
7965       /* Look for an expression-statement instead.  */
7966       statement = cp_parser_expression_statement (parser, in_statement_expr);
7967     }
7968
7969   /* Set the line number for the statement.  */
7970   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7971     SET_EXPR_LOCATION (statement, statement_location);
7972 }
7973
7974 /* Parse the label for a labeled-statement, i.e.
7975
7976    identifier :
7977    case constant-expression :
7978    default :
7979
7980    GNU Extension:
7981    case constant-expression ... constant-expression : statement
7982
7983    When a label is parsed without errors, the label is added to the
7984    parse tree by the finish_* functions, so this function doesn't
7985    have to return the label.  */
7986
7987 static void
7988 cp_parser_label_for_labeled_statement (cp_parser* parser)
7989 {
7990   cp_token *token;
7991   tree label = NULL_TREE;
7992   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7993
7994   /* The next token should be an identifier.  */
7995   token = cp_lexer_peek_token (parser->lexer);
7996   if (token->type != CPP_NAME
7997       && token->type != CPP_KEYWORD)
7998     {
7999       cp_parser_error (parser, "expected labeled-statement");
8000       return;
8001     }
8002
8003   parser->colon_corrects_to_scope_p = false;
8004   switch (token->keyword)
8005     {
8006     case RID_CASE:
8007       {
8008         tree expr, expr_hi;
8009         cp_token *ellipsis;
8010
8011         /* Consume the `case' token.  */
8012         cp_lexer_consume_token (parser->lexer);
8013         /* Parse the constant-expression.  */
8014         expr = cp_parser_constant_expression (parser,
8015                                               /*allow_non_constant_p=*/false,
8016                                               NULL);
8017
8018         ellipsis = cp_lexer_peek_token (parser->lexer);
8019         if (ellipsis->type == CPP_ELLIPSIS)
8020           {
8021             /* Consume the `...' token.  */
8022             cp_lexer_consume_token (parser->lexer);
8023             expr_hi =
8024               cp_parser_constant_expression (parser,
8025                                              /*allow_non_constant_p=*/false,
8026                                              NULL);
8027             /* We don't need to emit warnings here, as the common code
8028                will do this for us.  */
8029           }
8030         else
8031           expr_hi = NULL_TREE;
8032
8033         if (parser->in_switch_statement_p)
8034           finish_case_label (token->location, expr, expr_hi);
8035         else
8036           error_at (token->location,
8037                     "case label %qE not within a switch statement",
8038                     expr);
8039       }
8040       break;
8041
8042     case RID_DEFAULT:
8043       /* Consume the `default' token.  */
8044       cp_lexer_consume_token (parser->lexer);
8045
8046       if (parser->in_switch_statement_p)
8047         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8048       else
8049         error_at (token->location, "case label not within a switch statement");
8050       break;
8051
8052     default:
8053       /* Anything else must be an ordinary label.  */
8054       label = finish_label_stmt (cp_parser_identifier (parser));
8055       break;
8056     }
8057
8058   /* Require the `:' token.  */
8059   cp_parser_require (parser, CPP_COLON, RT_COLON);
8060
8061   /* An ordinary label may optionally be followed by attributes.
8062      However, this is only permitted if the attributes are then
8063      followed by a semicolon.  This is because, for backward
8064      compatibility, when parsing
8065        lab: __attribute__ ((unused)) int i;
8066      we want the attribute to attach to "i", not "lab".  */
8067   if (label != NULL_TREE
8068       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8069     {
8070       tree attrs;
8071
8072       cp_parser_parse_tentatively (parser);
8073       attrs = cp_parser_attributes_opt (parser);
8074       if (attrs == NULL_TREE
8075           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8076         cp_parser_abort_tentative_parse (parser);
8077       else if (!cp_parser_parse_definitely (parser))
8078         ;
8079       else
8080         cplus_decl_attributes (&label, attrs, 0);
8081     }
8082
8083   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8084 }
8085
8086 /* Parse an expression-statement.
8087
8088    expression-statement:
8089      expression [opt] ;
8090
8091    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8092    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8093    indicates whether this expression-statement is part of an
8094    expression statement.  */
8095
8096 static tree
8097 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8098 {
8099   tree statement = NULL_TREE;
8100   cp_token *token = cp_lexer_peek_token (parser->lexer);
8101
8102   /* If the next token is a ';', then there is no expression
8103      statement.  */
8104   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8105     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8106
8107   /* Give a helpful message for "A<T>::type t;" and the like.  */
8108   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8109       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8110     {
8111       if (TREE_CODE (statement) == SCOPE_REF)
8112         error_at (token->location, "need %<typename%> before %qE because "
8113                   "%qT is a dependent scope",
8114                   statement, TREE_OPERAND (statement, 0));
8115       else if (is_overloaded_fn (statement)
8116                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8117         {
8118           /* A::A a; */
8119           tree fn = get_first_fn (statement);
8120           error_at (token->location,
8121                     "%<%T::%D%> names the constructor, not the type",
8122                     DECL_CONTEXT (fn), DECL_NAME (fn));
8123         }
8124     }
8125
8126   /* Consume the final `;'.  */
8127   cp_parser_consume_semicolon_at_end_of_statement (parser);
8128
8129   if (in_statement_expr
8130       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8131     /* This is the final expression statement of a statement
8132        expression.  */
8133     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8134   else if (statement)
8135     statement = finish_expr_stmt (statement);
8136   else
8137     finish_stmt ();
8138
8139   return statement;
8140 }
8141
8142 /* Parse a compound-statement.
8143
8144    compound-statement:
8145      { statement-seq [opt] }
8146
8147    GNU extension:
8148
8149    compound-statement:
8150      { label-declaration-seq [opt] statement-seq [opt] }
8151
8152    label-declaration-seq:
8153      label-declaration
8154      label-declaration-seq label-declaration
8155
8156    Returns a tree representing the statement.  */
8157
8158 static tree
8159 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8160                               bool in_try, bool function_body)
8161 {
8162   tree compound_stmt;
8163
8164   /* Consume the `{'.  */
8165   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8166     return error_mark_node;
8167   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8168       && !function_body)
8169     pedwarn (input_location, OPT_pedantic,
8170              "compound-statement in constexpr function");
8171   /* Begin the compound-statement.  */
8172   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8173   /* If the next keyword is `__label__' we have a label declaration.  */
8174   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8175     cp_parser_label_declaration (parser);
8176   /* Parse an (optional) statement-seq.  */
8177   cp_parser_statement_seq_opt (parser, in_statement_expr);
8178   /* Finish the compound-statement.  */
8179   finish_compound_stmt (compound_stmt);
8180   /* Consume the `}'.  */
8181   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8182
8183   return compound_stmt;
8184 }
8185
8186 /* Parse an (optional) statement-seq.
8187
8188    statement-seq:
8189      statement
8190      statement-seq [opt] statement  */
8191
8192 static void
8193 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8194 {
8195   /* Scan statements until there aren't any more.  */
8196   while (true)
8197     {
8198       cp_token *token = cp_lexer_peek_token (parser->lexer);
8199
8200       /* If we are looking at a `}', then we have run out of
8201          statements; the same is true if we have reached the end
8202          of file, or have stumbled upon a stray '@end'.  */
8203       if (token->type == CPP_CLOSE_BRACE
8204           || token->type == CPP_EOF
8205           || token->type == CPP_PRAGMA_EOL
8206           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8207         break;
8208       
8209       /* If we are in a compound statement and find 'else' then
8210          something went wrong.  */
8211       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8212         {
8213           if (parser->in_statement & IN_IF_STMT) 
8214             break;
8215           else
8216             {
8217               token = cp_lexer_consume_token (parser->lexer);
8218               error_at (token->location, "%<else%> without a previous %<if%>");
8219             }
8220         }
8221
8222       /* Parse the statement.  */
8223       cp_parser_statement (parser, in_statement_expr, true, NULL);
8224     }
8225 }
8226
8227 /* Parse a selection-statement.
8228
8229    selection-statement:
8230      if ( condition ) statement
8231      if ( condition ) statement else statement
8232      switch ( condition ) statement
8233
8234    Returns the new IF_STMT or SWITCH_STMT.
8235
8236    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8237    is a (possibly labeled) if statement which is not enclosed in
8238    braces and has an else clause.  This is used to implement
8239    -Wparentheses.  */
8240
8241 static tree
8242 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8243 {
8244   cp_token *token;
8245   enum rid keyword;
8246
8247   if (if_p != NULL)
8248     *if_p = false;
8249
8250   /* Peek at the next token.  */
8251   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8252
8253   /* See what kind of keyword it is.  */
8254   keyword = token->keyword;
8255   switch (keyword)
8256     {
8257     case RID_IF:
8258     case RID_SWITCH:
8259       {
8260         tree statement;
8261         tree condition;
8262
8263         /* Look for the `('.  */
8264         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8265           {
8266             cp_parser_skip_to_end_of_statement (parser);
8267             return error_mark_node;
8268           }
8269
8270         /* Begin the selection-statement.  */
8271         if (keyword == RID_IF)
8272           statement = begin_if_stmt ();
8273         else
8274           statement = begin_switch_stmt ();
8275
8276         /* Parse the condition.  */
8277         condition = cp_parser_condition (parser);
8278         /* Look for the `)'.  */
8279         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8280           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8281                                                  /*consume_paren=*/true);
8282
8283         if (keyword == RID_IF)
8284           {
8285             bool nested_if;
8286             unsigned char in_statement;
8287
8288             /* Add the condition.  */
8289             finish_if_stmt_cond (condition, statement);
8290
8291             /* Parse the then-clause.  */
8292             in_statement = parser->in_statement;
8293             parser->in_statement |= IN_IF_STMT;
8294             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8295               {
8296                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8297                 add_stmt (build_empty_stmt (loc));
8298                 cp_lexer_consume_token (parser->lexer);
8299                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8300                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8301                               "empty body in an %<if%> statement");
8302                 nested_if = false;
8303               }
8304             else
8305               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8306             parser->in_statement = in_statement;
8307
8308             finish_then_clause (statement);
8309
8310             /* If the next token is `else', parse the else-clause.  */
8311             if (cp_lexer_next_token_is_keyword (parser->lexer,
8312                                                 RID_ELSE))
8313               {
8314                 /* Consume the `else' keyword.  */
8315                 cp_lexer_consume_token (parser->lexer);
8316                 begin_else_clause (statement);
8317                 /* Parse the else-clause.  */
8318                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8319                   {
8320                     location_t loc;
8321                     loc = cp_lexer_peek_token (parser->lexer)->location;
8322                     warning_at (loc,
8323                                 OPT_Wempty_body, "suggest braces around "
8324                                 "empty body in an %<else%> statement");
8325                     add_stmt (build_empty_stmt (loc));
8326                     cp_lexer_consume_token (parser->lexer);
8327                   }
8328                 else
8329                   cp_parser_implicitly_scoped_statement (parser, NULL);
8330
8331                 finish_else_clause (statement);
8332
8333                 /* If we are currently parsing a then-clause, then
8334                    IF_P will not be NULL.  We set it to true to
8335                    indicate that this if statement has an else clause.
8336                    This may trigger the Wparentheses warning below
8337                    when we get back up to the parent if statement.  */
8338                 if (if_p != NULL)
8339                   *if_p = true;
8340               }
8341             else
8342               {
8343                 /* This if statement does not have an else clause.  If
8344                    NESTED_IF is true, then the then-clause is an if
8345                    statement which does have an else clause.  We warn
8346                    about the potential ambiguity.  */
8347                 if (nested_if)
8348                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8349                               "suggest explicit braces to avoid ambiguous"
8350                               " %<else%>");
8351               }
8352
8353             /* Now we're all done with the if-statement.  */
8354             finish_if_stmt (statement);
8355           }
8356         else
8357           {
8358             bool in_switch_statement_p;
8359             unsigned char in_statement;
8360
8361             /* Add the condition.  */
8362             finish_switch_cond (condition, statement);
8363
8364             /* Parse the body of the switch-statement.  */
8365             in_switch_statement_p = parser->in_switch_statement_p;
8366             in_statement = parser->in_statement;
8367             parser->in_switch_statement_p = true;
8368             parser->in_statement |= IN_SWITCH_STMT;
8369             cp_parser_implicitly_scoped_statement (parser, NULL);
8370             parser->in_switch_statement_p = in_switch_statement_p;
8371             parser->in_statement = in_statement;
8372
8373             /* Now we're all done with the switch-statement.  */
8374             finish_switch_stmt (statement);
8375           }
8376
8377         return statement;
8378       }
8379       break;
8380
8381     default:
8382       cp_parser_error (parser, "expected selection-statement");
8383       return error_mark_node;
8384     }
8385 }
8386
8387 /* Parse a condition.
8388
8389    condition:
8390      expression
8391      type-specifier-seq declarator = initializer-clause
8392      type-specifier-seq declarator braced-init-list
8393
8394    GNU Extension:
8395
8396    condition:
8397      type-specifier-seq declarator asm-specification [opt]
8398        attributes [opt] = assignment-expression
8399
8400    Returns the expression that should be tested.  */
8401
8402 static tree
8403 cp_parser_condition (cp_parser* parser)
8404 {
8405   cp_decl_specifier_seq type_specifiers;
8406   const char *saved_message;
8407   int declares_class_or_enum;
8408
8409   /* Try the declaration first.  */
8410   cp_parser_parse_tentatively (parser);
8411   /* New types are not allowed in the type-specifier-seq for a
8412      condition.  */
8413   saved_message = parser->type_definition_forbidden_message;
8414   parser->type_definition_forbidden_message
8415     = G_("types may not be defined in conditions");
8416   /* Parse the type-specifier-seq.  */
8417   cp_parser_decl_specifier_seq (parser,
8418                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8419                                 &type_specifiers,
8420                                 &declares_class_or_enum);
8421   /* Restore the saved message.  */
8422   parser->type_definition_forbidden_message = saved_message;
8423   /* If all is well, we might be looking at a declaration.  */
8424   if (!cp_parser_error_occurred (parser))
8425     {
8426       tree decl;
8427       tree asm_specification;
8428       tree attributes;
8429       cp_declarator *declarator;
8430       tree initializer = NULL_TREE;
8431
8432       /* Parse the declarator.  */
8433       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8434                                          /*ctor_dtor_or_conv_p=*/NULL,
8435                                          /*parenthesized_p=*/NULL,
8436                                          /*member_p=*/false);
8437       /* Parse the attributes.  */
8438       attributes = cp_parser_attributes_opt (parser);
8439       /* Parse the asm-specification.  */
8440       asm_specification = cp_parser_asm_specification_opt (parser);
8441       /* If the next token is not an `=' or '{', then we might still be
8442          looking at an expression.  For example:
8443
8444            if (A(a).x)
8445
8446          looks like a decl-specifier-seq and a declarator -- but then
8447          there is no `=', so this is an expression.  */
8448       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8449           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8450         cp_parser_simulate_error (parser);
8451         
8452       /* If we did see an `=' or '{', then we are looking at a declaration
8453          for sure.  */
8454       if (cp_parser_parse_definitely (parser))
8455         {
8456           tree pushed_scope;
8457           bool non_constant_p;
8458           bool flags = LOOKUP_ONLYCONVERTING;
8459
8460           /* Create the declaration.  */
8461           decl = start_decl (declarator, &type_specifiers,
8462                              /*initialized_p=*/true,
8463                              attributes, /*prefix_attributes=*/NULL_TREE,
8464                              &pushed_scope);
8465
8466           /* Parse the initializer.  */
8467           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8468             {
8469               initializer = cp_parser_braced_list (parser, &non_constant_p);
8470               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8471               flags = 0;
8472             }
8473           else
8474             {
8475               /* Consume the `='.  */
8476               cp_parser_require (parser, CPP_EQ, RT_EQ);
8477               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8478             }
8479           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8480             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8481
8482           /* Process the initializer.  */
8483           cp_finish_decl (decl,
8484                           initializer, !non_constant_p,
8485                           asm_specification,
8486                           flags);
8487
8488           if (pushed_scope)
8489             pop_scope (pushed_scope);
8490
8491           return convert_from_reference (decl);
8492         }
8493     }
8494   /* If we didn't even get past the declarator successfully, we are
8495      definitely not looking at a declaration.  */
8496   else
8497     cp_parser_abort_tentative_parse (parser);
8498
8499   /* Otherwise, we are looking at an expression.  */
8500   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8501 }
8502
8503 /* Parses a for-statement or range-for-statement until the closing ')',
8504    not included. */
8505
8506 static tree
8507 cp_parser_for (cp_parser *parser)
8508 {
8509   tree init, scope, decl;
8510   bool is_range_for;
8511
8512   /* Begin the for-statement.  */
8513   scope = begin_for_scope (&init);
8514
8515   /* Parse the initialization.  */
8516   is_range_for = cp_parser_for_init_statement (parser, &decl);
8517
8518   if (is_range_for)
8519     return cp_parser_range_for (parser, scope, init, decl);
8520   else
8521     return cp_parser_c_for (parser, scope, init);
8522 }
8523
8524 static tree
8525 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8526 {
8527   /* Normal for loop */
8528   tree condition = NULL_TREE;
8529   tree expression = NULL_TREE;
8530   tree stmt;
8531
8532   stmt = begin_for_stmt (scope, init);
8533   /* The for-init-statement has already been parsed in
8534      cp_parser_for_init_statement, so no work is needed here.  */
8535   finish_for_init_stmt (stmt);
8536
8537   /* If there's a condition, process it.  */
8538   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8539     condition = cp_parser_condition (parser);
8540   finish_for_cond (condition, stmt);
8541   /* Look for the `;'.  */
8542   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8543
8544   /* If there's an expression, process it.  */
8545   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8546     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8547   finish_for_expr (expression, stmt);
8548
8549   return stmt;
8550 }
8551
8552 /* Tries to parse a range-based for-statement:
8553
8554   range-based-for:
8555     decl-specifier-seq declarator : expression
8556
8557   The decl-specifier-seq declarator and the `:' are already parsed by
8558   cp_parser_for_init_statement. If processing_template_decl it returns a
8559   newly created RANGE_FOR_STMT; if not, it is converted to a
8560   regular FOR_STMT.  */
8561
8562 static tree
8563 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8564 {
8565   tree stmt, range_expr;
8566
8567   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8568     {
8569       bool expr_non_constant_p;
8570       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8571     }
8572   else
8573     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8574
8575   /* If in template, STMT is converted to a normal for-statement
8576      at instantiation. If not, it is done just ahead. */
8577   if (processing_template_decl)
8578     {
8579       stmt = begin_range_for_stmt (scope, init);
8580       finish_range_for_decl (stmt, range_decl, range_expr);
8581     }
8582   else
8583     {
8584       stmt = begin_for_stmt (scope, init);
8585       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8586     }
8587   return stmt;
8588 }
8589
8590 /* Converts a range-based for-statement into a normal
8591    for-statement, as per the definition.
8592
8593       for (RANGE_DECL : RANGE_EXPR)
8594         BLOCK
8595
8596    should be equivalent to:
8597
8598       {
8599         auto &&__range = RANGE_EXPR;
8600         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8601               __begin != __end;
8602               ++__begin)
8603           {
8604               RANGE_DECL = *__begin;
8605               BLOCK
8606           }
8607       }
8608
8609    If RANGE_EXPR is an array:
8610         BEGIN_EXPR = __range
8611         END_EXPR = __range + ARRAY_SIZE(__range)
8612    Else if RANGE_EXPR has a member 'begin' or 'end':
8613         BEGIN_EXPR = __range.begin()
8614         END_EXPR = __range.end()
8615    Else:
8616         BEGIN_EXPR = begin(__range)
8617         END_EXPR = end(__range);
8618
8619    If __range has a member 'begin' but not 'end', or vice versa, we must
8620    still use the second alternative (it will surely fail, however).
8621    When calling begin()/end() in the third alternative we must use
8622    argument dependent lookup, but always considering 'std' as an associated
8623    namespace.  */
8624
8625 tree
8626 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8627 {
8628   tree range_type, range_temp;
8629   tree begin, end;
8630   tree iter_type, begin_expr, end_expr;
8631   tree condition, expression;
8632
8633   if (range_decl == error_mark_node || range_expr == error_mark_node)
8634     /* If an error happened previously do nothing or else a lot of
8635        unhelpful errors would be issued.  */
8636     begin_expr = end_expr = iter_type = error_mark_node;
8637   else
8638     {
8639       /* Find out the type deduced by the declaration
8640          `auto &&__range = range_expr'.  */
8641       range_type = cp_build_reference_type (make_auto (), true);
8642       range_type = do_auto_deduction (range_type, range_expr,
8643                                       type_uses_auto (range_type));
8644
8645       /* Create the __range variable.  */
8646       range_temp = build_decl (input_location, VAR_DECL,
8647                                get_identifier ("__for_range"), range_type);
8648       TREE_USED (range_temp) = 1;
8649       DECL_ARTIFICIAL (range_temp) = 1;
8650       pushdecl (range_temp);
8651       cp_finish_decl (range_temp, range_expr,
8652                       /*is_constant_init*/false, NULL_TREE,
8653                       LOOKUP_ONLYCONVERTING);
8654
8655       range_temp = convert_from_reference (range_temp);
8656       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8657                                                       &begin_expr, &end_expr);
8658     }
8659
8660   /* The new for initialization statement.  */
8661   begin = build_decl (input_location, VAR_DECL,
8662                       get_identifier ("__for_begin"), iter_type);
8663   TREE_USED (begin) = 1;
8664   DECL_ARTIFICIAL (begin) = 1;
8665   pushdecl (begin);
8666   cp_finish_decl (begin, begin_expr,
8667                   /*is_constant_init*/false, NULL_TREE,
8668                   LOOKUP_ONLYCONVERTING);
8669
8670   end = build_decl (input_location, VAR_DECL,
8671                     get_identifier ("__for_end"), iter_type);
8672   TREE_USED (end) = 1;
8673   DECL_ARTIFICIAL (end) = 1;
8674   pushdecl (end);
8675   cp_finish_decl (end, end_expr,
8676                   /*is_constant_init*/false, NULL_TREE,
8677                   LOOKUP_ONLYCONVERTING);
8678
8679   finish_for_init_stmt (statement);
8680
8681   /* The new for condition.  */
8682   condition = build_x_binary_op (NE_EXPR,
8683                                  begin, ERROR_MARK,
8684                                  end, ERROR_MARK,
8685                                  NULL, tf_warning_or_error);
8686   finish_for_cond (condition, statement);
8687
8688   /* The new increment expression.  */
8689   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8690   finish_for_expr (expression, statement);
8691
8692   /* The declaration is initialized with *__begin inside the loop body.  */
8693   cp_finish_decl (range_decl,
8694                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8695                   /*is_constant_init*/false, NULL_TREE,
8696                   LOOKUP_ONLYCONVERTING);
8697
8698   return statement;
8699 }
8700
8701 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8702    We need to solve both at the same time because the method used
8703    depends on the existence of members begin or end.
8704    Returns the type deduced for the iterator expression.  */
8705
8706 static tree
8707 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8708 {
8709   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8710     {
8711       error ("range-based %<for%> expression of type %qT "
8712              "has incomplete type", TREE_TYPE (range));
8713       *begin = *end = error_mark_node;
8714       return error_mark_node;
8715     }
8716   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8717     {
8718       /* If RANGE is an array, we will use pointer arithmetic.  */
8719       *begin = range;
8720       *end = build_binary_op (input_location, PLUS_EXPR,
8721                               range,
8722                               array_type_nelts_top (TREE_TYPE (range)),
8723                               0);
8724       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8725     }
8726   else
8727     {
8728       /* If it is not an array, we must do a bit of magic.  */
8729       tree id_begin, id_end;
8730       tree member_begin, member_end;
8731
8732       *begin = *end = error_mark_node;
8733
8734       id_begin = get_identifier ("begin");
8735       id_end = get_identifier ("end");
8736       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8737                                     /*protect=*/2, /*want_type=*/false);
8738       member_end = lookup_member (TREE_TYPE (range), id_end,
8739                                   /*protect=*/2, /*want_type=*/false);
8740
8741       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8742         {
8743           /* Use the member functions.  */
8744           if (member_begin != NULL_TREE)
8745             *begin = cp_parser_range_for_member_function (range, id_begin);
8746           else
8747             error ("range-based %<for%> expression of type %qT has an "
8748                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8749
8750           if (member_end != NULL_TREE)
8751             *end = cp_parser_range_for_member_function (range, id_end);
8752           else
8753             error ("range-based %<for%> expression of type %qT has a "
8754                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8755         }
8756       else
8757         {
8758           /* Use global functions with ADL.  */
8759           VEC(tree,gc) *vec;
8760           vec = make_tree_vector ();
8761
8762           VEC_safe_push (tree, gc, vec, range);
8763
8764           member_begin = perform_koenig_lookup (id_begin, vec,
8765                                                 /*include_std=*/true,
8766                                                 tf_warning_or_error);
8767           *begin = finish_call_expr (member_begin, &vec, false, true,
8768                                      tf_warning_or_error);
8769           member_end = perform_koenig_lookup (id_end, vec,
8770                                               /*include_std=*/true,
8771                                               tf_warning_or_error);
8772           *end = finish_call_expr (member_end, &vec, false, true,
8773                                    tf_warning_or_error);
8774
8775           release_tree_vector (vec);
8776         }
8777
8778       /* Last common checks.  */
8779       if (*begin == error_mark_node || *end == error_mark_node)
8780         {
8781           /* If one of the expressions is an error do no more checks.  */
8782           *begin = *end = error_mark_node;
8783           return error_mark_node;
8784         }
8785       else
8786         {
8787           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8788           /* The unqualified type of the __begin and __end temporaries should
8789              be the same, as required by the multiple auto declaration.  */
8790           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8791             error ("inconsistent begin/end types in range-based %<for%> "
8792                    "statement: %qT and %qT",
8793                    TREE_TYPE (*begin), TREE_TYPE (*end));
8794           return iter_type;
8795         }
8796     }
8797 }
8798
8799 /* Helper function for cp_parser_perform_range_for_lookup.
8800    Builds a tree for RANGE.IDENTIFIER().  */
8801
8802 static tree
8803 cp_parser_range_for_member_function (tree range, tree identifier)
8804 {
8805   tree member, res;
8806   VEC(tree,gc) *vec;
8807
8808   member = finish_class_member_access_expr (range, identifier,
8809                                             false, tf_warning_or_error);
8810   if (member == error_mark_node)
8811     return error_mark_node;
8812
8813   vec = make_tree_vector ();
8814   res = finish_call_expr (member, &vec,
8815                           /*disallow_virtual=*/false,
8816                           /*koenig_p=*/false,
8817                           tf_warning_or_error);
8818   release_tree_vector (vec);
8819   return res;
8820 }
8821
8822 /* Parse an iteration-statement.
8823
8824    iteration-statement:
8825      while ( condition ) statement
8826      do statement while ( expression ) ;
8827      for ( for-init-statement condition [opt] ; expression [opt] )
8828        statement
8829
8830    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8831
8832 static tree
8833 cp_parser_iteration_statement (cp_parser* parser)
8834 {
8835   cp_token *token;
8836   enum rid keyword;
8837   tree statement;
8838   unsigned char in_statement;
8839
8840   /* Peek at the next token.  */
8841   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8842   if (!token)
8843     return error_mark_node;
8844
8845   /* Remember whether or not we are already within an iteration
8846      statement.  */
8847   in_statement = parser->in_statement;
8848
8849   /* See what kind of keyword it is.  */
8850   keyword = token->keyword;
8851   switch (keyword)
8852     {
8853     case RID_WHILE:
8854       {
8855         tree condition;
8856
8857         /* Begin the while-statement.  */
8858         statement = begin_while_stmt ();
8859         /* Look for the `('.  */
8860         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8861         /* Parse the condition.  */
8862         condition = cp_parser_condition (parser);
8863         finish_while_stmt_cond (condition, statement);
8864         /* Look for the `)'.  */
8865         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8866         /* Parse the dependent statement.  */
8867         parser->in_statement = IN_ITERATION_STMT;
8868         cp_parser_already_scoped_statement (parser);
8869         parser->in_statement = in_statement;
8870         /* We're done with the while-statement.  */
8871         finish_while_stmt (statement);
8872       }
8873       break;
8874
8875     case RID_DO:
8876       {
8877         tree expression;
8878
8879         /* Begin the do-statement.  */
8880         statement = begin_do_stmt ();
8881         /* Parse the body of the do-statement.  */
8882         parser->in_statement = IN_ITERATION_STMT;
8883         cp_parser_implicitly_scoped_statement (parser, NULL);
8884         parser->in_statement = in_statement;
8885         finish_do_body (statement);
8886         /* Look for the `while' keyword.  */
8887         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8888         /* Look for the `('.  */
8889         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8890         /* Parse the expression.  */
8891         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8892         /* We're done with the do-statement.  */
8893         finish_do_stmt (expression, statement);
8894         /* Look for the `)'.  */
8895         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8896         /* Look for the `;'.  */
8897         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8898       }
8899       break;
8900
8901     case RID_FOR:
8902       {
8903         /* Look for the `('.  */
8904         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8905
8906         statement = cp_parser_for (parser);
8907
8908         /* Look for the `)'.  */
8909         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8910
8911         /* Parse the body of the for-statement.  */
8912         parser->in_statement = IN_ITERATION_STMT;
8913         cp_parser_already_scoped_statement (parser);
8914         parser->in_statement = in_statement;
8915
8916         /* We're done with the for-statement.  */
8917         finish_for_stmt (statement);
8918       }
8919       break;
8920
8921     default:
8922       cp_parser_error (parser, "expected iteration-statement");
8923       statement = error_mark_node;
8924       break;
8925     }
8926
8927   return statement;
8928 }
8929
8930 /* Parse a for-init-statement or the declarator of a range-based-for.
8931    Returns true if a range-based-for declaration is seen.
8932
8933    for-init-statement:
8934      expression-statement
8935      simple-declaration  */
8936
8937 static bool
8938 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8939 {
8940   /* If the next token is a `;', then we have an empty
8941      expression-statement.  Grammatically, this is also a
8942      simple-declaration, but an invalid one, because it does not
8943      declare anything.  Therefore, if we did not handle this case
8944      specially, we would issue an error message about an invalid
8945      declaration.  */
8946   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8947     {
8948       bool is_range_for = false;
8949       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8950
8951       parser->colon_corrects_to_scope_p = false;
8952
8953       /* We're going to speculatively look for a declaration, falling back
8954          to an expression, if necessary.  */
8955       cp_parser_parse_tentatively (parser);
8956       /* Parse the declaration.  */
8957       cp_parser_simple_declaration (parser,
8958                                     /*function_definition_allowed_p=*/false,
8959                                     decl);
8960       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8961       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8962         {
8963           /* It is a range-for, consume the ':' */
8964           cp_lexer_consume_token (parser->lexer);
8965           is_range_for = true;
8966           if (cxx_dialect < cxx0x)
8967             {
8968               error_at (cp_lexer_peek_token (parser->lexer)->location,
8969                         "range-based %<for%> loops are not allowed "
8970                         "in C++98 mode");
8971               *decl = error_mark_node;
8972             }
8973         }
8974       else
8975           /* The ';' is not consumed yet because we told
8976              cp_parser_simple_declaration not to.  */
8977           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8978
8979       if (cp_parser_parse_definitely (parser))
8980         return is_range_for;
8981       /* If the tentative parse failed, then we shall need to look for an
8982          expression-statement.  */
8983     }
8984   /* If we are here, it is an expression-statement.  */
8985   cp_parser_expression_statement (parser, NULL_TREE);
8986   return false;
8987 }
8988
8989 /* Parse a jump-statement.
8990
8991    jump-statement:
8992      break ;
8993      continue ;
8994      return expression [opt] ;
8995      return braced-init-list ;
8996      goto identifier ;
8997
8998    GNU extension:
8999
9000    jump-statement:
9001      goto * expression ;
9002
9003    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9004
9005 static tree
9006 cp_parser_jump_statement (cp_parser* parser)
9007 {
9008   tree statement = error_mark_node;
9009   cp_token *token;
9010   enum rid keyword;
9011   unsigned char in_statement;
9012
9013   /* Peek at the next token.  */
9014   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9015   if (!token)
9016     return error_mark_node;
9017
9018   /* See what kind of keyword it is.  */
9019   keyword = token->keyword;
9020   switch (keyword)
9021     {
9022     case RID_BREAK:
9023       in_statement = parser->in_statement & ~IN_IF_STMT;      
9024       switch (in_statement)
9025         {
9026         case 0:
9027           error_at (token->location, "break statement not within loop or switch");
9028           break;
9029         default:
9030           gcc_assert ((in_statement & IN_SWITCH_STMT)
9031                       || in_statement == IN_ITERATION_STMT);
9032           statement = finish_break_stmt ();
9033           break;
9034         case IN_OMP_BLOCK:
9035           error_at (token->location, "invalid exit from OpenMP structured block");
9036           break;
9037         case IN_OMP_FOR:
9038           error_at (token->location, "break statement used with OpenMP for loop");
9039           break;
9040         }
9041       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9042       break;
9043
9044     case RID_CONTINUE:
9045       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9046         {
9047         case 0:
9048           error_at (token->location, "continue statement not within a loop");
9049           break;
9050         case IN_ITERATION_STMT:
9051         case IN_OMP_FOR:
9052           statement = finish_continue_stmt ();
9053           break;
9054         case IN_OMP_BLOCK:
9055           error_at (token->location, "invalid exit from OpenMP structured block");
9056           break;
9057         default:
9058           gcc_unreachable ();
9059         }
9060       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9061       break;
9062
9063     case RID_RETURN:
9064       {
9065         tree expr;
9066         bool expr_non_constant_p;
9067
9068         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9069           {
9070             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9071             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9072           }
9073         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9074           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9075         else
9076           /* If the next token is a `;', then there is no
9077              expression.  */
9078           expr = NULL_TREE;
9079         /* Build the return-statement.  */
9080         statement = finish_return_stmt (expr);
9081         /* Look for the final `;'.  */
9082         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9083       }
9084       break;
9085
9086     case RID_GOTO:
9087       /* Create the goto-statement.  */
9088       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9089         {
9090           /* Issue a warning about this use of a GNU extension.  */
9091           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9092           /* Consume the '*' token.  */
9093           cp_lexer_consume_token (parser->lexer);
9094           /* Parse the dependent expression.  */
9095           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9096         }
9097       else
9098         finish_goto_stmt (cp_parser_identifier (parser));
9099       /* Look for the final `;'.  */
9100       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9101       break;
9102
9103     default:
9104       cp_parser_error (parser, "expected jump-statement");
9105       break;
9106     }
9107
9108   return statement;
9109 }
9110
9111 /* Parse a declaration-statement.
9112
9113    declaration-statement:
9114      block-declaration  */
9115
9116 static void
9117 cp_parser_declaration_statement (cp_parser* parser)
9118 {
9119   void *p;
9120
9121   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9122   p = obstack_alloc (&declarator_obstack, 0);
9123
9124  /* Parse the block-declaration.  */
9125   cp_parser_block_declaration (parser, /*statement_p=*/true);
9126
9127   /* Free any declarators allocated.  */
9128   obstack_free (&declarator_obstack, p);
9129
9130   /* Finish off the statement.  */
9131   finish_stmt ();
9132 }
9133
9134 /* Some dependent statements (like `if (cond) statement'), are
9135    implicitly in their own scope.  In other words, if the statement is
9136    a single statement (as opposed to a compound-statement), it is
9137    none-the-less treated as if it were enclosed in braces.  Any
9138    declarations appearing in the dependent statement are out of scope
9139    after control passes that point.  This function parses a statement,
9140    but ensures that is in its own scope, even if it is not a
9141    compound-statement.
9142
9143    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9144    is a (possibly labeled) if statement which is not enclosed in
9145    braces and has an else clause.  This is used to implement
9146    -Wparentheses.
9147
9148    Returns the new statement.  */
9149
9150 static tree
9151 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9152 {
9153   tree statement;
9154
9155   if (if_p != NULL)
9156     *if_p = false;
9157
9158   /* Mark if () ; with a special NOP_EXPR.  */
9159   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9160     {
9161       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9162       cp_lexer_consume_token (parser->lexer);
9163       statement = add_stmt (build_empty_stmt (loc));
9164     }
9165   /* if a compound is opened, we simply parse the statement directly.  */
9166   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9167     statement = cp_parser_compound_statement (parser, NULL, false, false);
9168   /* If the token is not a `{', then we must take special action.  */
9169   else
9170     {
9171       /* Create a compound-statement.  */
9172       statement = begin_compound_stmt (0);
9173       /* Parse the dependent-statement.  */
9174       cp_parser_statement (parser, NULL_TREE, false, if_p);
9175       /* Finish the dummy compound-statement.  */
9176       finish_compound_stmt (statement);
9177     }
9178
9179   /* Return the statement.  */
9180   return statement;
9181 }
9182
9183 /* For some dependent statements (like `while (cond) statement'), we
9184    have already created a scope.  Therefore, even if the dependent
9185    statement is a compound-statement, we do not want to create another
9186    scope.  */
9187
9188 static void
9189 cp_parser_already_scoped_statement (cp_parser* parser)
9190 {
9191   /* If the token is a `{', then we must take special action.  */
9192   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9193     cp_parser_statement (parser, NULL_TREE, false, NULL);
9194   else
9195     {
9196       /* Avoid calling cp_parser_compound_statement, so that we
9197          don't create a new scope.  Do everything else by hand.  */
9198       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9199       /* If the next keyword is `__label__' we have a label declaration.  */
9200       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9201         cp_parser_label_declaration (parser);
9202       /* Parse an (optional) statement-seq.  */
9203       cp_parser_statement_seq_opt (parser, NULL_TREE);
9204       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9205     }
9206 }
9207
9208 /* Declarations [gram.dcl.dcl] */
9209
9210 /* Parse an optional declaration-sequence.
9211
9212    declaration-seq:
9213      declaration
9214      declaration-seq declaration  */
9215
9216 static void
9217 cp_parser_declaration_seq_opt (cp_parser* parser)
9218 {
9219   while (true)
9220     {
9221       cp_token *token;
9222
9223       token = cp_lexer_peek_token (parser->lexer);
9224
9225       if (token->type == CPP_CLOSE_BRACE
9226           || token->type == CPP_EOF
9227           || token->type == CPP_PRAGMA_EOL)
9228         break;
9229
9230       if (token->type == CPP_SEMICOLON)
9231         {
9232           /* A declaration consisting of a single semicolon is
9233              invalid.  Allow it unless we're being pedantic.  */
9234           cp_lexer_consume_token (parser->lexer);
9235           if (!in_system_header)
9236             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9237           continue;
9238         }
9239
9240       /* If we're entering or exiting a region that's implicitly
9241          extern "C", modify the lang context appropriately.  */
9242       if (!parser->implicit_extern_c && token->implicit_extern_c)
9243         {
9244           push_lang_context (lang_name_c);
9245           parser->implicit_extern_c = true;
9246         }
9247       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9248         {
9249           pop_lang_context ();
9250           parser->implicit_extern_c = false;
9251         }
9252
9253       if (token->type == CPP_PRAGMA)
9254         {
9255           /* A top-level declaration can consist solely of a #pragma.
9256              A nested declaration cannot, so this is done here and not
9257              in cp_parser_declaration.  (A #pragma at block scope is
9258              handled in cp_parser_statement.)  */
9259           cp_parser_pragma (parser, pragma_external);
9260           continue;
9261         }
9262
9263       /* Parse the declaration itself.  */
9264       cp_parser_declaration (parser);
9265     }
9266 }
9267
9268 /* Parse a declaration.
9269
9270    declaration:
9271      block-declaration
9272      function-definition
9273      template-declaration
9274      explicit-instantiation
9275      explicit-specialization
9276      linkage-specification
9277      namespace-definition
9278
9279    GNU extension:
9280
9281    declaration:
9282       __extension__ declaration */
9283
9284 static void
9285 cp_parser_declaration (cp_parser* parser)
9286 {
9287   cp_token token1;
9288   cp_token token2;
9289   int saved_pedantic;
9290   void *p;
9291   tree attributes = NULL_TREE;
9292
9293   /* Check for the `__extension__' keyword.  */
9294   if (cp_parser_extension_opt (parser, &saved_pedantic))
9295     {
9296       /* Parse the qualified declaration.  */
9297       cp_parser_declaration (parser);
9298       /* Restore the PEDANTIC flag.  */
9299       pedantic = saved_pedantic;
9300
9301       return;
9302     }
9303
9304   /* Try to figure out what kind of declaration is present.  */
9305   token1 = *cp_lexer_peek_token (parser->lexer);
9306
9307   if (token1.type != CPP_EOF)
9308     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9309   else
9310     {
9311       token2.type = CPP_EOF;
9312       token2.keyword = RID_MAX;
9313     }
9314
9315   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9316   p = obstack_alloc (&declarator_obstack, 0);
9317
9318   /* If the next token is `extern' and the following token is a string
9319      literal, then we have a linkage specification.  */
9320   if (token1.keyword == RID_EXTERN
9321       && cp_parser_is_string_literal (&token2))
9322     cp_parser_linkage_specification (parser);
9323   /* If the next token is `template', then we have either a template
9324      declaration, an explicit instantiation, or an explicit
9325      specialization.  */
9326   else if (token1.keyword == RID_TEMPLATE)
9327     {
9328       /* `template <>' indicates a template specialization.  */
9329       if (token2.type == CPP_LESS
9330           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9331         cp_parser_explicit_specialization (parser);
9332       /* `template <' indicates a template declaration.  */
9333       else if (token2.type == CPP_LESS)
9334         cp_parser_template_declaration (parser, /*member_p=*/false);
9335       /* Anything else must be an explicit instantiation.  */
9336       else
9337         cp_parser_explicit_instantiation (parser);
9338     }
9339   /* If the next token is `export', then we have a template
9340      declaration.  */
9341   else if (token1.keyword == RID_EXPORT)
9342     cp_parser_template_declaration (parser, /*member_p=*/false);
9343   /* If the next token is `extern', 'static' or 'inline' and the one
9344      after that is `template', we have a GNU extended explicit
9345      instantiation directive.  */
9346   else if (cp_parser_allow_gnu_extensions_p (parser)
9347            && (token1.keyword == RID_EXTERN
9348                || token1.keyword == RID_STATIC
9349                || token1.keyword == RID_INLINE)
9350            && token2.keyword == RID_TEMPLATE)
9351     cp_parser_explicit_instantiation (parser);
9352   /* If the next token is `namespace', check for a named or unnamed
9353      namespace definition.  */
9354   else if (token1.keyword == RID_NAMESPACE
9355            && (/* A named namespace definition.  */
9356                (token2.type == CPP_NAME
9357                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9358                     != CPP_EQ))
9359                /* An unnamed namespace definition.  */
9360                || token2.type == CPP_OPEN_BRACE
9361                || token2.keyword == RID_ATTRIBUTE))
9362     cp_parser_namespace_definition (parser);
9363   /* An inline (associated) namespace definition.  */
9364   else if (token1.keyword == RID_INLINE
9365            && token2.keyword == RID_NAMESPACE)
9366     cp_parser_namespace_definition (parser);
9367   /* Objective-C++ declaration/definition.  */
9368   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9369     cp_parser_objc_declaration (parser, NULL_TREE);
9370   else if (c_dialect_objc ()
9371            && token1.keyword == RID_ATTRIBUTE
9372            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9373     cp_parser_objc_declaration (parser, attributes);
9374   /* We must have either a block declaration or a function
9375      definition.  */
9376   else
9377     /* Try to parse a block-declaration, or a function-definition.  */
9378     cp_parser_block_declaration (parser, /*statement_p=*/false);
9379
9380   /* Free any declarators allocated.  */
9381   obstack_free (&declarator_obstack, p);
9382 }
9383
9384 /* Parse a block-declaration.
9385
9386    block-declaration:
9387      simple-declaration
9388      asm-definition
9389      namespace-alias-definition
9390      using-declaration
9391      using-directive
9392
9393    GNU Extension:
9394
9395    block-declaration:
9396      __extension__ block-declaration
9397
9398    C++0x Extension:
9399
9400    block-declaration:
9401      static_assert-declaration
9402
9403    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9404    part of a declaration-statement.  */
9405
9406 static void
9407 cp_parser_block_declaration (cp_parser *parser,
9408                              bool      statement_p)
9409 {
9410   cp_token *token1;
9411   int saved_pedantic;
9412
9413   /* Check for the `__extension__' keyword.  */
9414   if (cp_parser_extension_opt (parser, &saved_pedantic))
9415     {
9416       /* Parse the qualified declaration.  */
9417       cp_parser_block_declaration (parser, statement_p);
9418       /* Restore the PEDANTIC flag.  */
9419       pedantic = saved_pedantic;
9420
9421       return;
9422     }
9423
9424   /* Peek at the next token to figure out which kind of declaration is
9425      present.  */
9426   token1 = cp_lexer_peek_token (parser->lexer);
9427
9428   /* If the next keyword is `asm', we have an asm-definition.  */
9429   if (token1->keyword == RID_ASM)
9430     {
9431       if (statement_p)
9432         cp_parser_commit_to_tentative_parse (parser);
9433       cp_parser_asm_definition (parser);
9434     }
9435   /* If the next keyword is `namespace', we have a
9436      namespace-alias-definition.  */
9437   else if (token1->keyword == RID_NAMESPACE)
9438     cp_parser_namespace_alias_definition (parser);
9439   /* If the next keyword is `using', we have either a
9440      using-declaration or a using-directive.  */
9441   else if (token1->keyword == RID_USING)
9442     {
9443       cp_token *token2;
9444
9445       if (statement_p)
9446         cp_parser_commit_to_tentative_parse (parser);
9447       /* If the token after `using' is `namespace', then we have a
9448          using-directive.  */
9449       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9450       if (token2->keyword == RID_NAMESPACE)
9451         cp_parser_using_directive (parser);
9452       /* Otherwise, it's a using-declaration.  */
9453       else
9454         cp_parser_using_declaration (parser,
9455                                      /*access_declaration_p=*/false);
9456     }
9457   /* If the next keyword is `__label__' we have a misplaced label
9458      declaration.  */
9459   else if (token1->keyword == RID_LABEL)
9460     {
9461       cp_lexer_consume_token (parser->lexer);
9462       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9463       cp_parser_skip_to_end_of_statement (parser);
9464       /* If the next token is now a `;', consume it.  */
9465       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9466         cp_lexer_consume_token (parser->lexer);
9467     }
9468   /* If the next token is `static_assert' we have a static assertion.  */
9469   else if (token1->keyword == RID_STATIC_ASSERT)
9470     cp_parser_static_assert (parser, /*member_p=*/false);
9471   /* Anything else must be a simple-declaration.  */
9472   else
9473     cp_parser_simple_declaration (parser, !statement_p,
9474                                   /*maybe_range_for_decl*/NULL);
9475 }
9476
9477 /* Parse a simple-declaration.
9478
9479    simple-declaration:
9480      decl-specifier-seq [opt] init-declarator-list [opt] ;
9481
9482    init-declarator-list:
9483      init-declarator
9484      init-declarator-list , init-declarator
9485
9486    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9487    function-definition as a simple-declaration.
9488
9489    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9490    parsed declaration if it is an uninitialized single declarator not followed
9491    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9492    if present, will not be consumed.  */
9493
9494 static void
9495 cp_parser_simple_declaration (cp_parser* parser,
9496                               bool function_definition_allowed_p,
9497                               tree *maybe_range_for_decl)
9498 {
9499   cp_decl_specifier_seq decl_specifiers;
9500   int declares_class_or_enum;
9501   bool saw_declarator;
9502
9503   if (maybe_range_for_decl)
9504     *maybe_range_for_decl = NULL_TREE;
9505
9506   /* Defer access checks until we know what is being declared; the
9507      checks for names appearing in the decl-specifier-seq should be
9508      done as if we were in the scope of the thing being declared.  */
9509   push_deferring_access_checks (dk_deferred);
9510
9511   /* Parse the decl-specifier-seq.  We have to keep track of whether
9512      or not the decl-specifier-seq declares a named class or
9513      enumeration type, since that is the only case in which the
9514      init-declarator-list is allowed to be empty.
9515
9516      [dcl.dcl]
9517
9518      In a simple-declaration, the optional init-declarator-list can be
9519      omitted only when declaring a class or enumeration, that is when
9520      the decl-specifier-seq contains either a class-specifier, an
9521      elaborated-type-specifier, or an enum-specifier.  */
9522   cp_parser_decl_specifier_seq (parser,
9523                                 CP_PARSER_FLAGS_OPTIONAL,
9524                                 &decl_specifiers,
9525                                 &declares_class_or_enum);
9526   /* We no longer need to defer access checks.  */
9527   stop_deferring_access_checks ();
9528
9529   /* In a block scope, a valid declaration must always have a
9530      decl-specifier-seq.  By not trying to parse declarators, we can
9531      resolve the declaration/expression ambiguity more quickly.  */
9532   if (!function_definition_allowed_p
9533       && !decl_specifiers.any_specifiers_p)
9534     {
9535       cp_parser_error (parser, "expected declaration");
9536       goto done;
9537     }
9538
9539   /* If the next two tokens are both identifiers, the code is
9540      erroneous. The usual cause of this situation is code like:
9541
9542        T t;
9543
9544      where "T" should name a type -- but does not.  */
9545   if (!decl_specifiers.any_type_specifiers_p
9546       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9547     {
9548       /* If parsing tentatively, we should commit; we really are
9549          looking at a declaration.  */
9550       cp_parser_commit_to_tentative_parse (parser);
9551       /* Give up.  */
9552       goto done;
9553     }
9554
9555   /* If we have seen at least one decl-specifier, and the next token
9556      is not a parenthesis, then we must be looking at a declaration.
9557      (After "int (" we might be looking at a functional cast.)  */
9558   if (decl_specifiers.any_specifiers_p
9559       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9560       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9561       && !cp_parser_error_occurred (parser))
9562     cp_parser_commit_to_tentative_parse (parser);
9563
9564   /* Keep going until we hit the `;' at the end of the simple
9565      declaration.  */
9566   saw_declarator = false;
9567   while (cp_lexer_next_token_is_not (parser->lexer,
9568                                      CPP_SEMICOLON))
9569     {
9570       cp_token *token;
9571       bool function_definition_p;
9572       tree decl;
9573
9574       if (saw_declarator)
9575         {
9576           /* If we are processing next declarator, coma is expected */
9577           token = cp_lexer_peek_token (parser->lexer);
9578           gcc_assert (token->type == CPP_COMMA);
9579           cp_lexer_consume_token (parser->lexer);
9580           if (maybe_range_for_decl)
9581             *maybe_range_for_decl = error_mark_node;
9582         }
9583       else
9584         saw_declarator = true;
9585
9586       /* Parse the init-declarator.  */
9587       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9588                                         /*checks=*/NULL,
9589                                         function_definition_allowed_p,
9590                                         /*member_p=*/false,
9591                                         declares_class_or_enum,
9592                                         &function_definition_p,
9593                                         maybe_range_for_decl);
9594       /* If an error occurred while parsing tentatively, exit quickly.
9595          (That usually happens when in the body of a function; each
9596          statement is treated as a declaration-statement until proven
9597          otherwise.)  */
9598       if (cp_parser_error_occurred (parser))
9599         goto done;
9600       /* Handle function definitions specially.  */
9601       if (function_definition_p)
9602         {
9603           /* If the next token is a `,', then we are probably
9604              processing something like:
9605
9606                void f() {}, *p;
9607
9608              which is erroneous.  */
9609           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9610             {
9611               cp_token *token = cp_lexer_peek_token (parser->lexer);
9612               error_at (token->location,
9613                         "mixing"
9614                         " declarations and function-definitions is forbidden");
9615             }
9616           /* Otherwise, we're done with the list of declarators.  */
9617           else
9618             {
9619               pop_deferring_access_checks ();
9620               return;
9621             }
9622         }
9623       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9624         *maybe_range_for_decl = decl;
9625       /* The next token should be either a `,' or a `;'.  */
9626       token = cp_lexer_peek_token (parser->lexer);
9627       /* If it's a `,', there are more declarators to come.  */
9628       if (token->type == CPP_COMMA)
9629         /* will be consumed next time around */;
9630       /* If it's a `;', we are done.  */
9631       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9632         break;
9633       /* Anything else is an error.  */
9634       else
9635         {
9636           /* If we have already issued an error message we don't need
9637              to issue another one.  */
9638           if (decl != error_mark_node
9639               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9640             cp_parser_error (parser, "expected %<,%> or %<;%>");
9641           /* Skip tokens until we reach the end of the statement.  */
9642           cp_parser_skip_to_end_of_statement (parser);
9643           /* If the next token is now a `;', consume it.  */
9644           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9645             cp_lexer_consume_token (parser->lexer);
9646           goto done;
9647         }
9648       /* After the first time around, a function-definition is not
9649          allowed -- even if it was OK at first.  For example:
9650
9651            int i, f() {}
9652
9653          is not valid.  */
9654       function_definition_allowed_p = false;
9655     }
9656
9657   /* Issue an error message if no declarators are present, and the
9658      decl-specifier-seq does not itself declare a class or
9659      enumeration.  */
9660   if (!saw_declarator)
9661     {
9662       if (cp_parser_declares_only_class_p (parser))
9663         shadow_tag (&decl_specifiers);
9664       /* Perform any deferred access checks.  */
9665       perform_deferred_access_checks ();
9666     }
9667
9668   /* Consume the `;'.  */
9669   if (!maybe_range_for_decl)
9670       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9671
9672  done:
9673   pop_deferring_access_checks ();
9674 }
9675
9676 /* Parse a decl-specifier-seq.
9677
9678    decl-specifier-seq:
9679      decl-specifier-seq [opt] decl-specifier
9680
9681    decl-specifier:
9682      storage-class-specifier
9683      type-specifier
9684      function-specifier
9685      friend
9686      typedef
9687
9688    GNU Extension:
9689
9690    decl-specifier:
9691      attributes
9692
9693    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9694
9695    The parser flags FLAGS is used to control type-specifier parsing.
9696
9697    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9698    flags:
9699
9700      1: one of the decl-specifiers is an elaborated-type-specifier
9701         (i.e., a type declaration)
9702      2: one of the decl-specifiers is an enum-specifier or a
9703         class-specifier (i.e., a type definition)
9704
9705    */
9706
9707 static void
9708 cp_parser_decl_specifier_seq (cp_parser* parser,
9709                               cp_parser_flags flags,
9710                               cp_decl_specifier_seq *decl_specs,
9711                               int* declares_class_or_enum)
9712 {
9713   bool constructor_possible_p = !parser->in_declarator_p;
9714   cp_token *start_token = NULL;
9715
9716   /* Clear DECL_SPECS.  */
9717   clear_decl_specs (decl_specs);
9718
9719   /* Assume no class or enumeration type is declared.  */
9720   *declares_class_or_enum = 0;
9721
9722   /* Keep reading specifiers until there are no more to read.  */
9723   while (true)
9724     {
9725       bool constructor_p;
9726       bool found_decl_spec;
9727       cp_token *token;
9728
9729       /* Peek at the next token.  */
9730       token = cp_lexer_peek_token (parser->lexer);
9731
9732       /* Save the first token of the decl spec list for error
9733          reporting.  */
9734       if (!start_token)
9735         start_token = token;
9736       /* Handle attributes.  */
9737       if (token->keyword == RID_ATTRIBUTE)
9738         {
9739           /* Parse the attributes.  */
9740           decl_specs->attributes
9741             = chainon (decl_specs->attributes,
9742                        cp_parser_attributes_opt (parser));
9743           continue;
9744         }
9745       /* Assume we will find a decl-specifier keyword.  */
9746       found_decl_spec = true;
9747       /* If the next token is an appropriate keyword, we can simply
9748          add it to the list.  */
9749       switch (token->keyword)
9750         {
9751           /* decl-specifier:
9752                friend
9753                constexpr */
9754         case RID_FRIEND:
9755           if (!at_class_scope_p ())
9756             {
9757               error_at (token->location, "%<friend%> used outside of class");
9758               cp_lexer_purge_token (parser->lexer);
9759             }
9760           else
9761             {
9762               ++decl_specs->specs[(int) ds_friend];
9763               /* Consume the token.  */
9764               cp_lexer_consume_token (parser->lexer);
9765             }
9766           break;
9767
9768         case RID_CONSTEXPR:
9769           ++decl_specs->specs[(int) ds_constexpr];
9770           cp_lexer_consume_token (parser->lexer);
9771           break;
9772
9773           /* function-specifier:
9774                inline
9775                virtual
9776                explicit  */
9777         case RID_INLINE:
9778         case RID_VIRTUAL:
9779         case RID_EXPLICIT:
9780           cp_parser_function_specifier_opt (parser, decl_specs);
9781           break;
9782
9783           /* decl-specifier:
9784                typedef  */
9785         case RID_TYPEDEF:
9786           ++decl_specs->specs[(int) ds_typedef];
9787           /* Consume the token.  */
9788           cp_lexer_consume_token (parser->lexer);
9789           /* A constructor declarator cannot appear in a typedef.  */
9790           constructor_possible_p = false;
9791           /* The "typedef" keyword can only occur in a declaration; we
9792              may as well commit at this point.  */
9793           cp_parser_commit_to_tentative_parse (parser);
9794
9795           if (decl_specs->storage_class != sc_none)
9796             decl_specs->conflicting_specifiers_p = true;
9797           break;
9798
9799           /* storage-class-specifier:
9800                auto
9801                register
9802                static
9803                extern
9804                mutable
9805
9806              GNU Extension:
9807                thread  */
9808         case RID_AUTO:
9809           if (cxx_dialect == cxx98) 
9810             {
9811               /* Consume the token.  */
9812               cp_lexer_consume_token (parser->lexer);
9813
9814               /* Complain about `auto' as a storage specifier, if
9815                  we're complaining about C++0x compatibility.  */
9816               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9817                           " will change meaning in C++0x; please remove it");
9818
9819               /* Set the storage class anyway.  */
9820               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9821                                            token->location);
9822             }
9823           else
9824             /* C++0x auto type-specifier.  */
9825             found_decl_spec = false;
9826           break;
9827
9828         case RID_REGISTER:
9829         case RID_STATIC:
9830         case RID_EXTERN:
9831         case RID_MUTABLE:
9832           /* Consume the token.  */
9833           cp_lexer_consume_token (parser->lexer);
9834           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9835                                        token->location);
9836           break;
9837         case RID_THREAD:
9838           /* Consume the token.  */
9839           cp_lexer_consume_token (parser->lexer);
9840           ++decl_specs->specs[(int) ds_thread];
9841           break;
9842
9843         default:
9844           /* We did not yet find a decl-specifier yet.  */
9845           found_decl_spec = false;
9846           break;
9847         }
9848
9849       if (found_decl_spec
9850           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9851           && token->keyword != RID_CONSTEXPR)
9852         error ("decl-specifier invalid in condition");
9853
9854       /* Constructors are a special case.  The `S' in `S()' is not a
9855          decl-specifier; it is the beginning of the declarator.  */
9856       constructor_p
9857         = (!found_decl_spec
9858            && constructor_possible_p
9859            && (cp_parser_constructor_declarator_p
9860                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9861
9862       /* If we don't have a DECL_SPEC yet, then we must be looking at
9863          a type-specifier.  */
9864       if (!found_decl_spec && !constructor_p)
9865         {
9866           int decl_spec_declares_class_or_enum;
9867           bool is_cv_qualifier;
9868           tree type_spec;
9869
9870           type_spec
9871             = cp_parser_type_specifier (parser, flags,
9872                                         decl_specs,
9873                                         /*is_declaration=*/true,
9874                                         &decl_spec_declares_class_or_enum,
9875                                         &is_cv_qualifier);
9876           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9877
9878           /* If this type-specifier referenced a user-defined type
9879              (a typedef, class-name, etc.), then we can't allow any
9880              more such type-specifiers henceforth.
9881
9882              [dcl.spec]
9883
9884              The longest sequence of decl-specifiers that could
9885              possibly be a type name is taken as the
9886              decl-specifier-seq of a declaration.  The sequence shall
9887              be self-consistent as described below.
9888
9889              [dcl.type]
9890
9891              As a general rule, at most one type-specifier is allowed
9892              in the complete decl-specifier-seq of a declaration.  The
9893              only exceptions are the following:
9894
9895              -- const or volatile can be combined with any other
9896                 type-specifier.
9897
9898              -- signed or unsigned can be combined with char, long,
9899                 short, or int.
9900
9901              -- ..
9902
9903              Example:
9904
9905                typedef char* Pc;
9906                void g (const int Pc);
9907
9908              Here, Pc is *not* part of the decl-specifier seq; it's
9909              the declarator.  Therefore, once we see a type-specifier
9910              (other than a cv-qualifier), we forbid any additional
9911              user-defined types.  We *do* still allow things like `int
9912              int' to be considered a decl-specifier-seq, and issue the
9913              error message later.  */
9914           if (type_spec && !is_cv_qualifier)
9915             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9916           /* A constructor declarator cannot follow a type-specifier.  */
9917           if (type_spec)
9918             {
9919               constructor_possible_p = false;
9920               found_decl_spec = true;
9921               if (!is_cv_qualifier)
9922                 decl_specs->any_type_specifiers_p = true;
9923             }
9924         }
9925
9926       /* If we still do not have a DECL_SPEC, then there are no more
9927          decl-specifiers.  */
9928       if (!found_decl_spec)
9929         break;
9930
9931       decl_specs->any_specifiers_p = true;
9932       /* After we see one decl-specifier, further decl-specifiers are
9933          always optional.  */
9934       flags |= CP_PARSER_FLAGS_OPTIONAL;
9935     }
9936
9937   cp_parser_check_decl_spec (decl_specs, start_token->location);
9938
9939   /* Don't allow a friend specifier with a class definition.  */
9940   if (decl_specs->specs[(int) ds_friend] != 0
9941       && (*declares_class_or_enum & 2))
9942     error_at (start_token->location,
9943               "class definition may not be declared a friend");
9944 }
9945
9946 /* Parse an (optional) storage-class-specifier.
9947
9948    storage-class-specifier:
9949      auto
9950      register
9951      static
9952      extern
9953      mutable
9954
9955    GNU Extension:
9956
9957    storage-class-specifier:
9958      thread
9959
9960    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9961
9962 static tree
9963 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9964 {
9965   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9966     {
9967     case RID_AUTO:
9968       if (cxx_dialect != cxx98)
9969         return NULL_TREE;
9970       /* Fall through for C++98.  */
9971
9972     case RID_REGISTER:
9973     case RID_STATIC:
9974     case RID_EXTERN:
9975     case RID_MUTABLE:
9976     case RID_THREAD:
9977       /* Consume the token.  */
9978       return cp_lexer_consume_token (parser->lexer)->u.value;
9979
9980     default:
9981       return NULL_TREE;
9982     }
9983 }
9984
9985 /* Parse an (optional) function-specifier.
9986
9987    function-specifier:
9988      inline
9989      virtual
9990      explicit
9991
9992    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9993    Updates DECL_SPECS, if it is non-NULL.  */
9994
9995 static tree
9996 cp_parser_function_specifier_opt (cp_parser* parser,
9997                                   cp_decl_specifier_seq *decl_specs)
9998 {
9999   cp_token *token = cp_lexer_peek_token (parser->lexer);
10000   switch (token->keyword)
10001     {
10002     case RID_INLINE:
10003       if (decl_specs)
10004         ++decl_specs->specs[(int) ds_inline];
10005       break;
10006
10007     case RID_VIRTUAL:
10008       /* 14.5.2.3 [temp.mem]
10009
10010          A member function template shall not be virtual.  */
10011       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10012         error_at (token->location, "templates may not be %<virtual%>");
10013       else if (decl_specs)
10014         ++decl_specs->specs[(int) ds_virtual];
10015       break;
10016
10017     case RID_EXPLICIT:
10018       if (decl_specs)
10019         ++decl_specs->specs[(int) ds_explicit];
10020       break;
10021
10022     default:
10023       return NULL_TREE;
10024     }
10025
10026   /* Consume the token.  */
10027   return cp_lexer_consume_token (parser->lexer)->u.value;
10028 }
10029
10030 /* Parse a linkage-specification.
10031
10032    linkage-specification:
10033      extern string-literal { declaration-seq [opt] }
10034      extern string-literal declaration  */
10035
10036 static void
10037 cp_parser_linkage_specification (cp_parser* parser)
10038 {
10039   tree linkage;
10040
10041   /* Look for the `extern' keyword.  */
10042   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10043
10044   /* Look for the string-literal.  */
10045   linkage = cp_parser_string_literal (parser, false, false);
10046
10047   /* Transform the literal into an identifier.  If the literal is a
10048      wide-character string, or contains embedded NULs, then we can't
10049      handle it as the user wants.  */
10050   if (strlen (TREE_STRING_POINTER (linkage))
10051       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10052     {
10053       cp_parser_error (parser, "invalid linkage-specification");
10054       /* Assume C++ linkage.  */
10055       linkage = lang_name_cplusplus;
10056     }
10057   else
10058     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10059
10060   /* We're now using the new linkage.  */
10061   push_lang_context (linkage);
10062
10063   /* If the next token is a `{', then we're using the first
10064      production.  */
10065   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10066     {
10067       /* Consume the `{' token.  */
10068       cp_lexer_consume_token (parser->lexer);
10069       /* Parse the declarations.  */
10070       cp_parser_declaration_seq_opt (parser);
10071       /* Look for the closing `}'.  */
10072       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10073     }
10074   /* Otherwise, there's just one declaration.  */
10075   else
10076     {
10077       bool saved_in_unbraced_linkage_specification_p;
10078
10079       saved_in_unbraced_linkage_specification_p
10080         = parser->in_unbraced_linkage_specification_p;
10081       parser->in_unbraced_linkage_specification_p = true;
10082       cp_parser_declaration (parser);
10083       parser->in_unbraced_linkage_specification_p
10084         = saved_in_unbraced_linkage_specification_p;
10085     }
10086
10087   /* We're done with the linkage-specification.  */
10088   pop_lang_context ();
10089 }
10090
10091 /* Parse a static_assert-declaration.
10092
10093    static_assert-declaration:
10094      static_assert ( constant-expression , string-literal ) ; 
10095
10096    If MEMBER_P, this static_assert is a class member.  */
10097
10098 static void 
10099 cp_parser_static_assert(cp_parser *parser, bool member_p)
10100 {
10101   tree condition;
10102   tree message;
10103   cp_token *token;
10104   location_t saved_loc;
10105   bool dummy;
10106
10107   /* Peek at the `static_assert' token so we can keep track of exactly
10108      where the static assertion started.  */
10109   token = cp_lexer_peek_token (parser->lexer);
10110   saved_loc = token->location;
10111
10112   /* Look for the `static_assert' keyword.  */
10113   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10114                                   RT_STATIC_ASSERT))
10115     return;
10116
10117   /*  We know we are in a static assertion; commit to any tentative
10118       parse.  */
10119   if (cp_parser_parsing_tentatively (parser))
10120     cp_parser_commit_to_tentative_parse (parser);
10121
10122   /* Parse the `(' starting the static assertion condition.  */
10123   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10124
10125   /* Parse the constant-expression.  Allow a non-constant expression
10126      here in order to give better diagnostics in finish_static_assert.  */
10127   condition = 
10128     cp_parser_constant_expression (parser,
10129                                    /*allow_non_constant_p=*/true,
10130                                    /*non_constant_p=*/&dummy);
10131
10132   /* Parse the separating `,'.  */
10133   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10134
10135   /* Parse the string-literal message.  */
10136   message = cp_parser_string_literal (parser, 
10137                                       /*translate=*/false,
10138                                       /*wide_ok=*/true);
10139
10140   /* A `)' completes the static assertion.  */
10141   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10142     cp_parser_skip_to_closing_parenthesis (parser, 
10143                                            /*recovering=*/true, 
10144                                            /*or_comma=*/false,
10145                                            /*consume_paren=*/true);
10146
10147   /* A semicolon terminates the declaration.  */
10148   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10149
10150   /* Complete the static assertion, which may mean either processing 
10151      the static assert now or saving it for template instantiation.  */
10152   finish_static_assert (condition, message, saved_loc, member_p);
10153 }
10154
10155 /* Parse a `decltype' type. Returns the type. 
10156
10157    simple-type-specifier:
10158      decltype ( expression )  */
10159
10160 static tree
10161 cp_parser_decltype (cp_parser *parser)
10162 {
10163   tree expr;
10164   bool id_expression_or_member_access_p = false;
10165   const char *saved_message;
10166   bool saved_integral_constant_expression_p;
10167   bool saved_non_integral_constant_expression_p;
10168   cp_token *id_expr_start_token;
10169
10170   /* Look for the `decltype' token.  */
10171   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10172     return error_mark_node;
10173
10174   /* Types cannot be defined in a `decltype' expression.  Save away the
10175      old message.  */
10176   saved_message = parser->type_definition_forbidden_message;
10177
10178   /* And create the new one.  */
10179   parser->type_definition_forbidden_message
10180     = G_("types may not be defined in %<decltype%> expressions");
10181
10182   /* The restrictions on constant-expressions do not apply inside
10183      decltype expressions.  */
10184   saved_integral_constant_expression_p
10185     = parser->integral_constant_expression_p;
10186   saved_non_integral_constant_expression_p
10187     = parser->non_integral_constant_expression_p;
10188   parser->integral_constant_expression_p = false;
10189
10190   /* Do not actually evaluate the expression.  */
10191   ++cp_unevaluated_operand;
10192
10193   /* Do not warn about problems with the expression.  */
10194   ++c_inhibit_evaluation_warnings;
10195
10196   /* Parse the opening `('.  */
10197   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10198     return error_mark_node;
10199   
10200   /* First, try parsing an id-expression.  */
10201   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10202   cp_parser_parse_tentatively (parser);
10203   expr = cp_parser_id_expression (parser,
10204                                   /*template_keyword_p=*/false,
10205                                   /*check_dependency_p=*/true,
10206                                   /*template_p=*/NULL,
10207                                   /*declarator_p=*/false,
10208                                   /*optional_p=*/false);
10209
10210   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10211     {
10212       bool non_integral_constant_expression_p = false;
10213       tree id_expression = expr;
10214       cp_id_kind idk;
10215       const char *error_msg;
10216
10217       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10218         /* Lookup the name we got back from the id-expression.  */
10219         expr = cp_parser_lookup_name (parser, expr,
10220                                       none_type,
10221                                       /*is_template=*/false,
10222                                       /*is_namespace=*/false,
10223                                       /*check_dependency=*/true,
10224                                       /*ambiguous_decls=*/NULL,
10225                                       id_expr_start_token->location);
10226
10227       if (expr
10228           && expr != error_mark_node
10229           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10230           && TREE_CODE (expr) != TYPE_DECL
10231           && (TREE_CODE (expr) != BIT_NOT_EXPR
10232               || !TYPE_P (TREE_OPERAND (expr, 0)))
10233           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10234         {
10235           /* Complete lookup of the id-expression.  */
10236           expr = (finish_id_expression
10237                   (id_expression, expr, parser->scope, &idk,
10238                    /*integral_constant_expression_p=*/false,
10239                    /*allow_non_integral_constant_expression_p=*/true,
10240                    &non_integral_constant_expression_p,
10241                    /*template_p=*/false,
10242                    /*done=*/true,
10243                    /*address_p=*/false,
10244                    /*template_arg_p=*/false,
10245                    &error_msg,
10246                    id_expr_start_token->location));
10247
10248           if (expr == error_mark_node)
10249             /* We found an id-expression, but it was something that we
10250                should not have found. This is an error, not something
10251                we can recover from, so note that we found an
10252                id-expression and we'll recover as gracefully as
10253                possible.  */
10254             id_expression_or_member_access_p = true;
10255         }
10256
10257       if (expr 
10258           && expr != error_mark_node
10259           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10260         /* We have an id-expression.  */
10261         id_expression_or_member_access_p = true;
10262     }
10263
10264   if (!id_expression_or_member_access_p)
10265     {
10266       /* Abort the id-expression parse.  */
10267       cp_parser_abort_tentative_parse (parser);
10268
10269       /* Parsing tentatively, again.  */
10270       cp_parser_parse_tentatively (parser);
10271
10272       /* Parse a class member access.  */
10273       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10274                                            /*cast_p=*/false,
10275                                            /*member_access_only_p=*/true, NULL);
10276
10277       if (expr 
10278           && expr != error_mark_node
10279           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10280         /* We have an id-expression.  */
10281         id_expression_or_member_access_p = true;
10282     }
10283
10284   if (id_expression_or_member_access_p)
10285     /* We have parsed the complete id-expression or member access.  */
10286     cp_parser_parse_definitely (parser);
10287   else
10288     {
10289       bool saved_greater_than_is_operator_p;
10290
10291       /* Abort our attempt to parse an id-expression or member access
10292          expression.  */
10293       cp_parser_abort_tentative_parse (parser);
10294
10295       /* Within a parenthesized expression, a `>' token is always
10296          the greater-than operator.  */
10297       saved_greater_than_is_operator_p
10298         = parser->greater_than_is_operator_p;
10299       parser->greater_than_is_operator_p = true;
10300
10301       /* Parse a full expression.  */
10302       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10303
10304       /* The `>' token might be the end of a template-id or
10305          template-parameter-list now.  */
10306       parser->greater_than_is_operator_p
10307         = saved_greater_than_is_operator_p;
10308     }
10309
10310   /* Go back to evaluating expressions.  */
10311   --cp_unevaluated_operand;
10312   --c_inhibit_evaluation_warnings;
10313
10314   /* Restore the old message and the integral constant expression
10315      flags.  */
10316   parser->type_definition_forbidden_message = saved_message;
10317   parser->integral_constant_expression_p
10318     = saved_integral_constant_expression_p;
10319   parser->non_integral_constant_expression_p
10320     = saved_non_integral_constant_expression_p;
10321
10322   if (expr == error_mark_node)
10323     {
10324       /* Skip everything up to the closing `)'.  */
10325       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10326                                              /*consume_paren=*/true);
10327       return error_mark_node;
10328     }
10329   
10330   /* Parse to the closing `)'.  */
10331   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10332     {
10333       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10334                                              /*consume_paren=*/true);
10335       return error_mark_node;
10336     }
10337
10338   return finish_decltype_type (expr, id_expression_or_member_access_p,
10339                                tf_warning_or_error);
10340 }
10341
10342 /* Special member functions [gram.special] */
10343
10344 /* Parse a conversion-function-id.
10345
10346    conversion-function-id:
10347      operator conversion-type-id
10348
10349    Returns an IDENTIFIER_NODE representing the operator.  */
10350
10351 static tree
10352 cp_parser_conversion_function_id (cp_parser* parser)
10353 {
10354   tree type;
10355   tree saved_scope;
10356   tree saved_qualifying_scope;
10357   tree saved_object_scope;
10358   tree pushed_scope = NULL_TREE;
10359
10360   /* Look for the `operator' token.  */
10361   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10362     return error_mark_node;
10363   /* When we parse the conversion-type-id, the current scope will be
10364      reset.  However, we need that information in able to look up the
10365      conversion function later, so we save it here.  */
10366   saved_scope = parser->scope;
10367   saved_qualifying_scope = parser->qualifying_scope;
10368   saved_object_scope = parser->object_scope;
10369   /* We must enter the scope of the class so that the names of
10370      entities declared within the class are available in the
10371      conversion-type-id.  For example, consider:
10372
10373        struct S {
10374          typedef int I;
10375          operator I();
10376        };
10377
10378        S::operator I() { ... }
10379
10380      In order to see that `I' is a type-name in the definition, we
10381      must be in the scope of `S'.  */
10382   if (saved_scope)
10383     pushed_scope = push_scope (saved_scope);
10384   /* Parse the conversion-type-id.  */
10385   type = cp_parser_conversion_type_id (parser);
10386   /* Leave the scope of the class, if any.  */
10387   if (pushed_scope)
10388     pop_scope (pushed_scope);
10389   /* Restore the saved scope.  */
10390   parser->scope = saved_scope;
10391   parser->qualifying_scope = saved_qualifying_scope;
10392   parser->object_scope = saved_object_scope;
10393   /* If the TYPE is invalid, indicate failure.  */
10394   if (type == error_mark_node)
10395     return error_mark_node;
10396   return mangle_conv_op_name_for_type (type);
10397 }
10398
10399 /* Parse a conversion-type-id:
10400
10401    conversion-type-id:
10402      type-specifier-seq conversion-declarator [opt]
10403
10404    Returns the TYPE specified.  */
10405
10406 static tree
10407 cp_parser_conversion_type_id (cp_parser* parser)
10408 {
10409   tree attributes;
10410   cp_decl_specifier_seq type_specifiers;
10411   cp_declarator *declarator;
10412   tree type_specified;
10413
10414   /* Parse the attributes.  */
10415   attributes = cp_parser_attributes_opt (parser);
10416   /* Parse the type-specifiers.  */
10417   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10418                                 /*is_trailing_return=*/false,
10419                                 &type_specifiers);
10420   /* If that didn't work, stop.  */
10421   if (type_specifiers.type == error_mark_node)
10422     return error_mark_node;
10423   /* Parse the conversion-declarator.  */
10424   declarator = cp_parser_conversion_declarator_opt (parser);
10425
10426   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10427                                     /*initialized=*/0, &attributes);
10428   if (attributes)
10429     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10430
10431   /* Don't give this error when parsing tentatively.  This happens to
10432      work because we always parse this definitively once.  */
10433   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10434       && type_uses_auto (type_specified))
10435     {
10436       error ("invalid use of %<auto%> in conversion operator");
10437       return error_mark_node;
10438     }
10439
10440   return type_specified;
10441 }
10442
10443 /* Parse an (optional) conversion-declarator.
10444
10445    conversion-declarator:
10446      ptr-operator conversion-declarator [opt]
10447
10448    */
10449
10450 static cp_declarator *
10451 cp_parser_conversion_declarator_opt (cp_parser* parser)
10452 {
10453   enum tree_code code;
10454   tree class_type;
10455   cp_cv_quals cv_quals;
10456
10457   /* We don't know if there's a ptr-operator next, or not.  */
10458   cp_parser_parse_tentatively (parser);
10459   /* Try the ptr-operator.  */
10460   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10461   /* If it worked, look for more conversion-declarators.  */
10462   if (cp_parser_parse_definitely (parser))
10463     {
10464       cp_declarator *declarator;
10465
10466       /* Parse another optional declarator.  */
10467       declarator = cp_parser_conversion_declarator_opt (parser);
10468
10469       return cp_parser_make_indirect_declarator
10470         (code, class_type, cv_quals, declarator);
10471    }
10472
10473   return NULL;
10474 }
10475
10476 /* Parse an (optional) ctor-initializer.
10477
10478    ctor-initializer:
10479      : mem-initializer-list
10480
10481    Returns TRUE iff the ctor-initializer was actually present.  */
10482
10483 static bool
10484 cp_parser_ctor_initializer_opt (cp_parser* parser)
10485 {
10486   /* If the next token is not a `:', then there is no
10487      ctor-initializer.  */
10488   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10489     {
10490       /* Do default initialization of any bases and members.  */
10491       if (DECL_CONSTRUCTOR_P (current_function_decl))
10492         finish_mem_initializers (NULL_TREE);
10493
10494       return false;
10495     }
10496
10497   /* Consume the `:' token.  */
10498   cp_lexer_consume_token (parser->lexer);
10499   /* And the mem-initializer-list.  */
10500   cp_parser_mem_initializer_list (parser);
10501
10502   return true;
10503 }
10504
10505 /* Parse a mem-initializer-list.
10506
10507    mem-initializer-list:
10508      mem-initializer ... [opt]
10509      mem-initializer ... [opt] , mem-initializer-list  */
10510
10511 static void
10512 cp_parser_mem_initializer_list (cp_parser* parser)
10513 {
10514   tree mem_initializer_list = NULL_TREE;
10515   cp_token *token = cp_lexer_peek_token (parser->lexer);
10516
10517   /* Let the semantic analysis code know that we are starting the
10518      mem-initializer-list.  */
10519   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10520     error_at (token->location,
10521               "only constructors take member initializers");
10522
10523   /* Loop through the list.  */
10524   while (true)
10525     {
10526       tree mem_initializer;
10527
10528       token = cp_lexer_peek_token (parser->lexer);
10529       /* Parse the mem-initializer.  */
10530       mem_initializer = cp_parser_mem_initializer (parser);
10531       /* If the next token is a `...', we're expanding member initializers. */
10532       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10533         {
10534           /* Consume the `...'. */
10535           cp_lexer_consume_token (parser->lexer);
10536
10537           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10538              can be expanded but members cannot. */
10539           if (mem_initializer != error_mark_node
10540               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10541             {
10542               error_at (token->location,
10543                         "cannot expand initializer for member %<%D%>",
10544                         TREE_PURPOSE (mem_initializer));
10545               mem_initializer = error_mark_node;
10546             }
10547
10548           /* Construct the pack expansion type. */
10549           if (mem_initializer != error_mark_node)
10550             mem_initializer = make_pack_expansion (mem_initializer);
10551         }
10552       /* Add it to the list, unless it was erroneous.  */
10553       if (mem_initializer != error_mark_node)
10554         {
10555           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10556           mem_initializer_list = mem_initializer;
10557         }
10558       /* If the next token is not a `,', we're done.  */
10559       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10560         break;
10561       /* Consume the `,' token.  */
10562       cp_lexer_consume_token (parser->lexer);
10563     }
10564
10565   /* Perform semantic analysis.  */
10566   if (DECL_CONSTRUCTOR_P (current_function_decl))
10567     finish_mem_initializers (mem_initializer_list);
10568 }
10569
10570 /* Parse a mem-initializer.
10571
10572    mem-initializer:
10573      mem-initializer-id ( expression-list [opt] )
10574      mem-initializer-id braced-init-list
10575
10576    GNU extension:
10577
10578    mem-initializer:
10579      ( expression-list [opt] )
10580
10581    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10582    class) or FIELD_DECL (for a non-static data member) to initialize;
10583    the TREE_VALUE is the expression-list.  An empty initialization
10584    list is represented by void_list_node.  */
10585
10586 static tree
10587 cp_parser_mem_initializer (cp_parser* parser)
10588 {
10589   tree mem_initializer_id;
10590   tree expression_list;
10591   tree member;
10592   cp_token *token = cp_lexer_peek_token (parser->lexer);
10593
10594   /* Find out what is being initialized.  */
10595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10596     {
10597       permerror (token->location,
10598                  "anachronistic old-style base class initializer");
10599       mem_initializer_id = NULL_TREE;
10600     }
10601   else
10602     {
10603       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10604       if (mem_initializer_id == error_mark_node)
10605         return mem_initializer_id;
10606     }
10607   member = expand_member_init (mem_initializer_id);
10608   if (member && !DECL_P (member))
10609     in_base_initializer = 1;
10610
10611   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10612     {
10613       bool expr_non_constant_p;
10614       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10615       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10616       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10617       expression_list = build_tree_list (NULL_TREE, expression_list);
10618     }
10619   else
10620     {
10621       VEC(tree,gc)* vec;
10622       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10623                                                      /*cast_p=*/false,
10624                                                      /*allow_expansion_p=*/true,
10625                                                      /*non_constant_p=*/NULL);
10626       if (vec == NULL)
10627         return error_mark_node;
10628       expression_list = build_tree_list_vec (vec);
10629       release_tree_vector (vec);
10630     }
10631
10632   if (expression_list == error_mark_node)
10633     return error_mark_node;
10634   if (!expression_list)
10635     expression_list = void_type_node;
10636
10637   in_base_initializer = 0;
10638
10639   return member ? build_tree_list (member, expression_list) : error_mark_node;
10640 }
10641
10642 /* Parse a mem-initializer-id.
10643
10644    mem-initializer-id:
10645      :: [opt] nested-name-specifier [opt] class-name
10646      identifier
10647
10648    Returns a TYPE indicating the class to be initializer for the first
10649    production.  Returns an IDENTIFIER_NODE indicating the data member
10650    to be initialized for the second production.  */
10651
10652 static tree
10653 cp_parser_mem_initializer_id (cp_parser* parser)
10654 {
10655   bool global_scope_p;
10656   bool nested_name_specifier_p;
10657   bool template_p = false;
10658   tree id;
10659
10660   cp_token *token = cp_lexer_peek_token (parser->lexer);
10661
10662   /* `typename' is not allowed in this context ([temp.res]).  */
10663   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10664     {
10665       error_at (token->location, 
10666                 "keyword %<typename%> not allowed in this context (a qualified "
10667                 "member initializer is implicitly a type)");
10668       cp_lexer_consume_token (parser->lexer);
10669     }
10670   /* Look for the optional `::' operator.  */
10671   global_scope_p
10672     = (cp_parser_global_scope_opt (parser,
10673                                    /*current_scope_valid_p=*/false)
10674        != NULL_TREE);
10675   /* Look for the optional nested-name-specifier.  The simplest way to
10676      implement:
10677
10678        [temp.res]
10679
10680        The keyword `typename' is not permitted in a base-specifier or
10681        mem-initializer; in these contexts a qualified name that
10682        depends on a template-parameter is implicitly assumed to be a
10683        type name.
10684
10685      is to assume that we have seen the `typename' keyword at this
10686      point.  */
10687   nested_name_specifier_p
10688     = (cp_parser_nested_name_specifier_opt (parser,
10689                                             /*typename_keyword_p=*/true,
10690                                             /*check_dependency_p=*/true,
10691                                             /*type_p=*/true,
10692                                             /*is_declaration=*/true)
10693        != NULL_TREE);
10694   if (nested_name_specifier_p)
10695     template_p = cp_parser_optional_template_keyword (parser);
10696   /* If there is a `::' operator or a nested-name-specifier, then we
10697      are definitely looking for a class-name.  */
10698   if (global_scope_p || nested_name_specifier_p)
10699     return cp_parser_class_name (parser,
10700                                  /*typename_keyword_p=*/true,
10701                                  /*template_keyword_p=*/template_p,
10702                                  typename_type,
10703                                  /*check_dependency_p=*/true,
10704                                  /*class_head_p=*/false,
10705                                  /*is_declaration=*/true);
10706   /* Otherwise, we could also be looking for an ordinary identifier.  */
10707   cp_parser_parse_tentatively (parser);
10708   /* Try a class-name.  */
10709   id = cp_parser_class_name (parser,
10710                              /*typename_keyword_p=*/true,
10711                              /*template_keyword_p=*/false,
10712                              none_type,
10713                              /*check_dependency_p=*/true,
10714                              /*class_head_p=*/false,
10715                              /*is_declaration=*/true);
10716   /* If we found one, we're done.  */
10717   if (cp_parser_parse_definitely (parser))
10718     return id;
10719   /* Otherwise, look for an ordinary identifier.  */
10720   return cp_parser_identifier (parser);
10721 }
10722
10723 /* Overloading [gram.over] */
10724
10725 /* Parse an operator-function-id.
10726
10727    operator-function-id:
10728      operator operator
10729
10730    Returns an IDENTIFIER_NODE for the operator which is a
10731    human-readable spelling of the identifier, e.g., `operator +'.  */
10732
10733 static tree
10734 cp_parser_operator_function_id (cp_parser* parser)
10735 {
10736   /* Look for the `operator' keyword.  */
10737   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10738     return error_mark_node;
10739   /* And then the name of the operator itself.  */
10740   return cp_parser_operator (parser);
10741 }
10742
10743 /* Parse an operator.
10744
10745    operator:
10746      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10747      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10748      || ++ -- , ->* -> () []
10749
10750    GNU Extensions:
10751
10752    operator:
10753      <? >? <?= >?=
10754
10755    Returns an IDENTIFIER_NODE for the operator which is a
10756    human-readable spelling of the identifier, e.g., `operator +'.  */
10757
10758 static tree
10759 cp_parser_operator (cp_parser* parser)
10760 {
10761   tree id = NULL_TREE;
10762   cp_token *token;
10763
10764   /* Peek at the next token.  */
10765   token = cp_lexer_peek_token (parser->lexer);
10766   /* Figure out which operator we have.  */
10767   switch (token->type)
10768     {
10769     case CPP_KEYWORD:
10770       {
10771         enum tree_code op;
10772
10773         /* The keyword should be either `new' or `delete'.  */
10774         if (token->keyword == RID_NEW)
10775           op = NEW_EXPR;
10776         else if (token->keyword == RID_DELETE)
10777           op = DELETE_EXPR;
10778         else
10779           break;
10780
10781         /* Consume the `new' or `delete' token.  */
10782         cp_lexer_consume_token (parser->lexer);
10783
10784         /* Peek at the next token.  */
10785         token = cp_lexer_peek_token (parser->lexer);
10786         /* If it's a `[' token then this is the array variant of the
10787            operator.  */
10788         if (token->type == CPP_OPEN_SQUARE)
10789           {
10790             /* Consume the `[' token.  */
10791             cp_lexer_consume_token (parser->lexer);
10792             /* Look for the `]' token.  */
10793             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10794             id = ansi_opname (op == NEW_EXPR
10795                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10796           }
10797         /* Otherwise, we have the non-array variant.  */
10798         else
10799           id = ansi_opname (op);
10800
10801         return id;
10802       }
10803
10804     case CPP_PLUS:
10805       id = ansi_opname (PLUS_EXPR);
10806       break;
10807
10808     case CPP_MINUS:
10809       id = ansi_opname (MINUS_EXPR);
10810       break;
10811
10812     case CPP_MULT:
10813       id = ansi_opname (MULT_EXPR);
10814       break;
10815
10816     case CPP_DIV:
10817       id = ansi_opname (TRUNC_DIV_EXPR);
10818       break;
10819
10820     case CPP_MOD:
10821       id = ansi_opname (TRUNC_MOD_EXPR);
10822       break;
10823
10824     case CPP_XOR:
10825       id = ansi_opname (BIT_XOR_EXPR);
10826       break;
10827
10828     case CPP_AND:
10829       id = ansi_opname (BIT_AND_EXPR);
10830       break;
10831
10832     case CPP_OR:
10833       id = ansi_opname (BIT_IOR_EXPR);
10834       break;
10835
10836     case CPP_COMPL:
10837       id = ansi_opname (BIT_NOT_EXPR);
10838       break;
10839
10840     case CPP_NOT:
10841       id = ansi_opname (TRUTH_NOT_EXPR);
10842       break;
10843
10844     case CPP_EQ:
10845       id = ansi_assopname (NOP_EXPR);
10846       break;
10847
10848     case CPP_LESS:
10849       id = ansi_opname (LT_EXPR);
10850       break;
10851
10852     case CPP_GREATER:
10853       id = ansi_opname (GT_EXPR);
10854       break;
10855
10856     case CPP_PLUS_EQ:
10857       id = ansi_assopname (PLUS_EXPR);
10858       break;
10859
10860     case CPP_MINUS_EQ:
10861       id = ansi_assopname (MINUS_EXPR);
10862       break;
10863
10864     case CPP_MULT_EQ:
10865       id = ansi_assopname (MULT_EXPR);
10866       break;
10867
10868     case CPP_DIV_EQ:
10869       id = ansi_assopname (TRUNC_DIV_EXPR);
10870       break;
10871
10872     case CPP_MOD_EQ:
10873       id = ansi_assopname (TRUNC_MOD_EXPR);
10874       break;
10875
10876     case CPP_XOR_EQ:
10877       id = ansi_assopname (BIT_XOR_EXPR);
10878       break;
10879
10880     case CPP_AND_EQ:
10881       id = ansi_assopname (BIT_AND_EXPR);
10882       break;
10883
10884     case CPP_OR_EQ:
10885       id = ansi_assopname (BIT_IOR_EXPR);
10886       break;
10887
10888     case CPP_LSHIFT:
10889       id = ansi_opname (LSHIFT_EXPR);
10890       break;
10891
10892     case CPP_RSHIFT:
10893       id = ansi_opname (RSHIFT_EXPR);
10894       break;
10895
10896     case CPP_LSHIFT_EQ:
10897       id = ansi_assopname (LSHIFT_EXPR);
10898       break;
10899
10900     case CPP_RSHIFT_EQ:
10901       id = ansi_assopname (RSHIFT_EXPR);
10902       break;
10903
10904     case CPP_EQ_EQ:
10905       id = ansi_opname (EQ_EXPR);
10906       break;
10907
10908     case CPP_NOT_EQ:
10909       id = ansi_opname (NE_EXPR);
10910       break;
10911
10912     case CPP_LESS_EQ:
10913       id = ansi_opname (LE_EXPR);
10914       break;
10915
10916     case CPP_GREATER_EQ:
10917       id = ansi_opname (GE_EXPR);
10918       break;
10919
10920     case CPP_AND_AND:
10921       id = ansi_opname (TRUTH_ANDIF_EXPR);
10922       break;
10923
10924     case CPP_OR_OR:
10925       id = ansi_opname (TRUTH_ORIF_EXPR);
10926       break;
10927
10928     case CPP_PLUS_PLUS:
10929       id = ansi_opname (POSTINCREMENT_EXPR);
10930       break;
10931
10932     case CPP_MINUS_MINUS:
10933       id = ansi_opname (PREDECREMENT_EXPR);
10934       break;
10935
10936     case CPP_COMMA:
10937       id = ansi_opname (COMPOUND_EXPR);
10938       break;
10939
10940     case CPP_DEREF_STAR:
10941       id = ansi_opname (MEMBER_REF);
10942       break;
10943
10944     case CPP_DEREF:
10945       id = ansi_opname (COMPONENT_REF);
10946       break;
10947
10948     case CPP_OPEN_PAREN:
10949       /* Consume the `('.  */
10950       cp_lexer_consume_token (parser->lexer);
10951       /* Look for the matching `)'.  */
10952       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10953       return ansi_opname (CALL_EXPR);
10954
10955     case CPP_OPEN_SQUARE:
10956       /* Consume the `['.  */
10957       cp_lexer_consume_token (parser->lexer);
10958       /* Look for the matching `]'.  */
10959       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10960       return ansi_opname (ARRAY_REF);
10961
10962     default:
10963       /* Anything else is an error.  */
10964       break;
10965     }
10966
10967   /* If we have selected an identifier, we need to consume the
10968      operator token.  */
10969   if (id)
10970     cp_lexer_consume_token (parser->lexer);
10971   /* Otherwise, no valid operator name was present.  */
10972   else
10973     {
10974       cp_parser_error (parser, "expected operator");
10975       id = error_mark_node;
10976     }
10977
10978   return id;
10979 }
10980
10981 /* Parse a template-declaration.
10982
10983    template-declaration:
10984      export [opt] template < template-parameter-list > declaration
10985
10986    If MEMBER_P is TRUE, this template-declaration occurs within a
10987    class-specifier.
10988
10989    The grammar rule given by the standard isn't correct.  What
10990    is really meant is:
10991
10992    template-declaration:
10993      export [opt] template-parameter-list-seq
10994        decl-specifier-seq [opt] init-declarator [opt] ;
10995      export [opt] template-parameter-list-seq
10996        function-definition
10997
10998    template-parameter-list-seq:
10999      template-parameter-list-seq [opt]
11000      template < template-parameter-list >  */
11001
11002 static void
11003 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11004 {
11005   /* Check for `export'.  */
11006   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11007     {
11008       /* Consume the `export' token.  */
11009       cp_lexer_consume_token (parser->lexer);
11010       /* Warn that we do not support `export'.  */
11011       warning (0, "keyword %<export%> not implemented, and will be ignored");
11012     }
11013
11014   cp_parser_template_declaration_after_export (parser, member_p);
11015 }
11016
11017 /* Parse a template-parameter-list.
11018
11019    template-parameter-list:
11020      template-parameter
11021      template-parameter-list , template-parameter
11022
11023    Returns a TREE_LIST.  Each node represents a template parameter.
11024    The nodes are connected via their TREE_CHAINs.  */
11025
11026 static tree
11027 cp_parser_template_parameter_list (cp_parser* parser)
11028 {
11029   tree parameter_list = NULL_TREE;
11030
11031   begin_template_parm_list ();
11032
11033   /* The loop below parses the template parms.  We first need to know
11034      the total number of template parms to be able to compute proper
11035      canonical types of each dependent type. So after the loop, when
11036      we know the total number of template parms,
11037      end_template_parm_list computes the proper canonical types and
11038      fixes up the dependent types accordingly.  */
11039   while (true)
11040     {
11041       tree parameter;
11042       bool is_non_type;
11043       bool is_parameter_pack;
11044       location_t parm_loc;
11045
11046       /* Parse the template-parameter.  */
11047       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11048       parameter = cp_parser_template_parameter (parser, 
11049                                                 &is_non_type,
11050                                                 &is_parameter_pack);
11051       /* Add it to the list.  */
11052       if (parameter != error_mark_node)
11053         parameter_list = process_template_parm (parameter_list,
11054                                                 parm_loc,
11055                                                 parameter,
11056                                                 is_non_type,
11057                                                 is_parameter_pack,
11058                                                 0);
11059       else
11060        {
11061          tree err_parm = build_tree_list (parameter, parameter);
11062          parameter_list = chainon (parameter_list, err_parm);
11063        }
11064
11065       /* If the next token is not a `,', we're done.  */
11066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11067         break;
11068       /* Otherwise, consume the `,' token.  */
11069       cp_lexer_consume_token (parser->lexer);
11070     }
11071
11072   return end_template_parm_list (parameter_list);
11073 }
11074
11075 /* Parse a template-parameter.
11076
11077    template-parameter:
11078      type-parameter
11079      parameter-declaration
11080
11081    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11082    the parameter.  The TREE_PURPOSE is the default value, if any.
11083    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11084    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11085    set to true iff this parameter is a parameter pack. */
11086
11087 static tree
11088 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11089                               bool *is_parameter_pack)
11090 {
11091   cp_token *token;
11092   cp_parameter_declarator *parameter_declarator;
11093   cp_declarator *id_declarator;
11094   tree parm;
11095
11096   /* Assume it is a type parameter or a template parameter.  */
11097   *is_non_type = false;
11098   /* Assume it not a parameter pack. */
11099   *is_parameter_pack = false;
11100   /* Peek at the next token.  */
11101   token = cp_lexer_peek_token (parser->lexer);
11102   /* If it is `class' or `template', we have a type-parameter.  */
11103   if (token->keyword == RID_TEMPLATE)
11104     return cp_parser_type_parameter (parser, is_parameter_pack);
11105   /* If it is `class' or `typename' we do not know yet whether it is a
11106      type parameter or a non-type parameter.  Consider:
11107
11108        template <typename T, typename T::X X> ...
11109
11110      or:
11111
11112        template <class C, class D*> ...
11113
11114      Here, the first parameter is a type parameter, and the second is
11115      a non-type parameter.  We can tell by looking at the token after
11116      the identifier -- if it is a `,', `=', or `>' then we have a type
11117      parameter.  */
11118   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11119     {
11120       /* Peek at the token after `class' or `typename'.  */
11121       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11122       /* If it's an ellipsis, we have a template type parameter
11123          pack. */
11124       if (token->type == CPP_ELLIPSIS)
11125         return cp_parser_type_parameter (parser, is_parameter_pack);
11126       /* If it's an identifier, skip it.  */
11127       if (token->type == CPP_NAME)
11128         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11129       /* Now, see if the token looks like the end of a template
11130          parameter.  */
11131       if (token->type == CPP_COMMA
11132           || token->type == CPP_EQ
11133           || token->type == CPP_GREATER)
11134         return cp_parser_type_parameter (parser, is_parameter_pack);
11135     }
11136
11137   /* Otherwise, it is a non-type parameter.
11138
11139      [temp.param]
11140
11141      When parsing a default template-argument for a non-type
11142      template-parameter, the first non-nested `>' is taken as the end
11143      of the template parameter-list rather than a greater-than
11144      operator.  */
11145   *is_non_type = true;
11146   parameter_declarator
11147      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11148                                         /*parenthesized_p=*/NULL);
11149
11150   /* If the parameter declaration is marked as a parameter pack, set
11151      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11152      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11153      grokdeclarator. */
11154   if (parameter_declarator
11155       && parameter_declarator->declarator
11156       && parameter_declarator->declarator->parameter_pack_p)
11157     {
11158       *is_parameter_pack = true;
11159       parameter_declarator->declarator->parameter_pack_p = false;
11160     }
11161
11162   /* If the next token is an ellipsis, and we don't already have it
11163      marked as a parameter pack, then we have a parameter pack (that
11164      has no declarator).  */
11165   if (!*is_parameter_pack
11166       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11167       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11168     {
11169       /* Consume the `...'.  */
11170       cp_lexer_consume_token (parser->lexer);
11171       maybe_warn_variadic_templates ();
11172       
11173       *is_parameter_pack = true;
11174     }
11175   /* We might end up with a pack expansion as the type of the non-type
11176      template parameter, in which case this is a non-type template
11177      parameter pack.  */
11178   else if (parameter_declarator
11179            && parameter_declarator->decl_specifiers.type
11180            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11181     {
11182       *is_parameter_pack = true;
11183       parameter_declarator->decl_specifiers.type = 
11184         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11185     }
11186
11187   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11188     {
11189       /* Parameter packs cannot have default arguments.  However, a
11190          user may try to do so, so we'll parse them and give an
11191          appropriate diagnostic here.  */
11192
11193       /* Consume the `='.  */
11194       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11195       cp_lexer_consume_token (parser->lexer);
11196       
11197       /* Find the name of the parameter pack.  */     
11198       id_declarator = parameter_declarator->declarator;
11199       while (id_declarator && id_declarator->kind != cdk_id)
11200         id_declarator = id_declarator->declarator;
11201       
11202       if (id_declarator && id_declarator->kind == cdk_id)
11203         error_at (start_token->location,
11204                   "template parameter pack %qD cannot have a default argument",
11205                   id_declarator->u.id.unqualified_name);
11206       else
11207         error_at (start_token->location,
11208                   "template parameter pack cannot have a default argument");
11209       
11210       /* Parse the default argument, but throw away the result.  */
11211       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11212     }
11213
11214   parm = grokdeclarator (parameter_declarator->declarator,
11215                          &parameter_declarator->decl_specifiers,
11216                          TPARM, /*initialized=*/0,
11217                          /*attrlist=*/NULL);
11218   if (parm == error_mark_node)
11219     return error_mark_node;
11220
11221   return build_tree_list (parameter_declarator->default_argument, parm);
11222 }
11223
11224 /* Parse a type-parameter.
11225
11226    type-parameter:
11227      class identifier [opt]
11228      class identifier [opt] = type-id
11229      typename identifier [opt]
11230      typename identifier [opt] = type-id
11231      template < template-parameter-list > class identifier [opt]
11232      template < template-parameter-list > class identifier [opt]
11233        = id-expression
11234
11235    GNU Extension (variadic templates):
11236
11237    type-parameter:
11238      class ... identifier [opt]
11239      typename ... identifier [opt]
11240
11241    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11242    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11243    the declaration of the parameter.
11244
11245    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11246
11247 static tree
11248 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11249 {
11250   cp_token *token;
11251   tree parameter;
11252
11253   /* Look for a keyword to tell us what kind of parameter this is.  */
11254   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11255   if (!token)
11256     return error_mark_node;
11257
11258   switch (token->keyword)
11259     {
11260     case RID_CLASS:
11261     case RID_TYPENAME:
11262       {
11263         tree identifier;
11264         tree default_argument;
11265
11266         /* If the next token is an ellipsis, we have a template
11267            argument pack. */
11268         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11269           {
11270             /* Consume the `...' token. */
11271             cp_lexer_consume_token (parser->lexer);
11272             maybe_warn_variadic_templates ();
11273
11274             *is_parameter_pack = true;
11275           }
11276
11277         /* If the next token is an identifier, then it names the
11278            parameter.  */
11279         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11280           identifier = cp_parser_identifier (parser);
11281         else
11282           identifier = NULL_TREE;
11283
11284         /* Create the parameter.  */
11285         parameter = finish_template_type_parm (class_type_node, identifier);
11286
11287         /* If the next token is an `=', we have a default argument.  */
11288         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11289           {
11290             /* Consume the `=' token.  */
11291             cp_lexer_consume_token (parser->lexer);
11292             /* Parse the default-argument.  */
11293             push_deferring_access_checks (dk_no_deferred);
11294             default_argument = cp_parser_type_id (parser);
11295
11296             /* Template parameter packs cannot have default
11297                arguments. */
11298             if (*is_parameter_pack)
11299               {
11300                 if (identifier)
11301                   error_at (token->location,
11302                             "template parameter pack %qD cannot have a "
11303                             "default argument", identifier);
11304                 else
11305                   error_at (token->location,
11306                             "template parameter packs cannot have "
11307                             "default arguments");
11308                 default_argument = NULL_TREE;
11309               }
11310             pop_deferring_access_checks ();
11311           }
11312         else
11313           default_argument = NULL_TREE;
11314
11315         /* Create the combined representation of the parameter and the
11316            default argument.  */
11317         parameter = build_tree_list (default_argument, parameter);
11318       }
11319       break;
11320
11321     case RID_TEMPLATE:
11322       {
11323         tree identifier;
11324         tree default_argument;
11325
11326         /* Look for the `<'.  */
11327         cp_parser_require (parser, CPP_LESS, RT_LESS);
11328         /* Parse the template-parameter-list.  */
11329         cp_parser_template_parameter_list (parser);
11330         /* Look for the `>'.  */
11331         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11332         /* Look for the `class' keyword.  */
11333         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11334         /* If the next token is an ellipsis, we have a template
11335            argument pack. */
11336         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11337           {
11338             /* Consume the `...' token. */
11339             cp_lexer_consume_token (parser->lexer);
11340             maybe_warn_variadic_templates ();
11341
11342             *is_parameter_pack = true;
11343           }
11344         /* If the next token is an `=', then there is a
11345            default-argument.  If the next token is a `>', we are at
11346            the end of the parameter-list.  If the next token is a `,',
11347            then we are at the end of this parameter.  */
11348         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11349             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11350             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11351           {
11352             identifier = cp_parser_identifier (parser);
11353             /* Treat invalid names as if the parameter were nameless.  */
11354             if (identifier == error_mark_node)
11355               identifier = NULL_TREE;
11356           }
11357         else
11358           identifier = NULL_TREE;
11359
11360         /* Create the template parameter.  */
11361         parameter = finish_template_template_parm (class_type_node,
11362                                                    identifier);
11363
11364         /* If the next token is an `=', then there is a
11365            default-argument.  */
11366         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11367           {
11368             bool is_template;
11369
11370             /* Consume the `='.  */
11371             cp_lexer_consume_token (parser->lexer);
11372             /* Parse the id-expression.  */
11373             push_deferring_access_checks (dk_no_deferred);
11374             /* save token before parsing the id-expression, for error
11375                reporting */
11376             token = cp_lexer_peek_token (parser->lexer);
11377             default_argument
11378               = cp_parser_id_expression (parser,
11379                                          /*template_keyword_p=*/false,
11380                                          /*check_dependency_p=*/true,
11381                                          /*template_p=*/&is_template,
11382                                          /*declarator_p=*/false,
11383                                          /*optional_p=*/false);
11384             if (TREE_CODE (default_argument) == TYPE_DECL)
11385               /* If the id-expression was a template-id that refers to
11386                  a template-class, we already have the declaration here,
11387                  so no further lookup is needed.  */
11388                  ;
11389             else
11390               /* Look up the name.  */
11391               default_argument
11392                 = cp_parser_lookup_name (parser, default_argument,
11393                                          none_type,
11394                                          /*is_template=*/is_template,
11395                                          /*is_namespace=*/false,
11396                                          /*check_dependency=*/true,
11397                                          /*ambiguous_decls=*/NULL,
11398                                          token->location);
11399             /* See if the default argument is valid.  */
11400             default_argument
11401               = check_template_template_default_arg (default_argument);
11402
11403             /* Template parameter packs cannot have default
11404                arguments. */
11405             if (*is_parameter_pack)
11406               {
11407                 if (identifier)
11408                   error_at (token->location,
11409                             "template parameter pack %qD cannot "
11410                             "have a default argument",
11411                             identifier);
11412                 else
11413                   error_at (token->location, "template parameter packs cannot "
11414                             "have default arguments");
11415                 default_argument = NULL_TREE;
11416               }
11417             pop_deferring_access_checks ();
11418           }
11419         else
11420           default_argument = NULL_TREE;
11421
11422         /* Create the combined representation of the parameter and the
11423            default argument.  */
11424         parameter = build_tree_list (default_argument, parameter);
11425       }
11426       break;
11427
11428     default:
11429       gcc_unreachable ();
11430       break;
11431     }
11432
11433   return parameter;
11434 }
11435
11436 /* Parse a template-id.
11437
11438    template-id:
11439      template-name < template-argument-list [opt] >
11440
11441    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11442    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11443    returned.  Otherwise, if the template-name names a function, or set
11444    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11445    names a class, returns a TYPE_DECL for the specialization.
11446
11447    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11448    uninstantiated templates.  */
11449
11450 static tree
11451 cp_parser_template_id (cp_parser *parser,
11452                        bool template_keyword_p,
11453                        bool check_dependency_p,
11454                        bool is_declaration)
11455 {
11456   int i;
11457   tree templ;
11458   tree arguments;
11459   tree template_id;
11460   cp_token_position start_of_id = 0;
11461   deferred_access_check *chk;
11462   VEC (deferred_access_check,gc) *access_check;
11463   cp_token *next_token = NULL, *next_token_2 = NULL;
11464   bool is_identifier;
11465
11466   /* If the next token corresponds to a template-id, there is no need
11467      to reparse it.  */
11468   next_token = cp_lexer_peek_token (parser->lexer);
11469   if (next_token->type == CPP_TEMPLATE_ID)
11470     {
11471       struct tree_check *check_value;
11472
11473       /* Get the stored value.  */
11474       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11475       /* Perform any access checks that were deferred.  */
11476       access_check = check_value->checks;
11477       if (access_check)
11478         {
11479           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11480             perform_or_defer_access_check (chk->binfo,
11481                                            chk->decl,
11482                                            chk->diag_decl);
11483         }
11484       /* Return the stored value.  */
11485       return check_value->value;
11486     }
11487
11488   /* Avoid performing name lookup if there is no possibility of
11489      finding a template-id.  */
11490   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11491       || (next_token->type == CPP_NAME
11492           && !cp_parser_nth_token_starts_template_argument_list_p
11493                (parser, 2)))
11494     {
11495       cp_parser_error (parser, "expected template-id");
11496       return error_mark_node;
11497     }
11498
11499   /* Remember where the template-id starts.  */
11500   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11501     start_of_id = cp_lexer_token_position (parser->lexer, false);
11502
11503   push_deferring_access_checks (dk_deferred);
11504
11505   /* Parse the template-name.  */
11506   is_identifier = false;
11507   templ = cp_parser_template_name (parser, template_keyword_p,
11508                                    check_dependency_p,
11509                                    is_declaration,
11510                                    &is_identifier);
11511   if (templ == error_mark_node || is_identifier)
11512     {
11513       pop_deferring_access_checks ();
11514       return templ;
11515     }
11516
11517   /* If we find the sequence `[:' after a template-name, it's probably
11518      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11519      parse correctly the argument list.  */
11520   next_token = cp_lexer_peek_token (parser->lexer);
11521   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11522   if (next_token->type == CPP_OPEN_SQUARE
11523       && next_token->flags & DIGRAPH
11524       && next_token_2->type == CPP_COLON
11525       && !(next_token_2->flags & PREV_WHITE))
11526     {
11527       cp_parser_parse_tentatively (parser);
11528       /* Change `:' into `::'.  */
11529       next_token_2->type = CPP_SCOPE;
11530       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11531          CPP_LESS.  */
11532       cp_lexer_consume_token (parser->lexer);
11533
11534       /* Parse the arguments.  */
11535       arguments = cp_parser_enclosed_template_argument_list (parser);
11536       if (!cp_parser_parse_definitely (parser))
11537         {
11538           /* If we couldn't parse an argument list, then we revert our changes
11539              and return simply an error. Maybe this is not a template-id
11540              after all.  */
11541           next_token_2->type = CPP_COLON;
11542           cp_parser_error (parser, "expected %<<%>");
11543           pop_deferring_access_checks ();
11544           return error_mark_node;
11545         }
11546       /* Otherwise, emit an error about the invalid digraph, but continue
11547          parsing because we got our argument list.  */
11548       if (permerror (next_token->location,
11549                      "%<<::%> cannot begin a template-argument list"))
11550         {
11551           static bool hint = false;
11552           inform (next_token->location,
11553                   "%<<:%> is an alternate spelling for %<[%>."
11554                   " Insert whitespace between %<<%> and %<::%>");
11555           if (!hint && !flag_permissive)
11556             {
11557               inform (next_token->location, "(if you use %<-fpermissive%>"
11558                       " G++ will accept your code)");
11559               hint = true;
11560             }
11561         }
11562     }
11563   else
11564     {
11565       /* Look for the `<' that starts the template-argument-list.  */
11566       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11567         {
11568           pop_deferring_access_checks ();
11569           return error_mark_node;
11570         }
11571       /* Parse the arguments.  */
11572       arguments = cp_parser_enclosed_template_argument_list (parser);
11573     }
11574
11575   /* Build a representation of the specialization.  */
11576   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11577     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11578   else if (DECL_CLASS_TEMPLATE_P (templ)
11579            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11580     {
11581       bool entering_scope;
11582       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11583          template (rather than some instantiation thereof) only if
11584          is not nested within some other construct.  For example, in
11585          "template <typename T> void f(T) { A<T>::", A<T> is just an
11586          instantiation of A.  */
11587       entering_scope = (template_parm_scope_p ()
11588                         && cp_lexer_next_token_is (parser->lexer,
11589                                                    CPP_SCOPE));
11590       template_id
11591         = finish_template_type (templ, arguments, entering_scope);
11592     }
11593   else
11594     {
11595       /* If it's not a class-template or a template-template, it should be
11596          a function-template.  */
11597       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11598                    || TREE_CODE (templ) == OVERLOAD
11599                    || BASELINK_P (templ)));
11600
11601       template_id = lookup_template_function (templ, arguments);
11602     }
11603
11604   /* If parsing tentatively, replace the sequence of tokens that makes
11605      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11606      should we re-parse the token stream, we will not have to repeat
11607      the effort required to do the parse, nor will we issue duplicate
11608      error messages about problems during instantiation of the
11609      template.  */
11610   if (start_of_id)
11611     {
11612       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11613
11614       /* Reset the contents of the START_OF_ID token.  */
11615       token->type = CPP_TEMPLATE_ID;
11616       /* Retrieve any deferred checks.  Do not pop this access checks yet
11617          so the memory will not be reclaimed during token replacing below.  */
11618       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11619       token->u.tree_check_value->value = template_id;
11620       token->u.tree_check_value->checks = get_deferred_access_checks ();
11621       token->keyword = RID_MAX;
11622
11623       /* Purge all subsequent tokens.  */
11624       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11625
11626       /* ??? Can we actually assume that, if template_id ==
11627          error_mark_node, we will have issued a diagnostic to the
11628          user, as opposed to simply marking the tentative parse as
11629          failed?  */
11630       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11631         error_at (token->location, "parse error in template argument list");
11632     }
11633
11634   pop_deferring_access_checks ();
11635   return template_id;
11636 }
11637
11638 /* Parse a template-name.
11639
11640    template-name:
11641      identifier
11642
11643    The standard should actually say:
11644
11645    template-name:
11646      identifier
11647      operator-function-id
11648
11649    A defect report has been filed about this issue.
11650
11651    A conversion-function-id cannot be a template name because they cannot
11652    be part of a template-id. In fact, looking at this code:
11653
11654    a.operator K<int>()
11655
11656    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11657    It is impossible to call a templated conversion-function-id with an
11658    explicit argument list, since the only allowed template parameter is
11659    the type to which it is converting.
11660
11661    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11662    `template' keyword, in a construction like:
11663
11664      T::template f<3>()
11665
11666    In that case `f' is taken to be a template-name, even though there
11667    is no way of knowing for sure.
11668
11669    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11670    name refers to a set of overloaded functions, at least one of which
11671    is a template, or an IDENTIFIER_NODE with the name of the template,
11672    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11673    names are looked up inside uninstantiated templates.  */
11674
11675 static tree
11676 cp_parser_template_name (cp_parser* parser,
11677                          bool template_keyword_p,
11678                          bool check_dependency_p,
11679                          bool is_declaration,
11680                          bool *is_identifier)
11681 {
11682   tree identifier;
11683   tree decl;
11684   tree fns;
11685   cp_token *token = cp_lexer_peek_token (parser->lexer);
11686
11687   /* If the next token is `operator', then we have either an
11688      operator-function-id or a conversion-function-id.  */
11689   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11690     {
11691       /* We don't know whether we're looking at an
11692          operator-function-id or a conversion-function-id.  */
11693       cp_parser_parse_tentatively (parser);
11694       /* Try an operator-function-id.  */
11695       identifier = cp_parser_operator_function_id (parser);
11696       /* If that didn't work, try a conversion-function-id.  */
11697       if (!cp_parser_parse_definitely (parser))
11698         {
11699           cp_parser_error (parser, "expected template-name");
11700           return error_mark_node;
11701         }
11702     }
11703   /* Look for the identifier.  */
11704   else
11705     identifier = cp_parser_identifier (parser);
11706
11707   /* If we didn't find an identifier, we don't have a template-id.  */
11708   if (identifier == error_mark_node)
11709     return error_mark_node;
11710
11711   /* If the name immediately followed the `template' keyword, then it
11712      is a template-name.  However, if the next token is not `<', then
11713      we do not treat it as a template-name, since it is not being used
11714      as part of a template-id.  This enables us to handle constructs
11715      like:
11716
11717        template <typename T> struct S { S(); };
11718        template <typename T> S<T>::S();
11719
11720      correctly.  We would treat `S' as a template -- if it were `S<T>'
11721      -- but we do not if there is no `<'.  */
11722
11723   if (processing_template_decl
11724       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11725     {
11726       /* In a declaration, in a dependent context, we pretend that the
11727          "template" keyword was present in order to improve error
11728          recovery.  For example, given:
11729
11730            template <typename T> void f(T::X<int>);
11731
11732          we want to treat "X<int>" as a template-id.  */
11733       if (is_declaration
11734           && !template_keyword_p
11735           && parser->scope && TYPE_P (parser->scope)
11736           && check_dependency_p
11737           && dependent_scope_p (parser->scope)
11738           /* Do not do this for dtors (or ctors), since they never
11739              need the template keyword before their name.  */
11740           && !constructor_name_p (identifier, parser->scope))
11741         {
11742           cp_token_position start = 0;
11743
11744           /* Explain what went wrong.  */
11745           error_at (token->location, "non-template %qD used as template",
11746                     identifier);
11747           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11748                   parser->scope, identifier);
11749           /* If parsing tentatively, find the location of the "<" token.  */
11750           if (cp_parser_simulate_error (parser))
11751             start = cp_lexer_token_position (parser->lexer, true);
11752           /* Parse the template arguments so that we can issue error
11753              messages about them.  */
11754           cp_lexer_consume_token (parser->lexer);
11755           cp_parser_enclosed_template_argument_list (parser);
11756           /* Skip tokens until we find a good place from which to
11757              continue parsing.  */
11758           cp_parser_skip_to_closing_parenthesis (parser,
11759                                                  /*recovering=*/true,
11760                                                  /*or_comma=*/true,
11761                                                  /*consume_paren=*/false);
11762           /* If parsing tentatively, permanently remove the
11763              template argument list.  That will prevent duplicate
11764              error messages from being issued about the missing
11765              "template" keyword.  */
11766           if (start)
11767             cp_lexer_purge_tokens_after (parser->lexer, start);
11768           if (is_identifier)
11769             *is_identifier = true;
11770           return identifier;
11771         }
11772
11773       /* If the "template" keyword is present, then there is generally
11774          no point in doing name-lookup, so we just return IDENTIFIER.
11775          But, if the qualifying scope is non-dependent then we can
11776          (and must) do name-lookup normally.  */
11777       if (template_keyword_p
11778           && (!parser->scope
11779               || (TYPE_P (parser->scope)
11780                   && dependent_type_p (parser->scope))))
11781         return identifier;
11782     }
11783
11784   /* Look up the name.  */
11785   decl = cp_parser_lookup_name (parser, identifier,
11786                                 none_type,
11787                                 /*is_template=*/true,
11788                                 /*is_namespace=*/false,
11789                                 check_dependency_p,
11790                                 /*ambiguous_decls=*/NULL,
11791                                 token->location);
11792
11793   /* If DECL is a template, then the name was a template-name.  */
11794   if (TREE_CODE (decl) == TEMPLATE_DECL)
11795     ;
11796   else
11797     {
11798       tree fn = NULL_TREE;
11799
11800       /* The standard does not explicitly indicate whether a name that
11801          names a set of overloaded declarations, some of which are
11802          templates, is a template-name.  However, such a name should
11803          be a template-name; otherwise, there is no way to form a
11804          template-id for the overloaded templates.  */
11805       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11806       if (TREE_CODE (fns) == OVERLOAD)
11807         for (fn = fns; fn; fn = OVL_NEXT (fn))
11808           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11809             break;
11810
11811       if (!fn)
11812         {
11813           /* The name does not name a template.  */
11814           cp_parser_error (parser, "expected template-name");
11815           return error_mark_node;
11816         }
11817     }
11818
11819   /* If DECL is dependent, and refers to a function, then just return
11820      its name; we will look it up again during template instantiation.  */
11821   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11822     {
11823       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11824       if (TYPE_P (scope) && dependent_type_p (scope))
11825         return identifier;
11826     }
11827
11828   return decl;
11829 }
11830
11831 /* Parse a template-argument-list.
11832
11833    template-argument-list:
11834      template-argument ... [opt]
11835      template-argument-list , template-argument ... [opt]
11836
11837    Returns a TREE_VEC containing the arguments.  */
11838
11839 static tree
11840 cp_parser_template_argument_list (cp_parser* parser)
11841 {
11842   tree fixed_args[10];
11843   unsigned n_args = 0;
11844   unsigned alloced = 10;
11845   tree *arg_ary = fixed_args;
11846   tree vec;
11847   bool saved_in_template_argument_list_p;
11848   bool saved_ice_p;
11849   bool saved_non_ice_p;
11850
11851   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11852   parser->in_template_argument_list_p = true;
11853   /* Even if the template-id appears in an integral
11854      constant-expression, the contents of the argument list do
11855      not.  */
11856   saved_ice_p = parser->integral_constant_expression_p;
11857   parser->integral_constant_expression_p = false;
11858   saved_non_ice_p = parser->non_integral_constant_expression_p;
11859   parser->non_integral_constant_expression_p = false;
11860   /* Parse the arguments.  */
11861   do
11862     {
11863       tree argument;
11864
11865       if (n_args)
11866         /* Consume the comma.  */
11867         cp_lexer_consume_token (parser->lexer);
11868
11869       /* Parse the template-argument.  */
11870       argument = cp_parser_template_argument (parser);
11871
11872       /* If the next token is an ellipsis, we're expanding a template
11873          argument pack. */
11874       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11875         {
11876           if (argument == error_mark_node)
11877             {
11878               cp_token *token = cp_lexer_peek_token (parser->lexer);
11879               error_at (token->location,
11880                         "expected parameter pack before %<...%>");
11881             }
11882           /* Consume the `...' token. */
11883           cp_lexer_consume_token (parser->lexer);
11884
11885           /* Make the argument into a TYPE_PACK_EXPANSION or
11886              EXPR_PACK_EXPANSION. */
11887           argument = make_pack_expansion (argument);
11888         }
11889
11890       if (n_args == alloced)
11891         {
11892           alloced *= 2;
11893
11894           if (arg_ary == fixed_args)
11895             {
11896               arg_ary = XNEWVEC (tree, alloced);
11897               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11898             }
11899           else
11900             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11901         }
11902       arg_ary[n_args++] = argument;
11903     }
11904   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11905
11906   vec = make_tree_vec (n_args);
11907
11908   while (n_args--)
11909     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11910
11911   if (arg_ary != fixed_args)
11912     free (arg_ary);
11913   parser->non_integral_constant_expression_p = saved_non_ice_p;
11914   parser->integral_constant_expression_p = saved_ice_p;
11915   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11916 #ifdef ENABLE_CHECKING
11917   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11918 #endif
11919   return vec;
11920 }
11921
11922 /* Parse a template-argument.
11923
11924    template-argument:
11925      assignment-expression
11926      type-id
11927      id-expression
11928
11929    The representation is that of an assignment-expression, type-id, or
11930    id-expression -- except that the qualified id-expression is
11931    evaluated, so that the value returned is either a DECL or an
11932    OVERLOAD.
11933
11934    Although the standard says "assignment-expression", it forbids
11935    throw-expressions or assignments in the template argument.
11936    Therefore, we use "conditional-expression" instead.  */
11937
11938 static tree
11939 cp_parser_template_argument (cp_parser* parser)
11940 {
11941   tree argument;
11942   bool template_p;
11943   bool address_p;
11944   bool maybe_type_id = false;
11945   cp_token *token = NULL, *argument_start_token = NULL;
11946   cp_id_kind idk;
11947
11948   /* There's really no way to know what we're looking at, so we just
11949      try each alternative in order.
11950
11951        [temp.arg]
11952
11953        In a template-argument, an ambiguity between a type-id and an
11954        expression is resolved to a type-id, regardless of the form of
11955        the corresponding template-parameter.
11956
11957      Therefore, we try a type-id first.  */
11958   cp_parser_parse_tentatively (parser);
11959   argument = cp_parser_template_type_arg (parser);
11960   /* If there was no error parsing the type-id but the next token is a
11961      '>>', our behavior depends on which dialect of C++ we're
11962      parsing. In C++98, we probably found a typo for '> >'. But there
11963      are type-id which are also valid expressions. For instance:
11964
11965      struct X { int operator >> (int); };
11966      template <int V> struct Foo {};
11967      Foo<X () >> 5> r;
11968
11969      Here 'X()' is a valid type-id of a function type, but the user just
11970      wanted to write the expression "X() >> 5". Thus, we remember that we
11971      found a valid type-id, but we still try to parse the argument as an
11972      expression to see what happens. 
11973
11974      In C++0x, the '>>' will be considered two separate '>'
11975      tokens.  */
11976   if (!cp_parser_error_occurred (parser)
11977       && cxx_dialect == cxx98
11978       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11979     {
11980       maybe_type_id = true;
11981       cp_parser_abort_tentative_parse (parser);
11982     }
11983   else
11984     {
11985       /* If the next token isn't a `,' or a `>', then this argument wasn't
11986       really finished. This means that the argument is not a valid
11987       type-id.  */
11988       if (!cp_parser_next_token_ends_template_argument_p (parser))
11989         cp_parser_error (parser, "expected template-argument");
11990       /* If that worked, we're done.  */
11991       if (cp_parser_parse_definitely (parser))
11992         return argument;
11993     }
11994   /* We're still not sure what the argument will be.  */
11995   cp_parser_parse_tentatively (parser);
11996   /* Try a template.  */
11997   argument_start_token = cp_lexer_peek_token (parser->lexer);
11998   argument = cp_parser_id_expression (parser,
11999                                       /*template_keyword_p=*/false,
12000                                       /*check_dependency_p=*/true,
12001                                       &template_p,
12002                                       /*declarator_p=*/false,
12003                                       /*optional_p=*/false);
12004   /* If the next token isn't a `,' or a `>', then this argument wasn't
12005      really finished.  */
12006   if (!cp_parser_next_token_ends_template_argument_p (parser))
12007     cp_parser_error (parser, "expected template-argument");
12008   if (!cp_parser_error_occurred (parser))
12009     {
12010       /* Figure out what is being referred to.  If the id-expression
12011          was for a class template specialization, then we will have a
12012          TYPE_DECL at this point.  There is no need to do name lookup
12013          at this point in that case.  */
12014       if (TREE_CODE (argument) != TYPE_DECL)
12015         argument = cp_parser_lookup_name (parser, argument,
12016                                           none_type,
12017                                           /*is_template=*/template_p,
12018                                           /*is_namespace=*/false,
12019                                           /*check_dependency=*/true,
12020                                           /*ambiguous_decls=*/NULL,
12021                                           argument_start_token->location);
12022       if (TREE_CODE (argument) != TEMPLATE_DECL
12023           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12024         cp_parser_error (parser, "expected template-name");
12025     }
12026   if (cp_parser_parse_definitely (parser))
12027     return argument;
12028   /* It must be a non-type argument.  There permitted cases are given
12029      in [temp.arg.nontype]:
12030
12031      -- an integral constant-expression of integral or enumeration
12032         type; or
12033
12034      -- the name of a non-type template-parameter; or
12035
12036      -- the name of an object or function with external linkage...
12037
12038      -- the address of an object or function with external linkage...
12039
12040      -- a pointer to member...  */
12041   /* Look for a non-type template parameter.  */
12042   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12043     {
12044       cp_parser_parse_tentatively (parser);
12045       argument = cp_parser_primary_expression (parser,
12046                                                /*address_p=*/false,
12047                                                /*cast_p=*/false,
12048                                                /*template_arg_p=*/true,
12049                                                &idk);
12050       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12051           || !cp_parser_next_token_ends_template_argument_p (parser))
12052         cp_parser_simulate_error (parser);
12053       if (cp_parser_parse_definitely (parser))
12054         return argument;
12055     }
12056
12057   /* If the next token is "&", the argument must be the address of an
12058      object or function with external linkage.  */
12059   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12060   if (address_p)
12061     cp_lexer_consume_token (parser->lexer);
12062   /* See if we might have an id-expression.  */
12063   token = cp_lexer_peek_token (parser->lexer);
12064   if (token->type == CPP_NAME
12065       || token->keyword == RID_OPERATOR
12066       || token->type == CPP_SCOPE
12067       || token->type == CPP_TEMPLATE_ID
12068       || token->type == CPP_NESTED_NAME_SPECIFIER)
12069     {
12070       cp_parser_parse_tentatively (parser);
12071       argument = cp_parser_primary_expression (parser,
12072                                                address_p,
12073                                                /*cast_p=*/false,
12074                                                /*template_arg_p=*/true,
12075                                                &idk);
12076       if (cp_parser_error_occurred (parser)
12077           || !cp_parser_next_token_ends_template_argument_p (parser))
12078         cp_parser_abort_tentative_parse (parser);
12079       else
12080         {
12081           tree probe;
12082
12083           if (TREE_CODE (argument) == INDIRECT_REF)
12084             {
12085               gcc_assert (REFERENCE_REF_P (argument));
12086               argument = TREE_OPERAND (argument, 0);
12087             }
12088
12089           /* If we're in a template, we represent a qualified-id referring
12090              to a static data member as a SCOPE_REF even if the scope isn't
12091              dependent so that we can check access control later.  */
12092           probe = argument;
12093           if (TREE_CODE (probe) == SCOPE_REF)
12094             probe = TREE_OPERAND (probe, 1);
12095           if (TREE_CODE (probe) == VAR_DECL)
12096             {
12097               /* A variable without external linkage might still be a
12098                  valid constant-expression, so no error is issued here
12099                  if the external-linkage check fails.  */
12100               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12101                 cp_parser_simulate_error (parser);
12102             }
12103           else if (is_overloaded_fn (argument))
12104             /* All overloaded functions are allowed; if the external
12105                linkage test does not pass, an error will be issued
12106                later.  */
12107             ;
12108           else if (address_p
12109                    && (TREE_CODE (argument) == OFFSET_REF
12110                        || TREE_CODE (argument) == SCOPE_REF))
12111             /* A pointer-to-member.  */
12112             ;
12113           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12114             ;
12115           else
12116             cp_parser_simulate_error (parser);
12117
12118           if (cp_parser_parse_definitely (parser))
12119             {
12120               if (address_p)
12121                 argument = build_x_unary_op (ADDR_EXPR, argument,
12122                                              tf_warning_or_error);
12123               return argument;
12124             }
12125         }
12126     }
12127   /* If the argument started with "&", there are no other valid
12128      alternatives at this point.  */
12129   if (address_p)
12130     {
12131       cp_parser_error (parser, "invalid non-type template argument");
12132       return error_mark_node;
12133     }
12134
12135   /* If the argument wasn't successfully parsed as a type-id followed
12136      by '>>', the argument can only be a constant expression now.
12137      Otherwise, we try parsing the constant-expression tentatively,
12138      because the argument could really be a type-id.  */
12139   if (maybe_type_id)
12140     cp_parser_parse_tentatively (parser);
12141   argument = cp_parser_constant_expression (parser,
12142                                             /*allow_non_constant_p=*/false,
12143                                             /*non_constant_p=*/NULL);
12144   argument = fold_non_dependent_expr (argument);
12145   if (!maybe_type_id)
12146     return argument;
12147   if (!cp_parser_next_token_ends_template_argument_p (parser))
12148     cp_parser_error (parser, "expected template-argument");
12149   if (cp_parser_parse_definitely (parser))
12150     return argument;
12151   /* We did our best to parse the argument as a non type-id, but that
12152      was the only alternative that matched (albeit with a '>' after
12153      it). We can assume it's just a typo from the user, and a
12154      diagnostic will then be issued.  */
12155   return cp_parser_template_type_arg (parser);
12156 }
12157
12158 /* Parse an explicit-instantiation.
12159
12160    explicit-instantiation:
12161      template declaration
12162
12163    Although the standard says `declaration', what it really means is:
12164
12165    explicit-instantiation:
12166      template decl-specifier-seq [opt] declarator [opt] ;
12167
12168    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12169    supposed to be allowed.  A defect report has been filed about this
12170    issue.
12171
12172    GNU Extension:
12173
12174    explicit-instantiation:
12175      storage-class-specifier template
12176        decl-specifier-seq [opt] declarator [opt] ;
12177      function-specifier template
12178        decl-specifier-seq [opt] declarator [opt] ;  */
12179
12180 static void
12181 cp_parser_explicit_instantiation (cp_parser* parser)
12182 {
12183   int declares_class_or_enum;
12184   cp_decl_specifier_seq decl_specifiers;
12185   tree extension_specifier = NULL_TREE;
12186
12187   timevar_push (TV_TEMPLATE_INST);
12188
12189   /* Look for an (optional) storage-class-specifier or
12190      function-specifier.  */
12191   if (cp_parser_allow_gnu_extensions_p (parser))
12192     {
12193       extension_specifier
12194         = cp_parser_storage_class_specifier_opt (parser);
12195       if (!extension_specifier)
12196         extension_specifier
12197           = cp_parser_function_specifier_opt (parser,
12198                                               /*decl_specs=*/NULL);
12199     }
12200
12201   /* Look for the `template' keyword.  */
12202   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12203   /* Let the front end know that we are processing an explicit
12204      instantiation.  */
12205   begin_explicit_instantiation ();
12206   /* [temp.explicit] says that we are supposed to ignore access
12207      control while processing explicit instantiation directives.  */
12208   push_deferring_access_checks (dk_no_check);
12209   /* Parse a decl-specifier-seq.  */
12210   cp_parser_decl_specifier_seq (parser,
12211                                 CP_PARSER_FLAGS_OPTIONAL,
12212                                 &decl_specifiers,
12213                                 &declares_class_or_enum);
12214   /* If there was exactly one decl-specifier, and it declared a class,
12215      and there's no declarator, then we have an explicit type
12216      instantiation.  */
12217   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12218     {
12219       tree type;
12220
12221       type = check_tag_decl (&decl_specifiers);
12222       /* Turn access control back on for names used during
12223          template instantiation.  */
12224       pop_deferring_access_checks ();
12225       if (type)
12226         do_type_instantiation (type, extension_specifier,
12227                                /*complain=*/tf_error);
12228     }
12229   else
12230     {
12231       cp_declarator *declarator;
12232       tree decl;
12233
12234       /* Parse the declarator.  */
12235       declarator
12236         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12237                                 /*ctor_dtor_or_conv_p=*/NULL,
12238                                 /*parenthesized_p=*/NULL,
12239                                 /*member_p=*/false);
12240       if (declares_class_or_enum & 2)
12241         cp_parser_check_for_definition_in_return_type (declarator,
12242                                                        decl_specifiers.type,
12243                                                        decl_specifiers.type_location);
12244       if (declarator != cp_error_declarator)
12245         {
12246           if (decl_specifiers.specs[(int)ds_inline])
12247             permerror (input_location, "explicit instantiation shall not use"
12248                        " %<inline%> specifier");
12249           if (decl_specifiers.specs[(int)ds_constexpr])
12250             permerror (input_location, "explicit instantiation shall not use"
12251                        " %<constexpr%> specifier");
12252
12253           decl = grokdeclarator (declarator, &decl_specifiers,
12254                                  NORMAL, 0, &decl_specifiers.attributes);
12255           /* Turn access control back on for names used during
12256              template instantiation.  */
12257           pop_deferring_access_checks ();
12258           /* Do the explicit instantiation.  */
12259           do_decl_instantiation (decl, extension_specifier);
12260         }
12261       else
12262         {
12263           pop_deferring_access_checks ();
12264           /* Skip the body of the explicit instantiation.  */
12265           cp_parser_skip_to_end_of_statement (parser);
12266         }
12267     }
12268   /* We're done with the instantiation.  */
12269   end_explicit_instantiation ();
12270
12271   cp_parser_consume_semicolon_at_end_of_statement (parser);
12272
12273   timevar_pop (TV_TEMPLATE_INST);
12274 }
12275
12276 /* Parse an explicit-specialization.
12277
12278    explicit-specialization:
12279      template < > declaration
12280
12281    Although the standard says `declaration', what it really means is:
12282
12283    explicit-specialization:
12284      template <> decl-specifier [opt] init-declarator [opt] ;
12285      template <> function-definition
12286      template <> explicit-specialization
12287      template <> template-declaration  */
12288
12289 static void
12290 cp_parser_explicit_specialization (cp_parser* parser)
12291 {
12292   bool need_lang_pop;
12293   cp_token *token = cp_lexer_peek_token (parser->lexer);
12294
12295   /* Look for the `template' keyword.  */
12296   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12297   /* Look for the `<'.  */
12298   cp_parser_require (parser, CPP_LESS, RT_LESS);
12299   /* Look for the `>'.  */
12300   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12301   /* We have processed another parameter list.  */
12302   ++parser->num_template_parameter_lists;
12303   /* [temp]
12304
12305      A template ... explicit specialization ... shall not have C
12306      linkage.  */
12307   if (current_lang_name == lang_name_c)
12308     {
12309       error_at (token->location, "template specialization with C linkage");
12310       /* Give it C++ linkage to avoid confusing other parts of the
12311          front end.  */
12312       push_lang_context (lang_name_cplusplus);
12313       need_lang_pop = true;
12314     }
12315   else
12316     need_lang_pop = false;
12317   /* Let the front end know that we are beginning a specialization.  */
12318   if (!begin_specialization ())
12319     {
12320       end_specialization ();
12321       return;
12322     }
12323
12324   /* If the next keyword is `template', we need to figure out whether
12325      or not we're looking a template-declaration.  */
12326   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12327     {
12328       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12329           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12330         cp_parser_template_declaration_after_export (parser,
12331                                                      /*member_p=*/false);
12332       else
12333         cp_parser_explicit_specialization (parser);
12334     }
12335   else
12336     /* Parse the dependent declaration.  */
12337     cp_parser_single_declaration (parser,
12338                                   /*checks=*/NULL,
12339                                   /*member_p=*/false,
12340                                   /*explicit_specialization_p=*/true,
12341                                   /*friend_p=*/NULL);
12342   /* We're done with the specialization.  */
12343   end_specialization ();
12344   /* For the erroneous case of a template with C linkage, we pushed an
12345      implicit C++ linkage scope; exit that scope now.  */
12346   if (need_lang_pop)
12347     pop_lang_context ();
12348   /* We're done with this parameter list.  */
12349   --parser->num_template_parameter_lists;
12350 }
12351
12352 /* Parse a type-specifier.
12353
12354    type-specifier:
12355      simple-type-specifier
12356      class-specifier
12357      enum-specifier
12358      elaborated-type-specifier
12359      cv-qualifier
12360
12361    GNU Extension:
12362
12363    type-specifier:
12364      __complex__
12365
12366    Returns a representation of the type-specifier.  For a
12367    class-specifier, enum-specifier, or elaborated-type-specifier, a
12368    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12369
12370    The parser flags FLAGS is used to control type-specifier parsing.
12371
12372    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12373    in a decl-specifier-seq.
12374
12375    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12376    class-specifier, enum-specifier, or elaborated-type-specifier, then
12377    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12378    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12379    zero.
12380
12381    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12382    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12383    is set to FALSE.  */
12384
12385 static tree
12386 cp_parser_type_specifier (cp_parser* parser,
12387                           cp_parser_flags flags,
12388                           cp_decl_specifier_seq *decl_specs,
12389                           bool is_declaration,
12390                           int* declares_class_or_enum,
12391                           bool* is_cv_qualifier)
12392 {
12393   tree type_spec = NULL_TREE;
12394   cp_token *token;
12395   enum rid keyword;
12396   cp_decl_spec ds = ds_last;
12397
12398   /* Assume this type-specifier does not declare a new type.  */
12399   if (declares_class_or_enum)
12400     *declares_class_or_enum = 0;
12401   /* And that it does not specify a cv-qualifier.  */
12402   if (is_cv_qualifier)
12403     *is_cv_qualifier = false;
12404   /* Peek at the next token.  */
12405   token = cp_lexer_peek_token (parser->lexer);
12406
12407   /* If we're looking at a keyword, we can use that to guide the
12408      production we choose.  */
12409   keyword = token->keyword;
12410   switch (keyword)
12411     {
12412     case RID_ENUM:
12413       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12414         goto elaborated_type_specifier;
12415
12416       /* Look for the enum-specifier.  */
12417       type_spec = cp_parser_enum_specifier (parser);
12418       /* If that worked, we're done.  */
12419       if (type_spec)
12420         {
12421           if (declares_class_or_enum)
12422             *declares_class_or_enum = 2;
12423           if (decl_specs)
12424             cp_parser_set_decl_spec_type (decl_specs,
12425                                           type_spec,
12426                                           token->location,
12427                                           /*user_defined_p=*/true);
12428           return type_spec;
12429         }
12430       else
12431         goto elaborated_type_specifier;
12432
12433       /* Any of these indicate either a class-specifier, or an
12434          elaborated-type-specifier.  */
12435     case RID_CLASS:
12436     case RID_STRUCT:
12437     case RID_UNION:
12438       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12439         goto elaborated_type_specifier;
12440
12441       /* Parse tentatively so that we can back up if we don't find a
12442          class-specifier.  */
12443       cp_parser_parse_tentatively (parser);
12444       /* Look for the class-specifier.  */
12445       type_spec = cp_parser_class_specifier (parser);
12446       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12447       /* If that worked, we're done.  */
12448       if (cp_parser_parse_definitely (parser))
12449         {
12450           if (declares_class_or_enum)
12451             *declares_class_or_enum = 2;
12452           if (decl_specs)
12453             cp_parser_set_decl_spec_type (decl_specs,
12454                                           type_spec,
12455                                           token->location,
12456                                           /*user_defined_p=*/true);
12457           return type_spec;
12458         }
12459
12460       /* Fall through.  */
12461     elaborated_type_specifier:
12462       /* We're declaring (not defining) a class or enum.  */
12463       if (declares_class_or_enum)
12464         *declares_class_or_enum = 1;
12465
12466       /* Fall through.  */
12467     case RID_TYPENAME:
12468       /* Look for an elaborated-type-specifier.  */
12469       type_spec
12470         = (cp_parser_elaborated_type_specifier
12471            (parser,
12472             decl_specs && decl_specs->specs[(int) ds_friend],
12473             is_declaration));
12474       if (decl_specs)
12475         cp_parser_set_decl_spec_type (decl_specs,
12476                                       type_spec,
12477                                       token->location,
12478                                       /*user_defined_p=*/true);
12479       return type_spec;
12480
12481     case RID_CONST:
12482       ds = ds_const;
12483       if (is_cv_qualifier)
12484         *is_cv_qualifier = true;
12485       break;
12486
12487     case RID_VOLATILE:
12488       ds = ds_volatile;
12489       if (is_cv_qualifier)
12490         *is_cv_qualifier = true;
12491       break;
12492
12493     case RID_RESTRICT:
12494       ds = ds_restrict;
12495       if (is_cv_qualifier)
12496         *is_cv_qualifier = true;
12497       break;
12498
12499     case RID_COMPLEX:
12500       /* The `__complex__' keyword is a GNU extension.  */
12501       ds = ds_complex;
12502       break;
12503
12504     default:
12505       break;
12506     }
12507
12508   /* Handle simple keywords.  */
12509   if (ds != ds_last)
12510     {
12511       if (decl_specs)
12512         {
12513           ++decl_specs->specs[(int)ds];
12514           decl_specs->any_specifiers_p = true;
12515         }
12516       return cp_lexer_consume_token (parser->lexer)->u.value;
12517     }
12518
12519   /* If we do not already have a type-specifier, assume we are looking
12520      at a simple-type-specifier.  */
12521   type_spec = cp_parser_simple_type_specifier (parser,
12522                                                decl_specs,
12523                                                flags);
12524
12525   /* If we didn't find a type-specifier, and a type-specifier was not
12526      optional in this context, issue an error message.  */
12527   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12528     {
12529       cp_parser_error (parser, "expected type specifier");
12530       return error_mark_node;
12531     }
12532
12533   return type_spec;
12534 }
12535
12536 /* Parse a simple-type-specifier.
12537
12538    simple-type-specifier:
12539      :: [opt] nested-name-specifier [opt] type-name
12540      :: [opt] nested-name-specifier template template-id
12541      char
12542      wchar_t
12543      bool
12544      short
12545      int
12546      long
12547      signed
12548      unsigned
12549      float
12550      double
12551      void
12552
12553    C++0x Extension:
12554
12555    simple-type-specifier:
12556      auto
12557      decltype ( expression )   
12558      char16_t
12559      char32_t
12560      __underlying_type ( type-id )
12561
12562    GNU Extension:
12563
12564    simple-type-specifier:
12565      __int128
12566      __typeof__ unary-expression
12567      __typeof__ ( type-id )
12568
12569    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12570    appropriately updated.  */
12571
12572 static tree
12573 cp_parser_simple_type_specifier (cp_parser* parser,
12574                                  cp_decl_specifier_seq *decl_specs,
12575                                  cp_parser_flags flags)
12576 {
12577   tree type = NULL_TREE;
12578   cp_token *token;
12579
12580   /* Peek at the next token.  */
12581   token = cp_lexer_peek_token (parser->lexer);
12582
12583   /* If we're looking at a keyword, things are easy.  */
12584   switch (token->keyword)
12585     {
12586     case RID_CHAR:
12587       if (decl_specs)
12588         decl_specs->explicit_char_p = true;
12589       type = char_type_node;
12590       break;
12591     case RID_CHAR16:
12592       type = char16_type_node;
12593       break;
12594     case RID_CHAR32:
12595       type = char32_type_node;
12596       break;
12597     case RID_WCHAR:
12598       type = wchar_type_node;
12599       break;
12600     case RID_BOOL:
12601       type = boolean_type_node;
12602       break;
12603     case RID_SHORT:
12604       if (decl_specs)
12605         ++decl_specs->specs[(int) ds_short];
12606       type = short_integer_type_node;
12607       break;
12608     case RID_INT:
12609       if (decl_specs)
12610         decl_specs->explicit_int_p = true;
12611       type = integer_type_node;
12612       break;
12613     case RID_INT128:
12614       if (!int128_integer_type_node)
12615         break;
12616       if (decl_specs)
12617         decl_specs->explicit_int128_p = true;
12618       type = int128_integer_type_node;
12619       break;
12620     case RID_LONG:
12621       if (decl_specs)
12622         ++decl_specs->specs[(int) ds_long];
12623       type = long_integer_type_node;
12624       break;
12625     case RID_SIGNED:
12626       if (decl_specs)
12627         ++decl_specs->specs[(int) ds_signed];
12628       type = integer_type_node;
12629       break;
12630     case RID_UNSIGNED:
12631       if (decl_specs)
12632         ++decl_specs->specs[(int) ds_unsigned];
12633       type = unsigned_type_node;
12634       break;
12635     case RID_FLOAT:
12636       type = float_type_node;
12637       break;
12638     case RID_DOUBLE:
12639       type = double_type_node;
12640       break;
12641     case RID_VOID:
12642       type = void_type_node;
12643       break;
12644       
12645     case RID_AUTO:
12646       maybe_warn_cpp0x (CPP0X_AUTO);
12647       type = make_auto ();
12648       break;
12649
12650     case RID_DECLTYPE:
12651       /* Parse the `decltype' type.  */
12652       type = cp_parser_decltype (parser);
12653
12654       if (decl_specs)
12655         cp_parser_set_decl_spec_type (decl_specs, type,
12656                                       token->location,
12657                                       /*user_defined_p=*/true);
12658
12659       return type;
12660
12661     case RID_TYPEOF:
12662       /* Consume the `typeof' token.  */
12663       cp_lexer_consume_token (parser->lexer);
12664       /* Parse the operand to `typeof'.  */
12665       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12666       /* If it is not already a TYPE, take its type.  */
12667       if (!TYPE_P (type))
12668         type = finish_typeof (type);
12669
12670       if (decl_specs)
12671         cp_parser_set_decl_spec_type (decl_specs, type,
12672                                       token->location,
12673                                       /*user_defined_p=*/true);
12674
12675       return type;
12676
12677     case RID_UNDERLYING_TYPE:
12678       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12679
12680       if (decl_specs)
12681         cp_parser_set_decl_spec_type (decl_specs, type,
12682                                       token->location,
12683                                       /*user_defined_p=*/true);
12684
12685       return type;
12686
12687     default:
12688       break;
12689     }
12690
12691   /* If the type-specifier was for a built-in type, we're done.  */
12692   if (type)
12693     {
12694       /* Record the type.  */
12695       if (decl_specs
12696           && (token->keyword != RID_SIGNED
12697               && token->keyword != RID_UNSIGNED
12698               && token->keyword != RID_SHORT
12699               && token->keyword != RID_LONG))
12700         cp_parser_set_decl_spec_type (decl_specs,
12701                                       type,
12702                                       token->location,
12703                                       /*user_defined=*/false);
12704       if (decl_specs)
12705         decl_specs->any_specifiers_p = true;
12706
12707       /* Consume the token.  */
12708       cp_lexer_consume_token (parser->lexer);
12709
12710       /* There is no valid C++ program where a non-template type is
12711          followed by a "<".  That usually indicates that the user thought
12712          that the type was a template.  */
12713       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12714
12715       return TYPE_NAME (type);
12716     }
12717
12718   /* The type-specifier must be a user-defined type.  */
12719   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12720     {
12721       bool qualified_p;
12722       bool global_p;
12723
12724       /* Don't gobble tokens or issue error messages if this is an
12725          optional type-specifier.  */
12726       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12727         cp_parser_parse_tentatively (parser);
12728
12729       /* Look for the optional `::' operator.  */
12730       global_p
12731         = (cp_parser_global_scope_opt (parser,
12732                                        /*current_scope_valid_p=*/false)
12733            != NULL_TREE);
12734       /* Look for the nested-name specifier.  */
12735       qualified_p
12736         = (cp_parser_nested_name_specifier_opt (parser,
12737                                                 /*typename_keyword_p=*/false,
12738                                                 /*check_dependency_p=*/true,
12739                                                 /*type_p=*/false,
12740                                                 /*is_declaration=*/false)
12741            != NULL_TREE);
12742       token = cp_lexer_peek_token (parser->lexer);
12743       /* If we have seen a nested-name-specifier, and the next token
12744          is `template', then we are using the template-id production.  */
12745       if (parser->scope
12746           && cp_parser_optional_template_keyword (parser))
12747         {
12748           /* Look for the template-id.  */
12749           type = cp_parser_template_id (parser,
12750                                         /*template_keyword_p=*/true,
12751                                         /*check_dependency_p=*/true,
12752                                         /*is_declaration=*/false);
12753           /* If the template-id did not name a type, we are out of
12754              luck.  */
12755           if (TREE_CODE (type) != TYPE_DECL)
12756             {
12757               cp_parser_error (parser, "expected template-id for type");
12758               type = NULL_TREE;
12759             }
12760         }
12761       /* Otherwise, look for a type-name.  */
12762       else
12763         type = cp_parser_type_name (parser);
12764       /* Keep track of all name-lookups performed in class scopes.  */
12765       if (type
12766           && !global_p
12767           && !qualified_p
12768           && TREE_CODE (type) == TYPE_DECL
12769           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12770         maybe_note_name_used_in_class (DECL_NAME (type), type);
12771       /* If it didn't work out, we don't have a TYPE.  */
12772       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12773           && !cp_parser_parse_definitely (parser))
12774         type = NULL_TREE;
12775       if (type && decl_specs)
12776         cp_parser_set_decl_spec_type (decl_specs, type,
12777                                       token->location,
12778                                       /*user_defined=*/true);
12779     }
12780
12781   /* If we didn't get a type-name, issue an error message.  */
12782   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12783     {
12784       cp_parser_error (parser, "expected type-name");
12785       return error_mark_node;
12786     }
12787
12788   if (type && type != error_mark_node)
12789     {
12790       /* See if TYPE is an Objective-C type, and if so, parse and
12791          accept any protocol references following it.  Do this before
12792          the cp_parser_check_for_invalid_template_id() call, because
12793          Objective-C types can be followed by '<...>' which would
12794          enclose protocol names rather than template arguments, and so
12795          everything is fine.  */
12796       if (c_dialect_objc () && !parser->scope
12797           && (objc_is_id (type) || objc_is_class_name (type)))
12798         {
12799           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12800           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12801
12802           /* Clobber the "unqualified" type previously entered into
12803              DECL_SPECS with the new, improved protocol-qualified version.  */
12804           if (decl_specs)
12805             decl_specs->type = qual_type;
12806
12807           return qual_type;
12808         }
12809
12810       /* There is no valid C++ program where a non-template type is
12811          followed by a "<".  That usually indicates that the user
12812          thought that the type was a template.  */
12813       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12814                                                token->location);
12815     }
12816
12817   return type;
12818 }
12819
12820 /* Parse a type-name.
12821
12822    type-name:
12823      class-name
12824      enum-name
12825      typedef-name
12826
12827    enum-name:
12828      identifier
12829
12830    typedef-name:
12831      identifier
12832
12833    Returns a TYPE_DECL for the type.  */
12834
12835 static tree
12836 cp_parser_type_name (cp_parser* parser)
12837 {
12838   tree type_decl;
12839
12840   /* We can't know yet whether it is a class-name or not.  */
12841   cp_parser_parse_tentatively (parser);
12842   /* Try a class-name.  */
12843   type_decl = cp_parser_class_name (parser,
12844                                     /*typename_keyword_p=*/false,
12845                                     /*template_keyword_p=*/false,
12846                                     none_type,
12847                                     /*check_dependency_p=*/true,
12848                                     /*class_head_p=*/false,
12849                                     /*is_declaration=*/false);
12850   /* If it's not a class-name, keep looking.  */
12851   if (!cp_parser_parse_definitely (parser))
12852     {
12853       /* It must be a typedef-name or an enum-name.  */
12854       return cp_parser_nonclass_name (parser);
12855     }
12856
12857   return type_decl;
12858 }
12859
12860 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12861
12862    enum-name:
12863      identifier
12864
12865    typedef-name:
12866      identifier
12867
12868    Returns a TYPE_DECL for the type.  */
12869
12870 static tree
12871 cp_parser_nonclass_name (cp_parser* parser)
12872 {
12873   tree type_decl;
12874   tree identifier;
12875
12876   cp_token *token = cp_lexer_peek_token (parser->lexer);
12877   identifier = cp_parser_identifier (parser);
12878   if (identifier == error_mark_node)
12879     return error_mark_node;
12880
12881   /* Look up the type-name.  */
12882   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12883
12884   if (TREE_CODE (type_decl) != TYPE_DECL
12885       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12886     {
12887       /* See if this is an Objective-C type.  */
12888       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12889       tree type = objc_get_protocol_qualified_type (identifier, protos);
12890       if (type)
12891         type_decl = TYPE_NAME (type);
12892     }
12893
12894   /* Issue an error if we did not find a type-name.  */
12895   if (TREE_CODE (type_decl) != TYPE_DECL
12896       /* In Objective-C, we have the complication that class names are
12897          normally type names and start declarations (eg, the
12898          "NSObject" in "NSObject *object;"), but can be used in an
12899          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12900          is an expression.  So, a classname followed by a dot is not a
12901          valid type-name.  */
12902       || (objc_is_class_name (TREE_TYPE (type_decl))
12903           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12904     {
12905       if (!cp_parser_simulate_error (parser))
12906         cp_parser_name_lookup_error (parser, identifier, type_decl,
12907                                      NLE_TYPE, token->location);
12908       return error_mark_node;
12909     }
12910   /* Remember that the name was used in the definition of the
12911      current class so that we can check later to see if the
12912      meaning would have been different after the class was
12913      entirely defined.  */
12914   else if (type_decl != error_mark_node
12915            && !parser->scope)
12916     maybe_note_name_used_in_class (identifier, type_decl);
12917   
12918   return type_decl;
12919 }
12920
12921 /* Parse an elaborated-type-specifier.  Note that the grammar given
12922    here incorporates the resolution to DR68.
12923
12924    elaborated-type-specifier:
12925      class-key :: [opt] nested-name-specifier [opt] identifier
12926      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12927      enum-key :: [opt] nested-name-specifier [opt] identifier
12928      typename :: [opt] nested-name-specifier identifier
12929      typename :: [opt] nested-name-specifier template [opt]
12930        template-id
12931
12932    GNU extension:
12933
12934    elaborated-type-specifier:
12935      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12936      class-key attributes :: [opt] nested-name-specifier [opt]
12937                template [opt] template-id
12938      enum attributes :: [opt] nested-name-specifier [opt] identifier
12939
12940    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12941    declared `friend'.  If IS_DECLARATION is TRUE, then this
12942    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12943    something is being declared.
12944
12945    Returns the TYPE specified.  */
12946
12947 static tree
12948 cp_parser_elaborated_type_specifier (cp_parser* parser,
12949                                      bool is_friend,
12950                                      bool is_declaration)
12951 {
12952   enum tag_types tag_type;
12953   tree identifier;
12954   tree type = NULL_TREE;
12955   tree attributes = NULL_TREE;
12956   tree globalscope;
12957   cp_token *token = NULL;
12958
12959   /* See if we're looking at the `enum' keyword.  */
12960   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12961     {
12962       /* Consume the `enum' token.  */
12963       cp_lexer_consume_token (parser->lexer);
12964       /* Remember that it's an enumeration type.  */
12965       tag_type = enum_type;
12966       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12967          enums) is used here.  */
12968       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12969           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12970         {
12971             pedwarn (input_location, 0, "elaborated-type-specifier "
12972                       "for a scoped enum must not use the %<%D%> keyword",
12973                       cp_lexer_peek_token (parser->lexer)->u.value);
12974           /* Consume the `struct' or `class' and parse it anyway.  */
12975           cp_lexer_consume_token (parser->lexer);
12976         }
12977       /* Parse the attributes.  */
12978       attributes = cp_parser_attributes_opt (parser);
12979     }
12980   /* Or, it might be `typename'.  */
12981   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12982                                            RID_TYPENAME))
12983     {
12984       /* Consume the `typename' token.  */
12985       cp_lexer_consume_token (parser->lexer);
12986       /* Remember that it's a `typename' type.  */
12987       tag_type = typename_type;
12988     }
12989   /* Otherwise it must be a class-key.  */
12990   else
12991     {
12992       tag_type = cp_parser_class_key (parser);
12993       if (tag_type == none_type)
12994         return error_mark_node;
12995       /* Parse the attributes.  */
12996       attributes = cp_parser_attributes_opt (parser);
12997     }
12998
12999   /* Look for the `::' operator.  */
13000   globalscope =  cp_parser_global_scope_opt (parser,
13001                                              /*current_scope_valid_p=*/false);
13002   /* Look for the nested-name-specifier.  */
13003   if (tag_type == typename_type && !globalscope)
13004     {
13005       if (!cp_parser_nested_name_specifier (parser,
13006                                            /*typename_keyword_p=*/true,
13007                                            /*check_dependency_p=*/true,
13008                                            /*type_p=*/true,
13009                                             is_declaration))
13010         return error_mark_node;
13011     }
13012   else
13013     /* Even though `typename' is not present, the proposed resolution
13014        to Core Issue 180 says that in `class A<T>::B', `B' should be
13015        considered a type-name, even if `A<T>' is dependent.  */
13016     cp_parser_nested_name_specifier_opt (parser,
13017                                          /*typename_keyword_p=*/true,
13018                                          /*check_dependency_p=*/true,
13019                                          /*type_p=*/true,
13020                                          is_declaration);
13021  /* For everything but enumeration types, consider a template-id.
13022     For an enumeration type, consider only a plain identifier.  */
13023   if (tag_type != enum_type)
13024     {
13025       bool template_p = false;
13026       tree decl;
13027
13028       /* Allow the `template' keyword.  */
13029       template_p = cp_parser_optional_template_keyword (parser);
13030       /* If we didn't see `template', we don't know if there's a
13031          template-id or not.  */
13032       if (!template_p)
13033         cp_parser_parse_tentatively (parser);
13034       /* Parse the template-id.  */
13035       token = cp_lexer_peek_token (parser->lexer);
13036       decl = cp_parser_template_id (parser, template_p,
13037                                     /*check_dependency_p=*/true,
13038                                     is_declaration);
13039       /* If we didn't find a template-id, look for an ordinary
13040          identifier.  */
13041       if (!template_p && !cp_parser_parse_definitely (parser))
13042         ;
13043       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13044          in effect, then we must assume that, upon instantiation, the
13045          template will correspond to a class.  */
13046       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13047                && tag_type == typename_type)
13048         type = make_typename_type (parser->scope, decl,
13049                                    typename_type,
13050                                    /*complain=*/tf_error);
13051       /* If the `typename' keyword is in effect and DECL is not a type
13052          decl. Then type is non existant.   */
13053       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13054         type = NULL_TREE; 
13055       else 
13056         type = TREE_TYPE (decl);
13057     }
13058
13059   if (!type)
13060     {
13061       token = cp_lexer_peek_token (parser->lexer);
13062       identifier = cp_parser_identifier (parser);
13063
13064       if (identifier == error_mark_node)
13065         {
13066           parser->scope = NULL_TREE;
13067           return error_mark_node;
13068         }
13069
13070       /* For a `typename', we needn't call xref_tag.  */
13071       if (tag_type == typename_type
13072           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13073         return cp_parser_make_typename_type (parser, parser->scope,
13074                                              identifier,
13075                                              token->location);
13076       /* Look up a qualified name in the usual way.  */
13077       if (parser->scope)
13078         {
13079           tree decl;
13080           tree ambiguous_decls;
13081
13082           decl = cp_parser_lookup_name (parser, identifier,
13083                                         tag_type,
13084                                         /*is_template=*/false,
13085                                         /*is_namespace=*/false,
13086                                         /*check_dependency=*/true,
13087                                         &ambiguous_decls,
13088                                         token->location);
13089
13090           /* If the lookup was ambiguous, an error will already have been
13091              issued.  */
13092           if (ambiguous_decls)
13093             return error_mark_node;
13094
13095           /* If we are parsing friend declaration, DECL may be a
13096              TEMPLATE_DECL tree node here.  However, we need to check
13097              whether this TEMPLATE_DECL results in valid code.  Consider
13098              the following example:
13099
13100                namespace N {
13101                  template <class T> class C {};
13102                }
13103                class X {
13104                  template <class T> friend class N::C; // #1, valid code
13105                };
13106                template <class T> class Y {
13107                  friend class N::C;                    // #2, invalid code
13108                };
13109
13110              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13111              name lookup of `N::C'.  We see that friend declaration must
13112              be template for the code to be valid.  Note that
13113              processing_template_decl does not work here since it is
13114              always 1 for the above two cases.  */
13115
13116           decl = (cp_parser_maybe_treat_template_as_class
13117                   (decl, /*tag_name_p=*/is_friend
13118                          && parser->num_template_parameter_lists));
13119
13120           if (TREE_CODE (decl) != TYPE_DECL)
13121             {
13122               cp_parser_diagnose_invalid_type_name (parser,
13123                                                     parser->scope,
13124                                                     identifier,
13125                                                     token->location);
13126               return error_mark_node;
13127             }
13128
13129           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13130             {
13131               bool allow_template = (parser->num_template_parameter_lists
13132                                       || DECL_SELF_REFERENCE_P (decl));
13133               type = check_elaborated_type_specifier (tag_type, decl, 
13134                                                       allow_template);
13135
13136               if (type == error_mark_node)
13137                 return error_mark_node;
13138             }
13139
13140           /* Forward declarations of nested types, such as
13141
13142                class C1::C2;
13143                class C1::C2::C3;
13144
13145              are invalid unless all components preceding the final '::'
13146              are complete.  If all enclosing types are complete, these
13147              declarations become merely pointless.
13148
13149              Invalid forward declarations of nested types are errors
13150              caught elsewhere in parsing.  Those that are pointless arrive
13151              here.  */
13152
13153           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13154               && !is_friend && !processing_explicit_instantiation)
13155             warning (0, "declaration %qD does not declare anything", decl);
13156
13157           type = TREE_TYPE (decl);
13158         }
13159       else
13160         {
13161           /* An elaborated-type-specifier sometimes introduces a new type and
13162              sometimes names an existing type.  Normally, the rule is that it
13163              introduces a new type only if there is not an existing type of
13164              the same name already in scope.  For example, given:
13165
13166                struct S {};
13167                void f() { struct S s; }
13168
13169              the `struct S' in the body of `f' is the same `struct S' as in
13170              the global scope; the existing definition is used.  However, if
13171              there were no global declaration, this would introduce a new
13172              local class named `S'.
13173
13174              An exception to this rule applies to the following code:
13175
13176                namespace N { struct S; }
13177
13178              Here, the elaborated-type-specifier names a new type
13179              unconditionally; even if there is already an `S' in the
13180              containing scope this declaration names a new type.
13181              This exception only applies if the elaborated-type-specifier
13182              forms the complete declaration:
13183
13184                [class.name]
13185
13186                A declaration consisting solely of `class-key identifier ;' is
13187                either a redeclaration of the name in the current scope or a
13188                forward declaration of the identifier as a class name.  It
13189                introduces the name into the current scope.
13190
13191              We are in this situation precisely when the next token is a `;'.
13192
13193              An exception to the exception is that a `friend' declaration does
13194              *not* name a new type; i.e., given:
13195
13196                struct S { friend struct T; };
13197
13198              `T' is not a new type in the scope of `S'.
13199
13200              Also, `new struct S' or `sizeof (struct S)' never results in the
13201              definition of a new type; a new type can only be declared in a
13202              declaration context.  */
13203
13204           tag_scope ts;
13205           bool template_p;
13206
13207           if (is_friend)
13208             /* Friends have special name lookup rules.  */
13209             ts = ts_within_enclosing_non_class;
13210           else if (is_declaration
13211                    && cp_lexer_next_token_is (parser->lexer,
13212                                               CPP_SEMICOLON))
13213             /* This is a `class-key identifier ;' */
13214             ts = ts_current;
13215           else
13216             ts = ts_global;
13217
13218           template_p =
13219             (parser->num_template_parameter_lists
13220              && (cp_parser_next_token_starts_class_definition_p (parser)
13221                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13222           /* An unqualified name was used to reference this type, so
13223              there were no qualifying templates.  */
13224           if (!cp_parser_check_template_parameters (parser,
13225                                                     /*num_templates=*/0,
13226                                                     token->location,
13227                                                     /*declarator=*/NULL))
13228             return error_mark_node;
13229           type = xref_tag (tag_type, identifier, ts, template_p);
13230         }
13231     }
13232
13233   if (type == error_mark_node)
13234     return error_mark_node;
13235
13236   /* Allow attributes on forward declarations of classes.  */
13237   if (attributes)
13238     {
13239       if (TREE_CODE (type) == TYPENAME_TYPE)
13240         warning (OPT_Wattributes,
13241                  "attributes ignored on uninstantiated type");
13242       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13243                && ! processing_explicit_instantiation)
13244         warning (OPT_Wattributes,
13245                  "attributes ignored on template instantiation");
13246       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13247         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13248       else
13249         warning (OPT_Wattributes,
13250                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13251     }
13252
13253   if (tag_type != enum_type)
13254     cp_parser_check_class_key (tag_type, type);
13255
13256   /* A "<" cannot follow an elaborated type specifier.  If that
13257      happens, the user was probably trying to form a template-id.  */
13258   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13259
13260   return type;
13261 }
13262
13263 /* Parse an enum-specifier.
13264
13265    enum-specifier:
13266      enum-head { enumerator-list [opt] }
13267
13268    enum-head:
13269      enum-key identifier [opt] enum-base [opt]
13270      enum-key nested-name-specifier identifier enum-base [opt]
13271
13272    enum-key:
13273      enum
13274      enum class   [C++0x]
13275      enum struct  [C++0x]
13276
13277    enum-base:   [C++0x]
13278      : type-specifier-seq
13279
13280    opaque-enum-specifier:
13281      enum-key identifier enum-base [opt] ;
13282
13283    GNU Extensions:
13284      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13285        { enumerator-list [opt] }attributes[opt]
13286
13287    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13288    if the token stream isn't an enum-specifier after all.  */
13289
13290 static tree
13291 cp_parser_enum_specifier (cp_parser* parser)
13292 {
13293   tree identifier;
13294   tree type = NULL_TREE;
13295   tree prev_scope;
13296   tree nested_name_specifier = NULL_TREE;
13297   tree attributes;
13298   bool scoped_enum_p = false;
13299   bool has_underlying_type = false;
13300   bool nested_being_defined = false;
13301   bool new_value_list = false;
13302   bool is_new_type = false;
13303   bool is_anonymous = false;
13304   tree underlying_type = NULL_TREE;
13305   cp_token *type_start_token = NULL;
13306   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13307
13308   parser->colon_corrects_to_scope_p = false;
13309
13310   /* Parse tentatively so that we can back up if we don't find a
13311      enum-specifier.  */
13312   cp_parser_parse_tentatively (parser);
13313
13314   /* Caller guarantees that the current token is 'enum', an identifier
13315      possibly follows, and the token after that is an opening brace.
13316      If we don't have an identifier, fabricate an anonymous name for
13317      the enumeration being defined.  */
13318   cp_lexer_consume_token (parser->lexer);
13319
13320   /* Parse the "class" or "struct", which indicates a scoped
13321      enumeration type in C++0x.  */
13322   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13323       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13324     {
13325       if (cxx_dialect < cxx0x)
13326         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13327
13328       /* Consume the `struct' or `class' token.  */
13329       cp_lexer_consume_token (parser->lexer);
13330
13331       scoped_enum_p = true;
13332     }
13333
13334   attributes = cp_parser_attributes_opt (parser);
13335
13336   /* Clear the qualification.  */
13337   parser->scope = NULL_TREE;
13338   parser->qualifying_scope = NULL_TREE;
13339   parser->object_scope = NULL_TREE;
13340
13341   /* Figure out in what scope the declaration is being placed.  */
13342   prev_scope = current_scope ();
13343
13344   type_start_token = cp_lexer_peek_token (parser->lexer);
13345
13346   push_deferring_access_checks (dk_no_check);
13347   nested_name_specifier
13348       = cp_parser_nested_name_specifier_opt (parser,
13349                                              /*typename_keyword_p=*/true,
13350                                              /*check_dependency_p=*/false,
13351                                              /*type_p=*/false,
13352                                              /*is_declaration=*/false);
13353
13354   if (nested_name_specifier)
13355     {
13356       tree name;
13357
13358       identifier = cp_parser_identifier (parser);
13359       name =  cp_parser_lookup_name (parser, identifier,
13360                                      enum_type,
13361                                      /*is_template=*/false,
13362                                      /*is_namespace=*/false,
13363                                      /*check_dependency=*/true,
13364                                      /*ambiguous_decls=*/NULL,
13365                                      input_location);
13366       if (name)
13367         {
13368           type = TREE_TYPE (name);
13369           if (TREE_CODE (type) == TYPENAME_TYPE)
13370             {
13371               /* Are template enums allowed in ISO? */
13372               if (template_parm_scope_p ())
13373                 pedwarn (type_start_token->location, OPT_pedantic,
13374                          "%qD is an enumeration template", name);
13375               /* ignore a typename reference, for it will be solved by name
13376                  in start_enum.  */
13377               type = NULL_TREE;
13378             }
13379         }
13380       else
13381         error_at (type_start_token->location,
13382                   "%qD is not an enumerator-name", identifier);
13383     }
13384   else
13385     {
13386       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13387         identifier = cp_parser_identifier (parser);
13388       else
13389         {
13390           identifier = make_anon_name ();
13391           is_anonymous = true;
13392         }
13393     }
13394   pop_deferring_access_checks ();
13395
13396   /* Check for the `:' that denotes a specified underlying type in C++0x.
13397      Note that a ':' could also indicate a bitfield width, however.  */
13398   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13399     {
13400       cp_decl_specifier_seq type_specifiers;
13401
13402       /* Consume the `:'.  */
13403       cp_lexer_consume_token (parser->lexer);
13404
13405       /* Parse the type-specifier-seq.  */
13406       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13407                                     /*is_trailing_return=*/false,
13408                                     &type_specifiers);
13409
13410       /* At this point this is surely not elaborated type specifier.  */
13411       if (!cp_parser_parse_definitely (parser))
13412         return NULL_TREE;
13413
13414       if (cxx_dialect < cxx0x)
13415         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13416
13417       has_underlying_type = true;
13418
13419       /* If that didn't work, stop.  */
13420       if (type_specifiers.type != error_mark_node)
13421         {
13422           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13423                                             /*initialized=*/0, NULL);
13424           if (underlying_type == error_mark_node)
13425             underlying_type = NULL_TREE;
13426         }
13427     }
13428
13429   /* Look for the `{' but don't consume it yet.  */
13430   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13431     {
13432       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13433         {
13434           cp_parser_error (parser, "expected %<{%>");
13435           if (has_underlying_type)
13436             {
13437               type = NULL_TREE;
13438               goto out;
13439             }
13440         }
13441       /* An opaque-enum-specifier must have a ';' here.  */
13442       if ((scoped_enum_p || underlying_type)
13443           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13444         {
13445           cp_parser_error (parser, "expected %<;%> or %<{%>");
13446           if (has_underlying_type)
13447             {
13448               type = NULL_TREE;
13449               goto out;
13450             }
13451         }
13452     }
13453
13454   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13455     return NULL_TREE;
13456
13457   if (nested_name_specifier)
13458     {
13459       if (CLASS_TYPE_P (nested_name_specifier))
13460         {
13461           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13462           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13463           push_scope (nested_name_specifier);
13464         }
13465       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13466         {
13467           push_nested_namespace (nested_name_specifier);
13468         }
13469     }
13470
13471   /* Issue an error message if type-definitions are forbidden here.  */
13472   if (!cp_parser_check_type_definition (parser))
13473     type = error_mark_node;
13474   else
13475     /* Create the new type.  We do this before consuming the opening
13476        brace so the enum will be recorded as being on the line of its
13477        tag (or the 'enum' keyword, if there is no tag).  */
13478     type = start_enum (identifier, type, underlying_type,
13479                        scoped_enum_p, &is_new_type);
13480
13481   /* If the next token is not '{' it is an opaque-enum-specifier or an
13482      elaborated-type-specifier.  */
13483   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13484     {
13485       timevar_push (TV_PARSE_ENUM);
13486       if (nested_name_specifier)
13487         {
13488           /* The following catches invalid code such as:
13489              enum class S<int>::E { A, B, C }; */
13490           if (!processing_specialization
13491               && CLASS_TYPE_P (nested_name_specifier)
13492               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13493             error_at (type_start_token->location, "cannot add an enumerator "
13494                       "list to a template instantiation");
13495
13496           /* If that scope does not contain the scope in which the
13497              class was originally declared, the program is invalid.  */
13498           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13499             {
13500               if (at_namespace_scope_p ())
13501                 error_at (type_start_token->location,
13502                           "declaration of %qD in namespace %qD which does not "
13503                           "enclose %qD",
13504                           type, prev_scope, nested_name_specifier);
13505               else
13506                 error_at (type_start_token->location,
13507                           "declaration of %qD in %qD which does not enclose %qD",
13508                           type, prev_scope, nested_name_specifier);
13509               type = error_mark_node;
13510             }
13511         }
13512
13513       if (scoped_enum_p)
13514         begin_scope (sk_scoped_enum, type);
13515
13516       /* Consume the opening brace.  */
13517       cp_lexer_consume_token (parser->lexer);
13518
13519       if (type == error_mark_node)
13520         ; /* Nothing to add */
13521       else if (OPAQUE_ENUM_P (type)
13522                || (cxx_dialect > cxx98 && processing_specialization))
13523         {
13524           new_value_list = true;
13525           SET_OPAQUE_ENUM_P (type, false);
13526           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13527         }
13528       else
13529         {
13530           error_at (type_start_token->location, "multiple definition of %q#T", type);
13531           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13532                     "previous definition here");
13533           type = error_mark_node;
13534         }
13535
13536       if (type == error_mark_node)
13537         cp_parser_skip_to_end_of_block_or_statement (parser);
13538       /* If the next token is not '}', then there are some enumerators.  */
13539       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13540         cp_parser_enumerator_list (parser, type);
13541
13542       /* Consume the final '}'.  */
13543       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13544
13545       if (scoped_enum_p)
13546         finish_scope ();
13547       timevar_pop (TV_PARSE_ENUM);
13548     }
13549   else
13550     {
13551       /* If a ';' follows, then it is an opaque-enum-specifier
13552         and additional restrictions apply.  */
13553       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13554         {
13555           if (is_anonymous)
13556             error_at (type_start_token->location,
13557                       "opaque-enum-specifier without name");
13558           else if (nested_name_specifier)
13559             error_at (type_start_token->location,
13560                       "opaque-enum-specifier must use a simple identifier");
13561         }
13562     }
13563
13564   /* Look for trailing attributes to apply to this enumeration, and
13565      apply them if appropriate.  */
13566   if (cp_parser_allow_gnu_extensions_p (parser))
13567     {
13568       tree trailing_attr = cp_parser_attributes_opt (parser);
13569       trailing_attr = chainon (trailing_attr, attributes);
13570       cplus_decl_attributes (&type,
13571                              trailing_attr,
13572                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13573     }
13574
13575   /* Finish up the enumeration.  */
13576   if (type != error_mark_node)
13577     {
13578       if (new_value_list)
13579         finish_enum_value_list (type);
13580       if (is_new_type)
13581         finish_enum (type);
13582     }
13583
13584   if (nested_name_specifier)
13585     {
13586       if (CLASS_TYPE_P (nested_name_specifier))
13587         {
13588           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13589           pop_scope (nested_name_specifier);
13590         }
13591       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13592         {
13593           pop_nested_namespace (nested_name_specifier);
13594         }
13595     }
13596  out:
13597   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13598   return type;
13599 }
13600
13601 /* Parse an enumerator-list.  The enumerators all have the indicated
13602    TYPE.
13603
13604    enumerator-list:
13605      enumerator-definition
13606      enumerator-list , enumerator-definition  */
13607
13608 static void
13609 cp_parser_enumerator_list (cp_parser* parser, tree type)
13610 {
13611   while (true)
13612     {
13613       /* Parse an enumerator-definition.  */
13614       cp_parser_enumerator_definition (parser, type);
13615
13616       /* If the next token is not a ',', we've reached the end of
13617          the list.  */
13618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13619         break;
13620       /* Otherwise, consume the `,' and keep going.  */
13621       cp_lexer_consume_token (parser->lexer);
13622       /* If the next token is a `}', there is a trailing comma.  */
13623       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13624         {
13625           if (!in_system_header)
13626             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13627           break;
13628         }
13629     }
13630 }
13631
13632 /* Parse an enumerator-definition.  The enumerator has the indicated
13633    TYPE.
13634
13635    enumerator-definition:
13636      enumerator
13637      enumerator = constant-expression
13638
13639    enumerator:
13640      identifier  */
13641
13642 static void
13643 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13644 {
13645   tree identifier;
13646   tree value;
13647   location_t loc;
13648
13649   /* Save the input location because we are interested in the location
13650      of the identifier and not the location of the explicit value.  */
13651   loc = cp_lexer_peek_token (parser->lexer)->location;
13652
13653   /* Look for the identifier.  */
13654   identifier = cp_parser_identifier (parser);
13655   if (identifier == error_mark_node)
13656     return;
13657
13658   /* If the next token is an '=', then there is an explicit value.  */
13659   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13660     {
13661       /* Consume the `=' token.  */
13662       cp_lexer_consume_token (parser->lexer);
13663       /* Parse the value.  */
13664       value = cp_parser_constant_expression (parser,
13665                                              /*allow_non_constant_p=*/false,
13666                                              NULL);
13667     }
13668   else
13669     value = NULL_TREE;
13670
13671   /* If we are processing a template, make sure the initializer of the
13672      enumerator doesn't contain any bare template parameter pack.  */
13673   if (check_for_bare_parameter_packs (value))
13674     value = error_mark_node;
13675
13676   /* integral_constant_value will pull out this expression, so make sure
13677      it's folded as appropriate.  */
13678   value = fold_non_dependent_expr (value);
13679
13680   /* Create the enumerator.  */
13681   build_enumerator (identifier, value, type, loc);
13682 }
13683
13684 /* Parse a namespace-name.
13685
13686    namespace-name:
13687      original-namespace-name
13688      namespace-alias
13689
13690    Returns the NAMESPACE_DECL for the namespace.  */
13691
13692 static tree
13693 cp_parser_namespace_name (cp_parser* parser)
13694 {
13695   tree identifier;
13696   tree namespace_decl;
13697
13698   cp_token *token = cp_lexer_peek_token (parser->lexer);
13699
13700   /* Get the name of the namespace.  */
13701   identifier = cp_parser_identifier (parser);
13702   if (identifier == error_mark_node)
13703     return error_mark_node;
13704
13705   /* Look up the identifier in the currently active scope.  Look only
13706      for namespaces, due to:
13707
13708        [basic.lookup.udir]
13709
13710        When looking up a namespace-name in a using-directive or alias
13711        definition, only namespace names are considered.
13712
13713      And:
13714
13715        [basic.lookup.qual]
13716
13717        During the lookup of a name preceding the :: scope resolution
13718        operator, object, function, and enumerator names are ignored.
13719
13720      (Note that cp_parser_qualifying_entity only calls this
13721      function if the token after the name is the scope resolution
13722      operator.)  */
13723   namespace_decl = cp_parser_lookup_name (parser, identifier,
13724                                           none_type,
13725                                           /*is_template=*/false,
13726                                           /*is_namespace=*/true,
13727                                           /*check_dependency=*/true,
13728                                           /*ambiguous_decls=*/NULL,
13729                                           token->location);
13730   /* If it's not a namespace, issue an error.  */
13731   if (namespace_decl == error_mark_node
13732       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13733     {
13734       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13735         error_at (token->location, "%qD is not a namespace-name", identifier);
13736       cp_parser_error (parser, "expected namespace-name");
13737       namespace_decl = error_mark_node;
13738     }
13739
13740   return namespace_decl;
13741 }
13742
13743 /* Parse a namespace-definition.
13744
13745    namespace-definition:
13746      named-namespace-definition
13747      unnamed-namespace-definition
13748
13749    named-namespace-definition:
13750      original-namespace-definition
13751      extension-namespace-definition
13752
13753    original-namespace-definition:
13754      namespace identifier { namespace-body }
13755
13756    extension-namespace-definition:
13757      namespace original-namespace-name { namespace-body }
13758
13759    unnamed-namespace-definition:
13760      namespace { namespace-body } */
13761
13762 static void
13763 cp_parser_namespace_definition (cp_parser* parser)
13764 {
13765   tree identifier, attribs;
13766   bool has_visibility;
13767   bool is_inline;
13768
13769   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13770     {
13771       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13772       is_inline = true;
13773       cp_lexer_consume_token (parser->lexer);
13774     }
13775   else
13776     is_inline = false;
13777
13778   /* Look for the `namespace' keyword.  */
13779   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13780
13781   /* Get the name of the namespace.  We do not attempt to distinguish
13782      between an original-namespace-definition and an
13783      extension-namespace-definition at this point.  The semantic
13784      analysis routines are responsible for that.  */
13785   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13786     identifier = cp_parser_identifier (parser);
13787   else
13788     identifier = NULL_TREE;
13789
13790   /* Parse any specified attributes.  */
13791   attribs = cp_parser_attributes_opt (parser);
13792
13793   /* Look for the `{' to start the namespace.  */
13794   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13795   /* Start the namespace.  */
13796   push_namespace (identifier);
13797
13798   /* "inline namespace" is equivalent to a stub namespace definition
13799      followed by a strong using directive.  */
13800   if (is_inline)
13801     {
13802       tree name_space = current_namespace;
13803       /* Set up namespace association.  */
13804       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13805         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13806                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13807       /* Import the contents of the inline namespace.  */
13808       pop_namespace ();
13809       do_using_directive (name_space);
13810       push_namespace (identifier);
13811     }
13812
13813   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13814
13815   /* Parse the body of the namespace.  */
13816   cp_parser_namespace_body (parser);
13817
13818   if (has_visibility)
13819     pop_visibility (1);
13820
13821   /* Finish the namespace.  */
13822   pop_namespace ();
13823   /* Look for the final `}'.  */
13824   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13825 }
13826
13827 /* Parse a namespace-body.
13828
13829    namespace-body:
13830      declaration-seq [opt]  */
13831
13832 static void
13833 cp_parser_namespace_body (cp_parser* parser)
13834 {
13835   cp_parser_declaration_seq_opt (parser);
13836 }
13837
13838 /* Parse a namespace-alias-definition.
13839
13840    namespace-alias-definition:
13841      namespace identifier = qualified-namespace-specifier ;  */
13842
13843 static void
13844 cp_parser_namespace_alias_definition (cp_parser* parser)
13845 {
13846   tree identifier;
13847   tree namespace_specifier;
13848
13849   cp_token *token = cp_lexer_peek_token (parser->lexer);
13850
13851   /* Look for the `namespace' keyword.  */
13852   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13853   /* Look for the identifier.  */
13854   identifier = cp_parser_identifier (parser);
13855   if (identifier == error_mark_node)
13856     return;
13857   /* Look for the `=' token.  */
13858   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13859       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13860     {
13861       error_at (token->location, "%<namespace%> definition is not allowed here");
13862       /* Skip the definition.  */
13863       cp_lexer_consume_token (parser->lexer);
13864       if (cp_parser_skip_to_closing_brace (parser))
13865         cp_lexer_consume_token (parser->lexer);
13866       return;
13867     }
13868   cp_parser_require (parser, CPP_EQ, RT_EQ);
13869   /* Look for the qualified-namespace-specifier.  */
13870   namespace_specifier
13871     = cp_parser_qualified_namespace_specifier (parser);
13872   /* Look for the `;' token.  */
13873   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13874
13875   /* Register the alias in the symbol table.  */
13876   do_namespace_alias (identifier, namespace_specifier);
13877 }
13878
13879 /* Parse a qualified-namespace-specifier.
13880
13881    qualified-namespace-specifier:
13882      :: [opt] nested-name-specifier [opt] namespace-name
13883
13884    Returns a NAMESPACE_DECL corresponding to the specified
13885    namespace.  */
13886
13887 static tree
13888 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13889 {
13890   /* Look for the optional `::'.  */
13891   cp_parser_global_scope_opt (parser,
13892                               /*current_scope_valid_p=*/false);
13893
13894   /* Look for the optional nested-name-specifier.  */
13895   cp_parser_nested_name_specifier_opt (parser,
13896                                        /*typename_keyword_p=*/false,
13897                                        /*check_dependency_p=*/true,
13898                                        /*type_p=*/false,
13899                                        /*is_declaration=*/true);
13900
13901   return cp_parser_namespace_name (parser);
13902 }
13903
13904 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13905    access declaration.
13906
13907    using-declaration:
13908      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13909      using :: unqualified-id ;  
13910
13911    access-declaration:
13912      qualified-id ;  
13913
13914    */
13915
13916 static bool
13917 cp_parser_using_declaration (cp_parser* parser, 
13918                              bool access_declaration_p)
13919 {
13920   cp_token *token;
13921   bool typename_p = false;
13922   bool global_scope_p;
13923   tree decl;
13924   tree identifier;
13925   tree qscope;
13926
13927   if (access_declaration_p)
13928     cp_parser_parse_tentatively (parser);
13929   else
13930     {
13931       /* Look for the `using' keyword.  */
13932       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13933       
13934       /* Peek at the next token.  */
13935       token = cp_lexer_peek_token (parser->lexer);
13936       /* See if it's `typename'.  */
13937       if (token->keyword == RID_TYPENAME)
13938         {
13939           /* Remember that we've seen it.  */
13940           typename_p = true;
13941           /* Consume the `typename' token.  */
13942           cp_lexer_consume_token (parser->lexer);
13943         }
13944     }
13945
13946   /* Look for the optional global scope qualification.  */
13947   global_scope_p
13948     = (cp_parser_global_scope_opt (parser,
13949                                    /*current_scope_valid_p=*/false)
13950        != NULL_TREE);
13951
13952   /* If we saw `typename', or didn't see `::', then there must be a
13953      nested-name-specifier present.  */
13954   if (typename_p || !global_scope_p)
13955     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13956                                               /*check_dependency_p=*/true,
13957                                               /*type_p=*/false,
13958                                               /*is_declaration=*/true);
13959   /* Otherwise, we could be in either of the two productions.  In that
13960      case, treat the nested-name-specifier as optional.  */
13961   else
13962     qscope = cp_parser_nested_name_specifier_opt (parser,
13963                                                   /*typename_keyword_p=*/false,
13964                                                   /*check_dependency_p=*/true,
13965                                                   /*type_p=*/false,
13966                                                   /*is_declaration=*/true);
13967   if (!qscope)
13968     qscope = global_namespace;
13969
13970   if (access_declaration_p && cp_parser_error_occurred (parser))
13971     /* Something has already gone wrong; there's no need to parse
13972        further.  Since an error has occurred, the return value of
13973        cp_parser_parse_definitely will be false, as required.  */
13974     return cp_parser_parse_definitely (parser);
13975
13976   token = cp_lexer_peek_token (parser->lexer);
13977   /* Parse the unqualified-id.  */
13978   identifier = cp_parser_unqualified_id (parser,
13979                                          /*template_keyword_p=*/false,
13980                                          /*check_dependency_p=*/true,
13981                                          /*declarator_p=*/true,
13982                                          /*optional_p=*/false);
13983
13984   if (access_declaration_p)
13985     {
13986       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13987         cp_parser_simulate_error (parser);
13988       if (!cp_parser_parse_definitely (parser))
13989         return false;
13990     }
13991
13992   /* The function we call to handle a using-declaration is different
13993      depending on what scope we are in.  */
13994   if (qscope == error_mark_node || identifier == error_mark_node)
13995     ;
13996   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13997            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13998     /* [namespace.udecl]
13999
14000        A using declaration shall not name a template-id.  */
14001     error_at (token->location,
14002               "a template-id may not appear in a using-declaration");
14003   else
14004     {
14005       if (at_class_scope_p ())
14006         {
14007           /* Create the USING_DECL.  */
14008           decl = do_class_using_decl (parser->scope, identifier);
14009
14010           if (check_for_bare_parameter_packs (decl))
14011             return false;
14012           else
14013             /* Add it to the list of members in this class.  */
14014             finish_member_declaration (decl);
14015         }
14016       else
14017         {
14018           decl = cp_parser_lookup_name_simple (parser,
14019                                                identifier,
14020                                                token->location);
14021           if (decl == error_mark_node)
14022             cp_parser_name_lookup_error (parser, identifier,
14023                                          decl, NLE_NULL,
14024                                          token->location);
14025           else if (check_for_bare_parameter_packs (decl))
14026             return false;
14027           else if (!at_namespace_scope_p ())
14028             do_local_using_decl (decl, qscope, identifier);
14029           else
14030             do_toplevel_using_decl (decl, qscope, identifier);
14031         }
14032     }
14033
14034   /* Look for the final `;'.  */
14035   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14036   
14037   return true;
14038 }
14039
14040 /* Parse a using-directive.
14041
14042    using-directive:
14043      using namespace :: [opt] nested-name-specifier [opt]
14044        namespace-name ;  */
14045
14046 static void
14047 cp_parser_using_directive (cp_parser* parser)
14048 {
14049   tree namespace_decl;
14050   tree attribs;
14051
14052   /* Look for the `using' keyword.  */
14053   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14054   /* And the `namespace' keyword.  */
14055   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14056   /* Look for the optional `::' operator.  */
14057   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14058   /* And the optional nested-name-specifier.  */
14059   cp_parser_nested_name_specifier_opt (parser,
14060                                        /*typename_keyword_p=*/false,
14061                                        /*check_dependency_p=*/true,
14062                                        /*type_p=*/false,
14063                                        /*is_declaration=*/true);
14064   /* Get the namespace being used.  */
14065   namespace_decl = cp_parser_namespace_name (parser);
14066   /* And any specified attributes.  */
14067   attribs = cp_parser_attributes_opt (parser);
14068   /* Update the symbol table.  */
14069   parse_using_directive (namespace_decl, attribs);
14070   /* Look for the final `;'.  */
14071   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14072 }
14073
14074 /* Parse an asm-definition.
14075
14076    asm-definition:
14077      asm ( string-literal ) ;
14078
14079    GNU Extension:
14080
14081    asm-definition:
14082      asm volatile [opt] ( string-literal ) ;
14083      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14084      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14085                           : asm-operand-list [opt] ) ;
14086      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14087                           : asm-operand-list [opt]
14088                           : asm-clobber-list [opt] ) ;
14089      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14090                                : asm-clobber-list [opt]
14091                                : asm-goto-list ) ;  */
14092
14093 static void
14094 cp_parser_asm_definition (cp_parser* parser)
14095 {
14096   tree string;
14097   tree outputs = NULL_TREE;
14098   tree inputs = NULL_TREE;
14099   tree clobbers = NULL_TREE;
14100   tree labels = NULL_TREE;
14101   tree asm_stmt;
14102   bool volatile_p = false;
14103   bool extended_p = false;
14104   bool invalid_inputs_p = false;
14105   bool invalid_outputs_p = false;
14106   bool goto_p = false;
14107   required_token missing = RT_NONE;
14108
14109   /* Look for the `asm' keyword.  */
14110   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14111   /* See if the next token is `volatile'.  */
14112   if (cp_parser_allow_gnu_extensions_p (parser)
14113       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14114     {
14115       /* Remember that we saw the `volatile' keyword.  */
14116       volatile_p = true;
14117       /* Consume the token.  */
14118       cp_lexer_consume_token (parser->lexer);
14119     }
14120   if (cp_parser_allow_gnu_extensions_p (parser)
14121       && parser->in_function_body
14122       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14123     {
14124       /* Remember that we saw the `goto' keyword.  */
14125       goto_p = true;
14126       /* Consume the token.  */
14127       cp_lexer_consume_token (parser->lexer);
14128     }
14129   /* Look for the opening `('.  */
14130   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14131     return;
14132   /* Look for the string.  */
14133   string = cp_parser_string_literal (parser, false, false);
14134   if (string == error_mark_node)
14135     {
14136       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14137                                              /*consume_paren=*/true);
14138       return;
14139     }
14140
14141   /* If we're allowing GNU extensions, check for the extended assembly
14142      syntax.  Unfortunately, the `:' tokens need not be separated by
14143      a space in C, and so, for compatibility, we tolerate that here
14144      too.  Doing that means that we have to treat the `::' operator as
14145      two `:' tokens.  */
14146   if (cp_parser_allow_gnu_extensions_p (parser)
14147       && parser->in_function_body
14148       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14149           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14150     {
14151       bool inputs_p = false;
14152       bool clobbers_p = false;
14153       bool labels_p = false;
14154
14155       /* The extended syntax was used.  */
14156       extended_p = true;
14157
14158       /* Look for outputs.  */
14159       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14160         {
14161           /* Consume the `:'.  */
14162           cp_lexer_consume_token (parser->lexer);
14163           /* Parse the output-operands.  */
14164           if (cp_lexer_next_token_is_not (parser->lexer,
14165                                           CPP_COLON)
14166               && cp_lexer_next_token_is_not (parser->lexer,
14167                                              CPP_SCOPE)
14168               && cp_lexer_next_token_is_not (parser->lexer,
14169                                              CPP_CLOSE_PAREN)
14170               && !goto_p)
14171             outputs = cp_parser_asm_operand_list (parser);
14172
14173             if (outputs == error_mark_node)
14174               invalid_outputs_p = true;
14175         }
14176       /* If the next token is `::', there are no outputs, and the
14177          next token is the beginning of the inputs.  */
14178       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14179         /* The inputs are coming next.  */
14180         inputs_p = true;
14181
14182       /* Look for inputs.  */
14183       if (inputs_p
14184           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14185         {
14186           /* Consume the `:' or `::'.  */
14187           cp_lexer_consume_token (parser->lexer);
14188           /* Parse the output-operands.  */
14189           if (cp_lexer_next_token_is_not (parser->lexer,
14190                                           CPP_COLON)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_SCOPE)
14193               && cp_lexer_next_token_is_not (parser->lexer,
14194                                              CPP_CLOSE_PAREN))
14195             inputs = cp_parser_asm_operand_list (parser);
14196
14197             if (inputs == error_mark_node)
14198               invalid_inputs_p = true;
14199         }
14200       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14201         /* The clobbers are coming next.  */
14202         clobbers_p = true;
14203
14204       /* Look for clobbers.  */
14205       if (clobbers_p
14206           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14207         {
14208           clobbers_p = true;
14209           /* Consume the `:' or `::'.  */
14210           cp_lexer_consume_token (parser->lexer);
14211           /* Parse the clobbers.  */
14212           if (cp_lexer_next_token_is_not (parser->lexer,
14213                                           CPP_COLON)
14214               && cp_lexer_next_token_is_not (parser->lexer,
14215                                              CPP_CLOSE_PAREN))
14216             clobbers = cp_parser_asm_clobber_list (parser);
14217         }
14218       else if (goto_p
14219                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14220         /* The labels are coming next.  */
14221         labels_p = true;
14222
14223       /* Look for labels.  */
14224       if (labels_p
14225           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14226         {
14227           labels_p = true;
14228           /* Consume the `:' or `::'.  */
14229           cp_lexer_consume_token (parser->lexer);
14230           /* Parse the labels.  */
14231           labels = cp_parser_asm_label_list (parser);
14232         }
14233
14234       if (goto_p && !labels_p)
14235         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14236     }
14237   else if (goto_p)
14238     missing = RT_COLON_SCOPE;
14239
14240   /* Look for the closing `)'.  */
14241   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14242                           missing ? missing : RT_CLOSE_PAREN))
14243     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14244                                            /*consume_paren=*/true);
14245   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14246
14247   if (!invalid_inputs_p && !invalid_outputs_p)
14248     {
14249       /* Create the ASM_EXPR.  */
14250       if (parser->in_function_body)
14251         {
14252           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14253                                       inputs, clobbers, labels);
14254           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14255           if (!extended_p)
14256             {
14257               tree temp = asm_stmt;
14258               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14259                 temp = TREE_OPERAND (temp, 0);
14260
14261               ASM_INPUT_P (temp) = 1;
14262             }
14263         }
14264       else
14265         cgraph_add_asm_node (string);
14266     }
14267 }
14268
14269 /* Declarators [gram.dcl.decl] */
14270
14271 /* Parse an init-declarator.
14272
14273    init-declarator:
14274      declarator initializer [opt]
14275
14276    GNU Extension:
14277
14278    init-declarator:
14279      declarator asm-specification [opt] attributes [opt] initializer [opt]
14280
14281    function-definition:
14282      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14283        function-body
14284      decl-specifier-seq [opt] declarator function-try-block
14285
14286    GNU Extension:
14287
14288    function-definition:
14289      __extension__ function-definition
14290
14291    The DECL_SPECIFIERS apply to this declarator.  Returns a
14292    representation of the entity declared.  If MEMBER_P is TRUE, then
14293    this declarator appears in a class scope.  The new DECL created by
14294    this declarator is returned.
14295
14296    The CHECKS are access checks that should be performed once we know
14297    what entity is being declared (and, therefore, what classes have
14298    befriended it).
14299
14300    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14301    for a function-definition here as well.  If the declarator is a
14302    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14303    be TRUE upon return.  By that point, the function-definition will
14304    have been completely parsed.
14305
14306    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14307    is FALSE.
14308
14309    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14310    parsed declaration if it is an uninitialized single declarator not followed
14311    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14312    if present, will not be consumed.  If returned, this declarator will be
14313    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14314
14315 static tree
14316 cp_parser_init_declarator (cp_parser* parser,
14317                            cp_decl_specifier_seq *decl_specifiers,
14318                            VEC (deferred_access_check,gc)* checks,
14319                            bool function_definition_allowed_p,
14320                            bool member_p,
14321                            int declares_class_or_enum,
14322                            bool* function_definition_p,
14323                            tree* maybe_range_for_decl)
14324 {
14325   cp_token *token = NULL, *asm_spec_start_token = NULL,
14326            *attributes_start_token = NULL;
14327   cp_declarator *declarator;
14328   tree prefix_attributes;
14329   tree attributes;
14330   tree asm_specification;
14331   tree initializer;
14332   tree decl = NULL_TREE;
14333   tree scope;
14334   int is_initialized;
14335   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14336      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14337      "(...)".  */
14338   enum cpp_ttype initialization_kind;
14339   bool is_direct_init = false;
14340   bool is_non_constant_init;
14341   int ctor_dtor_or_conv_p;
14342   bool friend_p;
14343   tree pushed_scope = NULL_TREE;
14344   bool range_for_decl_p = false;
14345
14346   /* Gather the attributes that were provided with the
14347      decl-specifiers.  */
14348   prefix_attributes = decl_specifiers->attributes;
14349
14350   /* Assume that this is not the declarator for a function
14351      definition.  */
14352   if (function_definition_p)
14353     *function_definition_p = false;
14354
14355   /* Defer access checks while parsing the declarator; we cannot know
14356      what names are accessible until we know what is being
14357      declared.  */
14358   resume_deferring_access_checks ();
14359
14360   /* Parse the declarator.  */
14361   token = cp_lexer_peek_token (parser->lexer);
14362   declarator
14363     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14364                             &ctor_dtor_or_conv_p,
14365                             /*parenthesized_p=*/NULL,
14366                             /*member_p=*/false);
14367   /* Gather up the deferred checks.  */
14368   stop_deferring_access_checks ();
14369
14370   /* If the DECLARATOR was erroneous, there's no need to go
14371      further.  */
14372   if (declarator == cp_error_declarator)
14373     return error_mark_node;
14374
14375   /* Check that the number of template-parameter-lists is OK.  */
14376   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14377                                                        token->location))
14378     return error_mark_node;
14379
14380   if (declares_class_or_enum & 2)
14381     cp_parser_check_for_definition_in_return_type (declarator,
14382                                                    decl_specifiers->type,
14383                                                    decl_specifiers->type_location);
14384
14385   /* Figure out what scope the entity declared by the DECLARATOR is
14386      located in.  `grokdeclarator' sometimes changes the scope, so
14387      we compute it now.  */
14388   scope = get_scope_of_declarator (declarator);
14389
14390   /* Perform any lookups in the declared type which were thought to be
14391      dependent, but are not in the scope of the declarator.  */
14392   decl_specifiers->type
14393     = maybe_update_decl_type (decl_specifiers->type, scope);
14394
14395   /* If we're allowing GNU extensions, look for an asm-specification
14396      and attributes.  */
14397   if (cp_parser_allow_gnu_extensions_p (parser))
14398     {
14399       /* Look for an asm-specification.  */
14400       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14401       asm_specification = cp_parser_asm_specification_opt (parser);
14402       /* And attributes.  */
14403       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14404       attributes = cp_parser_attributes_opt (parser);
14405     }
14406   else
14407     {
14408       asm_specification = NULL_TREE;
14409       attributes = NULL_TREE;
14410     }
14411
14412   /* Peek at the next token.  */
14413   token = cp_lexer_peek_token (parser->lexer);
14414   /* Check to see if the token indicates the start of a
14415      function-definition.  */
14416   if (function_declarator_p (declarator)
14417       && cp_parser_token_starts_function_definition_p (token))
14418     {
14419       if (!function_definition_allowed_p)
14420         {
14421           /* If a function-definition should not appear here, issue an
14422              error message.  */
14423           cp_parser_error (parser,
14424                            "a function-definition is not allowed here");
14425           return error_mark_node;
14426         }
14427       else
14428         {
14429           location_t func_brace_location
14430             = cp_lexer_peek_token (parser->lexer)->location;
14431
14432           /* Neither attributes nor an asm-specification are allowed
14433              on a function-definition.  */
14434           if (asm_specification)
14435             error_at (asm_spec_start_token->location,
14436                       "an asm-specification is not allowed "
14437                       "on a function-definition");
14438           if (attributes)
14439             error_at (attributes_start_token->location,
14440                       "attributes are not allowed on a function-definition");
14441           /* This is a function-definition.  */
14442           *function_definition_p = true;
14443
14444           /* Parse the function definition.  */
14445           if (member_p)
14446             decl = cp_parser_save_member_function_body (parser,
14447                                                         decl_specifiers,
14448                                                         declarator,
14449                                                         prefix_attributes);
14450           else
14451             decl
14452               = (cp_parser_function_definition_from_specifiers_and_declarator
14453                  (parser, decl_specifiers, prefix_attributes, declarator));
14454
14455           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14456             {
14457               /* This is where the prologue starts...  */
14458               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14459                 = func_brace_location;
14460             }
14461
14462           return decl;
14463         }
14464     }
14465
14466   /* [dcl.dcl]
14467
14468      Only in function declarations for constructors, destructors, and
14469      type conversions can the decl-specifier-seq be omitted.
14470
14471      We explicitly postpone this check past the point where we handle
14472      function-definitions because we tolerate function-definitions
14473      that are missing their return types in some modes.  */
14474   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14475     {
14476       cp_parser_error (parser,
14477                        "expected constructor, destructor, or type conversion");
14478       return error_mark_node;
14479     }
14480
14481   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14482   if (token->type == CPP_EQ
14483       || token->type == CPP_OPEN_PAREN
14484       || token->type == CPP_OPEN_BRACE)
14485     {
14486       is_initialized = SD_INITIALIZED;
14487       initialization_kind = token->type;
14488       if (maybe_range_for_decl)
14489         *maybe_range_for_decl = error_mark_node;
14490
14491       if (token->type == CPP_EQ
14492           && function_declarator_p (declarator))
14493         {
14494           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14495           if (t2->keyword == RID_DEFAULT)
14496             is_initialized = SD_DEFAULTED;
14497           else if (t2->keyword == RID_DELETE)
14498             is_initialized = SD_DELETED;
14499         }
14500     }
14501   else
14502     {
14503       /* If the init-declarator isn't initialized and isn't followed by a
14504          `,' or `;', it's not a valid init-declarator.  */
14505       if (token->type != CPP_COMMA
14506           && token->type != CPP_SEMICOLON)
14507         {
14508           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14509             range_for_decl_p = true;
14510           else
14511             {
14512               cp_parser_error (parser, "expected initializer");
14513               return error_mark_node;
14514             }
14515         }
14516       is_initialized = SD_UNINITIALIZED;
14517       initialization_kind = CPP_EOF;
14518     }
14519
14520   /* Because start_decl has side-effects, we should only call it if we
14521      know we're going ahead.  By this point, we know that we cannot
14522      possibly be looking at any other construct.  */
14523   cp_parser_commit_to_tentative_parse (parser);
14524
14525   /* If the decl specifiers were bad, issue an error now that we're
14526      sure this was intended to be a declarator.  Then continue
14527      declaring the variable(s), as int, to try to cut down on further
14528      errors.  */
14529   if (decl_specifiers->any_specifiers_p
14530       && decl_specifiers->type == error_mark_node)
14531     {
14532       cp_parser_error (parser, "invalid type in declaration");
14533       decl_specifiers->type = integer_type_node;
14534     }
14535
14536   /* Check to see whether or not this declaration is a friend.  */
14537   friend_p = cp_parser_friend_p (decl_specifiers);
14538
14539   /* Enter the newly declared entry in the symbol table.  If we're
14540      processing a declaration in a class-specifier, we wait until
14541      after processing the initializer.  */
14542   if (!member_p)
14543     {
14544       if (parser->in_unbraced_linkage_specification_p)
14545         decl_specifiers->storage_class = sc_extern;
14546       decl = start_decl (declarator, decl_specifiers,
14547                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14548                          attributes, prefix_attributes,
14549                          &pushed_scope);
14550       /* Adjust location of decl if declarator->id_loc is more appropriate:
14551          set, and decl wasn't merged with another decl, in which case its
14552          location would be different from input_location, and more accurate.  */
14553       if (DECL_P (decl)
14554           && declarator->id_loc != UNKNOWN_LOCATION
14555           && DECL_SOURCE_LOCATION (decl) == input_location)
14556         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14557     }
14558   else if (scope)
14559     /* Enter the SCOPE.  That way unqualified names appearing in the
14560        initializer will be looked up in SCOPE.  */
14561     pushed_scope = push_scope (scope);
14562
14563   /* Perform deferred access control checks, now that we know in which
14564      SCOPE the declared entity resides.  */
14565   if (!member_p && decl)
14566     {
14567       tree saved_current_function_decl = NULL_TREE;
14568
14569       /* If the entity being declared is a function, pretend that we
14570          are in its scope.  If it is a `friend', it may have access to
14571          things that would not otherwise be accessible.  */
14572       if (TREE_CODE (decl) == FUNCTION_DECL)
14573         {
14574           saved_current_function_decl = current_function_decl;
14575           current_function_decl = decl;
14576         }
14577
14578       /* Perform access checks for template parameters.  */
14579       cp_parser_perform_template_parameter_access_checks (checks);
14580
14581       /* Perform the access control checks for the declarator and the
14582          decl-specifiers.  */
14583       perform_deferred_access_checks ();
14584
14585       /* Restore the saved value.  */
14586       if (TREE_CODE (decl) == FUNCTION_DECL)
14587         current_function_decl = saved_current_function_decl;
14588     }
14589
14590   /* Parse the initializer.  */
14591   initializer = NULL_TREE;
14592   is_direct_init = false;
14593   is_non_constant_init = true;
14594   if (is_initialized)
14595     {
14596       if (function_declarator_p (declarator))
14597         {
14598           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14599            if (initialization_kind == CPP_EQ)
14600              initializer = cp_parser_pure_specifier (parser);
14601            else
14602              {
14603                /* If the declaration was erroneous, we don't really
14604                   know what the user intended, so just silently
14605                   consume the initializer.  */
14606                if (decl != error_mark_node)
14607                  error_at (initializer_start_token->location,
14608                            "initializer provided for function");
14609                cp_parser_skip_to_closing_parenthesis (parser,
14610                                                       /*recovering=*/true,
14611                                                       /*or_comma=*/false,
14612                                                       /*consume_paren=*/true);
14613              }
14614         }
14615       else
14616         {
14617           /* We want to record the extra mangling scope for in-class
14618              initializers of class members and initializers of static data
14619              member templates.  The former is a C++0x feature which isn't
14620              implemented yet, and I expect it will involve deferring
14621              parsing of the initializer until end of class as with default
14622              arguments.  So right here we only handle the latter.  */
14623           if (!member_p && processing_template_decl)
14624             start_lambda_scope (decl);
14625           initializer = cp_parser_initializer (parser,
14626                                                &is_direct_init,
14627                                                &is_non_constant_init);
14628           if (!member_p && processing_template_decl)
14629             finish_lambda_scope ();
14630         }
14631     }
14632
14633   /* The old parser allows attributes to appear after a parenthesized
14634      initializer.  Mark Mitchell proposed removing this functionality
14635      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14636      attributes -- but ignores them.  */
14637   if (cp_parser_allow_gnu_extensions_p (parser)
14638       && initialization_kind == CPP_OPEN_PAREN)
14639     if (cp_parser_attributes_opt (parser))
14640       warning (OPT_Wattributes,
14641                "attributes after parenthesized initializer ignored");
14642
14643   /* For an in-class declaration, use `grokfield' to create the
14644      declaration.  */
14645   if (member_p)
14646     {
14647       if (pushed_scope)
14648         {
14649           pop_scope (pushed_scope);
14650           pushed_scope = NULL_TREE;
14651         }
14652       decl = grokfield (declarator, decl_specifiers,
14653                         initializer, !is_non_constant_init,
14654                         /*asmspec=*/NULL_TREE,
14655                         prefix_attributes);
14656       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14657         cp_parser_save_default_args (parser, decl);
14658     }
14659
14660   /* Finish processing the declaration.  But, skip member
14661      declarations.  */
14662   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14663     {
14664       cp_finish_decl (decl,
14665                       initializer, !is_non_constant_init,
14666                       asm_specification,
14667                       /* If the initializer is in parentheses, then this is
14668                          a direct-initialization, which means that an
14669                          `explicit' constructor is OK.  Otherwise, an
14670                          `explicit' constructor cannot be used.  */
14671                       ((is_direct_init || !is_initialized)
14672                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14673     }
14674   else if ((cxx_dialect != cxx98) && friend_p
14675            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14676     /* Core issue #226 (C++0x only): A default template-argument
14677        shall not be specified in a friend class template
14678        declaration. */
14679     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14680                              /*is_partial=*/0, /*is_friend_decl=*/1);
14681
14682   if (!friend_p && pushed_scope)
14683     pop_scope (pushed_scope);
14684
14685   return decl;
14686 }
14687
14688 /* Parse a declarator.
14689
14690    declarator:
14691      direct-declarator
14692      ptr-operator declarator
14693
14694    abstract-declarator:
14695      ptr-operator abstract-declarator [opt]
14696      direct-abstract-declarator
14697
14698    GNU Extensions:
14699
14700    declarator:
14701      attributes [opt] direct-declarator
14702      attributes [opt] ptr-operator declarator
14703
14704    abstract-declarator:
14705      attributes [opt] ptr-operator abstract-declarator [opt]
14706      attributes [opt] direct-abstract-declarator
14707
14708    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14709    detect constructor, destructor or conversion operators. It is set
14710    to -1 if the declarator is a name, and +1 if it is a
14711    function. Otherwise it is set to zero. Usually you just want to
14712    test for >0, but internally the negative value is used.
14713
14714    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14715    a decl-specifier-seq unless it declares a constructor, destructor,
14716    or conversion.  It might seem that we could check this condition in
14717    semantic analysis, rather than parsing, but that makes it difficult
14718    to handle something like `f()'.  We want to notice that there are
14719    no decl-specifiers, and therefore realize that this is an
14720    expression, not a declaration.)
14721
14722    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14723    the declarator is a direct-declarator of the form "(...)".
14724
14725    MEMBER_P is true iff this declarator is a member-declarator.  */
14726
14727 static cp_declarator *
14728 cp_parser_declarator (cp_parser* parser,
14729                       cp_parser_declarator_kind dcl_kind,
14730                       int* ctor_dtor_or_conv_p,
14731                       bool* parenthesized_p,
14732                       bool member_p)
14733 {
14734   cp_declarator *declarator;
14735   enum tree_code code;
14736   cp_cv_quals cv_quals;
14737   tree class_type;
14738   tree attributes = NULL_TREE;
14739
14740   /* Assume this is not a constructor, destructor, or type-conversion
14741      operator.  */
14742   if (ctor_dtor_or_conv_p)
14743     *ctor_dtor_or_conv_p = 0;
14744
14745   if (cp_parser_allow_gnu_extensions_p (parser))
14746     attributes = cp_parser_attributes_opt (parser);
14747
14748   /* Check for the ptr-operator production.  */
14749   cp_parser_parse_tentatively (parser);
14750   /* Parse the ptr-operator.  */
14751   code = cp_parser_ptr_operator (parser,
14752                                  &class_type,
14753                                  &cv_quals);
14754   /* If that worked, then we have a ptr-operator.  */
14755   if (cp_parser_parse_definitely (parser))
14756     {
14757       /* If a ptr-operator was found, then this declarator was not
14758          parenthesized.  */
14759       if (parenthesized_p)
14760         *parenthesized_p = true;
14761       /* The dependent declarator is optional if we are parsing an
14762          abstract-declarator.  */
14763       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14764         cp_parser_parse_tentatively (parser);
14765
14766       /* Parse the dependent declarator.  */
14767       declarator = cp_parser_declarator (parser, dcl_kind,
14768                                          /*ctor_dtor_or_conv_p=*/NULL,
14769                                          /*parenthesized_p=*/NULL,
14770                                          /*member_p=*/false);
14771
14772       /* If we are parsing an abstract-declarator, we must handle the
14773          case where the dependent declarator is absent.  */
14774       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14775           && !cp_parser_parse_definitely (parser))
14776         declarator = NULL;
14777
14778       declarator = cp_parser_make_indirect_declarator
14779         (code, class_type, cv_quals, declarator);
14780     }
14781   /* Everything else is a direct-declarator.  */
14782   else
14783     {
14784       if (parenthesized_p)
14785         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14786                                                    CPP_OPEN_PAREN);
14787       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14788                                                 ctor_dtor_or_conv_p,
14789                                                 member_p);
14790     }
14791
14792   if (attributes && declarator && declarator != cp_error_declarator)
14793     declarator->attributes = attributes;
14794
14795   return declarator;
14796 }
14797
14798 /* Parse a direct-declarator or direct-abstract-declarator.
14799
14800    direct-declarator:
14801      declarator-id
14802      direct-declarator ( parameter-declaration-clause )
14803        cv-qualifier-seq [opt]
14804        exception-specification [opt]
14805      direct-declarator [ constant-expression [opt] ]
14806      ( declarator )
14807
14808    direct-abstract-declarator:
14809      direct-abstract-declarator [opt]
14810        ( parameter-declaration-clause )
14811        cv-qualifier-seq [opt]
14812        exception-specification [opt]
14813      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14814      ( abstract-declarator )
14815
14816    Returns a representation of the declarator.  DCL_KIND is
14817    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14818    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14819    we are parsing a direct-declarator.  It is
14820    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14821    of ambiguity we prefer an abstract declarator, as per
14822    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14823    cp_parser_declarator.  */
14824
14825 static cp_declarator *
14826 cp_parser_direct_declarator (cp_parser* parser,
14827                              cp_parser_declarator_kind dcl_kind,
14828                              int* ctor_dtor_or_conv_p,
14829                              bool member_p)
14830 {
14831   cp_token *token;
14832   cp_declarator *declarator = NULL;
14833   tree scope = NULL_TREE;
14834   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14835   bool saved_in_declarator_p = parser->in_declarator_p;
14836   bool first = true;
14837   tree pushed_scope = NULL_TREE;
14838
14839   while (true)
14840     {
14841       /* Peek at the next token.  */
14842       token = cp_lexer_peek_token (parser->lexer);
14843       if (token->type == CPP_OPEN_PAREN)
14844         {
14845           /* This is either a parameter-declaration-clause, or a
14846              parenthesized declarator. When we know we are parsing a
14847              named declarator, it must be a parenthesized declarator
14848              if FIRST is true. For instance, `(int)' is a
14849              parameter-declaration-clause, with an omitted
14850              direct-abstract-declarator. But `((*))', is a
14851              parenthesized abstract declarator. Finally, when T is a
14852              template parameter `(T)' is a
14853              parameter-declaration-clause, and not a parenthesized
14854              named declarator.
14855
14856              We first try and parse a parameter-declaration-clause,
14857              and then try a nested declarator (if FIRST is true).
14858
14859              It is not an error for it not to be a
14860              parameter-declaration-clause, even when FIRST is
14861              false. Consider,
14862
14863                int i (int);
14864                int i (3);
14865
14866              The first is the declaration of a function while the
14867              second is the definition of a variable, including its
14868              initializer.
14869
14870              Having seen only the parenthesis, we cannot know which of
14871              these two alternatives should be selected.  Even more
14872              complex are examples like:
14873
14874                int i (int (a));
14875                int i (int (3));
14876
14877              The former is a function-declaration; the latter is a
14878              variable initialization.
14879
14880              Thus again, we try a parameter-declaration-clause, and if
14881              that fails, we back out and return.  */
14882
14883           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14884             {
14885               tree params;
14886               unsigned saved_num_template_parameter_lists;
14887               bool is_declarator = false;
14888               tree t;
14889
14890               /* In a member-declarator, the only valid interpretation
14891                  of a parenthesis is the start of a
14892                  parameter-declaration-clause.  (It is invalid to
14893                  initialize a static data member with a parenthesized
14894                  initializer; only the "=" form of initialization is
14895                  permitted.)  */
14896               if (!member_p)
14897                 cp_parser_parse_tentatively (parser);
14898
14899               /* Consume the `('.  */
14900               cp_lexer_consume_token (parser->lexer);
14901               if (first)
14902                 {
14903                   /* If this is going to be an abstract declarator, we're
14904                      in a declarator and we can't have default args.  */
14905                   parser->default_arg_ok_p = false;
14906                   parser->in_declarator_p = true;
14907                 }
14908
14909               /* Inside the function parameter list, surrounding
14910                  template-parameter-lists do not apply.  */
14911               saved_num_template_parameter_lists
14912                 = parser->num_template_parameter_lists;
14913               parser->num_template_parameter_lists = 0;
14914
14915               begin_scope (sk_function_parms, NULL_TREE);
14916
14917               /* Parse the parameter-declaration-clause.  */
14918               params = cp_parser_parameter_declaration_clause (parser);
14919
14920               parser->num_template_parameter_lists
14921                 = saved_num_template_parameter_lists;
14922
14923               /* Consume the `)'.  */
14924               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14925
14926               /* If all went well, parse the cv-qualifier-seq and the
14927                  exception-specification.  */
14928               if (member_p || cp_parser_parse_definitely (parser))
14929                 {
14930                   cp_cv_quals cv_quals;
14931                   cp_virt_specifiers virt_specifiers;
14932                   tree exception_specification;
14933                   tree late_return;
14934
14935                   is_declarator = true;
14936
14937                   if (ctor_dtor_or_conv_p)
14938                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14939                   first = false;
14940
14941                   /* Parse the cv-qualifier-seq.  */
14942                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14943                   /* And the exception-specification.  */
14944                   exception_specification
14945                     = cp_parser_exception_specification_opt (parser);
14946                   /* Parse the virt-specifier-seq.  */
14947                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14948
14949                   late_return
14950                     = cp_parser_late_return_type_opt (parser);
14951
14952                   /* Create the function-declarator.  */
14953                   declarator = make_call_declarator (declarator,
14954                                                      params,
14955                                                      cv_quals,
14956                                                      virt_specifiers,
14957                                                      exception_specification,
14958                                                      late_return);
14959                   /* Any subsequent parameter lists are to do with
14960                      return type, so are not those of the declared
14961                      function.  */
14962                   parser->default_arg_ok_p = false;
14963                 }
14964
14965               /* Remove the function parms from scope.  */
14966               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14967                 pop_binding (DECL_NAME (t), t);
14968               leave_scope();
14969
14970               if (is_declarator)
14971                 /* Repeat the main loop.  */
14972                 continue;
14973             }
14974
14975           /* If this is the first, we can try a parenthesized
14976              declarator.  */
14977           if (first)
14978             {
14979               bool saved_in_type_id_in_expr_p;
14980
14981               parser->default_arg_ok_p = saved_default_arg_ok_p;
14982               parser->in_declarator_p = saved_in_declarator_p;
14983
14984               /* Consume the `('.  */
14985               cp_lexer_consume_token (parser->lexer);
14986               /* Parse the nested declarator.  */
14987               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14988               parser->in_type_id_in_expr_p = true;
14989               declarator
14990                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14991                                         /*parenthesized_p=*/NULL,
14992                                         member_p);
14993               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14994               first = false;
14995               /* Expect a `)'.  */
14996               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14997                 declarator = cp_error_declarator;
14998               if (declarator == cp_error_declarator)
14999                 break;
15000
15001               goto handle_declarator;
15002             }
15003           /* Otherwise, we must be done.  */
15004           else
15005             break;
15006         }
15007       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15008                && token->type == CPP_OPEN_SQUARE)
15009         {
15010           /* Parse an array-declarator.  */
15011           tree bounds;
15012
15013           if (ctor_dtor_or_conv_p)
15014             *ctor_dtor_or_conv_p = 0;
15015
15016           first = false;
15017           parser->default_arg_ok_p = false;
15018           parser->in_declarator_p = true;
15019           /* Consume the `['.  */
15020           cp_lexer_consume_token (parser->lexer);
15021           /* Peek at the next token.  */
15022           token = cp_lexer_peek_token (parser->lexer);
15023           /* If the next token is `]', then there is no
15024              constant-expression.  */
15025           if (token->type != CPP_CLOSE_SQUARE)
15026             {
15027               bool non_constant_p;
15028
15029               bounds
15030                 = cp_parser_constant_expression (parser,
15031                                                  /*allow_non_constant=*/true,
15032                                                  &non_constant_p);
15033               if (!non_constant_p)
15034                 /* OK */;
15035               /* Normally, the array bound must be an integral constant
15036                  expression.  However, as an extension, we allow VLAs
15037                  in function scopes as long as they aren't part of a
15038                  parameter declaration.  */
15039               else if (!parser->in_function_body
15040                        || current_binding_level->kind == sk_function_parms)
15041                 {
15042                   cp_parser_error (parser,
15043                                    "array bound is not an integer constant");
15044                   bounds = error_mark_node;
15045                 }
15046               else if (processing_template_decl && !error_operand_p (bounds))
15047                 {
15048                   /* Remember this wasn't a constant-expression.  */
15049                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15050                   TREE_SIDE_EFFECTS (bounds) = 1;
15051                 }
15052             }
15053           else
15054             bounds = NULL_TREE;
15055           /* Look for the closing `]'.  */
15056           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15057             {
15058               declarator = cp_error_declarator;
15059               break;
15060             }
15061
15062           declarator = make_array_declarator (declarator, bounds);
15063         }
15064       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15065         {
15066           {
15067             tree qualifying_scope;
15068             tree unqualified_name;
15069             special_function_kind sfk;
15070             bool abstract_ok;
15071             bool pack_expansion_p = false;
15072             cp_token *declarator_id_start_token;
15073
15074             /* Parse a declarator-id */
15075             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15076             if (abstract_ok)
15077               {
15078                 cp_parser_parse_tentatively (parser);
15079
15080                 /* If we see an ellipsis, we should be looking at a
15081                    parameter pack. */
15082                 if (token->type == CPP_ELLIPSIS)
15083                   {
15084                     /* Consume the `...' */
15085                     cp_lexer_consume_token (parser->lexer);
15086
15087                     pack_expansion_p = true;
15088                   }
15089               }
15090
15091             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15092             unqualified_name
15093               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15094             qualifying_scope = parser->scope;
15095             if (abstract_ok)
15096               {
15097                 bool okay = false;
15098
15099                 if (!unqualified_name && pack_expansion_p)
15100                   {
15101                     /* Check whether an error occurred. */
15102                     okay = !cp_parser_error_occurred (parser);
15103
15104                     /* We already consumed the ellipsis to mark a
15105                        parameter pack, but we have no way to report it,
15106                        so abort the tentative parse. We will be exiting
15107                        immediately anyway. */
15108                     cp_parser_abort_tentative_parse (parser);
15109                   }
15110                 else
15111                   okay = cp_parser_parse_definitely (parser);
15112
15113                 if (!okay)
15114                   unqualified_name = error_mark_node;
15115                 else if (unqualified_name
15116                          && (qualifying_scope
15117                              || (TREE_CODE (unqualified_name)
15118                                  != IDENTIFIER_NODE)))
15119                   {
15120                     cp_parser_error (parser, "expected unqualified-id");
15121                     unqualified_name = error_mark_node;
15122                   }
15123               }
15124
15125             if (!unqualified_name)
15126               return NULL;
15127             if (unqualified_name == error_mark_node)
15128               {
15129                 declarator = cp_error_declarator;
15130                 pack_expansion_p = false;
15131                 declarator->parameter_pack_p = false;
15132                 break;
15133               }
15134
15135             if (qualifying_scope && at_namespace_scope_p ()
15136                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15137               {
15138                 /* In the declaration of a member of a template class
15139                    outside of the class itself, the SCOPE will sometimes
15140                    be a TYPENAME_TYPE.  For example, given:
15141
15142                    template <typename T>
15143                    int S<T>::R::i = 3;
15144
15145                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15146                    this context, we must resolve S<T>::R to an ordinary
15147                    type, rather than a typename type.
15148
15149                    The reason we normally avoid resolving TYPENAME_TYPEs
15150                    is that a specialization of `S' might render
15151                    `S<T>::R' not a type.  However, if `S' is
15152                    specialized, then this `i' will not be used, so there
15153                    is no harm in resolving the types here.  */
15154                 tree type;
15155
15156                 /* Resolve the TYPENAME_TYPE.  */
15157                 type = resolve_typename_type (qualifying_scope,
15158                                               /*only_current_p=*/false);
15159                 /* If that failed, the declarator is invalid.  */
15160                 if (TREE_CODE (type) == TYPENAME_TYPE)
15161                   {
15162                     if (typedef_variant_p (type))
15163                       error_at (declarator_id_start_token->location,
15164                                 "cannot define member of dependent typedef "
15165                                 "%qT", type);
15166                     else
15167                       error_at (declarator_id_start_token->location,
15168                                 "%<%T::%E%> is not a type",
15169                                 TYPE_CONTEXT (qualifying_scope),
15170                                 TYPE_IDENTIFIER (qualifying_scope));
15171                   }
15172                 qualifying_scope = type;
15173               }
15174
15175             sfk = sfk_none;
15176
15177             if (unqualified_name)
15178               {
15179                 tree class_type;
15180
15181                 if (qualifying_scope
15182                     && CLASS_TYPE_P (qualifying_scope))
15183                   class_type = qualifying_scope;
15184                 else
15185                   class_type = current_class_type;
15186
15187                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15188                   {
15189                     tree name_type = TREE_TYPE (unqualified_name);
15190                     if (class_type && same_type_p (name_type, class_type))
15191                       {
15192                         if (qualifying_scope
15193                             && CLASSTYPE_USE_TEMPLATE (name_type))
15194                           {
15195                             error_at (declarator_id_start_token->location,
15196                                       "invalid use of constructor as a template");
15197                             inform (declarator_id_start_token->location,
15198                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15199                                     "name the constructor in a qualified name",
15200                                     class_type,
15201                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15202                                     class_type, name_type);
15203                             declarator = cp_error_declarator;
15204                             break;
15205                           }
15206                         else
15207                           unqualified_name = constructor_name (class_type);
15208                       }
15209                     else
15210                       {
15211                         /* We do not attempt to print the declarator
15212                            here because we do not have enough
15213                            information about its original syntactic
15214                            form.  */
15215                         cp_parser_error (parser, "invalid declarator");
15216                         declarator = cp_error_declarator;
15217                         break;
15218                       }
15219                   }
15220
15221                 if (class_type)
15222                   {
15223                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15224                       sfk = sfk_destructor;
15225                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15226                       sfk = sfk_conversion;
15227                     else if (/* There's no way to declare a constructor
15228                                 for an anonymous type, even if the type
15229                                 got a name for linkage purposes.  */
15230                              !TYPE_WAS_ANONYMOUS (class_type)
15231                              && constructor_name_p (unqualified_name,
15232                                                     class_type))
15233                       {
15234                         unqualified_name = constructor_name (class_type);
15235                         sfk = sfk_constructor;
15236                       }
15237                     else if (is_overloaded_fn (unqualified_name)
15238                              && DECL_CONSTRUCTOR_P (get_first_fn
15239                                                     (unqualified_name)))
15240                       sfk = sfk_constructor;
15241
15242                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15243                       *ctor_dtor_or_conv_p = -1;
15244                   }
15245               }
15246             declarator = make_id_declarator (qualifying_scope,
15247                                              unqualified_name,
15248                                              sfk);
15249             declarator->id_loc = token->location;
15250             declarator->parameter_pack_p = pack_expansion_p;
15251
15252             if (pack_expansion_p)
15253               maybe_warn_variadic_templates ();
15254           }
15255
15256         handle_declarator:;
15257           scope = get_scope_of_declarator (declarator);
15258           if (scope)
15259             /* Any names that appear after the declarator-id for a
15260                member are looked up in the containing scope.  */
15261             pushed_scope = push_scope (scope);
15262           parser->in_declarator_p = true;
15263           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15264               || (declarator && declarator->kind == cdk_id))
15265             /* Default args are only allowed on function
15266                declarations.  */
15267             parser->default_arg_ok_p = saved_default_arg_ok_p;
15268           else
15269             parser->default_arg_ok_p = false;
15270
15271           first = false;
15272         }
15273       /* We're done.  */
15274       else
15275         break;
15276     }
15277
15278   /* For an abstract declarator, we might wind up with nothing at this
15279      point.  That's an error; the declarator is not optional.  */
15280   if (!declarator)
15281     cp_parser_error (parser, "expected declarator");
15282
15283   /* If we entered a scope, we must exit it now.  */
15284   if (pushed_scope)
15285     pop_scope (pushed_scope);
15286
15287   parser->default_arg_ok_p = saved_default_arg_ok_p;
15288   parser->in_declarator_p = saved_in_declarator_p;
15289
15290   return declarator;
15291 }
15292
15293 /* Parse a ptr-operator.
15294
15295    ptr-operator:
15296      * cv-qualifier-seq [opt]
15297      &
15298      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15299
15300    GNU Extension:
15301
15302    ptr-operator:
15303      & cv-qualifier-seq [opt]
15304
15305    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15306    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15307    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15308    filled in with the TYPE containing the member.  *CV_QUALS is
15309    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15310    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15311    Note that the tree codes returned by this function have nothing
15312    to do with the types of trees that will be eventually be created
15313    to represent the pointer or reference type being parsed. They are
15314    just constants with suggestive names. */
15315 static enum tree_code
15316 cp_parser_ptr_operator (cp_parser* parser,
15317                         tree* type,
15318                         cp_cv_quals *cv_quals)
15319 {
15320   enum tree_code code = ERROR_MARK;
15321   cp_token *token;
15322
15323   /* Assume that it's not a pointer-to-member.  */
15324   *type = NULL_TREE;
15325   /* And that there are no cv-qualifiers.  */
15326   *cv_quals = TYPE_UNQUALIFIED;
15327
15328   /* Peek at the next token.  */
15329   token = cp_lexer_peek_token (parser->lexer);
15330
15331   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15332   if (token->type == CPP_MULT)
15333     code = INDIRECT_REF;
15334   else if (token->type == CPP_AND)
15335     code = ADDR_EXPR;
15336   else if ((cxx_dialect != cxx98) &&
15337            token->type == CPP_AND_AND) /* C++0x only */
15338     code = NON_LVALUE_EXPR;
15339
15340   if (code != ERROR_MARK)
15341     {
15342       /* Consume the `*', `&' or `&&'.  */
15343       cp_lexer_consume_token (parser->lexer);
15344
15345       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15346          `&', if we are allowing GNU extensions.  (The only qualifier
15347          that can legally appear after `&' is `restrict', but that is
15348          enforced during semantic analysis.  */
15349       if (code == INDIRECT_REF
15350           || cp_parser_allow_gnu_extensions_p (parser))
15351         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15352     }
15353   else
15354     {
15355       /* Try the pointer-to-member case.  */
15356       cp_parser_parse_tentatively (parser);
15357       /* Look for the optional `::' operator.  */
15358       cp_parser_global_scope_opt (parser,
15359                                   /*current_scope_valid_p=*/false);
15360       /* Look for the nested-name specifier.  */
15361       token = cp_lexer_peek_token (parser->lexer);
15362       cp_parser_nested_name_specifier (parser,
15363                                        /*typename_keyword_p=*/false,
15364                                        /*check_dependency_p=*/true,
15365                                        /*type_p=*/false,
15366                                        /*is_declaration=*/false);
15367       /* If we found it, and the next token is a `*', then we are
15368          indeed looking at a pointer-to-member operator.  */
15369       if (!cp_parser_error_occurred (parser)
15370           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15371         {
15372           /* Indicate that the `*' operator was used.  */
15373           code = INDIRECT_REF;
15374
15375           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15376             error_at (token->location, "%qD is a namespace", parser->scope);
15377           else
15378             {
15379               /* The type of which the member is a member is given by the
15380                  current SCOPE.  */
15381               *type = parser->scope;
15382               /* The next name will not be qualified.  */
15383               parser->scope = NULL_TREE;
15384               parser->qualifying_scope = NULL_TREE;
15385               parser->object_scope = NULL_TREE;
15386               /* Look for the optional cv-qualifier-seq.  */
15387               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15388             }
15389         }
15390       /* If that didn't work we don't have a ptr-operator.  */
15391       if (!cp_parser_parse_definitely (parser))
15392         cp_parser_error (parser, "expected ptr-operator");
15393     }
15394
15395   return code;
15396 }
15397
15398 /* Parse an (optional) cv-qualifier-seq.
15399
15400    cv-qualifier-seq:
15401      cv-qualifier cv-qualifier-seq [opt]
15402
15403    cv-qualifier:
15404      const
15405      volatile
15406
15407    GNU Extension:
15408
15409    cv-qualifier:
15410      __restrict__
15411
15412    Returns a bitmask representing the cv-qualifiers.  */
15413
15414 static cp_cv_quals
15415 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15416 {
15417   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15418
15419   while (true)
15420     {
15421       cp_token *token;
15422       cp_cv_quals cv_qualifier;
15423
15424       /* Peek at the next token.  */
15425       token = cp_lexer_peek_token (parser->lexer);
15426       /* See if it's a cv-qualifier.  */
15427       switch (token->keyword)
15428         {
15429         case RID_CONST:
15430           cv_qualifier = TYPE_QUAL_CONST;
15431           break;
15432
15433         case RID_VOLATILE:
15434           cv_qualifier = TYPE_QUAL_VOLATILE;
15435           break;
15436
15437         case RID_RESTRICT:
15438           cv_qualifier = TYPE_QUAL_RESTRICT;
15439           break;
15440
15441         default:
15442           cv_qualifier = TYPE_UNQUALIFIED;
15443           break;
15444         }
15445
15446       if (!cv_qualifier)
15447         break;
15448
15449       if (cv_quals & cv_qualifier)
15450         {
15451           error_at (token->location, "duplicate cv-qualifier");
15452           cp_lexer_purge_token (parser->lexer);
15453         }
15454       else
15455         {
15456           cp_lexer_consume_token (parser->lexer);
15457           cv_quals |= cv_qualifier;
15458         }
15459     }
15460
15461   return cv_quals;
15462 }
15463
15464 /* Parse an (optional) virt-specifier-seq.
15465
15466    virt-specifier-seq:
15467      virt-specifier virt-specifier-seq [opt]
15468
15469    virt-specifier:
15470      override
15471      final
15472
15473    Returns a bitmask representing the virt-specifiers.  */
15474
15475 static cp_virt_specifiers
15476 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15477 {
15478   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15479
15480   while (true)
15481     {
15482       cp_token *token;
15483       cp_virt_specifiers virt_specifier;
15484
15485       /* Peek at the next token.  */
15486       token = cp_lexer_peek_token (parser->lexer);
15487       /* See if it's a virt-specifier-qualifier.  */
15488       if (token->type != CPP_NAME)
15489         break;
15490       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15491         virt_specifier = VIRT_SPEC_OVERRIDE;
15492       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15493         virt_specifier = VIRT_SPEC_FINAL;
15494       else
15495         break;
15496
15497       if (virt_specifiers & virt_specifier)
15498         {
15499           error_at (token->location, "duplicate virt-specifier");
15500           cp_lexer_purge_token (parser->lexer);
15501         }
15502       else
15503         {
15504           cp_lexer_consume_token (parser->lexer);
15505           virt_specifiers |= virt_specifier;
15506         }
15507     }
15508   return virt_specifiers;
15509 }
15510
15511 /* Parse a late-specified return type, if any.  This is not a separate
15512    non-terminal, but part of a function declarator, which looks like
15513
15514    -> trailing-type-specifier-seq abstract-declarator(opt)
15515
15516    Returns the type indicated by the type-id.  */
15517
15518 static tree
15519 cp_parser_late_return_type_opt (cp_parser* parser)
15520 {
15521   cp_token *token;
15522
15523   /* Peek at the next token.  */
15524   token = cp_lexer_peek_token (parser->lexer);
15525   /* A late-specified return type is indicated by an initial '->'. */
15526   if (token->type != CPP_DEREF)
15527     return NULL_TREE;
15528
15529   /* Consume the ->.  */
15530   cp_lexer_consume_token (parser->lexer);
15531
15532   return cp_parser_trailing_type_id (parser);
15533 }
15534
15535 /* Parse a declarator-id.
15536
15537    declarator-id:
15538      id-expression
15539      :: [opt] nested-name-specifier [opt] type-name
15540
15541    In the `id-expression' case, the value returned is as for
15542    cp_parser_id_expression if the id-expression was an unqualified-id.
15543    If the id-expression was a qualified-id, then a SCOPE_REF is
15544    returned.  The first operand is the scope (either a NAMESPACE_DECL
15545    or TREE_TYPE), but the second is still just a representation of an
15546    unqualified-id.  */
15547
15548 static tree
15549 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15550 {
15551   tree id;
15552   /* The expression must be an id-expression.  Assume that qualified
15553      names are the names of types so that:
15554
15555        template <class T>
15556        int S<T>::R::i = 3;
15557
15558      will work; we must treat `S<T>::R' as the name of a type.
15559      Similarly, assume that qualified names are templates, where
15560      required, so that:
15561
15562        template <class T>
15563        int S<T>::R<T>::i = 3;
15564
15565      will work, too.  */
15566   id = cp_parser_id_expression (parser,
15567                                 /*template_keyword_p=*/false,
15568                                 /*check_dependency_p=*/false,
15569                                 /*template_p=*/NULL,
15570                                 /*declarator_p=*/true,
15571                                 optional_p);
15572   if (id && BASELINK_P (id))
15573     id = BASELINK_FUNCTIONS (id);
15574   return id;
15575 }
15576
15577 /* Parse a type-id.
15578
15579    type-id:
15580      type-specifier-seq abstract-declarator [opt]
15581
15582    Returns the TYPE specified.  */
15583
15584 static tree
15585 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15586                      bool is_trailing_return)
15587 {
15588   cp_decl_specifier_seq type_specifier_seq;
15589   cp_declarator *abstract_declarator;
15590
15591   /* Parse the type-specifier-seq.  */
15592   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15593                                 is_trailing_return,
15594                                 &type_specifier_seq);
15595   if (type_specifier_seq.type == error_mark_node)
15596     return error_mark_node;
15597
15598   /* There might or might not be an abstract declarator.  */
15599   cp_parser_parse_tentatively (parser);
15600   /* Look for the declarator.  */
15601   abstract_declarator
15602     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15603                             /*parenthesized_p=*/NULL,
15604                             /*member_p=*/false);
15605   /* Check to see if there really was a declarator.  */
15606   if (!cp_parser_parse_definitely (parser))
15607     abstract_declarator = NULL;
15608
15609   if (type_specifier_seq.type
15610       && type_uses_auto (type_specifier_seq.type))
15611     {
15612       /* A type-id with type 'auto' is only ok if the abstract declarator
15613          is a function declarator with a late-specified return type.  */
15614       if (abstract_declarator
15615           && abstract_declarator->kind == cdk_function
15616           && abstract_declarator->u.function.late_return_type)
15617         /* OK */;
15618       else
15619         {
15620           error ("invalid use of %<auto%>");
15621           return error_mark_node;
15622         }
15623     }
15624   
15625   return groktypename (&type_specifier_seq, abstract_declarator,
15626                        is_template_arg);
15627 }
15628
15629 static tree cp_parser_type_id (cp_parser *parser)
15630 {
15631   return cp_parser_type_id_1 (parser, false, false);
15632 }
15633
15634 static tree cp_parser_template_type_arg (cp_parser *parser)
15635 {
15636   tree r;
15637   const char *saved_message = parser->type_definition_forbidden_message;
15638   parser->type_definition_forbidden_message
15639     = G_("types may not be defined in template arguments");
15640   r = cp_parser_type_id_1 (parser, true, false);
15641   parser->type_definition_forbidden_message = saved_message;
15642   return r;
15643 }
15644
15645 static tree cp_parser_trailing_type_id (cp_parser *parser)
15646 {
15647   return cp_parser_type_id_1 (parser, false, true);
15648 }
15649
15650 /* Parse a type-specifier-seq.
15651
15652    type-specifier-seq:
15653      type-specifier type-specifier-seq [opt]
15654
15655    GNU extension:
15656
15657    type-specifier-seq:
15658      attributes type-specifier-seq [opt]
15659
15660    If IS_DECLARATION is true, we are at the start of a "condition" or
15661    exception-declaration, so we might be followed by a declarator-id.
15662
15663    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15664    i.e. we've just seen "->".
15665
15666    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15667
15668 static void
15669 cp_parser_type_specifier_seq (cp_parser* parser,
15670                               bool is_declaration,
15671                               bool is_trailing_return,
15672                               cp_decl_specifier_seq *type_specifier_seq)
15673 {
15674   bool seen_type_specifier = false;
15675   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15676   cp_token *start_token = NULL;
15677
15678   /* Clear the TYPE_SPECIFIER_SEQ.  */
15679   clear_decl_specs (type_specifier_seq);
15680
15681   /* In the context of a trailing return type, enum E { } is an
15682      elaborated-type-specifier followed by a function-body, not an
15683      enum-specifier.  */
15684   if (is_trailing_return)
15685     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15686
15687   /* Parse the type-specifiers and attributes.  */
15688   while (true)
15689     {
15690       tree type_specifier;
15691       bool is_cv_qualifier;
15692
15693       /* Check for attributes first.  */
15694       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15695         {
15696           type_specifier_seq->attributes =
15697             chainon (type_specifier_seq->attributes,
15698                      cp_parser_attributes_opt (parser));
15699           continue;
15700         }
15701
15702       /* record the token of the beginning of the type specifier seq,
15703          for error reporting purposes*/
15704      if (!start_token)
15705        start_token = cp_lexer_peek_token (parser->lexer);
15706
15707       /* Look for the type-specifier.  */
15708       type_specifier = cp_parser_type_specifier (parser,
15709                                                  flags,
15710                                                  type_specifier_seq,
15711                                                  /*is_declaration=*/false,
15712                                                  NULL,
15713                                                  &is_cv_qualifier);
15714       if (!type_specifier)
15715         {
15716           /* If the first type-specifier could not be found, this is not a
15717              type-specifier-seq at all.  */
15718           if (!seen_type_specifier)
15719             {
15720               cp_parser_error (parser, "expected type-specifier");
15721               type_specifier_seq->type = error_mark_node;
15722               return;
15723             }
15724           /* If subsequent type-specifiers could not be found, the
15725              type-specifier-seq is complete.  */
15726           break;
15727         }
15728
15729       seen_type_specifier = true;
15730       /* The standard says that a condition can be:
15731
15732             type-specifier-seq declarator = assignment-expression
15733
15734          However, given:
15735
15736            struct S {};
15737            if (int S = ...)
15738
15739          we should treat the "S" as a declarator, not as a
15740          type-specifier.  The standard doesn't say that explicitly for
15741          type-specifier-seq, but it does say that for
15742          decl-specifier-seq in an ordinary declaration.  Perhaps it
15743          would be clearer just to allow a decl-specifier-seq here, and
15744          then add a semantic restriction that if any decl-specifiers
15745          that are not type-specifiers appear, the program is invalid.  */
15746       if (is_declaration && !is_cv_qualifier)
15747         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15748     }
15749
15750   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15751 }
15752
15753 /* Parse a parameter-declaration-clause.
15754
15755    parameter-declaration-clause:
15756      parameter-declaration-list [opt] ... [opt]
15757      parameter-declaration-list , ...
15758
15759    Returns a representation for the parameter declarations.  A return
15760    value of NULL indicates a parameter-declaration-clause consisting
15761    only of an ellipsis.  */
15762
15763 static tree
15764 cp_parser_parameter_declaration_clause (cp_parser* parser)
15765 {
15766   tree parameters;
15767   cp_token *token;
15768   bool ellipsis_p;
15769   bool is_error;
15770
15771   /* Peek at the next token.  */
15772   token = cp_lexer_peek_token (parser->lexer);
15773   /* Check for trivial parameter-declaration-clauses.  */
15774   if (token->type == CPP_ELLIPSIS)
15775     {
15776       /* Consume the `...' token.  */
15777       cp_lexer_consume_token (parser->lexer);
15778       return NULL_TREE;
15779     }
15780   else if (token->type == CPP_CLOSE_PAREN)
15781     /* There are no parameters.  */
15782     {
15783 #ifndef NO_IMPLICIT_EXTERN_C
15784       if (in_system_header && current_class_type == NULL
15785           && current_lang_name == lang_name_c)
15786         return NULL_TREE;
15787       else
15788 #endif
15789         return void_list_node;
15790     }
15791   /* Check for `(void)', too, which is a special case.  */
15792   else if (token->keyword == RID_VOID
15793            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15794                == CPP_CLOSE_PAREN))
15795     {
15796       /* Consume the `void' token.  */
15797       cp_lexer_consume_token (parser->lexer);
15798       /* There are no parameters.  */
15799       return void_list_node;
15800     }
15801
15802   /* Parse the parameter-declaration-list.  */
15803   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15804   /* If a parse error occurred while parsing the
15805      parameter-declaration-list, then the entire
15806      parameter-declaration-clause is erroneous.  */
15807   if (is_error)
15808     return NULL;
15809
15810   /* Peek at the next token.  */
15811   token = cp_lexer_peek_token (parser->lexer);
15812   /* If it's a `,', the clause should terminate with an ellipsis.  */
15813   if (token->type == CPP_COMMA)
15814     {
15815       /* Consume the `,'.  */
15816       cp_lexer_consume_token (parser->lexer);
15817       /* Expect an ellipsis.  */
15818       ellipsis_p
15819         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15820     }
15821   /* It might also be `...' if the optional trailing `,' was
15822      omitted.  */
15823   else if (token->type == CPP_ELLIPSIS)
15824     {
15825       /* Consume the `...' token.  */
15826       cp_lexer_consume_token (parser->lexer);
15827       /* And remember that we saw it.  */
15828       ellipsis_p = true;
15829     }
15830   else
15831     ellipsis_p = false;
15832
15833   /* Finish the parameter list.  */
15834   if (!ellipsis_p)
15835     parameters = chainon (parameters, void_list_node);
15836
15837   return parameters;
15838 }
15839
15840 /* Parse a parameter-declaration-list.
15841
15842    parameter-declaration-list:
15843      parameter-declaration
15844      parameter-declaration-list , parameter-declaration
15845
15846    Returns a representation of the parameter-declaration-list, as for
15847    cp_parser_parameter_declaration_clause.  However, the
15848    `void_list_node' is never appended to the list.  Upon return,
15849    *IS_ERROR will be true iff an error occurred.  */
15850
15851 static tree
15852 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15853 {
15854   tree parameters = NULL_TREE;
15855   tree *tail = &parameters; 
15856   bool saved_in_unbraced_linkage_specification_p;
15857   int index = 0;
15858
15859   /* Assume all will go well.  */
15860   *is_error = false;
15861   /* The special considerations that apply to a function within an
15862      unbraced linkage specifications do not apply to the parameters
15863      to the function.  */
15864   saved_in_unbraced_linkage_specification_p 
15865     = parser->in_unbraced_linkage_specification_p;
15866   parser->in_unbraced_linkage_specification_p = false;
15867
15868   /* Look for more parameters.  */
15869   while (true)
15870     {
15871       cp_parameter_declarator *parameter;
15872       tree decl = error_mark_node;
15873       bool parenthesized_p;
15874       /* Parse the parameter.  */
15875       parameter
15876         = cp_parser_parameter_declaration (parser,
15877                                            /*template_parm_p=*/false,
15878                                            &parenthesized_p);
15879
15880       /* We don't know yet if the enclosing context is deprecated, so wait
15881          and warn in grokparms if appropriate.  */
15882       deprecated_state = DEPRECATED_SUPPRESS;
15883
15884       if (parameter)
15885         decl = grokdeclarator (parameter->declarator,
15886                                &parameter->decl_specifiers,
15887                                PARM,
15888                                parameter->default_argument != NULL_TREE,
15889                                &parameter->decl_specifiers.attributes);
15890
15891       deprecated_state = DEPRECATED_NORMAL;
15892
15893       /* If a parse error occurred parsing the parameter declaration,
15894          then the entire parameter-declaration-list is erroneous.  */
15895       if (decl == error_mark_node)
15896         {
15897           *is_error = true;
15898           parameters = error_mark_node;
15899           break;
15900         }
15901
15902       if (parameter->decl_specifiers.attributes)
15903         cplus_decl_attributes (&decl,
15904                                parameter->decl_specifiers.attributes,
15905                                0);
15906       if (DECL_NAME (decl))
15907         decl = pushdecl (decl);
15908
15909       if (decl != error_mark_node)
15910         {
15911           retrofit_lang_decl (decl);
15912           DECL_PARM_INDEX (decl) = ++index;
15913           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15914         }
15915
15916       /* Add the new parameter to the list.  */
15917       *tail = build_tree_list (parameter->default_argument, decl);
15918       tail = &TREE_CHAIN (*tail);
15919
15920       /* Peek at the next token.  */
15921       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15922           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15923           /* These are for Objective-C++ */
15924           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15925           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15926         /* The parameter-declaration-list is complete.  */
15927         break;
15928       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15929         {
15930           cp_token *token;
15931
15932           /* Peek at the next token.  */
15933           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15934           /* If it's an ellipsis, then the list is complete.  */
15935           if (token->type == CPP_ELLIPSIS)
15936             break;
15937           /* Otherwise, there must be more parameters.  Consume the
15938              `,'.  */
15939           cp_lexer_consume_token (parser->lexer);
15940           /* When parsing something like:
15941
15942                 int i(float f, double d)
15943
15944              we can tell after seeing the declaration for "f" that we
15945              are not looking at an initialization of a variable "i",
15946              but rather at the declaration of a function "i".
15947
15948              Due to the fact that the parsing of template arguments
15949              (as specified to a template-id) requires backtracking we
15950              cannot use this technique when inside a template argument
15951              list.  */
15952           if (!parser->in_template_argument_list_p
15953               && !parser->in_type_id_in_expr_p
15954               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15955               /* However, a parameter-declaration of the form
15956                  "foat(f)" (which is a valid declaration of a
15957                  parameter "f") can also be interpreted as an
15958                  expression (the conversion of "f" to "float").  */
15959               && !parenthesized_p)
15960             cp_parser_commit_to_tentative_parse (parser);
15961         }
15962       else
15963         {
15964           cp_parser_error (parser, "expected %<,%> or %<...%>");
15965           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15966             cp_parser_skip_to_closing_parenthesis (parser,
15967                                                    /*recovering=*/true,
15968                                                    /*or_comma=*/false,
15969                                                    /*consume_paren=*/false);
15970           break;
15971         }
15972     }
15973
15974   parser->in_unbraced_linkage_specification_p
15975     = saved_in_unbraced_linkage_specification_p;
15976
15977   return parameters;
15978 }
15979
15980 /* Parse a parameter declaration.
15981
15982    parameter-declaration:
15983      decl-specifier-seq ... [opt] declarator
15984      decl-specifier-seq declarator = assignment-expression
15985      decl-specifier-seq ... [opt] abstract-declarator [opt]
15986      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15987
15988    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15989    declares a template parameter.  (In that case, a non-nested `>'
15990    token encountered during the parsing of the assignment-expression
15991    is not interpreted as a greater-than operator.)
15992
15993    Returns a representation of the parameter, or NULL if an error
15994    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15995    true iff the declarator is of the form "(p)".  */
15996
15997 static cp_parameter_declarator *
15998 cp_parser_parameter_declaration (cp_parser *parser,
15999                                  bool template_parm_p,
16000                                  bool *parenthesized_p)
16001 {
16002   int declares_class_or_enum;
16003   cp_decl_specifier_seq decl_specifiers;
16004   cp_declarator *declarator;
16005   tree default_argument;
16006   cp_token *token = NULL, *declarator_token_start = NULL;
16007   const char *saved_message;
16008
16009   /* In a template parameter, `>' is not an operator.
16010
16011      [temp.param]
16012
16013      When parsing a default template-argument for a non-type
16014      template-parameter, the first non-nested `>' is taken as the end
16015      of the template parameter-list rather than a greater-than
16016      operator.  */
16017
16018   /* Type definitions may not appear in parameter types.  */
16019   saved_message = parser->type_definition_forbidden_message;
16020   parser->type_definition_forbidden_message
16021     = G_("types may not be defined in parameter types");
16022
16023   /* Parse the declaration-specifiers.  */
16024   cp_parser_decl_specifier_seq (parser,
16025                                 CP_PARSER_FLAGS_NONE,
16026                                 &decl_specifiers,
16027                                 &declares_class_or_enum);
16028
16029   /* Complain about missing 'typename' or other invalid type names.  */
16030   if (!decl_specifiers.any_type_specifiers_p)
16031     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16032
16033   /* If an error occurred, there's no reason to attempt to parse the
16034      rest of the declaration.  */
16035   if (cp_parser_error_occurred (parser))
16036     {
16037       parser->type_definition_forbidden_message = saved_message;
16038       return NULL;
16039     }
16040
16041   /* Peek at the next token.  */
16042   token = cp_lexer_peek_token (parser->lexer);
16043
16044   /* If the next token is a `)', `,', `=', `>', or `...', then there
16045      is no declarator. However, when variadic templates are enabled,
16046      there may be a declarator following `...'.  */
16047   if (token->type == CPP_CLOSE_PAREN
16048       || token->type == CPP_COMMA
16049       || token->type == CPP_EQ
16050       || token->type == CPP_GREATER)
16051     {
16052       declarator = NULL;
16053       if (parenthesized_p)
16054         *parenthesized_p = false;
16055     }
16056   /* Otherwise, there should be a declarator.  */
16057   else
16058     {
16059       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16060       parser->default_arg_ok_p = false;
16061
16062       /* After seeing a decl-specifier-seq, if the next token is not a
16063          "(", there is no possibility that the code is a valid
16064          expression.  Therefore, if parsing tentatively, we commit at
16065          this point.  */
16066       if (!parser->in_template_argument_list_p
16067           /* In an expression context, having seen:
16068
16069                (int((char ...
16070
16071              we cannot be sure whether we are looking at a
16072              function-type (taking a "char" as a parameter) or a cast
16073              of some object of type "char" to "int".  */
16074           && !parser->in_type_id_in_expr_p
16075           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16076           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16077           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16078         cp_parser_commit_to_tentative_parse (parser);
16079       /* Parse the declarator.  */
16080       declarator_token_start = token;
16081       declarator = cp_parser_declarator (parser,
16082                                          CP_PARSER_DECLARATOR_EITHER,
16083                                          /*ctor_dtor_or_conv_p=*/NULL,
16084                                          parenthesized_p,
16085                                          /*member_p=*/false);
16086       parser->default_arg_ok_p = saved_default_arg_ok_p;
16087       /* After the declarator, allow more attributes.  */
16088       decl_specifiers.attributes
16089         = chainon (decl_specifiers.attributes,
16090                    cp_parser_attributes_opt (parser));
16091     }
16092
16093   /* If the next token is an ellipsis, and we have not seen a
16094      declarator name, and the type of the declarator contains parameter
16095      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16096      a parameter pack expansion expression. Otherwise, leave the
16097      ellipsis for a C-style variadic function. */
16098   token = cp_lexer_peek_token (parser->lexer);
16099   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16100     {
16101       tree type = decl_specifiers.type;
16102
16103       if (type && DECL_P (type))
16104         type = TREE_TYPE (type);
16105
16106       if (type
16107           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16108           && declarator_can_be_parameter_pack (declarator)
16109           && (!declarator || !declarator->parameter_pack_p)
16110           && uses_parameter_packs (type))
16111         {
16112           /* Consume the `...'. */
16113           cp_lexer_consume_token (parser->lexer);
16114           maybe_warn_variadic_templates ();
16115           
16116           /* Build a pack expansion type */
16117           if (declarator)
16118             declarator->parameter_pack_p = true;
16119           else
16120             decl_specifiers.type = make_pack_expansion (type);
16121         }
16122     }
16123
16124   /* The restriction on defining new types applies only to the type
16125      of the parameter, not to the default argument.  */
16126   parser->type_definition_forbidden_message = saved_message;
16127
16128   /* If the next token is `=', then process a default argument.  */
16129   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16130     {
16131       /* Consume the `='.  */
16132       cp_lexer_consume_token (parser->lexer);
16133
16134       /* If we are defining a class, then the tokens that make up the
16135          default argument must be saved and processed later.  */
16136       if (!template_parm_p && at_class_scope_p ()
16137           && TYPE_BEING_DEFINED (current_class_type)
16138           && !LAMBDA_TYPE_P (current_class_type))
16139         {
16140           unsigned depth = 0;
16141           int maybe_template_id = 0;
16142           cp_token *first_token;
16143           cp_token *token;
16144
16145           /* Add tokens until we have processed the entire default
16146              argument.  We add the range [first_token, token).  */
16147           first_token = cp_lexer_peek_token (parser->lexer);
16148           while (true)
16149             {
16150               bool done = false;
16151
16152               /* Peek at the next token.  */
16153               token = cp_lexer_peek_token (parser->lexer);
16154               /* What we do depends on what token we have.  */
16155               switch (token->type)
16156                 {
16157                   /* In valid code, a default argument must be
16158                      immediately followed by a `,' `)', or `...'.  */
16159                 case CPP_COMMA:
16160                   if (depth == 0 && maybe_template_id)
16161                     {
16162                       /* If we've seen a '<', we might be in a
16163                          template-argument-list.  Until Core issue 325 is
16164                          resolved, we don't know how this situation ought
16165                          to be handled, so try to DTRT.  We check whether
16166                          what comes after the comma is a valid parameter
16167                          declaration list.  If it is, then the comma ends
16168                          the default argument; otherwise the default
16169                          argument continues.  */
16170                       bool error = false;
16171                       tree t;
16172
16173                       /* Set ITALP so cp_parser_parameter_declaration_list
16174                          doesn't decide to commit to this parse.  */
16175                       bool saved_italp = parser->in_template_argument_list_p;
16176                       parser->in_template_argument_list_p = true;
16177
16178                       cp_parser_parse_tentatively (parser);
16179                       cp_lexer_consume_token (parser->lexer);
16180                       begin_scope (sk_function_parms, NULL_TREE);
16181                       cp_parser_parameter_declaration_list (parser, &error);
16182                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16183                         pop_binding (DECL_NAME (t), t);
16184                       leave_scope ();
16185                       if (!cp_parser_error_occurred (parser) && !error)
16186                         done = true;
16187                       cp_parser_abort_tentative_parse (parser);
16188
16189                       parser->in_template_argument_list_p = saved_italp;
16190                       break;
16191                     }
16192                 case CPP_CLOSE_PAREN:
16193                 case CPP_ELLIPSIS:
16194                   /* If we run into a non-nested `;', `}', or `]',
16195                      then the code is invalid -- but the default
16196                      argument is certainly over.  */
16197                 case CPP_SEMICOLON:
16198                 case CPP_CLOSE_BRACE:
16199                 case CPP_CLOSE_SQUARE:
16200                   if (depth == 0)
16201                     done = true;
16202                   /* Update DEPTH, if necessary.  */
16203                   else if (token->type == CPP_CLOSE_PAREN
16204                            || token->type == CPP_CLOSE_BRACE
16205                            || token->type == CPP_CLOSE_SQUARE)
16206                     --depth;
16207                   break;
16208
16209                 case CPP_OPEN_PAREN:
16210                 case CPP_OPEN_SQUARE:
16211                 case CPP_OPEN_BRACE:
16212                   ++depth;
16213                   break;
16214
16215                 case CPP_LESS:
16216                   if (depth == 0)
16217                     /* This might be the comparison operator, or it might
16218                        start a template argument list.  */
16219                     ++maybe_template_id;
16220                   break;
16221
16222                 case CPP_RSHIFT:
16223                   if (cxx_dialect == cxx98)
16224                     break;
16225                   /* Fall through for C++0x, which treats the `>>'
16226                      operator like two `>' tokens in certain
16227                      cases.  */
16228
16229                 case CPP_GREATER:
16230                   if (depth == 0)
16231                     {
16232                       /* This might be an operator, or it might close a
16233                          template argument list.  But if a previous '<'
16234                          started a template argument list, this will have
16235                          closed it, so we can't be in one anymore.  */
16236                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16237                       if (maybe_template_id < 0)
16238                         maybe_template_id = 0;
16239                     }
16240                   break;
16241
16242                   /* If we run out of tokens, issue an error message.  */
16243                 case CPP_EOF:
16244                 case CPP_PRAGMA_EOL:
16245                   error_at (token->location, "file ends in default argument");
16246                   done = true;
16247                   break;
16248
16249                 case CPP_NAME:
16250                 case CPP_SCOPE:
16251                   /* In these cases, we should look for template-ids.
16252                      For example, if the default argument is
16253                      `X<int, double>()', we need to do name lookup to
16254                      figure out whether or not `X' is a template; if
16255                      so, the `,' does not end the default argument.
16256
16257                      That is not yet done.  */
16258                   break;
16259
16260                 default:
16261                   break;
16262                 }
16263
16264               /* If we've reached the end, stop.  */
16265               if (done)
16266                 break;
16267
16268               /* Add the token to the token block.  */
16269               token = cp_lexer_consume_token (parser->lexer);
16270             }
16271
16272           /* Create a DEFAULT_ARG to represent the unparsed default
16273              argument.  */
16274           default_argument = make_node (DEFAULT_ARG);
16275           DEFARG_TOKENS (default_argument)
16276             = cp_token_cache_new (first_token, token);
16277           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16278         }
16279       /* Outside of a class definition, we can just parse the
16280          assignment-expression.  */
16281       else
16282         {
16283           token = cp_lexer_peek_token (parser->lexer);
16284           default_argument 
16285             = cp_parser_default_argument (parser, template_parm_p);
16286         }
16287
16288       if (!parser->default_arg_ok_p)
16289         {
16290           if (flag_permissive)
16291             warning (0, "deprecated use of default argument for parameter of non-function");
16292           else
16293             {
16294               error_at (token->location,
16295                         "default arguments are only "
16296                         "permitted for function parameters");
16297               default_argument = NULL_TREE;
16298             }
16299         }
16300       else if ((declarator && declarator->parameter_pack_p)
16301                || (decl_specifiers.type
16302                    && PACK_EXPANSION_P (decl_specifiers.type)))
16303         {
16304           /* Find the name of the parameter pack.  */     
16305           cp_declarator *id_declarator = declarator;
16306           while (id_declarator && id_declarator->kind != cdk_id)
16307             id_declarator = id_declarator->declarator;
16308           
16309           if (id_declarator && id_declarator->kind == cdk_id)
16310             error_at (declarator_token_start->location,
16311                       template_parm_p 
16312                       ? "template parameter pack %qD"
16313                       " cannot have a default argument"
16314                       : "parameter pack %qD cannot have a default argument",
16315                       id_declarator->u.id.unqualified_name);
16316           else
16317             error_at (declarator_token_start->location,
16318                       template_parm_p 
16319                       ? "template parameter pack cannot have a default argument"
16320                       : "parameter pack cannot have a default argument");
16321           
16322           default_argument = NULL_TREE;
16323         }
16324     }
16325   else
16326     default_argument = NULL_TREE;
16327
16328   return make_parameter_declarator (&decl_specifiers,
16329                                     declarator,
16330                                     default_argument);
16331 }
16332
16333 /* Parse a default argument and return it.
16334
16335    TEMPLATE_PARM_P is true if this is a default argument for a
16336    non-type template parameter.  */
16337 static tree
16338 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16339 {
16340   tree default_argument = NULL_TREE;
16341   bool saved_greater_than_is_operator_p;
16342   bool saved_local_variables_forbidden_p;
16343
16344   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16345      set correctly.  */
16346   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16347   parser->greater_than_is_operator_p = !template_parm_p;
16348   /* Local variable names (and the `this' keyword) may not
16349      appear in a default argument.  */
16350   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16351   parser->local_variables_forbidden_p = true;
16352   /* Parse the assignment-expression.  */
16353   if (template_parm_p)
16354     push_deferring_access_checks (dk_no_deferred);
16355   default_argument
16356     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16357   if (template_parm_p)
16358     pop_deferring_access_checks ();
16359   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16360   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16361
16362   return default_argument;
16363 }
16364
16365 /* Parse a function-body.
16366
16367    function-body:
16368      compound_statement  */
16369
16370 static void
16371 cp_parser_function_body (cp_parser *parser)
16372 {
16373   cp_parser_compound_statement (parser, NULL, false, true);
16374 }
16375
16376 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16377    true if a ctor-initializer was present.  */
16378
16379 static bool
16380 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16381 {
16382   tree body, list;
16383   bool ctor_initializer_p;
16384   const bool check_body_p =
16385      DECL_CONSTRUCTOR_P (current_function_decl)
16386      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16387   tree last = NULL;
16388
16389   /* Begin the function body.  */
16390   body = begin_function_body ();
16391   /* Parse the optional ctor-initializer.  */
16392   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16393
16394   /* If we're parsing a constexpr constructor definition, we need
16395      to check that the constructor body is indeed empty.  However,
16396      before we get to cp_parser_function_body lot of junk has been
16397      generated, so we can't just check that we have an empty block.
16398      Rather we take a snapshot of the outermost block, and check whether
16399      cp_parser_function_body changed its state.  */
16400   if (check_body_p)
16401     {
16402       list = body;
16403       if (TREE_CODE (list) == BIND_EXPR)
16404         list = BIND_EXPR_BODY (list);
16405       if (TREE_CODE (list) == STATEMENT_LIST
16406           && STATEMENT_LIST_TAIL (list) != NULL)
16407         last = STATEMENT_LIST_TAIL (list)->stmt;
16408     }
16409   /* Parse the function-body.  */
16410   cp_parser_function_body (parser);
16411   if (check_body_p)
16412     check_constexpr_ctor_body (last, list);
16413   /* Finish the function body.  */
16414   finish_function_body (body);
16415
16416   return ctor_initializer_p;
16417 }
16418
16419 /* Parse an initializer.
16420
16421    initializer:
16422      = initializer-clause
16423      ( expression-list )
16424
16425    Returns an expression representing the initializer.  If no
16426    initializer is present, NULL_TREE is returned.
16427
16428    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16429    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16430    set to TRUE if there is no initializer present.  If there is an
16431    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16432    is set to true; otherwise it is set to false.  */
16433
16434 static tree
16435 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16436                        bool* non_constant_p)
16437 {
16438   cp_token *token;
16439   tree init;
16440
16441   /* Peek at the next token.  */
16442   token = cp_lexer_peek_token (parser->lexer);
16443
16444   /* Let our caller know whether or not this initializer was
16445      parenthesized.  */
16446   *is_direct_init = (token->type != CPP_EQ);
16447   /* Assume that the initializer is constant.  */
16448   *non_constant_p = false;
16449
16450   if (token->type == CPP_EQ)
16451     {
16452       /* Consume the `='.  */
16453       cp_lexer_consume_token (parser->lexer);
16454       /* Parse the initializer-clause.  */
16455       init = cp_parser_initializer_clause (parser, non_constant_p);
16456     }
16457   else if (token->type == CPP_OPEN_PAREN)
16458     {
16459       VEC(tree,gc) *vec;
16460       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16461                                                      /*cast_p=*/false,
16462                                                      /*allow_expansion_p=*/true,
16463                                                      non_constant_p);
16464       if (vec == NULL)
16465         return error_mark_node;
16466       init = build_tree_list_vec (vec);
16467       release_tree_vector (vec);
16468     }
16469   else if (token->type == CPP_OPEN_BRACE)
16470     {
16471       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16472       init = cp_parser_braced_list (parser, non_constant_p);
16473       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16474     }
16475   else
16476     {
16477       /* Anything else is an error.  */
16478       cp_parser_error (parser, "expected initializer");
16479       init = error_mark_node;
16480     }
16481
16482   return init;
16483 }
16484
16485 /* Parse an initializer-clause.
16486
16487    initializer-clause:
16488      assignment-expression
16489      braced-init-list
16490
16491    Returns an expression representing the initializer.
16492
16493    If the `assignment-expression' production is used the value
16494    returned is simply a representation for the expression.
16495
16496    Otherwise, calls cp_parser_braced_list.  */
16497
16498 static tree
16499 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16500 {
16501   tree initializer;
16502
16503   /* Assume the expression is constant.  */
16504   *non_constant_p = false;
16505
16506   /* If it is not a `{', then we are looking at an
16507      assignment-expression.  */
16508   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16509     {
16510       initializer
16511         = cp_parser_constant_expression (parser,
16512                                         /*allow_non_constant_p=*/true,
16513                                         non_constant_p);
16514       if (!*non_constant_p)
16515         {
16516           /* We only want to fold if this is really a constant
16517              expression.  FIXME Actually, we don't want to fold here, but in
16518              cp_finish_decl.  */
16519           tree folded = fold_non_dependent_expr (initializer);
16520           folded = maybe_constant_value (folded);
16521           if (TREE_CONSTANT (folded))
16522             initializer = folded;
16523         }
16524     }
16525   else
16526     initializer = cp_parser_braced_list (parser, non_constant_p);
16527
16528   return initializer;
16529 }
16530
16531 /* Parse a brace-enclosed initializer list.
16532
16533    braced-init-list:
16534      { initializer-list , [opt] }
16535      { }
16536
16537    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16538    the elements of the initializer-list (or NULL, if the last
16539    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16540    NULL_TREE.  There is no way to detect whether or not the optional
16541    trailing `,' was provided.  NON_CONSTANT_P is as for
16542    cp_parser_initializer.  */     
16543
16544 static tree
16545 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16546 {
16547   tree initializer;
16548
16549   /* Consume the `{' token.  */
16550   cp_lexer_consume_token (parser->lexer);
16551   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16552   initializer = make_node (CONSTRUCTOR);
16553   /* If it's not a `}', then there is a non-trivial initializer.  */
16554   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16555     {
16556       /* Parse the initializer list.  */
16557       CONSTRUCTOR_ELTS (initializer)
16558         = cp_parser_initializer_list (parser, non_constant_p);
16559       /* A trailing `,' token is allowed.  */
16560       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16561         cp_lexer_consume_token (parser->lexer);
16562     }
16563   /* Now, there should be a trailing `}'.  */
16564   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16565   TREE_TYPE (initializer) = init_list_type_node;
16566   return initializer;
16567 }
16568
16569 /* Parse an initializer-list.
16570
16571    initializer-list:
16572      initializer-clause ... [opt]
16573      initializer-list , initializer-clause ... [opt]
16574
16575    GNU Extension:
16576
16577    initializer-list:
16578      identifier : initializer-clause
16579      initializer-list, identifier : initializer-clause
16580
16581    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16582    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16583    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16584    as for cp_parser_initializer.  */
16585
16586 static VEC(constructor_elt,gc) *
16587 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16588 {
16589   VEC(constructor_elt,gc) *v = NULL;
16590
16591   /* Assume all of the expressions are constant.  */
16592   *non_constant_p = false;
16593
16594   /* Parse the rest of the list.  */
16595   while (true)
16596     {
16597       cp_token *token;
16598       tree identifier;
16599       tree initializer;
16600       bool clause_non_constant_p;
16601
16602       /* If the next token is an identifier and the following one is a
16603          colon, we are looking at the GNU designated-initializer
16604          syntax.  */
16605       if (cp_parser_allow_gnu_extensions_p (parser)
16606           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16607           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16608         {
16609           /* Warn the user that they are using an extension.  */
16610           pedwarn (input_location, OPT_pedantic, 
16611                    "ISO C++ does not allow designated initializers");
16612           /* Consume the identifier.  */
16613           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16614           /* Consume the `:'.  */
16615           cp_lexer_consume_token (parser->lexer);
16616         }
16617       else
16618         identifier = NULL_TREE;
16619
16620       /* Parse the initializer.  */
16621       initializer = cp_parser_initializer_clause (parser,
16622                                                   &clause_non_constant_p);
16623       /* If any clause is non-constant, so is the entire initializer.  */
16624       if (clause_non_constant_p)
16625         *non_constant_p = true;
16626
16627       /* If we have an ellipsis, this is an initializer pack
16628          expansion.  */
16629       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16630         {
16631           /* Consume the `...'.  */
16632           cp_lexer_consume_token (parser->lexer);
16633
16634           /* Turn the initializer into an initializer expansion.  */
16635           initializer = make_pack_expansion (initializer);
16636         }
16637
16638       /* Add it to the vector.  */
16639       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16640
16641       /* If the next token is not a comma, we have reached the end of
16642          the list.  */
16643       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16644         break;
16645
16646       /* Peek at the next token.  */
16647       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16648       /* If the next token is a `}', then we're still done.  An
16649          initializer-clause can have a trailing `,' after the
16650          initializer-list and before the closing `}'.  */
16651       if (token->type == CPP_CLOSE_BRACE)
16652         break;
16653
16654       /* Consume the `,' token.  */
16655       cp_lexer_consume_token (parser->lexer);
16656     }
16657
16658   return v;
16659 }
16660
16661 /* Classes [gram.class] */
16662
16663 /* Parse a class-name.
16664
16665    class-name:
16666      identifier
16667      template-id
16668
16669    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16670    to indicate that names looked up in dependent types should be
16671    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16672    keyword has been used to indicate that the name that appears next
16673    is a template.  TAG_TYPE indicates the explicit tag given before
16674    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16675    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16676    is the class being defined in a class-head.
16677
16678    Returns the TYPE_DECL representing the class.  */
16679
16680 static tree
16681 cp_parser_class_name (cp_parser *parser,
16682                       bool typename_keyword_p,
16683                       bool template_keyword_p,
16684                       enum tag_types tag_type,
16685                       bool check_dependency_p,
16686                       bool class_head_p,
16687                       bool is_declaration)
16688 {
16689   tree decl;
16690   tree scope;
16691   bool typename_p;
16692   cp_token *token;
16693   tree identifier = NULL_TREE;
16694
16695   /* All class-names start with an identifier.  */
16696   token = cp_lexer_peek_token (parser->lexer);
16697   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16698     {
16699       cp_parser_error (parser, "expected class-name");
16700       return error_mark_node;
16701     }
16702
16703   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16704      to a template-id, so we save it here.  */
16705   scope = parser->scope;
16706   if (scope == error_mark_node)
16707     return error_mark_node;
16708
16709   /* Any name names a type if we're following the `typename' keyword
16710      in a qualified name where the enclosing scope is type-dependent.  */
16711   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16712                 && dependent_type_p (scope));
16713   /* Handle the common case (an identifier, but not a template-id)
16714      efficiently.  */
16715   if (token->type == CPP_NAME
16716       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16717     {
16718       cp_token *identifier_token;
16719       bool ambiguous_p;
16720
16721       /* Look for the identifier.  */
16722       identifier_token = cp_lexer_peek_token (parser->lexer);
16723       ambiguous_p = identifier_token->ambiguous_p;
16724       identifier = cp_parser_identifier (parser);
16725       /* If the next token isn't an identifier, we are certainly not
16726          looking at a class-name.  */
16727       if (identifier == error_mark_node)
16728         decl = error_mark_node;
16729       /* If we know this is a type-name, there's no need to look it
16730          up.  */
16731       else if (typename_p)
16732         decl = identifier;
16733       else
16734         {
16735           tree ambiguous_decls;
16736           /* If we already know that this lookup is ambiguous, then
16737              we've already issued an error message; there's no reason
16738              to check again.  */
16739           if (ambiguous_p)
16740             {
16741               cp_parser_simulate_error (parser);
16742               return error_mark_node;
16743             }
16744           /* If the next token is a `::', then the name must be a type
16745              name.
16746
16747              [basic.lookup.qual]
16748
16749              During the lookup for a name preceding the :: scope
16750              resolution operator, object, function, and enumerator
16751              names are ignored.  */
16752           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16753             tag_type = typename_type;
16754           /* Look up the name.  */
16755           decl = cp_parser_lookup_name (parser, identifier,
16756                                         tag_type,
16757                                         /*is_template=*/false,
16758                                         /*is_namespace=*/false,
16759                                         check_dependency_p,
16760                                         &ambiguous_decls,
16761                                         identifier_token->location);
16762           if (ambiguous_decls)
16763             {
16764               if (cp_parser_parsing_tentatively (parser))
16765                 cp_parser_simulate_error (parser);
16766               return error_mark_node;
16767             }
16768         }
16769     }
16770   else
16771     {
16772       /* Try a template-id.  */
16773       decl = cp_parser_template_id (parser, template_keyword_p,
16774                                     check_dependency_p,
16775                                     is_declaration);
16776       if (decl == error_mark_node)
16777         return error_mark_node;
16778     }
16779
16780   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16781
16782   /* If this is a typename, create a TYPENAME_TYPE.  */
16783   if (typename_p && decl != error_mark_node)
16784     {
16785       decl = make_typename_type (scope, decl, typename_type,
16786                                  /*complain=*/tf_error);
16787       if (decl != error_mark_node)
16788         decl = TYPE_NAME (decl);
16789     }
16790
16791   /* Check to see that it is really the name of a class.  */
16792   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16793       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16794       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16795     /* Situations like this:
16796
16797          template <typename T> struct A {
16798            typename T::template X<int>::I i;
16799          };
16800
16801        are problematic.  Is `T::template X<int>' a class-name?  The
16802        standard does not seem to be definitive, but there is no other
16803        valid interpretation of the following `::'.  Therefore, those
16804        names are considered class-names.  */
16805     {
16806       decl = make_typename_type (scope, decl, tag_type, tf_error);
16807       if (decl != error_mark_node)
16808         decl = TYPE_NAME (decl);
16809     }
16810   else if (TREE_CODE (decl) != TYPE_DECL
16811            || TREE_TYPE (decl) == error_mark_node
16812            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16813            /* In Objective-C 2.0, a classname followed by '.' starts a
16814               dot-syntax expression, and it's not a type-name.  */
16815            || (c_dialect_objc ()
16816                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16817                && objc_is_class_name (decl)))
16818     decl = error_mark_node;
16819
16820   if (decl == error_mark_node)
16821     cp_parser_error (parser, "expected class-name");
16822   else if (identifier && !parser->scope)
16823     maybe_note_name_used_in_class (identifier, decl);
16824
16825   return decl;
16826 }
16827
16828 /* Parse a class-specifier.
16829
16830    class-specifier:
16831      class-head { member-specification [opt] }
16832
16833    Returns the TREE_TYPE representing the class.  */
16834
16835 static tree
16836 cp_parser_class_specifier_1 (cp_parser* parser)
16837 {
16838   tree type;
16839   tree attributes = NULL_TREE;
16840   bool nested_name_specifier_p;
16841   unsigned saved_num_template_parameter_lists;
16842   bool saved_in_function_body;
16843   bool saved_in_unbraced_linkage_specification_p;
16844   tree old_scope = NULL_TREE;
16845   tree scope = NULL_TREE;
16846   tree bases;
16847   cp_token *closing_brace;
16848
16849   push_deferring_access_checks (dk_no_deferred);
16850
16851   /* Parse the class-head.  */
16852   type = cp_parser_class_head (parser,
16853                                &nested_name_specifier_p,
16854                                &attributes,
16855                                &bases);
16856   /* If the class-head was a semantic disaster, skip the entire body
16857      of the class.  */
16858   if (!type)
16859     {
16860       cp_parser_skip_to_end_of_block_or_statement (parser);
16861       pop_deferring_access_checks ();
16862       return error_mark_node;
16863     }
16864
16865   /* Look for the `{'.  */
16866   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16867     {
16868       pop_deferring_access_checks ();
16869       return error_mark_node;
16870     }
16871
16872   /* Process the base classes. If they're invalid, skip the 
16873      entire class body.  */
16874   if (!xref_basetypes (type, bases))
16875     {
16876       /* Consuming the closing brace yields better error messages
16877          later on.  */
16878       if (cp_parser_skip_to_closing_brace (parser))
16879         cp_lexer_consume_token (parser->lexer);
16880       pop_deferring_access_checks ();
16881       return error_mark_node;
16882     }
16883
16884   /* Issue an error message if type-definitions are forbidden here.  */
16885   cp_parser_check_type_definition (parser);
16886   /* Remember that we are defining one more class.  */
16887   ++parser->num_classes_being_defined;
16888   /* Inside the class, surrounding template-parameter-lists do not
16889      apply.  */
16890   saved_num_template_parameter_lists
16891     = parser->num_template_parameter_lists;
16892   parser->num_template_parameter_lists = 0;
16893   /* We are not in a function body.  */
16894   saved_in_function_body = parser->in_function_body;
16895   parser->in_function_body = false;
16896   /* We are not immediately inside an extern "lang" block.  */
16897   saved_in_unbraced_linkage_specification_p
16898     = parser->in_unbraced_linkage_specification_p;
16899   parser->in_unbraced_linkage_specification_p = false;
16900
16901   /* Start the class.  */
16902   if (nested_name_specifier_p)
16903     {
16904       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16905       old_scope = push_inner_scope (scope);
16906     }
16907   type = begin_class_definition (type, attributes);
16908
16909   if (type == error_mark_node)
16910     /* If the type is erroneous, skip the entire body of the class.  */
16911     cp_parser_skip_to_closing_brace (parser);
16912   else
16913     /* Parse the member-specification.  */
16914     cp_parser_member_specification_opt (parser);
16915
16916   /* Look for the trailing `}'.  */
16917   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16918   /* Look for trailing attributes to apply to this class.  */
16919   if (cp_parser_allow_gnu_extensions_p (parser))
16920     attributes = cp_parser_attributes_opt (parser);
16921   if (type != error_mark_node)
16922     type = finish_struct (type, attributes);
16923   if (nested_name_specifier_p)
16924     pop_inner_scope (old_scope, scope);
16925
16926   /* We've finished a type definition.  Check for the common syntax
16927      error of forgetting a semicolon after the definition.  We need to
16928      be careful, as we can't just check for not-a-semicolon and be done
16929      with it; the user might have typed:
16930
16931      class X { } c = ...;
16932      class X { } *p = ...;
16933
16934      and so forth.  Instead, enumerate all the possible tokens that
16935      might follow this production; if we don't see one of them, then
16936      complain and silently insert the semicolon.  */
16937   {
16938     cp_token *token = cp_lexer_peek_token (parser->lexer);
16939     bool want_semicolon = true;
16940
16941     switch (token->type)
16942       {
16943       case CPP_NAME:
16944       case CPP_SEMICOLON:
16945       case CPP_MULT:
16946       case CPP_AND:
16947       case CPP_OPEN_PAREN:
16948       case CPP_CLOSE_PAREN:
16949       case CPP_COMMA:
16950         want_semicolon = false;
16951         break;
16952
16953         /* While it's legal for type qualifiers and storage class
16954            specifiers to follow type definitions in the grammar, only
16955            compiler testsuites contain code like that.  Assume that if
16956            we see such code, then what we're really seeing is a case
16957            like:
16958
16959            class X { }
16960            const <type> var = ...;
16961
16962            or
16963
16964            class Y { }
16965            static <type> func (...) ...
16966
16967            i.e. the qualifier or specifier applies to the next
16968            declaration.  To do so, however, we need to look ahead one
16969            more token to see if *that* token is a type specifier.
16970
16971            This code could be improved to handle:
16972
16973            class Z { }
16974            static const <type> var = ...;  */
16975       case CPP_KEYWORD:
16976         if (keyword_is_decl_specifier (token->keyword))
16977           {
16978             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16979
16980             /* Handling user-defined types here would be nice, but very
16981                tricky.  */
16982             want_semicolon
16983               = (lookahead->type == CPP_KEYWORD
16984                  && keyword_begins_type_specifier (lookahead->keyword));
16985           }
16986         break;
16987       default:
16988         break;
16989       }
16990
16991     /* If we don't have a type, then something is very wrong and we
16992        shouldn't try to do anything clever.  Likewise for not seeing the
16993        closing brace.  */
16994     if (closing_brace && TYPE_P (type) && want_semicolon)
16995       {
16996         cp_token_position prev
16997           = cp_lexer_previous_token_position (parser->lexer);
16998         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16999         location_t loc = prev_token->location;
17000
17001         if (CLASSTYPE_DECLARED_CLASS (type))
17002           error_at (loc, "expected %<;%> after class definition");
17003         else if (TREE_CODE (type) == RECORD_TYPE)
17004           error_at (loc, "expected %<;%> after struct definition");
17005         else if (TREE_CODE (type) == UNION_TYPE)
17006           error_at (loc, "expected %<;%> after union definition");
17007         else
17008           gcc_unreachable ();
17009
17010         /* Unget one token and smash it to look as though we encountered
17011            a semicolon in the input stream.  */
17012         cp_lexer_set_token_position (parser->lexer, prev);
17013         token = cp_lexer_peek_token (parser->lexer);
17014         token->type = CPP_SEMICOLON;
17015         token->keyword = RID_MAX;
17016       }
17017   }
17018
17019   /* If this class is not itself within the scope of another class,
17020      then we need to parse the bodies of all of the queued function
17021      definitions.  Note that the queued functions defined in a class
17022      are not always processed immediately following the
17023      class-specifier for that class.  Consider:
17024
17025        struct A {
17026          struct B { void f() { sizeof (A); } };
17027        };
17028
17029      If `f' were processed before the processing of `A' were
17030      completed, there would be no way to compute the size of `A'.
17031      Note that the nesting we are interested in here is lexical --
17032      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17033      for:
17034
17035        struct A { struct B; };
17036        struct A::B { void f() { } };
17037
17038      there is no need to delay the parsing of `A::B::f'.  */
17039   if (--parser->num_classes_being_defined == 0)
17040     {
17041       tree fn;
17042       tree class_type = NULL_TREE;
17043       tree pushed_scope = NULL_TREE;
17044       unsigned ix;
17045       cp_default_arg_entry *e;
17046
17047       /* In a first pass, parse default arguments to the functions.
17048          Then, in a second pass, parse the bodies of the functions.
17049          This two-phased approach handles cases like:
17050
17051             struct S {
17052               void f() { g(); }
17053               void g(int i = 3);
17054             };
17055
17056          */
17057       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17058                         ix, e)
17059         {
17060           fn = e->decl;
17061           /* If there are default arguments that have not yet been processed,
17062              take care of them now.  */
17063           if (class_type != e->class_type)
17064             {
17065               if (pushed_scope)
17066                 pop_scope (pushed_scope);
17067               class_type = e->class_type;
17068               pushed_scope = push_scope (class_type);
17069             }
17070           /* Make sure that any template parameters are in scope.  */
17071           maybe_begin_member_template_processing (fn);
17072           /* Parse the default argument expressions.  */
17073           cp_parser_late_parsing_default_args (parser, fn);
17074           /* Remove any template parameters from the symbol table.  */
17075           maybe_end_member_template_processing ();
17076         }
17077       if (pushed_scope)
17078         pop_scope (pushed_scope);
17079       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17080       /* Now parse the body of the functions.  */
17081       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17082         cp_parser_late_parsing_for_member (parser, fn);
17083       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17084     }
17085
17086   /* Put back any saved access checks.  */
17087   pop_deferring_access_checks ();
17088
17089   /* Restore saved state.  */
17090   parser->in_function_body = saved_in_function_body;
17091   parser->num_template_parameter_lists
17092     = saved_num_template_parameter_lists;
17093   parser->in_unbraced_linkage_specification_p
17094     = saved_in_unbraced_linkage_specification_p;
17095
17096   return type;
17097 }
17098
17099 static tree
17100 cp_parser_class_specifier (cp_parser* parser)
17101 {
17102   tree ret;
17103   timevar_push (TV_PARSE_STRUCT);
17104   ret = cp_parser_class_specifier_1 (parser);
17105   timevar_pop (TV_PARSE_STRUCT);
17106   return ret;
17107 }
17108
17109 /* Parse a class-head.
17110
17111    class-head:
17112      class-key identifier [opt] base-clause [opt]
17113      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17114      class-key nested-name-specifier [opt] template-id
17115        base-clause [opt]
17116
17117    class-virt-specifier:
17118      final
17119
17120    GNU Extensions:
17121      class-key attributes identifier [opt] base-clause [opt]
17122      class-key attributes nested-name-specifier identifier base-clause [opt]
17123      class-key attributes nested-name-specifier [opt] template-id
17124        base-clause [opt]
17125
17126    Upon return BASES is initialized to the list of base classes (or
17127    NULL, if there are none) in the same form returned by
17128    cp_parser_base_clause.
17129
17130    Returns the TYPE of the indicated class.  Sets
17131    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17132    involving a nested-name-specifier was used, and FALSE otherwise.
17133
17134    Returns error_mark_node if this is not a class-head.
17135
17136    Returns NULL_TREE if the class-head is syntactically valid, but
17137    semantically invalid in a way that means we should skip the entire
17138    body of the class.  */
17139
17140 static tree
17141 cp_parser_class_head (cp_parser* parser,
17142                       bool* nested_name_specifier_p,
17143                       tree *attributes_p,
17144                       tree *bases)
17145 {
17146   tree nested_name_specifier;
17147   enum tag_types class_key;
17148   tree id = NULL_TREE;
17149   tree type = NULL_TREE;
17150   tree attributes;
17151   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17152   bool template_id_p = false;
17153   bool qualified_p = false;
17154   bool invalid_nested_name_p = false;
17155   bool invalid_explicit_specialization_p = false;
17156   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17157   tree pushed_scope = NULL_TREE;
17158   unsigned num_templates;
17159   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17160   /* Assume no nested-name-specifier will be present.  */
17161   *nested_name_specifier_p = false;
17162   /* Assume no template parameter lists will be used in defining the
17163      type.  */
17164   num_templates = 0;
17165   parser->colon_corrects_to_scope_p = false;
17166
17167   *bases = NULL_TREE;
17168
17169   /* Look for the class-key.  */
17170   class_key = cp_parser_class_key (parser);
17171   if (class_key == none_type)
17172     return error_mark_node;
17173
17174   /* Parse the attributes.  */
17175   attributes = cp_parser_attributes_opt (parser);
17176
17177   /* If the next token is `::', that is invalid -- but sometimes
17178      people do try to write:
17179
17180        struct ::S {};
17181
17182      Handle this gracefully by accepting the extra qualifier, and then
17183      issuing an error about it later if this really is a
17184      class-head.  If it turns out just to be an elaborated type
17185      specifier, remain silent.  */
17186   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17187     qualified_p = true;
17188
17189   push_deferring_access_checks (dk_no_check);
17190
17191   /* Determine the name of the class.  Begin by looking for an
17192      optional nested-name-specifier.  */
17193   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17194   nested_name_specifier
17195     = cp_parser_nested_name_specifier_opt (parser,
17196                                            /*typename_keyword_p=*/false,
17197                                            /*check_dependency_p=*/false,
17198                                            /*type_p=*/false,
17199                                            /*is_declaration=*/false);
17200   /* If there was a nested-name-specifier, then there *must* be an
17201      identifier.  */
17202   if (nested_name_specifier)
17203     {
17204       type_start_token = cp_lexer_peek_token (parser->lexer);
17205       /* Although the grammar says `identifier', it really means
17206          `class-name' or `template-name'.  You are only allowed to
17207          define a class that has already been declared with this
17208          syntax.
17209
17210          The proposed resolution for Core Issue 180 says that wherever
17211          you see `class T::X' you should treat `X' as a type-name.
17212
17213          It is OK to define an inaccessible class; for example:
17214
17215            class A { class B; };
17216            class A::B {};
17217
17218          We do not know if we will see a class-name, or a
17219          template-name.  We look for a class-name first, in case the
17220          class-name is a template-id; if we looked for the
17221          template-name first we would stop after the template-name.  */
17222       cp_parser_parse_tentatively (parser);
17223       type = cp_parser_class_name (parser,
17224                                    /*typename_keyword_p=*/false,
17225                                    /*template_keyword_p=*/false,
17226                                    class_type,
17227                                    /*check_dependency_p=*/false,
17228                                    /*class_head_p=*/true,
17229                                    /*is_declaration=*/false);
17230       /* If that didn't work, ignore the nested-name-specifier.  */
17231       if (!cp_parser_parse_definitely (parser))
17232         {
17233           invalid_nested_name_p = true;
17234           type_start_token = cp_lexer_peek_token (parser->lexer);
17235           id = cp_parser_identifier (parser);
17236           if (id == error_mark_node)
17237             id = NULL_TREE;
17238         }
17239       /* If we could not find a corresponding TYPE, treat this
17240          declaration like an unqualified declaration.  */
17241       if (type == error_mark_node)
17242         nested_name_specifier = NULL_TREE;
17243       /* Otherwise, count the number of templates used in TYPE and its
17244          containing scopes.  */
17245       else
17246         {
17247           tree scope;
17248
17249           for (scope = TREE_TYPE (type);
17250                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17251                scope = (TYPE_P (scope)
17252                         ? TYPE_CONTEXT (scope)
17253                         : DECL_CONTEXT (scope)))
17254             if (TYPE_P (scope)
17255                 && CLASS_TYPE_P (scope)
17256                 && CLASSTYPE_TEMPLATE_INFO (scope)
17257                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17258                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17259               ++num_templates;
17260         }
17261     }
17262   /* Otherwise, the identifier is optional.  */
17263   else
17264     {
17265       /* We don't know whether what comes next is a template-id,
17266          an identifier, or nothing at all.  */
17267       cp_parser_parse_tentatively (parser);
17268       /* Check for a template-id.  */
17269       type_start_token = cp_lexer_peek_token (parser->lexer);
17270       id = cp_parser_template_id (parser,
17271                                   /*template_keyword_p=*/false,
17272                                   /*check_dependency_p=*/true,
17273                                   /*is_declaration=*/true);
17274       /* If that didn't work, it could still be an identifier.  */
17275       if (!cp_parser_parse_definitely (parser))
17276         {
17277           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17278             {
17279               type_start_token = cp_lexer_peek_token (parser->lexer);
17280               id = cp_parser_identifier (parser);
17281             }
17282           else
17283             id = NULL_TREE;
17284         }
17285       else
17286         {
17287           template_id_p = true;
17288           ++num_templates;
17289         }
17290     }
17291
17292   pop_deferring_access_checks ();
17293
17294   if (id)
17295     {
17296       cp_parser_check_for_invalid_template_id (parser, id,
17297                                                type_start_token->location);
17298       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17299     }
17300
17301   /* If it's not a `:' or a `{' then we can't really be looking at a
17302      class-head, since a class-head only appears as part of a
17303      class-specifier.  We have to detect this situation before calling
17304      xref_tag, since that has irreversible side-effects.  */
17305   if (!cp_parser_next_token_starts_class_definition_p (parser))
17306     {
17307       cp_parser_error (parser, "expected %<{%> or %<:%>");
17308       type = error_mark_node;
17309       goto out;
17310     }
17311
17312   /* At this point, we're going ahead with the class-specifier, even
17313      if some other problem occurs.  */
17314   cp_parser_commit_to_tentative_parse (parser);
17315   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17316     {
17317       cp_parser_error (parser,
17318                        "cannot specify %<override%> for a class");
17319       type = error_mark_node;
17320       goto out;
17321     }
17322   /* Issue the error about the overly-qualified name now.  */
17323   if (qualified_p)
17324     {
17325       cp_parser_error (parser,
17326                        "global qualification of class name is invalid");
17327       type = error_mark_node;
17328       goto out;
17329     }
17330   else if (invalid_nested_name_p)
17331     {
17332       cp_parser_error (parser,
17333                        "qualified name does not name a class");
17334       type = error_mark_node;
17335       goto out;
17336     }
17337   else if (nested_name_specifier)
17338     {
17339       tree scope;
17340
17341       /* Reject typedef-names in class heads.  */
17342       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17343         {
17344           error_at (type_start_token->location,
17345                     "invalid class name in declaration of %qD",
17346                     type);
17347           type = NULL_TREE;
17348           goto done;
17349         }
17350
17351       /* Figure out in what scope the declaration is being placed.  */
17352       scope = current_scope ();
17353       /* If that scope does not contain the scope in which the
17354          class was originally declared, the program is invalid.  */
17355       if (scope && !is_ancestor (scope, nested_name_specifier))
17356         {
17357           if (at_namespace_scope_p ())
17358             error_at (type_start_token->location,
17359                       "declaration of %qD in namespace %qD which does not "
17360                       "enclose %qD",
17361                       type, scope, nested_name_specifier);
17362           else
17363             error_at (type_start_token->location,
17364                       "declaration of %qD in %qD which does not enclose %qD",
17365                       type, scope, nested_name_specifier);
17366           type = NULL_TREE;
17367           goto done;
17368         }
17369       /* [dcl.meaning]
17370
17371          A declarator-id shall not be qualified except for the
17372          definition of a ... nested class outside of its class
17373          ... [or] the definition or explicit instantiation of a
17374          class member of a namespace outside of its namespace.  */
17375       if (scope == nested_name_specifier)
17376         {
17377           permerror (nested_name_specifier_token_start->location,
17378                      "extra qualification not allowed");
17379           nested_name_specifier = NULL_TREE;
17380           num_templates = 0;
17381         }
17382     }
17383   /* An explicit-specialization must be preceded by "template <>".  If
17384      it is not, try to recover gracefully.  */
17385   if (at_namespace_scope_p ()
17386       && parser->num_template_parameter_lists == 0
17387       && template_id_p)
17388     {
17389       error_at (type_start_token->location,
17390                 "an explicit specialization must be preceded by %<template <>%>");
17391       invalid_explicit_specialization_p = true;
17392       /* Take the same action that would have been taken by
17393          cp_parser_explicit_specialization.  */
17394       ++parser->num_template_parameter_lists;
17395       begin_specialization ();
17396     }
17397   /* There must be no "return" statements between this point and the
17398      end of this function; set "type "to the correct return value and
17399      use "goto done;" to return.  */
17400   /* Make sure that the right number of template parameters were
17401      present.  */
17402   if (!cp_parser_check_template_parameters (parser, num_templates,
17403                                             type_start_token->location,
17404                                             /*declarator=*/NULL))
17405     {
17406       /* If something went wrong, there is no point in even trying to
17407          process the class-definition.  */
17408       type = NULL_TREE;
17409       goto done;
17410     }
17411
17412   /* Look up the type.  */
17413   if (template_id_p)
17414     {
17415       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17416           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17417               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17418         {
17419           error_at (type_start_token->location,
17420                     "function template %qD redeclared as a class template", id);
17421           type = error_mark_node;
17422         }
17423       else
17424         {
17425           type = TREE_TYPE (id);
17426           type = maybe_process_partial_specialization (type);
17427         }
17428       if (nested_name_specifier)
17429         pushed_scope = push_scope (nested_name_specifier);
17430     }
17431   else if (nested_name_specifier)
17432     {
17433       tree class_type;
17434
17435       /* Given:
17436
17437             template <typename T> struct S { struct T };
17438             template <typename T> struct S<T>::T { };
17439
17440          we will get a TYPENAME_TYPE when processing the definition of
17441          `S::T'.  We need to resolve it to the actual type before we
17442          try to define it.  */
17443       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17444         {
17445           class_type = resolve_typename_type (TREE_TYPE (type),
17446                                               /*only_current_p=*/false);
17447           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17448             type = TYPE_NAME (class_type);
17449           else
17450             {
17451               cp_parser_error (parser, "could not resolve typename type");
17452               type = error_mark_node;
17453             }
17454         }
17455
17456       if (maybe_process_partial_specialization (TREE_TYPE (type))
17457           == error_mark_node)
17458         {
17459           type = NULL_TREE;
17460           goto done;
17461         }
17462
17463       class_type = current_class_type;
17464       /* Enter the scope indicated by the nested-name-specifier.  */
17465       pushed_scope = push_scope (nested_name_specifier);
17466       /* Get the canonical version of this type.  */
17467       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17468       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17469           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17470         {
17471           type = push_template_decl (type);
17472           if (type == error_mark_node)
17473             {
17474               type = NULL_TREE;
17475               goto done;
17476             }
17477         }
17478
17479       type = TREE_TYPE (type);
17480       *nested_name_specifier_p = true;
17481     }
17482   else      /* The name is not a nested name.  */
17483     {
17484       /* If the class was unnamed, create a dummy name.  */
17485       if (!id)
17486         id = make_anon_name ();
17487       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17488                        parser->num_template_parameter_lists);
17489     }
17490
17491   /* Indicate whether this class was declared as a `class' or as a
17492      `struct'.  */
17493   if (TREE_CODE (type) == RECORD_TYPE)
17494     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17495   cp_parser_check_class_key (class_key, type);
17496
17497   /* If this type was already complete, and we see another definition,
17498      that's an error.  */
17499   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17500     {
17501       error_at (type_start_token->location, "redefinition of %q#T",
17502                 type);
17503       error_at (type_start_token->location, "previous definition of %q+#T",
17504                 type);
17505       type = NULL_TREE;
17506       goto done;
17507     }
17508   else if (type == error_mark_node)
17509     type = NULL_TREE;
17510
17511   /* We will have entered the scope containing the class; the names of
17512      base classes should be looked up in that context.  For example:
17513
17514        struct A { struct B {}; struct C; };
17515        struct A::C : B {};
17516
17517      is valid.  */
17518
17519   /* Get the list of base-classes, if there is one.  */
17520   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17521     *bases = cp_parser_base_clause (parser);
17522
17523  done:
17524   /* Leave the scope given by the nested-name-specifier.  We will
17525      enter the class scope itself while processing the members.  */
17526   if (pushed_scope)
17527     pop_scope (pushed_scope);
17528
17529   if (invalid_explicit_specialization_p)
17530     {
17531       end_specialization ();
17532       --parser->num_template_parameter_lists;
17533     }
17534
17535   if (type)
17536     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17537   *attributes_p = attributes;
17538   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17539     CLASSTYPE_FINAL (type) = 1;
17540  out:
17541   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17542   return type;
17543 }
17544
17545 /* Parse a class-key.
17546
17547    class-key:
17548      class
17549      struct
17550      union
17551
17552    Returns the kind of class-key specified, or none_type to indicate
17553    error.  */
17554
17555 static enum tag_types
17556 cp_parser_class_key (cp_parser* parser)
17557 {
17558   cp_token *token;
17559   enum tag_types tag_type;
17560
17561   /* Look for the class-key.  */
17562   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17563   if (!token)
17564     return none_type;
17565
17566   /* Check to see if the TOKEN is a class-key.  */
17567   tag_type = cp_parser_token_is_class_key (token);
17568   if (!tag_type)
17569     cp_parser_error (parser, "expected class-key");
17570   return tag_type;
17571 }
17572
17573 /* Parse an (optional) member-specification.
17574
17575    member-specification:
17576      member-declaration member-specification [opt]
17577      access-specifier : member-specification [opt]  */
17578
17579 static void
17580 cp_parser_member_specification_opt (cp_parser* parser)
17581 {
17582   while (true)
17583     {
17584       cp_token *token;
17585       enum rid keyword;
17586
17587       /* Peek at the next token.  */
17588       token = cp_lexer_peek_token (parser->lexer);
17589       /* If it's a `}', or EOF then we've seen all the members.  */
17590       if (token->type == CPP_CLOSE_BRACE
17591           || token->type == CPP_EOF
17592           || token->type == CPP_PRAGMA_EOL)
17593         break;
17594
17595       /* See if this token is a keyword.  */
17596       keyword = token->keyword;
17597       switch (keyword)
17598         {
17599         case RID_PUBLIC:
17600         case RID_PROTECTED:
17601         case RID_PRIVATE:
17602           /* Consume the access-specifier.  */
17603           cp_lexer_consume_token (parser->lexer);
17604           /* Remember which access-specifier is active.  */
17605           current_access_specifier = token->u.value;
17606           /* Look for the `:'.  */
17607           cp_parser_require (parser, CPP_COLON, RT_COLON);
17608           break;
17609
17610         default:
17611           /* Accept #pragmas at class scope.  */
17612           if (token->type == CPP_PRAGMA)
17613             {
17614               cp_parser_pragma (parser, pragma_external);
17615               break;
17616             }
17617
17618           /* Otherwise, the next construction must be a
17619              member-declaration.  */
17620           cp_parser_member_declaration (parser);
17621         }
17622     }
17623 }
17624
17625 /* Parse a member-declaration.
17626
17627    member-declaration:
17628      decl-specifier-seq [opt] member-declarator-list [opt] ;
17629      function-definition ; [opt]
17630      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17631      using-declaration
17632      template-declaration
17633
17634    member-declarator-list:
17635      member-declarator
17636      member-declarator-list , member-declarator
17637
17638    member-declarator:
17639      declarator pure-specifier [opt]
17640      declarator constant-initializer [opt]
17641      identifier [opt] : constant-expression
17642
17643    GNU Extensions:
17644
17645    member-declaration:
17646      __extension__ member-declaration
17647
17648    member-declarator:
17649      declarator attributes [opt] pure-specifier [opt]
17650      declarator attributes [opt] constant-initializer [opt]
17651      identifier [opt] attributes [opt] : constant-expression  
17652
17653    C++0x Extensions:
17654
17655    member-declaration:
17656      static_assert-declaration  */
17657
17658 static void
17659 cp_parser_member_declaration (cp_parser* parser)
17660 {
17661   cp_decl_specifier_seq decl_specifiers;
17662   tree prefix_attributes;
17663   tree decl;
17664   int declares_class_or_enum;
17665   bool friend_p;
17666   cp_token *token = NULL;
17667   cp_token *decl_spec_token_start = NULL;
17668   cp_token *initializer_token_start = NULL;
17669   int saved_pedantic;
17670   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17671
17672   /* Check for the `__extension__' keyword.  */
17673   if (cp_parser_extension_opt (parser, &saved_pedantic))
17674     {
17675       /* Recurse.  */
17676       cp_parser_member_declaration (parser);
17677       /* Restore the old value of the PEDANTIC flag.  */
17678       pedantic = saved_pedantic;
17679
17680       return;
17681     }
17682
17683   /* Check for a template-declaration.  */
17684   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17685     {
17686       /* An explicit specialization here is an error condition, and we
17687          expect the specialization handler to detect and report this.  */
17688       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17689           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17690         cp_parser_explicit_specialization (parser);
17691       else
17692         cp_parser_template_declaration (parser, /*member_p=*/true);
17693
17694       return;
17695     }
17696
17697   /* Check for a using-declaration.  */
17698   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17699     {
17700       /* Parse the using-declaration.  */
17701       cp_parser_using_declaration (parser,
17702                                    /*access_declaration_p=*/false);
17703       return;
17704     }
17705
17706   /* Check for @defs.  */
17707   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17708     {
17709       tree ivar, member;
17710       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17711       ivar = ivar_chains;
17712       while (ivar)
17713         {
17714           member = ivar;
17715           ivar = TREE_CHAIN (member);
17716           TREE_CHAIN (member) = NULL_TREE;
17717           finish_member_declaration (member);
17718         }
17719       return;
17720     }
17721
17722   /* If the next token is `static_assert' we have a static assertion.  */
17723   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17724     {
17725       cp_parser_static_assert (parser, /*member_p=*/true);
17726       return;
17727     }
17728
17729   parser->colon_corrects_to_scope_p = false;
17730
17731   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17732     goto out;
17733
17734   /* Parse the decl-specifier-seq.  */
17735   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17736   cp_parser_decl_specifier_seq (parser,
17737                                 CP_PARSER_FLAGS_OPTIONAL,
17738                                 &decl_specifiers,
17739                                 &declares_class_or_enum);
17740   prefix_attributes = decl_specifiers.attributes;
17741   decl_specifiers.attributes = NULL_TREE;
17742   /* Check for an invalid type-name.  */
17743   if (!decl_specifiers.any_type_specifiers_p
17744       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17745     goto out;
17746   /* If there is no declarator, then the decl-specifier-seq should
17747      specify a type.  */
17748   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17749     {
17750       /* If there was no decl-specifier-seq, and the next token is a
17751          `;', then we have something like:
17752
17753            struct S { ; };
17754
17755          [class.mem]
17756
17757          Each member-declaration shall declare at least one member
17758          name of the class.  */
17759       if (!decl_specifiers.any_specifiers_p)
17760         {
17761           cp_token *token = cp_lexer_peek_token (parser->lexer);
17762           if (!in_system_header_at (token->location))
17763             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17764         }
17765       else
17766         {
17767           tree type;
17768
17769           /* See if this declaration is a friend.  */
17770           friend_p = cp_parser_friend_p (&decl_specifiers);
17771           /* If there were decl-specifiers, check to see if there was
17772              a class-declaration.  */
17773           type = check_tag_decl (&decl_specifiers);
17774           /* Nested classes have already been added to the class, but
17775              a `friend' needs to be explicitly registered.  */
17776           if (friend_p)
17777             {
17778               /* If the `friend' keyword was present, the friend must
17779                  be introduced with a class-key.  */
17780                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17781                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17782                           "in C++03 a class-key must be used "
17783                           "when declaring a friend");
17784                /* In this case:
17785
17786                     template <typename T> struct A {
17787                       friend struct A<T>::B;
17788                     };
17789
17790                   A<T>::B will be represented by a TYPENAME_TYPE, and
17791                   therefore not recognized by check_tag_decl.  */
17792                if (!type)
17793                  {
17794                    type = decl_specifiers.type;
17795                    if (type && TREE_CODE (type) == TYPE_DECL)
17796                      type = TREE_TYPE (type);
17797                  }
17798                if (!type || !TYPE_P (type))
17799                  error_at (decl_spec_token_start->location,
17800                            "friend declaration does not name a class or "
17801                            "function");
17802                else
17803                  make_friend_class (current_class_type, type,
17804                                     /*complain=*/true);
17805             }
17806           /* If there is no TYPE, an error message will already have
17807              been issued.  */
17808           else if (!type || type == error_mark_node)
17809             ;
17810           /* An anonymous aggregate has to be handled specially; such
17811              a declaration really declares a data member (with a
17812              particular type), as opposed to a nested class.  */
17813           else if (ANON_AGGR_TYPE_P (type))
17814             {
17815               /* Remove constructors and such from TYPE, now that we
17816                  know it is an anonymous aggregate.  */
17817               fixup_anonymous_aggr (type);
17818               /* And make the corresponding data member.  */
17819               decl = build_decl (decl_spec_token_start->location,
17820                                  FIELD_DECL, NULL_TREE, type);
17821               /* Add it to the class.  */
17822               finish_member_declaration (decl);
17823             }
17824           else
17825             cp_parser_check_access_in_redeclaration
17826                                               (TYPE_NAME (type),
17827                                                decl_spec_token_start->location);
17828         }
17829     }
17830   else
17831     {
17832       bool assume_semicolon = false;
17833
17834       /* See if these declarations will be friends.  */
17835       friend_p = cp_parser_friend_p (&decl_specifiers);
17836
17837       /* Keep going until we hit the `;' at the end of the
17838          declaration.  */
17839       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17840         {
17841           tree attributes = NULL_TREE;
17842           tree first_attribute;
17843
17844           /* Peek at the next token.  */
17845           token = cp_lexer_peek_token (parser->lexer);
17846
17847           /* Check for a bitfield declaration.  */
17848           if (token->type == CPP_COLON
17849               || (token->type == CPP_NAME
17850                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17851                   == CPP_COLON))
17852             {
17853               tree identifier;
17854               tree width;
17855
17856               /* Get the name of the bitfield.  Note that we cannot just
17857                  check TOKEN here because it may have been invalidated by
17858                  the call to cp_lexer_peek_nth_token above.  */
17859               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17860                 identifier = cp_parser_identifier (parser);
17861               else
17862                 identifier = NULL_TREE;
17863
17864               /* Consume the `:' token.  */
17865               cp_lexer_consume_token (parser->lexer);
17866               /* Get the width of the bitfield.  */
17867               width
17868                 = cp_parser_constant_expression (parser,
17869                                                  /*allow_non_constant=*/false,
17870                                                  NULL);
17871
17872               /* Look for attributes that apply to the bitfield.  */
17873               attributes = cp_parser_attributes_opt (parser);
17874               /* Remember which attributes are prefix attributes and
17875                  which are not.  */
17876               first_attribute = attributes;
17877               /* Combine the attributes.  */
17878               attributes = chainon (prefix_attributes, attributes);
17879
17880               /* Create the bitfield declaration.  */
17881               decl = grokbitfield (identifier
17882                                    ? make_id_declarator (NULL_TREE,
17883                                                          identifier,
17884                                                          sfk_none)
17885                                    : NULL,
17886                                    &decl_specifiers,
17887                                    width,
17888                                    attributes);
17889             }
17890           else
17891             {
17892               cp_declarator *declarator;
17893               tree initializer;
17894               tree asm_specification;
17895               int ctor_dtor_or_conv_p;
17896
17897               /* Parse the declarator.  */
17898               declarator
17899                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17900                                         &ctor_dtor_or_conv_p,
17901                                         /*parenthesized_p=*/NULL,
17902                                         /*member_p=*/true);
17903
17904               /* If something went wrong parsing the declarator, make sure
17905                  that we at least consume some tokens.  */
17906               if (declarator == cp_error_declarator)
17907                 {
17908                   /* Skip to the end of the statement.  */
17909                   cp_parser_skip_to_end_of_statement (parser);
17910                   /* If the next token is not a semicolon, that is
17911                      probably because we just skipped over the body of
17912                      a function.  So, we consume a semicolon if
17913                      present, but do not issue an error message if it
17914                      is not present.  */
17915                   if (cp_lexer_next_token_is (parser->lexer,
17916                                               CPP_SEMICOLON))
17917                     cp_lexer_consume_token (parser->lexer);
17918                   goto out;
17919                 }
17920
17921               if (declares_class_or_enum & 2)
17922                 cp_parser_check_for_definition_in_return_type
17923                                             (declarator, decl_specifiers.type,
17924                                              decl_specifiers.type_location);
17925
17926               /* Look for an asm-specification.  */
17927               asm_specification = cp_parser_asm_specification_opt (parser);
17928               /* Look for attributes that apply to the declaration.  */
17929               attributes = cp_parser_attributes_opt (parser);
17930               /* Remember which attributes are prefix attributes and
17931                  which are not.  */
17932               first_attribute = attributes;
17933               /* Combine the attributes.  */
17934               attributes = chainon (prefix_attributes, attributes);
17935
17936               /* If it's an `=', then we have a constant-initializer or a
17937                  pure-specifier.  It is not correct to parse the
17938                  initializer before registering the member declaration
17939                  since the member declaration should be in scope while
17940                  its initializer is processed.  However, the rest of the
17941                  front end does not yet provide an interface that allows
17942                  us to handle this correctly.  */
17943               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17944                 {
17945                   /* In [class.mem]:
17946
17947                      A pure-specifier shall be used only in the declaration of
17948                      a virtual function.
17949
17950                      A member-declarator can contain a constant-initializer
17951                      only if it declares a static member of integral or
17952                      enumeration type.
17953
17954                      Therefore, if the DECLARATOR is for a function, we look
17955                      for a pure-specifier; otherwise, we look for a
17956                      constant-initializer.  When we call `grokfield', it will
17957                      perform more stringent semantics checks.  */
17958                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17959                   if (function_declarator_p (declarator))
17960                     initializer = cp_parser_pure_specifier (parser);
17961                   else
17962                     /* Parse the initializer.  */
17963                     initializer = cp_parser_constant_initializer (parser);
17964                 }
17965               /* Otherwise, there is no initializer.  */
17966               else
17967                 initializer = NULL_TREE;
17968
17969               /* See if we are probably looking at a function
17970                  definition.  We are certainly not looking at a
17971                  member-declarator.  Calling `grokfield' has
17972                  side-effects, so we must not do it unless we are sure
17973                  that we are looking at a member-declarator.  */
17974               if (cp_parser_token_starts_function_definition_p
17975                   (cp_lexer_peek_token (parser->lexer)))
17976                 {
17977                   /* The grammar does not allow a pure-specifier to be
17978                      used when a member function is defined.  (It is
17979                      possible that this fact is an oversight in the
17980                      standard, since a pure function may be defined
17981                      outside of the class-specifier.  */
17982                   if (initializer)
17983                     error_at (initializer_token_start->location,
17984                               "pure-specifier on function-definition");
17985                   decl = cp_parser_save_member_function_body (parser,
17986                                                               &decl_specifiers,
17987                                                               declarator,
17988                                                               attributes);
17989                   /* If the member was not a friend, declare it here.  */
17990                   if (!friend_p)
17991                     finish_member_declaration (decl);
17992                   /* Peek at the next token.  */
17993                   token = cp_lexer_peek_token (parser->lexer);
17994                   /* If the next token is a semicolon, consume it.  */
17995                   if (token->type == CPP_SEMICOLON)
17996                     cp_lexer_consume_token (parser->lexer);
17997                   goto out;
17998                 }
17999               else
18000                 if (declarator->kind == cdk_function)
18001                   declarator->id_loc = token->location;
18002                 /* Create the declaration.  */
18003                 decl = grokfield (declarator, &decl_specifiers,
18004                                   initializer, /*init_const_expr_p=*/true,
18005                                   asm_specification,
18006                                   attributes);
18007             }
18008
18009           /* Reset PREFIX_ATTRIBUTES.  */
18010           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18011             attributes = TREE_CHAIN (attributes);
18012           if (attributes)
18013             TREE_CHAIN (attributes) = NULL_TREE;
18014
18015           /* If there is any qualification still in effect, clear it
18016              now; we will be starting fresh with the next declarator.  */
18017           parser->scope = NULL_TREE;
18018           parser->qualifying_scope = NULL_TREE;
18019           parser->object_scope = NULL_TREE;
18020           /* If it's a `,', then there are more declarators.  */
18021           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18022             cp_lexer_consume_token (parser->lexer);
18023           /* If the next token isn't a `;', then we have a parse error.  */
18024           else if (cp_lexer_next_token_is_not (parser->lexer,
18025                                                CPP_SEMICOLON))
18026             {
18027               /* The next token might be a ways away from where the
18028                  actual semicolon is missing.  Find the previous token
18029                  and use that for our error position.  */
18030               cp_token *token = cp_lexer_previous_token (parser->lexer);
18031               error_at (token->location,
18032                         "expected %<;%> at end of member declaration");
18033
18034               /* Assume that the user meant to provide a semicolon.  If
18035                  we were to cp_parser_skip_to_end_of_statement, we might
18036                  skip to a semicolon inside a member function definition
18037                  and issue nonsensical error messages.  */
18038               assume_semicolon = true;
18039             }
18040
18041           if (decl)
18042             {
18043               /* Add DECL to the list of members.  */
18044               if (!friend_p)
18045                 finish_member_declaration (decl);
18046
18047               if (TREE_CODE (decl) == FUNCTION_DECL)
18048                 cp_parser_save_default_args (parser, decl);
18049             }
18050
18051           if (assume_semicolon)
18052             goto out;
18053         }
18054     }
18055
18056   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18057  out:
18058   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18059 }
18060
18061 /* Parse a pure-specifier.
18062
18063    pure-specifier:
18064      = 0
18065
18066    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18067    Otherwise, ERROR_MARK_NODE is returned.  */
18068
18069 static tree
18070 cp_parser_pure_specifier (cp_parser* parser)
18071 {
18072   cp_token *token;
18073
18074   /* Look for the `=' token.  */
18075   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18076     return error_mark_node;
18077   /* Look for the `0' token.  */
18078   token = cp_lexer_peek_token (parser->lexer);
18079
18080   if (token->type == CPP_EOF
18081       || token->type == CPP_PRAGMA_EOL)
18082     return error_mark_node;
18083
18084   cp_lexer_consume_token (parser->lexer);
18085
18086   /* Accept = default or = delete in c++0x mode.  */
18087   if (token->keyword == RID_DEFAULT
18088       || token->keyword == RID_DELETE)
18089     {
18090       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18091       return token->u.value;
18092     }
18093
18094   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18095   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18096     {
18097       cp_parser_error (parser,
18098                        "invalid pure specifier (only %<= 0%> is allowed)");
18099       cp_parser_skip_to_end_of_statement (parser);
18100       return error_mark_node;
18101     }
18102   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18103     {
18104       error_at (token->location, "templates may not be %<virtual%>");
18105       return error_mark_node;
18106     }
18107
18108   return integer_zero_node;
18109 }
18110
18111 /* Parse a constant-initializer.
18112
18113    constant-initializer:
18114      = constant-expression
18115
18116    Returns a representation of the constant-expression.  */
18117
18118 static tree
18119 cp_parser_constant_initializer (cp_parser* parser)
18120 {
18121   /* Look for the `=' token.  */
18122   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18123     return error_mark_node;
18124
18125   /* It is invalid to write:
18126
18127        struct S { static const int i = { 7 }; };
18128
18129      */
18130   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18131     {
18132       cp_parser_error (parser,
18133                        "a brace-enclosed initializer is not allowed here");
18134       /* Consume the opening brace.  */
18135       cp_lexer_consume_token (parser->lexer);
18136       /* Skip the initializer.  */
18137       cp_parser_skip_to_closing_brace (parser);
18138       /* Look for the trailing `}'.  */
18139       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18140
18141       return error_mark_node;
18142     }
18143
18144   return cp_parser_constant_expression (parser,
18145                                         /*allow_non_constant=*/false,
18146                                         NULL);
18147 }
18148
18149 /* Derived classes [gram.class.derived] */
18150
18151 /* Parse a base-clause.
18152
18153    base-clause:
18154      : base-specifier-list
18155
18156    base-specifier-list:
18157      base-specifier ... [opt]
18158      base-specifier-list , base-specifier ... [opt]
18159
18160    Returns a TREE_LIST representing the base-classes, in the order in
18161    which they were declared.  The representation of each node is as
18162    described by cp_parser_base_specifier.
18163
18164    In the case that no bases are specified, this function will return
18165    NULL_TREE, not ERROR_MARK_NODE.  */
18166
18167 static tree
18168 cp_parser_base_clause (cp_parser* parser)
18169 {
18170   tree bases = NULL_TREE;
18171
18172   /* Look for the `:' that begins the list.  */
18173   cp_parser_require (parser, CPP_COLON, RT_COLON);
18174
18175   /* Scan the base-specifier-list.  */
18176   while (true)
18177     {
18178       cp_token *token;
18179       tree base;
18180       bool pack_expansion_p = false;
18181
18182       /* Look for the base-specifier.  */
18183       base = cp_parser_base_specifier (parser);
18184       /* Look for the (optional) ellipsis. */
18185       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18186         {
18187           /* Consume the `...'. */
18188           cp_lexer_consume_token (parser->lexer);
18189
18190           pack_expansion_p = true;
18191         }
18192
18193       /* Add BASE to the front of the list.  */
18194       if (base != error_mark_node)
18195         {
18196           if (pack_expansion_p)
18197             /* Make this a pack expansion type. */
18198             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18199           
18200
18201           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18202             {
18203               TREE_CHAIN (base) = bases;
18204               bases = base;
18205             }
18206         }
18207       /* Peek at the next token.  */
18208       token = cp_lexer_peek_token (parser->lexer);
18209       /* If it's not a comma, then the list is complete.  */
18210       if (token->type != CPP_COMMA)
18211         break;
18212       /* Consume the `,'.  */
18213       cp_lexer_consume_token (parser->lexer);
18214     }
18215
18216   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18217      base class had a qualified name.  However, the next name that
18218      appears is certainly not qualified.  */
18219   parser->scope = NULL_TREE;
18220   parser->qualifying_scope = NULL_TREE;
18221   parser->object_scope = NULL_TREE;
18222
18223   return nreverse (bases);
18224 }
18225
18226 /* Parse a base-specifier.
18227
18228    base-specifier:
18229      :: [opt] nested-name-specifier [opt] class-name
18230      virtual access-specifier [opt] :: [opt] nested-name-specifier
18231        [opt] class-name
18232      access-specifier virtual [opt] :: [opt] nested-name-specifier
18233        [opt] class-name
18234
18235    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18236    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18237    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18238    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18239
18240 static tree
18241 cp_parser_base_specifier (cp_parser* parser)
18242 {
18243   cp_token *token;
18244   bool done = false;
18245   bool virtual_p = false;
18246   bool duplicate_virtual_error_issued_p = false;
18247   bool duplicate_access_error_issued_p = false;
18248   bool class_scope_p, template_p;
18249   tree access = access_default_node;
18250   tree type;
18251
18252   /* Process the optional `virtual' and `access-specifier'.  */
18253   while (!done)
18254     {
18255       /* Peek at the next token.  */
18256       token = cp_lexer_peek_token (parser->lexer);
18257       /* Process `virtual'.  */
18258       switch (token->keyword)
18259         {
18260         case RID_VIRTUAL:
18261           /* If `virtual' appears more than once, issue an error.  */
18262           if (virtual_p && !duplicate_virtual_error_issued_p)
18263             {
18264               cp_parser_error (parser,
18265                                "%<virtual%> specified more than once in base-specified");
18266               duplicate_virtual_error_issued_p = true;
18267             }
18268
18269           virtual_p = true;
18270
18271           /* Consume the `virtual' token.  */
18272           cp_lexer_consume_token (parser->lexer);
18273
18274           break;
18275
18276         case RID_PUBLIC:
18277         case RID_PROTECTED:
18278         case RID_PRIVATE:
18279           /* If more than one access specifier appears, issue an
18280              error.  */
18281           if (access != access_default_node
18282               && !duplicate_access_error_issued_p)
18283             {
18284               cp_parser_error (parser,
18285                                "more than one access specifier in base-specified");
18286               duplicate_access_error_issued_p = true;
18287             }
18288
18289           access = ridpointers[(int) token->keyword];
18290
18291           /* Consume the access-specifier.  */
18292           cp_lexer_consume_token (parser->lexer);
18293
18294           break;
18295
18296         default:
18297           done = true;
18298           break;
18299         }
18300     }
18301   /* It is not uncommon to see programs mechanically, erroneously, use
18302      the 'typename' keyword to denote (dependent) qualified types
18303      as base classes.  */
18304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18305     {
18306       token = cp_lexer_peek_token (parser->lexer);
18307       if (!processing_template_decl)
18308         error_at (token->location,
18309                   "keyword %<typename%> not allowed outside of templates");
18310       else
18311         error_at (token->location,
18312                   "keyword %<typename%> not allowed in this context "
18313                   "(the base class is implicitly a type)");
18314       cp_lexer_consume_token (parser->lexer);
18315     }
18316
18317   /* Look for the optional `::' operator.  */
18318   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18319   /* Look for the nested-name-specifier.  The simplest way to
18320      implement:
18321
18322        [temp.res]
18323
18324        The keyword `typename' is not permitted in a base-specifier or
18325        mem-initializer; in these contexts a qualified name that
18326        depends on a template-parameter is implicitly assumed to be a
18327        type name.
18328
18329      is to pretend that we have seen the `typename' keyword at this
18330      point.  */
18331   cp_parser_nested_name_specifier_opt (parser,
18332                                        /*typename_keyword_p=*/true,
18333                                        /*check_dependency_p=*/true,
18334                                        typename_type,
18335                                        /*is_declaration=*/true);
18336   /* If the base class is given by a qualified name, assume that names
18337      we see are type names or templates, as appropriate.  */
18338   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18339   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18340
18341   /* Finally, look for the class-name.  */
18342   type = cp_parser_class_name (parser,
18343                                class_scope_p,
18344                                template_p,
18345                                typename_type,
18346                                /*check_dependency_p=*/true,
18347                                /*class_head_p=*/false,
18348                                /*is_declaration=*/true);
18349
18350   if (type == error_mark_node)
18351     return error_mark_node;
18352
18353   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18354 }
18355
18356 /* Exception handling [gram.exception] */
18357
18358 /* Parse an (optional) exception-specification.
18359
18360    exception-specification:
18361      throw ( type-id-list [opt] )
18362
18363    Returns a TREE_LIST representing the exception-specification.  The
18364    TREE_VALUE of each node is a type.  */
18365
18366 static tree
18367 cp_parser_exception_specification_opt (cp_parser* parser)
18368 {
18369   cp_token *token;
18370   tree type_id_list;
18371   const char *saved_message;
18372
18373   /* Peek at the next token.  */
18374   token = cp_lexer_peek_token (parser->lexer);
18375
18376   /* Is it a noexcept-specification?  */
18377   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18378     {
18379       tree expr;
18380       cp_lexer_consume_token (parser->lexer);
18381
18382       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18383         {
18384           cp_lexer_consume_token (parser->lexer);
18385
18386           /* Types may not be defined in an exception-specification.  */
18387           saved_message = parser->type_definition_forbidden_message;
18388           parser->type_definition_forbidden_message
18389             = G_("types may not be defined in an exception-specification");
18390
18391           expr = cp_parser_constant_expression (parser, false, NULL);
18392
18393           /* Restore the saved message.  */
18394           parser->type_definition_forbidden_message = saved_message;
18395
18396           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18397         }
18398       else
18399         expr = boolean_true_node;
18400
18401       return build_noexcept_spec (expr, tf_warning_or_error);
18402     }
18403
18404   /* If it's not `throw', then there's no exception-specification.  */
18405   if (!cp_parser_is_keyword (token, RID_THROW))
18406     return NULL_TREE;
18407
18408 #if 0
18409   /* Enable this once a lot of code has transitioned to noexcept?  */
18410   if (cxx_dialect == cxx0x && !in_system_header)
18411     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18412              "deprecated in C++0x; use %<noexcept%> instead");
18413 #endif
18414
18415   /* Consume the `throw'.  */
18416   cp_lexer_consume_token (parser->lexer);
18417
18418   /* Look for the `('.  */
18419   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18420
18421   /* Peek at the next token.  */
18422   token = cp_lexer_peek_token (parser->lexer);
18423   /* If it's not a `)', then there is a type-id-list.  */
18424   if (token->type != CPP_CLOSE_PAREN)
18425     {
18426       /* Types may not be defined in an exception-specification.  */
18427       saved_message = parser->type_definition_forbidden_message;
18428       parser->type_definition_forbidden_message
18429         = G_("types may not be defined in an exception-specification");
18430       /* Parse the type-id-list.  */
18431       type_id_list = cp_parser_type_id_list (parser);
18432       /* Restore the saved message.  */
18433       parser->type_definition_forbidden_message = saved_message;
18434     }
18435   else
18436     type_id_list = empty_except_spec;
18437
18438   /* Look for the `)'.  */
18439   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18440
18441   return type_id_list;
18442 }
18443
18444 /* Parse an (optional) type-id-list.
18445
18446    type-id-list:
18447      type-id ... [opt]
18448      type-id-list , type-id ... [opt]
18449
18450    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18451    in the order that the types were presented.  */
18452
18453 static tree
18454 cp_parser_type_id_list (cp_parser* parser)
18455 {
18456   tree types = NULL_TREE;
18457
18458   while (true)
18459     {
18460       cp_token *token;
18461       tree type;
18462
18463       /* Get the next type-id.  */
18464       type = cp_parser_type_id (parser);
18465       /* Parse the optional ellipsis. */
18466       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18467         {
18468           /* Consume the `...'. */
18469           cp_lexer_consume_token (parser->lexer);
18470
18471           /* Turn the type into a pack expansion expression. */
18472           type = make_pack_expansion (type);
18473         }
18474       /* Add it to the list.  */
18475       types = add_exception_specifier (types, type, /*complain=*/1);
18476       /* Peek at the next token.  */
18477       token = cp_lexer_peek_token (parser->lexer);
18478       /* If it is not a `,', we are done.  */
18479       if (token->type != CPP_COMMA)
18480         break;
18481       /* Consume the `,'.  */
18482       cp_lexer_consume_token (parser->lexer);
18483     }
18484
18485   return nreverse (types);
18486 }
18487
18488 /* Parse a try-block.
18489
18490    try-block:
18491      try compound-statement handler-seq  */
18492
18493 static tree
18494 cp_parser_try_block (cp_parser* parser)
18495 {
18496   tree try_block;
18497
18498   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18499   try_block = begin_try_block ();
18500   cp_parser_compound_statement (parser, NULL, true, false);
18501   finish_try_block (try_block);
18502   cp_parser_handler_seq (parser);
18503   finish_handler_sequence (try_block);
18504
18505   return try_block;
18506 }
18507
18508 /* Parse a function-try-block.
18509
18510    function-try-block:
18511      try ctor-initializer [opt] function-body handler-seq  */
18512
18513 static bool
18514 cp_parser_function_try_block (cp_parser* parser)
18515 {
18516   tree compound_stmt;
18517   tree try_block;
18518   bool ctor_initializer_p;
18519
18520   /* Look for the `try' keyword.  */
18521   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18522     return false;
18523   /* Let the rest of the front end know where we are.  */
18524   try_block = begin_function_try_block (&compound_stmt);
18525   /* Parse the function-body.  */
18526   ctor_initializer_p
18527     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18528   /* We're done with the `try' part.  */
18529   finish_function_try_block (try_block);
18530   /* Parse the handlers.  */
18531   cp_parser_handler_seq (parser);
18532   /* We're done with the handlers.  */
18533   finish_function_handler_sequence (try_block, compound_stmt);
18534
18535   return ctor_initializer_p;
18536 }
18537
18538 /* Parse a handler-seq.
18539
18540    handler-seq:
18541      handler handler-seq [opt]  */
18542
18543 static void
18544 cp_parser_handler_seq (cp_parser* parser)
18545 {
18546   while (true)
18547     {
18548       cp_token *token;
18549
18550       /* Parse the handler.  */
18551       cp_parser_handler (parser);
18552       /* Peek at the next token.  */
18553       token = cp_lexer_peek_token (parser->lexer);
18554       /* If it's not `catch' then there are no more handlers.  */
18555       if (!cp_parser_is_keyword (token, RID_CATCH))
18556         break;
18557     }
18558 }
18559
18560 /* Parse a handler.
18561
18562    handler:
18563      catch ( exception-declaration ) compound-statement  */
18564
18565 static void
18566 cp_parser_handler (cp_parser* parser)
18567 {
18568   tree handler;
18569   tree declaration;
18570
18571   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18572   handler = begin_handler ();
18573   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18574   declaration = cp_parser_exception_declaration (parser);
18575   finish_handler_parms (declaration, handler);
18576   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18577   cp_parser_compound_statement (parser, NULL, false, false);
18578   finish_handler (handler);
18579 }
18580
18581 /* Parse an exception-declaration.
18582
18583    exception-declaration:
18584      type-specifier-seq declarator
18585      type-specifier-seq abstract-declarator
18586      type-specifier-seq
18587      ...
18588
18589    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18590    ellipsis variant is used.  */
18591
18592 static tree
18593 cp_parser_exception_declaration (cp_parser* parser)
18594 {
18595   cp_decl_specifier_seq type_specifiers;
18596   cp_declarator *declarator;
18597   const char *saved_message;
18598
18599   /* If it's an ellipsis, it's easy to handle.  */
18600   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18601     {
18602       /* Consume the `...' token.  */
18603       cp_lexer_consume_token (parser->lexer);
18604       return NULL_TREE;
18605     }
18606
18607   /* Types may not be defined in exception-declarations.  */
18608   saved_message = parser->type_definition_forbidden_message;
18609   parser->type_definition_forbidden_message
18610     = G_("types may not be defined in exception-declarations");
18611
18612   /* Parse the type-specifier-seq.  */
18613   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18614                                 /*is_trailing_return=*/false,
18615                                 &type_specifiers);
18616   /* If it's a `)', then there is no declarator.  */
18617   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18618     declarator = NULL;
18619   else
18620     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18621                                        /*ctor_dtor_or_conv_p=*/NULL,
18622                                        /*parenthesized_p=*/NULL,
18623                                        /*member_p=*/false);
18624
18625   /* Restore the saved message.  */
18626   parser->type_definition_forbidden_message = saved_message;
18627
18628   if (!type_specifiers.any_specifiers_p)
18629     return error_mark_node;
18630
18631   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18632 }
18633
18634 /* Parse a throw-expression.
18635
18636    throw-expression:
18637      throw assignment-expression [opt]
18638
18639    Returns a THROW_EXPR representing the throw-expression.  */
18640
18641 static tree
18642 cp_parser_throw_expression (cp_parser* parser)
18643 {
18644   tree expression;
18645   cp_token* token;
18646
18647   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18648   token = cp_lexer_peek_token (parser->lexer);
18649   /* Figure out whether or not there is an assignment-expression
18650      following the "throw" keyword.  */
18651   if (token->type == CPP_COMMA
18652       || token->type == CPP_SEMICOLON
18653       || token->type == CPP_CLOSE_PAREN
18654       || token->type == CPP_CLOSE_SQUARE
18655       || token->type == CPP_CLOSE_BRACE
18656       || token->type == CPP_COLON)
18657     expression = NULL_TREE;
18658   else
18659     expression = cp_parser_assignment_expression (parser,
18660                                                   /*cast_p=*/false, NULL);
18661
18662   return build_throw (expression);
18663 }
18664
18665 /* GNU Extensions */
18666
18667 /* Parse an (optional) asm-specification.
18668
18669    asm-specification:
18670      asm ( string-literal )
18671
18672    If the asm-specification is present, returns a STRING_CST
18673    corresponding to the string-literal.  Otherwise, returns
18674    NULL_TREE.  */
18675
18676 static tree
18677 cp_parser_asm_specification_opt (cp_parser* parser)
18678 {
18679   cp_token *token;
18680   tree asm_specification;
18681
18682   /* Peek at the next token.  */
18683   token = cp_lexer_peek_token (parser->lexer);
18684   /* If the next token isn't the `asm' keyword, then there's no
18685      asm-specification.  */
18686   if (!cp_parser_is_keyword (token, RID_ASM))
18687     return NULL_TREE;
18688
18689   /* Consume the `asm' token.  */
18690   cp_lexer_consume_token (parser->lexer);
18691   /* Look for the `('.  */
18692   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18693
18694   /* Look for the string-literal.  */
18695   asm_specification = cp_parser_string_literal (parser, false, false);
18696
18697   /* Look for the `)'.  */
18698   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18699
18700   return asm_specification;
18701 }
18702
18703 /* Parse an asm-operand-list.
18704
18705    asm-operand-list:
18706      asm-operand
18707      asm-operand-list , asm-operand
18708
18709    asm-operand:
18710      string-literal ( expression )
18711      [ string-literal ] string-literal ( expression )
18712
18713    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18714    each node is the expression.  The TREE_PURPOSE is itself a
18715    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18716    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18717    is a STRING_CST for the string literal before the parenthesis. Returns
18718    ERROR_MARK_NODE if any of the operands are invalid.  */
18719
18720 static tree
18721 cp_parser_asm_operand_list (cp_parser* parser)
18722 {
18723   tree asm_operands = NULL_TREE;
18724   bool invalid_operands = false;
18725
18726   while (true)
18727     {
18728       tree string_literal;
18729       tree expression;
18730       tree name;
18731
18732       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18733         {
18734           /* Consume the `[' token.  */
18735           cp_lexer_consume_token (parser->lexer);
18736           /* Read the operand name.  */
18737           name = cp_parser_identifier (parser);
18738           if (name != error_mark_node)
18739             name = build_string (IDENTIFIER_LENGTH (name),
18740                                  IDENTIFIER_POINTER (name));
18741           /* Look for the closing `]'.  */
18742           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18743         }
18744       else
18745         name = NULL_TREE;
18746       /* Look for the string-literal.  */
18747       string_literal = cp_parser_string_literal (parser, false, false);
18748
18749       /* Look for the `('.  */
18750       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18751       /* Parse the expression.  */
18752       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18753       /* Look for the `)'.  */
18754       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18755
18756       if (name == error_mark_node 
18757           || string_literal == error_mark_node 
18758           || expression == error_mark_node)
18759         invalid_operands = true;
18760
18761       /* Add this operand to the list.  */
18762       asm_operands = tree_cons (build_tree_list (name, string_literal),
18763                                 expression,
18764                                 asm_operands);
18765       /* If the next token is not a `,', there are no more
18766          operands.  */
18767       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18768         break;
18769       /* Consume the `,'.  */
18770       cp_lexer_consume_token (parser->lexer);
18771     }
18772
18773   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18774 }
18775
18776 /* Parse an asm-clobber-list.
18777
18778    asm-clobber-list:
18779      string-literal
18780      asm-clobber-list , string-literal
18781
18782    Returns a TREE_LIST, indicating the clobbers in the order that they
18783    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18784
18785 static tree
18786 cp_parser_asm_clobber_list (cp_parser* parser)
18787 {
18788   tree clobbers = NULL_TREE;
18789
18790   while (true)
18791     {
18792       tree string_literal;
18793
18794       /* Look for the string literal.  */
18795       string_literal = cp_parser_string_literal (parser, false, false);
18796       /* Add it to the list.  */
18797       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18798       /* If the next token is not a `,', then the list is
18799          complete.  */
18800       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18801         break;
18802       /* Consume the `,' token.  */
18803       cp_lexer_consume_token (parser->lexer);
18804     }
18805
18806   return clobbers;
18807 }
18808
18809 /* Parse an asm-label-list.
18810
18811    asm-label-list:
18812      identifier
18813      asm-label-list , identifier
18814
18815    Returns a TREE_LIST, indicating the labels in the order that they
18816    appeared.  The TREE_VALUE of each node is a label.  */
18817
18818 static tree
18819 cp_parser_asm_label_list (cp_parser* parser)
18820 {
18821   tree labels = NULL_TREE;
18822
18823   while (true)
18824     {
18825       tree identifier, label, name;
18826
18827       /* Look for the identifier.  */
18828       identifier = cp_parser_identifier (parser);
18829       if (!error_operand_p (identifier))
18830         {
18831           label = lookup_label (identifier);
18832           if (TREE_CODE (label) == LABEL_DECL)
18833             {
18834               TREE_USED (label) = 1;
18835               check_goto (label);
18836               name = build_string (IDENTIFIER_LENGTH (identifier),
18837                                    IDENTIFIER_POINTER (identifier));
18838               labels = tree_cons (name, label, labels);
18839             }
18840         }
18841       /* If the next token is not a `,', then the list is
18842          complete.  */
18843       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18844         break;
18845       /* Consume the `,' token.  */
18846       cp_lexer_consume_token (parser->lexer);
18847     }
18848
18849   return nreverse (labels);
18850 }
18851
18852 /* Parse an (optional) series of attributes.
18853
18854    attributes:
18855      attributes attribute
18856
18857    attribute:
18858      __attribute__ (( attribute-list [opt] ))
18859
18860    The return value is as for cp_parser_attribute_list.  */
18861
18862 static tree
18863 cp_parser_attributes_opt (cp_parser* parser)
18864 {
18865   tree attributes = NULL_TREE;
18866
18867   while (true)
18868     {
18869       cp_token *token;
18870       tree attribute_list;
18871
18872       /* Peek at the next token.  */
18873       token = cp_lexer_peek_token (parser->lexer);
18874       /* If it's not `__attribute__', then we're done.  */
18875       if (token->keyword != RID_ATTRIBUTE)
18876         break;
18877
18878       /* Consume the `__attribute__' keyword.  */
18879       cp_lexer_consume_token (parser->lexer);
18880       /* Look for the two `(' tokens.  */
18881       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18882       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18883
18884       /* Peek at the next token.  */
18885       token = cp_lexer_peek_token (parser->lexer);
18886       if (token->type != CPP_CLOSE_PAREN)
18887         /* Parse the attribute-list.  */
18888         attribute_list = cp_parser_attribute_list (parser);
18889       else
18890         /* If the next token is a `)', then there is no attribute
18891            list.  */
18892         attribute_list = NULL;
18893
18894       /* Look for the two `)' tokens.  */
18895       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18896       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18897
18898       /* Add these new attributes to the list.  */
18899       attributes = chainon (attributes, attribute_list);
18900     }
18901
18902   return attributes;
18903 }
18904
18905 /* Parse an attribute-list.
18906
18907    attribute-list:
18908      attribute
18909      attribute-list , attribute
18910
18911    attribute:
18912      identifier
18913      identifier ( identifier )
18914      identifier ( identifier , expression-list )
18915      identifier ( expression-list )
18916
18917    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18918    to an attribute.  The TREE_PURPOSE of each node is the identifier
18919    indicating which attribute is in use.  The TREE_VALUE represents
18920    the arguments, if any.  */
18921
18922 static tree
18923 cp_parser_attribute_list (cp_parser* parser)
18924 {
18925   tree attribute_list = NULL_TREE;
18926   bool save_translate_strings_p = parser->translate_strings_p;
18927
18928   parser->translate_strings_p = false;
18929   while (true)
18930     {
18931       cp_token *token;
18932       tree identifier;
18933       tree attribute;
18934
18935       /* Look for the identifier.  We also allow keywords here; for
18936          example `__attribute__ ((const))' is legal.  */
18937       token = cp_lexer_peek_token (parser->lexer);
18938       if (token->type == CPP_NAME
18939           || token->type == CPP_KEYWORD)
18940         {
18941           tree arguments = NULL_TREE;
18942
18943           /* Consume the token.  */
18944           token = cp_lexer_consume_token (parser->lexer);
18945
18946           /* Save away the identifier that indicates which attribute
18947              this is.  */
18948           identifier = (token->type == CPP_KEYWORD) 
18949             /* For keywords, use the canonical spelling, not the
18950                parsed identifier.  */
18951             ? ridpointers[(int) token->keyword]
18952             : token->u.value;
18953           
18954           attribute = build_tree_list (identifier, NULL_TREE);
18955
18956           /* Peek at the next token.  */
18957           token = cp_lexer_peek_token (parser->lexer);
18958           /* If it's an `(', then parse the attribute arguments.  */
18959           if (token->type == CPP_OPEN_PAREN)
18960             {
18961               VEC(tree,gc) *vec;
18962               int attr_flag = (attribute_takes_identifier_p (identifier)
18963                                ? id_attr : normal_attr);
18964               vec = cp_parser_parenthesized_expression_list
18965                     (parser, attr_flag, /*cast_p=*/false,
18966                      /*allow_expansion_p=*/false,
18967                      /*non_constant_p=*/NULL);
18968               if (vec == NULL)
18969                 arguments = error_mark_node;
18970               else
18971                 {
18972                   arguments = build_tree_list_vec (vec);
18973                   release_tree_vector (vec);
18974                 }
18975               /* Save the arguments away.  */
18976               TREE_VALUE (attribute) = arguments;
18977             }
18978
18979           if (arguments != error_mark_node)
18980             {
18981               /* Add this attribute to the list.  */
18982               TREE_CHAIN (attribute) = attribute_list;
18983               attribute_list = attribute;
18984             }
18985
18986           token = cp_lexer_peek_token (parser->lexer);
18987         }
18988       /* Now, look for more attributes.  If the next token isn't a
18989          `,', we're done.  */
18990       if (token->type != CPP_COMMA)
18991         break;
18992
18993       /* Consume the comma and keep going.  */
18994       cp_lexer_consume_token (parser->lexer);
18995     }
18996   parser->translate_strings_p = save_translate_strings_p;
18997
18998   /* We built up the list in reverse order.  */
18999   return nreverse (attribute_list);
19000 }
19001
19002 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19003    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19004    current value of the PEDANTIC flag, regardless of whether or not
19005    the `__extension__' keyword is present.  The caller is responsible
19006    for restoring the value of the PEDANTIC flag.  */
19007
19008 static bool
19009 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19010 {
19011   /* Save the old value of the PEDANTIC flag.  */
19012   *saved_pedantic = pedantic;
19013
19014   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19015     {
19016       /* Consume the `__extension__' token.  */
19017       cp_lexer_consume_token (parser->lexer);
19018       /* We're not being pedantic while the `__extension__' keyword is
19019          in effect.  */
19020       pedantic = 0;
19021
19022       return true;
19023     }
19024
19025   return false;
19026 }
19027
19028 /* Parse a label declaration.
19029
19030    label-declaration:
19031      __label__ label-declarator-seq ;
19032
19033    label-declarator-seq:
19034      identifier , label-declarator-seq
19035      identifier  */
19036
19037 static void
19038 cp_parser_label_declaration (cp_parser* parser)
19039 {
19040   /* Look for the `__label__' keyword.  */
19041   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19042
19043   while (true)
19044     {
19045       tree identifier;
19046
19047       /* Look for an identifier.  */
19048       identifier = cp_parser_identifier (parser);
19049       /* If we failed, stop.  */
19050       if (identifier == error_mark_node)
19051         break;
19052       /* Declare it as a label.  */
19053       finish_label_decl (identifier);
19054       /* If the next token is a `;', stop.  */
19055       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19056         break;
19057       /* Look for the `,' separating the label declarations.  */
19058       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19059     }
19060
19061   /* Look for the final `;'.  */
19062   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19063 }
19064
19065 /* Support Functions */
19066
19067 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19068    NAME should have one of the representations used for an
19069    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19070    is returned.  If PARSER->SCOPE is a dependent type, then a
19071    SCOPE_REF is returned.
19072
19073    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19074    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19075    was formed.  Abstractly, such entities should not be passed to this
19076    function, because they do not need to be looked up, but it is
19077    simpler to check for this special case here, rather than at the
19078    call-sites.
19079
19080    In cases not explicitly covered above, this function returns a
19081    DECL, OVERLOAD, or baselink representing the result of the lookup.
19082    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19083    is returned.
19084
19085    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19086    (e.g., "struct") that was used.  In that case bindings that do not
19087    refer to types are ignored.
19088
19089    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19090    ignored.
19091
19092    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19093    are ignored.
19094
19095    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19096    types.
19097
19098    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19099    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19100    NULL_TREE otherwise.  */
19101
19102 static tree
19103 cp_parser_lookup_name (cp_parser *parser, tree name,
19104                        enum tag_types tag_type,
19105                        bool is_template,
19106                        bool is_namespace,
19107                        bool check_dependency,
19108                        tree *ambiguous_decls,
19109                        location_t name_location)
19110 {
19111   int flags = 0;
19112   tree decl;
19113   tree object_type = parser->context->object_type;
19114
19115   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19116     flags |= LOOKUP_COMPLAIN;
19117
19118   /* Assume that the lookup will be unambiguous.  */
19119   if (ambiguous_decls)
19120     *ambiguous_decls = NULL_TREE;
19121
19122   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19123      no longer valid.  Note that if we are parsing tentatively, and
19124      the parse fails, OBJECT_TYPE will be automatically restored.  */
19125   parser->context->object_type = NULL_TREE;
19126
19127   if (name == error_mark_node)
19128     return error_mark_node;
19129
19130   /* A template-id has already been resolved; there is no lookup to
19131      do.  */
19132   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19133     return name;
19134   if (BASELINK_P (name))
19135     {
19136       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19137                   == TEMPLATE_ID_EXPR);
19138       return name;
19139     }
19140
19141   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19142      it should already have been checked to make sure that the name
19143      used matches the type being destroyed.  */
19144   if (TREE_CODE (name) == BIT_NOT_EXPR)
19145     {
19146       tree type;
19147
19148       /* Figure out to which type this destructor applies.  */
19149       if (parser->scope)
19150         type = parser->scope;
19151       else if (object_type)
19152         type = object_type;
19153       else
19154         type = current_class_type;
19155       /* If that's not a class type, there is no destructor.  */
19156       if (!type || !CLASS_TYPE_P (type))
19157         return error_mark_node;
19158       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19159         lazily_declare_fn (sfk_destructor, type);
19160       if (!CLASSTYPE_DESTRUCTORS (type))
19161           return error_mark_node;
19162       /* If it was a class type, return the destructor.  */
19163       return CLASSTYPE_DESTRUCTORS (type);
19164     }
19165
19166   /* By this point, the NAME should be an ordinary identifier.  If
19167      the id-expression was a qualified name, the qualifying scope is
19168      stored in PARSER->SCOPE at this point.  */
19169   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19170
19171   /* Perform the lookup.  */
19172   if (parser->scope)
19173     {
19174       bool dependent_p;
19175
19176       if (parser->scope == error_mark_node)
19177         return error_mark_node;
19178
19179       /* If the SCOPE is dependent, the lookup must be deferred until
19180          the template is instantiated -- unless we are explicitly
19181          looking up names in uninstantiated templates.  Even then, we
19182          cannot look up the name if the scope is not a class type; it
19183          might, for example, be a template type parameter.  */
19184       dependent_p = (TYPE_P (parser->scope)
19185                      && dependent_scope_p (parser->scope));
19186       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19187           && dependent_p)
19188         /* Defer lookup.  */
19189         decl = error_mark_node;
19190       else
19191         {
19192           tree pushed_scope = NULL_TREE;
19193
19194           /* If PARSER->SCOPE is a dependent type, then it must be a
19195              class type, and we must not be checking dependencies;
19196              otherwise, we would have processed this lookup above.  So
19197              that PARSER->SCOPE is not considered a dependent base by
19198              lookup_member, we must enter the scope here.  */
19199           if (dependent_p)
19200             pushed_scope = push_scope (parser->scope);
19201
19202           /* If the PARSER->SCOPE is a template specialization, it
19203              may be instantiated during name lookup.  In that case,
19204              errors may be issued.  Even if we rollback the current
19205              tentative parse, those errors are valid.  */
19206           decl = lookup_qualified_name (parser->scope, name,
19207                                         tag_type != none_type,
19208                                         /*complain=*/true);
19209
19210           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19211              lookup result and the nested-name-specifier nominates a class C:
19212                * if the name specified after the nested-name-specifier, when
19213                looked up in C, is the injected-class-name of C (Clause 9), or
19214                * if the name specified after the nested-name-specifier is the
19215                same as the identifier or the simple-template-id's template-
19216                name in the last component of the nested-name-specifier,
19217              the name is instead considered to name the constructor of
19218              class C. [ Note: for example, the constructor is not an
19219              acceptable lookup result in an elaborated-type-specifier so
19220              the constructor would not be used in place of the
19221              injected-class-name. --end note ] Such a constructor name
19222              shall be used only in the declarator-id of a declaration that
19223              names a constructor or in a using-declaration.  */
19224           if (tag_type == none_type
19225               && DECL_SELF_REFERENCE_P (decl)
19226               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19227             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19228                                           tag_type != none_type,
19229                                           /*complain=*/true);
19230
19231           /* If we have a single function from a using decl, pull it out.  */
19232           if (TREE_CODE (decl) == OVERLOAD
19233               && !really_overloaded_fn (decl))
19234             decl = OVL_FUNCTION (decl);
19235
19236           if (pushed_scope)
19237             pop_scope (pushed_scope);
19238         }
19239
19240       /* If the scope is a dependent type and either we deferred lookup or
19241          we did lookup but didn't find the name, rememeber the name.  */
19242       if (decl == error_mark_node && TYPE_P (parser->scope)
19243           && dependent_type_p (parser->scope))
19244         {
19245           if (tag_type)
19246             {
19247               tree type;
19248
19249               /* The resolution to Core Issue 180 says that `struct
19250                  A::B' should be considered a type-name, even if `A'
19251                  is dependent.  */
19252               type = make_typename_type (parser->scope, name, tag_type,
19253                                          /*complain=*/tf_error);
19254               decl = TYPE_NAME (type);
19255             }
19256           else if (is_template
19257                    && (cp_parser_next_token_ends_template_argument_p (parser)
19258                        || cp_lexer_next_token_is (parser->lexer,
19259                                                   CPP_CLOSE_PAREN)))
19260             decl = make_unbound_class_template (parser->scope,
19261                                                 name, NULL_TREE,
19262                                                 /*complain=*/tf_error);
19263           else
19264             decl = build_qualified_name (/*type=*/NULL_TREE,
19265                                          parser->scope, name,
19266                                          is_template);
19267         }
19268       parser->qualifying_scope = parser->scope;
19269       parser->object_scope = NULL_TREE;
19270     }
19271   else if (object_type)
19272     {
19273       tree object_decl = NULL_TREE;
19274       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19275          OBJECT_TYPE is not a class.  */
19276       if (CLASS_TYPE_P (object_type))
19277         /* If the OBJECT_TYPE is a template specialization, it may
19278            be instantiated during name lookup.  In that case, errors
19279            may be issued.  Even if we rollback the current tentative
19280            parse, those errors are valid.  */
19281         object_decl = lookup_member (object_type,
19282                                      name,
19283                                      /*protect=*/0,
19284                                      tag_type != none_type);
19285       /* Look it up in the enclosing context, too.  */
19286       decl = lookup_name_real (name, tag_type != none_type,
19287                                /*nonclass=*/0,
19288                                /*block_p=*/true, is_namespace, flags);
19289       parser->object_scope = object_type;
19290       parser->qualifying_scope = NULL_TREE;
19291       if (object_decl)
19292         decl = object_decl;
19293     }
19294   else
19295     {
19296       decl = lookup_name_real (name, tag_type != none_type,
19297                                /*nonclass=*/0,
19298                                /*block_p=*/true, is_namespace, flags);
19299       parser->qualifying_scope = NULL_TREE;
19300       parser->object_scope = NULL_TREE;
19301     }
19302
19303   /* If the lookup failed, let our caller know.  */
19304   if (!decl || decl == error_mark_node)
19305     return error_mark_node;
19306
19307   /* Pull out the template from an injected-class-name (or multiple).  */
19308   if (is_template)
19309     decl = maybe_get_template_decl_from_type_decl (decl);
19310
19311   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19312   if (TREE_CODE (decl) == TREE_LIST)
19313     {
19314       if (ambiguous_decls)
19315         *ambiguous_decls = decl;
19316       /* The error message we have to print is too complicated for
19317          cp_parser_error, so we incorporate its actions directly.  */
19318       if (!cp_parser_simulate_error (parser))
19319         {
19320           error_at (name_location, "reference to %qD is ambiguous",
19321                     name);
19322           print_candidates (decl);
19323         }
19324       return error_mark_node;
19325     }
19326
19327   gcc_assert (DECL_P (decl)
19328               || TREE_CODE (decl) == OVERLOAD
19329               || TREE_CODE (decl) == SCOPE_REF
19330               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19331               || BASELINK_P (decl));
19332
19333   /* If we have resolved the name of a member declaration, check to
19334      see if the declaration is accessible.  When the name resolves to
19335      set of overloaded functions, accessibility is checked when
19336      overload resolution is done.
19337
19338      During an explicit instantiation, access is not checked at all,
19339      as per [temp.explicit].  */
19340   if (DECL_P (decl))
19341     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19342
19343   return decl;
19344 }
19345
19346 /* Like cp_parser_lookup_name, but for use in the typical case where
19347    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19348    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19349
19350 static tree
19351 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19352 {
19353   return cp_parser_lookup_name (parser, name,
19354                                 none_type,
19355                                 /*is_template=*/false,
19356                                 /*is_namespace=*/false,
19357                                 /*check_dependency=*/true,
19358                                 /*ambiguous_decls=*/NULL,
19359                                 location);
19360 }
19361
19362 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19363    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19364    true, the DECL indicates the class being defined in a class-head,
19365    or declared in an elaborated-type-specifier.
19366
19367    Otherwise, return DECL.  */
19368
19369 static tree
19370 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19371 {
19372   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19373      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19374
19375        struct A {
19376          template <typename T> struct B;
19377        };
19378
19379        template <typename T> struct A::B {};
19380
19381      Similarly, in an elaborated-type-specifier:
19382
19383        namespace N { struct X{}; }
19384
19385        struct A {
19386          template <typename T> friend struct N::X;
19387        };
19388
19389      However, if the DECL refers to a class type, and we are in
19390      the scope of the class, then the name lookup automatically
19391      finds the TYPE_DECL created by build_self_reference rather
19392      than a TEMPLATE_DECL.  For example, in:
19393
19394        template <class T> struct S {
19395          S s;
19396        };
19397
19398      there is no need to handle such case.  */
19399
19400   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19401     return DECL_TEMPLATE_RESULT (decl);
19402
19403   return decl;
19404 }
19405
19406 /* If too many, or too few, template-parameter lists apply to the
19407    declarator, issue an error message.  Returns TRUE if all went well,
19408    and FALSE otherwise.  */
19409
19410 static bool
19411 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19412                                                 cp_declarator *declarator,
19413                                                 location_t declarator_location)
19414 {
19415   unsigned num_templates;
19416
19417   /* We haven't seen any classes that involve template parameters yet.  */
19418   num_templates = 0;
19419
19420   switch (declarator->kind)
19421     {
19422     case cdk_id:
19423       if (declarator->u.id.qualifying_scope)
19424         {
19425           tree scope;
19426
19427           scope = declarator->u.id.qualifying_scope;
19428
19429           while (scope && CLASS_TYPE_P (scope))
19430             {
19431               /* You're supposed to have one `template <...>'
19432                  for every template class, but you don't need one
19433                  for a full specialization.  For example:
19434
19435                  template <class T> struct S{};
19436                  template <> struct S<int> { void f(); };
19437                  void S<int>::f () {}
19438
19439                  is correct; there shouldn't be a `template <>' for
19440                  the definition of `S<int>::f'.  */
19441               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19442                 /* If SCOPE does not have template information of any
19443                    kind, then it is not a template, nor is it nested
19444                    within a template.  */
19445                 break;
19446               if (explicit_class_specialization_p (scope))
19447                 break;
19448               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19449                 ++num_templates;
19450
19451               scope = TYPE_CONTEXT (scope);
19452             }
19453         }
19454       else if (TREE_CODE (declarator->u.id.unqualified_name)
19455                == TEMPLATE_ID_EXPR)
19456         /* If the DECLARATOR has the form `X<y>' then it uses one
19457            additional level of template parameters.  */
19458         ++num_templates;
19459
19460       return cp_parser_check_template_parameters 
19461         (parser, num_templates, declarator_location, declarator);
19462
19463
19464     case cdk_function:
19465     case cdk_array:
19466     case cdk_pointer:
19467     case cdk_reference:
19468     case cdk_ptrmem:
19469       return (cp_parser_check_declarator_template_parameters
19470               (parser, declarator->declarator, declarator_location));
19471
19472     case cdk_error:
19473       return true;
19474
19475     default:
19476       gcc_unreachable ();
19477     }
19478   return false;
19479 }
19480
19481 /* NUM_TEMPLATES were used in the current declaration.  If that is
19482    invalid, return FALSE and issue an error messages.  Otherwise,
19483    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19484    declarator and we can print more accurate diagnostics.  */
19485
19486 static bool
19487 cp_parser_check_template_parameters (cp_parser* parser,
19488                                      unsigned num_templates,
19489                                      location_t location,
19490                                      cp_declarator *declarator)
19491 {
19492   /* If there are the same number of template classes and parameter
19493      lists, that's OK.  */
19494   if (parser->num_template_parameter_lists == num_templates)
19495     return true;
19496   /* If there are more, but only one more, then we are referring to a
19497      member template.  That's OK too.  */
19498   if (parser->num_template_parameter_lists == num_templates + 1)
19499     return true;
19500   /* If there are more template classes than parameter lists, we have
19501      something like:
19502
19503        template <class T> void S<T>::R<T>::f ();  */
19504   if (parser->num_template_parameter_lists < num_templates)
19505     {
19506       if (declarator && !current_function_decl)
19507         error_at (location, "specializing member %<%T::%E%> "
19508                   "requires %<template<>%> syntax", 
19509                   declarator->u.id.qualifying_scope,
19510                   declarator->u.id.unqualified_name);
19511       else if (declarator)
19512         error_at (location, "invalid declaration of %<%T::%E%>",
19513                   declarator->u.id.qualifying_scope,
19514                   declarator->u.id.unqualified_name);
19515       else 
19516         error_at (location, "too few template-parameter-lists");
19517       return false;
19518     }
19519   /* Otherwise, there are too many template parameter lists.  We have
19520      something like:
19521
19522      template <class T> template <class U> void S::f();  */
19523   error_at (location, "too many template-parameter-lists");
19524   return false;
19525 }
19526
19527 /* Parse an optional `::' token indicating that the following name is
19528    from the global namespace.  If so, PARSER->SCOPE is set to the
19529    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19530    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19531    Returns the new value of PARSER->SCOPE, if the `::' token is
19532    present, and NULL_TREE otherwise.  */
19533
19534 static tree
19535 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19536 {
19537   cp_token *token;
19538
19539   /* Peek at the next token.  */
19540   token = cp_lexer_peek_token (parser->lexer);
19541   /* If we're looking at a `::' token then we're starting from the
19542      global namespace, not our current location.  */
19543   if (token->type == CPP_SCOPE)
19544     {
19545       /* Consume the `::' token.  */
19546       cp_lexer_consume_token (parser->lexer);
19547       /* Set the SCOPE so that we know where to start the lookup.  */
19548       parser->scope = global_namespace;
19549       parser->qualifying_scope = global_namespace;
19550       parser->object_scope = NULL_TREE;
19551
19552       return parser->scope;
19553     }
19554   else if (!current_scope_valid_p)
19555     {
19556       parser->scope = NULL_TREE;
19557       parser->qualifying_scope = NULL_TREE;
19558       parser->object_scope = NULL_TREE;
19559     }
19560
19561   return NULL_TREE;
19562 }
19563
19564 /* Returns TRUE if the upcoming token sequence is the start of a
19565    constructor declarator.  If FRIEND_P is true, the declarator is
19566    preceded by the `friend' specifier.  */
19567
19568 static bool
19569 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19570 {
19571   bool constructor_p;
19572   tree nested_name_specifier;
19573   cp_token *next_token;
19574
19575   /* The common case is that this is not a constructor declarator, so
19576      try to avoid doing lots of work if at all possible.  It's not
19577      valid declare a constructor at function scope.  */
19578   if (parser->in_function_body)
19579     return false;
19580   /* And only certain tokens can begin a constructor declarator.  */
19581   next_token = cp_lexer_peek_token (parser->lexer);
19582   if (next_token->type != CPP_NAME
19583       && next_token->type != CPP_SCOPE
19584       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19585       && next_token->type != CPP_TEMPLATE_ID)
19586     return false;
19587
19588   /* Parse tentatively; we are going to roll back all of the tokens
19589      consumed here.  */
19590   cp_parser_parse_tentatively (parser);
19591   /* Assume that we are looking at a constructor declarator.  */
19592   constructor_p = true;
19593
19594   /* Look for the optional `::' operator.  */
19595   cp_parser_global_scope_opt (parser,
19596                               /*current_scope_valid_p=*/false);
19597   /* Look for the nested-name-specifier.  */
19598   nested_name_specifier
19599     = (cp_parser_nested_name_specifier_opt (parser,
19600                                             /*typename_keyword_p=*/false,
19601                                             /*check_dependency_p=*/false,
19602                                             /*type_p=*/false,
19603                                             /*is_declaration=*/false));
19604   /* Outside of a class-specifier, there must be a
19605      nested-name-specifier.  */
19606   if (!nested_name_specifier &&
19607       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19608        || friend_p))
19609     constructor_p = false;
19610   else if (nested_name_specifier == error_mark_node)
19611     constructor_p = false;
19612
19613   /* If we have a class scope, this is easy; DR 147 says that S::S always
19614      names the constructor, and no other qualified name could.  */
19615   if (constructor_p && nested_name_specifier
19616       && CLASS_TYPE_P (nested_name_specifier))
19617     {
19618       tree id = cp_parser_unqualified_id (parser,
19619                                           /*template_keyword_p=*/false,
19620                                           /*check_dependency_p=*/false,
19621                                           /*declarator_p=*/true,
19622                                           /*optional_p=*/false);
19623       if (is_overloaded_fn (id))
19624         id = DECL_NAME (get_first_fn (id));
19625       if (!constructor_name_p (id, nested_name_specifier))
19626         constructor_p = false;
19627     }
19628   /* If we still think that this might be a constructor-declarator,
19629      look for a class-name.  */
19630   else if (constructor_p)
19631     {
19632       /* If we have:
19633
19634            template <typename T> struct S {
19635              S();
19636            };
19637
19638          we must recognize that the nested `S' names a class.  */
19639       tree type_decl;
19640       type_decl = cp_parser_class_name (parser,
19641                                         /*typename_keyword_p=*/false,
19642                                         /*template_keyword_p=*/false,
19643                                         none_type,
19644                                         /*check_dependency_p=*/false,
19645                                         /*class_head_p=*/false,
19646                                         /*is_declaration=*/false);
19647       /* If there was no class-name, then this is not a constructor.  */
19648       constructor_p = !cp_parser_error_occurred (parser);
19649
19650       /* If we're still considering a constructor, we have to see a `(',
19651          to begin the parameter-declaration-clause, followed by either a
19652          `)', an `...', or a decl-specifier.  We need to check for a
19653          type-specifier to avoid being fooled into thinking that:
19654
19655            S (f) (int);
19656
19657          is a constructor.  (It is actually a function named `f' that
19658          takes one parameter (of type `int') and returns a value of type
19659          `S'.  */
19660       if (constructor_p
19661           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19662         constructor_p = false;
19663
19664       if (constructor_p
19665           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19666           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19667           /* A parameter declaration begins with a decl-specifier,
19668              which is either the "attribute" keyword, a storage class
19669              specifier, or (usually) a type-specifier.  */
19670           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19671         {
19672           tree type;
19673           tree pushed_scope = NULL_TREE;
19674           unsigned saved_num_template_parameter_lists;
19675
19676           /* Names appearing in the type-specifier should be looked up
19677              in the scope of the class.  */
19678           if (current_class_type)
19679             type = NULL_TREE;
19680           else
19681             {
19682               type = TREE_TYPE (type_decl);
19683               if (TREE_CODE (type) == TYPENAME_TYPE)
19684                 {
19685                   type = resolve_typename_type (type,
19686                                                 /*only_current_p=*/false);
19687                   if (TREE_CODE (type) == TYPENAME_TYPE)
19688                     {
19689                       cp_parser_abort_tentative_parse (parser);
19690                       return false;
19691                     }
19692                 }
19693               pushed_scope = push_scope (type);
19694             }
19695
19696           /* Inside the constructor parameter list, surrounding
19697              template-parameter-lists do not apply.  */
19698           saved_num_template_parameter_lists
19699             = parser->num_template_parameter_lists;
19700           parser->num_template_parameter_lists = 0;
19701
19702           /* Look for the type-specifier.  */
19703           cp_parser_type_specifier (parser,
19704                                     CP_PARSER_FLAGS_NONE,
19705                                     /*decl_specs=*/NULL,
19706                                     /*is_declarator=*/true,
19707                                     /*declares_class_or_enum=*/NULL,
19708                                     /*is_cv_qualifier=*/NULL);
19709
19710           parser->num_template_parameter_lists
19711             = saved_num_template_parameter_lists;
19712
19713           /* Leave the scope of the class.  */
19714           if (pushed_scope)
19715             pop_scope (pushed_scope);
19716
19717           constructor_p = !cp_parser_error_occurred (parser);
19718         }
19719     }
19720
19721   /* We did not really want to consume any tokens.  */
19722   cp_parser_abort_tentative_parse (parser);
19723
19724   return constructor_p;
19725 }
19726
19727 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19728    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19729    they must be performed once we are in the scope of the function.
19730
19731    Returns the function defined.  */
19732
19733 static tree
19734 cp_parser_function_definition_from_specifiers_and_declarator
19735   (cp_parser* parser,
19736    cp_decl_specifier_seq *decl_specifiers,
19737    tree attributes,
19738    const cp_declarator *declarator)
19739 {
19740   tree fn;
19741   bool success_p;
19742
19743   /* Begin the function-definition.  */
19744   success_p = start_function (decl_specifiers, declarator, attributes);
19745
19746   /* The things we're about to see are not directly qualified by any
19747      template headers we've seen thus far.  */
19748   reset_specialization ();
19749
19750   /* If there were names looked up in the decl-specifier-seq that we
19751      did not check, check them now.  We must wait until we are in the
19752      scope of the function to perform the checks, since the function
19753      might be a friend.  */
19754   perform_deferred_access_checks ();
19755
19756   if (!success_p)
19757     {
19758       /* Skip the entire function.  */
19759       cp_parser_skip_to_end_of_block_or_statement (parser);
19760       fn = error_mark_node;
19761     }
19762   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19763     {
19764       /* Seen already, skip it.  An error message has already been output.  */
19765       cp_parser_skip_to_end_of_block_or_statement (parser);
19766       fn = current_function_decl;
19767       current_function_decl = NULL_TREE;
19768       /* If this is a function from a class, pop the nested class.  */
19769       if (current_class_name)
19770         pop_nested_class ();
19771     }
19772   else
19773     {
19774       timevar_id_t tv;
19775       if (DECL_DECLARED_INLINE_P (current_function_decl))
19776         tv = TV_PARSE_INLINE;
19777       else
19778         tv = TV_PARSE_FUNC;
19779       timevar_push (tv);
19780       fn = cp_parser_function_definition_after_declarator (parser,
19781                                                          /*inline_p=*/false);
19782       timevar_pop (tv);
19783     }
19784
19785   return fn;
19786 }
19787
19788 /* Parse the part of a function-definition that follows the
19789    declarator.  INLINE_P is TRUE iff this function is an inline
19790    function defined within a class-specifier.
19791
19792    Returns the function defined.  */
19793
19794 static tree
19795 cp_parser_function_definition_after_declarator (cp_parser* parser,
19796                                                 bool inline_p)
19797 {
19798   tree fn;
19799   bool ctor_initializer_p = false;
19800   bool saved_in_unbraced_linkage_specification_p;
19801   bool saved_in_function_body;
19802   unsigned saved_num_template_parameter_lists;
19803   cp_token *token;
19804
19805   saved_in_function_body = parser->in_function_body;
19806   parser->in_function_body = true;
19807   /* If the next token is `return', then the code may be trying to
19808      make use of the "named return value" extension that G++ used to
19809      support.  */
19810   token = cp_lexer_peek_token (parser->lexer);
19811   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19812     {
19813       /* Consume the `return' keyword.  */
19814       cp_lexer_consume_token (parser->lexer);
19815       /* Look for the identifier that indicates what value is to be
19816          returned.  */
19817       cp_parser_identifier (parser);
19818       /* Issue an error message.  */
19819       error_at (token->location,
19820                 "named return values are no longer supported");
19821       /* Skip tokens until we reach the start of the function body.  */
19822       while (true)
19823         {
19824           cp_token *token = cp_lexer_peek_token (parser->lexer);
19825           if (token->type == CPP_OPEN_BRACE
19826               || token->type == CPP_EOF
19827               || token->type == CPP_PRAGMA_EOL)
19828             break;
19829           cp_lexer_consume_token (parser->lexer);
19830         }
19831     }
19832   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19833      anything declared inside `f'.  */
19834   saved_in_unbraced_linkage_specification_p
19835     = parser->in_unbraced_linkage_specification_p;
19836   parser->in_unbraced_linkage_specification_p = false;
19837   /* Inside the function, surrounding template-parameter-lists do not
19838      apply.  */
19839   saved_num_template_parameter_lists
19840     = parser->num_template_parameter_lists;
19841   parser->num_template_parameter_lists = 0;
19842
19843   start_lambda_scope (current_function_decl);
19844
19845   /* If the next token is `try', then we are looking at a
19846      function-try-block.  */
19847   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19848     ctor_initializer_p = cp_parser_function_try_block (parser);
19849   /* A function-try-block includes the function-body, so we only do
19850      this next part if we're not processing a function-try-block.  */
19851   else
19852     ctor_initializer_p
19853       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19854
19855   finish_lambda_scope ();
19856
19857   /* Finish the function.  */
19858   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19859                         (inline_p ? 2 : 0));
19860   /* Generate code for it, if necessary.  */
19861   expand_or_defer_fn (fn);
19862   /* Restore the saved values.  */
19863   parser->in_unbraced_linkage_specification_p
19864     = saved_in_unbraced_linkage_specification_p;
19865   parser->num_template_parameter_lists
19866     = saved_num_template_parameter_lists;
19867   parser->in_function_body = saved_in_function_body;
19868
19869   return fn;
19870 }
19871
19872 /* Parse a template-declaration, assuming that the `export' (and
19873    `extern') keywords, if present, has already been scanned.  MEMBER_P
19874    is as for cp_parser_template_declaration.  */
19875
19876 static void
19877 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19878 {
19879   tree decl = NULL_TREE;
19880   VEC (deferred_access_check,gc) *checks;
19881   tree parameter_list;
19882   bool friend_p = false;
19883   bool need_lang_pop;
19884   cp_token *token;
19885
19886   /* Look for the `template' keyword.  */
19887   token = cp_lexer_peek_token (parser->lexer);
19888   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19889     return;
19890
19891   /* And the `<'.  */
19892   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19893     return;
19894   if (at_class_scope_p () && current_function_decl)
19895     {
19896       /* 14.5.2.2 [temp.mem]
19897
19898          A local class shall not have member templates.  */
19899       error_at (token->location,
19900                 "invalid declaration of member template in local class");
19901       cp_parser_skip_to_end_of_block_or_statement (parser);
19902       return;
19903     }
19904   /* [temp]
19905
19906      A template ... shall not have C linkage.  */
19907   if (current_lang_name == lang_name_c)
19908     {
19909       error_at (token->location, "template with C linkage");
19910       /* Give it C++ linkage to avoid confusing other parts of the
19911          front end.  */
19912       push_lang_context (lang_name_cplusplus);
19913       need_lang_pop = true;
19914     }
19915   else
19916     need_lang_pop = false;
19917
19918   /* We cannot perform access checks on the template parameter
19919      declarations until we know what is being declared, just as we
19920      cannot check the decl-specifier list.  */
19921   push_deferring_access_checks (dk_deferred);
19922
19923   /* If the next token is `>', then we have an invalid
19924      specialization.  Rather than complain about an invalid template
19925      parameter, issue an error message here.  */
19926   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19927     {
19928       cp_parser_error (parser, "invalid explicit specialization");
19929       begin_specialization ();
19930       parameter_list = NULL_TREE;
19931     }
19932   else
19933     {
19934       /* Parse the template parameters.  */
19935       parameter_list = cp_parser_template_parameter_list (parser);
19936       fixup_template_parms ();
19937     }
19938
19939   /* Get the deferred access checks from the parameter list.  These
19940      will be checked once we know what is being declared, as for a
19941      member template the checks must be performed in the scope of the
19942      class containing the member.  */
19943   checks = get_deferred_access_checks ();
19944
19945   /* Look for the `>'.  */
19946   cp_parser_skip_to_end_of_template_parameter_list (parser);
19947   /* We just processed one more parameter list.  */
19948   ++parser->num_template_parameter_lists;
19949   /* If the next token is `template', there are more template
19950      parameters.  */
19951   if (cp_lexer_next_token_is_keyword (parser->lexer,
19952                                       RID_TEMPLATE))
19953     cp_parser_template_declaration_after_export (parser, member_p);
19954   else
19955     {
19956       /* There are no access checks when parsing a template, as we do not
19957          know if a specialization will be a friend.  */
19958       push_deferring_access_checks (dk_no_check);
19959       token = cp_lexer_peek_token (parser->lexer);
19960       decl = cp_parser_single_declaration (parser,
19961                                            checks,
19962                                            member_p,
19963                                            /*explicit_specialization_p=*/false,
19964                                            &friend_p);
19965       pop_deferring_access_checks ();
19966
19967       /* If this is a member template declaration, let the front
19968          end know.  */
19969       if (member_p && !friend_p && decl)
19970         {
19971           if (TREE_CODE (decl) == TYPE_DECL)
19972             cp_parser_check_access_in_redeclaration (decl, token->location);
19973
19974           decl = finish_member_template_decl (decl);
19975         }
19976       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19977         make_friend_class (current_class_type, TREE_TYPE (decl),
19978                            /*complain=*/true);
19979     }
19980   /* We are done with the current parameter list.  */
19981   --parser->num_template_parameter_lists;
19982
19983   pop_deferring_access_checks ();
19984
19985   /* Finish up.  */
19986   finish_template_decl (parameter_list);
19987
19988   /* Register member declarations.  */
19989   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19990     finish_member_declaration (decl);
19991   /* For the erroneous case of a template with C linkage, we pushed an
19992      implicit C++ linkage scope; exit that scope now.  */
19993   if (need_lang_pop)
19994     pop_lang_context ();
19995   /* If DECL is a function template, we must return to parse it later.
19996      (Even though there is no definition, there might be default
19997      arguments that need handling.)  */
19998   if (member_p && decl
19999       && (TREE_CODE (decl) == FUNCTION_DECL
20000           || DECL_FUNCTION_TEMPLATE_P (decl)))
20001     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20002 }
20003
20004 /* Perform the deferred access checks from a template-parameter-list.
20005    CHECKS is a TREE_LIST of access checks, as returned by
20006    get_deferred_access_checks.  */
20007
20008 static void
20009 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20010 {
20011   ++processing_template_parmlist;
20012   perform_access_checks (checks);
20013   --processing_template_parmlist;
20014 }
20015
20016 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20017    `function-definition' sequence.  MEMBER_P is true, this declaration
20018    appears in a class scope.
20019
20020    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20021    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20022
20023 static tree
20024 cp_parser_single_declaration (cp_parser* parser,
20025                               VEC (deferred_access_check,gc)* checks,
20026                               bool member_p,
20027                               bool explicit_specialization_p,
20028                               bool* friend_p)
20029 {
20030   int declares_class_or_enum;
20031   tree decl = NULL_TREE;
20032   cp_decl_specifier_seq decl_specifiers;
20033   bool function_definition_p = false;
20034   cp_token *decl_spec_token_start;
20035
20036   /* This function is only used when processing a template
20037      declaration.  */
20038   gcc_assert (innermost_scope_kind () == sk_template_parms
20039               || innermost_scope_kind () == sk_template_spec);
20040
20041   /* Defer access checks until we know what is being declared.  */
20042   push_deferring_access_checks (dk_deferred);
20043
20044   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20045      alternative.  */
20046   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20047   cp_parser_decl_specifier_seq (parser,
20048                                 CP_PARSER_FLAGS_OPTIONAL,
20049                                 &decl_specifiers,
20050                                 &declares_class_or_enum);
20051   if (friend_p)
20052     *friend_p = cp_parser_friend_p (&decl_specifiers);
20053
20054   /* There are no template typedefs.  */
20055   if (decl_specifiers.specs[(int) ds_typedef])
20056     {
20057       error_at (decl_spec_token_start->location,
20058                 "template declaration of %<typedef%>");
20059       decl = error_mark_node;
20060     }
20061
20062   /* Gather up the access checks that occurred the
20063      decl-specifier-seq.  */
20064   stop_deferring_access_checks ();
20065
20066   /* Check for the declaration of a template class.  */
20067   if (declares_class_or_enum)
20068     {
20069       if (cp_parser_declares_only_class_p (parser))
20070         {
20071           decl = shadow_tag (&decl_specifiers);
20072
20073           /* In this case:
20074
20075                struct C {
20076                  friend template <typename T> struct A<T>::B;
20077                };
20078
20079              A<T>::B will be represented by a TYPENAME_TYPE, and
20080              therefore not recognized by shadow_tag.  */
20081           if (friend_p && *friend_p
20082               && !decl
20083               && decl_specifiers.type
20084               && TYPE_P (decl_specifiers.type))
20085             decl = decl_specifiers.type;
20086
20087           if (decl && decl != error_mark_node)
20088             decl = TYPE_NAME (decl);
20089           else
20090             decl = error_mark_node;
20091
20092           /* Perform access checks for template parameters.  */
20093           cp_parser_perform_template_parameter_access_checks (checks);
20094         }
20095     }
20096
20097   /* Complain about missing 'typename' or other invalid type names.  */
20098   if (!decl_specifiers.any_type_specifiers_p
20099       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20100     {
20101       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20102          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20103          the rest of this declaration.  */
20104       decl = error_mark_node;
20105       goto out;
20106     }
20107
20108   /* If it's not a template class, try for a template function.  If
20109      the next token is a `;', then this declaration does not declare
20110      anything.  But, if there were errors in the decl-specifiers, then
20111      the error might well have come from an attempted class-specifier.
20112      In that case, there's no need to warn about a missing declarator.  */
20113   if (!decl
20114       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20115           || decl_specifiers.type != error_mark_node))
20116     {
20117       decl = cp_parser_init_declarator (parser,
20118                                         &decl_specifiers,
20119                                         checks,
20120                                         /*function_definition_allowed_p=*/true,
20121                                         member_p,
20122                                         declares_class_or_enum,
20123                                         &function_definition_p,
20124                                         NULL);
20125
20126     /* 7.1.1-1 [dcl.stc]
20127
20128        A storage-class-specifier shall not be specified in an explicit
20129        specialization...  */
20130     if (decl
20131         && explicit_specialization_p
20132         && decl_specifiers.storage_class != sc_none)
20133       {
20134         error_at (decl_spec_token_start->location,
20135                   "explicit template specialization cannot have a storage class");
20136         decl = error_mark_node;
20137       }
20138     }
20139
20140   /* Look for a trailing `;' after the declaration.  */
20141   if (!function_definition_p
20142       && (decl == error_mark_node
20143           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20144     cp_parser_skip_to_end_of_block_or_statement (parser);
20145
20146  out:
20147   pop_deferring_access_checks ();
20148
20149   /* Clear any current qualification; whatever comes next is the start
20150      of something new.  */
20151   parser->scope = NULL_TREE;
20152   parser->qualifying_scope = NULL_TREE;
20153   parser->object_scope = NULL_TREE;
20154
20155   return decl;
20156 }
20157
20158 /* Parse a cast-expression that is not the operand of a unary "&".  */
20159
20160 static tree
20161 cp_parser_simple_cast_expression (cp_parser *parser)
20162 {
20163   return cp_parser_cast_expression (parser, /*address_p=*/false,
20164                                     /*cast_p=*/false, NULL);
20165 }
20166
20167 /* Parse a functional cast to TYPE.  Returns an expression
20168    representing the cast.  */
20169
20170 static tree
20171 cp_parser_functional_cast (cp_parser* parser, tree type)
20172 {
20173   VEC(tree,gc) *vec;
20174   tree expression_list;
20175   tree cast;
20176   bool nonconst_p;
20177
20178   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20179     {
20180       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20181       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20182       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20183       if (TREE_CODE (type) == TYPE_DECL)
20184         type = TREE_TYPE (type);
20185       return finish_compound_literal (type, expression_list,
20186                                       tf_warning_or_error);
20187     }
20188
20189
20190   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20191                                                  /*cast_p=*/true,
20192                                                  /*allow_expansion_p=*/true,
20193                                                  /*non_constant_p=*/NULL);
20194   if (vec == NULL)
20195     expression_list = error_mark_node;
20196   else
20197     {
20198       expression_list = build_tree_list_vec (vec);
20199       release_tree_vector (vec);
20200     }
20201
20202   cast = build_functional_cast (type, expression_list,
20203                                 tf_warning_or_error);
20204   /* [expr.const]/1: In an integral constant expression "only type
20205      conversions to integral or enumeration type can be used".  */
20206   if (TREE_CODE (type) == TYPE_DECL)
20207     type = TREE_TYPE (type);
20208   if (cast != error_mark_node
20209       && !cast_valid_in_integral_constant_expression_p (type)
20210       && cp_parser_non_integral_constant_expression (parser,
20211                                                      NIC_CONSTRUCTOR))
20212     return error_mark_node;
20213   return cast;
20214 }
20215
20216 /* Save the tokens that make up the body of a member function defined
20217    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20218    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20219    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20220    for the member function.  */
20221
20222 static tree
20223 cp_parser_save_member_function_body (cp_parser* parser,
20224                                      cp_decl_specifier_seq *decl_specifiers,
20225                                      cp_declarator *declarator,
20226                                      tree attributes)
20227 {
20228   cp_token *first;
20229   cp_token *last;
20230   tree fn;
20231
20232   /* Create the FUNCTION_DECL.  */
20233   fn = grokmethod (decl_specifiers, declarator, attributes);
20234   /* If something went badly wrong, bail out now.  */
20235   if (fn == error_mark_node)
20236     {
20237       /* If there's a function-body, skip it.  */
20238       if (cp_parser_token_starts_function_definition_p
20239           (cp_lexer_peek_token (parser->lexer)))
20240         cp_parser_skip_to_end_of_block_or_statement (parser);
20241       return error_mark_node;
20242     }
20243
20244   /* Remember it, if there default args to post process.  */
20245   cp_parser_save_default_args (parser, fn);
20246
20247   /* Save away the tokens that make up the body of the
20248      function.  */
20249   first = parser->lexer->next_token;
20250   /* We can have braced-init-list mem-initializers before the fn body.  */
20251   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20252     {
20253       cp_lexer_consume_token (parser->lexer);
20254       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20255              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20256         {
20257           /* cache_group will stop after an un-nested { } pair, too.  */
20258           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20259             break;
20260
20261           /* variadic mem-inits have ... after the ')'.  */
20262           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20263             cp_lexer_consume_token (parser->lexer);
20264         }
20265     }
20266   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20267   /* Handle function try blocks.  */
20268   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20269     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20270   last = parser->lexer->next_token;
20271
20272   /* Save away the inline definition; we will process it when the
20273      class is complete.  */
20274   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20275   DECL_PENDING_INLINE_P (fn) = 1;
20276
20277   /* We need to know that this was defined in the class, so that
20278      friend templates are handled correctly.  */
20279   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20280
20281   /* Add FN to the queue of functions to be parsed later.  */
20282   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20283
20284   return fn;
20285 }
20286
20287 /* Parse a template-argument-list, as well as the trailing ">" (but
20288    not the opening ">").  See cp_parser_template_argument_list for the
20289    return value.  */
20290
20291 static tree
20292 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20293 {
20294   tree arguments;
20295   tree saved_scope;
20296   tree saved_qualifying_scope;
20297   tree saved_object_scope;
20298   bool saved_greater_than_is_operator_p;
20299   int saved_unevaluated_operand;
20300   int saved_inhibit_evaluation_warnings;
20301
20302   /* [temp.names]
20303
20304      When parsing a template-id, the first non-nested `>' is taken as
20305      the end of the template-argument-list rather than a greater-than
20306      operator.  */
20307   saved_greater_than_is_operator_p
20308     = parser->greater_than_is_operator_p;
20309   parser->greater_than_is_operator_p = false;
20310   /* Parsing the argument list may modify SCOPE, so we save it
20311      here.  */
20312   saved_scope = parser->scope;
20313   saved_qualifying_scope = parser->qualifying_scope;
20314   saved_object_scope = parser->object_scope;
20315   /* We need to evaluate the template arguments, even though this
20316      template-id may be nested within a "sizeof".  */
20317   saved_unevaluated_operand = cp_unevaluated_operand;
20318   cp_unevaluated_operand = 0;
20319   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20320   c_inhibit_evaluation_warnings = 0;
20321   /* Parse the template-argument-list itself.  */
20322   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20323       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20324     arguments = NULL_TREE;
20325   else
20326     arguments = cp_parser_template_argument_list (parser);
20327   /* Look for the `>' that ends the template-argument-list. If we find
20328      a '>>' instead, it's probably just a typo.  */
20329   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20330     {
20331       if (cxx_dialect != cxx98)
20332         {
20333           /* In C++0x, a `>>' in a template argument list or cast
20334              expression is considered to be two separate `>'
20335              tokens. So, change the current token to a `>', but don't
20336              consume it: it will be consumed later when the outer
20337              template argument list (or cast expression) is parsed.
20338              Note that this replacement of `>' for `>>' is necessary
20339              even if we are parsing tentatively: in the tentative
20340              case, after calling
20341              cp_parser_enclosed_template_argument_list we will always
20342              throw away all of the template arguments and the first
20343              closing `>', either because the template argument list
20344              was erroneous or because we are replacing those tokens
20345              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20346              not have been thrown away) is needed either to close an
20347              outer template argument list or to complete a new-style
20348              cast.  */
20349           cp_token *token = cp_lexer_peek_token (parser->lexer);
20350           token->type = CPP_GREATER;
20351         }
20352       else if (!saved_greater_than_is_operator_p)
20353         {
20354           /* If we're in a nested template argument list, the '>>' has
20355             to be a typo for '> >'. We emit the error message, but we
20356             continue parsing and we push a '>' as next token, so that
20357             the argument list will be parsed correctly.  Note that the
20358             global source location is still on the token before the
20359             '>>', so we need to say explicitly where we want it.  */
20360           cp_token *token = cp_lexer_peek_token (parser->lexer);
20361           error_at (token->location, "%<>>%> should be %<> >%> "
20362                     "within a nested template argument list");
20363
20364           token->type = CPP_GREATER;
20365         }
20366       else
20367         {
20368           /* If this is not a nested template argument list, the '>>'
20369             is a typo for '>'. Emit an error message and continue.
20370             Same deal about the token location, but here we can get it
20371             right by consuming the '>>' before issuing the diagnostic.  */
20372           cp_token *token = cp_lexer_consume_token (parser->lexer);
20373           error_at (token->location,
20374                     "spurious %<>>%>, use %<>%> to terminate "
20375                     "a template argument list");
20376         }
20377     }
20378   else
20379     cp_parser_skip_to_end_of_template_parameter_list (parser);
20380   /* The `>' token might be a greater-than operator again now.  */
20381   parser->greater_than_is_operator_p
20382     = saved_greater_than_is_operator_p;
20383   /* Restore the SAVED_SCOPE.  */
20384   parser->scope = saved_scope;
20385   parser->qualifying_scope = saved_qualifying_scope;
20386   parser->object_scope = saved_object_scope;
20387   cp_unevaluated_operand = saved_unevaluated_operand;
20388   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20389
20390   return arguments;
20391 }
20392
20393 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20394    arguments, or the body of the function have not yet been parsed,
20395    parse them now.  */
20396
20397 static void
20398 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20399 {
20400   timevar_push (TV_PARSE_INMETH);
20401   /* If this member is a template, get the underlying
20402      FUNCTION_DECL.  */
20403   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20404     member_function = DECL_TEMPLATE_RESULT (member_function);
20405
20406   /* There should not be any class definitions in progress at this
20407      point; the bodies of members are only parsed outside of all class
20408      definitions.  */
20409   gcc_assert (parser->num_classes_being_defined == 0);
20410   /* While we're parsing the member functions we might encounter more
20411      classes.  We want to handle them right away, but we don't want
20412      them getting mixed up with functions that are currently in the
20413      queue.  */
20414   push_unparsed_function_queues (parser);
20415
20416   /* Make sure that any template parameters are in scope.  */
20417   maybe_begin_member_template_processing (member_function);
20418
20419   /* If the body of the function has not yet been parsed, parse it
20420      now.  */
20421   if (DECL_PENDING_INLINE_P (member_function))
20422     {
20423       tree function_scope;
20424       cp_token_cache *tokens;
20425
20426       /* The function is no longer pending; we are processing it.  */
20427       tokens = DECL_PENDING_INLINE_INFO (member_function);
20428       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20429       DECL_PENDING_INLINE_P (member_function) = 0;
20430
20431       /* If this is a local class, enter the scope of the containing
20432          function.  */
20433       function_scope = current_function_decl;
20434       if (function_scope)
20435         push_function_context ();
20436
20437       /* Push the body of the function onto the lexer stack.  */
20438       cp_parser_push_lexer_for_tokens (parser, tokens);
20439
20440       /* Let the front end know that we going to be defining this
20441          function.  */
20442       start_preparsed_function (member_function, NULL_TREE,
20443                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20444
20445       /* Don't do access checking if it is a templated function.  */
20446       if (processing_template_decl)
20447         push_deferring_access_checks (dk_no_check);
20448
20449       /* Now, parse the body of the function.  */
20450       cp_parser_function_definition_after_declarator (parser,
20451                                                       /*inline_p=*/true);
20452
20453       if (processing_template_decl)
20454         pop_deferring_access_checks ();
20455
20456       /* Leave the scope of the containing function.  */
20457       if (function_scope)
20458         pop_function_context ();
20459       cp_parser_pop_lexer (parser);
20460     }
20461
20462   /* Remove any template parameters from the symbol table.  */
20463   maybe_end_member_template_processing ();
20464
20465   /* Restore the queue.  */
20466   pop_unparsed_function_queues (parser);
20467   timevar_pop (TV_PARSE_INMETH);
20468 }
20469
20470 /* If DECL contains any default args, remember it on the unparsed
20471    functions queue.  */
20472
20473 static void
20474 cp_parser_save_default_args (cp_parser* parser, tree decl)
20475 {
20476   tree probe;
20477
20478   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20479        probe;
20480        probe = TREE_CHAIN (probe))
20481     if (TREE_PURPOSE (probe))
20482       {
20483         cp_default_arg_entry *entry
20484           = VEC_safe_push (cp_default_arg_entry, gc,
20485                            unparsed_funs_with_default_args, NULL);
20486         entry->class_type = current_class_type;
20487         entry->decl = decl;
20488         break;
20489       }
20490 }
20491
20492 /* FN is a FUNCTION_DECL which may contains a parameter with an
20493    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20494    assumes that the current scope is the scope in which the default
20495    argument should be processed.  */
20496
20497 static void
20498 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20499 {
20500   bool saved_local_variables_forbidden_p;
20501   tree parm, parmdecl;
20502
20503   /* While we're parsing the default args, we might (due to the
20504      statement expression extension) encounter more classes.  We want
20505      to handle them right away, but we don't want them getting mixed
20506      up with default args that are currently in the queue.  */
20507   push_unparsed_function_queues (parser);
20508
20509   /* Local variable names (and the `this' keyword) may not appear
20510      in a default argument.  */
20511   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20512   parser->local_variables_forbidden_p = true;
20513
20514   push_defarg_context (fn);
20515
20516   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20517          parmdecl = DECL_ARGUMENTS (fn);
20518        parm && parm != void_list_node;
20519        parm = TREE_CHAIN (parm),
20520          parmdecl = DECL_CHAIN (parmdecl))
20521     {
20522       cp_token_cache *tokens;
20523       tree default_arg = TREE_PURPOSE (parm);
20524       tree parsed_arg;
20525       VEC(tree,gc) *insts;
20526       tree copy;
20527       unsigned ix;
20528
20529       if (!default_arg)
20530         continue;
20531
20532       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20533         /* This can happen for a friend declaration for a function
20534            already declared with default arguments.  */
20535         continue;
20536
20537        /* Push the saved tokens for the default argument onto the parser's
20538           lexer stack.  */
20539       tokens = DEFARG_TOKENS (default_arg);
20540       cp_parser_push_lexer_for_tokens (parser, tokens);
20541
20542       start_lambda_scope (parmdecl);
20543
20544       /* Parse the assignment-expression.  */
20545       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20546       if (parsed_arg == error_mark_node)
20547         {
20548           cp_parser_pop_lexer (parser);
20549           continue;
20550         }
20551
20552       if (!processing_template_decl)
20553         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20554
20555       TREE_PURPOSE (parm) = parsed_arg;
20556
20557       /* Update any instantiations we've already created.  */
20558       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20559            VEC_iterate (tree, insts, ix, copy); ix++)
20560         TREE_PURPOSE (copy) = parsed_arg;
20561
20562       finish_lambda_scope ();
20563
20564       /* If the token stream has not been completely used up, then
20565          there was extra junk after the end of the default
20566          argument.  */
20567       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20568         cp_parser_error (parser, "expected %<,%>");
20569
20570       /* Revert to the main lexer.  */
20571       cp_parser_pop_lexer (parser);
20572     }
20573
20574   pop_defarg_context ();
20575
20576   /* Make sure no default arg is missing.  */
20577   check_default_args (fn);
20578
20579   /* Restore the state of local_variables_forbidden_p.  */
20580   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20581
20582   /* Restore the queue.  */
20583   pop_unparsed_function_queues (parser);
20584 }
20585
20586 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20587    either a TYPE or an expression, depending on the form of the
20588    input.  The KEYWORD indicates which kind of expression we have
20589    encountered.  */
20590
20591 static tree
20592 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20593 {
20594   tree expr = NULL_TREE;
20595   const char *saved_message;
20596   char *tmp;
20597   bool saved_integral_constant_expression_p;
20598   bool saved_non_integral_constant_expression_p;
20599   bool pack_expansion_p = false;
20600
20601   /* Types cannot be defined in a `sizeof' expression.  Save away the
20602      old message.  */
20603   saved_message = parser->type_definition_forbidden_message;
20604   /* And create the new one.  */
20605   tmp = concat ("types may not be defined in %<",
20606                 IDENTIFIER_POINTER (ridpointers[keyword]),
20607                 "%> expressions", NULL);
20608   parser->type_definition_forbidden_message = tmp;
20609
20610   /* The restrictions on constant-expressions do not apply inside
20611      sizeof expressions.  */
20612   saved_integral_constant_expression_p
20613     = parser->integral_constant_expression_p;
20614   saved_non_integral_constant_expression_p
20615     = parser->non_integral_constant_expression_p;
20616   parser->integral_constant_expression_p = false;
20617
20618   /* If it's a `...', then we are computing the length of a parameter
20619      pack.  */
20620   if (keyword == RID_SIZEOF
20621       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20622     {
20623       /* Consume the `...'.  */
20624       cp_lexer_consume_token (parser->lexer);
20625       maybe_warn_variadic_templates ();
20626
20627       /* Note that this is an expansion.  */
20628       pack_expansion_p = true;
20629     }
20630
20631   /* Do not actually evaluate the expression.  */
20632   ++cp_unevaluated_operand;
20633   ++c_inhibit_evaluation_warnings;
20634   /* If it's a `(', then we might be looking at the type-id
20635      construction.  */
20636   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20637     {
20638       tree type;
20639       bool saved_in_type_id_in_expr_p;
20640
20641       /* We can't be sure yet whether we're looking at a type-id or an
20642          expression.  */
20643       cp_parser_parse_tentatively (parser);
20644       /* Consume the `('.  */
20645       cp_lexer_consume_token (parser->lexer);
20646       /* Parse the type-id.  */
20647       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20648       parser->in_type_id_in_expr_p = true;
20649       type = cp_parser_type_id (parser);
20650       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20651       /* Now, look for the trailing `)'.  */
20652       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20653       /* If all went well, then we're done.  */
20654       if (cp_parser_parse_definitely (parser))
20655         {
20656           cp_decl_specifier_seq decl_specs;
20657
20658           /* Build a trivial decl-specifier-seq.  */
20659           clear_decl_specs (&decl_specs);
20660           decl_specs.type = type;
20661
20662           /* Call grokdeclarator to figure out what type this is.  */
20663           expr = grokdeclarator (NULL,
20664                                  &decl_specs,
20665                                  TYPENAME,
20666                                  /*initialized=*/0,
20667                                  /*attrlist=*/NULL);
20668         }
20669     }
20670
20671   /* If the type-id production did not work out, then we must be
20672      looking at the unary-expression production.  */
20673   if (!expr)
20674     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20675                                        /*cast_p=*/false, NULL);
20676
20677   if (pack_expansion_p)
20678     /* Build a pack expansion. */
20679     expr = make_pack_expansion (expr);
20680
20681   /* Go back to evaluating expressions.  */
20682   --cp_unevaluated_operand;
20683   --c_inhibit_evaluation_warnings;
20684
20685   /* Free the message we created.  */
20686   free (tmp);
20687   /* And restore the old one.  */
20688   parser->type_definition_forbidden_message = saved_message;
20689   parser->integral_constant_expression_p
20690     = saved_integral_constant_expression_p;
20691   parser->non_integral_constant_expression_p
20692     = saved_non_integral_constant_expression_p;
20693
20694   return expr;
20695 }
20696
20697 /* If the current declaration has no declarator, return true.  */
20698
20699 static bool
20700 cp_parser_declares_only_class_p (cp_parser *parser)
20701 {
20702   /* If the next token is a `;' or a `,' then there is no
20703      declarator.  */
20704   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20705           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20706 }
20707
20708 /* Update the DECL_SPECS to reflect the storage class indicated by
20709    KEYWORD.  */
20710
20711 static void
20712 cp_parser_set_storage_class (cp_parser *parser,
20713                              cp_decl_specifier_seq *decl_specs,
20714                              enum rid keyword,
20715                              location_t location)
20716 {
20717   cp_storage_class storage_class;
20718
20719   if (parser->in_unbraced_linkage_specification_p)
20720     {
20721       error_at (location, "invalid use of %qD in linkage specification",
20722                 ridpointers[keyword]);
20723       return;
20724     }
20725   else if (decl_specs->storage_class != sc_none)
20726     {
20727       decl_specs->conflicting_specifiers_p = true;
20728       return;
20729     }
20730
20731   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20732       && decl_specs->specs[(int) ds_thread])
20733     {
20734       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20735       decl_specs->specs[(int) ds_thread] = 0;
20736     }
20737
20738   switch (keyword)
20739     {
20740     case RID_AUTO:
20741       storage_class = sc_auto;
20742       break;
20743     case RID_REGISTER:
20744       storage_class = sc_register;
20745       break;
20746     case RID_STATIC:
20747       storage_class = sc_static;
20748       break;
20749     case RID_EXTERN:
20750       storage_class = sc_extern;
20751       break;
20752     case RID_MUTABLE:
20753       storage_class = sc_mutable;
20754       break;
20755     default:
20756       gcc_unreachable ();
20757     }
20758   decl_specs->storage_class = storage_class;
20759
20760   /* A storage class specifier cannot be applied alongside a typedef 
20761      specifier. If there is a typedef specifier present then set 
20762      conflicting_specifiers_p which will trigger an error later
20763      on in grokdeclarator. */
20764   if (decl_specs->specs[(int)ds_typedef])
20765     decl_specs->conflicting_specifiers_p = true;
20766 }
20767
20768 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20769    is true, the type is a user-defined type; otherwise it is a
20770    built-in type specified by a keyword.  */
20771
20772 static void
20773 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20774                               tree type_spec,
20775                               location_t location,
20776                               bool user_defined_p)
20777 {
20778   decl_specs->any_specifiers_p = true;
20779
20780   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20781      (with, for example, in "typedef int wchar_t;") we remember that
20782      this is what happened.  In system headers, we ignore these
20783      declarations so that G++ can work with system headers that are not
20784      C++-safe.  */
20785   if (decl_specs->specs[(int) ds_typedef]
20786       && !user_defined_p
20787       && (type_spec == boolean_type_node
20788           || type_spec == char16_type_node
20789           || type_spec == char32_type_node
20790           || type_spec == wchar_type_node)
20791       && (decl_specs->type
20792           || decl_specs->specs[(int) ds_long]
20793           || decl_specs->specs[(int) ds_short]
20794           || decl_specs->specs[(int) ds_unsigned]
20795           || decl_specs->specs[(int) ds_signed]))
20796     {
20797       decl_specs->redefined_builtin_type = type_spec;
20798       if (!decl_specs->type)
20799         {
20800           decl_specs->type = type_spec;
20801           decl_specs->user_defined_type_p = false;
20802           decl_specs->type_location = location;
20803         }
20804     }
20805   else if (decl_specs->type)
20806     decl_specs->multiple_types_p = true;
20807   else
20808     {
20809       decl_specs->type = type_spec;
20810       decl_specs->user_defined_type_p = user_defined_p;
20811       decl_specs->redefined_builtin_type = NULL_TREE;
20812       decl_specs->type_location = location;
20813     }
20814 }
20815
20816 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20817    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20818
20819 static bool
20820 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20821 {
20822   return decl_specifiers->specs[(int) ds_friend] != 0;
20823 }
20824
20825 /* Issue an error message indicating that TOKEN_DESC was expected.
20826    If KEYWORD is true, it indicated this function is called by
20827    cp_parser_require_keword and the required token can only be
20828    a indicated keyword. */
20829
20830 static void
20831 cp_parser_required_error (cp_parser *parser,
20832                           required_token token_desc,
20833                           bool keyword)
20834 {
20835   switch (token_desc)
20836     {
20837       case RT_NEW:
20838         cp_parser_error (parser, "expected %<new%>");
20839         return;
20840       case RT_DELETE:
20841         cp_parser_error (parser, "expected %<delete%>");
20842         return;
20843       case RT_RETURN:
20844         cp_parser_error (parser, "expected %<return%>");
20845         return;
20846       case RT_WHILE:
20847         cp_parser_error (parser, "expected %<while%>");
20848         return;
20849       case RT_EXTERN:
20850         cp_parser_error (parser, "expected %<extern%>");
20851         return;
20852       case RT_STATIC_ASSERT:
20853         cp_parser_error (parser, "expected %<static_assert%>");
20854         return;
20855       case RT_DECLTYPE:
20856         cp_parser_error (parser, "expected %<decltype%>");
20857         return;
20858       case RT_OPERATOR:
20859         cp_parser_error (parser, "expected %<operator%>");
20860         return;
20861       case RT_CLASS:
20862         cp_parser_error (parser, "expected %<class%>");
20863         return;
20864       case RT_TEMPLATE:
20865         cp_parser_error (parser, "expected %<template%>");
20866         return;
20867       case RT_NAMESPACE:
20868         cp_parser_error (parser, "expected %<namespace%>");
20869         return;
20870       case RT_USING:
20871         cp_parser_error (parser, "expected %<using%>");
20872         return;
20873       case RT_ASM:
20874         cp_parser_error (parser, "expected %<asm%>");
20875         return;
20876       case RT_TRY:
20877         cp_parser_error (parser, "expected %<try%>");
20878         return;
20879       case RT_CATCH:
20880         cp_parser_error (parser, "expected %<catch%>");
20881         return;
20882       case RT_THROW:
20883         cp_parser_error (parser, "expected %<throw%>");
20884         return;
20885       case RT_LABEL:
20886         cp_parser_error (parser, "expected %<__label__%>");
20887         return;
20888       case RT_AT_TRY:
20889         cp_parser_error (parser, "expected %<@try%>");
20890         return;
20891       case RT_AT_SYNCHRONIZED:
20892         cp_parser_error (parser, "expected %<@synchronized%>");
20893         return;
20894       case RT_AT_THROW:
20895         cp_parser_error (parser, "expected %<@throw%>");
20896         return;
20897       default:
20898         break;
20899     }
20900   if (!keyword)
20901     {
20902       switch (token_desc)
20903         {
20904           case RT_SEMICOLON:
20905             cp_parser_error (parser, "expected %<;%>");
20906             return;
20907           case RT_OPEN_PAREN:
20908             cp_parser_error (parser, "expected %<(%>");
20909             return;
20910           case RT_CLOSE_BRACE:
20911             cp_parser_error (parser, "expected %<}%>");
20912             return;
20913           case RT_OPEN_BRACE:
20914             cp_parser_error (parser, "expected %<{%>");
20915             return;
20916           case RT_CLOSE_SQUARE:
20917             cp_parser_error (parser, "expected %<]%>");
20918             return;
20919           case RT_OPEN_SQUARE:
20920             cp_parser_error (parser, "expected %<[%>");
20921             return;
20922           case RT_COMMA:
20923             cp_parser_error (parser, "expected %<,%>");
20924             return;
20925           case RT_SCOPE:
20926             cp_parser_error (parser, "expected %<::%>");
20927             return;
20928           case RT_LESS:
20929             cp_parser_error (parser, "expected %<<%>");
20930             return;
20931           case RT_GREATER:
20932             cp_parser_error (parser, "expected %<>%>");
20933             return;
20934           case RT_EQ:
20935             cp_parser_error (parser, "expected %<=%>");
20936             return;
20937           case RT_ELLIPSIS:
20938             cp_parser_error (parser, "expected %<...%>");
20939             return;
20940           case RT_MULT:
20941             cp_parser_error (parser, "expected %<*%>");
20942             return;
20943           case RT_COMPL:
20944             cp_parser_error (parser, "expected %<~%>");
20945             return;
20946           case RT_COLON:
20947             cp_parser_error (parser, "expected %<:%>");
20948             return;
20949           case RT_COLON_SCOPE:
20950             cp_parser_error (parser, "expected %<:%> or %<::%>");
20951             return;
20952           case RT_CLOSE_PAREN:
20953             cp_parser_error (parser, "expected %<)%>");
20954             return;
20955           case RT_COMMA_CLOSE_PAREN:
20956             cp_parser_error (parser, "expected %<,%> or %<)%>");
20957             return;
20958           case RT_PRAGMA_EOL:
20959             cp_parser_error (parser, "expected end of line");
20960             return;
20961           case RT_NAME:
20962             cp_parser_error (parser, "expected identifier");
20963             return;
20964           case RT_SELECT:
20965             cp_parser_error (parser, "expected selection-statement");
20966             return;
20967           case RT_INTERATION:
20968             cp_parser_error (parser, "expected iteration-statement");
20969             return;
20970           case RT_JUMP:
20971             cp_parser_error (parser, "expected jump-statement");
20972             return;
20973           case RT_CLASS_KEY:
20974             cp_parser_error (parser, "expected class-key");
20975             return;
20976           case RT_CLASS_TYPENAME_TEMPLATE:
20977             cp_parser_error (parser,
20978                  "expected %<class%>, %<typename%>, or %<template%>");
20979             return;
20980           default:
20981             gcc_unreachable ();
20982         }
20983     }
20984   else
20985     gcc_unreachable ();
20986 }
20987
20988
20989
20990 /* If the next token is of the indicated TYPE, 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 (cp_parser* parser,
20998                    enum cpp_ttype type,
20999                    required_token token_desc)
21000 {
21001   if (cp_lexer_next_token_is (parser->lexer, type))
21002     return cp_lexer_consume_token (parser->lexer);
21003   else
21004     {
21005       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21006       if (!cp_parser_simulate_error (parser))
21007         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21008       return NULL;
21009     }
21010 }
21011
21012 /* An error message is produced if the next token is not '>'.
21013    All further tokens are skipped until the desired token is
21014    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21015
21016 static void
21017 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21018 {
21019   /* Current level of '< ... >'.  */
21020   unsigned level = 0;
21021   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21022   unsigned nesting_depth = 0;
21023
21024   /* Are we ready, yet?  If not, issue error message.  */
21025   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21026     return;
21027
21028   /* Skip tokens until the desired token is found.  */
21029   while (true)
21030     {
21031       /* Peek at the next token.  */
21032       switch (cp_lexer_peek_token (parser->lexer)->type)
21033         {
21034         case CPP_LESS:
21035           if (!nesting_depth)
21036             ++level;
21037           break;
21038
21039         case CPP_RSHIFT:
21040           if (cxx_dialect == cxx98)
21041             /* C++0x views the `>>' operator as two `>' tokens, but
21042                C++98 does not. */
21043             break;
21044           else if (!nesting_depth && level-- == 0)
21045             {
21046               /* We've hit a `>>' where the first `>' closes the
21047                  template argument list, and the second `>' is
21048                  spurious.  Just consume the `>>' and stop; we've
21049                  already produced at least one error.  */
21050               cp_lexer_consume_token (parser->lexer);
21051               return;
21052             }
21053           /* Fall through for C++0x, so we handle the second `>' in
21054              the `>>'.  */
21055
21056         case CPP_GREATER:
21057           if (!nesting_depth && level-- == 0)
21058             {
21059               /* We've reached the token we want, consume it and stop.  */
21060               cp_lexer_consume_token (parser->lexer);
21061               return;
21062             }
21063           break;
21064
21065         case CPP_OPEN_PAREN:
21066         case CPP_OPEN_SQUARE:
21067           ++nesting_depth;
21068           break;
21069
21070         case CPP_CLOSE_PAREN:
21071         case CPP_CLOSE_SQUARE:
21072           if (nesting_depth-- == 0)
21073             return;
21074           break;
21075
21076         case CPP_EOF:
21077         case CPP_PRAGMA_EOL:
21078         case CPP_SEMICOLON:
21079         case CPP_OPEN_BRACE:
21080         case CPP_CLOSE_BRACE:
21081           /* The '>' was probably forgotten, don't look further.  */
21082           return;
21083
21084         default:
21085           break;
21086         }
21087
21088       /* Consume this token.  */
21089       cp_lexer_consume_token (parser->lexer);
21090     }
21091 }
21092
21093 /* If the next token is the indicated keyword, consume it.  Otherwise,
21094    issue an error message indicating that TOKEN_DESC was expected.
21095
21096    Returns the token consumed, if the token had the appropriate type.
21097    Otherwise, returns NULL.  */
21098
21099 static cp_token *
21100 cp_parser_require_keyword (cp_parser* parser,
21101                            enum rid keyword,
21102                            required_token token_desc)
21103 {
21104   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21105
21106   if (token && token->keyword != keyword)
21107     {
21108       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21109       return NULL;
21110     }
21111
21112   return token;
21113 }
21114
21115 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21116    function-definition.  */
21117
21118 static bool
21119 cp_parser_token_starts_function_definition_p (cp_token* token)
21120 {
21121   return (/* An ordinary function-body begins with an `{'.  */
21122           token->type == CPP_OPEN_BRACE
21123           /* A ctor-initializer begins with a `:'.  */
21124           || token->type == CPP_COLON
21125           /* A function-try-block begins with `try'.  */
21126           || token->keyword == RID_TRY
21127           /* The named return value extension begins with `return'.  */
21128           || token->keyword == RID_RETURN);
21129 }
21130
21131 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21132    definition.  */
21133
21134 static bool
21135 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21136 {
21137   cp_token *token;
21138
21139   token = cp_lexer_peek_token (parser->lexer);
21140   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21141 }
21142
21143 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21144    C++0x) ending a template-argument.  */
21145
21146 static bool
21147 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21148 {
21149   cp_token *token;
21150
21151   token = cp_lexer_peek_token (parser->lexer);
21152   return (token->type == CPP_COMMA 
21153           || token->type == CPP_GREATER
21154           || token->type == CPP_ELLIPSIS
21155           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21156 }
21157
21158 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21159    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21160
21161 static bool
21162 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21163                                                      size_t n)
21164 {
21165   cp_token *token;
21166
21167   token = cp_lexer_peek_nth_token (parser->lexer, n);
21168   if (token->type == CPP_LESS)
21169     return true;
21170   /* Check for the sequence `<::' in the original code. It would be lexed as
21171      `[:', where `[' is a digraph, and there is no whitespace before
21172      `:'.  */
21173   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21174     {
21175       cp_token *token2;
21176       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21177       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21178         return true;
21179     }
21180   return false;
21181 }
21182
21183 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21184    or none_type otherwise.  */
21185
21186 static enum tag_types
21187 cp_parser_token_is_class_key (cp_token* token)
21188 {
21189   switch (token->keyword)
21190     {
21191     case RID_CLASS:
21192       return class_type;
21193     case RID_STRUCT:
21194       return record_type;
21195     case RID_UNION:
21196       return union_type;
21197
21198     default:
21199       return none_type;
21200     }
21201 }
21202
21203 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21204
21205 static void
21206 cp_parser_check_class_key (enum tag_types class_key, tree type)
21207 {
21208   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21209     permerror (input_location, "%qs tag used in naming %q#T",
21210             class_key == union_type ? "union"
21211              : class_key == record_type ? "struct" : "class",
21212              type);
21213 }
21214
21215 /* Issue an error message if DECL is redeclared with different
21216    access than its original declaration [class.access.spec/3].
21217    This applies to nested classes and nested class templates.
21218    [class.mem/1].  */
21219
21220 static void
21221 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21222 {
21223   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21224     return;
21225
21226   if ((TREE_PRIVATE (decl)
21227        != (current_access_specifier == access_private_node))
21228       || (TREE_PROTECTED (decl)
21229           != (current_access_specifier == access_protected_node)))
21230     error_at (location, "%qD redeclared with different access", decl);
21231 }
21232
21233 /* Look for the `template' keyword, as a syntactic disambiguator.
21234    Return TRUE iff it is present, in which case it will be
21235    consumed.  */
21236
21237 static bool
21238 cp_parser_optional_template_keyword (cp_parser *parser)
21239 {
21240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21241     {
21242       /* The `template' keyword can only be used within templates;
21243          outside templates the parser can always figure out what is a
21244          template and what is not.  */
21245       if (!processing_template_decl)
21246         {
21247           cp_token *token = cp_lexer_peek_token (parser->lexer);
21248           error_at (token->location,
21249                     "%<template%> (as a disambiguator) is only allowed "
21250                     "within templates");
21251           /* If this part of the token stream is rescanned, the same
21252              error message would be generated.  So, we purge the token
21253              from the stream.  */
21254           cp_lexer_purge_token (parser->lexer);
21255           return false;
21256         }
21257       else
21258         {
21259           /* Consume the `template' keyword.  */
21260           cp_lexer_consume_token (parser->lexer);
21261           return true;
21262         }
21263     }
21264
21265   return false;
21266 }
21267
21268 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21269    set PARSER->SCOPE, and perform other related actions.  */
21270
21271 static void
21272 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21273 {
21274   int i;
21275   struct tree_check *check_value;
21276   deferred_access_check *chk;
21277   VEC (deferred_access_check,gc) *checks;
21278
21279   /* Get the stored value.  */
21280   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21281   /* Perform any access checks that were deferred.  */
21282   checks = check_value->checks;
21283   if (checks)
21284     {
21285       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21286         perform_or_defer_access_check (chk->binfo,
21287                                        chk->decl,
21288                                        chk->diag_decl);
21289     }
21290   /* Set the scope from the stored value.  */
21291   parser->scope = check_value->value;
21292   parser->qualifying_scope = check_value->qualifying_scope;
21293   parser->object_scope = NULL_TREE;
21294 }
21295
21296 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21297    encounter the end of a block before what we were looking for.  */
21298
21299 static bool
21300 cp_parser_cache_group (cp_parser *parser,
21301                        enum cpp_ttype end,
21302                        unsigned depth)
21303 {
21304   while (true)
21305     {
21306       cp_token *token = cp_lexer_peek_token (parser->lexer);
21307
21308       /* Abort a parenthesized expression if we encounter a semicolon.  */
21309       if ((end == CPP_CLOSE_PAREN || depth == 0)
21310           && token->type == CPP_SEMICOLON)
21311         return true;
21312       /* If we've reached the end of the file, stop.  */
21313       if (token->type == CPP_EOF
21314           || (end != CPP_PRAGMA_EOL
21315               && token->type == CPP_PRAGMA_EOL))
21316         return true;
21317       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21318         /* We've hit the end of an enclosing block, so there's been some
21319            kind of syntax error.  */
21320         return true;
21321
21322       /* Consume the token.  */
21323       cp_lexer_consume_token (parser->lexer);
21324       /* See if it starts a new group.  */
21325       if (token->type == CPP_OPEN_BRACE)
21326         {
21327           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21328           /* In theory this should probably check end == '}', but
21329              cp_parser_save_member_function_body needs it to exit
21330              after either '}' or ')' when called with ')'.  */
21331           if (depth == 0)
21332             return false;
21333         }
21334       else if (token->type == CPP_OPEN_PAREN)
21335         {
21336           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21337           if (depth == 0 && end == CPP_CLOSE_PAREN)
21338             return false;
21339         }
21340       else if (token->type == CPP_PRAGMA)
21341         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21342       else if (token->type == end)
21343         return false;
21344     }
21345 }
21346
21347 /* Begin parsing tentatively.  We always save tokens while parsing
21348    tentatively so that if the tentative parsing fails we can restore the
21349    tokens.  */
21350
21351 static void
21352 cp_parser_parse_tentatively (cp_parser* parser)
21353 {
21354   /* Enter a new parsing context.  */
21355   parser->context = cp_parser_context_new (parser->context);
21356   /* Begin saving tokens.  */
21357   cp_lexer_save_tokens (parser->lexer);
21358   /* In order to avoid repetitive access control error messages,
21359      access checks are queued up until we are no longer parsing
21360      tentatively.  */
21361   push_deferring_access_checks (dk_deferred);
21362 }
21363
21364 /* Commit to the currently active tentative parse.  */
21365
21366 static void
21367 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21368 {
21369   cp_parser_context *context;
21370   cp_lexer *lexer;
21371
21372   /* Mark all of the levels as committed.  */
21373   lexer = parser->lexer;
21374   for (context = parser->context; context->next; context = context->next)
21375     {
21376       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21377         break;
21378       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21379       while (!cp_lexer_saving_tokens (lexer))
21380         lexer = lexer->next;
21381       cp_lexer_commit_tokens (lexer);
21382     }
21383 }
21384
21385 /* Abort the currently active tentative parse.  All consumed tokens
21386    will be rolled back, and no diagnostics will be issued.  */
21387
21388 static void
21389 cp_parser_abort_tentative_parse (cp_parser* parser)
21390 {
21391   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21392               || errorcount > 0);
21393   cp_parser_simulate_error (parser);
21394   /* Now, pretend that we want to see if the construct was
21395      successfully parsed.  */
21396   cp_parser_parse_definitely (parser);
21397 }
21398
21399 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21400    token stream.  Otherwise, commit to the tokens we have consumed.
21401    Returns true if no error occurred; false otherwise.  */
21402
21403 static bool
21404 cp_parser_parse_definitely (cp_parser* parser)
21405 {
21406   bool error_occurred;
21407   cp_parser_context *context;
21408
21409   /* Remember whether or not an error occurred, since we are about to
21410      destroy that information.  */
21411   error_occurred = cp_parser_error_occurred (parser);
21412   /* Remove the topmost context from the stack.  */
21413   context = parser->context;
21414   parser->context = context->next;
21415   /* If no parse errors occurred, commit to the tentative parse.  */
21416   if (!error_occurred)
21417     {
21418       /* Commit to the tokens read tentatively, unless that was
21419          already done.  */
21420       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21421         cp_lexer_commit_tokens (parser->lexer);
21422
21423       pop_to_parent_deferring_access_checks ();
21424     }
21425   /* Otherwise, if errors occurred, roll back our state so that things
21426      are just as they were before we began the tentative parse.  */
21427   else
21428     {
21429       cp_lexer_rollback_tokens (parser->lexer);
21430       pop_deferring_access_checks ();
21431     }
21432   /* Add the context to the front of the free list.  */
21433   context->next = cp_parser_context_free_list;
21434   cp_parser_context_free_list = context;
21435
21436   return !error_occurred;
21437 }
21438
21439 /* Returns true if we are parsing tentatively and are not committed to
21440    this tentative parse.  */
21441
21442 static bool
21443 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21444 {
21445   return (cp_parser_parsing_tentatively (parser)
21446           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21447 }
21448
21449 /* Returns nonzero iff an error has occurred during the most recent
21450    tentative parse.  */
21451
21452 static bool
21453 cp_parser_error_occurred (cp_parser* parser)
21454 {
21455   return (cp_parser_parsing_tentatively (parser)
21456           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21457 }
21458
21459 /* Returns nonzero if GNU extensions are allowed.  */
21460
21461 static bool
21462 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21463 {
21464   return parser->allow_gnu_extensions_p;
21465 }
21466 \f
21467 /* Objective-C++ Productions */
21468
21469
21470 /* Parse an Objective-C expression, which feeds into a primary-expression
21471    above.
21472
21473    objc-expression:
21474      objc-message-expression
21475      objc-string-literal
21476      objc-encode-expression
21477      objc-protocol-expression
21478      objc-selector-expression
21479
21480   Returns a tree representation of the expression.  */
21481
21482 static tree
21483 cp_parser_objc_expression (cp_parser* parser)
21484 {
21485   /* Try to figure out what kind of declaration is present.  */
21486   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21487
21488   switch (kwd->type)
21489     {
21490     case CPP_OPEN_SQUARE:
21491       return cp_parser_objc_message_expression (parser);
21492
21493     case CPP_OBJC_STRING:
21494       kwd = cp_lexer_consume_token (parser->lexer);
21495       return objc_build_string_object (kwd->u.value);
21496
21497     case CPP_KEYWORD:
21498       switch (kwd->keyword)
21499         {
21500         case RID_AT_ENCODE:
21501           return cp_parser_objc_encode_expression (parser);
21502
21503         case RID_AT_PROTOCOL:
21504           return cp_parser_objc_protocol_expression (parser);
21505
21506         case RID_AT_SELECTOR:
21507           return cp_parser_objc_selector_expression (parser);
21508
21509         default:
21510           break;
21511         }
21512     default:
21513       error_at (kwd->location,
21514                 "misplaced %<@%D%> Objective-C++ construct",
21515                 kwd->u.value);
21516       cp_parser_skip_to_end_of_block_or_statement (parser);
21517     }
21518
21519   return error_mark_node;
21520 }
21521
21522 /* Parse an Objective-C message expression.
21523
21524    objc-message-expression:
21525      [ objc-message-receiver objc-message-args ]
21526
21527    Returns a representation of an Objective-C message.  */
21528
21529 static tree
21530 cp_parser_objc_message_expression (cp_parser* parser)
21531 {
21532   tree receiver, messageargs;
21533
21534   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21535   receiver = cp_parser_objc_message_receiver (parser);
21536   messageargs = cp_parser_objc_message_args (parser);
21537   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21538
21539   return objc_build_message_expr (receiver, messageargs);
21540 }
21541
21542 /* Parse an objc-message-receiver.
21543
21544    objc-message-receiver:
21545      expression
21546      simple-type-specifier
21547
21548   Returns a representation of the type or expression.  */
21549
21550 static tree
21551 cp_parser_objc_message_receiver (cp_parser* parser)
21552 {
21553   tree rcv;
21554
21555   /* An Objective-C message receiver may be either (1) a type
21556      or (2) an expression.  */
21557   cp_parser_parse_tentatively (parser);
21558   rcv = cp_parser_expression (parser, false, NULL);
21559
21560   if (cp_parser_parse_definitely (parser))
21561     return rcv;
21562
21563   rcv = cp_parser_simple_type_specifier (parser,
21564                                          /*decl_specs=*/NULL,
21565                                          CP_PARSER_FLAGS_NONE);
21566
21567   return objc_get_class_reference (rcv);
21568 }
21569
21570 /* Parse the arguments and selectors comprising an Objective-C message.
21571
21572    objc-message-args:
21573      objc-selector
21574      objc-selector-args
21575      objc-selector-args , objc-comma-args
21576
21577    objc-selector-args:
21578      objc-selector [opt] : assignment-expression
21579      objc-selector-args objc-selector [opt] : assignment-expression
21580
21581    objc-comma-args:
21582      assignment-expression
21583      objc-comma-args , assignment-expression
21584
21585    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21586    selector arguments and TREE_VALUE containing a list of comma
21587    arguments.  */
21588
21589 static tree
21590 cp_parser_objc_message_args (cp_parser* parser)
21591 {
21592   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21593   bool maybe_unary_selector_p = true;
21594   cp_token *token = cp_lexer_peek_token (parser->lexer);
21595
21596   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21597     {
21598       tree selector = NULL_TREE, arg;
21599
21600       if (token->type != CPP_COLON)
21601         selector = cp_parser_objc_selector (parser);
21602
21603       /* Detect if we have a unary selector.  */
21604       if (maybe_unary_selector_p
21605           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21606         return build_tree_list (selector, NULL_TREE);
21607
21608       maybe_unary_selector_p = false;
21609       cp_parser_require (parser, CPP_COLON, RT_COLON);
21610       arg = cp_parser_assignment_expression (parser, false, NULL);
21611
21612       sel_args
21613         = chainon (sel_args,
21614                    build_tree_list (selector, arg));
21615
21616       token = cp_lexer_peek_token (parser->lexer);
21617     }
21618
21619   /* Handle non-selector arguments, if any. */
21620   while (token->type == CPP_COMMA)
21621     {
21622       tree arg;
21623
21624       cp_lexer_consume_token (parser->lexer);
21625       arg = cp_parser_assignment_expression (parser, false, NULL);
21626
21627       addl_args
21628         = chainon (addl_args,
21629                    build_tree_list (NULL_TREE, arg));
21630
21631       token = cp_lexer_peek_token (parser->lexer);
21632     }
21633
21634   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21635     {
21636       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21637       return build_tree_list (error_mark_node, error_mark_node);
21638     }
21639
21640   return build_tree_list (sel_args, addl_args);
21641 }
21642
21643 /* Parse an Objective-C encode expression.
21644
21645    objc-encode-expression:
21646      @encode objc-typename
21647
21648    Returns an encoded representation of the type argument.  */
21649
21650 static tree
21651 cp_parser_objc_encode_expression (cp_parser* parser)
21652 {
21653   tree type;
21654   cp_token *token;
21655
21656   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21657   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21658   token = cp_lexer_peek_token (parser->lexer);
21659   type = complete_type (cp_parser_type_id (parser));
21660   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21661
21662   if (!type)
21663     {
21664       error_at (token->location, 
21665                 "%<@encode%> must specify a type as an argument");
21666       return error_mark_node;
21667     }
21668
21669   /* This happens if we find @encode(T) (where T is a template
21670      typename or something dependent on a template typename) when
21671      parsing a template.  In that case, we can't compile it
21672      immediately, but we rather create an AT_ENCODE_EXPR which will
21673      need to be instantiated when the template is used.
21674   */
21675   if (dependent_type_p (type))
21676     {
21677       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21678       TREE_READONLY (value) = 1;
21679       return value;
21680     }
21681
21682   return objc_build_encode_expr (type);
21683 }
21684
21685 /* Parse an Objective-C @defs expression.  */
21686
21687 static tree
21688 cp_parser_objc_defs_expression (cp_parser *parser)
21689 {
21690   tree name;
21691
21692   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21693   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21694   name = cp_parser_identifier (parser);
21695   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21696
21697   return objc_get_class_ivars (name);
21698 }
21699
21700 /* Parse an Objective-C protocol expression.
21701
21702   objc-protocol-expression:
21703     @protocol ( identifier )
21704
21705   Returns a representation of the protocol expression.  */
21706
21707 static tree
21708 cp_parser_objc_protocol_expression (cp_parser* parser)
21709 {
21710   tree proto;
21711
21712   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21713   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21714   proto = cp_parser_identifier (parser);
21715   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21716
21717   return objc_build_protocol_expr (proto);
21718 }
21719
21720 /* Parse an Objective-C selector expression.
21721
21722    objc-selector-expression:
21723      @selector ( objc-method-signature )
21724
21725    objc-method-signature:
21726      objc-selector
21727      objc-selector-seq
21728
21729    objc-selector-seq:
21730      objc-selector :
21731      objc-selector-seq objc-selector :
21732
21733   Returns a representation of the method selector.  */
21734
21735 static tree
21736 cp_parser_objc_selector_expression (cp_parser* parser)
21737 {
21738   tree sel_seq = NULL_TREE;
21739   bool maybe_unary_selector_p = true;
21740   cp_token *token;
21741   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21742
21743   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21744   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21745   token = cp_lexer_peek_token (parser->lexer);
21746
21747   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21748          || token->type == CPP_SCOPE)
21749     {
21750       tree selector = NULL_TREE;
21751
21752       if (token->type != CPP_COLON
21753           || token->type == CPP_SCOPE)
21754         selector = cp_parser_objc_selector (parser);
21755
21756       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21757           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21758         {
21759           /* Detect if we have a unary selector.  */
21760           if (maybe_unary_selector_p)
21761             {
21762               sel_seq = selector;
21763               goto finish_selector;
21764             }
21765           else
21766             {
21767               cp_parser_error (parser, "expected %<:%>");
21768             }
21769         }
21770       maybe_unary_selector_p = false;
21771       token = cp_lexer_consume_token (parser->lexer);
21772
21773       if (token->type == CPP_SCOPE)
21774         {
21775           sel_seq
21776             = chainon (sel_seq,
21777                        build_tree_list (selector, NULL_TREE));
21778           sel_seq
21779             = chainon (sel_seq,
21780                        build_tree_list (NULL_TREE, NULL_TREE));
21781         }
21782       else
21783         sel_seq
21784           = chainon (sel_seq,
21785                      build_tree_list (selector, NULL_TREE));
21786
21787       token = cp_lexer_peek_token (parser->lexer);
21788     }
21789
21790  finish_selector:
21791   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21792
21793   return objc_build_selector_expr (loc, sel_seq);
21794 }
21795
21796 /* Parse a list of identifiers.
21797
21798    objc-identifier-list:
21799      identifier
21800      objc-identifier-list , identifier
21801
21802    Returns a TREE_LIST of identifier nodes.  */
21803
21804 static tree
21805 cp_parser_objc_identifier_list (cp_parser* parser)
21806 {
21807   tree identifier;
21808   tree list;
21809   cp_token *sep;
21810
21811   identifier = cp_parser_identifier (parser);
21812   if (identifier == error_mark_node)
21813     return error_mark_node;      
21814
21815   list = build_tree_list (NULL_TREE, identifier);
21816   sep = cp_lexer_peek_token (parser->lexer);
21817
21818   while (sep->type == CPP_COMMA)
21819     {
21820       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21821       identifier = cp_parser_identifier (parser);
21822       if (identifier == error_mark_node)
21823         return list;
21824
21825       list = chainon (list, build_tree_list (NULL_TREE,
21826                                              identifier));
21827       sep = cp_lexer_peek_token (parser->lexer);
21828     }
21829   
21830   return list;
21831 }
21832
21833 /* Parse an Objective-C alias declaration.
21834
21835    objc-alias-declaration:
21836      @compatibility_alias identifier identifier ;
21837
21838    This function registers the alias mapping with the Objective-C front end.
21839    It returns nothing.  */
21840
21841 static void
21842 cp_parser_objc_alias_declaration (cp_parser* parser)
21843 {
21844   tree alias, orig;
21845
21846   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21847   alias = cp_parser_identifier (parser);
21848   orig = cp_parser_identifier (parser);
21849   objc_declare_alias (alias, orig);
21850   cp_parser_consume_semicolon_at_end_of_statement (parser);
21851 }
21852
21853 /* Parse an Objective-C class forward-declaration.
21854
21855    objc-class-declaration:
21856      @class objc-identifier-list ;
21857
21858    The function registers the forward declarations with the Objective-C
21859    front end.  It returns nothing.  */
21860
21861 static void
21862 cp_parser_objc_class_declaration (cp_parser* parser)
21863 {
21864   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21865   while (true)
21866     {
21867       tree id;
21868       
21869       id = cp_parser_identifier (parser);
21870       if (id == error_mark_node)
21871         break;
21872       
21873       objc_declare_class (id);
21874
21875       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21876         cp_lexer_consume_token (parser->lexer);
21877       else
21878         break;
21879     }
21880   cp_parser_consume_semicolon_at_end_of_statement (parser);
21881 }
21882
21883 /* Parse a list of Objective-C protocol references.
21884
21885    objc-protocol-refs-opt:
21886      objc-protocol-refs [opt]
21887
21888    objc-protocol-refs:
21889      < objc-identifier-list >
21890
21891    Returns a TREE_LIST of identifiers, if any.  */
21892
21893 static tree
21894 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21895 {
21896   tree protorefs = NULL_TREE;
21897
21898   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21899     {
21900       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21901       protorefs = cp_parser_objc_identifier_list (parser);
21902       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21903     }
21904
21905   return protorefs;
21906 }
21907
21908 /* Parse a Objective-C visibility specification.  */
21909
21910 static void
21911 cp_parser_objc_visibility_spec (cp_parser* parser)
21912 {
21913   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21914
21915   switch (vis->keyword)
21916     {
21917     case RID_AT_PRIVATE:
21918       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21919       break;
21920     case RID_AT_PROTECTED:
21921       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21922       break;
21923     case RID_AT_PUBLIC:
21924       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21925       break;
21926     case RID_AT_PACKAGE:
21927       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21928       break;
21929     default:
21930       return;
21931     }
21932
21933   /* Eat '@private'/'@protected'/'@public'.  */
21934   cp_lexer_consume_token (parser->lexer);
21935 }
21936
21937 /* Parse an Objective-C method type.  Return 'true' if it is a class
21938    (+) method, and 'false' if it is an instance (-) method.  */
21939
21940 static inline bool
21941 cp_parser_objc_method_type (cp_parser* parser)
21942 {
21943   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21944     return true;
21945   else
21946     return false;
21947 }
21948
21949 /* Parse an Objective-C protocol qualifier.  */
21950
21951 static tree
21952 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21953 {
21954   tree quals = NULL_TREE, node;
21955   cp_token *token = cp_lexer_peek_token (parser->lexer);
21956
21957   node = token->u.value;
21958
21959   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21960          && (node == ridpointers [(int) RID_IN]
21961              || node == ridpointers [(int) RID_OUT]
21962              || node == ridpointers [(int) RID_INOUT]
21963              || node == ridpointers [(int) RID_BYCOPY]
21964              || node == ridpointers [(int) RID_BYREF]
21965              || node == ridpointers [(int) RID_ONEWAY]))
21966     {
21967       quals = tree_cons (NULL_TREE, node, quals);
21968       cp_lexer_consume_token (parser->lexer);
21969       token = cp_lexer_peek_token (parser->lexer);
21970       node = token->u.value;
21971     }
21972
21973   return quals;
21974 }
21975
21976 /* Parse an Objective-C typename.  */
21977
21978 static tree
21979 cp_parser_objc_typename (cp_parser* parser)
21980 {
21981   tree type_name = NULL_TREE;
21982
21983   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21984     {
21985       tree proto_quals, cp_type = NULL_TREE;
21986
21987       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21988       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21989
21990       /* An ObjC type name may consist of just protocol qualifiers, in which
21991          case the type shall default to 'id'.  */
21992       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21993         {
21994           cp_type = cp_parser_type_id (parser);
21995           
21996           /* If the type could not be parsed, an error has already
21997              been produced.  For error recovery, behave as if it had
21998              not been specified, which will use the default type
21999              'id'.  */
22000           if (cp_type == error_mark_node)
22001             {
22002               cp_type = NULL_TREE;
22003               /* We need to skip to the closing parenthesis as
22004                  cp_parser_type_id() does not seem to do it for
22005                  us.  */
22006               cp_parser_skip_to_closing_parenthesis (parser,
22007                                                      /*recovering=*/true,
22008                                                      /*or_comma=*/false,
22009                                                      /*consume_paren=*/false);
22010             }
22011         }
22012
22013       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22014       type_name = build_tree_list (proto_quals, cp_type);
22015     }
22016
22017   return type_name;
22018 }
22019
22020 /* Check to see if TYPE refers to an Objective-C selector name.  */
22021
22022 static bool
22023 cp_parser_objc_selector_p (enum cpp_ttype type)
22024 {
22025   return (type == CPP_NAME || type == CPP_KEYWORD
22026           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22027           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22028           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22029           || type == CPP_XOR || type == CPP_XOR_EQ);
22030 }
22031
22032 /* Parse an Objective-C selector.  */
22033
22034 static tree
22035 cp_parser_objc_selector (cp_parser* parser)
22036 {
22037   cp_token *token = cp_lexer_consume_token (parser->lexer);
22038
22039   if (!cp_parser_objc_selector_p (token->type))
22040     {
22041       error_at (token->location, "invalid Objective-C++ selector name");
22042       return error_mark_node;
22043     }
22044
22045   /* C++ operator names are allowed to appear in ObjC selectors.  */
22046   switch (token->type)
22047     {
22048     case CPP_AND_AND: return get_identifier ("and");
22049     case CPP_AND_EQ: return get_identifier ("and_eq");
22050     case CPP_AND: return get_identifier ("bitand");
22051     case CPP_OR: return get_identifier ("bitor");
22052     case CPP_COMPL: return get_identifier ("compl");
22053     case CPP_NOT: return get_identifier ("not");
22054     case CPP_NOT_EQ: return get_identifier ("not_eq");
22055     case CPP_OR_OR: return get_identifier ("or");
22056     case CPP_OR_EQ: return get_identifier ("or_eq");
22057     case CPP_XOR: return get_identifier ("xor");
22058     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22059     default: return token->u.value;
22060     }
22061 }
22062
22063 /* Parse an Objective-C params list.  */
22064
22065 static tree
22066 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22067 {
22068   tree params = NULL_TREE;
22069   bool maybe_unary_selector_p = true;
22070   cp_token *token = cp_lexer_peek_token (parser->lexer);
22071
22072   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22073     {
22074       tree selector = NULL_TREE, type_name, identifier;
22075       tree parm_attr = NULL_TREE;
22076
22077       if (token->keyword == RID_ATTRIBUTE)
22078         break;
22079
22080       if (token->type != CPP_COLON)
22081         selector = cp_parser_objc_selector (parser);
22082
22083       /* Detect if we have a unary selector.  */
22084       if (maybe_unary_selector_p
22085           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22086         {
22087           params = selector; /* Might be followed by attributes.  */
22088           break;
22089         }
22090
22091       maybe_unary_selector_p = false;
22092       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22093         {
22094           /* Something went quite wrong.  There should be a colon
22095              here, but there is not.  Stop parsing parameters.  */
22096           break;
22097         }
22098       type_name = cp_parser_objc_typename (parser);
22099       /* New ObjC allows attributes on parameters too.  */
22100       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22101         parm_attr = cp_parser_attributes_opt (parser);
22102       identifier = cp_parser_identifier (parser);
22103
22104       params
22105         = chainon (params,
22106                    objc_build_keyword_decl (selector,
22107                                             type_name,
22108                                             identifier,
22109                                             parm_attr));
22110
22111       token = cp_lexer_peek_token (parser->lexer);
22112     }
22113
22114   if (params == NULL_TREE)
22115     {
22116       cp_parser_error (parser, "objective-c++ method declaration is expected");
22117       return error_mark_node;
22118     }
22119
22120   /* We allow tail attributes for the method.  */
22121   if (token->keyword == RID_ATTRIBUTE)
22122     {
22123       *attributes = cp_parser_attributes_opt (parser);
22124       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22125           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22126         return params;
22127       cp_parser_error (parser, 
22128                        "method attributes must be specified at the end");
22129       return error_mark_node;
22130     }
22131
22132   if (params == NULL_TREE)
22133     {
22134       cp_parser_error (parser, "objective-c++ method declaration is expected");
22135       return error_mark_node;
22136     }
22137   return params;
22138 }
22139
22140 /* Parse the non-keyword Objective-C params.  */
22141
22142 static tree
22143 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22144                                        tree* attributes)
22145 {
22146   tree params = make_node (TREE_LIST);
22147   cp_token *token = cp_lexer_peek_token (parser->lexer);
22148   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22149
22150   while (token->type == CPP_COMMA)
22151     {
22152       cp_parameter_declarator *parmdecl;
22153       tree parm;
22154
22155       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22156       token = cp_lexer_peek_token (parser->lexer);
22157
22158       if (token->type == CPP_ELLIPSIS)
22159         {
22160           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22161           *ellipsisp = true;
22162           token = cp_lexer_peek_token (parser->lexer);
22163           break;
22164         }
22165
22166       /* TODO: parse attributes for tail parameters.  */
22167       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22168       parm = grokdeclarator (parmdecl->declarator,
22169                              &parmdecl->decl_specifiers,
22170                              PARM, /*initialized=*/0,
22171                              /*attrlist=*/NULL);
22172
22173       chainon (params, build_tree_list (NULL_TREE, parm));
22174       token = cp_lexer_peek_token (parser->lexer);
22175     }
22176
22177   /* We allow tail attributes for the method.  */
22178   if (token->keyword == RID_ATTRIBUTE)
22179     {
22180       if (*attributes == NULL_TREE)
22181         {
22182           *attributes = cp_parser_attributes_opt (parser);
22183           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22184               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22185             return params;
22186         }
22187       else        
22188         /* We have an error, but parse the attributes, so that we can 
22189            carry on.  */
22190         *attributes = cp_parser_attributes_opt (parser);
22191
22192       cp_parser_error (parser, 
22193                        "method attributes must be specified at the end");
22194       return error_mark_node;
22195     }
22196
22197   return params;
22198 }
22199
22200 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22201
22202 static void
22203 cp_parser_objc_interstitial_code (cp_parser* parser)
22204 {
22205   cp_token *token = cp_lexer_peek_token (parser->lexer);
22206
22207   /* If the next token is `extern' and the following token is a string
22208      literal, then we have a linkage specification.  */
22209   if (token->keyword == RID_EXTERN
22210       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22211     cp_parser_linkage_specification (parser);
22212   /* Handle #pragma, if any.  */
22213   else if (token->type == CPP_PRAGMA)
22214     cp_parser_pragma (parser, pragma_external);
22215   /* Allow stray semicolons.  */
22216   else if (token->type == CPP_SEMICOLON)
22217     cp_lexer_consume_token (parser->lexer);
22218   /* Mark methods as optional or required, when building protocols.  */
22219   else if (token->keyword == RID_AT_OPTIONAL)
22220     {
22221       cp_lexer_consume_token (parser->lexer);
22222       objc_set_method_opt (true);
22223     }
22224   else if (token->keyword == RID_AT_REQUIRED)
22225     {
22226       cp_lexer_consume_token (parser->lexer);
22227       objc_set_method_opt (false);
22228     }
22229   else if (token->keyword == RID_NAMESPACE)
22230     cp_parser_namespace_definition (parser);
22231   /* Other stray characters must generate errors.  */
22232   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22233     {
22234       cp_lexer_consume_token (parser->lexer);
22235       error ("stray %qs between Objective-C++ methods",
22236              token->type == CPP_OPEN_BRACE ? "{" : "}");
22237     }
22238   /* Finally, try to parse a block-declaration, or a function-definition.  */
22239   else
22240     cp_parser_block_declaration (parser, /*statement_p=*/false);
22241 }
22242
22243 /* Parse a method signature.  */
22244
22245 static tree
22246 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22247 {
22248   tree rettype, kwdparms, optparms;
22249   bool ellipsis = false;
22250   bool is_class_method;
22251
22252   is_class_method = cp_parser_objc_method_type (parser);
22253   rettype = cp_parser_objc_typename (parser);
22254   *attributes = NULL_TREE;
22255   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22256   if (kwdparms == error_mark_node)
22257     return error_mark_node;
22258   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22259   if (optparms == error_mark_node)
22260     return error_mark_node;
22261
22262   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22263 }
22264
22265 static bool
22266 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22267 {
22268   tree tattr;  
22269   cp_lexer_save_tokens (parser->lexer);
22270   tattr = cp_parser_attributes_opt (parser);
22271   gcc_assert (tattr) ;
22272   
22273   /* If the attributes are followed by a method introducer, this is not allowed.
22274      Dump the attributes and flag the situation.  */
22275   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22276       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22277     return true;
22278
22279   /* Otherwise, the attributes introduce some interstitial code, possibly so
22280      rewind to allow that check.  */
22281   cp_lexer_rollback_tokens (parser->lexer);
22282   return false;  
22283 }
22284
22285 /* Parse an Objective-C method prototype list.  */
22286
22287 static void
22288 cp_parser_objc_method_prototype_list (cp_parser* parser)
22289 {
22290   cp_token *token = cp_lexer_peek_token (parser->lexer);
22291
22292   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22293     {
22294       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22295         {
22296           tree attributes, sig;
22297           bool is_class_method;
22298           if (token->type == CPP_PLUS)
22299             is_class_method = true;
22300           else
22301             is_class_method = false;
22302           sig = cp_parser_objc_method_signature (parser, &attributes);
22303           if (sig == error_mark_node)
22304             {
22305               cp_parser_skip_to_end_of_block_or_statement (parser);
22306               token = cp_lexer_peek_token (parser->lexer);
22307               continue;
22308             }
22309           objc_add_method_declaration (is_class_method, sig, attributes);
22310           cp_parser_consume_semicolon_at_end_of_statement (parser);
22311         }
22312       else if (token->keyword == RID_AT_PROPERTY)
22313         cp_parser_objc_at_property_declaration (parser);
22314       else if (token->keyword == RID_ATTRIBUTE 
22315                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22316         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22317                     OPT_Wattributes, 
22318                     "prefix attributes are ignored for methods");
22319       else
22320         /* Allow for interspersed non-ObjC++ code.  */
22321         cp_parser_objc_interstitial_code (parser);
22322
22323       token = cp_lexer_peek_token (parser->lexer);
22324     }
22325
22326   if (token->type != CPP_EOF)
22327     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22328   else
22329     cp_parser_error (parser, "expected %<@end%>");
22330
22331   objc_finish_interface ();
22332 }
22333
22334 /* Parse an Objective-C method definition list.  */
22335
22336 static void
22337 cp_parser_objc_method_definition_list (cp_parser* parser)
22338 {
22339   cp_token *token = cp_lexer_peek_token (parser->lexer);
22340
22341   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22342     {
22343       tree meth;
22344
22345       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22346         {
22347           cp_token *ptk;
22348           tree sig, attribute;
22349           bool is_class_method;
22350           if (token->type == CPP_PLUS)
22351             is_class_method = true;
22352           else
22353             is_class_method = false;
22354           push_deferring_access_checks (dk_deferred);
22355           sig = cp_parser_objc_method_signature (parser, &attribute);
22356           if (sig == error_mark_node)
22357             {
22358               cp_parser_skip_to_end_of_block_or_statement (parser);
22359               token = cp_lexer_peek_token (parser->lexer);
22360               continue;
22361             }
22362           objc_start_method_definition (is_class_method, sig, attribute,
22363                                         NULL_TREE);
22364
22365           /* For historical reasons, we accept an optional semicolon.  */
22366           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22367             cp_lexer_consume_token (parser->lexer);
22368
22369           ptk = cp_lexer_peek_token (parser->lexer);
22370           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22371                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22372             {
22373               perform_deferred_access_checks ();
22374               stop_deferring_access_checks ();
22375               meth = cp_parser_function_definition_after_declarator (parser,
22376                                                                      false);
22377               pop_deferring_access_checks ();
22378               objc_finish_method_definition (meth);
22379             }
22380         }
22381       /* The following case will be removed once @synthesize is
22382          completely implemented.  */
22383       else if (token->keyword == RID_AT_PROPERTY)
22384         cp_parser_objc_at_property_declaration (parser);
22385       else if (token->keyword == RID_AT_SYNTHESIZE)
22386         cp_parser_objc_at_synthesize_declaration (parser);
22387       else if (token->keyword == RID_AT_DYNAMIC)
22388         cp_parser_objc_at_dynamic_declaration (parser);
22389       else if (token->keyword == RID_ATTRIBUTE 
22390                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22391         warning_at (token->location, OPT_Wattributes,
22392                     "prefix attributes are ignored for methods");
22393       else
22394         /* Allow for interspersed non-ObjC++ code.  */
22395         cp_parser_objc_interstitial_code (parser);
22396
22397       token = cp_lexer_peek_token (parser->lexer);
22398     }
22399
22400   if (token->type != CPP_EOF)
22401     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22402   else
22403     cp_parser_error (parser, "expected %<@end%>");
22404
22405   objc_finish_implementation ();
22406 }
22407
22408 /* Parse Objective-C ivars.  */
22409
22410 static void
22411 cp_parser_objc_class_ivars (cp_parser* parser)
22412 {
22413   cp_token *token = cp_lexer_peek_token (parser->lexer);
22414
22415   if (token->type != CPP_OPEN_BRACE)
22416     return;     /* No ivars specified.  */
22417
22418   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22419   token = cp_lexer_peek_token (parser->lexer);
22420
22421   while (token->type != CPP_CLOSE_BRACE 
22422         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22423     {
22424       cp_decl_specifier_seq declspecs;
22425       int decl_class_or_enum_p;
22426       tree prefix_attributes;
22427
22428       cp_parser_objc_visibility_spec (parser);
22429
22430       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22431         break;
22432
22433       cp_parser_decl_specifier_seq (parser,
22434                                     CP_PARSER_FLAGS_OPTIONAL,
22435                                     &declspecs,
22436                                     &decl_class_or_enum_p);
22437
22438       /* auto, register, static, extern, mutable.  */
22439       if (declspecs.storage_class != sc_none)
22440         {
22441           cp_parser_error (parser, "invalid type for instance variable");         
22442           declspecs.storage_class = sc_none;
22443         }
22444
22445       /* __thread.  */
22446       if (declspecs.specs[(int) ds_thread])
22447         {
22448           cp_parser_error (parser, "invalid type for instance variable");
22449           declspecs.specs[(int) ds_thread] = 0;
22450         }
22451       
22452       /* typedef.  */
22453       if (declspecs.specs[(int) ds_typedef])
22454         {
22455           cp_parser_error (parser, "invalid type for instance variable");
22456           declspecs.specs[(int) ds_typedef] = 0;
22457         }
22458
22459       prefix_attributes = declspecs.attributes;
22460       declspecs.attributes = NULL_TREE;
22461
22462       /* Keep going until we hit the `;' at the end of the
22463          declaration.  */
22464       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22465         {
22466           tree width = NULL_TREE, attributes, first_attribute, decl;
22467           cp_declarator *declarator = NULL;
22468           int ctor_dtor_or_conv_p;
22469
22470           /* Check for a (possibly unnamed) bitfield declaration.  */
22471           token = cp_lexer_peek_token (parser->lexer);
22472           if (token->type == CPP_COLON)
22473             goto eat_colon;
22474
22475           if (token->type == CPP_NAME
22476               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22477                   == CPP_COLON))
22478             {
22479               /* Get the name of the bitfield.  */
22480               declarator = make_id_declarator (NULL_TREE,
22481                                                cp_parser_identifier (parser),
22482                                                sfk_none);
22483
22484              eat_colon:
22485               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22486               /* Get the width of the bitfield.  */
22487               width
22488                 = cp_parser_constant_expression (parser,
22489                                                  /*allow_non_constant=*/false,
22490                                                  NULL);
22491             }
22492           else
22493             {
22494               /* Parse the declarator.  */
22495               declarator
22496                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22497                                         &ctor_dtor_or_conv_p,
22498                                         /*parenthesized_p=*/NULL,
22499                                         /*member_p=*/false);
22500             }
22501
22502           /* Look for attributes that apply to the ivar.  */
22503           attributes = cp_parser_attributes_opt (parser);
22504           /* Remember which attributes are prefix attributes and
22505              which are not.  */
22506           first_attribute = attributes;
22507           /* Combine the attributes.  */
22508           attributes = chainon (prefix_attributes, attributes);
22509
22510           if (width)
22511               /* Create the bitfield declaration.  */
22512               decl = grokbitfield (declarator, &declspecs,
22513                                    width,
22514                                    attributes);
22515           else
22516             decl = grokfield (declarator, &declspecs,
22517                               NULL_TREE, /*init_const_expr_p=*/false,
22518                               NULL_TREE, attributes);
22519
22520           /* Add the instance variable.  */
22521           if (decl != error_mark_node && decl != NULL_TREE)
22522             objc_add_instance_variable (decl);
22523
22524           /* Reset PREFIX_ATTRIBUTES.  */
22525           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22526             attributes = TREE_CHAIN (attributes);
22527           if (attributes)
22528             TREE_CHAIN (attributes) = NULL_TREE;
22529
22530           token = cp_lexer_peek_token (parser->lexer);
22531
22532           if (token->type == CPP_COMMA)
22533             {
22534               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22535               continue;
22536             }
22537           break;
22538         }
22539
22540       cp_parser_consume_semicolon_at_end_of_statement (parser);
22541       token = cp_lexer_peek_token (parser->lexer);
22542     }
22543
22544   if (token->keyword == RID_AT_END)
22545     cp_parser_error (parser, "expected %<}%>");
22546
22547   /* Do not consume the RID_AT_END, so it will be read again as terminating
22548      the @interface of @implementation.  */ 
22549   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22550     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22551     
22552   /* For historical reasons, we accept an optional semicolon.  */
22553   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22554     cp_lexer_consume_token (parser->lexer);
22555 }
22556
22557 /* Parse an Objective-C protocol declaration.  */
22558
22559 static void
22560 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22561 {
22562   tree proto, protorefs;
22563   cp_token *tok;
22564
22565   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22566   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22567     {
22568       tok = cp_lexer_peek_token (parser->lexer);
22569       error_at (tok->location, "identifier expected after %<@protocol%>");
22570       cp_parser_consume_semicolon_at_end_of_statement (parser);
22571       return;
22572     }
22573
22574   /* See if we have a forward declaration or a definition.  */
22575   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22576
22577   /* Try a forward declaration first.  */
22578   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22579     {
22580       while (true)
22581         {
22582           tree id;
22583           
22584           id = cp_parser_identifier (parser);
22585           if (id == error_mark_node)
22586             break;
22587           
22588           objc_declare_protocol (id, attributes);
22589           
22590           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22591             cp_lexer_consume_token (parser->lexer);
22592           else
22593             break;
22594         }
22595       cp_parser_consume_semicolon_at_end_of_statement (parser);
22596     }
22597
22598   /* Ok, we got a full-fledged definition (or at least should).  */
22599   else
22600     {
22601       proto = cp_parser_identifier (parser);
22602       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22603       objc_start_protocol (proto, protorefs, attributes);
22604       cp_parser_objc_method_prototype_list (parser);
22605     }
22606 }
22607
22608 /* Parse an Objective-C superclass or category.  */
22609
22610 static void
22611 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22612                                        bool iface_p,
22613                                        tree *super,
22614                                        tree *categ, bool *is_class_extension)
22615 {
22616   cp_token *next = cp_lexer_peek_token (parser->lexer);
22617
22618   *super = *categ = NULL_TREE;
22619   *is_class_extension = false;
22620   if (next->type == CPP_COLON)
22621     {
22622       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22623       *super = cp_parser_identifier (parser);
22624     }
22625   else if (next->type == CPP_OPEN_PAREN)
22626     {
22627       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22628
22629       /* If there is no category name, and this is an @interface, we
22630          have a class extension.  */
22631       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22632         {
22633           *categ = NULL_TREE;
22634           *is_class_extension = true;
22635         }
22636       else
22637         *categ = cp_parser_identifier (parser);
22638
22639       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22640     }
22641 }
22642
22643 /* Parse an Objective-C class interface.  */
22644
22645 static void
22646 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22647 {
22648   tree name, super, categ, protos;
22649   bool is_class_extension;
22650
22651   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22652   name = cp_parser_identifier (parser);
22653   if (name == error_mark_node)
22654     {
22655       /* It's hard to recover because even if valid @interface stuff
22656          is to follow, we can't compile it (or validate it) if we
22657          don't even know which class it refers to.  Let's assume this
22658          was a stray '@interface' token in the stream and skip it.
22659       */
22660       return;
22661     }
22662   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22663                                          &is_class_extension);
22664   protos = cp_parser_objc_protocol_refs_opt (parser);
22665
22666   /* We have either a class or a category on our hands.  */
22667   if (categ || is_class_extension)
22668     objc_start_category_interface (name, categ, protos, attributes);
22669   else
22670     {
22671       objc_start_class_interface (name, super, protos, attributes);
22672       /* Handle instance variable declarations, if any.  */
22673       cp_parser_objc_class_ivars (parser);
22674       objc_continue_interface ();
22675     }
22676
22677   cp_parser_objc_method_prototype_list (parser);
22678 }
22679
22680 /* Parse an Objective-C class implementation.  */
22681
22682 static void
22683 cp_parser_objc_class_implementation (cp_parser* parser)
22684 {
22685   tree name, super, categ;
22686   bool is_class_extension;
22687
22688   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22689   name = cp_parser_identifier (parser);
22690   if (name == error_mark_node)
22691     {
22692       /* It's hard to recover because even if valid @implementation
22693          stuff is to follow, we can't compile it (or validate it) if
22694          we don't even know which class it refers to.  Let's assume
22695          this was a stray '@implementation' token in the stream and
22696          skip it.
22697       */
22698       return;
22699     }
22700   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22701                                          &is_class_extension);
22702
22703   /* We have either a class or a category on our hands.  */
22704   if (categ)
22705     objc_start_category_implementation (name, categ);
22706   else
22707     {
22708       objc_start_class_implementation (name, super);
22709       /* Handle instance variable declarations, if any.  */
22710       cp_parser_objc_class_ivars (parser);
22711       objc_continue_implementation ();
22712     }
22713
22714   cp_parser_objc_method_definition_list (parser);
22715 }
22716
22717 /* Consume the @end token and finish off the implementation.  */
22718
22719 static void
22720 cp_parser_objc_end_implementation (cp_parser* parser)
22721 {
22722   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22723   objc_finish_implementation ();
22724 }
22725
22726 /* Parse an Objective-C declaration.  */
22727
22728 static void
22729 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22730 {
22731   /* Try to figure out what kind of declaration is present.  */
22732   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22733
22734   if (attributes)
22735     switch (kwd->keyword)
22736       {
22737         case RID_AT_ALIAS:
22738         case RID_AT_CLASS:
22739         case RID_AT_END:
22740           error_at (kwd->location, "attributes may not be specified before"
22741                     " the %<@%D%> Objective-C++ keyword",
22742                     kwd->u.value);
22743           attributes = NULL;
22744           break;
22745         case RID_AT_IMPLEMENTATION:
22746           warning_at (kwd->location, OPT_Wattributes,
22747                       "prefix attributes are ignored before %<@%D%>",
22748                       kwd->u.value);
22749           attributes = NULL;
22750         default:
22751           break;
22752       }
22753
22754   switch (kwd->keyword)
22755     {
22756     case RID_AT_ALIAS:
22757       cp_parser_objc_alias_declaration (parser);
22758       break;
22759     case RID_AT_CLASS:
22760       cp_parser_objc_class_declaration (parser);
22761       break;
22762     case RID_AT_PROTOCOL:
22763       cp_parser_objc_protocol_declaration (parser, attributes);
22764       break;
22765     case RID_AT_INTERFACE:
22766       cp_parser_objc_class_interface (parser, attributes);
22767       break;
22768     case RID_AT_IMPLEMENTATION:
22769       cp_parser_objc_class_implementation (parser);
22770       break;
22771     case RID_AT_END:
22772       cp_parser_objc_end_implementation (parser);
22773       break;
22774     default:
22775       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22776                 kwd->u.value);
22777       cp_parser_skip_to_end_of_block_or_statement (parser);
22778     }
22779 }
22780
22781 /* Parse an Objective-C try-catch-finally statement.
22782
22783    objc-try-catch-finally-stmt:
22784      @try compound-statement objc-catch-clause-seq [opt]
22785        objc-finally-clause [opt]
22786
22787    objc-catch-clause-seq:
22788      objc-catch-clause objc-catch-clause-seq [opt]
22789
22790    objc-catch-clause:
22791      @catch ( objc-exception-declaration ) compound-statement
22792
22793    objc-finally-clause:
22794      @finally compound-statement
22795
22796    objc-exception-declaration:
22797      parameter-declaration
22798      '...'
22799
22800    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22801
22802    Returns NULL_TREE.
22803
22804    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22805    for C.  Keep them in sync.  */   
22806
22807 static tree
22808 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22809 {
22810   location_t location;
22811   tree stmt;
22812
22813   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22814   location = cp_lexer_peek_token (parser->lexer)->location;
22815   objc_maybe_warn_exceptions (location);
22816   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22817      node, lest it get absorbed into the surrounding block.  */
22818   stmt = push_stmt_list ();
22819   cp_parser_compound_statement (parser, NULL, false, false);
22820   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22821
22822   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22823     {
22824       cp_parameter_declarator *parm;
22825       tree parameter_declaration = error_mark_node;
22826       bool seen_open_paren = false;
22827
22828       cp_lexer_consume_token (parser->lexer);
22829       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22830         seen_open_paren = true;
22831       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22832         {
22833           /* We have "@catch (...)" (where the '...' are literally
22834              what is in the code).  Skip the '...'.
22835              parameter_declaration is set to NULL_TREE, and
22836              objc_being_catch_clauses() knows that that means
22837              '...'.  */
22838           cp_lexer_consume_token (parser->lexer);
22839           parameter_declaration = NULL_TREE;
22840         }
22841       else
22842         {
22843           /* We have "@catch (NSException *exception)" or something
22844              like that.  Parse the parameter declaration.  */
22845           parm = cp_parser_parameter_declaration (parser, false, NULL);
22846           if (parm == NULL)
22847             parameter_declaration = error_mark_node;
22848           else
22849             parameter_declaration = grokdeclarator (parm->declarator,
22850                                                     &parm->decl_specifiers,
22851                                                     PARM, /*initialized=*/0,
22852                                                     /*attrlist=*/NULL);
22853         }
22854       if (seen_open_paren)
22855         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22856       else
22857         {
22858           /* If there was no open parenthesis, we are recovering from
22859              an error, and we are trying to figure out what mistake
22860              the user has made.  */
22861
22862           /* If there is an immediate closing parenthesis, the user
22863              probably forgot the opening one (ie, they typed "@catch
22864              NSException *e)".  Parse the closing parenthesis and keep
22865              going.  */
22866           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22867             cp_lexer_consume_token (parser->lexer);
22868           
22869           /* If these is no immediate closing parenthesis, the user
22870              probably doesn't know that parenthesis are required at
22871              all (ie, they typed "@catch NSException *e").  So, just
22872              forget about the closing parenthesis and keep going.  */
22873         }
22874       objc_begin_catch_clause (parameter_declaration);
22875       cp_parser_compound_statement (parser, NULL, false, false);
22876       objc_finish_catch_clause ();
22877     }
22878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22879     {
22880       cp_lexer_consume_token (parser->lexer);
22881       location = cp_lexer_peek_token (parser->lexer)->location;
22882       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22883          node, lest it get absorbed into the surrounding block.  */
22884       stmt = push_stmt_list ();
22885       cp_parser_compound_statement (parser, NULL, false, false);
22886       objc_build_finally_clause (location, pop_stmt_list (stmt));
22887     }
22888
22889   return objc_finish_try_stmt ();
22890 }
22891
22892 /* Parse an Objective-C synchronized statement.
22893
22894    objc-synchronized-stmt:
22895      @synchronized ( expression ) compound-statement
22896
22897    Returns NULL_TREE.  */
22898
22899 static tree
22900 cp_parser_objc_synchronized_statement (cp_parser *parser)
22901 {
22902   location_t location;
22903   tree lock, stmt;
22904
22905   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22906
22907   location = cp_lexer_peek_token (parser->lexer)->location;
22908   objc_maybe_warn_exceptions (location);
22909   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22910   lock = cp_parser_expression (parser, false, NULL);
22911   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22912
22913   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22914      node, lest it get absorbed into the surrounding block.  */
22915   stmt = push_stmt_list ();
22916   cp_parser_compound_statement (parser, NULL, false, false);
22917
22918   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22919 }
22920
22921 /* Parse an Objective-C throw statement.
22922
22923    objc-throw-stmt:
22924      @throw assignment-expression [opt] ;
22925
22926    Returns a constructed '@throw' statement.  */
22927
22928 static tree
22929 cp_parser_objc_throw_statement (cp_parser *parser)
22930 {
22931   tree expr = NULL_TREE;
22932   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22933
22934   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22935
22936   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22937     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22938
22939   cp_parser_consume_semicolon_at_end_of_statement (parser);
22940
22941   return objc_build_throw_stmt (loc, expr);
22942 }
22943
22944 /* Parse an Objective-C statement.  */
22945
22946 static tree
22947 cp_parser_objc_statement (cp_parser * parser)
22948 {
22949   /* Try to figure out what kind of declaration is present.  */
22950   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22951
22952   switch (kwd->keyword)
22953     {
22954     case RID_AT_TRY:
22955       return cp_parser_objc_try_catch_finally_statement (parser);
22956     case RID_AT_SYNCHRONIZED:
22957       return cp_parser_objc_synchronized_statement (parser);
22958     case RID_AT_THROW:
22959       return cp_parser_objc_throw_statement (parser);
22960     default:
22961       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22962                kwd->u.value);
22963       cp_parser_skip_to_end_of_block_or_statement (parser);
22964     }
22965
22966   return error_mark_node;
22967 }
22968
22969 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22970    look ahead to see if an objc keyword follows the attributes.  This
22971    is to detect the use of prefix attributes on ObjC @interface and 
22972    @protocol.  */
22973
22974 static bool
22975 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22976 {
22977   cp_lexer_save_tokens (parser->lexer);
22978   *attrib = cp_parser_attributes_opt (parser);
22979   gcc_assert (*attrib);
22980   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22981     {
22982       cp_lexer_commit_tokens (parser->lexer);
22983       return true;
22984     }
22985   cp_lexer_rollback_tokens (parser->lexer);
22986   return false;  
22987 }
22988
22989 /* This routine is a minimal replacement for
22990    c_parser_struct_declaration () used when parsing the list of
22991    types/names or ObjC++ properties.  For example, when parsing the
22992    code
22993
22994    @property (readonly) int a, b, c;
22995
22996    this function is responsible for parsing "int a, int b, int c" and
22997    returning the declarations as CHAIN of DECLs.
22998
22999    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23000    similar parsing.  */
23001 static tree
23002 cp_parser_objc_struct_declaration (cp_parser *parser)
23003 {
23004   tree decls = NULL_TREE;
23005   cp_decl_specifier_seq declspecs;
23006   int decl_class_or_enum_p;
23007   tree prefix_attributes;
23008
23009   cp_parser_decl_specifier_seq (parser,
23010                                 CP_PARSER_FLAGS_NONE,
23011                                 &declspecs,
23012                                 &decl_class_or_enum_p);
23013
23014   if (declspecs.type == error_mark_node)
23015     return error_mark_node;
23016
23017   /* auto, register, static, extern, mutable.  */
23018   if (declspecs.storage_class != sc_none)
23019     {
23020       cp_parser_error (parser, "invalid type for property");
23021       declspecs.storage_class = sc_none;
23022     }
23023   
23024   /* __thread.  */
23025   if (declspecs.specs[(int) ds_thread])
23026     {
23027       cp_parser_error (parser, "invalid type for property");
23028       declspecs.specs[(int) ds_thread] = 0;
23029     }
23030   
23031   /* typedef.  */
23032   if (declspecs.specs[(int) ds_typedef])
23033     {
23034       cp_parser_error (parser, "invalid type for property");
23035       declspecs.specs[(int) ds_typedef] = 0;
23036     }
23037
23038   prefix_attributes = declspecs.attributes;
23039   declspecs.attributes = NULL_TREE;
23040
23041   /* Keep going until we hit the `;' at the end of the declaration. */
23042   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23043     {
23044       tree attributes, first_attribute, decl;
23045       cp_declarator *declarator;
23046       cp_token *token;
23047
23048       /* Parse the declarator.  */
23049       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23050                                          NULL, NULL, false);
23051
23052       /* Look for attributes that apply to the ivar.  */
23053       attributes = cp_parser_attributes_opt (parser);
23054       /* Remember which attributes are prefix attributes and
23055          which are not.  */
23056       first_attribute = attributes;
23057       /* Combine the attributes.  */
23058       attributes = chainon (prefix_attributes, attributes);
23059       
23060       decl = grokfield (declarator, &declspecs,
23061                         NULL_TREE, /*init_const_expr_p=*/false,
23062                         NULL_TREE, attributes);
23063
23064       if (decl == error_mark_node || decl == NULL_TREE)
23065         return error_mark_node;
23066       
23067       /* Reset PREFIX_ATTRIBUTES.  */
23068       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23069         attributes = TREE_CHAIN (attributes);
23070       if (attributes)
23071         TREE_CHAIN (attributes) = NULL_TREE;
23072
23073       DECL_CHAIN (decl) = decls;
23074       decls = decl;
23075
23076       token = cp_lexer_peek_token (parser->lexer);
23077       if (token->type == CPP_COMMA)
23078         {
23079           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23080           continue;
23081         }
23082       else
23083         break;
23084     }
23085   return decls;
23086 }
23087
23088 /* Parse an Objective-C @property declaration.  The syntax is:
23089
23090    objc-property-declaration:
23091      '@property' objc-property-attributes[opt] struct-declaration ;
23092
23093    objc-property-attributes:
23094     '(' objc-property-attribute-list ')'
23095
23096    objc-property-attribute-list:
23097      objc-property-attribute
23098      objc-property-attribute-list, objc-property-attribute
23099
23100    objc-property-attribute
23101      'getter' = identifier
23102      'setter' = identifier
23103      'readonly'
23104      'readwrite'
23105      'assign'
23106      'retain'
23107      'copy'
23108      'nonatomic'
23109
23110   For example:
23111     @property NSString *name;
23112     @property (readonly) id object;
23113     @property (retain, nonatomic, getter=getTheName) id name;
23114     @property int a, b, c;
23115
23116    PS: This function is identical to
23117    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23118 static void 
23119 cp_parser_objc_at_property_declaration (cp_parser *parser)
23120 {
23121   /* The following variables hold the attributes of the properties as
23122      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23123      seen.  When we see an attribute, we set them to 'true' (if they
23124      are boolean properties) or to the identifier (if they have an
23125      argument, ie, for getter and setter).  Note that here we only
23126      parse the list of attributes, check the syntax and accumulate the
23127      attributes that we find.  objc_add_property_declaration() will
23128      then process the information.  */
23129   bool property_assign = false;
23130   bool property_copy = false;
23131   tree property_getter_ident = NULL_TREE;
23132   bool property_nonatomic = false;
23133   bool property_readonly = false;
23134   bool property_readwrite = false;
23135   bool property_retain = false;
23136   tree property_setter_ident = NULL_TREE;
23137
23138   /* 'properties' is the list of properties that we read.  Usually a
23139      single one, but maybe more (eg, in "@property int a, b, c;" there
23140      are three).  */
23141   tree properties;
23142   location_t loc;
23143
23144   loc = cp_lexer_peek_token (parser->lexer)->location;
23145
23146   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23147
23148   /* Parse the optional attribute list...  */
23149   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23150     {
23151       /* Eat the '('.  */
23152       cp_lexer_consume_token (parser->lexer);
23153
23154       while (true)
23155         {
23156           bool syntax_error = false;
23157           cp_token *token = cp_lexer_peek_token (parser->lexer);
23158           enum rid keyword;
23159
23160           if (token->type != CPP_NAME)
23161             {
23162               cp_parser_error (parser, "expected identifier");
23163               break;
23164             }
23165           keyword = C_RID_CODE (token->u.value);
23166           cp_lexer_consume_token (parser->lexer);
23167           switch (keyword)
23168             {
23169             case RID_ASSIGN:    property_assign = true;    break;
23170             case RID_COPY:      property_copy = true;      break;
23171             case RID_NONATOMIC: property_nonatomic = true; break;
23172             case RID_READONLY:  property_readonly = true;  break;
23173             case RID_READWRITE: property_readwrite = true; break;
23174             case RID_RETAIN:    property_retain = true;    break;
23175
23176             case RID_GETTER:
23177             case RID_SETTER:
23178               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23179                 {
23180                   if (keyword == RID_GETTER)
23181                     cp_parser_error (parser,
23182                                      "missing %<=%> (after %<getter%> attribute)");
23183                   else
23184                     cp_parser_error (parser,
23185                                      "missing %<=%> (after %<setter%> attribute)");
23186                   syntax_error = true;
23187                   break;
23188                 }
23189               cp_lexer_consume_token (parser->lexer); /* eat the = */
23190               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23191                 {
23192                   cp_parser_error (parser, "expected identifier");
23193                   syntax_error = true;
23194                   break;
23195                 }
23196               if (keyword == RID_SETTER)
23197                 {
23198                   if (property_setter_ident != NULL_TREE)
23199                     {
23200                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23201                       cp_lexer_consume_token (parser->lexer);
23202                     }
23203                   else
23204                     property_setter_ident = cp_parser_objc_selector (parser);
23205                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23206                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23207                   else
23208                     cp_lexer_consume_token (parser->lexer);
23209                 }
23210               else
23211                 {
23212                   if (property_getter_ident != NULL_TREE)
23213                     {
23214                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23215                       cp_lexer_consume_token (parser->lexer);
23216                     }
23217                   else
23218                     property_getter_ident = cp_parser_objc_selector (parser);
23219                 }
23220               break;
23221             default:
23222               cp_parser_error (parser, "unknown property attribute");
23223               syntax_error = true;
23224               break;
23225             }
23226
23227           if (syntax_error)
23228             break;
23229
23230           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23231             cp_lexer_consume_token (parser->lexer);
23232           else
23233             break;
23234         }
23235
23236       /* FIXME: "@property (setter, assign);" will generate a spurious
23237          "error: expected â€˜)’ before â€˜,’ token".  This is because
23238          cp_parser_require, unlike the C counterpart, will produce an
23239          error even if we are in error recovery.  */
23240       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23241         {
23242           cp_parser_skip_to_closing_parenthesis (parser,
23243                                                  /*recovering=*/true,
23244                                                  /*or_comma=*/false,
23245                                                  /*consume_paren=*/true);
23246         }
23247     }
23248
23249   /* ... and the property declaration(s).  */
23250   properties = cp_parser_objc_struct_declaration (parser);
23251
23252   if (properties == error_mark_node)
23253     {
23254       cp_parser_skip_to_end_of_statement (parser);
23255       /* If the next token is now a `;', consume it.  */
23256       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23257         cp_lexer_consume_token (parser->lexer);
23258       return;
23259     }
23260
23261   if (properties == NULL_TREE)
23262     cp_parser_error (parser, "expected identifier");
23263   else
23264     {
23265       /* Comma-separated properties are chained together in
23266          reverse order; add them one by one.  */
23267       properties = nreverse (properties);
23268       
23269       for (; properties; properties = TREE_CHAIN (properties))
23270         objc_add_property_declaration (loc, copy_node (properties),
23271                                        property_readonly, property_readwrite,
23272                                        property_assign, property_retain,
23273                                        property_copy, property_nonatomic,
23274                                        property_getter_ident, property_setter_ident);
23275     }
23276   
23277   cp_parser_consume_semicolon_at_end_of_statement (parser);
23278 }
23279
23280 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23281
23282    objc-synthesize-declaration:
23283      @synthesize objc-synthesize-identifier-list ;
23284
23285    objc-synthesize-identifier-list:
23286      objc-synthesize-identifier
23287      objc-synthesize-identifier-list, objc-synthesize-identifier
23288
23289    objc-synthesize-identifier
23290      identifier
23291      identifier = identifier
23292
23293   For example:
23294     @synthesize MyProperty;
23295     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23296
23297   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23298   for C.  Keep them in sync.
23299 */
23300 static void 
23301 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23302 {
23303   tree list = NULL_TREE;
23304   location_t loc;
23305   loc = cp_lexer_peek_token (parser->lexer)->location;
23306
23307   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23308   while (true)
23309     {
23310       tree property, ivar;
23311       property = cp_parser_identifier (parser);
23312       if (property == error_mark_node)
23313         {
23314           cp_parser_consume_semicolon_at_end_of_statement (parser);
23315           return;
23316         }
23317       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23318         {
23319           cp_lexer_consume_token (parser->lexer);
23320           ivar = cp_parser_identifier (parser);
23321           if (ivar == error_mark_node)
23322             {
23323               cp_parser_consume_semicolon_at_end_of_statement (parser);
23324               return;
23325             }
23326         }
23327       else
23328         ivar = NULL_TREE;
23329       list = chainon (list, build_tree_list (ivar, property));
23330       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23331         cp_lexer_consume_token (parser->lexer);
23332       else
23333         break;
23334     }
23335   cp_parser_consume_semicolon_at_end_of_statement (parser);
23336   objc_add_synthesize_declaration (loc, list);
23337 }
23338
23339 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23340
23341    objc-dynamic-declaration:
23342      @dynamic identifier-list ;
23343
23344    For example:
23345      @dynamic MyProperty;
23346      @dynamic MyProperty, AnotherProperty;
23347
23348   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23349   for C.  Keep them in sync.
23350 */
23351 static void 
23352 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23353 {
23354   tree list = NULL_TREE;
23355   location_t loc;
23356   loc = cp_lexer_peek_token (parser->lexer)->location;
23357
23358   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23359   while (true)
23360     {
23361       tree property;
23362       property = cp_parser_identifier (parser);
23363       if (property == error_mark_node)
23364         {
23365           cp_parser_consume_semicolon_at_end_of_statement (parser);
23366           return;
23367         }
23368       list = chainon (list, build_tree_list (NULL, property));
23369       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23370         cp_lexer_consume_token (parser->lexer);
23371       else
23372         break;
23373     }
23374   cp_parser_consume_semicolon_at_end_of_statement (parser);
23375   objc_add_dynamic_declaration (loc, list);
23376 }
23377
23378 \f
23379 /* OpenMP 2.5 parsing routines.  */
23380
23381 /* Returns name of the next clause.
23382    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23383    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23384    returned and the token is consumed.  */
23385
23386 static pragma_omp_clause
23387 cp_parser_omp_clause_name (cp_parser *parser)
23388 {
23389   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23390
23391   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23392     result = PRAGMA_OMP_CLAUSE_IF;
23393   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23394     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23395   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23396     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23397   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23398     {
23399       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23400       const char *p = IDENTIFIER_POINTER (id);
23401
23402       switch (p[0])
23403         {
23404         case 'c':
23405           if (!strcmp ("collapse", p))
23406             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23407           else if (!strcmp ("copyin", p))
23408             result = PRAGMA_OMP_CLAUSE_COPYIN;
23409           else if (!strcmp ("copyprivate", p))
23410             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23411           break;
23412         case 'f':
23413           if (!strcmp ("firstprivate", p))
23414             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23415           break;
23416         case 'l':
23417           if (!strcmp ("lastprivate", p))
23418             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23419           break;
23420         case 'n':
23421           if (!strcmp ("nowait", p))
23422             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23423           else if (!strcmp ("num_threads", p))
23424             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23425           break;
23426         case 'o':
23427           if (!strcmp ("ordered", p))
23428             result = PRAGMA_OMP_CLAUSE_ORDERED;
23429           break;
23430         case 'r':
23431           if (!strcmp ("reduction", p))
23432             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23433           break;
23434         case 's':
23435           if (!strcmp ("schedule", p))
23436             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23437           else if (!strcmp ("shared", p))
23438             result = PRAGMA_OMP_CLAUSE_SHARED;
23439           break;
23440         case 'u':
23441           if (!strcmp ("untied", p))
23442             result = PRAGMA_OMP_CLAUSE_UNTIED;
23443           break;
23444         }
23445     }
23446
23447   if (result != PRAGMA_OMP_CLAUSE_NONE)
23448     cp_lexer_consume_token (parser->lexer);
23449
23450   return result;
23451 }
23452
23453 /* Validate that a clause of the given type does not already exist.  */
23454
23455 static void
23456 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23457                            const char *name, location_t location)
23458 {
23459   tree c;
23460
23461   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23462     if (OMP_CLAUSE_CODE (c) == code)
23463       {
23464         error_at (location, "too many %qs clauses", name);
23465         break;
23466       }
23467 }
23468
23469 /* OpenMP 2.5:
23470    variable-list:
23471      identifier
23472      variable-list , identifier
23473
23474    In addition, we match a closing parenthesis.  An opening parenthesis
23475    will have been consumed by the caller.
23476
23477    If KIND is nonzero, create the appropriate node and install the decl
23478    in OMP_CLAUSE_DECL and add the node to the head of the list.
23479
23480    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23481    return the list created.  */
23482
23483 static tree
23484 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23485                                 tree list)
23486 {
23487   cp_token *token;
23488   while (1)
23489     {
23490       tree name, decl;
23491
23492       token = cp_lexer_peek_token (parser->lexer);
23493       name = cp_parser_id_expression (parser, /*template_p=*/false,
23494                                       /*check_dependency_p=*/true,
23495                                       /*template_p=*/NULL,
23496                                       /*declarator_p=*/false,
23497                                       /*optional_p=*/false);
23498       if (name == error_mark_node)
23499         goto skip_comma;
23500
23501       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23502       if (decl == error_mark_node)
23503         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23504                                      token->location);
23505       else if (kind != 0)
23506         {
23507           tree u = build_omp_clause (token->location, kind);
23508           OMP_CLAUSE_DECL (u) = decl;
23509           OMP_CLAUSE_CHAIN (u) = list;
23510           list = u;
23511         }
23512       else
23513         list = tree_cons (decl, NULL_TREE, list);
23514
23515     get_comma:
23516       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23517         break;
23518       cp_lexer_consume_token (parser->lexer);
23519     }
23520
23521   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23522     {
23523       int ending;
23524
23525       /* Try to resync to an unnested comma.  Copied from
23526          cp_parser_parenthesized_expression_list.  */
23527     skip_comma:
23528       ending = cp_parser_skip_to_closing_parenthesis (parser,
23529                                                       /*recovering=*/true,
23530                                                       /*or_comma=*/true,
23531                                                       /*consume_paren=*/true);
23532       if (ending < 0)
23533         goto get_comma;
23534     }
23535
23536   return list;
23537 }
23538
23539 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23540    common case for omp clauses.  */
23541
23542 static tree
23543 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23544 {
23545   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23546     return cp_parser_omp_var_list_no_open (parser, kind, list);
23547   return list;
23548 }
23549
23550 /* OpenMP 3.0:
23551    collapse ( constant-expression ) */
23552
23553 static tree
23554 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23555 {
23556   tree c, num;
23557   location_t loc;
23558   HOST_WIDE_INT n;
23559
23560   loc = cp_lexer_peek_token (parser->lexer)->location;
23561   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23562     return list;
23563
23564   num = cp_parser_constant_expression (parser, false, NULL);
23565
23566   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23567     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23568                                            /*or_comma=*/false,
23569                                            /*consume_paren=*/true);
23570
23571   if (num == error_mark_node)
23572     return list;
23573   num = fold_non_dependent_expr (num);
23574   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23575       || !host_integerp (num, 0)
23576       || (n = tree_low_cst (num, 0)) <= 0
23577       || (int) n != n)
23578     {
23579       error_at (loc, "collapse argument needs positive constant integer expression");
23580       return list;
23581     }
23582
23583   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23584   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23585   OMP_CLAUSE_CHAIN (c) = list;
23586   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23587
23588   return c;
23589 }
23590
23591 /* OpenMP 2.5:
23592    default ( shared | none ) */
23593
23594 static tree
23595 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23596 {
23597   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23598   tree c;
23599
23600   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23601     return list;
23602   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23603     {
23604       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23605       const char *p = IDENTIFIER_POINTER (id);
23606
23607       switch (p[0])
23608         {
23609         case 'n':
23610           if (strcmp ("none", p) != 0)
23611             goto invalid_kind;
23612           kind = OMP_CLAUSE_DEFAULT_NONE;
23613           break;
23614
23615         case 's':
23616           if (strcmp ("shared", p) != 0)
23617             goto invalid_kind;
23618           kind = OMP_CLAUSE_DEFAULT_SHARED;
23619           break;
23620
23621         default:
23622           goto invalid_kind;
23623         }
23624
23625       cp_lexer_consume_token (parser->lexer);
23626     }
23627   else
23628     {
23629     invalid_kind:
23630       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23631     }
23632
23633   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23634     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23635                                            /*or_comma=*/false,
23636                                            /*consume_paren=*/true);
23637
23638   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23639     return list;
23640
23641   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23642   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23643   OMP_CLAUSE_CHAIN (c) = list;
23644   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23645
23646   return c;
23647 }
23648
23649 /* OpenMP 2.5:
23650    if ( expression ) */
23651
23652 static tree
23653 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23654 {
23655   tree t, c;
23656
23657   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23658     return list;
23659
23660   t = cp_parser_condition (parser);
23661
23662   if (t == error_mark_node
23663       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23664     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23665                                            /*or_comma=*/false,
23666                                            /*consume_paren=*/true);
23667
23668   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23669
23670   c = build_omp_clause (location, OMP_CLAUSE_IF);
23671   OMP_CLAUSE_IF_EXPR (c) = t;
23672   OMP_CLAUSE_CHAIN (c) = list;
23673
23674   return c;
23675 }
23676
23677 /* OpenMP 2.5:
23678    nowait */
23679
23680 static tree
23681 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23682                              tree list, location_t location)
23683 {
23684   tree c;
23685
23686   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23687
23688   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23689   OMP_CLAUSE_CHAIN (c) = list;
23690   return c;
23691 }
23692
23693 /* OpenMP 2.5:
23694    num_threads ( expression ) */
23695
23696 static tree
23697 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23698                                   location_t location)
23699 {
23700   tree t, c;
23701
23702   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23703     return list;
23704
23705   t = cp_parser_expression (parser, false, NULL);
23706
23707   if (t == error_mark_node
23708       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23709     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23710                                            /*or_comma=*/false,
23711                                            /*consume_paren=*/true);
23712
23713   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23714                              "num_threads", location);
23715
23716   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23717   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23718   OMP_CLAUSE_CHAIN (c) = list;
23719
23720   return c;
23721 }
23722
23723 /* OpenMP 2.5:
23724    ordered */
23725
23726 static tree
23727 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23728                               tree list, location_t location)
23729 {
23730   tree c;
23731
23732   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23733                              "ordered", location);
23734
23735   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23736   OMP_CLAUSE_CHAIN (c) = list;
23737   return c;
23738 }
23739
23740 /* OpenMP 2.5:
23741    reduction ( reduction-operator : variable-list )
23742
23743    reduction-operator:
23744      One of: + * - & ^ | && || */
23745
23746 static tree
23747 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23748 {
23749   enum tree_code code;
23750   tree nlist, c;
23751
23752   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23753     return list;
23754
23755   switch (cp_lexer_peek_token (parser->lexer)->type)
23756     {
23757     case CPP_PLUS:
23758       code = PLUS_EXPR;
23759       break;
23760     case CPP_MULT:
23761       code = MULT_EXPR;
23762       break;
23763     case CPP_MINUS:
23764       code = MINUS_EXPR;
23765       break;
23766     case CPP_AND:
23767       code = BIT_AND_EXPR;
23768       break;
23769     case CPP_XOR:
23770       code = BIT_XOR_EXPR;
23771       break;
23772     case CPP_OR:
23773       code = BIT_IOR_EXPR;
23774       break;
23775     case CPP_AND_AND:
23776       code = TRUTH_ANDIF_EXPR;
23777       break;
23778     case CPP_OR_OR:
23779       code = TRUTH_ORIF_EXPR;
23780       break;
23781     default:
23782       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23783                                "%<|%>, %<&&%>, or %<||%>");
23784     resync_fail:
23785       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23786                                              /*or_comma=*/false,
23787                                              /*consume_paren=*/true);
23788       return list;
23789     }
23790   cp_lexer_consume_token (parser->lexer);
23791
23792   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23793     goto resync_fail;
23794
23795   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23796   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23797     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23798
23799   return nlist;
23800 }
23801
23802 /* OpenMP 2.5:
23803    schedule ( schedule-kind )
23804    schedule ( schedule-kind , expression )
23805
23806    schedule-kind:
23807      static | dynamic | guided | runtime | auto  */
23808
23809 static tree
23810 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23811 {
23812   tree c, t;
23813
23814   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23815     return list;
23816
23817   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23818
23819   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23820     {
23821       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23822       const char *p = IDENTIFIER_POINTER (id);
23823
23824       switch (p[0])
23825         {
23826         case 'd':
23827           if (strcmp ("dynamic", p) != 0)
23828             goto invalid_kind;
23829           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23830           break;
23831
23832         case 'g':
23833           if (strcmp ("guided", p) != 0)
23834             goto invalid_kind;
23835           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23836           break;
23837
23838         case 'r':
23839           if (strcmp ("runtime", p) != 0)
23840             goto invalid_kind;
23841           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23842           break;
23843
23844         default:
23845           goto invalid_kind;
23846         }
23847     }
23848   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23849     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23850   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23851     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23852   else
23853     goto invalid_kind;
23854   cp_lexer_consume_token (parser->lexer);
23855
23856   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23857     {
23858       cp_token *token;
23859       cp_lexer_consume_token (parser->lexer);
23860
23861       token = cp_lexer_peek_token (parser->lexer);
23862       t = cp_parser_assignment_expression (parser, false, NULL);
23863
23864       if (t == error_mark_node)
23865         goto resync_fail;
23866       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23867         error_at (token->location, "schedule %<runtime%> does not take "
23868                   "a %<chunk_size%> parameter");
23869       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23870         error_at (token->location, "schedule %<auto%> does not take "
23871                   "a %<chunk_size%> parameter");
23872       else
23873         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23874
23875       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23876         goto resync_fail;
23877     }
23878   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23879     goto resync_fail;
23880
23881   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23882   OMP_CLAUSE_CHAIN (c) = list;
23883   return c;
23884
23885  invalid_kind:
23886   cp_parser_error (parser, "invalid schedule kind");
23887  resync_fail:
23888   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23889                                          /*or_comma=*/false,
23890                                          /*consume_paren=*/true);
23891   return list;
23892 }
23893
23894 /* OpenMP 3.0:
23895    untied */
23896
23897 static tree
23898 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23899                              tree list, location_t location)
23900 {
23901   tree c;
23902
23903   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23904
23905   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23906   OMP_CLAUSE_CHAIN (c) = list;
23907   return c;
23908 }
23909
23910 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23911    is a bitmask in MASK.  Return the list of clauses found; the result
23912    of clause default goes in *pdefault.  */
23913
23914 static tree
23915 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23916                            const char *where, cp_token *pragma_tok)
23917 {
23918   tree clauses = NULL;
23919   bool first = true;
23920   cp_token *token = NULL;
23921
23922   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23923     {
23924       pragma_omp_clause c_kind;
23925       const char *c_name;
23926       tree prev = clauses;
23927
23928       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23929         cp_lexer_consume_token (parser->lexer);
23930
23931       token = cp_lexer_peek_token (parser->lexer);
23932       c_kind = cp_parser_omp_clause_name (parser);
23933       first = false;
23934
23935       switch (c_kind)
23936         {
23937         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23938           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23939                                                    token->location);
23940           c_name = "collapse";
23941           break;
23942         case PRAGMA_OMP_CLAUSE_COPYIN:
23943           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23944           c_name = "copyin";
23945           break;
23946         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23947           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23948                                             clauses);
23949           c_name = "copyprivate";
23950           break;
23951         case PRAGMA_OMP_CLAUSE_DEFAULT:
23952           clauses = cp_parser_omp_clause_default (parser, clauses,
23953                                                   token->location);
23954           c_name = "default";
23955           break;
23956         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23957           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23958                                             clauses);
23959           c_name = "firstprivate";
23960           break;
23961         case PRAGMA_OMP_CLAUSE_IF:
23962           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23963           c_name = "if";
23964           break;
23965         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23966           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23967                                             clauses);
23968           c_name = "lastprivate";
23969           break;
23970         case PRAGMA_OMP_CLAUSE_NOWAIT:
23971           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23972           c_name = "nowait";
23973           break;
23974         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23975           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23976                                                       token->location);
23977           c_name = "num_threads";
23978           break;
23979         case PRAGMA_OMP_CLAUSE_ORDERED:
23980           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23981                                                   token->location);
23982           c_name = "ordered";
23983           break;
23984         case PRAGMA_OMP_CLAUSE_PRIVATE:
23985           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23986                                             clauses);
23987           c_name = "private";
23988           break;
23989         case PRAGMA_OMP_CLAUSE_REDUCTION:
23990           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23991           c_name = "reduction";
23992           break;
23993         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23994           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23995                                                    token->location);
23996           c_name = "schedule";
23997           break;
23998         case PRAGMA_OMP_CLAUSE_SHARED:
23999           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24000                                             clauses);
24001           c_name = "shared";
24002           break;
24003         case PRAGMA_OMP_CLAUSE_UNTIED:
24004           clauses = cp_parser_omp_clause_untied (parser, clauses,
24005                                                  token->location);
24006           c_name = "nowait";
24007           break;
24008         default:
24009           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24010           goto saw_error;
24011         }
24012
24013       if (((mask >> c_kind) & 1) == 0)
24014         {
24015           /* Remove the invalid clause(s) from the list to avoid
24016              confusing the rest of the compiler.  */
24017           clauses = prev;
24018           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24019         }
24020     }
24021  saw_error:
24022   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24023   return finish_omp_clauses (clauses);
24024 }
24025
24026 /* OpenMP 2.5:
24027    structured-block:
24028      statement
24029
24030    In practice, we're also interested in adding the statement to an
24031    outer node.  So it is convenient if we work around the fact that
24032    cp_parser_statement calls add_stmt.  */
24033
24034 static unsigned
24035 cp_parser_begin_omp_structured_block (cp_parser *parser)
24036 {
24037   unsigned save = parser->in_statement;
24038
24039   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24040      This preserves the "not within loop or switch" style error messages
24041      for nonsense cases like
24042         void foo() {
24043         #pragma omp single
24044           break;
24045         }
24046   */
24047   if (parser->in_statement)
24048     parser->in_statement = IN_OMP_BLOCK;
24049
24050   return save;
24051 }
24052
24053 static void
24054 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24055 {
24056   parser->in_statement = save;
24057 }
24058
24059 static tree
24060 cp_parser_omp_structured_block (cp_parser *parser)
24061 {
24062   tree stmt = begin_omp_structured_block ();
24063   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24064
24065   cp_parser_statement (parser, NULL_TREE, false, NULL);
24066
24067   cp_parser_end_omp_structured_block (parser, save);
24068   return finish_omp_structured_block (stmt);
24069 }
24070
24071 /* OpenMP 2.5:
24072    # pragma omp atomic new-line
24073      expression-stmt
24074
24075    expression-stmt:
24076      x binop= expr | x++ | ++x | x-- | --x
24077    binop:
24078      +, *, -, /, &, ^, |, <<, >>
24079
24080   where x is an lvalue expression with scalar type.  */
24081
24082 static void
24083 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24084 {
24085   tree lhs, rhs;
24086   enum tree_code code;
24087
24088   cp_parser_require_pragma_eol (parser, pragma_tok);
24089
24090   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24091                                     /*cast_p=*/false, NULL);
24092   switch (TREE_CODE (lhs))
24093     {
24094     case ERROR_MARK:
24095       goto saw_error;
24096
24097     case PREINCREMENT_EXPR:
24098     case POSTINCREMENT_EXPR:
24099       lhs = TREE_OPERAND (lhs, 0);
24100       code = PLUS_EXPR;
24101       rhs = integer_one_node;
24102       break;
24103
24104     case PREDECREMENT_EXPR:
24105     case POSTDECREMENT_EXPR:
24106       lhs = TREE_OPERAND (lhs, 0);
24107       code = MINUS_EXPR;
24108       rhs = integer_one_node;
24109       break;
24110
24111     case COMPOUND_EXPR:
24112       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24113          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24114          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24115          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24116          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24117                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24118             == BOOLEAN_TYPE)
24119        /* Undo effects of boolean_increment for post {in,de}crement.  */
24120        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24121       /* FALLTHRU */
24122     case MODIFY_EXPR:
24123       if (TREE_CODE (lhs) == MODIFY_EXPR
24124          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24125        {
24126          /* Undo effects of boolean_increment.  */
24127          if (integer_onep (TREE_OPERAND (lhs, 1)))
24128            {
24129              /* This is pre or post increment.  */
24130              rhs = TREE_OPERAND (lhs, 1);
24131              lhs = TREE_OPERAND (lhs, 0);
24132              code = NOP_EXPR;
24133              break;
24134            }
24135        }
24136       /* FALLTHRU */
24137     default:
24138       switch (cp_lexer_peek_token (parser->lexer)->type)
24139         {
24140         case CPP_MULT_EQ:
24141           code = MULT_EXPR;
24142           break;
24143         case CPP_DIV_EQ:
24144           code = TRUNC_DIV_EXPR;
24145           break;
24146         case CPP_PLUS_EQ:
24147           code = PLUS_EXPR;
24148           break;
24149         case CPP_MINUS_EQ:
24150           code = MINUS_EXPR;
24151           break;
24152         case CPP_LSHIFT_EQ:
24153           code = LSHIFT_EXPR;
24154           break;
24155         case CPP_RSHIFT_EQ:
24156           code = RSHIFT_EXPR;
24157           break;
24158         case CPP_AND_EQ:
24159           code = BIT_AND_EXPR;
24160           break;
24161         case CPP_OR_EQ:
24162           code = BIT_IOR_EXPR;
24163           break;
24164         case CPP_XOR_EQ:
24165           code = BIT_XOR_EXPR;
24166           break;
24167         default:
24168           cp_parser_error (parser,
24169                            "invalid operator for %<#pragma omp atomic%>");
24170           goto saw_error;
24171         }
24172       cp_lexer_consume_token (parser->lexer);
24173
24174       rhs = cp_parser_expression (parser, false, NULL);
24175       if (rhs == error_mark_node)
24176         goto saw_error;
24177       break;
24178     }
24179   finish_omp_atomic (code, lhs, rhs);
24180   cp_parser_consume_semicolon_at_end_of_statement (parser);
24181   return;
24182
24183  saw_error:
24184   cp_parser_skip_to_end_of_block_or_statement (parser);
24185 }
24186
24187
24188 /* OpenMP 2.5:
24189    # pragma omp barrier new-line  */
24190
24191 static void
24192 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24193 {
24194   cp_parser_require_pragma_eol (parser, pragma_tok);
24195   finish_omp_barrier ();
24196 }
24197
24198 /* OpenMP 2.5:
24199    # pragma omp critical [(name)] new-line
24200      structured-block  */
24201
24202 static tree
24203 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24204 {
24205   tree stmt, name = NULL;
24206
24207   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24208     {
24209       cp_lexer_consume_token (parser->lexer);
24210
24211       name = cp_parser_identifier (parser);
24212
24213       if (name == error_mark_node
24214           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24215         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24216                                                /*or_comma=*/false,
24217                                                /*consume_paren=*/true);
24218       if (name == error_mark_node)
24219         name = NULL;
24220     }
24221   cp_parser_require_pragma_eol (parser, pragma_tok);
24222
24223   stmt = cp_parser_omp_structured_block (parser);
24224   return c_finish_omp_critical (input_location, stmt, name);
24225 }
24226
24227 /* OpenMP 2.5:
24228    # pragma omp flush flush-vars[opt] new-line
24229
24230    flush-vars:
24231      ( variable-list ) */
24232
24233 static void
24234 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24235 {
24236   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24237     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24238   cp_parser_require_pragma_eol (parser, pragma_tok);
24239
24240   finish_omp_flush ();
24241 }
24242
24243 /* Helper function, to parse omp for increment expression.  */
24244
24245 static tree
24246 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24247 {
24248   tree cond = cp_parser_binary_expression (parser, false, true,
24249                                            PREC_NOT_OPERATOR, NULL);
24250   bool overloaded_p;
24251
24252   if (cond == error_mark_node
24253       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24254     {
24255       cp_parser_skip_to_end_of_statement (parser);
24256       return error_mark_node;
24257     }
24258
24259   switch (TREE_CODE (cond))
24260     {
24261     case GT_EXPR:
24262     case GE_EXPR:
24263     case LT_EXPR:
24264     case LE_EXPR:
24265       break;
24266     default:
24267       return error_mark_node;
24268     }
24269
24270   /* If decl is an iterator, preserve LHS and RHS of the relational
24271      expr until finish_omp_for.  */
24272   if (decl
24273       && (type_dependent_expression_p (decl)
24274           || CLASS_TYPE_P (TREE_TYPE (decl))))
24275     return cond;
24276
24277   return build_x_binary_op (TREE_CODE (cond),
24278                             TREE_OPERAND (cond, 0), ERROR_MARK,
24279                             TREE_OPERAND (cond, 1), ERROR_MARK,
24280                             &overloaded_p, tf_warning_or_error);
24281 }
24282
24283 /* Helper function, to parse omp for increment expression.  */
24284
24285 static tree
24286 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24287 {
24288   cp_token *token = cp_lexer_peek_token (parser->lexer);
24289   enum tree_code op;
24290   tree lhs, rhs;
24291   cp_id_kind idk;
24292   bool decl_first;
24293
24294   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24295     {
24296       op = (token->type == CPP_PLUS_PLUS
24297             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24298       cp_lexer_consume_token (parser->lexer);
24299       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24300       if (lhs != decl)
24301         return error_mark_node;
24302       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24303     }
24304
24305   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24306   if (lhs != decl)
24307     return error_mark_node;
24308
24309   token = cp_lexer_peek_token (parser->lexer);
24310   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24311     {
24312       op = (token->type == CPP_PLUS_PLUS
24313             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24314       cp_lexer_consume_token (parser->lexer);
24315       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24316     }
24317
24318   op = cp_parser_assignment_operator_opt (parser);
24319   if (op == ERROR_MARK)
24320     return error_mark_node;
24321
24322   if (op != NOP_EXPR)
24323     {
24324       rhs = cp_parser_assignment_expression (parser, false, NULL);
24325       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24326       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24327     }
24328
24329   lhs = cp_parser_binary_expression (parser, false, false,
24330                                      PREC_ADDITIVE_EXPRESSION, NULL);
24331   token = cp_lexer_peek_token (parser->lexer);
24332   decl_first = lhs == decl;
24333   if (decl_first)
24334     lhs = NULL_TREE;
24335   if (token->type != CPP_PLUS
24336       && token->type != CPP_MINUS)
24337     return error_mark_node;
24338
24339   do
24340     {
24341       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24342       cp_lexer_consume_token (parser->lexer);
24343       rhs = cp_parser_binary_expression (parser, false, false,
24344                                          PREC_ADDITIVE_EXPRESSION, NULL);
24345       token = cp_lexer_peek_token (parser->lexer);
24346       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24347         {
24348           if (lhs == NULL_TREE)
24349             {
24350               if (op == PLUS_EXPR)
24351                 lhs = rhs;
24352               else
24353                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24354             }
24355           else
24356             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24357                                      NULL, tf_warning_or_error);
24358         }
24359     }
24360   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24361
24362   if (!decl_first)
24363     {
24364       if (rhs != decl || op == MINUS_EXPR)
24365         return error_mark_node;
24366       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24367     }
24368   else
24369     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24370
24371   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24372 }
24373
24374 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24375
24376 static tree
24377 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24378 {
24379   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24380   tree real_decl, initv, condv, incrv, declv;
24381   tree this_pre_body, cl;
24382   location_t loc_first;
24383   bool collapse_err = false;
24384   int i, collapse = 1, nbraces = 0;
24385   VEC(tree,gc) *for_block = make_tree_vector ();
24386
24387   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24388     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24389       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24390
24391   gcc_assert (collapse >= 1);
24392
24393   declv = make_tree_vec (collapse);
24394   initv = make_tree_vec (collapse);
24395   condv = make_tree_vec (collapse);
24396   incrv = make_tree_vec (collapse);
24397
24398   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24399
24400   for (i = 0; i < collapse; i++)
24401     {
24402       int bracecount = 0;
24403       bool add_private_clause = false;
24404       location_t loc;
24405
24406       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24407         {
24408           cp_parser_error (parser, "for statement expected");
24409           return NULL;
24410         }
24411       loc = cp_lexer_consume_token (parser->lexer)->location;
24412
24413       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24414         return NULL;
24415
24416       init = decl = real_decl = NULL;
24417       this_pre_body = push_stmt_list ();
24418       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24419         {
24420           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24421
24422              init-expr:
24423                        var = lb
24424                        integer-type var = lb
24425                        random-access-iterator-type var = lb
24426                        pointer-type var = lb
24427           */
24428           cp_decl_specifier_seq type_specifiers;
24429
24430           /* First, try to parse as an initialized declaration.  See
24431              cp_parser_condition, from whence the bulk of this is copied.  */
24432
24433           cp_parser_parse_tentatively (parser);
24434           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24435                                         /*is_trailing_return=*/false,
24436                                         &type_specifiers);
24437           if (cp_parser_parse_definitely (parser))
24438             {
24439               /* If parsing a type specifier seq succeeded, then this
24440                  MUST be a initialized declaration.  */
24441               tree asm_specification, attributes;
24442               cp_declarator *declarator;
24443
24444               declarator = cp_parser_declarator (parser,
24445                                                  CP_PARSER_DECLARATOR_NAMED,
24446                                                  /*ctor_dtor_or_conv_p=*/NULL,
24447                                                  /*parenthesized_p=*/NULL,
24448                                                  /*member_p=*/false);
24449               attributes = cp_parser_attributes_opt (parser);
24450               asm_specification = cp_parser_asm_specification_opt (parser);
24451
24452               if (declarator == cp_error_declarator) 
24453                 cp_parser_skip_to_end_of_statement (parser);
24454
24455               else 
24456                 {
24457                   tree pushed_scope, auto_node;
24458
24459                   decl = start_decl (declarator, &type_specifiers,
24460                                      SD_INITIALIZED, attributes,
24461                                      /*prefix_attributes=*/NULL_TREE,
24462                                      &pushed_scope);
24463
24464                   auto_node = type_uses_auto (TREE_TYPE (decl));
24465                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24466                     {
24467                       if (cp_lexer_next_token_is (parser->lexer, 
24468                                                   CPP_OPEN_PAREN))
24469                         error ("parenthesized initialization is not allowed in "
24470                                "OpenMP %<for%> loop");
24471                       else
24472                         /* Trigger an error.  */
24473                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24474
24475                       init = error_mark_node;
24476                       cp_parser_skip_to_end_of_statement (parser);
24477                     }
24478                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24479                            || type_dependent_expression_p (decl)
24480                            || auto_node)
24481                     {
24482                       bool is_direct_init, is_non_constant_init;
24483
24484                       init = cp_parser_initializer (parser,
24485                                                     &is_direct_init,
24486                                                     &is_non_constant_init);
24487
24488                       if (auto_node && describable_type (init))
24489                         {
24490                           TREE_TYPE (decl)
24491                             = do_auto_deduction (TREE_TYPE (decl), init,
24492                                                  auto_node);
24493
24494                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24495                               && !type_dependent_expression_p (decl))
24496                             goto non_class;
24497                         }
24498                       
24499                       cp_finish_decl (decl, init, !is_non_constant_init,
24500                                       asm_specification,
24501                                       LOOKUP_ONLYCONVERTING);
24502                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24503                         {
24504                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24505                           init = NULL_TREE;
24506                         }
24507                       else
24508                         init = pop_stmt_list (this_pre_body);
24509                       this_pre_body = NULL_TREE;
24510                     }
24511                   else
24512                     {
24513                       /* Consume '='.  */
24514                       cp_lexer_consume_token (parser->lexer);
24515                       init = cp_parser_assignment_expression (parser, false, NULL);
24516
24517                     non_class:
24518                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24519                         init = error_mark_node;
24520                       else
24521                         cp_finish_decl (decl, NULL_TREE,
24522                                         /*init_const_expr_p=*/false,
24523                                         asm_specification,
24524                                         LOOKUP_ONLYCONVERTING);
24525                     }
24526
24527                   if (pushed_scope)
24528                     pop_scope (pushed_scope);
24529                 }
24530             }
24531           else 
24532             {
24533               cp_id_kind idk;
24534               /* If parsing a type specifier sequence failed, then
24535                  this MUST be a simple expression.  */
24536               cp_parser_parse_tentatively (parser);
24537               decl = cp_parser_primary_expression (parser, false, false,
24538                                                    false, &idk);
24539               if (!cp_parser_error_occurred (parser)
24540                   && decl
24541                   && DECL_P (decl)
24542                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24543                 {
24544                   tree rhs;
24545
24546                   cp_parser_parse_definitely (parser);
24547                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24548                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24549                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24550                                                          rhs,
24551                                                          tf_warning_or_error));
24552                   add_private_clause = true;
24553                 }
24554               else
24555                 {
24556                   decl = NULL;
24557                   cp_parser_abort_tentative_parse (parser);
24558                   init = cp_parser_expression (parser, false, NULL);
24559                   if (init)
24560                     {
24561                       if (TREE_CODE (init) == MODIFY_EXPR
24562                           || TREE_CODE (init) == MODOP_EXPR)
24563                         real_decl = TREE_OPERAND (init, 0);
24564                     }
24565                 }
24566             }
24567         }
24568       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24569       if (this_pre_body)
24570         {
24571           this_pre_body = pop_stmt_list (this_pre_body);
24572           if (pre_body)
24573             {
24574               tree t = pre_body;
24575               pre_body = push_stmt_list ();
24576               add_stmt (t);
24577               add_stmt (this_pre_body);
24578               pre_body = pop_stmt_list (pre_body);
24579             }
24580           else
24581             pre_body = this_pre_body;
24582         }
24583
24584       if (decl)
24585         real_decl = decl;
24586       if (par_clauses != NULL && real_decl != NULL_TREE)
24587         {
24588           tree *c;
24589           for (c = par_clauses; *c ; )
24590             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24591                 && OMP_CLAUSE_DECL (*c) == real_decl)
24592               {
24593                 error_at (loc, "iteration variable %qD"
24594                           " should not be firstprivate", real_decl);
24595                 *c = OMP_CLAUSE_CHAIN (*c);
24596               }
24597             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24598                      && OMP_CLAUSE_DECL (*c) == real_decl)
24599               {
24600                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24601                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24602                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24603                 OMP_CLAUSE_DECL (l) = real_decl;
24604                 OMP_CLAUSE_CHAIN (l) = clauses;
24605                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24606                 clauses = l;
24607                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24608                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24609                 add_private_clause = false;
24610               }
24611             else
24612               {
24613                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24614                     && OMP_CLAUSE_DECL (*c) == real_decl)
24615                   add_private_clause = false;
24616                 c = &OMP_CLAUSE_CHAIN (*c);
24617               }
24618         }
24619
24620       if (add_private_clause)
24621         {
24622           tree c;
24623           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24624             {
24625               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24626                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24627                   && OMP_CLAUSE_DECL (c) == decl)
24628                 break;
24629               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24630                        && OMP_CLAUSE_DECL (c) == decl)
24631                 error_at (loc, "iteration variable %qD "
24632                           "should not be firstprivate",
24633                           decl);
24634               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24635                        && OMP_CLAUSE_DECL (c) == decl)
24636                 error_at (loc, "iteration variable %qD should not be reduction",
24637                           decl);
24638             }
24639           if (c == NULL)
24640             {
24641               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24642               OMP_CLAUSE_DECL (c) = decl;
24643               c = finish_omp_clauses (c);
24644               if (c)
24645                 {
24646                   OMP_CLAUSE_CHAIN (c) = clauses;
24647                   clauses = c;
24648                 }
24649             }
24650         }
24651
24652       cond = NULL;
24653       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24654         cond = cp_parser_omp_for_cond (parser, decl);
24655       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24656
24657       incr = NULL;
24658       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24659         {
24660           /* If decl is an iterator, preserve the operator on decl
24661              until finish_omp_for.  */
24662           if (decl
24663               && ((type_dependent_expression_p (decl)
24664                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24665                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24666             incr = cp_parser_omp_for_incr (parser, decl);
24667           else
24668             incr = cp_parser_expression (parser, false, NULL);
24669         }
24670
24671       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24672         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24673                                                /*or_comma=*/false,
24674                                                /*consume_paren=*/true);
24675
24676       TREE_VEC_ELT (declv, i) = decl;
24677       TREE_VEC_ELT (initv, i) = init;
24678       TREE_VEC_ELT (condv, i) = cond;
24679       TREE_VEC_ELT (incrv, i) = incr;
24680
24681       if (i == collapse - 1)
24682         break;
24683
24684       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24685          in between the collapsed for loops to be still considered perfectly
24686          nested.  Hopefully the final version clarifies this.
24687          For now handle (multiple) {'s and empty statements.  */
24688       cp_parser_parse_tentatively (parser);
24689       do
24690         {
24691           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24692             break;
24693           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24694             {
24695               cp_lexer_consume_token (parser->lexer);
24696               bracecount++;
24697             }
24698           else if (bracecount
24699                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24700             cp_lexer_consume_token (parser->lexer);
24701           else
24702             {
24703               loc = cp_lexer_peek_token (parser->lexer)->location;
24704               error_at (loc, "not enough collapsed for loops");
24705               collapse_err = true;
24706               cp_parser_abort_tentative_parse (parser);
24707               declv = NULL_TREE;
24708               break;
24709             }
24710         }
24711       while (1);
24712
24713       if (declv)
24714         {
24715           cp_parser_parse_definitely (parser);
24716           nbraces += bracecount;
24717         }
24718     }
24719
24720   /* Note that we saved the original contents of this flag when we entered
24721      the structured block, and so we don't need to re-save it here.  */
24722   parser->in_statement = IN_OMP_FOR;
24723
24724   /* Note that the grammar doesn't call for a structured block here,
24725      though the loop as a whole is a structured block.  */
24726   body = push_stmt_list ();
24727   cp_parser_statement (parser, NULL_TREE, false, NULL);
24728   body = pop_stmt_list (body);
24729
24730   if (declv == NULL_TREE)
24731     ret = NULL_TREE;
24732   else
24733     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24734                           pre_body, clauses);
24735
24736   while (nbraces)
24737     {
24738       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24739         {
24740           cp_lexer_consume_token (parser->lexer);
24741           nbraces--;
24742         }
24743       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24744         cp_lexer_consume_token (parser->lexer);
24745       else
24746         {
24747           if (!collapse_err)
24748             {
24749               error_at (cp_lexer_peek_token (parser->lexer)->location,
24750                         "collapsed loops not perfectly nested");
24751             }
24752           collapse_err = true;
24753           cp_parser_statement_seq_opt (parser, NULL);
24754           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24755             break;
24756         }
24757     }
24758
24759   while (!VEC_empty (tree, for_block))
24760     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24761   release_tree_vector (for_block);
24762
24763   return ret;
24764 }
24765
24766 /* OpenMP 2.5:
24767    #pragma omp for for-clause[optseq] new-line
24768      for-loop  */
24769
24770 #define OMP_FOR_CLAUSE_MASK                             \
24771         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24772         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24773         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24774         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24775         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24776         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24777         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24778         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24779
24780 static tree
24781 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24782 {
24783   tree clauses, sb, ret;
24784   unsigned int save;
24785
24786   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24787                                        "#pragma omp for", pragma_tok);
24788
24789   sb = begin_omp_structured_block ();
24790   save = cp_parser_begin_omp_structured_block (parser);
24791
24792   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24793
24794   cp_parser_end_omp_structured_block (parser, save);
24795   add_stmt (finish_omp_structured_block (sb));
24796
24797   return ret;
24798 }
24799
24800 /* OpenMP 2.5:
24801    # pragma omp master new-line
24802      structured-block  */
24803
24804 static tree
24805 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24806 {
24807   cp_parser_require_pragma_eol (parser, pragma_tok);
24808   return c_finish_omp_master (input_location,
24809                               cp_parser_omp_structured_block (parser));
24810 }
24811
24812 /* OpenMP 2.5:
24813    # pragma omp ordered new-line
24814      structured-block  */
24815
24816 static tree
24817 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24818 {
24819   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24820   cp_parser_require_pragma_eol (parser, pragma_tok);
24821   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24822 }
24823
24824 /* OpenMP 2.5:
24825
24826    section-scope:
24827      { section-sequence }
24828
24829    section-sequence:
24830      section-directive[opt] structured-block
24831      section-sequence section-directive structured-block  */
24832
24833 static tree
24834 cp_parser_omp_sections_scope (cp_parser *parser)
24835 {
24836   tree stmt, substmt;
24837   bool error_suppress = false;
24838   cp_token *tok;
24839
24840   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24841     return NULL_TREE;
24842
24843   stmt = push_stmt_list ();
24844
24845   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24846     {
24847       unsigned save;
24848
24849       substmt = begin_omp_structured_block ();
24850       save = cp_parser_begin_omp_structured_block (parser);
24851
24852       while (1)
24853         {
24854           cp_parser_statement (parser, NULL_TREE, false, NULL);
24855
24856           tok = cp_lexer_peek_token (parser->lexer);
24857           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24858             break;
24859           if (tok->type == CPP_CLOSE_BRACE)
24860             break;
24861           if (tok->type == CPP_EOF)
24862             break;
24863         }
24864
24865       cp_parser_end_omp_structured_block (parser, save);
24866       substmt = finish_omp_structured_block (substmt);
24867       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24868       add_stmt (substmt);
24869     }
24870
24871   while (1)
24872     {
24873       tok = cp_lexer_peek_token (parser->lexer);
24874       if (tok->type == CPP_CLOSE_BRACE)
24875         break;
24876       if (tok->type == CPP_EOF)
24877         break;
24878
24879       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24880         {
24881           cp_lexer_consume_token (parser->lexer);
24882           cp_parser_require_pragma_eol (parser, tok);
24883           error_suppress = false;
24884         }
24885       else if (!error_suppress)
24886         {
24887           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24888           error_suppress = true;
24889         }
24890
24891       substmt = cp_parser_omp_structured_block (parser);
24892       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24893       add_stmt (substmt);
24894     }
24895   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24896
24897   substmt = pop_stmt_list (stmt);
24898
24899   stmt = make_node (OMP_SECTIONS);
24900   TREE_TYPE (stmt) = void_type_node;
24901   OMP_SECTIONS_BODY (stmt) = substmt;
24902
24903   add_stmt (stmt);
24904   return stmt;
24905 }
24906
24907 /* OpenMP 2.5:
24908    # pragma omp sections sections-clause[optseq] newline
24909      sections-scope  */
24910
24911 #define OMP_SECTIONS_CLAUSE_MASK                        \
24912         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24913         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24914         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24915         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24916         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24917
24918 static tree
24919 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24920 {
24921   tree clauses, ret;
24922
24923   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24924                                        "#pragma omp sections", pragma_tok);
24925
24926   ret = cp_parser_omp_sections_scope (parser);
24927   if (ret)
24928     OMP_SECTIONS_CLAUSES (ret) = clauses;
24929
24930   return ret;
24931 }
24932
24933 /* OpenMP 2.5:
24934    # pragma parallel parallel-clause new-line
24935    # pragma parallel for parallel-for-clause new-line
24936    # pragma parallel sections parallel-sections-clause new-line  */
24937
24938 #define OMP_PARALLEL_CLAUSE_MASK                        \
24939         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24940         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24941         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24942         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24943         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24944         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24945         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24946         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24947
24948 static tree
24949 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24950 {
24951   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24952   const char *p_name = "#pragma omp parallel";
24953   tree stmt, clauses, par_clause, ws_clause, block;
24954   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24955   unsigned int save;
24956   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24957
24958   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24959     {
24960       cp_lexer_consume_token (parser->lexer);
24961       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24962       p_name = "#pragma omp parallel for";
24963       mask |= OMP_FOR_CLAUSE_MASK;
24964       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24965     }
24966   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24967     {
24968       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24969       const char *p = IDENTIFIER_POINTER (id);
24970       if (strcmp (p, "sections") == 0)
24971         {
24972           cp_lexer_consume_token (parser->lexer);
24973           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24974           p_name = "#pragma omp parallel sections";
24975           mask |= OMP_SECTIONS_CLAUSE_MASK;
24976           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24977         }
24978     }
24979
24980   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24981   block = begin_omp_parallel ();
24982   save = cp_parser_begin_omp_structured_block (parser);
24983
24984   switch (p_kind)
24985     {
24986     case PRAGMA_OMP_PARALLEL:
24987       cp_parser_statement (parser, NULL_TREE, false, NULL);
24988       par_clause = clauses;
24989       break;
24990
24991     case PRAGMA_OMP_PARALLEL_FOR:
24992       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24993       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24994       break;
24995
24996     case PRAGMA_OMP_PARALLEL_SECTIONS:
24997       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24998       stmt = cp_parser_omp_sections_scope (parser);
24999       if (stmt)
25000         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25001       break;
25002
25003     default:
25004       gcc_unreachable ();
25005     }
25006
25007   cp_parser_end_omp_structured_block (parser, save);
25008   stmt = finish_omp_parallel (par_clause, block);
25009   if (p_kind != PRAGMA_OMP_PARALLEL)
25010     OMP_PARALLEL_COMBINED (stmt) = 1;
25011   return stmt;
25012 }
25013
25014 /* OpenMP 2.5:
25015    # pragma omp single single-clause[optseq] new-line
25016      structured-block  */
25017
25018 #define OMP_SINGLE_CLAUSE_MASK                          \
25019         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25020         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25021         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25022         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25023
25024 static tree
25025 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25026 {
25027   tree stmt = make_node (OMP_SINGLE);
25028   TREE_TYPE (stmt) = void_type_node;
25029
25030   OMP_SINGLE_CLAUSES (stmt)
25031     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25032                                  "#pragma omp single", pragma_tok);
25033   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25034
25035   return add_stmt (stmt);
25036 }
25037
25038 /* OpenMP 3.0:
25039    # pragma omp task task-clause[optseq] new-line
25040      structured-block  */
25041
25042 #define OMP_TASK_CLAUSE_MASK                            \
25043         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25044         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25045         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25046         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25047         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25048         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25049
25050 static tree
25051 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25052 {
25053   tree clauses, block;
25054   unsigned int save;
25055
25056   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25057                                        "#pragma omp task", pragma_tok);
25058   block = begin_omp_task ();
25059   save = cp_parser_begin_omp_structured_block (parser);
25060   cp_parser_statement (parser, NULL_TREE, false, NULL);
25061   cp_parser_end_omp_structured_block (parser, save);
25062   return finish_omp_task (clauses, block);
25063 }
25064
25065 /* OpenMP 3.0:
25066    # pragma omp taskwait new-line  */
25067
25068 static void
25069 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25070 {
25071   cp_parser_require_pragma_eol (parser, pragma_tok);
25072   finish_omp_taskwait ();
25073 }
25074
25075 /* OpenMP 2.5:
25076    # pragma omp threadprivate (variable-list) */
25077
25078 static void
25079 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25080 {
25081   tree vars;
25082
25083   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25084   cp_parser_require_pragma_eol (parser, pragma_tok);
25085
25086   finish_omp_threadprivate (vars);
25087 }
25088
25089 /* Main entry point to OpenMP statement pragmas.  */
25090
25091 static void
25092 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25093 {
25094   tree stmt;
25095
25096   switch (pragma_tok->pragma_kind)
25097     {
25098     case PRAGMA_OMP_ATOMIC:
25099       cp_parser_omp_atomic (parser, pragma_tok);
25100       return;
25101     case PRAGMA_OMP_CRITICAL:
25102       stmt = cp_parser_omp_critical (parser, pragma_tok);
25103       break;
25104     case PRAGMA_OMP_FOR:
25105       stmt = cp_parser_omp_for (parser, pragma_tok);
25106       break;
25107     case PRAGMA_OMP_MASTER:
25108       stmt = cp_parser_omp_master (parser, pragma_tok);
25109       break;
25110     case PRAGMA_OMP_ORDERED:
25111       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25112       break;
25113     case PRAGMA_OMP_PARALLEL:
25114       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25115       break;
25116     case PRAGMA_OMP_SECTIONS:
25117       stmt = cp_parser_omp_sections (parser, pragma_tok);
25118       break;
25119     case PRAGMA_OMP_SINGLE:
25120       stmt = cp_parser_omp_single (parser, pragma_tok);
25121       break;
25122     case PRAGMA_OMP_TASK:
25123       stmt = cp_parser_omp_task (parser, pragma_tok);
25124       break;
25125     default:
25126       gcc_unreachable ();
25127     }
25128
25129   if (stmt)
25130     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25131 }
25132 \f
25133 /* The parser.  */
25134
25135 static GTY (()) cp_parser *the_parser;
25136
25137 \f
25138 /* Special handling for the first token or line in the file.  The first
25139    thing in the file might be #pragma GCC pch_preprocess, which loads a
25140    PCH file, which is a GC collection point.  So we need to handle this
25141    first pragma without benefit of an existing lexer structure.
25142
25143    Always returns one token to the caller in *FIRST_TOKEN.  This is
25144    either the true first token of the file, or the first token after
25145    the initial pragma.  */
25146
25147 static void
25148 cp_parser_initial_pragma (cp_token *first_token)
25149 {
25150   tree name = NULL;
25151
25152   cp_lexer_get_preprocessor_token (NULL, first_token);
25153   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25154     return;
25155
25156   cp_lexer_get_preprocessor_token (NULL, first_token);
25157   if (first_token->type == CPP_STRING)
25158     {
25159       name = first_token->u.value;
25160
25161       cp_lexer_get_preprocessor_token (NULL, first_token);
25162       if (first_token->type != CPP_PRAGMA_EOL)
25163         error_at (first_token->location,
25164                   "junk at end of %<#pragma GCC pch_preprocess%>");
25165     }
25166   else
25167     error_at (first_token->location, "expected string literal");
25168
25169   /* Skip to the end of the pragma.  */
25170   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25171     cp_lexer_get_preprocessor_token (NULL, first_token);
25172
25173   /* Now actually load the PCH file.  */
25174   if (name)
25175     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25176
25177   /* Read one more token to return to our caller.  We have to do this
25178      after reading the PCH file in, since its pointers have to be
25179      live.  */
25180   cp_lexer_get_preprocessor_token (NULL, first_token);
25181 }
25182
25183 /* Normal parsing of a pragma token.  Here we can (and must) use the
25184    regular lexer.  */
25185
25186 static bool
25187 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25188 {
25189   cp_token *pragma_tok;
25190   unsigned int id;
25191
25192   pragma_tok = cp_lexer_consume_token (parser->lexer);
25193   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25194   parser->lexer->in_pragma = true;
25195
25196   id = pragma_tok->pragma_kind;
25197   switch (id)
25198     {
25199     case PRAGMA_GCC_PCH_PREPROCESS:
25200       error_at (pragma_tok->location,
25201                 "%<#pragma GCC pch_preprocess%> must be first");
25202       break;
25203
25204     case PRAGMA_OMP_BARRIER:
25205       switch (context)
25206         {
25207         case pragma_compound:
25208           cp_parser_omp_barrier (parser, pragma_tok);
25209           return false;
25210         case pragma_stmt:
25211           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25212                     "used in compound statements");
25213           break;
25214         default:
25215           goto bad_stmt;
25216         }
25217       break;
25218
25219     case PRAGMA_OMP_FLUSH:
25220       switch (context)
25221         {
25222         case pragma_compound:
25223           cp_parser_omp_flush (parser, pragma_tok);
25224           return false;
25225         case pragma_stmt:
25226           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25227                     "used in compound statements");
25228           break;
25229         default:
25230           goto bad_stmt;
25231         }
25232       break;
25233
25234     case PRAGMA_OMP_TASKWAIT:
25235       switch (context)
25236         {
25237         case pragma_compound:
25238           cp_parser_omp_taskwait (parser, pragma_tok);
25239           return false;
25240         case pragma_stmt:
25241           error_at (pragma_tok->location,
25242                     "%<#pragma omp taskwait%> may only be "
25243                     "used in compound statements");
25244           break;
25245         default:
25246           goto bad_stmt;
25247         }
25248       break;
25249
25250     case PRAGMA_OMP_THREADPRIVATE:
25251       cp_parser_omp_threadprivate (parser, pragma_tok);
25252       return false;
25253
25254     case PRAGMA_OMP_ATOMIC:
25255     case PRAGMA_OMP_CRITICAL:
25256     case PRAGMA_OMP_FOR:
25257     case PRAGMA_OMP_MASTER:
25258     case PRAGMA_OMP_ORDERED:
25259     case PRAGMA_OMP_PARALLEL:
25260     case PRAGMA_OMP_SECTIONS:
25261     case PRAGMA_OMP_SINGLE:
25262     case PRAGMA_OMP_TASK:
25263       if (context == pragma_external)
25264         goto bad_stmt;
25265       cp_parser_omp_construct (parser, pragma_tok);
25266       return true;
25267
25268     case PRAGMA_OMP_SECTION:
25269       error_at (pragma_tok->location, 
25270                 "%<#pragma omp section%> may only be used in "
25271                 "%<#pragma omp sections%> construct");
25272       break;
25273
25274     default:
25275       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25276       c_invoke_pragma_handler (id);
25277       break;
25278
25279     bad_stmt:
25280       cp_parser_error (parser, "expected declaration specifiers");
25281       break;
25282     }
25283
25284   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25285   return false;
25286 }
25287
25288 /* The interface the pragma parsers have to the lexer.  */
25289
25290 enum cpp_ttype
25291 pragma_lex (tree *value)
25292 {
25293   cp_token *tok;
25294   enum cpp_ttype ret;
25295
25296   tok = cp_lexer_peek_token (the_parser->lexer);
25297
25298   ret = tok->type;
25299   *value = tok->u.value;
25300
25301   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25302     ret = CPP_EOF;
25303   else if (ret == CPP_STRING)
25304     *value = cp_parser_string_literal (the_parser, false, false);
25305   else
25306     {
25307       cp_lexer_consume_token (the_parser->lexer);
25308       if (ret == CPP_KEYWORD)
25309         ret = CPP_NAME;
25310     }
25311
25312   return ret;
25313 }
25314
25315 \f
25316 /* External interface.  */
25317
25318 /* Parse one entire translation unit.  */
25319
25320 void
25321 c_parse_file (void)
25322 {
25323   static bool already_called = false;
25324
25325   if (already_called)
25326     {
25327       sorry ("inter-module optimizations not implemented for C++");
25328       return;
25329     }
25330   already_called = true;
25331
25332   the_parser = cp_parser_new ();
25333   push_deferring_access_checks (flag_access_control
25334                                 ? dk_no_deferred : dk_no_check);
25335   cp_parser_translation_unit (the_parser);
25336   the_parser = NULL;
25337 }
25338
25339 #include "gt-cp-parser.h"