OSDN Git Service

cf9286a21dd11ba1aa3d9573d80e7d466e17a1ac
[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 void 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 class %qT 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                 || constructor_name_p (token->u.value, scope)))
4073           {
4074             cp_lexer_consume_token (parser->lexer);
4075             return build_nt (BIT_NOT_EXPR, scope);
4076           }
4077
4078         /* If there was an explicit qualification (S::~T), first look
4079            in the scope given by the qualification (i.e., S).
4080
4081            Note: in the calls to cp_parser_class_name below we pass
4082            typename_type so that lookup finds the injected-class-name
4083            rather than the constructor.  */
4084         done = false;
4085         type_decl = NULL_TREE;
4086         if (scope)
4087           {
4088             cp_parser_parse_tentatively (parser);
4089             type_decl = cp_parser_class_name (parser,
4090                                               /*typename_keyword_p=*/false,
4091                                               /*template_keyword_p=*/false,
4092                                               typename_type,
4093                                               /*check_dependency=*/false,
4094                                               /*class_head_p=*/false,
4095                                               declarator_p);
4096             if (cp_parser_parse_definitely (parser))
4097               done = true;
4098           }
4099         /* In "N::S::~S", look in "N" as well.  */
4100         if (!done && scope && qualifying_scope)
4101           {
4102             cp_parser_parse_tentatively (parser);
4103             parser->scope = qualifying_scope;
4104             parser->object_scope = NULL_TREE;
4105             parser->qualifying_scope = NULL_TREE;
4106             type_decl
4107               = cp_parser_class_name (parser,
4108                                       /*typename_keyword_p=*/false,
4109                                       /*template_keyword_p=*/false,
4110                                       typename_type,
4111                                       /*check_dependency=*/false,
4112                                       /*class_head_p=*/false,
4113                                       declarator_p);
4114             if (cp_parser_parse_definitely (parser))
4115               done = true;
4116           }
4117         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4118         else if (!done && object_scope)
4119           {
4120             cp_parser_parse_tentatively (parser);
4121             parser->scope = object_scope;
4122             parser->object_scope = NULL_TREE;
4123             parser->qualifying_scope = NULL_TREE;
4124             type_decl
4125               = cp_parser_class_name (parser,
4126                                       /*typename_keyword_p=*/false,
4127                                       /*template_keyword_p=*/false,
4128                                       typename_type,
4129                                       /*check_dependency=*/false,
4130                                       /*class_head_p=*/false,
4131                                       declarator_p);
4132             if (cp_parser_parse_definitely (parser))
4133               done = true;
4134           }
4135         /* Look in the surrounding context.  */
4136         if (!done)
4137           {
4138             parser->scope = NULL_TREE;
4139             parser->object_scope = NULL_TREE;
4140             parser->qualifying_scope = NULL_TREE;
4141             if (processing_template_decl)
4142               cp_parser_parse_tentatively (parser);
4143             type_decl
4144               = cp_parser_class_name (parser,
4145                                       /*typename_keyword_p=*/false,
4146                                       /*template_keyword_p=*/false,
4147                                       typename_type,
4148                                       /*check_dependency=*/false,
4149                                       /*class_head_p=*/false,
4150                                       declarator_p);
4151             if (processing_template_decl
4152                 && ! cp_parser_parse_definitely (parser))
4153               {
4154                 /* We couldn't find a type with this name, so just accept
4155                    it and check for a match at instantiation time.  */
4156                 type_decl = cp_parser_identifier (parser);
4157                 if (type_decl != error_mark_node)
4158                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4159                 return type_decl;
4160               }
4161           }
4162         /* If an error occurred, assume that the name of the
4163            destructor is the same as the name of the qualifying
4164            class.  That allows us to keep parsing after running
4165            into ill-formed destructor names.  */
4166         if (type_decl == error_mark_node && scope)
4167           return build_nt (BIT_NOT_EXPR, scope);
4168         else if (type_decl == error_mark_node)
4169           return error_mark_node;
4170
4171         /* Check that destructor name and scope match.  */
4172         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4173           {
4174             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4175               error_at (token->location,
4176                         "declaration of %<~%T%> as member of %qT",
4177                         type_decl, scope);
4178             cp_parser_simulate_error (parser);
4179             return error_mark_node;
4180           }
4181
4182         /* [class.dtor]
4183
4184            A typedef-name that names a class shall not be used as the
4185            identifier in the declarator for a destructor declaration.  */
4186         if (declarator_p
4187             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4188             && !DECL_SELF_REFERENCE_P (type_decl)
4189             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4190           error_at (token->location,
4191                     "typedef-name %qD used as destructor declarator",
4192                     type_decl);
4193
4194         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4195       }
4196
4197     case CPP_KEYWORD:
4198       if (token->keyword == RID_OPERATOR)
4199         {
4200           tree id;
4201
4202           /* This could be a template-id, so we try that first.  */
4203           cp_parser_parse_tentatively (parser);
4204           /* Try a template-id.  */
4205           id = cp_parser_template_id (parser, template_keyword_p,
4206                                       /*check_dependency_p=*/true,
4207                                       declarator_p);
4208           /* If that worked, we're done.  */
4209           if (cp_parser_parse_definitely (parser))
4210             return id;
4211           /* We still don't know whether we're looking at an
4212              operator-function-id or a conversion-function-id.  */
4213           cp_parser_parse_tentatively (parser);
4214           /* Try an operator-function-id.  */
4215           id = cp_parser_operator_function_id (parser);
4216           /* If that didn't work, try a conversion-function-id.  */
4217           if (!cp_parser_parse_definitely (parser))
4218             id = cp_parser_conversion_function_id (parser);
4219
4220           return id;
4221         }
4222       /* Fall through.  */
4223
4224     default:
4225       if (optional_p)
4226         return NULL_TREE;
4227       cp_parser_error (parser, "expected unqualified-id");
4228       return error_mark_node;
4229     }
4230 }
4231
4232 /* Parse an (optional) nested-name-specifier.
4233
4234    nested-name-specifier: [C++98]
4235      class-or-namespace-name :: nested-name-specifier [opt]
4236      class-or-namespace-name :: template nested-name-specifier [opt]
4237
4238    nested-name-specifier: [C++0x]
4239      type-name ::
4240      namespace-name ::
4241      nested-name-specifier identifier ::
4242      nested-name-specifier template [opt] simple-template-id ::
4243
4244    PARSER->SCOPE should be set appropriately before this function is
4245    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4246    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4247    in name lookups.
4248
4249    Sets PARSER->SCOPE to the class (TYPE) or namespace
4250    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4251    it unchanged if there is no nested-name-specifier.  Returns the new
4252    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4253
4254    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4255    part of a declaration and/or decl-specifier.  */
4256
4257 static tree
4258 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4259                                      bool typename_keyword_p,
4260                                      bool check_dependency_p,
4261                                      bool type_p,
4262                                      bool is_declaration)
4263 {
4264   bool success = false;
4265   cp_token_position start = 0;
4266   cp_token *token;
4267
4268   /* Remember where the nested-name-specifier starts.  */
4269   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4270     {
4271       start = cp_lexer_token_position (parser->lexer, false);
4272       push_deferring_access_checks (dk_deferred);
4273     }
4274
4275   while (true)
4276     {
4277       tree new_scope;
4278       tree old_scope;
4279       tree saved_qualifying_scope;
4280       bool template_keyword_p;
4281
4282       /* Spot cases that cannot be the beginning of a
4283          nested-name-specifier.  */
4284       token = cp_lexer_peek_token (parser->lexer);
4285
4286       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4287          the already parsed nested-name-specifier.  */
4288       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4289         {
4290           /* Grab the nested-name-specifier and continue the loop.  */
4291           cp_parser_pre_parsed_nested_name_specifier (parser);
4292           /* If we originally encountered this nested-name-specifier
4293              with IS_DECLARATION set to false, we will not have
4294              resolved TYPENAME_TYPEs, so we must do so here.  */
4295           if (is_declaration
4296               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4297             {
4298               new_scope = resolve_typename_type (parser->scope,
4299                                                  /*only_current_p=*/false);
4300               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4301                 parser->scope = new_scope;
4302             }
4303           success = true;
4304           continue;
4305         }
4306
4307       /* Spot cases that cannot be the beginning of a
4308          nested-name-specifier.  On the second and subsequent times
4309          through the loop, we look for the `template' keyword.  */
4310       if (success && token->keyword == RID_TEMPLATE)
4311         ;
4312       /* A template-id can start a nested-name-specifier.  */
4313       else if (token->type == CPP_TEMPLATE_ID)
4314         ;
4315       else
4316         {
4317           /* If the next token is not an identifier, then it is
4318              definitely not a type-name or namespace-name.  */
4319           if (token->type != CPP_NAME)
4320             break;
4321           /* If the following token is neither a `<' (to begin a
4322              template-id), nor a `::', then we are not looking at a
4323              nested-name-specifier.  */
4324           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4325
4326           if (token->type == CPP_COLON
4327               && parser->colon_corrects_to_scope_p
4328               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4329             {
4330               error_at (token->location,
4331                         "found %<:%> in nested-name-specifier, expected %<::%>");
4332               token->type = CPP_SCOPE;
4333             }
4334
4335           if (token->type != CPP_SCOPE
4336               && !cp_parser_nth_token_starts_template_argument_list_p
4337                   (parser, 2))
4338             break;
4339         }
4340
4341       /* The nested-name-specifier is optional, so we parse
4342          tentatively.  */
4343       cp_parser_parse_tentatively (parser);
4344
4345       /* Look for the optional `template' keyword, if this isn't the
4346          first time through the loop.  */
4347       if (success)
4348         template_keyword_p = cp_parser_optional_template_keyword (parser);
4349       else
4350         template_keyword_p = false;
4351
4352       /* Save the old scope since the name lookup we are about to do
4353          might destroy it.  */
4354       old_scope = parser->scope;
4355       saved_qualifying_scope = parser->qualifying_scope;
4356       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4357          look up names in "X<T>::I" in order to determine that "Y" is
4358          a template.  So, if we have a typename at this point, we make
4359          an effort to look through it.  */
4360       if (is_declaration
4361           && !typename_keyword_p
4362           && parser->scope
4363           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4364         parser->scope = resolve_typename_type (parser->scope,
4365                                                /*only_current_p=*/false);
4366       /* Parse the qualifying entity.  */
4367       new_scope
4368         = cp_parser_qualifying_entity (parser,
4369                                        typename_keyword_p,
4370                                        template_keyword_p,
4371                                        check_dependency_p,
4372                                        type_p,
4373                                        is_declaration);
4374       /* Look for the `::' token.  */
4375       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4376
4377       /* If we found what we wanted, we keep going; otherwise, we're
4378          done.  */
4379       if (!cp_parser_parse_definitely (parser))
4380         {
4381           bool error_p = false;
4382
4383           /* Restore the OLD_SCOPE since it was valid before the
4384              failed attempt at finding the last
4385              class-or-namespace-name.  */
4386           parser->scope = old_scope;
4387           parser->qualifying_scope = saved_qualifying_scope;
4388           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4389             break;
4390           /* If the next token is an identifier, and the one after
4391              that is a `::', then any valid interpretation would have
4392              found a class-or-namespace-name.  */
4393           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4394                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4395                      == CPP_SCOPE)
4396                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4397                      != CPP_COMPL))
4398             {
4399               token = cp_lexer_consume_token (parser->lexer);
4400               if (!error_p)
4401                 {
4402                   if (!token->ambiguous_p)
4403                     {
4404                       tree decl;
4405                       tree ambiguous_decls;
4406
4407                       decl = cp_parser_lookup_name (parser, token->u.value,
4408                                                     none_type,
4409                                                     /*is_template=*/false,
4410                                                     /*is_namespace=*/false,
4411                                                     /*check_dependency=*/true,
4412                                                     &ambiguous_decls,
4413                                                     token->location);
4414                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4415                         error_at (token->location,
4416                                   "%qD used without template parameters",
4417                                   decl);
4418                       else if (ambiguous_decls)
4419                         {
4420                           error_at (token->location,
4421                                     "reference to %qD is ambiguous",
4422                                     token->u.value);
4423                           print_candidates (ambiguous_decls);
4424                           decl = error_mark_node;
4425                         }
4426                       else
4427                         {
4428                           if (cxx_dialect != cxx98)
4429                             cp_parser_name_lookup_error
4430                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4431                              token->location);
4432                           else
4433                             cp_parser_name_lookup_error
4434                             (parser, token->u.value, decl, NLE_CXX98,
4435                              token->location);
4436                         }
4437                     }
4438                   parser->scope = error_mark_node;
4439                   error_p = true;
4440                   /* Treat this as a successful nested-name-specifier
4441                      due to:
4442
4443                      [basic.lookup.qual]
4444
4445                      If the name found is not a class-name (clause
4446                      _class_) or namespace-name (_namespace.def_), the
4447                      program is ill-formed.  */
4448                   success = true;
4449                 }
4450               cp_lexer_consume_token (parser->lexer);
4451             }
4452           break;
4453         }
4454       /* We've found one valid nested-name-specifier.  */
4455       success = true;
4456       /* Name lookup always gives us a DECL.  */
4457       if (TREE_CODE (new_scope) == TYPE_DECL)
4458         new_scope = TREE_TYPE (new_scope);
4459       /* Uses of "template" must be followed by actual templates.  */
4460       if (template_keyword_p
4461           && !(CLASS_TYPE_P (new_scope)
4462                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4463                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4464                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4465           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4466                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4467                    == TEMPLATE_ID_EXPR)))
4468         permerror (input_location, TYPE_P (new_scope)
4469                    ? "%qT is not a template"
4470                    : "%qD is not a template",
4471                    new_scope);
4472       /* If it is a class scope, try to complete it; we are about to
4473          be looking up names inside the class.  */
4474       if (TYPE_P (new_scope)
4475           /* Since checking types for dependency can be expensive,
4476              avoid doing it if the type is already complete.  */
4477           && !COMPLETE_TYPE_P (new_scope)
4478           /* Do not try to complete dependent types.  */
4479           && !dependent_type_p (new_scope))
4480         {
4481           new_scope = complete_type (new_scope);
4482           /* If it is a typedef to current class, use the current
4483              class instead, as the typedef won't have any names inside
4484              it yet.  */
4485           if (!COMPLETE_TYPE_P (new_scope)
4486               && currently_open_class (new_scope))
4487             new_scope = TYPE_MAIN_VARIANT (new_scope);
4488         }
4489       /* Make sure we look in the right scope the next time through
4490          the loop.  */
4491       parser->scope = new_scope;
4492     }
4493
4494   /* If parsing tentatively, replace the sequence of tokens that makes
4495      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4496      token.  That way, should we re-parse the token stream, we will
4497      not have to repeat the effort required to do the parse, nor will
4498      we issue duplicate error messages.  */
4499   if (success && start)
4500     {
4501       cp_token *token;
4502
4503       token = cp_lexer_token_at (parser->lexer, start);
4504       /* Reset the contents of the START token.  */
4505       token->type = CPP_NESTED_NAME_SPECIFIER;
4506       /* Retrieve any deferred checks.  Do not pop this access checks yet
4507          so the memory will not be reclaimed during token replacing below.  */
4508       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4509       token->u.tree_check_value->value = parser->scope;
4510       token->u.tree_check_value->checks = get_deferred_access_checks ();
4511       token->u.tree_check_value->qualifying_scope =
4512         parser->qualifying_scope;
4513       token->keyword = RID_MAX;
4514
4515       /* Purge all subsequent tokens.  */
4516       cp_lexer_purge_tokens_after (parser->lexer, start);
4517     }
4518
4519   if (start)
4520     pop_to_parent_deferring_access_checks ();
4521
4522   return success ? parser->scope : NULL_TREE;
4523 }
4524
4525 /* Parse a nested-name-specifier.  See
4526    cp_parser_nested_name_specifier_opt for details.  This function
4527    behaves identically, except that it will an issue an error if no
4528    nested-name-specifier is present.  */
4529
4530 static tree
4531 cp_parser_nested_name_specifier (cp_parser *parser,
4532                                  bool typename_keyword_p,
4533                                  bool check_dependency_p,
4534                                  bool type_p,
4535                                  bool is_declaration)
4536 {
4537   tree scope;
4538
4539   /* Look for the nested-name-specifier.  */
4540   scope = cp_parser_nested_name_specifier_opt (parser,
4541                                                typename_keyword_p,
4542                                                check_dependency_p,
4543                                                type_p,
4544                                                is_declaration);
4545   /* If it was not present, issue an error message.  */
4546   if (!scope)
4547     {
4548       cp_parser_error (parser, "expected nested-name-specifier");
4549       parser->scope = NULL_TREE;
4550     }
4551
4552   return scope;
4553 }
4554
4555 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4556    this is either a class-name or a namespace-name (which corresponds
4557    to the class-or-namespace-name production in the grammar). For
4558    C++0x, it can also be a type-name that refers to an enumeration
4559    type.
4560
4561    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4562    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4563    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4564    TYPE_P is TRUE iff the next name should be taken as a class-name,
4565    even the same name is declared to be another entity in the same
4566    scope.
4567
4568    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4569    specified by the class-or-namespace-name.  If neither is found the
4570    ERROR_MARK_NODE is returned.  */
4571
4572 static tree
4573 cp_parser_qualifying_entity (cp_parser *parser,
4574                              bool typename_keyword_p,
4575                              bool template_keyword_p,
4576                              bool check_dependency_p,
4577                              bool type_p,
4578                              bool is_declaration)
4579 {
4580   tree saved_scope;
4581   tree saved_qualifying_scope;
4582   tree saved_object_scope;
4583   tree scope;
4584   bool only_class_p;
4585   bool successful_parse_p;
4586
4587   /* Before we try to parse the class-name, we must save away the
4588      current PARSER->SCOPE since cp_parser_class_name will destroy
4589      it.  */
4590   saved_scope = parser->scope;
4591   saved_qualifying_scope = parser->qualifying_scope;
4592   saved_object_scope = parser->object_scope;
4593   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4594      there is no need to look for a namespace-name.  */
4595   only_class_p = template_keyword_p 
4596     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4597   if (!only_class_p)
4598     cp_parser_parse_tentatively (parser);
4599   scope = cp_parser_class_name (parser,
4600                                 typename_keyword_p,
4601                                 template_keyword_p,
4602                                 type_p ? class_type : none_type,
4603                                 check_dependency_p,
4604                                 /*class_head_p=*/false,
4605                                 is_declaration);
4606   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4607   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4608   if (!only_class_p 
4609       && cxx_dialect != cxx98
4610       && !successful_parse_p)
4611     {
4612       /* Restore the saved scope.  */
4613       parser->scope = saved_scope;
4614       parser->qualifying_scope = saved_qualifying_scope;
4615       parser->object_scope = saved_object_scope;
4616
4617       /* Parse tentatively.  */
4618       cp_parser_parse_tentatively (parser);
4619      
4620       /* Parse a typedef-name or enum-name.  */
4621       scope = cp_parser_nonclass_name (parser);
4622
4623       /* "If the name found does not designate a namespace or a class,
4624          enumeration, or dependent type, the program is ill-formed."
4625
4626          We cover classes and dependent types above and namespaces below,
4627          so this code is only looking for enums.  */
4628       if (!scope || TREE_CODE (scope) != TYPE_DECL
4629           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4630         cp_parser_simulate_error (parser);
4631
4632       successful_parse_p = cp_parser_parse_definitely (parser);
4633     }
4634   /* If that didn't work, try for a namespace-name.  */
4635   if (!only_class_p && !successful_parse_p)
4636     {
4637       /* Restore the saved scope.  */
4638       parser->scope = saved_scope;
4639       parser->qualifying_scope = saved_qualifying_scope;
4640       parser->object_scope = saved_object_scope;
4641       /* If we are not looking at an identifier followed by the scope
4642          resolution operator, then this is not part of a
4643          nested-name-specifier.  (Note that this function is only used
4644          to parse the components of a nested-name-specifier.)  */
4645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4646           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4647         return error_mark_node;
4648       scope = cp_parser_namespace_name (parser);
4649     }
4650
4651   return scope;
4652 }
4653
4654 /* Parse a postfix-expression.
4655
4656    postfix-expression:
4657      primary-expression
4658      postfix-expression [ expression ]
4659      postfix-expression ( expression-list [opt] )
4660      simple-type-specifier ( expression-list [opt] )
4661      typename :: [opt] nested-name-specifier identifier
4662        ( expression-list [opt] )
4663      typename :: [opt] nested-name-specifier template [opt] template-id
4664        ( expression-list [opt] )
4665      postfix-expression . template [opt] id-expression
4666      postfix-expression -> template [opt] id-expression
4667      postfix-expression . pseudo-destructor-name
4668      postfix-expression -> pseudo-destructor-name
4669      postfix-expression ++
4670      postfix-expression --
4671      dynamic_cast < type-id > ( expression )
4672      static_cast < type-id > ( expression )
4673      reinterpret_cast < type-id > ( expression )
4674      const_cast < type-id > ( expression )
4675      typeid ( expression )
4676      typeid ( type-id )
4677
4678    GNU Extension:
4679
4680    postfix-expression:
4681      ( type-id ) { initializer-list , [opt] }
4682
4683    This extension is a GNU version of the C99 compound-literal
4684    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4685    but they are essentially the same concept.)
4686
4687    If ADDRESS_P is true, the postfix expression is the operand of the
4688    `&' operator.  CAST_P is true if this expression is the target of a
4689    cast.
4690
4691    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4692    class member access expressions [expr.ref].
4693
4694    Returns a representation of the expression.  */
4695
4696 static tree
4697 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4698                               bool member_access_only_p,
4699                               cp_id_kind * pidk_return)
4700 {
4701   cp_token *token;
4702   enum rid keyword;
4703   cp_id_kind idk = CP_ID_KIND_NONE;
4704   tree postfix_expression = NULL_TREE;
4705   bool is_member_access = false;
4706
4707   /* Peek at the next token.  */
4708   token = cp_lexer_peek_token (parser->lexer);
4709   /* Some of the productions are determined by keywords.  */
4710   keyword = token->keyword;
4711   switch (keyword)
4712     {
4713     case RID_DYNCAST:
4714     case RID_STATCAST:
4715     case RID_REINTCAST:
4716     case RID_CONSTCAST:
4717       {
4718         tree type;
4719         tree expression;
4720         const char *saved_message;
4721
4722         /* All of these can be handled in the same way from the point
4723            of view of parsing.  Begin by consuming the token
4724            identifying the cast.  */
4725         cp_lexer_consume_token (parser->lexer);
4726
4727         /* New types cannot be defined in the cast.  */
4728         saved_message = parser->type_definition_forbidden_message;
4729         parser->type_definition_forbidden_message
4730           = G_("types may not be defined in casts");
4731
4732         /* Look for the opening `<'.  */
4733         cp_parser_require (parser, CPP_LESS, RT_LESS);
4734         /* Parse the type to which we are casting.  */
4735         type = cp_parser_type_id (parser);
4736         /* Look for the closing `>'.  */
4737         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4738         /* Restore the old message.  */
4739         parser->type_definition_forbidden_message = saved_message;
4740
4741         /* And the expression which is being cast.  */
4742         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4743         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4744         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4745
4746         /* Only type conversions to integral or enumeration types
4747            can be used in constant-expressions.  */
4748         if (!cast_valid_in_integral_constant_expression_p (type)
4749             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4750           return error_mark_node;
4751
4752         switch (keyword)
4753           {
4754           case RID_DYNCAST:
4755             postfix_expression
4756               = build_dynamic_cast (type, expression, tf_warning_or_error);
4757             break;
4758           case RID_STATCAST:
4759             postfix_expression
4760               = build_static_cast (type, expression, tf_warning_or_error);
4761             break;
4762           case RID_REINTCAST:
4763             postfix_expression
4764               = build_reinterpret_cast (type, expression, 
4765                                         tf_warning_or_error);
4766             break;
4767           case RID_CONSTCAST:
4768             postfix_expression
4769               = build_const_cast (type, expression, tf_warning_or_error);
4770             break;
4771           default:
4772             gcc_unreachable ();
4773           }
4774       }
4775       break;
4776
4777     case RID_TYPEID:
4778       {
4779         tree type;
4780         const char *saved_message;
4781         bool saved_in_type_id_in_expr_p;
4782
4783         /* Consume the `typeid' token.  */
4784         cp_lexer_consume_token (parser->lexer);
4785         /* Look for the `(' token.  */
4786         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4787         /* Types cannot be defined in a `typeid' expression.  */
4788         saved_message = parser->type_definition_forbidden_message;
4789         parser->type_definition_forbidden_message
4790           = G_("types may not be defined in a %<typeid%> expression");
4791         /* We can't be sure yet whether we're looking at a type-id or an
4792            expression.  */
4793         cp_parser_parse_tentatively (parser);
4794         /* Try a type-id first.  */
4795         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4796         parser->in_type_id_in_expr_p = true;
4797         type = cp_parser_type_id (parser);
4798         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4799         /* Look for the `)' token.  Otherwise, we can't be sure that
4800            we're not looking at an expression: consider `typeid (int
4801            (3))', for example.  */
4802         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4803         /* If all went well, simply lookup the type-id.  */
4804         if (cp_parser_parse_definitely (parser))
4805           postfix_expression = get_typeid (type);
4806         /* Otherwise, fall back to the expression variant.  */
4807         else
4808           {
4809             tree expression;
4810
4811             /* Look for an expression.  */
4812             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4813             /* Compute its typeid.  */
4814             postfix_expression = build_typeid (expression);
4815             /* Look for the `)' token.  */
4816             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4817           }
4818         /* Restore the saved message.  */
4819         parser->type_definition_forbidden_message = saved_message;
4820         /* `typeid' may not appear in an integral constant expression.  */
4821         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4822           return error_mark_node;
4823       }
4824       break;
4825
4826     case RID_TYPENAME:
4827       {
4828         tree type;
4829         /* The syntax permitted here is the same permitted for an
4830            elaborated-type-specifier.  */
4831         type = cp_parser_elaborated_type_specifier (parser,
4832                                                     /*is_friend=*/false,
4833                                                     /*is_declaration=*/false);
4834         postfix_expression = cp_parser_functional_cast (parser, type);
4835       }
4836       break;
4837
4838     default:
4839       {
4840         tree type;
4841
4842         /* If the next thing is a simple-type-specifier, we may be
4843            looking at a functional cast.  We could also be looking at
4844            an id-expression.  So, we try the functional cast, and if
4845            that doesn't work we fall back to the primary-expression.  */
4846         cp_parser_parse_tentatively (parser);
4847         /* Look for the simple-type-specifier.  */
4848         type = cp_parser_simple_type_specifier (parser,
4849                                                 /*decl_specs=*/NULL,
4850                                                 CP_PARSER_FLAGS_NONE);
4851         /* Parse the cast itself.  */
4852         if (!cp_parser_error_occurred (parser))
4853           postfix_expression
4854             = cp_parser_functional_cast (parser, type);
4855         /* If that worked, we're done.  */
4856         if (cp_parser_parse_definitely (parser))
4857           break;
4858
4859         /* If the functional-cast didn't work out, try a
4860            compound-literal.  */
4861         if (cp_parser_allow_gnu_extensions_p (parser)
4862             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4863           {
4864             VEC(constructor_elt,gc) *initializer_list = NULL;
4865             bool saved_in_type_id_in_expr_p;
4866
4867             cp_parser_parse_tentatively (parser);
4868             /* Consume the `('.  */
4869             cp_lexer_consume_token (parser->lexer);
4870             /* Parse the type.  */
4871             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4872             parser->in_type_id_in_expr_p = true;
4873             type = cp_parser_type_id (parser);
4874             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4875             /* Look for the `)'.  */
4876             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4877             /* Look for the `{'.  */
4878             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4879             /* If things aren't going well, there's no need to
4880                keep going.  */
4881             if (!cp_parser_error_occurred (parser))
4882               {
4883                 bool non_constant_p;
4884                 /* Parse the initializer-list.  */
4885                 initializer_list
4886                   = cp_parser_initializer_list (parser, &non_constant_p);
4887                 /* Allow a trailing `,'.  */
4888                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4889                   cp_lexer_consume_token (parser->lexer);
4890                 /* Look for the final `}'.  */
4891                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4892               }
4893             /* If that worked, we're definitely looking at a
4894                compound-literal expression.  */
4895             if (cp_parser_parse_definitely (parser))
4896               {
4897                 /* Warn the user that a compound literal is not
4898                    allowed in standard C++.  */
4899                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4900                 /* For simplicity, we disallow compound literals in
4901                    constant-expressions.  We could
4902                    allow compound literals of integer type, whose
4903                    initializer was a constant, in constant
4904                    expressions.  Permitting that usage, as a further
4905                    extension, would not change the meaning of any
4906                    currently accepted programs.  (Of course, as
4907                    compound literals are not part of ISO C++, the
4908                    standard has nothing to say.)  */
4909                 if (cp_parser_non_integral_constant_expression (parser,
4910                                                                 NIC_NCC))
4911                   {
4912                     postfix_expression = error_mark_node;
4913                     break;
4914                   }
4915                 /* Form the representation of the compound-literal.  */
4916                 postfix_expression
4917                   = (finish_compound_literal
4918                      (type, build_constructor (init_list_type_node,
4919                                                initializer_list),
4920                       tf_warning_or_error));
4921                 break;
4922               }
4923           }
4924
4925         /* It must be a primary-expression.  */
4926         postfix_expression
4927           = cp_parser_primary_expression (parser, address_p, cast_p,
4928                                           /*template_arg_p=*/false,
4929                                           &idk);
4930       }
4931       break;
4932     }
4933
4934   /* Keep looping until the postfix-expression is complete.  */
4935   while (true)
4936     {
4937       if (idk == CP_ID_KIND_UNQUALIFIED
4938           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4939           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4940         /* It is not a Koenig lookup function call.  */
4941         postfix_expression
4942           = unqualified_name_lookup_error (postfix_expression);
4943
4944       /* Peek at the next token.  */
4945       token = cp_lexer_peek_token (parser->lexer);
4946
4947       switch (token->type)
4948         {
4949         case CPP_OPEN_SQUARE:
4950           postfix_expression
4951             = cp_parser_postfix_open_square_expression (parser,
4952                                                         postfix_expression,
4953                                                         false);
4954           idk = CP_ID_KIND_NONE;
4955           is_member_access = false;
4956           break;
4957
4958         case CPP_OPEN_PAREN:
4959           /* postfix-expression ( expression-list [opt] ) */
4960           {
4961             bool koenig_p;
4962             bool is_builtin_constant_p;
4963             bool saved_integral_constant_expression_p = false;
4964             bool saved_non_integral_constant_expression_p = false;
4965             VEC(tree,gc) *args;
4966
4967             is_member_access = false;
4968
4969             is_builtin_constant_p
4970               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4971             if (is_builtin_constant_p)
4972               {
4973                 /* The whole point of __builtin_constant_p is to allow
4974                    non-constant expressions to appear as arguments.  */
4975                 saved_integral_constant_expression_p
4976                   = parser->integral_constant_expression_p;
4977                 saved_non_integral_constant_expression_p
4978                   = parser->non_integral_constant_expression_p;
4979                 parser->integral_constant_expression_p = false;
4980               }
4981             args = (cp_parser_parenthesized_expression_list
4982                     (parser, non_attr,
4983                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4984                      /*non_constant_p=*/NULL));
4985             if (is_builtin_constant_p)
4986               {
4987                 parser->integral_constant_expression_p
4988                   = saved_integral_constant_expression_p;
4989                 parser->non_integral_constant_expression_p
4990                   = saved_non_integral_constant_expression_p;
4991               }
4992
4993             if (args == NULL)
4994               {
4995                 postfix_expression = error_mark_node;
4996                 break;
4997               }
4998
4999             /* Function calls are not permitted in
5000                constant-expressions.  */
5001             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5002                 && cp_parser_non_integral_constant_expression (parser,
5003                                                                NIC_FUNC_CALL))
5004               {
5005                 postfix_expression = error_mark_node;
5006                 release_tree_vector (args);
5007                 break;
5008               }
5009
5010             koenig_p = false;
5011             if (idk == CP_ID_KIND_UNQUALIFIED
5012                 || idk == CP_ID_KIND_TEMPLATE_ID)
5013               {
5014                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5015                   {
5016                     if (!VEC_empty (tree, args))
5017                       {
5018                         koenig_p = true;
5019                         if (!any_type_dependent_arguments_p (args))
5020                           postfix_expression
5021                             = perform_koenig_lookup (postfix_expression, args,
5022                                                      /*include_std=*/false);
5023                       }
5024                     else
5025                       postfix_expression
5026                         = unqualified_fn_lookup_error (postfix_expression);
5027                   }
5028                 /* We do not perform argument-dependent lookup if
5029                    normal lookup finds a non-function, in accordance
5030                    with the expected resolution of DR 218.  */
5031                 else if (!VEC_empty (tree, args)
5032                          && is_overloaded_fn (postfix_expression))
5033                   {
5034                     tree fn = get_first_fn (postfix_expression);
5035                     fn = STRIP_TEMPLATE (fn);
5036
5037                     /* Do not do argument dependent lookup if regular
5038                        lookup finds a member function or a block-scope
5039                        function declaration.  [basic.lookup.argdep]/3  */
5040                     if (!DECL_FUNCTION_MEMBER_P (fn)
5041                         && !DECL_LOCAL_FUNCTION_P (fn))
5042                       {
5043                         koenig_p = true;
5044                         if (!any_type_dependent_arguments_p (args))
5045                           postfix_expression
5046                             = perform_koenig_lookup (postfix_expression, args,
5047                                                      /*include_std=*/false);
5048                       }
5049                   }
5050               }
5051
5052             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5053               {
5054                 tree instance = TREE_OPERAND (postfix_expression, 0);
5055                 tree fn = TREE_OPERAND (postfix_expression, 1);
5056
5057                 if (processing_template_decl
5058                     && (type_dependent_expression_p (instance)
5059                         || (!BASELINK_P (fn)
5060                             && TREE_CODE (fn) != FIELD_DECL)
5061                         || type_dependent_expression_p (fn)
5062                         || any_type_dependent_arguments_p (args)))
5063                   {
5064                     postfix_expression
5065                       = build_nt_call_vec (postfix_expression, args);
5066                     release_tree_vector (args);
5067                     break;
5068                   }
5069
5070                 if (BASELINK_P (fn))
5071                   {
5072                   postfix_expression
5073                     = (build_new_method_call
5074                        (instance, fn, &args, NULL_TREE,
5075                         (idk == CP_ID_KIND_QUALIFIED
5076                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5077                          : LOOKUP_NORMAL),
5078                         /*fn_p=*/NULL,
5079                         tf_warning_or_error));
5080                   }
5081                 else
5082                   postfix_expression
5083                     = finish_call_expr (postfix_expression, &args,
5084                                         /*disallow_virtual=*/false,
5085                                         /*koenig_p=*/false,
5086                                         tf_warning_or_error);
5087               }
5088             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5089                      || TREE_CODE (postfix_expression) == MEMBER_REF
5090                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5091               postfix_expression = (build_offset_ref_call_from_tree
5092                                     (postfix_expression, &args));
5093             else if (idk == CP_ID_KIND_QUALIFIED)
5094               /* A call to a static class member, or a namespace-scope
5095                  function.  */
5096               postfix_expression
5097                 = finish_call_expr (postfix_expression, &args,
5098                                     /*disallow_virtual=*/true,
5099                                     koenig_p,
5100                                     tf_warning_or_error);
5101             else
5102               /* All other function calls.  */
5103               postfix_expression
5104                 = finish_call_expr (postfix_expression, &args,
5105                                     /*disallow_virtual=*/false,
5106                                     koenig_p,
5107                                     tf_warning_or_error);
5108
5109             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5110             idk = CP_ID_KIND_NONE;
5111
5112             release_tree_vector (args);
5113           }
5114           break;
5115
5116         case CPP_DOT:
5117         case CPP_DEREF:
5118           /* postfix-expression . template [opt] id-expression
5119              postfix-expression . pseudo-destructor-name
5120              postfix-expression -> template [opt] id-expression
5121              postfix-expression -> pseudo-destructor-name */
5122
5123           /* Consume the `.' or `->' operator.  */
5124           cp_lexer_consume_token (parser->lexer);
5125
5126           postfix_expression
5127             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5128                                                       postfix_expression,
5129                                                       false, &idk,
5130                                                       token->location);
5131
5132           is_member_access = true;
5133           break;
5134
5135         case CPP_PLUS_PLUS:
5136           /* postfix-expression ++  */
5137           /* Consume the `++' token.  */
5138           cp_lexer_consume_token (parser->lexer);
5139           /* Generate a representation for the complete expression.  */
5140           postfix_expression
5141             = finish_increment_expr (postfix_expression,
5142                                      POSTINCREMENT_EXPR);
5143           /* Increments may not appear in constant-expressions.  */
5144           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5145             postfix_expression = error_mark_node;
5146           idk = CP_ID_KIND_NONE;
5147           is_member_access = false;
5148           break;
5149
5150         case CPP_MINUS_MINUS:
5151           /* postfix-expression -- */
5152           /* Consume the `--' token.  */
5153           cp_lexer_consume_token (parser->lexer);
5154           /* Generate a representation for the complete expression.  */
5155           postfix_expression
5156             = finish_increment_expr (postfix_expression,
5157                                      POSTDECREMENT_EXPR);
5158           /* Decrements may not appear in constant-expressions.  */
5159           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5160             postfix_expression = error_mark_node;
5161           idk = CP_ID_KIND_NONE;
5162           is_member_access = false;
5163           break;
5164
5165         default:
5166           if (pidk_return != NULL)
5167             * pidk_return = idk;
5168           if (member_access_only_p)
5169             return is_member_access? postfix_expression : error_mark_node;
5170           else
5171             return postfix_expression;
5172         }
5173     }
5174
5175   /* We should never get here.  */
5176   gcc_unreachable ();
5177   return error_mark_node;
5178 }
5179
5180 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5181    by cp_parser_builtin_offsetof.  We're looking for
5182
5183      postfix-expression [ expression ]
5184
5185    FOR_OFFSETOF is set if we're being called in that context, which
5186    changes how we deal with integer constant expressions.  */
5187
5188 static tree
5189 cp_parser_postfix_open_square_expression (cp_parser *parser,
5190                                           tree postfix_expression,
5191                                           bool for_offsetof)
5192 {
5193   tree index;
5194
5195   /* Consume the `[' token.  */
5196   cp_lexer_consume_token (parser->lexer);
5197
5198   /* Parse the index expression.  */
5199   /* ??? For offsetof, there is a question of what to allow here.  If
5200      offsetof is not being used in an integral constant expression context,
5201      then we *could* get the right answer by computing the value at runtime.
5202      If we are in an integral constant expression context, then we might
5203      could accept any constant expression; hard to say without analysis.
5204      Rather than open the barn door too wide right away, allow only integer
5205      constant expressions here.  */
5206   if (for_offsetof)
5207     index = cp_parser_constant_expression (parser, false, NULL);
5208   else
5209     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5210
5211   /* Look for the closing `]'.  */
5212   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5213
5214   /* Build the ARRAY_REF.  */
5215   postfix_expression = grok_array_decl (postfix_expression, index);
5216
5217   /* When not doing offsetof, array references are not permitted in
5218      constant-expressions.  */
5219   if (!for_offsetof
5220       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5221     postfix_expression = error_mark_node;
5222
5223   return postfix_expression;
5224 }
5225
5226 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5227    by cp_parser_builtin_offsetof.  We're looking for
5228
5229      postfix-expression . template [opt] id-expression
5230      postfix-expression . pseudo-destructor-name
5231      postfix-expression -> template [opt] id-expression
5232      postfix-expression -> pseudo-destructor-name
5233
5234    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5235    limits what of the above we'll actually accept, but nevermind.
5236    TOKEN_TYPE is the "." or "->" token, which will already have been
5237    removed from the stream.  */
5238
5239 static tree
5240 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5241                                         enum cpp_ttype token_type,
5242                                         tree postfix_expression,
5243                                         bool for_offsetof, cp_id_kind *idk,
5244                                         location_t location)
5245 {
5246   tree name;
5247   bool dependent_p;
5248   bool pseudo_destructor_p;
5249   tree scope = NULL_TREE;
5250
5251   /* If this is a `->' operator, dereference the pointer.  */
5252   if (token_type == CPP_DEREF)
5253     postfix_expression = build_x_arrow (postfix_expression);
5254   /* Check to see whether or not the expression is type-dependent.  */
5255   dependent_p = type_dependent_expression_p (postfix_expression);
5256   /* The identifier following the `->' or `.' is not qualified.  */
5257   parser->scope = NULL_TREE;
5258   parser->qualifying_scope = NULL_TREE;
5259   parser->object_scope = NULL_TREE;
5260   *idk = CP_ID_KIND_NONE;
5261
5262   /* Enter the scope corresponding to the type of the object
5263      given by the POSTFIX_EXPRESSION.  */
5264   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5265     {
5266       scope = TREE_TYPE (postfix_expression);
5267       /* According to the standard, no expression should ever have
5268          reference type.  Unfortunately, we do not currently match
5269          the standard in this respect in that our internal representation
5270          of an expression may have reference type even when the standard
5271          says it does not.  Therefore, we have to manually obtain the
5272          underlying type here.  */
5273       scope = non_reference (scope);
5274       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5275       if (scope == unknown_type_node)
5276         {
5277           error_at (location, "%qE does not have class type",
5278                     postfix_expression);
5279           scope = NULL_TREE;
5280         }
5281       else
5282         scope = complete_type_or_else (scope, NULL_TREE);
5283       /* Let the name lookup machinery know that we are processing a
5284          class member access expression.  */
5285       parser->context->object_type = scope;
5286       /* If something went wrong, we want to be able to discern that case,
5287          as opposed to the case where there was no SCOPE due to the type
5288          of expression being dependent.  */
5289       if (!scope)
5290         scope = error_mark_node;
5291       /* If the SCOPE was erroneous, make the various semantic analysis
5292          functions exit quickly -- and without issuing additional error
5293          messages.  */
5294       if (scope == error_mark_node)
5295         postfix_expression = error_mark_node;
5296     }
5297
5298   /* Assume this expression is not a pseudo-destructor access.  */
5299   pseudo_destructor_p = false;
5300
5301   /* If the SCOPE is a scalar type, then, if this is a valid program,
5302      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5303      is type dependent, it can be pseudo-destructor-name or something else.
5304      Try to parse it as pseudo-destructor-name first.  */
5305   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5306     {
5307       tree s;
5308       tree type;
5309
5310       cp_parser_parse_tentatively (parser);
5311       /* Parse the pseudo-destructor-name.  */
5312       s = NULL_TREE;
5313       cp_parser_pseudo_destructor_name (parser, &s, &type);
5314       if (dependent_p
5315           && (cp_parser_error_occurred (parser)
5316               || TREE_CODE (type) != TYPE_DECL
5317               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5318         cp_parser_abort_tentative_parse (parser);
5319       else if (cp_parser_parse_definitely (parser))
5320         {
5321           pseudo_destructor_p = true;
5322           postfix_expression
5323             = finish_pseudo_destructor_expr (postfix_expression,
5324                                              s, TREE_TYPE (type));
5325         }
5326     }
5327
5328   if (!pseudo_destructor_p)
5329     {
5330       /* If the SCOPE is not a scalar type, we are looking at an
5331          ordinary class member access expression, rather than a
5332          pseudo-destructor-name.  */
5333       bool template_p;
5334       cp_token *token = cp_lexer_peek_token (parser->lexer);
5335       /* Parse the id-expression.  */
5336       name = (cp_parser_id_expression
5337               (parser,
5338                cp_parser_optional_template_keyword (parser),
5339                /*check_dependency_p=*/true,
5340                &template_p,
5341                /*declarator_p=*/false,
5342                /*optional_p=*/false));
5343       /* In general, build a SCOPE_REF if the member name is qualified.
5344          However, if the name was not dependent and has already been
5345          resolved; there is no need to build the SCOPE_REF.  For example;
5346
5347              struct X { void f(); };
5348              template <typename T> void f(T* t) { t->X::f(); }
5349
5350          Even though "t" is dependent, "X::f" is not and has been resolved
5351          to a BASELINK; there is no need to include scope information.  */
5352
5353       /* But we do need to remember that there was an explicit scope for
5354          virtual function calls.  */
5355       if (parser->scope)
5356         *idk = CP_ID_KIND_QUALIFIED;
5357
5358       /* If the name is a template-id that names a type, we will get a
5359          TYPE_DECL here.  That is invalid code.  */
5360       if (TREE_CODE (name) == TYPE_DECL)
5361         {
5362           error_at (token->location, "invalid use of %qD", name);
5363           postfix_expression = error_mark_node;
5364         }
5365       else
5366         {
5367           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5368             {
5369               name = build_qualified_name (/*type=*/NULL_TREE,
5370                                            parser->scope,
5371                                            name,
5372                                            template_p);
5373               parser->scope = NULL_TREE;
5374               parser->qualifying_scope = NULL_TREE;
5375               parser->object_scope = NULL_TREE;
5376             }
5377           if (scope && name && BASELINK_P (name))
5378             adjust_result_of_qualified_name_lookup
5379               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5380           postfix_expression
5381             = finish_class_member_access_expr (postfix_expression, name,
5382                                                template_p, 
5383                                                tf_warning_or_error);
5384         }
5385     }
5386
5387   /* We no longer need to look up names in the scope of the object on
5388      the left-hand side of the `.' or `->' operator.  */
5389   parser->context->object_type = NULL_TREE;
5390
5391   /* Outside of offsetof, these operators may not appear in
5392      constant-expressions.  */
5393   if (!for_offsetof
5394       && (cp_parser_non_integral_constant_expression
5395           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5396     postfix_expression = error_mark_node;
5397
5398   return postfix_expression;
5399 }
5400
5401 /* Parse a parenthesized expression-list.
5402
5403    expression-list:
5404      assignment-expression
5405      expression-list, assignment-expression
5406
5407    attribute-list:
5408      expression-list
5409      identifier
5410      identifier, expression-list
5411
5412    CAST_P is true if this expression is the target of a cast.
5413
5414    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5415    argument pack.
5416
5417    Returns a vector of trees.  Each element is a representation of an
5418    assignment-expression.  NULL is returned if the ( and or ) are
5419    missing.  An empty, but allocated, vector is returned on no
5420    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5421    if we are parsing an attribute list for an attribute that wants a
5422    plain identifier argument, normal_attr for an attribute that wants
5423    an expression, or non_attr if we aren't parsing an attribute list.  If
5424    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5425    not all of the expressions in the list were constant.  */
5426
5427 static VEC(tree,gc) *
5428 cp_parser_parenthesized_expression_list (cp_parser* parser,
5429                                          int is_attribute_list,
5430                                          bool cast_p,
5431                                          bool allow_expansion_p,
5432                                          bool *non_constant_p)
5433 {
5434   VEC(tree,gc) *expression_list;
5435   bool fold_expr_p = is_attribute_list != non_attr;
5436   tree identifier = NULL_TREE;
5437   bool saved_greater_than_is_operator_p;
5438
5439   /* Assume all the expressions will be constant.  */
5440   if (non_constant_p)
5441     *non_constant_p = false;
5442
5443   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5444     return NULL;
5445
5446   expression_list = make_tree_vector ();
5447
5448   /* Within a parenthesized expression, a `>' token is always
5449      the greater-than operator.  */
5450   saved_greater_than_is_operator_p
5451     = parser->greater_than_is_operator_p;
5452   parser->greater_than_is_operator_p = true;
5453
5454   /* Consume expressions until there are no more.  */
5455   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5456     while (true)
5457       {
5458         tree expr;
5459
5460         /* At the beginning of attribute lists, check to see if the
5461            next token is an identifier.  */
5462         if (is_attribute_list == id_attr
5463             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5464           {
5465             cp_token *token;
5466
5467             /* Consume the identifier.  */
5468             token = cp_lexer_consume_token (parser->lexer);
5469             /* Save the identifier.  */
5470             identifier = token->u.value;
5471           }
5472         else
5473           {
5474             bool expr_non_constant_p;
5475
5476             /* Parse the next assignment-expression.  */
5477             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5478               {
5479                 /* A braced-init-list.  */
5480                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5481                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5482                 if (non_constant_p && expr_non_constant_p)
5483                   *non_constant_p = true;
5484               }
5485             else if (non_constant_p)
5486               {
5487                 expr = (cp_parser_constant_expression
5488                         (parser, /*allow_non_constant_p=*/true,
5489                          &expr_non_constant_p));
5490                 if (expr_non_constant_p)
5491                   *non_constant_p = true;
5492               }
5493             else
5494               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5495
5496             if (fold_expr_p)
5497               expr = fold_non_dependent_expr (expr);
5498
5499             /* If we have an ellipsis, then this is an expression
5500                expansion.  */
5501             if (allow_expansion_p
5502                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5503               {
5504                 /* Consume the `...'.  */
5505                 cp_lexer_consume_token (parser->lexer);
5506
5507                 /* Build the argument pack.  */
5508                 expr = make_pack_expansion (expr);
5509               }
5510
5511              /* Add it to the list.  We add error_mark_node
5512                 expressions to the list, so that we can still tell if
5513                 the correct form for a parenthesized expression-list
5514                 is found. That gives better errors.  */
5515             VEC_safe_push (tree, gc, expression_list, expr);
5516
5517             if (expr == error_mark_node)
5518               goto skip_comma;
5519           }
5520
5521         /* After the first item, attribute lists look the same as
5522            expression lists.  */
5523         is_attribute_list = non_attr;
5524
5525       get_comma:;
5526         /* If the next token isn't a `,', then we are done.  */
5527         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5528           break;
5529
5530         /* Otherwise, consume the `,' and keep going.  */
5531         cp_lexer_consume_token (parser->lexer);
5532       }
5533
5534   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5535     {
5536       int ending;
5537
5538     skip_comma:;
5539       /* We try and resync to an unnested comma, as that will give the
5540          user better diagnostics.  */
5541       ending = cp_parser_skip_to_closing_parenthesis (parser,
5542                                                       /*recovering=*/true,
5543                                                       /*or_comma=*/true,
5544                                                       /*consume_paren=*/true);
5545       if (ending < 0)
5546         goto get_comma;
5547       if (!ending)
5548         {
5549           parser->greater_than_is_operator_p
5550             = saved_greater_than_is_operator_p;
5551           return NULL;
5552         }
5553     }
5554
5555   parser->greater_than_is_operator_p
5556     = saved_greater_than_is_operator_p;
5557
5558   if (identifier)
5559     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5560
5561   return expression_list;
5562 }
5563
5564 /* Parse a pseudo-destructor-name.
5565
5566    pseudo-destructor-name:
5567      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5568      :: [opt] nested-name-specifier template template-id :: ~ type-name
5569      :: [opt] nested-name-specifier [opt] ~ type-name
5570
5571    If either of the first two productions is used, sets *SCOPE to the
5572    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5573    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5574    or ERROR_MARK_NODE if the parse fails.  */
5575
5576 static void
5577 cp_parser_pseudo_destructor_name (cp_parser* parser,
5578                                   tree* scope,
5579                                   tree* type)
5580 {
5581   bool nested_name_specifier_p;
5582
5583   /* Assume that things will not work out.  */
5584   *type = error_mark_node;
5585
5586   /* Look for the optional `::' operator.  */
5587   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5588   /* Look for the optional nested-name-specifier.  */
5589   nested_name_specifier_p
5590     = (cp_parser_nested_name_specifier_opt (parser,
5591                                             /*typename_keyword_p=*/false,
5592                                             /*check_dependency_p=*/true,
5593                                             /*type_p=*/false,
5594                                             /*is_declaration=*/false)
5595        != NULL_TREE);
5596   /* Now, if we saw a nested-name-specifier, we might be doing the
5597      second production.  */
5598   if (nested_name_specifier_p
5599       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5600     {
5601       /* Consume the `template' keyword.  */
5602       cp_lexer_consume_token (parser->lexer);
5603       /* Parse the template-id.  */
5604       cp_parser_template_id (parser,
5605                              /*template_keyword_p=*/true,
5606                              /*check_dependency_p=*/false,
5607                              /*is_declaration=*/true);
5608       /* Look for the `::' token.  */
5609       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5610     }
5611   /* If the next token is not a `~', then there might be some
5612      additional qualification.  */
5613   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5614     {
5615       /* At this point, we're looking for "type-name :: ~".  The type-name
5616          must not be a class-name, since this is a pseudo-destructor.  So,
5617          it must be either an enum-name, or a typedef-name -- both of which
5618          are just identifiers.  So, we peek ahead to check that the "::"
5619          and "~" tokens are present; if they are not, then we can avoid
5620          calling type_name.  */
5621       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5622           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5623           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5624         {
5625           cp_parser_error (parser, "non-scalar type");
5626           return;
5627         }
5628
5629       /* Look for the type-name.  */
5630       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5631       if (*scope == error_mark_node)
5632         return;
5633
5634       /* Look for the `::' token.  */
5635       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5636     }
5637   else
5638     *scope = NULL_TREE;
5639
5640   /* Look for the `~'.  */
5641   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5642   /* Look for the type-name again.  We are not responsible for
5643      checking that it matches the first type-name.  */
5644   *type = cp_parser_nonclass_name (parser);
5645 }
5646
5647 /* Parse a unary-expression.
5648
5649    unary-expression:
5650      postfix-expression
5651      ++ cast-expression
5652      -- cast-expression
5653      unary-operator cast-expression
5654      sizeof unary-expression
5655      sizeof ( type-id )
5656      alignof ( type-id )  [C++0x]
5657      new-expression
5658      delete-expression
5659
5660    GNU Extensions:
5661
5662    unary-expression:
5663      __extension__ cast-expression
5664      __alignof__ unary-expression
5665      __alignof__ ( type-id )
5666      alignof unary-expression  [C++0x]
5667      __real__ cast-expression
5668      __imag__ cast-expression
5669      && identifier
5670
5671    ADDRESS_P is true iff the unary-expression is appearing as the
5672    operand of the `&' operator.   CAST_P is true if this expression is
5673    the target of a cast.
5674
5675    Returns a representation of the expression.  */
5676
5677 static tree
5678 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5679                             cp_id_kind * pidk)
5680 {
5681   cp_token *token;
5682   enum tree_code unary_operator;
5683
5684   /* Peek at the next token.  */
5685   token = cp_lexer_peek_token (parser->lexer);
5686   /* Some keywords give away the kind of expression.  */
5687   if (token->type == CPP_KEYWORD)
5688     {
5689       enum rid keyword = token->keyword;
5690
5691       switch (keyword)
5692         {
5693         case RID_ALIGNOF:
5694         case RID_SIZEOF:
5695           {
5696             tree operand;
5697             enum tree_code op;
5698
5699             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5700             /* Consume the token.  */
5701             cp_lexer_consume_token (parser->lexer);
5702             /* Parse the operand.  */
5703             operand = cp_parser_sizeof_operand (parser, keyword);
5704
5705             if (TYPE_P (operand))
5706               return cxx_sizeof_or_alignof_type (operand, op, true);
5707             else
5708               {
5709                 /* ISO C++ defines alignof only with types, not with
5710                    expressions. So pedwarn if alignof is used with a non-
5711                    type expression. However, __alignof__ is ok.  */
5712                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5713                   pedwarn (token->location, OPT_pedantic,
5714                            "ISO C++ does not allow %<alignof%> "
5715                            "with a non-type");
5716
5717                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5718               }
5719           }
5720
5721         case RID_NEW:
5722           return cp_parser_new_expression (parser);
5723
5724         case RID_DELETE:
5725           return cp_parser_delete_expression (parser);
5726
5727         case RID_EXTENSION:
5728           {
5729             /* The saved value of the PEDANTIC flag.  */
5730             int saved_pedantic;
5731             tree expr;
5732
5733             /* Save away the PEDANTIC flag.  */
5734             cp_parser_extension_opt (parser, &saved_pedantic);
5735             /* Parse the cast-expression.  */
5736             expr = cp_parser_simple_cast_expression (parser);
5737             /* Restore the PEDANTIC flag.  */
5738             pedantic = saved_pedantic;
5739
5740             return expr;
5741           }
5742
5743         case RID_REALPART:
5744         case RID_IMAGPART:
5745           {
5746             tree expression;
5747
5748             /* Consume the `__real__' or `__imag__' token.  */
5749             cp_lexer_consume_token (parser->lexer);
5750             /* Parse the cast-expression.  */
5751             expression = cp_parser_simple_cast_expression (parser);
5752             /* Create the complete representation.  */
5753             return build_x_unary_op ((keyword == RID_REALPART
5754                                       ? REALPART_EXPR : IMAGPART_EXPR),
5755                                      expression,
5756                                      tf_warning_or_error);
5757           }
5758           break;
5759
5760         case RID_NOEXCEPT:
5761           {
5762             tree expr;
5763             const char *saved_message;
5764             bool saved_integral_constant_expression_p;
5765             bool saved_non_integral_constant_expression_p;
5766             bool saved_greater_than_is_operator_p;
5767
5768             cp_lexer_consume_token (parser->lexer);
5769             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5770
5771             saved_message = parser->type_definition_forbidden_message;
5772             parser->type_definition_forbidden_message
5773               = G_("types may not be defined in %<noexcept%> expressions");
5774
5775             saved_integral_constant_expression_p
5776               = parser->integral_constant_expression_p;
5777             saved_non_integral_constant_expression_p
5778               = parser->non_integral_constant_expression_p;
5779             parser->integral_constant_expression_p = false;
5780
5781             saved_greater_than_is_operator_p
5782               = parser->greater_than_is_operator_p;
5783             parser->greater_than_is_operator_p = true;
5784
5785             ++cp_unevaluated_operand;
5786             ++c_inhibit_evaluation_warnings;
5787             expr = cp_parser_expression (parser, false, NULL);
5788             --c_inhibit_evaluation_warnings;
5789             --cp_unevaluated_operand;
5790
5791             parser->greater_than_is_operator_p
5792               = saved_greater_than_is_operator_p;
5793
5794             parser->integral_constant_expression_p
5795               = saved_integral_constant_expression_p;
5796             parser->non_integral_constant_expression_p
5797               = saved_non_integral_constant_expression_p;
5798
5799             parser->type_definition_forbidden_message = saved_message;
5800
5801             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5802             return finish_noexcept_expr (expr, tf_warning_or_error);
5803           }
5804
5805         default:
5806           break;
5807         }
5808     }
5809
5810   /* Look for the `:: new' and `:: delete', which also signal the
5811      beginning of a new-expression, or delete-expression,
5812      respectively.  If the next token is `::', then it might be one of
5813      these.  */
5814   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5815     {
5816       enum rid keyword;
5817
5818       /* See if the token after the `::' is one of the keywords in
5819          which we're interested.  */
5820       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5821       /* If it's `new', we have a new-expression.  */
5822       if (keyword == RID_NEW)
5823         return cp_parser_new_expression (parser);
5824       /* Similarly, for `delete'.  */
5825       else if (keyword == RID_DELETE)
5826         return cp_parser_delete_expression (parser);
5827     }
5828
5829   /* Look for a unary operator.  */
5830   unary_operator = cp_parser_unary_operator (token);
5831   /* The `++' and `--' operators can be handled similarly, even though
5832      they are not technically unary-operators in the grammar.  */
5833   if (unary_operator == ERROR_MARK)
5834     {
5835       if (token->type == CPP_PLUS_PLUS)
5836         unary_operator = PREINCREMENT_EXPR;
5837       else if (token->type == CPP_MINUS_MINUS)
5838         unary_operator = PREDECREMENT_EXPR;
5839       /* Handle the GNU address-of-label extension.  */
5840       else if (cp_parser_allow_gnu_extensions_p (parser)
5841                && token->type == CPP_AND_AND)
5842         {
5843           tree identifier;
5844           tree expression;
5845           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5846
5847           /* Consume the '&&' token.  */
5848           cp_lexer_consume_token (parser->lexer);
5849           /* Look for the identifier.  */
5850           identifier = cp_parser_identifier (parser);
5851           /* Create an expression representing the address.  */
5852           expression = finish_label_address_expr (identifier, loc);
5853           if (cp_parser_non_integral_constant_expression (parser,
5854                                                           NIC_ADDR_LABEL))
5855             expression = error_mark_node;
5856           return expression;
5857         }
5858     }
5859   if (unary_operator != ERROR_MARK)
5860     {
5861       tree cast_expression;
5862       tree expression = error_mark_node;
5863       non_integral_constant non_constant_p = NIC_NONE;
5864
5865       /* Consume the operator token.  */
5866       token = cp_lexer_consume_token (parser->lexer);
5867       /* Parse the cast-expression.  */
5868       cast_expression
5869         = cp_parser_cast_expression (parser,
5870                                      unary_operator == ADDR_EXPR,
5871                                      /*cast_p=*/false, pidk);
5872       /* Now, build an appropriate representation.  */
5873       switch (unary_operator)
5874         {
5875         case INDIRECT_REF:
5876           non_constant_p = NIC_STAR;
5877           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5878                                              tf_warning_or_error);
5879           break;
5880
5881         case ADDR_EXPR:
5882            non_constant_p = NIC_ADDR;
5883           /* Fall through.  */
5884         case BIT_NOT_EXPR:
5885           expression = build_x_unary_op (unary_operator, cast_expression,
5886                                          tf_warning_or_error);
5887           break;
5888
5889         case PREINCREMENT_EXPR:
5890         case PREDECREMENT_EXPR:
5891           non_constant_p = unary_operator == PREINCREMENT_EXPR
5892                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5893           /* Fall through.  */
5894         case UNARY_PLUS_EXPR:
5895         case NEGATE_EXPR:
5896         case TRUTH_NOT_EXPR:
5897           expression = finish_unary_op_expr (unary_operator, cast_expression);
5898           break;
5899
5900         default:
5901           gcc_unreachable ();
5902         }
5903
5904       if (non_constant_p != NIC_NONE
5905           && cp_parser_non_integral_constant_expression (parser,
5906                                                          non_constant_p))
5907         expression = error_mark_node;
5908
5909       return expression;
5910     }
5911
5912   return cp_parser_postfix_expression (parser, address_p, cast_p,
5913                                        /*member_access_only_p=*/false,
5914                                        pidk);
5915 }
5916
5917 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5918    unary-operator, the corresponding tree code is returned.  */
5919
5920 static enum tree_code
5921 cp_parser_unary_operator (cp_token* token)
5922 {
5923   switch (token->type)
5924     {
5925     case CPP_MULT:
5926       return INDIRECT_REF;
5927
5928     case CPP_AND:
5929       return ADDR_EXPR;
5930
5931     case CPP_PLUS:
5932       return UNARY_PLUS_EXPR;
5933
5934     case CPP_MINUS:
5935       return NEGATE_EXPR;
5936
5937     case CPP_NOT:
5938       return TRUTH_NOT_EXPR;
5939
5940     case CPP_COMPL:
5941       return BIT_NOT_EXPR;
5942
5943     default:
5944       return ERROR_MARK;
5945     }
5946 }
5947
5948 /* Parse a new-expression.
5949
5950    new-expression:
5951      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5952      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5953
5954    Returns a representation of the expression.  */
5955
5956 static tree
5957 cp_parser_new_expression (cp_parser* parser)
5958 {
5959   bool global_scope_p;
5960   VEC(tree,gc) *placement;
5961   tree type;
5962   VEC(tree,gc) *initializer;
5963   tree nelts;
5964   tree ret;
5965
5966   /* Look for the optional `::' operator.  */
5967   global_scope_p
5968     = (cp_parser_global_scope_opt (parser,
5969                                    /*current_scope_valid_p=*/false)
5970        != NULL_TREE);
5971   /* Look for the `new' operator.  */
5972   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5973   /* There's no easy way to tell a new-placement from the
5974      `( type-id )' construct.  */
5975   cp_parser_parse_tentatively (parser);
5976   /* Look for a new-placement.  */
5977   placement = cp_parser_new_placement (parser);
5978   /* If that didn't work out, there's no new-placement.  */
5979   if (!cp_parser_parse_definitely (parser))
5980     {
5981       if (placement != NULL)
5982         release_tree_vector (placement);
5983       placement = NULL;
5984     }
5985
5986   /* If the next token is a `(', then we have a parenthesized
5987      type-id.  */
5988   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5989     {
5990       cp_token *token;
5991       /* Consume the `('.  */
5992       cp_lexer_consume_token (parser->lexer);
5993       /* Parse the type-id.  */
5994       type = cp_parser_type_id (parser);
5995       /* Look for the closing `)'.  */
5996       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5997       token = cp_lexer_peek_token (parser->lexer);
5998       /* There should not be a direct-new-declarator in this production,
5999          but GCC used to allowed this, so we check and emit a sensible error
6000          message for this case.  */
6001       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6002         {
6003           error_at (token->location,
6004                     "array bound forbidden after parenthesized type-id");
6005           inform (token->location, 
6006                   "try removing the parentheses around the type-id");
6007           cp_parser_direct_new_declarator (parser);
6008         }
6009       nelts = NULL_TREE;
6010     }
6011   /* Otherwise, there must be a new-type-id.  */
6012   else
6013     type = cp_parser_new_type_id (parser, &nelts);
6014
6015   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6016   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6017       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6018     initializer = cp_parser_new_initializer (parser);
6019   else
6020     initializer = NULL;
6021
6022   /* A new-expression may not appear in an integral constant
6023      expression.  */
6024   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6025     ret = error_mark_node;
6026   else
6027     {
6028       /* Create a representation of the new-expression.  */
6029       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6030                        tf_warning_or_error);
6031     }
6032
6033   if (placement != NULL)
6034     release_tree_vector (placement);
6035   if (initializer != NULL)
6036     release_tree_vector (initializer);
6037
6038   return ret;
6039 }
6040
6041 /* Parse a new-placement.
6042
6043    new-placement:
6044      ( expression-list )
6045
6046    Returns the same representation as for an expression-list.  */
6047
6048 static VEC(tree,gc) *
6049 cp_parser_new_placement (cp_parser* parser)
6050 {
6051   VEC(tree,gc) *expression_list;
6052
6053   /* Parse the expression-list.  */
6054   expression_list = (cp_parser_parenthesized_expression_list
6055                      (parser, non_attr, /*cast_p=*/false,
6056                       /*allow_expansion_p=*/true,
6057                       /*non_constant_p=*/NULL));
6058
6059   return expression_list;
6060 }
6061
6062 /* Parse a new-type-id.
6063
6064    new-type-id:
6065      type-specifier-seq new-declarator [opt]
6066
6067    Returns the TYPE allocated.  If the new-type-id indicates an array
6068    type, *NELTS is set to the number of elements in the last array
6069    bound; the TYPE will not include the last array bound.  */
6070
6071 static tree
6072 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6073 {
6074   cp_decl_specifier_seq type_specifier_seq;
6075   cp_declarator *new_declarator;
6076   cp_declarator *declarator;
6077   cp_declarator *outer_declarator;
6078   const char *saved_message;
6079   tree type;
6080
6081   /* The type-specifier sequence must not contain type definitions.
6082      (It cannot contain declarations of new types either, but if they
6083      are not definitions we will catch that because they are not
6084      complete.)  */
6085   saved_message = parser->type_definition_forbidden_message;
6086   parser->type_definition_forbidden_message
6087     = G_("types may not be defined in a new-type-id");
6088   /* Parse the type-specifier-seq.  */
6089   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6090                                 /*is_trailing_return=*/false,
6091                                 &type_specifier_seq);
6092   /* Restore the old message.  */
6093   parser->type_definition_forbidden_message = saved_message;
6094   /* Parse the new-declarator.  */
6095   new_declarator = cp_parser_new_declarator_opt (parser);
6096
6097   /* Determine the number of elements in the last array dimension, if
6098      any.  */
6099   *nelts = NULL_TREE;
6100   /* Skip down to the last array dimension.  */
6101   declarator = new_declarator;
6102   outer_declarator = NULL;
6103   while (declarator && (declarator->kind == cdk_pointer
6104                         || declarator->kind == cdk_ptrmem))
6105     {
6106       outer_declarator = declarator;
6107       declarator = declarator->declarator;
6108     }
6109   while (declarator
6110          && declarator->kind == cdk_array
6111          && declarator->declarator
6112          && declarator->declarator->kind == cdk_array)
6113     {
6114       outer_declarator = declarator;
6115       declarator = declarator->declarator;
6116     }
6117
6118   if (declarator && declarator->kind == cdk_array)
6119     {
6120       *nelts = declarator->u.array.bounds;
6121       if (*nelts == error_mark_node)
6122         *nelts = integer_one_node;
6123
6124       if (outer_declarator)
6125         outer_declarator->declarator = declarator->declarator;
6126       else
6127         new_declarator = NULL;
6128     }
6129
6130   type = groktypename (&type_specifier_seq, new_declarator, false);
6131   return type;
6132 }
6133
6134 /* Parse an (optional) new-declarator.
6135
6136    new-declarator:
6137      ptr-operator new-declarator [opt]
6138      direct-new-declarator
6139
6140    Returns the declarator.  */
6141
6142 static cp_declarator *
6143 cp_parser_new_declarator_opt (cp_parser* parser)
6144 {
6145   enum tree_code code;
6146   tree type;
6147   cp_cv_quals cv_quals;
6148
6149   /* We don't know if there's a ptr-operator next, or not.  */
6150   cp_parser_parse_tentatively (parser);
6151   /* Look for a ptr-operator.  */
6152   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6153   /* If that worked, look for more new-declarators.  */
6154   if (cp_parser_parse_definitely (parser))
6155     {
6156       cp_declarator *declarator;
6157
6158       /* Parse another optional declarator.  */
6159       declarator = cp_parser_new_declarator_opt (parser);
6160
6161       return cp_parser_make_indirect_declarator
6162         (code, type, cv_quals, declarator);
6163     }
6164
6165   /* If the next token is a `[', there is a direct-new-declarator.  */
6166   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6167     return cp_parser_direct_new_declarator (parser);
6168
6169   return NULL;
6170 }
6171
6172 /* Parse a direct-new-declarator.
6173
6174    direct-new-declarator:
6175      [ expression ]
6176      direct-new-declarator [constant-expression]
6177
6178    */
6179
6180 static cp_declarator *
6181 cp_parser_direct_new_declarator (cp_parser* parser)
6182 {
6183   cp_declarator *declarator = NULL;
6184
6185   while (true)
6186     {
6187       tree expression;
6188
6189       /* Look for the opening `['.  */
6190       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6191       /* The first expression is not required to be constant.  */
6192       if (!declarator)
6193         {
6194           cp_token *token = cp_lexer_peek_token (parser->lexer);
6195           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6196           /* The standard requires that the expression have integral
6197              type.  DR 74 adds enumeration types.  We believe that the
6198              real intent is that these expressions be handled like the
6199              expression in a `switch' condition, which also allows
6200              classes with a single conversion to integral or
6201              enumeration type.  */
6202           if (!processing_template_decl)
6203             {
6204               expression
6205                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6206                                               expression,
6207                                               /*complain=*/true);
6208               if (!expression)
6209                 {
6210                   error_at (token->location,
6211                             "expression in new-declarator must have integral "
6212                             "or enumeration type");
6213                   expression = error_mark_node;
6214                 }
6215             }
6216         }
6217       /* But all the other expressions must be.  */
6218       else
6219         expression
6220           = cp_parser_constant_expression (parser,
6221                                            /*allow_non_constant=*/false,
6222                                            NULL);
6223       /* Look for the closing `]'.  */
6224       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6225
6226       /* Add this bound to the declarator.  */
6227       declarator = make_array_declarator (declarator, expression);
6228
6229       /* If the next token is not a `[', then there are no more
6230          bounds.  */
6231       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6232         break;
6233     }
6234
6235   return declarator;
6236 }
6237
6238 /* Parse a new-initializer.
6239
6240    new-initializer:
6241      ( expression-list [opt] )
6242      braced-init-list
6243
6244    Returns a representation of the expression-list.  */
6245
6246 static VEC(tree,gc) *
6247 cp_parser_new_initializer (cp_parser* parser)
6248 {
6249   VEC(tree,gc) *expression_list;
6250
6251   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6252     {
6253       tree t;
6254       bool expr_non_constant_p;
6255       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6256       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6257       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6258       expression_list = make_tree_vector_single (t);
6259     }
6260   else
6261     expression_list = (cp_parser_parenthesized_expression_list
6262                        (parser, non_attr, /*cast_p=*/false,
6263                         /*allow_expansion_p=*/true,
6264                         /*non_constant_p=*/NULL));
6265
6266   return expression_list;
6267 }
6268
6269 /* Parse a delete-expression.
6270
6271    delete-expression:
6272      :: [opt] delete cast-expression
6273      :: [opt] delete [ ] cast-expression
6274
6275    Returns a representation of the expression.  */
6276
6277 static tree
6278 cp_parser_delete_expression (cp_parser* parser)
6279 {
6280   bool global_scope_p;
6281   bool array_p;
6282   tree expression;
6283
6284   /* Look for the optional `::' operator.  */
6285   global_scope_p
6286     = (cp_parser_global_scope_opt (parser,
6287                                    /*current_scope_valid_p=*/false)
6288        != NULL_TREE);
6289   /* Look for the `delete' keyword.  */
6290   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6291   /* See if the array syntax is in use.  */
6292   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6293     {
6294       /* Consume the `[' token.  */
6295       cp_lexer_consume_token (parser->lexer);
6296       /* Look for the `]' token.  */
6297       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6298       /* Remember that this is the `[]' construct.  */
6299       array_p = true;
6300     }
6301   else
6302     array_p = false;
6303
6304   /* Parse the cast-expression.  */
6305   expression = cp_parser_simple_cast_expression (parser);
6306
6307   /* A delete-expression may not appear in an integral constant
6308      expression.  */
6309   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6310     return error_mark_node;
6311
6312   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6313                         tf_warning_or_error);
6314 }
6315
6316 /* Returns true if TOKEN may start a cast-expression and false
6317    otherwise.  */
6318
6319 static bool
6320 cp_parser_token_starts_cast_expression (cp_token *token)
6321 {
6322   switch (token->type)
6323     {
6324     case CPP_COMMA:
6325     case CPP_SEMICOLON:
6326     case CPP_QUERY:
6327     case CPP_COLON:
6328     case CPP_CLOSE_SQUARE:
6329     case CPP_CLOSE_PAREN:
6330     case CPP_CLOSE_BRACE:
6331     case CPP_DOT:
6332     case CPP_DOT_STAR:
6333     case CPP_DEREF:
6334     case CPP_DEREF_STAR:
6335     case CPP_DIV:
6336     case CPP_MOD:
6337     case CPP_LSHIFT:
6338     case CPP_RSHIFT:
6339     case CPP_LESS:
6340     case CPP_GREATER:
6341     case CPP_LESS_EQ:
6342     case CPP_GREATER_EQ:
6343     case CPP_EQ_EQ:
6344     case CPP_NOT_EQ:
6345     case CPP_EQ:
6346     case CPP_MULT_EQ:
6347     case CPP_DIV_EQ:
6348     case CPP_MOD_EQ:
6349     case CPP_PLUS_EQ:
6350     case CPP_MINUS_EQ:
6351     case CPP_RSHIFT_EQ:
6352     case CPP_LSHIFT_EQ:
6353     case CPP_AND_EQ:
6354     case CPP_XOR_EQ:
6355     case CPP_OR_EQ:
6356     case CPP_XOR:
6357     case CPP_OR:
6358     case CPP_OR_OR:
6359     case CPP_EOF:
6360       return false;
6361
6362       /* '[' may start a primary-expression in obj-c++.  */
6363     case CPP_OPEN_SQUARE:
6364       return c_dialect_objc ();
6365
6366     default:
6367       return true;
6368     }
6369 }
6370
6371 /* Parse a cast-expression.
6372
6373    cast-expression:
6374      unary-expression
6375      ( type-id ) cast-expression
6376
6377    ADDRESS_P is true iff the unary-expression is appearing as the
6378    operand of the `&' operator.   CAST_P is true if this expression is
6379    the target of a cast.
6380
6381    Returns a representation of the expression.  */
6382
6383 static tree
6384 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6385                            cp_id_kind * pidk)
6386 {
6387   /* If it's a `(', then we might be looking at a cast.  */
6388   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6389     {
6390       tree type = NULL_TREE;
6391       tree expr = NULL_TREE;
6392       bool compound_literal_p;
6393       const char *saved_message;
6394
6395       /* There's no way to know yet whether or not this is a cast.
6396          For example, `(int (3))' is a unary-expression, while `(int)
6397          3' is a cast.  So, we resort to parsing tentatively.  */
6398       cp_parser_parse_tentatively (parser);
6399       /* Types may not be defined in a cast.  */
6400       saved_message = parser->type_definition_forbidden_message;
6401       parser->type_definition_forbidden_message
6402         = G_("types may not be defined in casts");
6403       /* Consume the `('.  */
6404       cp_lexer_consume_token (parser->lexer);
6405       /* A very tricky bit is that `(struct S) { 3 }' is a
6406          compound-literal (which we permit in C++ as an extension).
6407          But, that construct is not a cast-expression -- it is a
6408          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6409          is legal; if the compound-literal were a cast-expression,
6410          you'd need an extra set of parentheses.)  But, if we parse
6411          the type-id, and it happens to be a class-specifier, then we
6412          will commit to the parse at that point, because we cannot
6413          undo the action that is done when creating a new class.  So,
6414          then we cannot back up and do a postfix-expression.
6415
6416          Therefore, we scan ahead to the closing `)', and check to see
6417          if the token after the `)' is a `{'.  If so, we are not
6418          looking at a cast-expression.
6419
6420          Save tokens so that we can put them back.  */
6421       cp_lexer_save_tokens (parser->lexer);
6422       /* Skip tokens until the next token is a closing parenthesis.
6423          If we find the closing `)', and the next token is a `{', then
6424          we are looking at a compound-literal.  */
6425       compound_literal_p
6426         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6427                                                   /*consume_paren=*/true)
6428            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6429       /* Roll back the tokens we skipped.  */
6430       cp_lexer_rollback_tokens (parser->lexer);
6431       /* If we were looking at a compound-literal, simulate an error
6432          so that the call to cp_parser_parse_definitely below will
6433          fail.  */
6434       if (compound_literal_p)
6435         cp_parser_simulate_error (parser);
6436       else
6437         {
6438           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6439           parser->in_type_id_in_expr_p = true;
6440           /* Look for the type-id.  */
6441           type = cp_parser_type_id (parser);
6442           /* Look for the closing `)'.  */
6443           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6444           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6445         }
6446
6447       /* Restore the saved message.  */
6448       parser->type_definition_forbidden_message = saved_message;
6449
6450       /* At this point this can only be either a cast or a
6451          parenthesized ctor such as `(T ())' that looks like a cast to
6452          function returning T.  */
6453       if (!cp_parser_error_occurred (parser)
6454           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6455                                                      (parser->lexer)))
6456         {
6457           cp_parser_parse_definitely (parser);
6458           expr = cp_parser_cast_expression (parser,
6459                                             /*address_p=*/false,
6460                                             /*cast_p=*/true, pidk);
6461
6462           /* Warn about old-style casts, if so requested.  */
6463           if (warn_old_style_cast
6464               && !in_system_header
6465               && !VOID_TYPE_P (type)
6466               && current_lang_name != lang_name_c)
6467             warning (OPT_Wold_style_cast, "use of old-style cast");
6468
6469           /* Only type conversions to integral or enumeration types
6470              can be used in constant-expressions.  */
6471           if (!cast_valid_in_integral_constant_expression_p (type)
6472               && cp_parser_non_integral_constant_expression (parser,
6473                                                              NIC_CAST))
6474             return error_mark_node;
6475
6476           /* Perform the cast.  */
6477           expr = build_c_cast (input_location, type, expr);
6478           return expr;
6479         }
6480       else 
6481         cp_parser_abort_tentative_parse (parser);
6482     }
6483
6484   /* If we get here, then it's not a cast, so it must be a
6485      unary-expression.  */
6486   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6487 }
6488
6489 /* Parse a binary expression of the general form:
6490
6491    pm-expression:
6492      cast-expression
6493      pm-expression .* cast-expression
6494      pm-expression ->* cast-expression
6495
6496    multiplicative-expression:
6497      pm-expression
6498      multiplicative-expression * pm-expression
6499      multiplicative-expression / pm-expression
6500      multiplicative-expression % pm-expression
6501
6502    additive-expression:
6503      multiplicative-expression
6504      additive-expression + multiplicative-expression
6505      additive-expression - multiplicative-expression
6506
6507    shift-expression:
6508      additive-expression
6509      shift-expression << additive-expression
6510      shift-expression >> additive-expression
6511
6512    relational-expression:
6513      shift-expression
6514      relational-expression < shift-expression
6515      relational-expression > shift-expression
6516      relational-expression <= shift-expression
6517      relational-expression >= shift-expression
6518
6519   GNU Extension:
6520
6521    relational-expression:
6522      relational-expression <? shift-expression
6523      relational-expression >? shift-expression
6524
6525    equality-expression:
6526      relational-expression
6527      equality-expression == relational-expression
6528      equality-expression != relational-expression
6529
6530    and-expression:
6531      equality-expression
6532      and-expression & equality-expression
6533
6534    exclusive-or-expression:
6535      and-expression
6536      exclusive-or-expression ^ and-expression
6537
6538    inclusive-or-expression:
6539      exclusive-or-expression
6540      inclusive-or-expression | exclusive-or-expression
6541
6542    logical-and-expression:
6543      inclusive-or-expression
6544      logical-and-expression && inclusive-or-expression
6545
6546    logical-or-expression:
6547      logical-and-expression
6548      logical-or-expression || logical-and-expression
6549
6550    All these are implemented with a single function like:
6551
6552    binary-expression:
6553      simple-cast-expression
6554      binary-expression <token> binary-expression
6555
6556    CAST_P is true if this expression is the target of a cast.
6557
6558    The binops_by_token map is used to get the tree codes for each <token> type.
6559    binary-expressions are associated according to a precedence table.  */
6560
6561 #define TOKEN_PRECEDENCE(token)                              \
6562 (((token->type == CPP_GREATER                                \
6563    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6564   && !parser->greater_than_is_operator_p)                    \
6565  ? PREC_NOT_OPERATOR                                         \
6566  : binops_by_token[token->type].prec)
6567
6568 static tree
6569 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6570                              bool no_toplevel_fold_p,
6571                              enum cp_parser_prec prec,
6572                              cp_id_kind * pidk)
6573 {
6574   cp_parser_expression_stack stack;
6575   cp_parser_expression_stack_entry *sp = &stack[0];
6576   tree lhs, rhs;
6577   cp_token *token;
6578   enum tree_code tree_type, lhs_type, rhs_type;
6579   enum cp_parser_prec new_prec, lookahead_prec;
6580   bool overloaded_p;
6581
6582   /* Parse the first expression.  */
6583   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6584   lhs_type = ERROR_MARK;
6585
6586   for (;;)
6587     {
6588       /* Get an operator token.  */
6589       token = cp_lexer_peek_token (parser->lexer);
6590
6591       if (warn_cxx0x_compat
6592           && token->type == CPP_RSHIFT
6593           && !parser->greater_than_is_operator_p)
6594         {
6595           if (warning_at (token->location, OPT_Wc__0x_compat, 
6596                           "%<>>%> operator will be treated as"
6597                           " two right angle brackets in C++0x"))
6598             inform (token->location,
6599                     "suggest parentheses around %<>>%> expression");
6600         }
6601
6602       new_prec = TOKEN_PRECEDENCE (token);
6603
6604       /* Popping an entry off the stack means we completed a subexpression:
6605          - either we found a token which is not an operator (`>' where it is not
6606            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6607            will happen repeatedly;
6608          - or, we found an operator which has lower priority.  This is the case
6609            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6610            parsing `3 * 4'.  */
6611       if (new_prec <= prec)
6612         {
6613           if (sp == stack)
6614             break;
6615           else
6616             goto pop;
6617         }
6618
6619      get_rhs:
6620       tree_type = binops_by_token[token->type].tree_type;
6621
6622       /* We used the operator token.  */
6623       cp_lexer_consume_token (parser->lexer);
6624
6625       /* For "false && x" or "true || x", x will never be executed;
6626          disable warnings while evaluating it.  */
6627       if (tree_type == TRUTH_ANDIF_EXPR)
6628         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6629       else if (tree_type == TRUTH_ORIF_EXPR)
6630         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6631
6632       /* Extract another operand.  It may be the RHS of this expression
6633          or the LHS of a new, higher priority expression.  */
6634       rhs = cp_parser_simple_cast_expression (parser);
6635       rhs_type = ERROR_MARK;
6636
6637       /* Get another operator token.  Look up its precedence to avoid
6638          building a useless (immediately popped) stack entry for common
6639          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6640       token = cp_lexer_peek_token (parser->lexer);
6641       lookahead_prec = TOKEN_PRECEDENCE (token);
6642       if (lookahead_prec > new_prec)
6643         {
6644           /* ... and prepare to parse the RHS of the new, higher priority
6645              expression.  Since precedence levels on the stack are
6646              monotonically increasing, we do not have to care about
6647              stack overflows.  */
6648           sp->prec = prec;
6649           sp->tree_type = tree_type;
6650           sp->lhs = lhs;
6651           sp->lhs_type = lhs_type;
6652           sp++;
6653           lhs = rhs;
6654           lhs_type = rhs_type;
6655           prec = new_prec;
6656           new_prec = lookahead_prec;
6657           goto get_rhs;
6658
6659          pop:
6660           lookahead_prec = new_prec;
6661           /* If the stack is not empty, we have parsed into LHS the right side
6662              (`4' in the example above) of an expression we had suspended.
6663              We can use the information on the stack to recover the LHS (`3')
6664              from the stack together with the tree code (`MULT_EXPR'), and
6665              the precedence of the higher level subexpression
6666              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6667              which will be used to actually build the additive expression.  */
6668           --sp;
6669           prec = sp->prec;
6670           tree_type = sp->tree_type;
6671           rhs = lhs;
6672           rhs_type = lhs_type;
6673           lhs = sp->lhs;
6674           lhs_type = sp->lhs_type;
6675         }
6676
6677       /* Undo the disabling of warnings done above.  */
6678       if (tree_type == TRUTH_ANDIF_EXPR)
6679         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6680       else if (tree_type == TRUTH_ORIF_EXPR)
6681         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6682
6683       overloaded_p = false;
6684       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6685          ERROR_MARK for everything that is not a binary expression.
6686          This makes warn_about_parentheses miss some warnings that
6687          involve unary operators.  For unary expressions we should
6688          pass the correct tree_code unless the unary expression was
6689          surrounded by parentheses.
6690       */
6691       if (no_toplevel_fold_p
6692           && lookahead_prec <= prec
6693           && sp == stack
6694           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6695         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6696       else
6697         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6698                                  &overloaded_p, tf_warning_or_error);
6699       lhs_type = tree_type;
6700
6701       /* If the binary operator required the use of an overloaded operator,
6702          then this expression cannot be an integral constant-expression.
6703          An overloaded operator can be used even if both operands are
6704          otherwise permissible in an integral constant-expression if at
6705          least one of the operands is of enumeration type.  */
6706
6707       if (overloaded_p
6708           && cp_parser_non_integral_constant_expression (parser,
6709                                                          NIC_OVERLOADED))
6710         return error_mark_node;
6711     }
6712
6713   return lhs;
6714 }
6715
6716
6717 /* Parse the `? expression : assignment-expression' part of a
6718    conditional-expression.  The LOGICAL_OR_EXPR is the
6719    logical-or-expression that started the conditional-expression.
6720    Returns a representation of the entire conditional-expression.
6721
6722    This routine is used by cp_parser_assignment_expression.
6723
6724      ? expression : assignment-expression
6725
6726    GNU Extensions:
6727
6728      ? : assignment-expression */
6729
6730 static tree
6731 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6732 {
6733   tree expr;
6734   tree assignment_expr;
6735   struct cp_token *token;
6736
6737   /* Consume the `?' token.  */
6738   cp_lexer_consume_token (parser->lexer);
6739   token = cp_lexer_peek_token (parser->lexer);
6740   if (cp_parser_allow_gnu_extensions_p (parser)
6741       && token->type == CPP_COLON)
6742     {
6743       pedwarn (token->location, OPT_pedantic, 
6744                "ISO C++ does not allow ?: with omitted middle operand");
6745       /* Implicit true clause.  */
6746       expr = NULL_TREE;
6747       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6748       warn_for_omitted_condop (token->location, logical_or_expr);
6749     }
6750   else
6751     {
6752       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6753       parser->colon_corrects_to_scope_p = false;
6754       /* Parse the expression.  */
6755       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6756       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6757       c_inhibit_evaluation_warnings +=
6758         ((logical_or_expr == truthvalue_true_node)
6759          - (logical_or_expr == truthvalue_false_node));
6760       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6761     }
6762
6763   /* The next token should be a `:'.  */
6764   cp_parser_require (parser, CPP_COLON, RT_COLON);
6765   /* Parse the assignment-expression.  */
6766   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6767   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6768
6769   /* Build the conditional-expression.  */
6770   return build_x_conditional_expr (logical_or_expr,
6771                                    expr,
6772                                    assignment_expr,
6773                                    tf_warning_or_error);
6774 }
6775
6776 /* Parse an assignment-expression.
6777
6778    assignment-expression:
6779      conditional-expression
6780      logical-or-expression assignment-operator assignment_expression
6781      throw-expression
6782
6783    CAST_P is true if this expression is the target of a cast.
6784
6785    Returns a representation for the expression.  */
6786
6787 static tree
6788 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6789                                  cp_id_kind * pidk)
6790 {
6791   tree expr;
6792
6793   /* If the next token is the `throw' keyword, then we're looking at
6794      a throw-expression.  */
6795   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6796     expr = cp_parser_throw_expression (parser);
6797   /* Otherwise, it must be that we are looking at a
6798      logical-or-expression.  */
6799   else
6800     {
6801       /* Parse the binary expressions (logical-or-expression).  */
6802       expr = cp_parser_binary_expression (parser, cast_p, false,
6803                                           PREC_NOT_OPERATOR, pidk);
6804       /* If the next token is a `?' then we're actually looking at a
6805          conditional-expression.  */
6806       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6807         return cp_parser_question_colon_clause (parser, expr);
6808       else
6809         {
6810           enum tree_code assignment_operator;
6811
6812           /* If it's an assignment-operator, we're using the second
6813              production.  */
6814           assignment_operator
6815             = cp_parser_assignment_operator_opt (parser);
6816           if (assignment_operator != ERROR_MARK)
6817             {
6818               bool non_constant_p;
6819
6820               /* Parse the right-hand side of the assignment.  */
6821               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6822
6823               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6824                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6825
6826               /* An assignment may not appear in a
6827                  constant-expression.  */
6828               if (cp_parser_non_integral_constant_expression (parser,
6829                                                               NIC_ASSIGNMENT))
6830                 return error_mark_node;
6831               /* Build the assignment expression.  */
6832               expr = build_x_modify_expr (expr,
6833                                           assignment_operator,
6834                                           rhs,
6835                                           tf_warning_or_error);
6836             }
6837         }
6838     }
6839
6840   return expr;
6841 }
6842
6843 /* Parse an (optional) assignment-operator.
6844
6845    assignment-operator: one of
6846      = *= /= %= += -= >>= <<= &= ^= |=
6847
6848    GNU Extension:
6849
6850    assignment-operator: one of
6851      <?= >?=
6852
6853    If the next token is an assignment operator, the corresponding tree
6854    code is returned, and the token is consumed.  For example, for
6855    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6856    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6857    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6858    operator, ERROR_MARK is returned.  */
6859
6860 static enum tree_code
6861 cp_parser_assignment_operator_opt (cp_parser* parser)
6862 {
6863   enum tree_code op;
6864   cp_token *token;
6865
6866   /* Peek at the next token.  */
6867   token = cp_lexer_peek_token (parser->lexer);
6868
6869   switch (token->type)
6870     {
6871     case CPP_EQ:
6872       op = NOP_EXPR;
6873       break;
6874
6875     case CPP_MULT_EQ:
6876       op = MULT_EXPR;
6877       break;
6878
6879     case CPP_DIV_EQ:
6880       op = TRUNC_DIV_EXPR;
6881       break;
6882
6883     case CPP_MOD_EQ:
6884       op = TRUNC_MOD_EXPR;
6885       break;
6886
6887     case CPP_PLUS_EQ:
6888       op = PLUS_EXPR;
6889       break;
6890
6891     case CPP_MINUS_EQ:
6892       op = MINUS_EXPR;
6893       break;
6894
6895     case CPP_RSHIFT_EQ:
6896       op = RSHIFT_EXPR;
6897       break;
6898
6899     case CPP_LSHIFT_EQ:
6900       op = LSHIFT_EXPR;
6901       break;
6902
6903     case CPP_AND_EQ:
6904       op = BIT_AND_EXPR;
6905       break;
6906
6907     case CPP_XOR_EQ:
6908       op = BIT_XOR_EXPR;
6909       break;
6910
6911     case CPP_OR_EQ:
6912       op = BIT_IOR_EXPR;
6913       break;
6914
6915     default:
6916       /* Nothing else is an assignment operator.  */
6917       op = ERROR_MARK;
6918     }
6919
6920   /* If it was an assignment operator, consume it.  */
6921   if (op != ERROR_MARK)
6922     cp_lexer_consume_token (parser->lexer);
6923
6924   return op;
6925 }
6926
6927 /* Parse an expression.
6928
6929    expression:
6930      assignment-expression
6931      expression , assignment-expression
6932
6933    CAST_P is true if this expression is the target of a cast.
6934
6935    Returns a representation of the expression.  */
6936
6937 static tree
6938 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6939 {
6940   tree expression = NULL_TREE;
6941
6942   while (true)
6943     {
6944       tree assignment_expression;
6945
6946       /* Parse the next assignment-expression.  */
6947       assignment_expression
6948         = cp_parser_assignment_expression (parser, cast_p, pidk);
6949       /* If this is the first assignment-expression, we can just
6950          save it away.  */
6951       if (!expression)
6952         expression = assignment_expression;
6953       else
6954         expression = build_x_compound_expr (expression,
6955                                             assignment_expression,
6956                                             tf_warning_or_error);
6957       /* If the next token is not a comma, then we are done with the
6958          expression.  */
6959       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6960         break;
6961       /* Consume the `,'.  */
6962       cp_lexer_consume_token (parser->lexer);
6963       /* A comma operator cannot appear in a constant-expression.  */
6964       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6965         expression = error_mark_node;
6966     }
6967
6968   return expression;
6969 }
6970
6971 /* Parse a constant-expression.
6972
6973    constant-expression:
6974      conditional-expression
6975
6976   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6977   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6978   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6979   is false, NON_CONSTANT_P should be NULL.  */
6980
6981 static tree
6982 cp_parser_constant_expression (cp_parser* parser,
6983                                bool allow_non_constant_p,
6984                                bool *non_constant_p)
6985 {
6986   bool saved_integral_constant_expression_p;
6987   bool saved_allow_non_integral_constant_expression_p;
6988   bool saved_non_integral_constant_expression_p;
6989   tree expression;
6990
6991   /* It might seem that we could simply parse the
6992      conditional-expression, and then check to see if it were
6993      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6994      one that the compiler can figure out is constant, possibly after
6995      doing some simplifications or optimizations.  The standard has a
6996      precise definition of constant-expression, and we must honor
6997      that, even though it is somewhat more restrictive.
6998
6999      For example:
7000
7001        int i[(2, 3)];
7002
7003      is not a legal declaration, because `(2, 3)' is not a
7004      constant-expression.  The `,' operator is forbidden in a
7005      constant-expression.  However, GCC's constant-folding machinery
7006      will fold this operation to an INTEGER_CST for `3'.  */
7007
7008   /* Save the old settings.  */
7009   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7010   saved_allow_non_integral_constant_expression_p
7011     = parser->allow_non_integral_constant_expression_p;
7012   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7013   /* We are now parsing a constant-expression.  */
7014   parser->integral_constant_expression_p = true;
7015   parser->allow_non_integral_constant_expression_p
7016     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7017   parser->non_integral_constant_expression_p = false;
7018   /* Although the grammar says "conditional-expression", we parse an
7019      "assignment-expression", which also permits "throw-expression"
7020      and the use of assignment operators.  In the case that
7021      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7022      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7023      actually essential that we look for an assignment-expression.
7024      For example, cp_parser_initializer_clauses uses this function to
7025      determine whether a particular assignment-expression is in fact
7026      constant.  */
7027   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7028   /* Restore the old settings.  */
7029   parser->integral_constant_expression_p
7030     = saved_integral_constant_expression_p;
7031   parser->allow_non_integral_constant_expression_p
7032     = saved_allow_non_integral_constant_expression_p;
7033   if (cxx_dialect >= cxx0x)
7034     {
7035       /* Require an rvalue constant expression here; that's what our
7036          callers expect.  Reference constant expressions are handled
7037          separately in e.g. cp_parser_template_argument.  */
7038       bool is_const = potential_rvalue_constant_expression (expression);
7039       parser->non_integral_constant_expression_p = !is_const;
7040       if (!is_const && !allow_non_constant_p)
7041         require_potential_rvalue_constant_expression (expression);
7042     }
7043   if (allow_non_constant_p)
7044     *non_constant_p = parser->non_integral_constant_expression_p;
7045   else if (parser->non_integral_constant_expression_p)
7046     expression = error_mark_node;
7047   parser->non_integral_constant_expression_p
7048     = saved_non_integral_constant_expression_p;
7049
7050   return expression;
7051 }
7052
7053 /* Parse __builtin_offsetof.
7054
7055    offsetof-expression:
7056      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7057
7058    offsetof-member-designator:
7059      id-expression
7060      | offsetof-member-designator "." id-expression
7061      | offsetof-member-designator "[" expression "]"
7062      | offsetof-member-designator "->" id-expression  */
7063
7064 static tree
7065 cp_parser_builtin_offsetof (cp_parser *parser)
7066 {
7067   int save_ice_p, save_non_ice_p;
7068   tree type, expr;
7069   cp_id_kind dummy;
7070   cp_token *token;
7071
7072   /* We're about to accept non-integral-constant things, but will
7073      definitely yield an integral constant expression.  Save and
7074      restore these values around our local parsing.  */
7075   save_ice_p = parser->integral_constant_expression_p;
7076   save_non_ice_p = parser->non_integral_constant_expression_p;
7077
7078   /* Consume the "__builtin_offsetof" token.  */
7079   cp_lexer_consume_token (parser->lexer);
7080   /* Consume the opening `('.  */
7081   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7082   /* Parse the type-id.  */
7083   type = cp_parser_type_id (parser);
7084   /* Look for the `,'.  */
7085   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7086   token = cp_lexer_peek_token (parser->lexer);
7087
7088   /* Build the (type *)null that begins the traditional offsetof macro.  */
7089   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7090                             tf_warning_or_error);
7091
7092   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7093   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7094                                                  true, &dummy, token->location);
7095   while (true)
7096     {
7097       token = cp_lexer_peek_token (parser->lexer);
7098       switch (token->type)
7099         {
7100         case CPP_OPEN_SQUARE:
7101           /* offsetof-member-designator "[" expression "]" */
7102           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7103           break;
7104
7105         case CPP_DEREF:
7106           /* offsetof-member-designator "->" identifier */
7107           expr = grok_array_decl (expr, integer_zero_node);
7108           /* FALLTHRU */
7109
7110         case CPP_DOT:
7111           /* offsetof-member-designator "." identifier */
7112           cp_lexer_consume_token (parser->lexer);
7113           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7114                                                          expr, true, &dummy,
7115                                                          token->location);
7116           break;
7117
7118         case CPP_CLOSE_PAREN:
7119           /* Consume the ")" token.  */
7120           cp_lexer_consume_token (parser->lexer);
7121           goto success;
7122
7123         default:
7124           /* Error.  We know the following require will fail, but
7125              that gives the proper error message.  */
7126           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7127           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7128           expr = error_mark_node;
7129           goto failure;
7130         }
7131     }
7132
7133  success:
7134   /* If we're processing a template, we can't finish the semantics yet.
7135      Otherwise we can fold the entire expression now.  */
7136   if (processing_template_decl)
7137     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7138   else
7139     expr = finish_offsetof (expr);
7140
7141  failure:
7142   parser->integral_constant_expression_p = save_ice_p;
7143   parser->non_integral_constant_expression_p = save_non_ice_p;
7144
7145   return expr;
7146 }
7147
7148 /* Parse a trait expression.
7149
7150    Returns a representation of the expression, the underlying type
7151    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7152
7153 static tree
7154 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7155 {
7156   cp_trait_kind kind;
7157   tree type1, type2 = NULL_TREE;
7158   bool binary = false;
7159   cp_decl_specifier_seq decl_specs;
7160
7161   switch (keyword)
7162     {
7163     case RID_HAS_NOTHROW_ASSIGN:
7164       kind = CPTK_HAS_NOTHROW_ASSIGN;
7165       break;
7166     case RID_HAS_NOTHROW_CONSTRUCTOR:
7167       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7168       break;
7169     case RID_HAS_NOTHROW_COPY:
7170       kind = CPTK_HAS_NOTHROW_COPY;
7171       break;
7172     case RID_HAS_TRIVIAL_ASSIGN:
7173       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7174       break;
7175     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7176       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7177       break;
7178     case RID_HAS_TRIVIAL_COPY:
7179       kind = CPTK_HAS_TRIVIAL_COPY;
7180       break;
7181     case RID_HAS_TRIVIAL_DESTRUCTOR:
7182       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7183       break;
7184     case RID_HAS_VIRTUAL_DESTRUCTOR:
7185       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7186       break;
7187     case RID_IS_ABSTRACT:
7188       kind = CPTK_IS_ABSTRACT;
7189       break;
7190     case RID_IS_BASE_OF:
7191       kind = CPTK_IS_BASE_OF;
7192       binary = true;
7193       break;
7194     case RID_IS_CLASS:
7195       kind = CPTK_IS_CLASS;
7196       break;
7197     case RID_IS_CONVERTIBLE_TO:
7198       kind = CPTK_IS_CONVERTIBLE_TO;
7199       binary = true;
7200       break;
7201     case RID_IS_EMPTY:
7202       kind = CPTK_IS_EMPTY;
7203       break;
7204     case RID_IS_ENUM:
7205       kind = CPTK_IS_ENUM;
7206       break;
7207     case RID_IS_LITERAL_TYPE:
7208       kind = CPTK_IS_LITERAL_TYPE;
7209       break;
7210     case RID_IS_POD:
7211       kind = CPTK_IS_POD;
7212       break;
7213     case RID_IS_POLYMORPHIC:
7214       kind = CPTK_IS_POLYMORPHIC;
7215       break;
7216     case RID_IS_STD_LAYOUT:
7217       kind = CPTK_IS_STD_LAYOUT;
7218       break;
7219     case RID_IS_TRIVIAL:
7220       kind = CPTK_IS_TRIVIAL;
7221       break;
7222     case RID_IS_UNION:
7223       kind = CPTK_IS_UNION;
7224       break;
7225     case RID_UNDERLYING_TYPE:
7226       kind = CPTK_UNDERLYING_TYPE;
7227       break;
7228     default:
7229       gcc_unreachable ();
7230     }
7231
7232   /* Consume the token.  */
7233   cp_lexer_consume_token (parser->lexer);
7234
7235   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7236
7237   type1 = cp_parser_type_id (parser);
7238
7239   if (type1 == error_mark_node)
7240     return error_mark_node;
7241
7242   /* Build a trivial decl-specifier-seq.  */
7243   clear_decl_specs (&decl_specs);
7244   decl_specs.type = type1;
7245
7246   /* Call grokdeclarator to figure out what type this is.  */
7247   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7248                           /*initialized=*/0, /*attrlist=*/NULL);
7249
7250   if (binary)
7251     {
7252       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7253  
7254       type2 = cp_parser_type_id (parser);
7255
7256       if (type2 == error_mark_node)
7257         return error_mark_node;
7258
7259       /* Build a trivial decl-specifier-seq.  */
7260       clear_decl_specs (&decl_specs);
7261       decl_specs.type = type2;
7262
7263       /* Call grokdeclarator to figure out what type this is.  */
7264       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7265                               /*initialized=*/0, /*attrlist=*/NULL);
7266     }
7267
7268   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7269
7270   /* Complete the trait expression, which may mean either processing
7271      the trait expr now or saving it for template instantiation.  */
7272   return kind != CPTK_UNDERLYING_TYPE
7273     ? finish_trait_expr (kind, type1, type2)
7274     : finish_underlying_type (type1);
7275 }
7276
7277 /* Lambdas that appear in variable initializer or default argument scope
7278    get that in their mangling, so we need to record it.  We might as well
7279    use the count for function and namespace scopes as well.  */
7280 static GTY(()) tree lambda_scope;
7281 static GTY(()) int lambda_count;
7282 typedef struct GTY(()) tree_int
7283 {
7284   tree t;
7285   int i;
7286 } tree_int;
7287 DEF_VEC_O(tree_int);
7288 DEF_VEC_ALLOC_O(tree_int,gc);
7289 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7290
7291 static void
7292 start_lambda_scope (tree decl)
7293 {
7294   tree_int ti;
7295   gcc_assert (decl);
7296   /* Once we're inside a function, we ignore other scopes and just push
7297      the function again so that popping works properly.  */
7298   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7299     decl = current_function_decl;
7300   ti.t = lambda_scope;
7301   ti.i = lambda_count;
7302   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7303   if (lambda_scope != decl)
7304     {
7305       /* Don't reset the count if we're still in the same function.  */
7306       lambda_scope = decl;
7307       lambda_count = 0;
7308     }
7309 }
7310
7311 static void
7312 record_lambda_scope (tree lambda)
7313 {
7314   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7315   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7316 }
7317
7318 static void
7319 finish_lambda_scope (void)
7320 {
7321   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7322   if (lambda_scope != p->t)
7323     {
7324       lambda_scope = p->t;
7325       lambda_count = p->i;
7326     }
7327   VEC_pop (tree_int, lambda_scope_stack);
7328 }
7329
7330 /* Parse a lambda expression.
7331
7332    lambda-expression:
7333      lambda-introducer lambda-declarator [opt] compound-statement
7334
7335    Returns a representation of the expression.  */
7336
7337 static tree
7338 cp_parser_lambda_expression (cp_parser* parser)
7339 {
7340   tree lambda_expr = build_lambda_expr ();
7341   tree type;
7342
7343   LAMBDA_EXPR_LOCATION (lambda_expr)
7344     = cp_lexer_peek_token (parser->lexer)->location;
7345
7346   if (cp_unevaluated_operand)
7347     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7348               "lambda-expression in unevaluated context");
7349
7350   /* We may be in the middle of deferred access check.  Disable
7351      it now.  */
7352   push_deferring_access_checks (dk_no_deferred);
7353
7354   cp_parser_lambda_introducer (parser, lambda_expr);
7355
7356   type = begin_lambda_type (lambda_expr);
7357
7358   record_lambda_scope (lambda_expr);
7359
7360   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7361   determine_visibility (TYPE_NAME (type));
7362
7363   /* Now that we've started the type, add the capture fields for any
7364      explicit captures.  */
7365   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7366
7367   {
7368     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7369     unsigned int saved_num_template_parameter_lists
7370         = parser->num_template_parameter_lists;
7371
7372     parser->num_template_parameter_lists = 0;
7373
7374     /* By virtue of defining a local class, a lambda expression has access to
7375        the private variables of enclosing classes.  */
7376
7377     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7378
7379     cp_parser_lambda_body (parser, lambda_expr);
7380
7381     /* The capture list was built up in reverse order; fix that now.  */
7382     {
7383       tree newlist = NULL_TREE;
7384       tree elt, next;
7385
7386       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7387            elt; elt = next)
7388         {
7389           tree field = TREE_PURPOSE (elt);
7390           char *buf;
7391
7392           next = TREE_CHAIN (elt);
7393           TREE_CHAIN (elt) = newlist;
7394           newlist = elt;
7395
7396           /* Also add __ to the beginning of the field name so that code
7397              outside the lambda body can't see the captured name.  We could
7398              just remove the name entirely, but this is more useful for
7399              debugging.  */
7400           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7401             /* The 'this' capture already starts with __.  */
7402             continue;
7403
7404           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7405           buf[1] = buf[0] = '_';
7406           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7407                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7408           DECL_NAME (field) = get_identifier (buf);
7409         }
7410       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7411     }
7412
7413     maybe_add_lambda_conv_op (type);
7414
7415     type = finish_struct (type, /*attributes=*/NULL_TREE);
7416
7417     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7418   }
7419
7420   pop_deferring_access_checks ();
7421
7422   return build_lambda_object (lambda_expr);
7423 }
7424
7425 /* Parse the beginning of a lambda expression.
7426
7427    lambda-introducer:
7428      [ lambda-capture [opt] ]
7429
7430    LAMBDA_EXPR is the current representation of the lambda expression.  */
7431
7432 static void
7433 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7434 {
7435   /* Need commas after the first capture.  */
7436   bool first = true;
7437
7438   /* Eat the leading `['.  */
7439   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7440
7441   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7442   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7443       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7444     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7445   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7446     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7447
7448   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7449     {
7450       cp_lexer_consume_token (parser->lexer);
7451       first = false;
7452     }
7453
7454   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7455     {
7456       cp_token* capture_token;
7457       tree capture_id;
7458       tree capture_init_expr;
7459       cp_id_kind idk = CP_ID_KIND_NONE;
7460       bool explicit_init_p = false;
7461
7462       enum capture_kind_type
7463       {
7464         BY_COPY,
7465         BY_REFERENCE
7466       };
7467       enum capture_kind_type capture_kind = BY_COPY;
7468
7469       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7470         {
7471           error ("expected end of capture-list");
7472           return;
7473         }
7474
7475       if (first)
7476         first = false;
7477       else
7478         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7479
7480       /* Possibly capture `this'.  */
7481       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7482         {
7483           cp_lexer_consume_token (parser->lexer);
7484           add_capture (lambda_expr,
7485                        /*id=*/get_identifier ("__this"),
7486                        /*initializer=*/finish_this_expr(),
7487                        /*by_reference_p=*/false,
7488                        explicit_init_p);
7489           continue;
7490         }
7491
7492       /* Remember whether we want to capture as a reference or not.  */
7493       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7494         {
7495           capture_kind = BY_REFERENCE;
7496           cp_lexer_consume_token (parser->lexer);
7497         }
7498
7499       /* Get the identifier.  */
7500       capture_token = cp_lexer_peek_token (parser->lexer);
7501       capture_id = cp_parser_identifier (parser);
7502
7503       if (capture_id == error_mark_node)
7504         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7505            delimiters, but I modified this to stop on unnested ']' as well.  It
7506            was already changed to stop on unnested '}', so the
7507            "closing_parenthesis" name is no more misleading with my change.  */
7508         {
7509           cp_parser_skip_to_closing_parenthesis (parser,
7510                                                  /*recovering=*/true,
7511                                                  /*or_comma=*/true,
7512                                                  /*consume_paren=*/true);
7513           break;
7514         }
7515
7516       /* Find the initializer for this capture.  */
7517       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7518         {
7519           /* An explicit expression exists.  */
7520           cp_lexer_consume_token (parser->lexer);
7521           pedwarn (input_location, OPT_pedantic,
7522                    "ISO C++ does not allow initializers "
7523                    "in lambda expression capture lists");
7524           capture_init_expr = cp_parser_assignment_expression (parser,
7525                                                                /*cast_p=*/true,
7526                                                                &idk);
7527           explicit_init_p = true;
7528         }
7529       else
7530         {
7531           const char* error_msg;
7532
7533           /* Turn the identifier into an id-expression.  */
7534           capture_init_expr
7535             = cp_parser_lookup_name
7536                 (parser,
7537                  capture_id,
7538                  none_type,
7539                  /*is_template=*/false,
7540                  /*is_namespace=*/false,
7541                  /*check_dependency=*/true,
7542                  /*ambiguous_decls=*/NULL,
7543                  capture_token->location);
7544
7545           capture_init_expr
7546             = finish_id_expression
7547                 (capture_id,
7548                  capture_init_expr,
7549                  parser->scope,
7550                  &idk,
7551                  /*integral_constant_expression_p=*/false,
7552                  /*allow_non_integral_constant_expression_p=*/false,
7553                  /*non_integral_constant_expression_p=*/NULL,
7554                  /*template_p=*/false,
7555                  /*done=*/true,
7556                  /*address_p=*/false,
7557                  /*template_arg_p=*/false,
7558                  &error_msg,
7559                  capture_token->location);
7560         }
7561
7562       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7563         capture_init_expr
7564           = unqualified_name_lookup_error (capture_init_expr);
7565
7566       add_capture (lambda_expr,
7567                    capture_id,
7568                    capture_init_expr,
7569                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7570                    explicit_init_p);
7571     }
7572
7573   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7574 }
7575
7576 /* Parse the (optional) middle of a lambda expression.
7577
7578    lambda-declarator:
7579      ( parameter-declaration-clause [opt] )
7580        attribute-specifier [opt]
7581        mutable [opt]
7582        exception-specification [opt]
7583        lambda-return-type-clause [opt]
7584
7585    LAMBDA_EXPR is the current representation of the lambda expression.  */
7586
7587 static void
7588 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7589 {
7590   /* 5.1.1.4 of the standard says:
7591        If a lambda-expression does not include a lambda-declarator, it is as if
7592        the lambda-declarator were ().
7593      This means an empty parameter list, no attributes, and no exception
7594      specification.  */
7595   tree param_list = void_list_node;
7596   tree attributes = NULL_TREE;
7597   tree exception_spec = NULL_TREE;
7598   tree t;
7599
7600   /* The lambda-declarator is optional, but must begin with an opening
7601      parenthesis if present.  */
7602   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7603     {
7604       cp_lexer_consume_token (parser->lexer);
7605
7606       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7607
7608       /* Parse parameters.  */
7609       param_list = cp_parser_parameter_declaration_clause (parser);
7610
7611       /* Default arguments shall not be specified in the
7612          parameter-declaration-clause of a lambda-declarator.  */
7613       for (t = param_list; t; t = TREE_CHAIN (t))
7614         if (TREE_PURPOSE (t))
7615           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7616                    "default argument specified for lambda parameter");
7617
7618       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7619
7620       attributes = cp_parser_attributes_opt (parser);
7621
7622       /* Parse optional `mutable' keyword.  */
7623       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7624         {
7625           cp_lexer_consume_token (parser->lexer);
7626           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7627         }
7628
7629       /* Parse optional exception specification.  */
7630       exception_spec = cp_parser_exception_specification_opt (parser);
7631
7632       /* Parse optional trailing return type.  */
7633       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7634         {
7635           cp_lexer_consume_token (parser->lexer);
7636           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7637         }
7638
7639       /* The function parameters must be in scope all the way until after the
7640          trailing-return-type in case of decltype.  */
7641       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7642         pop_binding (DECL_NAME (t), t);
7643
7644       leave_scope ();
7645     }
7646
7647   /* Create the function call operator.
7648
7649      Messing with declarators like this is no uglier than building up the
7650      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7651      other code.  */
7652   {
7653     cp_decl_specifier_seq return_type_specs;
7654     cp_declarator* declarator;
7655     tree fco;
7656     int quals;
7657     void *p;
7658
7659     clear_decl_specs (&return_type_specs);
7660     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7661       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7662     else
7663       /* Maybe we will deduce the return type later, but we can use void
7664          as a placeholder return type anyways.  */
7665       return_type_specs.type = void_type_node;
7666
7667     p = obstack_alloc (&declarator_obstack, 0);
7668
7669     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7670                                      sfk_none);
7671
7672     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7673              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7674     declarator = make_call_declarator (declarator, param_list, quals,
7675                                        VIRT_SPEC_UNSPECIFIED,
7676                                        exception_spec,
7677                                        /*late_return_type=*/NULL_TREE);
7678     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7679
7680     fco = grokmethod (&return_type_specs,
7681                       declarator,
7682                       attributes);
7683     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7684     DECL_ARTIFICIAL (fco) = 1;
7685
7686     finish_member_declaration (fco);
7687
7688     obstack_free (&declarator_obstack, p);
7689   }
7690 }
7691
7692 /* Parse the body of a lambda expression, which is simply
7693
7694    compound-statement
7695
7696    but which requires special handling.
7697    LAMBDA_EXPR is the current representation of the lambda expression.  */
7698
7699 static void
7700 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7701 {
7702   bool nested = (current_function_decl != NULL_TREE);
7703   if (nested)
7704     push_function_context ();
7705
7706   /* Finish the function call operator
7707      - class_specifier
7708      + late_parsing_for_member
7709      + function_definition_after_declarator
7710      + ctor_initializer_opt_and_function_body  */
7711   {
7712     tree fco = lambda_function (lambda_expr);
7713     tree body;
7714     bool done = false;
7715
7716     /* Let the front end know that we are going to be defining this
7717        function.  */
7718     start_preparsed_function (fco,
7719                               NULL_TREE,
7720                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7721
7722     start_lambda_scope (fco);
7723     body = begin_function_body ();
7724
7725     /* 5.1.1.4 of the standard says:
7726          If a lambda-expression does not include a trailing-return-type, it
7727          is as if the trailing-return-type denotes the following type:
7728           * if the compound-statement is of the form
7729                { return attribute-specifier [opt] expression ; }
7730              the type of the returned expression after lvalue-to-rvalue
7731              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7732              (_conv.array_ 4.2), and function-to-pointer conversion
7733              (_conv.func_ 4.3);
7734           * otherwise, void.  */
7735
7736     /* In a lambda that has neither a lambda-return-type-clause
7737        nor a deducible form, errors should be reported for return statements
7738        in the body.  Since we used void as the placeholder return type, parsing
7739        the body as usual will give such desired behavior.  */
7740     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7741         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7742         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7743         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7744       {
7745         tree compound_stmt;
7746         tree expr = NULL_TREE;
7747         cp_id_kind idk = CP_ID_KIND_NONE;
7748
7749         /* Parse tentatively in case there's more after the initial return
7750            statement.  */
7751         cp_parser_parse_tentatively (parser);
7752
7753         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7754         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7755
7756         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7757
7758         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7759         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7760
7761         if (cp_parser_parse_definitely (parser))
7762           {
7763             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7764
7765             compound_stmt = begin_compound_stmt (0);
7766             /* Will get error here if type not deduced yet.  */
7767             finish_return_stmt (expr);
7768             finish_compound_stmt (compound_stmt);
7769
7770             done = true;
7771           }
7772       }
7773
7774     if (!done)
7775       {
7776         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7777           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7778         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7779            cp_parser_compound_stmt does not pass it.  */
7780         cp_parser_function_body (parser);
7781         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7782       }
7783
7784     finish_function_body (body);
7785     finish_lambda_scope ();
7786
7787     /* Finish the function and generate code for it if necessary.  */
7788     expand_or_defer_fn (finish_function (/*inline*/2));
7789   }
7790
7791   if (nested)
7792     pop_function_context();
7793 }
7794
7795 /* Statements [gram.stmt.stmt]  */
7796
7797 /* Parse a statement.
7798
7799    statement:
7800      labeled-statement
7801      expression-statement
7802      compound-statement
7803      selection-statement
7804      iteration-statement
7805      jump-statement
7806      declaration-statement
7807      try-block
7808
7809   IN_COMPOUND is true when the statement is nested inside a
7810   cp_parser_compound_statement; this matters for certain pragmas.
7811
7812   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7813   is a (possibly labeled) if statement which is not enclosed in braces
7814   and has an else clause.  This is used to implement -Wparentheses.  */
7815
7816 static void
7817 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7818                      bool in_compound, bool *if_p)
7819 {
7820   tree statement;
7821   cp_token *token;
7822   location_t statement_location;
7823
7824  restart:
7825   if (if_p != NULL)
7826     *if_p = false;
7827   /* There is no statement yet.  */
7828   statement = NULL_TREE;
7829   /* Peek at the next token.  */
7830   token = cp_lexer_peek_token (parser->lexer);
7831   /* Remember the location of the first token in the statement.  */
7832   statement_location = token->location;
7833   /* If this is a keyword, then that will often determine what kind of
7834      statement we have.  */
7835   if (token->type == CPP_KEYWORD)
7836     {
7837       enum rid keyword = token->keyword;
7838
7839       switch (keyword)
7840         {
7841         case RID_CASE:
7842         case RID_DEFAULT:
7843           /* Looks like a labeled-statement with a case label.
7844              Parse the label, and then use tail recursion to parse
7845              the statement.  */
7846           cp_parser_label_for_labeled_statement (parser);
7847           goto restart;
7848
7849         case RID_IF:
7850         case RID_SWITCH:
7851           statement = cp_parser_selection_statement (parser, if_p);
7852           break;
7853
7854         case RID_WHILE:
7855         case RID_DO:
7856         case RID_FOR:
7857           statement = cp_parser_iteration_statement (parser);
7858           break;
7859
7860         case RID_BREAK:
7861         case RID_CONTINUE:
7862         case RID_RETURN:
7863         case RID_GOTO:
7864           statement = cp_parser_jump_statement (parser);
7865           break;
7866
7867           /* Objective-C++ exception-handling constructs.  */
7868         case RID_AT_TRY:
7869         case RID_AT_CATCH:
7870         case RID_AT_FINALLY:
7871         case RID_AT_SYNCHRONIZED:
7872         case RID_AT_THROW:
7873           statement = cp_parser_objc_statement (parser);
7874           break;
7875
7876         case RID_TRY:
7877           statement = cp_parser_try_block (parser);
7878           break;
7879
7880         case RID_NAMESPACE:
7881           /* This must be a namespace alias definition.  */
7882           cp_parser_declaration_statement (parser);
7883           return;
7884           
7885         default:
7886           /* It might be a keyword like `int' that can start a
7887              declaration-statement.  */
7888           break;
7889         }
7890     }
7891   else if (token->type == CPP_NAME)
7892     {
7893       /* If the next token is a `:', then we are looking at a
7894          labeled-statement.  */
7895       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7896       if (token->type == CPP_COLON)
7897         {
7898           /* Looks like a labeled-statement with an ordinary label.
7899              Parse the label, and then use tail recursion to parse
7900              the statement.  */
7901           cp_parser_label_for_labeled_statement (parser);
7902           goto restart;
7903         }
7904     }
7905   /* Anything that starts with a `{' must be a compound-statement.  */
7906   else if (token->type == CPP_OPEN_BRACE)
7907     statement = cp_parser_compound_statement (parser, NULL, false, false);
7908   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7909      a statement all its own.  */
7910   else if (token->type == CPP_PRAGMA)
7911     {
7912       /* Only certain OpenMP pragmas are attached to statements, and thus
7913          are considered statements themselves.  All others are not.  In
7914          the context of a compound, accept the pragma as a "statement" and
7915          return so that we can check for a close brace.  Otherwise we
7916          require a real statement and must go back and read one.  */
7917       if (in_compound)
7918         cp_parser_pragma (parser, pragma_compound);
7919       else if (!cp_parser_pragma (parser, pragma_stmt))
7920         goto restart;
7921       return;
7922     }
7923   else if (token->type == CPP_EOF)
7924     {
7925       cp_parser_error (parser, "expected statement");
7926       return;
7927     }
7928
7929   /* Everything else must be a declaration-statement or an
7930      expression-statement.  Try for the declaration-statement
7931      first, unless we are looking at a `;', in which case we know that
7932      we have an expression-statement.  */
7933   if (!statement)
7934     {
7935       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7936         {
7937           cp_parser_parse_tentatively (parser);
7938           /* Try to parse the declaration-statement.  */
7939           cp_parser_declaration_statement (parser);
7940           /* If that worked, we're done.  */
7941           if (cp_parser_parse_definitely (parser))
7942             return;
7943         }
7944       /* Look for an expression-statement instead.  */
7945       statement = cp_parser_expression_statement (parser, in_statement_expr);
7946     }
7947
7948   /* Set the line number for the statement.  */
7949   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7950     SET_EXPR_LOCATION (statement, statement_location);
7951 }
7952
7953 /* Parse the label for a labeled-statement, i.e.
7954
7955    identifier :
7956    case constant-expression :
7957    default :
7958
7959    GNU Extension:
7960    case constant-expression ... constant-expression : statement
7961
7962    When a label is parsed without errors, the label is added to the
7963    parse tree by the finish_* functions, so this function doesn't
7964    have to return the label.  */
7965
7966 static void
7967 cp_parser_label_for_labeled_statement (cp_parser* parser)
7968 {
7969   cp_token *token;
7970   tree label = NULL_TREE;
7971   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7972
7973   /* The next token should be an identifier.  */
7974   token = cp_lexer_peek_token (parser->lexer);
7975   if (token->type != CPP_NAME
7976       && token->type != CPP_KEYWORD)
7977     {
7978       cp_parser_error (parser, "expected labeled-statement");
7979       return;
7980     }
7981
7982   parser->colon_corrects_to_scope_p = false;
7983   switch (token->keyword)
7984     {
7985     case RID_CASE:
7986       {
7987         tree expr, expr_hi;
7988         cp_token *ellipsis;
7989
7990         /* Consume the `case' token.  */
7991         cp_lexer_consume_token (parser->lexer);
7992         /* Parse the constant-expression.  */
7993         expr = cp_parser_constant_expression (parser,
7994                                               /*allow_non_constant_p=*/false,
7995                                               NULL);
7996
7997         ellipsis = cp_lexer_peek_token (parser->lexer);
7998         if (ellipsis->type == CPP_ELLIPSIS)
7999           {
8000             /* Consume the `...' token.  */
8001             cp_lexer_consume_token (parser->lexer);
8002             expr_hi =
8003               cp_parser_constant_expression (parser,
8004                                              /*allow_non_constant_p=*/false,
8005                                              NULL);
8006             /* We don't need to emit warnings here, as the common code
8007                will do this for us.  */
8008           }
8009         else
8010           expr_hi = NULL_TREE;
8011
8012         if (parser->in_switch_statement_p)
8013           finish_case_label (token->location, expr, expr_hi);
8014         else
8015           error_at (token->location,
8016                     "case label %qE not within a switch statement",
8017                     expr);
8018       }
8019       break;
8020
8021     case RID_DEFAULT:
8022       /* Consume the `default' token.  */
8023       cp_lexer_consume_token (parser->lexer);
8024
8025       if (parser->in_switch_statement_p)
8026         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8027       else
8028         error_at (token->location, "case label not within a switch statement");
8029       break;
8030
8031     default:
8032       /* Anything else must be an ordinary label.  */
8033       label = finish_label_stmt (cp_parser_identifier (parser));
8034       break;
8035     }
8036
8037   /* Require the `:' token.  */
8038   cp_parser_require (parser, CPP_COLON, RT_COLON);
8039
8040   /* An ordinary label may optionally be followed by attributes.
8041      However, this is only permitted if the attributes are then
8042      followed by a semicolon.  This is because, for backward
8043      compatibility, when parsing
8044        lab: __attribute__ ((unused)) int i;
8045      we want the attribute to attach to "i", not "lab".  */
8046   if (label != NULL_TREE
8047       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8048     {
8049       tree attrs;
8050
8051       cp_parser_parse_tentatively (parser);
8052       attrs = cp_parser_attributes_opt (parser);
8053       if (attrs == NULL_TREE
8054           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8055         cp_parser_abort_tentative_parse (parser);
8056       else if (!cp_parser_parse_definitely (parser))
8057         ;
8058       else
8059         cplus_decl_attributes (&label, attrs, 0);
8060     }
8061
8062   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8063 }
8064
8065 /* Parse an expression-statement.
8066
8067    expression-statement:
8068      expression [opt] ;
8069
8070    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8071    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8072    indicates whether this expression-statement is part of an
8073    expression statement.  */
8074
8075 static tree
8076 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8077 {
8078   tree statement = NULL_TREE;
8079   cp_token *token = cp_lexer_peek_token (parser->lexer);
8080
8081   /* If the next token is a ';', then there is no expression
8082      statement.  */
8083   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8084     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8085
8086   /* Give a helpful message for "A<T>::type t;" and the like.  */
8087   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8088       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8089     {
8090       if (TREE_CODE (statement) == SCOPE_REF)
8091         error_at (token->location, "need %<typename%> before %qE because "
8092                   "%qT is a dependent scope",
8093                   statement, TREE_OPERAND (statement, 0));
8094       else if (is_overloaded_fn (statement)
8095                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8096         {
8097           /* A::A a; */
8098           tree fn = get_first_fn (statement);
8099           error_at (token->location,
8100                     "%<%T::%D%> names the constructor, not the type",
8101                     DECL_CONTEXT (fn), DECL_NAME (fn));
8102         }
8103     }
8104
8105   /* Consume the final `;'.  */
8106   cp_parser_consume_semicolon_at_end_of_statement (parser);
8107
8108   if (in_statement_expr
8109       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8110     /* This is the final expression statement of a statement
8111        expression.  */
8112     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8113   else if (statement)
8114     statement = finish_expr_stmt (statement);
8115   else
8116     finish_stmt ();
8117
8118   return statement;
8119 }
8120
8121 /* Parse a compound-statement.
8122
8123    compound-statement:
8124      { statement-seq [opt] }
8125
8126    GNU extension:
8127
8128    compound-statement:
8129      { label-declaration-seq [opt] statement-seq [opt] }
8130
8131    label-declaration-seq:
8132      label-declaration
8133      label-declaration-seq label-declaration
8134
8135    Returns a tree representing the statement.  */
8136
8137 static tree
8138 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8139                               bool in_try, bool function_body)
8140 {
8141   tree compound_stmt;
8142
8143   /* Consume the `{'.  */
8144   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8145     return error_mark_node;
8146   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8147       && !function_body)
8148     pedwarn (input_location, OPT_pedantic,
8149              "compound-statement in constexpr function");
8150   /* Begin the compound-statement.  */
8151   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8152   /* If the next keyword is `__label__' we have a label declaration.  */
8153   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8154     cp_parser_label_declaration (parser);
8155   /* Parse an (optional) statement-seq.  */
8156   cp_parser_statement_seq_opt (parser, in_statement_expr);
8157   /* Finish the compound-statement.  */
8158   finish_compound_stmt (compound_stmt);
8159   /* Consume the `}'.  */
8160   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8161
8162   return compound_stmt;
8163 }
8164
8165 /* Parse an (optional) statement-seq.
8166
8167    statement-seq:
8168      statement
8169      statement-seq [opt] statement  */
8170
8171 static void
8172 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8173 {
8174   /* Scan statements until there aren't any more.  */
8175   while (true)
8176     {
8177       cp_token *token = cp_lexer_peek_token (parser->lexer);
8178
8179       /* If we are looking at a `}', then we have run out of
8180          statements; the same is true if we have reached the end
8181          of file, or have stumbled upon a stray '@end'.  */
8182       if (token->type == CPP_CLOSE_BRACE
8183           || token->type == CPP_EOF
8184           || token->type == CPP_PRAGMA_EOL
8185           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8186         break;
8187       
8188       /* If we are in a compound statement and find 'else' then
8189          something went wrong.  */
8190       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8191         {
8192           if (parser->in_statement & IN_IF_STMT) 
8193             break;
8194           else
8195             {
8196               token = cp_lexer_consume_token (parser->lexer);
8197               error_at (token->location, "%<else%> without a previous %<if%>");
8198             }
8199         }
8200
8201       /* Parse the statement.  */
8202       cp_parser_statement (parser, in_statement_expr, true, NULL);
8203     }
8204 }
8205
8206 /* Parse a selection-statement.
8207
8208    selection-statement:
8209      if ( condition ) statement
8210      if ( condition ) statement else statement
8211      switch ( condition ) statement
8212
8213    Returns the new IF_STMT or SWITCH_STMT.
8214
8215    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8216    is a (possibly labeled) if statement which is not enclosed in
8217    braces and has an else clause.  This is used to implement
8218    -Wparentheses.  */
8219
8220 static tree
8221 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8222 {
8223   cp_token *token;
8224   enum rid keyword;
8225
8226   if (if_p != NULL)
8227     *if_p = false;
8228
8229   /* Peek at the next token.  */
8230   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8231
8232   /* See what kind of keyword it is.  */
8233   keyword = token->keyword;
8234   switch (keyword)
8235     {
8236     case RID_IF:
8237     case RID_SWITCH:
8238       {
8239         tree statement;
8240         tree condition;
8241
8242         /* Look for the `('.  */
8243         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8244           {
8245             cp_parser_skip_to_end_of_statement (parser);
8246             return error_mark_node;
8247           }
8248
8249         /* Begin the selection-statement.  */
8250         if (keyword == RID_IF)
8251           statement = begin_if_stmt ();
8252         else
8253           statement = begin_switch_stmt ();
8254
8255         /* Parse the condition.  */
8256         condition = cp_parser_condition (parser);
8257         /* Look for the `)'.  */
8258         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8259           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8260                                                  /*consume_paren=*/true);
8261
8262         if (keyword == RID_IF)
8263           {
8264             bool nested_if;
8265             unsigned char in_statement;
8266
8267             /* Add the condition.  */
8268             finish_if_stmt_cond (condition, statement);
8269
8270             /* Parse the then-clause.  */
8271             in_statement = parser->in_statement;
8272             parser->in_statement |= IN_IF_STMT;
8273             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8274               {
8275                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8276                 add_stmt (build_empty_stmt (loc));
8277                 cp_lexer_consume_token (parser->lexer);
8278                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8279                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8280                               "empty body in an %<if%> statement");
8281                 nested_if = false;
8282               }
8283             else
8284               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8285             parser->in_statement = in_statement;
8286
8287             finish_then_clause (statement);
8288
8289             /* If the next token is `else', parse the else-clause.  */
8290             if (cp_lexer_next_token_is_keyword (parser->lexer,
8291                                                 RID_ELSE))
8292               {
8293                 /* Consume the `else' keyword.  */
8294                 cp_lexer_consume_token (parser->lexer);
8295                 begin_else_clause (statement);
8296                 /* Parse the else-clause.  */
8297                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8298                   {
8299                     location_t loc;
8300                     loc = cp_lexer_peek_token (parser->lexer)->location;
8301                     warning_at (loc,
8302                                 OPT_Wempty_body, "suggest braces around "
8303                                 "empty body in an %<else%> statement");
8304                     add_stmt (build_empty_stmt (loc));
8305                     cp_lexer_consume_token (parser->lexer);
8306                   }
8307                 else
8308                   cp_parser_implicitly_scoped_statement (parser, NULL);
8309
8310                 finish_else_clause (statement);
8311
8312                 /* If we are currently parsing a then-clause, then
8313                    IF_P will not be NULL.  We set it to true to
8314                    indicate that this if statement has an else clause.
8315                    This may trigger the Wparentheses warning below
8316                    when we get back up to the parent if statement.  */
8317                 if (if_p != NULL)
8318                   *if_p = true;
8319               }
8320             else
8321               {
8322                 /* This if statement does not have an else clause.  If
8323                    NESTED_IF is true, then the then-clause is an if
8324                    statement which does have an else clause.  We warn
8325                    about the potential ambiguity.  */
8326                 if (nested_if)
8327                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8328                               "suggest explicit braces to avoid ambiguous"
8329                               " %<else%>");
8330               }
8331
8332             /* Now we're all done with the if-statement.  */
8333             finish_if_stmt (statement);
8334           }
8335         else
8336           {
8337             bool in_switch_statement_p;
8338             unsigned char in_statement;
8339
8340             /* Add the condition.  */
8341             finish_switch_cond (condition, statement);
8342
8343             /* Parse the body of the switch-statement.  */
8344             in_switch_statement_p = parser->in_switch_statement_p;
8345             in_statement = parser->in_statement;
8346             parser->in_switch_statement_p = true;
8347             parser->in_statement |= IN_SWITCH_STMT;
8348             cp_parser_implicitly_scoped_statement (parser, NULL);
8349             parser->in_switch_statement_p = in_switch_statement_p;
8350             parser->in_statement = in_statement;
8351
8352             /* Now we're all done with the switch-statement.  */
8353             finish_switch_stmt (statement);
8354           }
8355
8356         return statement;
8357       }
8358       break;
8359
8360     default:
8361       cp_parser_error (parser, "expected selection-statement");
8362       return error_mark_node;
8363     }
8364 }
8365
8366 /* Parse a condition.
8367
8368    condition:
8369      expression
8370      type-specifier-seq declarator = initializer-clause
8371      type-specifier-seq declarator braced-init-list
8372
8373    GNU Extension:
8374
8375    condition:
8376      type-specifier-seq declarator asm-specification [opt]
8377        attributes [opt] = assignment-expression
8378
8379    Returns the expression that should be tested.  */
8380
8381 static tree
8382 cp_parser_condition (cp_parser* parser)
8383 {
8384   cp_decl_specifier_seq type_specifiers;
8385   const char *saved_message;
8386   int declares_class_or_enum;
8387
8388   /* Try the declaration first.  */
8389   cp_parser_parse_tentatively (parser);
8390   /* New types are not allowed in the type-specifier-seq for a
8391      condition.  */
8392   saved_message = parser->type_definition_forbidden_message;
8393   parser->type_definition_forbidden_message
8394     = G_("types may not be defined in conditions");
8395   /* Parse the type-specifier-seq.  */
8396   cp_parser_decl_specifier_seq (parser,
8397                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8398                                 &type_specifiers,
8399                                 &declares_class_or_enum);
8400   /* Restore the saved message.  */
8401   parser->type_definition_forbidden_message = saved_message;
8402   /* If all is well, we might be looking at a declaration.  */
8403   if (!cp_parser_error_occurred (parser))
8404     {
8405       tree decl;
8406       tree asm_specification;
8407       tree attributes;
8408       cp_declarator *declarator;
8409       tree initializer = NULL_TREE;
8410
8411       /* Parse the declarator.  */
8412       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8413                                          /*ctor_dtor_or_conv_p=*/NULL,
8414                                          /*parenthesized_p=*/NULL,
8415                                          /*member_p=*/false);
8416       /* Parse the attributes.  */
8417       attributes = cp_parser_attributes_opt (parser);
8418       /* Parse the asm-specification.  */
8419       asm_specification = cp_parser_asm_specification_opt (parser);
8420       /* If the next token is not an `=' or '{', then we might still be
8421          looking at an expression.  For example:
8422
8423            if (A(a).x)
8424
8425          looks like a decl-specifier-seq and a declarator -- but then
8426          there is no `=', so this is an expression.  */
8427       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8428           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8429         cp_parser_simulate_error (parser);
8430         
8431       /* If we did see an `=' or '{', then we are looking at a declaration
8432          for sure.  */
8433       if (cp_parser_parse_definitely (parser))
8434         {
8435           tree pushed_scope;
8436           bool non_constant_p;
8437           bool flags = LOOKUP_ONLYCONVERTING;
8438
8439           /* Create the declaration.  */
8440           decl = start_decl (declarator, &type_specifiers,
8441                              /*initialized_p=*/true,
8442                              attributes, /*prefix_attributes=*/NULL_TREE,
8443                              &pushed_scope);
8444
8445           /* Parse the initializer.  */
8446           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8447             {
8448               initializer = cp_parser_braced_list (parser, &non_constant_p);
8449               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8450               flags = 0;
8451             }
8452           else
8453             {
8454               /* Consume the `='.  */
8455               cp_parser_require (parser, CPP_EQ, RT_EQ);
8456               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8457             }
8458           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8459             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8460
8461           /* Process the initializer.  */
8462           cp_finish_decl (decl,
8463                           initializer, !non_constant_p,
8464                           asm_specification,
8465                           flags);
8466
8467           if (pushed_scope)
8468             pop_scope (pushed_scope);
8469
8470           return convert_from_reference (decl);
8471         }
8472     }
8473   /* If we didn't even get past the declarator successfully, we are
8474      definitely not looking at a declaration.  */
8475   else
8476     cp_parser_abort_tentative_parse (parser);
8477
8478   /* Otherwise, we are looking at an expression.  */
8479   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8480 }
8481
8482 /* Parses a for-statement or range-for-statement until the closing ')',
8483    not included. */
8484
8485 static tree
8486 cp_parser_for (cp_parser *parser)
8487 {
8488   tree init, scope, decl;
8489   bool is_range_for;
8490
8491   /* Begin the for-statement.  */
8492   scope = begin_for_scope (&init);
8493
8494   /* Parse the initialization.  */
8495   is_range_for = cp_parser_for_init_statement (parser, &decl);
8496
8497   if (is_range_for)
8498     return cp_parser_range_for (parser, scope, init, decl);
8499   else
8500     return cp_parser_c_for (parser, scope, init);
8501 }
8502
8503 static tree
8504 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8505 {
8506   /* Normal for loop */
8507   tree condition = NULL_TREE;
8508   tree expression = NULL_TREE;
8509   tree stmt;
8510
8511   stmt = begin_for_stmt (scope, init);
8512   /* The for-init-statement has already been parsed in
8513      cp_parser_for_init_statement, so no work is needed here.  */
8514   finish_for_init_stmt (stmt);
8515
8516   /* If there's a condition, process it.  */
8517   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8518     condition = cp_parser_condition (parser);
8519   finish_for_cond (condition, stmt);
8520   /* Look for the `;'.  */
8521   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8522
8523   /* If there's an expression, process it.  */
8524   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8525     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8526   finish_for_expr (expression, stmt);
8527
8528   return stmt;
8529 }
8530
8531 /* Tries to parse a range-based for-statement:
8532
8533   range-based-for:
8534     decl-specifier-seq declarator : expression
8535
8536   The decl-specifier-seq declarator and the `:' are already parsed by
8537   cp_parser_for_init_statement. If processing_template_decl it returns a
8538   newly created RANGE_FOR_STMT; if not, it is converted to a
8539   regular FOR_STMT.  */
8540
8541 static tree
8542 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8543 {
8544   tree stmt, range_expr;
8545
8546   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8547     {
8548       bool expr_non_constant_p;
8549       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8550     }
8551   else
8552     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8553
8554   /* If in template, STMT is converted to a normal for-statement
8555      at instantiation. If not, it is done just ahead. */
8556   if (processing_template_decl)
8557     {
8558       stmt = begin_range_for_stmt (scope, init);
8559       finish_range_for_decl (stmt, range_decl, range_expr);
8560     }
8561   else
8562     {
8563       stmt = begin_for_stmt (scope, init);
8564       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8565     }
8566   return stmt;
8567 }
8568
8569 /* Converts a range-based for-statement into a normal
8570    for-statement, as per the definition.
8571
8572       for (RANGE_DECL : RANGE_EXPR)
8573         BLOCK
8574
8575    should be equivalent to:
8576
8577       {
8578         auto &&__range = RANGE_EXPR;
8579         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8580               __begin != __end;
8581               ++__begin)
8582           {
8583               RANGE_DECL = *__begin;
8584               BLOCK
8585           }
8586       }
8587
8588    If RANGE_EXPR is an array:
8589         BEGIN_EXPR = __range
8590         END_EXPR = __range + ARRAY_SIZE(__range)
8591    Else if RANGE_EXPR has a member 'begin' or 'end':
8592         BEGIN_EXPR = __range.begin()
8593         END_EXPR = __range.end()
8594    Else:
8595         BEGIN_EXPR = begin(__range)
8596         END_EXPR = end(__range);
8597
8598    If __range has a member 'begin' but not 'end', or vice versa, we must
8599    still use the second alternative (it will surely fail, however).
8600    When calling begin()/end() in the third alternative we must use
8601    argument dependent lookup, but always considering 'std' as an associated
8602    namespace.  */
8603
8604 tree
8605 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8606 {
8607   tree range_type, range_temp;
8608   tree begin, end;
8609   tree iter_type, begin_expr, end_expr;
8610   tree condition, expression;
8611
8612   if (range_decl == error_mark_node || range_expr == error_mark_node)
8613     /* If an error happened previously do nothing or else a lot of
8614        unhelpful errors would be issued.  */
8615     begin_expr = end_expr = iter_type = error_mark_node;
8616   else
8617     {
8618       /* Find out the type deduced by the declaration
8619          `auto &&__range = range_expr'.  */
8620       range_type = cp_build_reference_type (make_auto (), true);
8621       range_type = do_auto_deduction (range_type, range_expr,
8622                                       type_uses_auto (range_type));
8623
8624       /* Create the __range variable.  */
8625       range_temp = build_decl (input_location, VAR_DECL,
8626                                get_identifier ("__for_range"), range_type);
8627       TREE_USED (range_temp) = 1;
8628       DECL_ARTIFICIAL (range_temp) = 1;
8629       pushdecl (range_temp);
8630       cp_finish_decl (range_temp, range_expr,
8631                       /*is_constant_init*/false, NULL_TREE,
8632                       LOOKUP_ONLYCONVERTING);
8633
8634       range_temp = convert_from_reference (range_temp);
8635       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8636                                                       &begin_expr, &end_expr);
8637     }
8638
8639   /* The new for initialization statement.  */
8640   begin = build_decl (input_location, VAR_DECL,
8641                       get_identifier ("__for_begin"), iter_type);
8642   TREE_USED (begin) = 1;
8643   DECL_ARTIFICIAL (begin) = 1;
8644   pushdecl (begin);
8645   cp_finish_decl (begin, begin_expr,
8646                   /*is_constant_init*/false, NULL_TREE,
8647                   LOOKUP_ONLYCONVERTING);
8648
8649   end = build_decl (input_location, VAR_DECL,
8650                     get_identifier ("__for_end"), iter_type);
8651   TREE_USED (end) = 1;
8652   DECL_ARTIFICIAL (end) = 1;
8653   pushdecl (end);
8654   cp_finish_decl (end, end_expr,
8655                   /*is_constant_init*/false, NULL_TREE,
8656                   LOOKUP_ONLYCONVERTING);
8657
8658   finish_for_init_stmt (statement);
8659
8660   /* The new for condition.  */
8661   condition = build_x_binary_op (NE_EXPR,
8662                                  begin, ERROR_MARK,
8663                                  end, ERROR_MARK,
8664                                  NULL, tf_warning_or_error);
8665   finish_for_cond (condition, statement);
8666
8667   /* The new increment expression.  */
8668   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8669   finish_for_expr (expression, statement);
8670
8671   /* The declaration is initialized with *__begin inside the loop body.  */
8672   cp_finish_decl (range_decl,
8673                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8674                   /*is_constant_init*/false, NULL_TREE,
8675                   LOOKUP_ONLYCONVERTING);
8676
8677   return statement;
8678 }
8679
8680 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8681    We need to solve both at the same time because the method used
8682    depends on the existence of members begin or end.
8683    Returns the type deduced for the iterator expression.  */
8684
8685 static tree
8686 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8687 {
8688   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8689     {
8690       error ("range-based %<for%> expression of type %qT "
8691              "has incomplete type", TREE_TYPE (range));
8692       *begin = *end = error_mark_node;
8693       return error_mark_node;
8694     }
8695   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8696     {
8697       /* If RANGE is an array, we will use pointer arithmetic.  */
8698       *begin = range;
8699       *end = build_binary_op (input_location, PLUS_EXPR,
8700                               range,
8701                               array_type_nelts_top (TREE_TYPE (range)),
8702                               0);
8703       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8704     }
8705   else
8706     {
8707       /* If it is not an array, we must do a bit of magic.  */
8708       tree id_begin, id_end;
8709       tree member_begin, member_end;
8710
8711       *begin = *end = error_mark_node;
8712
8713       id_begin = get_identifier ("begin");
8714       id_end = get_identifier ("end");
8715       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8716                                     /*protect=*/2, /*want_type=*/false);
8717       member_end = lookup_member (TREE_TYPE (range), id_end,
8718                                   /*protect=*/2, /*want_type=*/false);
8719
8720       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8721         {
8722           /* Use the member functions.  */
8723           if (member_begin != NULL_TREE)
8724             *begin = cp_parser_range_for_member_function (range, id_begin);
8725           else
8726             error ("range-based %<for%> expression of type %qT has an "
8727                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8728
8729           if (member_end != NULL_TREE)
8730             *end = cp_parser_range_for_member_function (range, id_end);
8731           else
8732             error ("range-based %<for%> expression of type %qT has a "
8733                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8734         }
8735       else
8736         {
8737           /* Use global functions with ADL.  */
8738           VEC(tree,gc) *vec;
8739           vec = make_tree_vector ();
8740
8741           VEC_safe_push (tree, gc, vec, range);
8742
8743           member_begin = perform_koenig_lookup (id_begin, vec,
8744                                                 /*include_std=*/true);
8745           *begin = finish_call_expr (member_begin, &vec, false, true,
8746                                      tf_warning_or_error);
8747           member_end = perform_koenig_lookup (id_end, vec,
8748                                               /*include_std=*/true);
8749           *end = finish_call_expr (member_end, &vec, false, true,
8750                                    tf_warning_or_error);
8751
8752           release_tree_vector (vec);
8753         }
8754
8755       /* Last common checks.  */
8756       if (*begin == error_mark_node || *end == error_mark_node)
8757         {
8758           /* If one of the expressions is an error do no more checks.  */
8759           *begin = *end = error_mark_node;
8760           return error_mark_node;
8761         }
8762       else
8763         {
8764           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8765           /* The unqualified type of the __begin and __end temporaries should
8766              be the same, as required by the multiple auto declaration.  */
8767           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8768             error ("inconsistent begin/end types in range-based %<for%> "
8769                    "statement: %qT and %qT",
8770                    TREE_TYPE (*begin), TREE_TYPE (*end));
8771           return iter_type;
8772         }
8773     }
8774 }
8775
8776 /* Helper function for cp_parser_perform_range_for_lookup.
8777    Builds a tree for RANGE.IDENTIFIER().  */
8778
8779 static tree
8780 cp_parser_range_for_member_function (tree range, tree identifier)
8781 {
8782   tree member, res;
8783   VEC(tree,gc) *vec;
8784
8785   member = finish_class_member_access_expr (range, identifier,
8786                                             false, tf_warning_or_error);
8787   if (member == error_mark_node)
8788     return error_mark_node;
8789
8790   vec = make_tree_vector ();
8791   res = finish_call_expr (member, &vec,
8792                           /*disallow_virtual=*/false,
8793                           /*koenig_p=*/false,
8794                           tf_warning_or_error);
8795   release_tree_vector (vec);
8796   return res;
8797 }
8798
8799 /* Parse an iteration-statement.
8800
8801    iteration-statement:
8802      while ( condition ) statement
8803      do statement while ( expression ) ;
8804      for ( for-init-statement condition [opt] ; expression [opt] )
8805        statement
8806
8807    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8808
8809 static tree
8810 cp_parser_iteration_statement (cp_parser* parser)
8811 {
8812   cp_token *token;
8813   enum rid keyword;
8814   tree statement;
8815   unsigned char in_statement;
8816
8817   /* Peek at the next token.  */
8818   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8819   if (!token)
8820     return error_mark_node;
8821
8822   /* Remember whether or not we are already within an iteration
8823      statement.  */
8824   in_statement = parser->in_statement;
8825
8826   /* See what kind of keyword it is.  */
8827   keyword = token->keyword;
8828   switch (keyword)
8829     {
8830     case RID_WHILE:
8831       {
8832         tree condition;
8833
8834         /* Begin the while-statement.  */
8835         statement = begin_while_stmt ();
8836         /* Look for the `('.  */
8837         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8838         /* Parse the condition.  */
8839         condition = cp_parser_condition (parser);
8840         finish_while_stmt_cond (condition, statement);
8841         /* Look for the `)'.  */
8842         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8843         /* Parse the dependent statement.  */
8844         parser->in_statement = IN_ITERATION_STMT;
8845         cp_parser_already_scoped_statement (parser);
8846         parser->in_statement = in_statement;
8847         /* We're done with the while-statement.  */
8848         finish_while_stmt (statement);
8849       }
8850       break;
8851
8852     case RID_DO:
8853       {
8854         tree expression;
8855
8856         /* Begin the do-statement.  */
8857         statement = begin_do_stmt ();
8858         /* Parse the body of the do-statement.  */
8859         parser->in_statement = IN_ITERATION_STMT;
8860         cp_parser_implicitly_scoped_statement (parser, NULL);
8861         parser->in_statement = in_statement;
8862         finish_do_body (statement);
8863         /* Look for the `while' keyword.  */
8864         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8865         /* Look for the `('.  */
8866         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8867         /* Parse the expression.  */
8868         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8869         /* We're done with the do-statement.  */
8870         finish_do_stmt (expression, statement);
8871         /* Look for the `)'.  */
8872         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8873         /* Look for the `;'.  */
8874         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8875       }
8876       break;
8877
8878     case RID_FOR:
8879       {
8880         /* Look for the `('.  */
8881         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8882
8883         statement = cp_parser_for (parser);
8884
8885         /* Look for the `)'.  */
8886         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8887
8888         /* Parse the body of the for-statement.  */
8889         parser->in_statement = IN_ITERATION_STMT;
8890         cp_parser_already_scoped_statement (parser);
8891         parser->in_statement = in_statement;
8892
8893         /* We're done with the for-statement.  */
8894         finish_for_stmt (statement);
8895       }
8896       break;
8897
8898     default:
8899       cp_parser_error (parser, "expected iteration-statement");
8900       statement = error_mark_node;
8901       break;
8902     }
8903
8904   return statement;
8905 }
8906
8907 /* Parse a for-init-statement or the declarator of a range-based-for.
8908    Returns true if a range-based-for declaration is seen.
8909
8910    for-init-statement:
8911      expression-statement
8912      simple-declaration  */
8913
8914 static bool
8915 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8916 {
8917   /* If the next token is a `;', then we have an empty
8918      expression-statement.  Grammatically, this is also a
8919      simple-declaration, but an invalid one, because it does not
8920      declare anything.  Therefore, if we did not handle this case
8921      specially, we would issue an error message about an invalid
8922      declaration.  */
8923   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8924     {
8925       bool is_range_for = false;
8926       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8927
8928       parser->colon_corrects_to_scope_p = false;
8929
8930       /* We're going to speculatively look for a declaration, falling back
8931          to an expression, if necessary.  */
8932       cp_parser_parse_tentatively (parser);
8933       /* Parse the declaration.  */
8934       cp_parser_simple_declaration (parser,
8935                                     /*function_definition_allowed_p=*/false,
8936                                     decl);
8937       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8938       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8939         {
8940           /* It is a range-for, consume the ':' */
8941           cp_lexer_consume_token (parser->lexer);
8942           is_range_for = true;
8943           if (cxx_dialect < cxx0x)
8944             {
8945               error_at (cp_lexer_peek_token (parser->lexer)->location,
8946                         "range-based %<for%> loops are not allowed "
8947                         "in C++98 mode");
8948               *decl = error_mark_node;
8949             }
8950         }
8951       else
8952           /* The ';' is not consumed yet because we told
8953              cp_parser_simple_declaration not to.  */
8954           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8955
8956       if (cp_parser_parse_definitely (parser))
8957         return is_range_for;
8958       /* If the tentative parse failed, then we shall need to look for an
8959          expression-statement.  */
8960     }
8961   /* If we are here, it is an expression-statement.  */
8962   cp_parser_expression_statement (parser, NULL_TREE);
8963   return false;
8964 }
8965
8966 /* Parse a jump-statement.
8967
8968    jump-statement:
8969      break ;
8970      continue ;
8971      return expression [opt] ;
8972      return braced-init-list ;
8973      goto identifier ;
8974
8975    GNU extension:
8976
8977    jump-statement:
8978      goto * expression ;
8979
8980    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8981
8982 static tree
8983 cp_parser_jump_statement (cp_parser* parser)
8984 {
8985   tree statement = error_mark_node;
8986   cp_token *token;
8987   enum rid keyword;
8988   unsigned char in_statement;
8989
8990   /* Peek at the next token.  */
8991   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8992   if (!token)
8993     return error_mark_node;
8994
8995   /* See what kind of keyword it is.  */
8996   keyword = token->keyword;
8997   switch (keyword)
8998     {
8999     case RID_BREAK:
9000       in_statement = parser->in_statement & ~IN_IF_STMT;      
9001       switch (in_statement)
9002         {
9003         case 0:
9004           error_at (token->location, "break statement not within loop or switch");
9005           break;
9006         default:
9007           gcc_assert ((in_statement & IN_SWITCH_STMT)
9008                       || in_statement == IN_ITERATION_STMT);
9009           statement = finish_break_stmt ();
9010           break;
9011         case IN_OMP_BLOCK:
9012           error_at (token->location, "invalid exit from OpenMP structured block");
9013           break;
9014         case IN_OMP_FOR:
9015           error_at (token->location, "break statement used with OpenMP for loop");
9016           break;
9017         }
9018       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9019       break;
9020
9021     case RID_CONTINUE:
9022       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9023         {
9024         case 0:
9025           error_at (token->location, "continue statement not within a loop");
9026           break;
9027         case IN_ITERATION_STMT:
9028         case IN_OMP_FOR:
9029           statement = finish_continue_stmt ();
9030           break;
9031         case IN_OMP_BLOCK:
9032           error_at (token->location, "invalid exit from OpenMP structured block");
9033           break;
9034         default:
9035           gcc_unreachable ();
9036         }
9037       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9038       break;
9039
9040     case RID_RETURN:
9041       {
9042         tree expr;
9043         bool expr_non_constant_p;
9044
9045         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9046           {
9047             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9048             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9049           }
9050         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9051           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9052         else
9053           /* If the next token is a `;', then there is no
9054              expression.  */
9055           expr = NULL_TREE;
9056         /* Build the return-statement.  */
9057         statement = finish_return_stmt (expr);
9058         /* Look for the final `;'.  */
9059         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9060       }
9061       break;
9062
9063     case RID_GOTO:
9064       /* Create the goto-statement.  */
9065       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9066         {
9067           /* Issue a warning about this use of a GNU extension.  */
9068           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9069           /* Consume the '*' token.  */
9070           cp_lexer_consume_token (parser->lexer);
9071           /* Parse the dependent expression.  */
9072           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9073         }
9074       else
9075         finish_goto_stmt (cp_parser_identifier (parser));
9076       /* Look for the final `;'.  */
9077       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9078       break;
9079
9080     default:
9081       cp_parser_error (parser, "expected jump-statement");
9082       break;
9083     }
9084
9085   return statement;
9086 }
9087
9088 /* Parse a declaration-statement.
9089
9090    declaration-statement:
9091      block-declaration  */
9092
9093 static void
9094 cp_parser_declaration_statement (cp_parser* parser)
9095 {
9096   void *p;
9097
9098   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9099   p = obstack_alloc (&declarator_obstack, 0);
9100
9101  /* Parse the block-declaration.  */
9102   cp_parser_block_declaration (parser, /*statement_p=*/true);
9103
9104   /* Free any declarators allocated.  */
9105   obstack_free (&declarator_obstack, p);
9106
9107   /* Finish off the statement.  */
9108   finish_stmt ();
9109 }
9110
9111 /* Some dependent statements (like `if (cond) statement'), are
9112    implicitly in their own scope.  In other words, if the statement is
9113    a single statement (as opposed to a compound-statement), it is
9114    none-the-less treated as if it were enclosed in braces.  Any
9115    declarations appearing in the dependent statement are out of scope
9116    after control passes that point.  This function parses a statement,
9117    but ensures that is in its own scope, even if it is not a
9118    compound-statement.
9119
9120    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9121    is a (possibly labeled) if statement which is not enclosed in
9122    braces and has an else clause.  This is used to implement
9123    -Wparentheses.
9124
9125    Returns the new statement.  */
9126
9127 static tree
9128 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9129 {
9130   tree statement;
9131
9132   if (if_p != NULL)
9133     *if_p = false;
9134
9135   /* Mark if () ; with a special NOP_EXPR.  */
9136   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9137     {
9138       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9139       cp_lexer_consume_token (parser->lexer);
9140       statement = add_stmt (build_empty_stmt (loc));
9141     }
9142   /* if a compound is opened, we simply parse the statement directly.  */
9143   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9144     statement = cp_parser_compound_statement (parser, NULL, false, false);
9145   /* If the token is not a `{', then we must take special action.  */
9146   else
9147     {
9148       /* Create a compound-statement.  */
9149       statement = begin_compound_stmt (0);
9150       /* Parse the dependent-statement.  */
9151       cp_parser_statement (parser, NULL_TREE, false, if_p);
9152       /* Finish the dummy compound-statement.  */
9153       finish_compound_stmt (statement);
9154     }
9155
9156   /* Return the statement.  */
9157   return statement;
9158 }
9159
9160 /* For some dependent statements (like `while (cond) statement'), we
9161    have already created a scope.  Therefore, even if the dependent
9162    statement is a compound-statement, we do not want to create another
9163    scope.  */
9164
9165 static void
9166 cp_parser_already_scoped_statement (cp_parser* parser)
9167 {
9168   /* If the token is a `{', then we must take special action.  */
9169   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9170     cp_parser_statement (parser, NULL_TREE, false, NULL);
9171   else
9172     {
9173       /* Avoid calling cp_parser_compound_statement, so that we
9174          don't create a new scope.  Do everything else by hand.  */
9175       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9176       /* If the next keyword is `__label__' we have a label declaration.  */
9177       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9178         cp_parser_label_declaration (parser);
9179       /* Parse an (optional) statement-seq.  */
9180       cp_parser_statement_seq_opt (parser, NULL_TREE);
9181       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9182     }
9183 }
9184
9185 /* Declarations [gram.dcl.dcl] */
9186
9187 /* Parse an optional declaration-sequence.
9188
9189    declaration-seq:
9190      declaration
9191      declaration-seq declaration  */
9192
9193 static void
9194 cp_parser_declaration_seq_opt (cp_parser* parser)
9195 {
9196   while (true)
9197     {
9198       cp_token *token;
9199
9200       token = cp_lexer_peek_token (parser->lexer);
9201
9202       if (token->type == CPP_CLOSE_BRACE
9203           || token->type == CPP_EOF
9204           || token->type == CPP_PRAGMA_EOL)
9205         break;
9206
9207       if (token->type == CPP_SEMICOLON)
9208         {
9209           /* A declaration consisting of a single semicolon is
9210              invalid.  Allow it unless we're being pedantic.  */
9211           cp_lexer_consume_token (parser->lexer);
9212           if (!in_system_header)
9213             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9214           continue;
9215         }
9216
9217       /* If we're entering or exiting a region that's implicitly
9218          extern "C", modify the lang context appropriately.  */
9219       if (!parser->implicit_extern_c && token->implicit_extern_c)
9220         {
9221           push_lang_context (lang_name_c);
9222           parser->implicit_extern_c = true;
9223         }
9224       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9225         {
9226           pop_lang_context ();
9227           parser->implicit_extern_c = false;
9228         }
9229
9230       if (token->type == CPP_PRAGMA)
9231         {
9232           /* A top-level declaration can consist solely of a #pragma.
9233              A nested declaration cannot, so this is done here and not
9234              in cp_parser_declaration.  (A #pragma at block scope is
9235              handled in cp_parser_statement.)  */
9236           cp_parser_pragma (parser, pragma_external);
9237           continue;
9238         }
9239
9240       /* Parse the declaration itself.  */
9241       cp_parser_declaration (parser);
9242     }
9243 }
9244
9245 /* Parse a declaration.
9246
9247    declaration:
9248      block-declaration
9249      function-definition
9250      template-declaration
9251      explicit-instantiation
9252      explicit-specialization
9253      linkage-specification
9254      namespace-definition
9255
9256    GNU extension:
9257
9258    declaration:
9259       __extension__ declaration */
9260
9261 static void
9262 cp_parser_declaration (cp_parser* parser)
9263 {
9264   cp_token token1;
9265   cp_token token2;
9266   int saved_pedantic;
9267   void *p;
9268   tree attributes = NULL_TREE;
9269
9270   /* Check for the `__extension__' keyword.  */
9271   if (cp_parser_extension_opt (parser, &saved_pedantic))
9272     {
9273       /* Parse the qualified declaration.  */
9274       cp_parser_declaration (parser);
9275       /* Restore the PEDANTIC flag.  */
9276       pedantic = saved_pedantic;
9277
9278       return;
9279     }
9280
9281   /* Try to figure out what kind of declaration is present.  */
9282   token1 = *cp_lexer_peek_token (parser->lexer);
9283
9284   if (token1.type != CPP_EOF)
9285     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9286   else
9287     {
9288       token2.type = CPP_EOF;
9289       token2.keyword = RID_MAX;
9290     }
9291
9292   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9293   p = obstack_alloc (&declarator_obstack, 0);
9294
9295   /* If the next token is `extern' and the following token is a string
9296      literal, then we have a linkage specification.  */
9297   if (token1.keyword == RID_EXTERN
9298       && cp_parser_is_string_literal (&token2))
9299     cp_parser_linkage_specification (parser);
9300   /* If the next token is `template', then we have either a template
9301      declaration, an explicit instantiation, or an explicit
9302      specialization.  */
9303   else if (token1.keyword == RID_TEMPLATE)
9304     {
9305       /* `template <>' indicates a template specialization.  */
9306       if (token2.type == CPP_LESS
9307           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9308         cp_parser_explicit_specialization (parser);
9309       /* `template <' indicates a template declaration.  */
9310       else if (token2.type == CPP_LESS)
9311         cp_parser_template_declaration (parser, /*member_p=*/false);
9312       /* Anything else must be an explicit instantiation.  */
9313       else
9314         cp_parser_explicit_instantiation (parser);
9315     }
9316   /* If the next token is `export', then we have a template
9317      declaration.  */
9318   else if (token1.keyword == RID_EXPORT)
9319     cp_parser_template_declaration (parser, /*member_p=*/false);
9320   /* If the next token is `extern', 'static' or 'inline' and the one
9321      after that is `template', we have a GNU extended explicit
9322      instantiation directive.  */
9323   else if (cp_parser_allow_gnu_extensions_p (parser)
9324            && (token1.keyword == RID_EXTERN
9325                || token1.keyword == RID_STATIC
9326                || token1.keyword == RID_INLINE)
9327            && token2.keyword == RID_TEMPLATE)
9328     cp_parser_explicit_instantiation (parser);
9329   /* If the next token is `namespace', check for a named or unnamed
9330      namespace definition.  */
9331   else if (token1.keyword == RID_NAMESPACE
9332            && (/* A named namespace definition.  */
9333                (token2.type == CPP_NAME
9334                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9335                     != CPP_EQ))
9336                /* An unnamed namespace definition.  */
9337                || token2.type == CPP_OPEN_BRACE
9338                || token2.keyword == RID_ATTRIBUTE))
9339     cp_parser_namespace_definition (parser);
9340   /* An inline (associated) namespace definition.  */
9341   else if (token1.keyword == RID_INLINE
9342            && token2.keyword == RID_NAMESPACE)
9343     cp_parser_namespace_definition (parser);
9344   /* Objective-C++ declaration/definition.  */
9345   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9346     cp_parser_objc_declaration (parser, NULL_TREE);
9347   else if (c_dialect_objc ()
9348            && token1.keyword == RID_ATTRIBUTE
9349            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9350     cp_parser_objc_declaration (parser, attributes);
9351   /* We must have either a block declaration or a function
9352      definition.  */
9353   else
9354     /* Try to parse a block-declaration, or a function-definition.  */
9355     cp_parser_block_declaration (parser, /*statement_p=*/false);
9356
9357   /* Free any declarators allocated.  */
9358   obstack_free (&declarator_obstack, p);
9359 }
9360
9361 /* Parse a block-declaration.
9362
9363    block-declaration:
9364      simple-declaration
9365      asm-definition
9366      namespace-alias-definition
9367      using-declaration
9368      using-directive
9369
9370    GNU Extension:
9371
9372    block-declaration:
9373      __extension__ block-declaration
9374
9375    C++0x Extension:
9376
9377    block-declaration:
9378      static_assert-declaration
9379
9380    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9381    part of a declaration-statement.  */
9382
9383 static void
9384 cp_parser_block_declaration (cp_parser *parser,
9385                              bool      statement_p)
9386 {
9387   cp_token *token1;
9388   int saved_pedantic;
9389
9390   /* Check for the `__extension__' keyword.  */
9391   if (cp_parser_extension_opt (parser, &saved_pedantic))
9392     {
9393       /* Parse the qualified declaration.  */
9394       cp_parser_block_declaration (parser, statement_p);
9395       /* Restore the PEDANTIC flag.  */
9396       pedantic = saved_pedantic;
9397
9398       return;
9399     }
9400
9401   /* Peek at the next token to figure out which kind of declaration is
9402      present.  */
9403   token1 = cp_lexer_peek_token (parser->lexer);
9404
9405   /* If the next keyword is `asm', we have an asm-definition.  */
9406   if (token1->keyword == RID_ASM)
9407     {
9408       if (statement_p)
9409         cp_parser_commit_to_tentative_parse (parser);
9410       cp_parser_asm_definition (parser);
9411     }
9412   /* If the next keyword is `namespace', we have a
9413      namespace-alias-definition.  */
9414   else if (token1->keyword == RID_NAMESPACE)
9415     cp_parser_namespace_alias_definition (parser);
9416   /* If the next keyword is `using', we have either a
9417      using-declaration or a using-directive.  */
9418   else if (token1->keyword == RID_USING)
9419     {
9420       cp_token *token2;
9421
9422       if (statement_p)
9423         cp_parser_commit_to_tentative_parse (parser);
9424       /* If the token after `using' is `namespace', then we have a
9425          using-directive.  */
9426       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9427       if (token2->keyword == RID_NAMESPACE)
9428         cp_parser_using_directive (parser);
9429       /* Otherwise, it's a using-declaration.  */
9430       else
9431         cp_parser_using_declaration (parser,
9432                                      /*access_declaration_p=*/false);
9433     }
9434   /* If the next keyword is `__label__' we have a misplaced label
9435      declaration.  */
9436   else if (token1->keyword == RID_LABEL)
9437     {
9438       cp_lexer_consume_token (parser->lexer);
9439       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9440       cp_parser_skip_to_end_of_statement (parser);
9441       /* If the next token is now a `;', consume it.  */
9442       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9443         cp_lexer_consume_token (parser->lexer);
9444     }
9445   /* If the next token is `static_assert' we have a static assertion.  */
9446   else if (token1->keyword == RID_STATIC_ASSERT)
9447     cp_parser_static_assert (parser, /*member_p=*/false);
9448   /* Anything else must be a simple-declaration.  */
9449   else
9450     cp_parser_simple_declaration (parser, !statement_p,
9451                                   /*maybe_range_for_decl*/NULL);
9452 }
9453
9454 /* Parse a simple-declaration.
9455
9456    simple-declaration:
9457      decl-specifier-seq [opt] init-declarator-list [opt] ;
9458
9459    init-declarator-list:
9460      init-declarator
9461      init-declarator-list , init-declarator
9462
9463    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9464    function-definition as a simple-declaration.
9465
9466    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9467    parsed declaration if it is an uninitialized single declarator not followed
9468    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9469    if present, will not be consumed.  */
9470
9471 static void
9472 cp_parser_simple_declaration (cp_parser* parser,
9473                               bool function_definition_allowed_p,
9474                               tree *maybe_range_for_decl)
9475 {
9476   cp_decl_specifier_seq decl_specifiers;
9477   int declares_class_or_enum;
9478   bool saw_declarator;
9479
9480   if (maybe_range_for_decl)
9481     *maybe_range_for_decl = NULL_TREE;
9482
9483   /* Defer access checks until we know what is being declared; the
9484      checks for names appearing in the decl-specifier-seq should be
9485      done as if we were in the scope of the thing being declared.  */
9486   push_deferring_access_checks (dk_deferred);
9487
9488   /* Parse the decl-specifier-seq.  We have to keep track of whether
9489      or not the decl-specifier-seq declares a named class or
9490      enumeration type, since that is the only case in which the
9491      init-declarator-list is allowed to be empty.
9492
9493      [dcl.dcl]
9494
9495      In a simple-declaration, the optional init-declarator-list can be
9496      omitted only when declaring a class or enumeration, that is when
9497      the decl-specifier-seq contains either a class-specifier, an
9498      elaborated-type-specifier, or an enum-specifier.  */
9499   cp_parser_decl_specifier_seq (parser,
9500                                 CP_PARSER_FLAGS_OPTIONAL,
9501                                 &decl_specifiers,
9502                                 &declares_class_or_enum);
9503   /* We no longer need to defer access checks.  */
9504   stop_deferring_access_checks ();
9505
9506   /* In a block scope, a valid declaration must always have a
9507      decl-specifier-seq.  By not trying to parse declarators, we can
9508      resolve the declaration/expression ambiguity more quickly.  */
9509   if (!function_definition_allowed_p
9510       && !decl_specifiers.any_specifiers_p)
9511     {
9512       cp_parser_error (parser, "expected declaration");
9513       goto done;
9514     }
9515
9516   /* If the next two tokens are both identifiers, the code is
9517      erroneous. The usual cause of this situation is code like:
9518
9519        T t;
9520
9521      where "T" should name a type -- but does not.  */
9522   if (!decl_specifiers.any_type_specifiers_p
9523       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9524     {
9525       /* If parsing tentatively, we should commit; we really are
9526          looking at a declaration.  */
9527       cp_parser_commit_to_tentative_parse (parser);
9528       /* Give up.  */
9529       goto done;
9530     }
9531
9532   /* If we have seen at least one decl-specifier, and the next token
9533      is not a parenthesis, then we must be looking at a declaration.
9534      (After "int (" we might be looking at a functional cast.)  */
9535   if (decl_specifiers.any_specifiers_p
9536       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9537       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9538       && !cp_parser_error_occurred (parser))
9539     cp_parser_commit_to_tentative_parse (parser);
9540
9541   /* Keep going until we hit the `;' at the end of the simple
9542      declaration.  */
9543   saw_declarator = false;
9544   while (cp_lexer_next_token_is_not (parser->lexer,
9545                                      CPP_SEMICOLON))
9546     {
9547       cp_token *token;
9548       bool function_definition_p;
9549       tree decl;
9550
9551       if (saw_declarator)
9552         {
9553           /* If we are processing next declarator, coma is expected */
9554           token = cp_lexer_peek_token (parser->lexer);
9555           gcc_assert (token->type == CPP_COMMA);
9556           cp_lexer_consume_token (parser->lexer);
9557           if (maybe_range_for_decl)
9558             *maybe_range_for_decl = error_mark_node;
9559         }
9560       else
9561         saw_declarator = true;
9562
9563       /* Parse the init-declarator.  */
9564       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9565                                         /*checks=*/NULL,
9566                                         function_definition_allowed_p,
9567                                         /*member_p=*/false,
9568                                         declares_class_or_enum,
9569                                         &function_definition_p,
9570                                         maybe_range_for_decl);
9571       /* If an error occurred while parsing tentatively, exit quickly.
9572          (That usually happens when in the body of a function; each
9573          statement is treated as a declaration-statement until proven
9574          otherwise.)  */
9575       if (cp_parser_error_occurred (parser))
9576         goto done;
9577       /* Handle function definitions specially.  */
9578       if (function_definition_p)
9579         {
9580           /* If the next token is a `,', then we are probably
9581              processing something like:
9582
9583                void f() {}, *p;
9584
9585              which is erroneous.  */
9586           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9587             {
9588               cp_token *token = cp_lexer_peek_token (parser->lexer);
9589               error_at (token->location,
9590                         "mixing"
9591                         " declarations and function-definitions is forbidden");
9592             }
9593           /* Otherwise, we're done with the list of declarators.  */
9594           else
9595             {
9596               pop_deferring_access_checks ();
9597               return;
9598             }
9599         }
9600       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9601         *maybe_range_for_decl = decl;
9602       /* The next token should be either a `,' or a `;'.  */
9603       token = cp_lexer_peek_token (parser->lexer);
9604       /* If it's a `,', there are more declarators to come.  */
9605       if (token->type == CPP_COMMA)
9606         /* will be consumed next time around */;
9607       /* If it's a `;', we are done.  */
9608       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9609         break;
9610       /* Anything else is an error.  */
9611       else
9612         {
9613           /* If we have already issued an error message we don't need
9614              to issue another one.  */
9615           if (decl != error_mark_node
9616               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9617             cp_parser_error (parser, "expected %<,%> or %<;%>");
9618           /* Skip tokens until we reach the end of the statement.  */
9619           cp_parser_skip_to_end_of_statement (parser);
9620           /* If the next token is now a `;', consume it.  */
9621           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9622             cp_lexer_consume_token (parser->lexer);
9623           goto done;
9624         }
9625       /* After the first time around, a function-definition is not
9626          allowed -- even if it was OK at first.  For example:
9627
9628            int i, f() {}
9629
9630          is not valid.  */
9631       function_definition_allowed_p = false;
9632     }
9633
9634   /* Issue an error message if no declarators are present, and the
9635      decl-specifier-seq does not itself declare a class or
9636      enumeration.  */
9637   if (!saw_declarator)
9638     {
9639       if (cp_parser_declares_only_class_p (parser))
9640         shadow_tag (&decl_specifiers);
9641       /* Perform any deferred access checks.  */
9642       perform_deferred_access_checks ();
9643     }
9644
9645   /* Consume the `;'.  */
9646   if (!maybe_range_for_decl)
9647       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9648
9649  done:
9650   pop_deferring_access_checks ();
9651 }
9652
9653 /* Parse a decl-specifier-seq.
9654
9655    decl-specifier-seq:
9656      decl-specifier-seq [opt] decl-specifier
9657
9658    decl-specifier:
9659      storage-class-specifier
9660      type-specifier
9661      function-specifier
9662      friend
9663      typedef
9664
9665    GNU Extension:
9666
9667    decl-specifier:
9668      attributes
9669
9670    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9671
9672    The parser flags FLAGS is used to control type-specifier parsing.
9673
9674    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9675    flags:
9676
9677      1: one of the decl-specifiers is an elaborated-type-specifier
9678         (i.e., a type declaration)
9679      2: one of the decl-specifiers is an enum-specifier or a
9680         class-specifier (i.e., a type definition)
9681
9682    */
9683
9684 static void
9685 cp_parser_decl_specifier_seq (cp_parser* parser,
9686                               cp_parser_flags flags,
9687                               cp_decl_specifier_seq *decl_specs,
9688                               int* declares_class_or_enum)
9689 {
9690   bool constructor_possible_p = !parser->in_declarator_p;
9691   cp_token *start_token = NULL;
9692
9693   /* Clear DECL_SPECS.  */
9694   clear_decl_specs (decl_specs);
9695
9696   /* Assume no class or enumeration type is declared.  */
9697   *declares_class_or_enum = 0;
9698
9699   /* Keep reading specifiers until there are no more to read.  */
9700   while (true)
9701     {
9702       bool constructor_p;
9703       bool found_decl_spec;
9704       cp_token *token;
9705
9706       /* Peek at the next token.  */
9707       token = cp_lexer_peek_token (parser->lexer);
9708
9709       /* Save the first token of the decl spec list for error
9710          reporting.  */
9711       if (!start_token)
9712         start_token = token;
9713       /* Handle attributes.  */
9714       if (token->keyword == RID_ATTRIBUTE)
9715         {
9716           /* Parse the attributes.  */
9717           decl_specs->attributes
9718             = chainon (decl_specs->attributes,
9719                        cp_parser_attributes_opt (parser));
9720           continue;
9721         }
9722       /* Assume we will find a decl-specifier keyword.  */
9723       found_decl_spec = true;
9724       /* If the next token is an appropriate keyword, we can simply
9725          add it to the list.  */
9726       switch (token->keyword)
9727         {
9728           /* decl-specifier:
9729                friend
9730                constexpr */
9731         case RID_FRIEND:
9732           if (!at_class_scope_p ())
9733             {
9734               error_at (token->location, "%<friend%> used outside of class");
9735               cp_lexer_purge_token (parser->lexer);
9736             }
9737           else
9738             {
9739               ++decl_specs->specs[(int) ds_friend];
9740               /* Consume the token.  */
9741               cp_lexer_consume_token (parser->lexer);
9742             }
9743           break;
9744
9745         case RID_CONSTEXPR:
9746           ++decl_specs->specs[(int) ds_constexpr];
9747           cp_lexer_consume_token (parser->lexer);
9748           break;
9749
9750           /* function-specifier:
9751                inline
9752                virtual
9753                explicit  */
9754         case RID_INLINE:
9755         case RID_VIRTUAL:
9756         case RID_EXPLICIT:
9757           cp_parser_function_specifier_opt (parser, decl_specs);
9758           break;
9759
9760           /* decl-specifier:
9761                typedef  */
9762         case RID_TYPEDEF:
9763           ++decl_specs->specs[(int) ds_typedef];
9764           /* Consume the token.  */
9765           cp_lexer_consume_token (parser->lexer);
9766           /* A constructor declarator cannot appear in a typedef.  */
9767           constructor_possible_p = false;
9768           /* The "typedef" keyword can only occur in a declaration; we
9769              may as well commit at this point.  */
9770           cp_parser_commit_to_tentative_parse (parser);
9771
9772           if (decl_specs->storage_class != sc_none)
9773             decl_specs->conflicting_specifiers_p = true;
9774           break;
9775
9776           /* storage-class-specifier:
9777                auto
9778                register
9779                static
9780                extern
9781                mutable
9782
9783              GNU Extension:
9784                thread  */
9785         case RID_AUTO:
9786           if (cxx_dialect == cxx98) 
9787             {
9788               /* Consume the token.  */
9789               cp_lexer_consume_token (parser->lexer);
9790
9791               /* Complain about `auto' as a storage specifier, if
9792                  we're complaining about C++0x compatibility.  */
9793               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9794                           " will change meaning in C++0x; please remove it");
9795
9796               /* Set the storage class anyway.  */
9797               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9798                                            token->location);
9799             }
9800           else
9801             /* C++0x auto type-specifier.  */
9802             found_decl_spec = false;
9803           break;
9804
9805         case RID_REGISTER:
9806         case RID_STATIC:
9807         case RID_EXTERN:
9808         case RID_MUTABLE:
9809           /* Consume the token.  */
9810           cp_lexer_consume_token (parser->lexer);
9811           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9812                                        token->location);
9813           break;
9814         case RID_THREAD:
9815           /* Consume the token.  */
9816           cp_lexer_consume_token (parser->lexer);
9817           ++decl_specs->specs[(int) ds_thread];
9818           break;
9819
9820         default:
9821           /* We did not yet find a decl-specifier yet.  */
9822           found_decl_spec = false;
9823           break;
9824         }
9825
9826       if (found_decl_spec
9827           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9828           && token->keyword != RID_CONSTEXPR)
9829         error ("decl-specifier invalid in condition");
9830
9831       /* Constructors are a special case.  The `S' in `S()' is not a
9832          decl-specifier; it is the beginning of the declarator.  */
9833       constructor_p
9834         = (!found_decl_spec
9835            && constructor_possible_p
9836            && (cp_parser_constructor_declarator_p
9837                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9838
9839       /* If we don't have a DECL_SPEC yet, then we must be looking at
9840          a type-specifier.  */
9841       if (!found_decl_spec && !constructor_p)
9842         {
9843           int decl_spec_declares_class_or_enum;
9844           bool is_cv_qualifier;
9845           tree type_spec;
9846
9847           type_spec
9848             = cp_parser_type_specifier (parser, flags,
9849                                         decl_specs,
9850                                         /*is_declaration=*/true,
9851                                         &decl_spec_declares_class_or_enum,
9852                                         &is_cv_qualifier);
9853           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9854
9855           /* If this type-specifier referenced a user-defined type
9856              (a typedef, class-name, etc.), then we can't allow any
9857              more such type-specifiers henceforth.
9858
9859              [dcl.spec]
9860
9861              The longest sequence of decl-specifiers that could
9862              possibly be a type name is taken as the
9863              decl-specifier-seq of a declaration.  The sequence shall
9864              be self-consistent as described below.
9865
9866              [dcl.type]
9867
9868              As a general rule, at most one type-specifier is allowed
9869              in the complete decl-specifier-seq of a declaration.  The
9870              only exceptions are the following:
9871
9872              -- const or volatile can be combined with any other
9873                 type-specifier.
9874
9875              -- signed or unsigned can be combined with char, long,
9876                 short, or int.
9877
9878              -- ..
9879
9880              Example:
9881
9882                typedef char* Pc;
9883                void g (const int Pc);
9884
9885              Here, Pc is *not* part of the decl-specifier seq; it's
9886              the declarator.  Therefore, once we see a type-specifier
9887              (other than a cv-qualifier), we forbid any additional
9888              user-defined types.  We *do* still allow things like `int
9889              int' to be considered a decl-specifier-seq, and issue the
9890              error message later.  */
9891           if (type_spec && !is_cv_qualifier)
9892             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9893           /* A constructor declarator cannot follow a type-specifier.  */
9894           if (type_spec)
9895             {
9896               constructor_possible_p = false;
9897               found_decl_spec = true;
9898               if (!is_cv_qualifier)
9899                 decl_specs->any_type_specifiers_p = true;
9900             }
9901         }
9902
9903       /* If we still do not have a DECL_SPEC, then there are no more
9904          decl-specifiers.  */
9905       if (!found_decl_spec)
9906         break;
9907
9908       decl_specs->any_specifiers_p = true;
9909       /* After we see one decl-specifier, further decl-specifiers are
9910          always optional.  */
9911       flags |= CP_PARSER_FLAGS_OPTIONAL;
9912     }
9913
9914   cp_parser_check_decl_spec (decl_specs, start_token->location);
9915
9916   /* Don't allow a friend specifier with a class definition.  */
9917   if (decl_specs->specs[(int) ds_friend] != 0
9918       && (*declares_class_or_enum & 2))
9919     error_at (start_token->location,
9920               "class definition may not be declared a friend");
9921 }
9922
9923 /* Parse an (optional) storage-class-specifier.
9924
9925    storage-class-specifier:
9926      auto
9927      register
9928      static
9929      extern
9930      mutable
9931
9932    GNU Extension:
9933
9934    storage-class-specifier:
9935      thread
9936
9937    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9938
9939 static tree
9940 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9941 {
9942   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9943     {
9944     case RID_AUTO:
9945       if (cxx_dialect != cxx98)
9946         return NULL_TREE;
9947       /* Fall through for C++98.  */
9948
9949     case RID_REGISTER:
9950     case RID_STATIC:
9951     case RID_EXTERN:
9952     case RID_MUTABLE:
9953     case RID_THREAD:
9954       /* Consume the token.  */
9955       return cp_lexer_consume_token (parser->lexer)->u.value;
9956
9957     default:
9958       return NULL_TREE;
9959     }
9960 }
9961
9962 /* Parse an (optional) function-specifier.
9963
9964    function-specifier:
9965      inline
9966      virtual
9967      explicit
9968
9969    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9970    Updates DECL_SPECS, if it is non-NULL.  */
9971
9972 static tree
9973 cp_parser_function_specifier_opt (cp_parser* parser,
9974                                   cp_decl_specifier_seq *decl_specs)
9975 {
9976   cp_token *token = cp_lexer_peek_token (parser->lexer);
9977   switch (token->keyword)
9978     {
9979     case RID_INLINE:
9980       if (decl_specs)
9981         ++decl_specs->specs[(int) ds_inline];
9982       break;
9983
9984     case RID_VIRTUAL:
9985       /* 14.5.2.3 [temp.mem]
9986
9987          A member function template shall not be virtual.  */
9988       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9989         error_at (token->location, "templates may not be %<virtual%>");
9990       else if (decl_specs)
9991         ++decl_specs->specs[(int) ds_virtual];
9992       break;
9993
9994     case RID_EXPLICIT:
9995       if (decl_specs)
9996         ++decl_specs->specs[(int) ds_explicit];
9997       break;
9998
9999     default:
10000       return NULL_TREE;
10001     }
10002
10003   /* Consume the token.  */
10004   return cp_lexer_consume_token (parser->lexer)->u.value;
10005 }
10006
10007 /* Parse a linkage-specification.
10008
10009    linkage-specification:
10010      extern string-literal { declaration-seq [opt] }
10011      extern string-literal declaration  */
10012
10013 static void
10014 cp_parser_linkage_specification (cp_parser* parser)
10015 {
10016   tree linkage;
10017
10018   /* Look for the `extern' keyword.  */
10019   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10020
10021   /* Look for the string-literal.  */
10022   linkage = cp_parser_string_literal (parser, false, false);
10023
10024   /* Transform the literal into an identifier.  If the literal is a
10025      wide-character string, or contains embedded NULs, then we can't
10026      handle it as the user wants.  */
10027   if (strlen (TREE_STRING_POINTER (linkage))
10028       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10029     {
10030       cp_parser_error (parser, "invalid linkage-specification");
10031       /* Assume C++ linkage.  */
10032       linkage = lang_name_cplusplus;
10033     }
10034   else
10035     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10036
10037   /* We're now using the new linkage.  */
10038   push_lang_context (linkage);
10039
10040   /* If the next token is a `{', then we're using the first
10041      production.  */
10042   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10043     {
10044       /* Consume the `{' token.  */
10045       cp_lexer_consume_token (parser->lexer);
10046       /* Parse the declarations.  */
10047       cp_parser_declaration_seq_opt (parser);
10048       /* Look for the closing `}'.  */
10049       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10050     }
10051   /* Otherwise, there's just one declaration.  */
10052   else
10053     {
10054       bool saved_in_unbraced_linkage_specification_p;
10055
10056       saved_in_unbraced_linkage_specification_p
10057         = parser->in_unbraced_linkage_specification_p;
10058       parser->in_unbraced_linkage_specification_p = true;
10059       cp_parser_declaration (parser);
10060       parser->in_unbraced_linkage_specification_p
10061         = saved_in_unbraced_linkage_specification_p;
10062     }
10063
10064   /* We're done with the linkage-specification.  */
10065   pop_lang_context ();
10066 }
10067
10068 /* Parse a static_assert-declaration.
10069
10070    static_assert-declaration:
10071      static_assert ( constant-expression , string-literal ) ; 
10072
10073    If MEMBER_P, this static_assert is a class member.  */
10074
10075 static void 
10076 cp_parser_static_assert(cp_parser *parser, bool member_p)
10077 {
10078   tree condition;
10079   tree message;
10080   cp_token *token;
10081   location_t saved_loc;
10082   bool dummy;
10083
10084   /* Peek at the `static_assert' token so we can keep track of exactly
10085      where the static assertion started.  */
10086   token = cp_lexer_peek_token (parser->lexer);
10087   saved_loc = token->location;
10088
10089   /* Look for the `static_assert' keyword.  */
10090   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10091                                   RT_STATIC_ASSERT))
10092     return;
10093
10094   /*  We know we are in a static assertion; commit to any tentative
10095       parse.  */
10096   if (cp_parser_parsing_tentatively (parser))
10097     cp_parser_commit_to_tentative_parse (parser);
10098
10099   /* Parse the `(' starting the static assertion condition.  */
10100   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10101
10102   /* Parse the constant-expression.  Allow a non-constant expression
10103      here in order to give better diagnostics in finish_static_assert.  */
10104   condition = 
10105     cp_parser_constant_expression (parser,
10106                                    /*allow_non_constant_p=*/true,
10107                                    /*non_constant_p=*/&dummy);
10108
10109   /* Parse the separating `,'.  */
10110   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10111
10112   /* Parse the string-literal message.  */
10113   message = cp_parser_string_literal (parser, 
10114                                       /*translate=*/false,
10115                                       /*wide_ok=*/true);
10116
10117   /* A `)' completes the static assertion.  */
10118   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10119     cp_parser_skip_to_closing_parenthesis (parser, 
10120                                            /*recovering=*/true, 
10121                                            /*or_comma=*/false,
10122                                            /*consume_paren=*/true);
10123
10124   /* A semicolon terminates the declaration.  */
10125   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10126
10127   /* Complete the static assertion, which may mean either processing 
10128      the static assert now or saving it for template instantiation.  */
10129   finish_static_assert (condition, message, saved_loc, member_p);
10130 }
10131
10132 /* Parse a `decltype' type. Returns the type. 
10133
10134    simple-type-specifier:
10135      decltype ( expression )  */
10136
10137 static tree
10138 cp_parser_decltype (cp_parser *parser)
10139 {
10140   tree expr;
10141   bool id_expression_or_member_access_p = false;
10142   const char *saved_message;
10143   bool saved_integral_constant_expression_p;
10144   bool saved_non_integral_constant_expression_p;
10145   cp_token *id_expr_start_token;
10146
10147   /* Look for the `decltype' token.  */
10148   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10149     return error_mark_node;
10150
10151   /* Types cannot be defined in a `decltype' expression.  Save away the
10152      old message.  */
10153   saved_message = parser->type_definition_forbidden_message;
10154
10155   /* And create the new one.  */
10156   parser->type_definition_forbidden_message
10157     = G_("types may not be defined in %<decltype%> expressions");
10158
10159   /* The restrictions on constant-expressions do not apply inside
10160      decltype expressions.  */
10161   saved_integral_constant_expression_p
10162     = parser->integral_constant_expression_p;
10163   saved_non_integral_constant_expression_p
10164     = parser->non_integral_constant_expression_p;
10165   parser->integral_constant_expression_p = false;
10166
10167   /* Do not actually evaluate the expression.  */
10168   ++cp_unevaluated_operand;
10169
10170   /* Do not warn about problems with the expression.  */
10171   ++c_inhibit_evaluation_warnings;
10172
10173   /* Parse the opening `('.  */
10174   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10175     return error_mark_node;
10176   
10177   /* First, try parsing an id-expression.  */
10178   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10179   cp_parser_parse_tentatively (parser);
10180   expr = cp_parser_id_expression (parser,
10181                                   /*template_keyword_p=*/false,
10182                                   /*check_dependency_p=*/true,
10183                                   /*template_p=*/NULL,
10184                                   /*declarator_p=*/false,
10185                                   /*optional_p=*/false);
10186
10187   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10188     {
10189       bool non_integral_constant_expression_p = false;
10190       tree id_expression = expr;
10191       cp_id_kind idk;
10192       const char *error_msg;
10193
10194       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10195         /* Lookup the name we got back from the id-expression.  */
10196         expr = cp_parser_lookup_name (parser, expr,
10197                                       none_type,
10198                                       /*is_template=*/false,
10199                                       /*is_namespace=*/false,
10200                                       /*check_dependency=*/true,
10201                                       /*ambiguous_decls=*/NULL,
10202                                       id_expr_start_token->location);
10203
10204       if (expr
10205           && expr != error_mark_node
10206           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10207           && TREE_CODE (expr) != TYPE_DECL
10208           && (TREE_CODE (expr) != BIT_NOT_EXPR
10209               || !TYPE_P (TREE_OPERAND (expr, 0)))
10210           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10211         {
10212           /* Complete lookup of the id-expression.  */
10213           expr = (finish_id_expression
10214                   (id_expression, expr, parser->scope, &idk,
10215                    /*integral_constant_expression_p=*/false,
10216                    /*allow_non_integral_constant_expression_p=*/true,
10217                    &non_integral_constant_expression_p,
10218                    /*template_p=*/false,
10219                    /*done=*/true,
10220                    /*address_p=*/false,
10221                    /*template_arg_p=*/false,
10222                    &error_msg,
10223                    id_expr_start_token->location));
10224
10225           if (expr == error_mark_node)
10226             /* We found an id-expression, but it was something that we
10227                should not have found. This is an error, not something
10228                we can recover from, so note that we found an
10229                id-expression and we'll recover as gracefully as
10230                possible.  */
10231             id_expression_or_member_access_p = true;
10232         }
10233
10234       if (expr 
10235           && expr != error_mark_node
10236           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10237         /* We have an id-expression.  */
10238         id_expression_or_member_access_p = true;
10239     }
10240
10241   if (!id_expression_or_member_access_p)
10242     {
10243       /* Abort the id-expression parse.  */
10244       cp_parser_abort_tentative_parse (parser);
10245
10246       /* Parsing tentatively, again.  */
10247       cp_parser_parse_tentatively (parser);
10248
10249       /* Parse a class member access.  */
10250       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10251                                            /*cast_p=*/false,
10252                                            /*member_access_only_p=*/true, NULL);
10253
10254       if (expr 
10255           && expr != error_mark_node
10256           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10257         /* We have an id-expression.  */
10258         id_expression_or_member_access_p = true;
10259     }
10260
10261   if (id_expression_or_member_access_p)
10262     /* We have parsed the complete id-expression or member access.  */
10263     cp_parser_parse_definitely (parser);
10264   else
10265     {
10266       bool saved_greater_than_is_operator_p;
10267
10268       /* Abort our attempt to parse an id-expression or member access
10269          expression.  */
10270       cp_parser_abort_tentative_parse (parser);
10271
10272       /* Within a parenthesized expression, a `>' token is always
10273          the greater-than operator.  */
10274       saved_greater_than_is_operator_p
10275         = parser->greater_than_is_operator_p;
10276       parser->greater_than_is_operator_p = true;
10277
10278       /* Parse a full expression.  */
10279       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10280
10281       /* The `>' token might be the end of a template-id or
10282          template-parameter-list now.  */
10283       parser->greater_than_is_operator_p
10284         = saved_greater_than_is_operator_p;
10285     }
10286
10287   /* Go back to evaluating expressions.  */
10288   --cp_unevaluated_operand;
10289   --c_inhibit_evaluation_warnings;
10290
10291   /* Restore the old message and the integral constant expression
10292      flags.  */
10293   parser->type_definition_forbidden_message = saved_message;
10294   parser->integral_constant_expression_p
10295     = saved_integral_constant_expression_p;
10296   parser->non_integral_constant_expression_p
10297     = saved_non_integral_constant_expression_p;
10298
10299   if (expr == error_mark_node)
10300     {
10301       /* Skip everything up to the closing `)'.  */
10302       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10303                                              /*consume_paren=*/true);
10304       return error_mark_node;
10305     }
10306   
10307   /* Parse to the closing `)'.  */
10308   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10309     {
10310       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10311                                              /*consume_paren=*/true);
10312       return error_mark_node;
10313     }
10314
10315   return finish_decltype_type (expr, id_expression_or_member_access_p,
10316                                tf_warning_or_error);
10317 }
10318
10319 /* Special member functions [gram.special] */
10320
10321 /* Parse a conversion-function-id.
10322
10323    conversion-function-id:
10324      operator conversion-type-id
10325
10326    Returns an IDENTIFIER_NODE representing the operator.  */
10327
10328 static tree
10329 cp_parser_conversion_function_id (cp_parser* parser)
10330 {
10331   tree type;
10332   tree saved_scope;
10333   tree saved_qualifying_scope;
10334   tree saved_object_scope;
10335   tree pushed_scope = NULL_TREE;
10336
10337   /* Look for the `operator' token.  */
10338   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10339     return error_mark_node;
10340   /* When we parse the conversion-type-id, the current scope will be
10341      reset.  However, we need that information in able to look up the
10342      conversion function later, so we save it here.  */
10343   saved_scope = parser->scope;
10344   saved_qualifying_scope = parser->qualifying_scope;
10345   saved_object_scope = parser->object_scope;
10346   /* We must enter the scope of the class so that the names of
10347      entities declared within the class are available in the
10348      conversion-type-id.  For example, consider:
10349
10350        struct S {
10351          typedef int I;
10352          operator I();
10353        };
10354
10355        S::operator I() { ... }
10356
10357      In order to see that `I' is a type-name in the definition, we
10358      must be in the scope of `S'.  */
10359   if (saved_scope)
10360     pushed_scope = push_scope (saved_scope);
10361   /* Parse the conversion-type-id.  */
10362   type = cp_parser_conversion_type_id (parser);
10363   /* Leave the scope of the class, if any.  */
10364   if (pushed_scope)
10365     pop_scope (pushed_scope);
10366   /* Restore the saved scope.  */
10367   parser->scope = saved_scope;
10368   parser->qualifying_scope = saved_qualifying_scope;
10369   parser->object_scope = saved_object_scope;
10370   /* If the TYPE is invalid, indicate failure.  */
10371   if (type == error_mark_node)
10372     return error_mark_node;
10373   return mangle_conv_op_name_for_type (type);
10374 }
10375
10376 /* Parse a conversion-type-id:
10377
10378    conversion-type-id:
10379      type-specifier-seq conversion-declarator [opt]
10380
10381    Returns the TYPE specified.  */
10382
10383 static tree
10384 cp_parser_conversion_type_id (cp_parser* parser)
10385 {
10386   tree attributes;
10387   cp_decl_specifier_seq type_specifiers;
10388   cp_declarator *declarator;
10389   tree type_specified;
10390
10391   /* Parse the attributes.  */
10392   attributes = cp_parser_attributes_opt (parser);
10393   /* Parse the type-specifiers.  */
10394   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10395                                 /*is_trailing_return=*/false,
10396                                 &type_specifiers);
10397   /* If that didn't work, stop.  */
10398   if (type_specifiers.type == error_mark_node)
10399     return error_mark_node;
10400   /* Parse the conversion-declarator.  */
10401   declarator = cp_parser_conversion_declarator_opt (parser);
10402
10403   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10404                                     /*initialized=*/0, &attributes);
10405   if (attributes)
10406     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10407
10408   /* Don't give this error when parsing tentatively.  This happens to
10409      work because we always parse this definitively once.  */
10410   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10411       && type_uses_auto (type_specified))
10412     {
10413       error ("invalid use of %<auto%> in conversion operator");
10414       return error_mark_node;
10415     }
10416
10417   return type_specified;
10418 }
10419
10420 /* Parse an (optional) conversion-declarator.
10421
10422    conversion-declarator:
10423      ptr-operator conversion-declarator [opt]
10424
10425    */
10426
10427 static cp_declarator *
10428 cp_parser_conversion_declarator_opt (cp_parser* parser)
10429 {
10430   enum tree_code code;
10431   tree class_type;
10432   cp_cv_quals cv_quals;
10433
10434   /* We don't know if there's a ptr-operator next, or not.  */
10435   cp_parser_parse_tentatively (parser);
10436   /* Try the ptr-operator.  */
10437   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10438   /* If it worked, look for more conversion-declarators.  */
10439   if (cp_parser_parse_definitely (parser))
10440     {
10441       cp_declarator *declarator;
10442
10443       /* Parse another optional declarator.  */
10444       declarator = cp_parser_conversion_declarator_opt (parser);
10445
10446       return cp_parser_make_indirect_declarator
10447         (code, class_type, cv_quals, declarator);
10448    }
10449
10450   return NULL;
10451 }
10452
10453 /* Parse an (optional) ctor-initializer.
10454
10455    ctor-initializer:
10456      : mem-initializer-list
10457
10458    Returns TRUE iff the ctor-initializer was actually present.  */
10459
10460 static bool
10461 cp_parser_ctor_initializer_opt (cp_parser* parser)
10462 {
10463   /* If the next token is not a `:', then there is no
10464      ctor-initializer.  */
10465   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10466     {
10467       /* Do default initialization of any bases and members.  */
10468       if (DECL_CONSTRUCTOR_P (current_function_decl))
10469         finish_mem_initializers (NULL_TREE);
10470
10471       return false;
10472     }
10473
10474   /* Consume the `:' token.  */
10475   cp_lexer_consume_token (parser->lexer);
10476   /* And the mem-initializer-list.  */
10477   cp_parser_mem_initializer_list (parser);
10478
10479   return true;
10480 }
10481
10482 /* Parse a mem-initializer-list.
10483
10484    mem-initializer-list:
10485      mem-initializer ... [opt]
10486      mem-initializer ... [opt] , mem-initializer-list  */
10487
10488 static void
10489 cp_parser_mem_initializer_list (cp_parser* parser)
10490 {
10491   tree mem_initializer_list = NULL_TREE;
10492   cp_token *token = cp_lexer_peek_token (parser->lexer);
10493
10494   /* Let the semantic analysis code know that we are starting the
10495      mem-initializer-list.  */
10496   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10497     error_at (token->location,
10498               "only constructors take member initializers");
10499
10500   /* Loop through the list.  */
10501   while (true)
10502     {
10503       tree mem_initializer;
10504
10505       token = cp_lexer_peek_token (parser->lexer);
10506       /* Parse the mem-initializer.  */
10507       mem_initializer = cp_parser_mem_initializer (parser);
10508       /* If the next token is a `...', we're expanding member initializers. */
10509       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10510         {
10511           /* Consume the `...'. */
10512           cp_lexer_consume_token (parser->lexer);
10513
10514           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10515              can be expanded but members cannot. */
10516           if (mem_initializer != error_mark_node
10517               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10518             {
10519               error_at (token->location,
10520                         "cannot expand initializer for member %<%D%>",
10521                         TREE_PURPOSE (mem_initializer));
10522               mem_initializer = error_mark_node;
10523             }
10524
10525           /* Construct the pack expansion type. */
10526           if (mem_initializer != error_mark_node)
10527             mem_initializer = make_pack_expansion (mem_initializer);
10528         }
10529       /* Add it to the list, unless it was erroneous.  */
10530       if (mem_initializer != error_mark_node)
10531         {
10532           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10533           mem_initializer_list = mem_initializer;
10534         }
10535       /* If the next token is not a `,', we're done.  */
10536       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10537         break;
10538       /* Consume the `,' token.  */
10539       cp_lexer_consume_token (parser->lexer);
10540     }
10541
10542   /* Perform semantic analysis.  */
10543   if (DECL_CONSTRUCTOR_P (current_function_decl))
10544     finish_mem_initializers (mem_initializer_list);
10545 }
10546
10547 /* Parse a mem-initializer.
10548
10549    mem-initializer:
10550      mem-initializer-id ( expression-list [opt] )
10551      mem-initializer-id braced-init-list
10552
10553    GNU extension:
10554
10555    mem-initializer:
10556      ( expression-list [opt] )
10557
10558    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10559    class) or FIELD_DECL (for a non-static data member) to initialize;
10560    the TREE_VALUE is the expression-list.  An empty initialization
10561    list is represented by void_list_node.  */
10562
10563 static tree
10564 cp_parser_mem_initializer (cp_parser* parser)
10565 {
10566   tree mem_initializer_id;
10567   tree expression_list;
10568   tree member;
10569   cp_token *token = cp_lexer_peek_token (parser->lexer);
10570
10571   /* Find out what is being initialized.  */
10572   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10573     {
10574       permerror (token->location,
10575                  "anachronistic old-style base class initializer");
10576       mem_initializer_id = NULL_TREE;
10577     }
10578   else
10579     {
10580       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10581       if (mem_initializer_id == error_mark_node)
10582         return mem_initializer_id;
10583     }
10584   member = expand_member_init (mem_initializer_id);
10585   if (member && !DECL_P (member))
10586     in_base_initializer = 1;
10587
10588   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10589     {
10590       bool expr_non_constant_p;
10591       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10592       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10593       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10594       expression_list = build_tree_list (NULL_TREE, expression_list);
10595     }
10596   else
10597     {
10598       VEC(tree,gc)* vec;
10599       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10600                                                      /*cast_p=*/false,
10601                                                      /*allow_expansion_p=*/true,
10602                                                      /*non_constant_p=*/NULL);
10603       if (vec == NULL)
10604         return error_mark_node;
10605       expression_list = build_tree_list_vec (vec);
10606       release_tree_vector (vec);
10607     }
10608
10609   if (expression_list == error_mark_node)
10610     return error_mark_node;
10611   if (!expression_list)
10612     expression_list = void_type_node;
10613
10614   in_base_initializer = 0;
10615
10616   return member ? build_tree_list (member, expression_list) : error_mark_node;
10617 }
10618
10619 /* Parse a mem-initializer-id.
10620
10621    mem-initializer-id:
10622      :: [opt] nested-name-specifier [opt] class-name
10623      identifier
10624
10625    Returns a TYPE indicating the class to be initializer for the first
10626    production.  Returns an IDENTIFIER_NODE indicating the data member
10627    to be initialized for the second production.  */
10628
10629 static tree
10630 cp_parser_mem_initializer_id (cp_parser* parser)
10631 {
10632   bool global_scope_p;
10633   bool nested_name_specifier_p;
10634   bool template_p = false;
10635   tree id;
10636
10637   cp_token *token = cp_lexer_peek_token (parser->lexer);
10638
10639   /* `typename' is not allowed in this context ([temp.res]).  */
10640   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10641     {
10642       error_at (token->location, 
10643                 "keyword %<typename%> not allowed in this context (a qualified "
10644                 "member initializer is implicitly a type)");
10645       cp_lexer_consume_token (parser->lexer);
10646     }
10647   /* Look for the optional `::' operator.  */
10648   global_scope_p
10649     = (cp_parser_global_scope_opt (parser,
10650                                    /*current_scope_valid_p=*/false)
10651        != NULL_TREE);
10652   /* Look for the optional nested-name-specifier.  The simplest way to
10653      implement:
10654
10655        [temp.res]
10656
10657        The keyword `typename' is not permitted in a base-specifier or
10658        mem-initializer; in these contexts a qualified name that
10659        depends on a template-parameter is implicitly assumed to be a
10660        type name.
10661
10662      is to assume that we have seen the `typename' keyword at this
10663      point.  */
10664   nested_name_specifier_p
10665     = (cp_parser_nested_name_specifier_opt (parser,
10666                                             /*typename_keyword_p=*/true,
10667                                             /*check_dependency_p=*/true,
10668                                             /*type_p=*/true,
10669                                             /*is_declaration=*/true)
10670        != NULL_TREE);
10671   if (nested_name_specifier_p)
10672     template_p = cp_parser_optional_template_keyword (parser);
10673   /* If there is a `::' operator or a nested-name-specifier, then we
10674      are definitely looking for a class-name.  */
10675   if (global_scope_p || nested_name_specifier_p)
10676     return cp_parser_class_name (parser,
10677                                  /*typename_keyword_p=*/true,
10678                                  /*template_keyword_p=*/template_p,
10679                                  typename_type,
10680                                  /*check_dependency_p=*/true,
10681                                  /*class_head_p=*/false,
10682                                  /*is_declaration=*/true);
10683   /* Otherwise, we could also be looking for an ordinary identifier.  */
10684   cp_parser_parse_tentatively (parser);
10685   /* Try a class-name.  */
10686   id = cp_parser_class_name (parser,
10687                              /*typename_keyword_p=*/true,
10688                              /*template_keyword_p=*/false,
10689                              none_type,
10690                              /*check_dependency_p=*/true,
10691                              /*class_head_p=*/false,
10692                              /*is_declaration=*/true);
10693   /* If we found one, we're done.  */
10694   if (cp_parser_parse_definitely (parser))
10695     return id;
10696   /* Otherwise, look for an ordinary identifier.  */
10697   return cp_parser_identifier (parser);
10698 }
10699
10700 /* Overloading [gram.over] */
10701
10702 /* Parse an operator-function-id.
10703
10704    operator-function-id:
10705      operator operator
10706
10707    Returns an IDENTIFIER_NODE for the operator which is a
10708    human-readable spelling of the identifier, e.g., `operator +'.  */
10709
10710 static tree
10711 cp_parser_operator_function_id (cp_parser* parser)
10712 {
10713   /* Look for the `operator' keyword.  */
10714   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10715     return error_mark_node;
10716   /* And then the name of the operator itself.  */
10717   return cp_parser_operator (parser);
10718 }
10719
10720 /* Parse an operator.
10721
10722    operator:
10723      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10724      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10725      || ++ -- , ->* -> () []
10726
10727    GNU Extensions:
10728
10729    operator:
10730      <? >? <?= >?=
10731
10732    Returns an IDENTIFIER_NODE for the operator which is a
10733    human-readable spelling of the identifier, e.g., `operator +'.  */
10734
10735 static tree
10736 cp_parser_operator (cp_parser* parser)
10737 {
10738   tree id = NULL_TREE;
10739   cp_token *token;
10740
10741   /* Peek at the next token.  */
10742   token = cp_lexer_peek_token (parser->lexer);
10743   /* Figure out which operator we have.  */
10744   switch (token->type)
10745     {
10746     case CPP_KEYWORD:
10747       {
10748         enum tree_code op;
10749
10750         /* The keyword should be either `new' or `delete'.  */
10751         if (token->keyword == RID_NEW)
10752           op = NEW_EXPR;
10753         else if (token->keyword == RID_DELETE)
10754           op = DELETE_EXPR;
10755         else
10756           break;
10757
10758         /* Consume the `new' or `delete' token.  */
10759         cp_lexer_consume_token (parser->lexer);
10760
10761         /* Peek at the next token.  */
10762         token = cp_lexer_peek_token (parser->lexer);
10763         /* If it's a `[' token then this is the array variant of the
10764            operator.  */
10765         if (token->type == CPP_OPEN_SQUARE)
10766           {
10767             /* Consume the `[' token.  */
10768             cp_lexer_consume_token (parser->lexer);
10769             /* Look for the `]' token.  */
10770             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10771             id = ansi_opname (op == NEW_EXPR
10772                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10773           }
10774         /* Otherwise, we have the non-array variant.  */
10775         else
10776           id = ansi_opname (op);
10777
10778         return id;
10779       }
10780
10781     case CPP_PLUS:
10782       id = ansi_opname (PLUS_EXPR);
10783       break;
10784
10785     case CPP_MINUS:
10786       id = ansi_opname (MINUS_EXPR);
10787       break;
10788
10789     case CPP_MULT:
10790       id = ansi_opname (MULT_EXPR);
10791       break;
10792
10793     case CPP_DIV:
10794       id = ansi_opname (TRUNC_DIV_EXPR);
10795       break;
10796
10797     case CPP_MOD:
10798       id = ansi_opname (TRUNC_MOD_EXPR);
10799       break;
10800
10801     case CPP_XOR:
10802       id = ansi_opname (BIT_XOR_EXPR);
10803       break;
10804
10805     case CPP_AND:
10806       id = ansi_opname (BIT_AND_EXPR);
10807       break;
10808
10809     case CPP_OR:
10810       id = ansi_opname (BIT_IOR_EXPR);
10811       break;
10812
10813     case CPP_COMPL:
10814       id = ansi_opname (BIT_NOT_EXPR);
10815       break;
10816
10817     case CPP_NOT:
10818       id = ansi_opname (TRUTH_NOT_EXPR);
10819       break;
10820
10821     case CPP_EQ:
10822       id = ansi_assopname (NOP_EXPR);
10823       break;
10824
10825     case CPP_LESS:
10826       id = ansi_opname (LT_EXPR);
10827       break;
10828
10829     case CPP_GREATER:
10830       id = ansi_opname (GT_EXPR);
10831       break;
10832
10833     case CPP_PLUS_EQ:
10834       id = ansi_assopname (PLUS_EXPR);
10835       break;
10836
10837     case CPP_MINUS_EQ:
10838       id = ansi_assopname (MINUS_EXPR);
10839       break;
10840
10841     case CPP_MULT_EQ:
10842       id = ansi_assopname (MULT_EXPR);
10843       break;
10844
10845     case CPP_DIV_EQ:
10846       id = ansi_assopname (TRUNC_DIV_EXPR);
10847       break;
10848
10849     case CPP_MOD_EQ:
10850       id = ansi_assopname (TRUNC_MOD_EXPR);
10851       break;
10852
10853     case CPP_XOR_EQ:
10854       id = ansi_assopname (BIT_XOR_EXPR);
10855       break;
10856
10857     case CPP_AND_EQ:
10858       id = ansi_assopname (BIT_AND_EXPR);
10859       break;
10860
10861     case CPP_OR_EQ:
10862       id = ansi_assopname (BIT_IOR_EXPR);
10863       break;
10864
10865     case CPP_LSHIFT:
10866       id = ansi_opname (LSHIFT_EXPR);
10867       break;
10868
10869     case CPP_RSHIFT:
10870       id = ansi_opname (RSHIFT_EXPR);
10871       break;
10872
10873     case CPP_LSHIFT_EQ:
10874       id = ansi_assopname (LSHIFT_EXPR);
10875       break;
10876
10877     case CPP_RSHIFT_EQ:
10878       id = ansi_assopname (RSHIFT_EXPR);
10879       break;
10880
10881     case CPP_EQ_EQ:
10882       id = ansi_opname (EQ_EXPR);
10883       break;
10884
10885     case CPP_NOT_EQ:
10886       id = ansi_opname (NE_EXPR);
10887       break;
10888
10889     case CPP_LESS_EQ:
10890       id = ansi_opname (LE_EXPR);
10891       break;
10892
10893     case CPP_GREATER_EQ:
10894       id = ansi_opname (GE_EXPR);
10895       break;
10896
10897     case CPP_AND_AND:
10898       id = ansi_opname (TRUTH_ANDIF_EXPR);
10899       break;
10900
10901     case CPP_OR_OR:
10902       id = ansi_opname (TRUTH_ORIF_EXPR);
10903       break;
10904
10905     case CPP_PLUS_PLUS:
10906       id = ansi_opname (POSTINCREMENT_EXPR);
10907       break;
10908
10909     case CPP_MINUS_MINUS:
10910       id = ansi_opname (PREDECREMENT_EXPR);
10911       break;
10912
10913     case CPP_COMMA:
10914       id = ansi_opname (COMPOUND_EXPR);
10915       break;
10916
10917     case CPP_DEREF_STAR:
10918       id = ansi_opname (MEMBER_REF);
10919       break;
10920
10921     case CPP_DEREF:
10922       id = ansi_opname (COMPONENT_REF);
10923       break;
10924
10925     case CPP_OPEN_PAREN:
10926       /* Consume the `('.  */
10927       cp_lexer_consume_token (parser->lexer);
10928       /* Look for the matching `)'.  */
10929       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10930       return ansi_opname (CALL_EXPR);
10931
10932     case CPP_OPEN_SQUARE:
10933       /* Consume the `['.  */
10934       cp_lexer_consume_token (parser->lexer);
10935       /* Look for the matching `]'.  */
10936       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10937       return ansi_opname (ARRAY_REF);
10938
10939     default:
10940       /* Anything else is an error.  */
10941       break;
10942     }
10943
10944   /* If we have selected an identifier, we need to consume the
10945      operator token.  */
10946   if (id)
10947     cp_lexer_consume_token (parser->lexer);
10948   /* Otherwise, no valid operator name was present.  */
10949   else
10950     {
10951       cp_parser_error (parser, "expected operator");
10952       id = error_mark_node;
10953     }
10954
10955   return id;
10956 }
10957
10958 /* Parse a template-declaration.
10959
10960    template-declaration:
10961      export [opt] template < template-parameter-list > declaration
10962
10963    If MEMBER_P is TRUE, this template-declaration occurs within a
10964    class-specifier.
10965
10966    The grammar rule given by the standard isn't correct.  What
10967    is really meant is:
10968
10969    template-declaration:
10970      export [opt] template-parameter-list-seq
10971        decl-specifier-seq [opt] init-declarator [opt] ;
10972      export [opt] template-parameter-list-seq
10973        function-definition
10974
10975    template-parameter-list-seq:
10976      template-parameter-list-seq [opt]
10977      template < template-parameter-list >  */
10978
10979 static void
10980 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10981 {
10982   /* Check for `export'.  */
10983   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10984     {
10985       /* Consume the `export' token.  */
10986       cp_lexer_consume_token (parser->lexer);
10987       /* Warn that we do not support `export'.  */
10988       warning (0, "keyword %<export%> not implemented, and will be ignored");
10989     }
10990
10991   cp_parser_template_declaration_after_export (parser, member_p);
10992 }
10993
10994 /* Parse a template-parameter-list.
10995
10996    template-parameter-list:
10997      template-parameter
10998      template-parameter-list , template-parameter
10999
11000    Returns a TREE_LIST.  Each node represents a template parameter.
11001    The nodes are connected via their TREE_CHAINs.  */
11002
11003 static tree
11004 cp_parser_template_parameter_list (cp_parser* parser)
11005 {
11006   tree parameter_list = NULL_TREE;
11007
11008   begin_template_parm_list ();
11009
11010   /* The loop below parses the template parms.  We first need to know
11011      the total number of template parms to be able to compute proper
11012      canonical types of each dependent type. So after the loop, when
11013      we know the total number of template parms,
11014      end_template_parm_list computes the proper canonical types and
11015      fixes up the dependent types accordingly.  */
11016   while (true)
11017     {
11018       tree parameter;
11019       bool is_non_type;
11020       bool is_parameter_pack;
11021       location_t parm_loc;
11022
11023       /* Parse the template-parameter.  */
11024       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11025       parameter = cp_parser_template_parameter (parser, 
11026                                                 &is_non_type,
11027                                                 &is_parameter_pack);
11028       /* Add it to the list.  */
11029       if (parameter != error_mark_node)
11030         parameter_list = process_template_parm (parameter_list,
11031                                                 parm_loc,
11032                                                 parameter,
11033                                                 is_non_type,
11034                                                 is_parameter_pack,
11035                                                 0);
11036       else
11037        {
11038          tree err_parm = build_tree_list (parameter, parameter);
11039          parameter_list = chainon (parameter_list, err_parm);
11040        }
11041
11042       /* If the next token is not a `,', we're done.  */
11043       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11044         break;
11045       /* Otherwise, consume the `,' token.  */
11046       cp_lexer_consume_token (parser->lexer);
11047     }
11048
11049   return end_template_parm_list (parameter_list);
11050 }
11051
11052 /* Parse a template-parameter.
11053
11054    template-parameter:
11055      type-parameter
11056      parameter-declaration
11057
11058    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11059    the parameter.  The TREE_PURPOSE is the default value, if any.
11060    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11061    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11062    set to true iff this parameter is a parameter pack. */
11063
11064 static tree
11065 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11066                               bool *is_parameter_pack)
11067 {
11068   cp_token *token;
11069   cp_parameter_declarator *parameter_declarator;
11070   cp_declarator *id_declarator;
11071   tree parm;
11072
11073   /* Assume it is a type parameter or a template parameter.  */
11074   *is_non_type = false;
11075   /* Assume it not a parameter pack. */
11076   *is_parameter_pack = false;
11077   /* Peek at the next token.  */
11078   token = cp_lexer_peek_token (parser->lexer);
11079   /* If it is `class' or `template', we have a type-parameter.  */
11080   if (token->keyword == RID_TEMPLATE)
11081     return cp_parser_type_parameter (parser, is_parameter_pack);
11082   /* If it is `class' or `typename' we do not know yet whether it is a
11083      type parameter or a non-type parameter.  Consider:
11084
11085        template <typename T, typename T::X X> ...
11086
11087      or:
11088
11089        template <class C, class D*> ...
11090
11091      Here, the first parameter is a type parameter, and the second is
11092      a non-type parameter.  We can tell by looking at the token after
11093      the identifier -- if it is a `,', `=', or `>' then we have a type
11094      parameter.  */
11095   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11096     {
11097       /* Peek at the token after `class' or `typename'.  */
11098       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11099       /* If it's an ellipsis, we have a template type parameter
11100          pack. */
11101       if (token->type == CPP_ELLIPSIS)
11102         return cp_parser_type_parameter (parser, is_parameter_pack);
11103       /* If it's an identifier, skip it.  */
11104       if (token->type == CPP_NAME)
11105         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11106       /* Now, see if the token looks like the end of a template
11107          parameter.  */
11108       if (token->type == CPP_COMMA
11109           || token->type == CPP_EQ
11110           || token->type == CPP_GREATER)
11111         return cp_parser_type_parameter (parser, is_parameter_pack);
11112     }
11113
11114   /* Otherwise, it is a non-type parameter.
11115
11116      [temp.param]
11117
11118      When parsing a default template-argument for a non-type
11119      template-parameter, the first non-nested `>' is taken as the end
11120      of the template parameter-list rather than a greater-than
11121      operator.  */
11122   *is_non_type = true;
11123   parameter_declarator
11124      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11125                                         /*parenthesized_p=*/NULL);
11126
11127   /* If the parameter declaration is marked as a parameter pack, set
11128      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11129      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11130      grokdeclarator. */
11131   if (parameter_declarator
11132       && parameter_declarator->declarator
11133       && parameter_declarator->declarator->parameter_pack_p)
11134     {
11135       *is_parameter_pack = true;
11136       parameter_declarator->declarator->parameter_pack_p = false;
11137     }
11138
11139   /* If the next token is an ellipsis, and we don't already have it
11140      marked as a parameter pack, then we have a parameter pack (that
11141      has no declarator).  */
11142   if (!*is_parameter_pack
11143       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11144       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11145     {
11146       /* Consume the `...'.  */
11147       cp_lexer_consume_token (parser->lexer);
11148       maybe_warn_variadic_templates ();
11149       
11150       *is_parameter_pack = true;
11151     }
11152   /* We might end up with a pack expansion as the type of the non-type
11153      template parameter, in which case this is a non-type template
11154      parameter pack.  */
11155   else if (parameter_declarator
11156            && parameter_declarator->decl_specifiers.type
11157            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11158     {
11159       *is_parameter_pack = true;
11160       parameter_declarator->decl_specifiers.type = 
11161         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11162     }
11163
11164   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11165     {
11166       /* Parameter packs cannot have default arguments.  However, a
11167          user may try to do so, so we'll parse them and give an
11168          appropriate diagnostic here.  */
11169
11170       /* Consume the `='.  */
11171       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11172       cp_lexer_consume_token (parser->lexer);
11173       
11174       /* Find the name of the parameter pack.  */     
11175       id_declarator = parameter_declarator->declarator;
11176       while (id_declarator && id_declarator->kind != cdk_id)
11177         id_declarator = id_declarator->declarator;
11178       
11179       if (id_declarator && id_declarator->kind == cdk_id)
11180         error_at (start_token->location,
11181                   "template parameter pack %qD cannot have a default argument",
11182                   id_declarator->u.id.unqualified_name);
11183       else
11184         error_at (start_token->location,
11185                   "template parameter pack cannot have a default argument");
11186       
11187       /* Parse the default argument, but throw away the result.  */
11188       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11189     }
11190
11191   parm = grokdeclarator (parameter_declarator->declarator,
11192                          &parameter_declarator->decl_specifiers,
11193                          TPARM, /*initialized=*/0,
11194                          /*attrlist=*/NULL);
11195   if (parm == error_mark_node)
11196     return error_mark_node;
11197
11198   return build_tree_list (parameter_declarator->default_argument, parm);
11199 }
11200
11201 /* Parse a type-parameter.
11202
11203    type-parameter:
11204      class identifier [opt]
11205      class identifier [opt] = type-id
11206      typename identifier [opt]
11207      typename identifier [opt] = type-id
11208      template < template-parameter-list > class identifier [opt]
11209      template < template-parameter-list > class identifier [opt]
11210        = id-expression
11211
11212    GNU Extension (variadic templates):
11213
11214    type-parameter:
11215      class ... identifier [opt]
11216      typename ... identifier [opt]
11217
11218    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11219    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11220    the declaration of the parameter.
11221
11222    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11223
11224 static tree
11225 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11226 {
11227   cp_token *token;
11228   tree parameter;
11229
11230   /* Look for a keyword to tell us what kind of parameter this is.  */
11231   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11232   if (!token)
11233     return error_mark_node;
11234
11235   switch (token->keyword)
11236     {
11237     case RID_CLASS:
11238     case RID_TYPENAME:
11239       {
11240         tree identifier;
11241         tree default_argument;
11242
11243         /* If the next token is an ellipsis, we have a template
11244            argument pack. */
11245         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11246           {
11247             /* Consume the `...' token. */
11248             cp_lexer_consume_token (parser->lexer);
11249             maybe_warn_variadic_templates ();
11250
11251             *is_parameter_pack = true;
11252           }
11253
11254         /* If the next token is an identifier, then it names the
11255            parameter.  */
11256         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11257           identifier = cp_parser_identifier (parser);
11258         else
11259           identifier = NULL_TREE;
11260
11261         /* Create the parameter.  */
11262         parameter = finish_template_type_parm (class_type_node, identifier);
11263
11264         /* If the next token is an `=', we have a default argument.  */
11265         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11266           {
11267             /* Consume the `=' token.  */
11268             cp_lexer_consume_token (parser->lexer);
11269             /* Parse the default-argument.  */
11270             push_deferring_access_checks (dk_no_deferred);
11271             default_argument = cp_parser_type_id (parser);
11272
11273             /* Template parameter packs cannot have default
11274                arguments. */
11275             if (*is_parameter_pack)
11276               {
11277                 if (identifier)
11278                   error_at (token->location,
11279                             "template parameter pack %qD cannot have a "
11280                             "default argument", identifier);
11281                 else
11282                   error_at (token->location,
11283                             "template parameter packs cannot have "
11284                             "default arguments");
11285                 default_argument = NULL_TREE;
11286               }
11287             pop_deferring_access_checks ();
11288           }
11289         else
11290           default_argument = NULL_TREE;
11291
11292         /* Create the combined representation of the parameter and the
11293            default argument.  */
11294         parameter = build_tree_list (default_argument, parameter);
11295       }
11296       break;
11297
11298     case RID_TEMPLATE:
11299       {
11300         tree identifier;
11301         tree default_argument;
11302
11303         /* Look for the `<'.  */
11304         cp_parser_require (parser, CPP_LESS, RT_LESS);
11305         /* Parse the template-parameter-list.  */
11306         cp_parser_template_parameter_list (parser);
11307         /* Look for the `>'.  */
11308         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11309         /* Look for the `class' keyword.  */
11310         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11311         /* If the next token is an ellipsis, we have a template
11312            argument pack. */
11313         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11314           {
11315             /* Consume the `...' token. */
11316             cp_lexer_consume_token (parser->lexer);
11317             maybe_warn_variadic_templates ();
11318
11319             *is_parameter_pack = true;
11320           }
11321         /* If the next token is an `=', then there is a
11322            default-argument.  If the next token is a `>', we are at
11323            the end of the parameter-list.  If the next token is a `,',
11324            then we are at the end of this parameter.  */
11325         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11326             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11327             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11328           {
11329             identifier = cp_parser_identifier (parser);
11330             /* Treat invalid names as if the parameter were nameless.  */
11331             if (identifier == error_mark_node)
11332               identifier = NULL_TREE;
11333           }
11334         else
11335           identifier = NULL_TREE;
11336
11337         /* Create the template parameter.  */
11338         parameter = finish_template_template_parm (class_type_node,
11339                                                    identifier);
11340
11341         /* If the next token is an `=', then there is a
11342            default-argument.  */
11343         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11344           {
11345             bool is_template;
11346
11347             /* Consume the `='.  */
11348             cp_lexer_consume_token (parser->lexer);
11349             /* Parse the id-expression.  */
11350             push_deferring_access_checks (dk_no_deferred);
11351             /* save token before parsing the id-expression, for error
11352                reporting */
11353             token = cp_lexer_peek_token (parser->lexer);
11354             default_argument
11355               = cp_parser_id_expression (parser,
11356                                          /*template_keyword_p=*/false,
11357                                          /*check_dependency_p=*/true,
11358                                          /*template_p=*/&is_template,
11359                                          /*declarator_p=*/false,
11360                                          /*optional_p=*/false);
11361             if (TREE_CODE (default_argument) == TYPE_DECL)
11362               /* If the id-expression was a template-id that refers to
11363                  a template-class, we already have the declaration here,
11364                  so no further lookup is needed.  */
11365                  ;
11366             else
11367               /* Look up the name.  */
11368               default_argument
11369                 = cp_parser_lookup_name (parser, default_argument,
11370                                          none_type,
11371                                          /*is_template=*/is_template,
11372                                          /*is_namespace=*/false,
11373                                          /*check_dependency=*/true,
11374                                          /*ambiguous_decls=*/NULL,
11375                                          token->location);
11376             /* See if the default argument is valid.  */
11377             default_argument
11378               = check_template_template_default_arg (default_argument);
11379
11380             /* Template parameter packs cannot have default
11381                arguments. */
11382             if (*is_parameter_pack)
11383               {
11384                 if (identifier)
11385                   error_at (token->location,
11386                             "template parameter pack %qD cannot "
11387                             "have a default argument",
11388                             identifier);
11389                 else
11390                   error_at (token->location, "template parameter packs cannot "
11391                             "have default arguments");
11392                 default_argument = NULL_TREE;
11393               }
11394             pop_deferring_access_checks ();
11395           }
11396         else
11397           default_argument = NULL_TREE;
11398
11399         /* Create the combined representation of the parameter and the
11400            default argument.  */
11401         parameter = build_tree_list (default_argument, parameter);
11402       }
11403       break;
11404
11405     default:
11406       gcc_unreachable ();
11407       break;
11408     }
11409
11410   return parameter;
11411 }
11412
11413 /* Parse a template-id.
11414
11415    template-id:
11416      template-name < template-argument-list [opt] >
11417
11418    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11419    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11420    returned.  Otherwise, if the template-name names a function, or set
11421    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11422    names a class, returns a TYPE_DECL for the specialization.
11423
11424    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11425    uninstantiated templates.  */
11426
11427 static tree
11428 cp_parser_template_id (cp_parser *parser,
11429                        bool template_keyword_p,
11430                        bool check_dependency_p,
11431                        bool is_declaration)
11432 {
11433   int i;
11434   tree templ;
11435   tree arguments;
11436   tree template_id;
11437   cp_token_position start_of_id = 0;
11438   deferred_access_check *chk;
11439   VEC (deferred_access_check,gc) *access_check;
11440   cp_token *next_token = NULL, *next_token_2 = NULL;
11441   bool is_identifier;
11442
11443   /* If the next token corresponds to a template-id, there is no need
11444      to reparse it.  */
11445   next_token = cp_lexer_peek_token (parser->lexer);
11446   if (next_token->type == CPP_TEMPLATE_ID)
11447     {
11448       struct tree_check *check_value;
11449
11450       /* Get the stored value.  */
11451       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11452       /* Perform any access checks that were deferred.  */
11453       access_check = check_value->checks;
11454       if (access_check)
11455         {
11456           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11457             perform_or_defer_access_check (chk->binfo,
11458                                            chk->decl,
11459                                            chk->diag_decl);
11460         }
11461       /* Return the stored value.  */
11462       return check_value->value;
11463     }
11464
11465   /* Avoid performing name lookup if there is no possibility of
11466      finding a template-id.  */
11467   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11468       || (next_token->type == CPP_NAME
11469           && !cp_parser_nth_token_starts_template_argument_list_p
11470                (parser, 2)))
11471     {
11472       cp_parser_error (parser, "expected template-id");
11473       return error_mark_node;
11474     }
11475
11476   /* Remember where the template-id starts.  */
11477   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11478     start_of_id = cp_lexer_token_position (parser->lexer, false);
11479
11480   push_deferring_access_checks (dk_deferred);
11481
11482   /* Parse the template-name.  */
11483   is_identifier = false;
11484   templ = cp_parser_template_name (parser, template_keyword_p,
11485                                    check_dependency_p,
11486                                    is_declaration,
11487                                    &is_identifier);
11488   if (templ == error_mark_node || is_identifier)
11489     {
11490       pop_deferring_access_checks ();
11491       return templ;
11492     }
11493
11494   /* If we find the sequence `[:' after a template-name, it's probably
11495      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11496      parse correctly the argument list.  */
11497   next_token = cp_lexer_peek_token (parser->lexer);
11498   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11499   if (next_token->type == CPP_OPEN_SQUARE
11500       && next_token->flags & DIGRAPH
11501       && next_token_2->type == CPP_COLON
11502       && !(next_token_2->flags & PREV_WHITE))
11503     {
11504       cp_parser_parse_tentatively (parser);
11505       /* Change `:' into `::'.  */
11506       next_token_2->type = CPP_SCOPE;
11507       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11508          CPP_LESS.  */
11509       cp_lexer_consume_token (parser->lexer);
11510
11511       /* Parse the arguments.  */
11512       arguments = cp_parser_enclosed_template_argument_list (parser);
11513       if (!cp_parser_parse_definitely (parser))
11514         {
11515           /* If we couldn't parse an argument list, then we revert our changes
11516              and return simply an error. Maybe this is not a template-id
11517              after all.  */
11518           next_token_2->type = CPP_COLON;
11519           cp_parser_error (parser, "expected %<<%>");
11520           pop_deferring_access_checks ();
11521           return error_mark_node;
11522         }
11523       /* Otherwise, emit an error about the invalid digraph, but continue
11524          parsing because we got our argument list.  */
11525       if (permerror (next_token->location,
11526                      "%<<::%> cannot begin a template-argument list"))
11527         {
11528           static bool hint = false;
11529           inform (next_token->location,
11530                   "%<<:%> is an alternate spelling for %<[%>."
11531                   " Insert whitespace between %<<%> and %<::%>");
11532           if (!hint && !flag_permissive)
11533             {
11534               inform (next_token->location, "(if you use %<-fpermissive%>"
11535                       " G++ will accept your code)");
11536               hint = true;
11537             }
11538         }
11539     }
11540   else
11541     {
11542       /* Look for the `<' that starts the template-argument-list.  */
11543       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11544         {
11545           pop_deferring_access_checks ();
11546           return error_mark_node;
11547         }
11548       /* Parse the arguments.  */
11549       arguments = cp_parser_enclosed_template_argument_list (parser);
11550     }
11551
11552   /* Build a representation of the specialization.  */
11553   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11554     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11555   else if (DECL_CLASS_TEMPLATE_P (templ)
11556            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11557     {
11558       bool entering_scope;
11559       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11560          template (rather than some instantiation thereof) only if
11561          is not nested within some other construct.  For example, in
11562          "template <typename T> void f(T) { A<T>::", A<T> is just an
11563          instantiation of A.  */
11564       entering_scope = (template_parm_scope_p ()
11565                         && cp_lexer_next_token_is (parser->lexer,
11566                                                    CPP_SCOPE));
11567       template_id
11568         = finish_template_type (templ, arguments, entering_scope);
11569     }
11570   else
11571     {
11572       /* If it's not a class-template or a template-template, it should be
11573          a function-template.  */
11574       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11575                    || TREE_CODE (templ) == OVERLOAD
11576                    || BASELINK_P (templ)));
11577
11578       template_id = lookup_template_function (templ, arguments);
11579     }
11580
11581   /* If parsing tentatively, replace the sequence of tokens that makes
11582      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11583      should we re-parse the token stream, we will not have to repeat
11584      the effort required to do the parse, nor will we issue duplicate
11585      error messages about problems during instantiation of the
11586      template.  */
11587   if (start_of_id)
11588     {
11589       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11590
11591       /* Reset the contents of the START_OF_ID token.  */
11592       token->type = CPP_TEMPLATE_ID;
11593       /* Retrieve any deferred checks.  Do not pop this access checks yet
11594          so the memory will not be reclaimed during token replacing below.  */
11595       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11596       token->u.tree_check_value->value = template_id;
11597       token->u.tree_check_value->checks = get_deferred_access_checks ();
11598       token->keyword = RID_MAX;
11599
11600       /* Purge all subsequent tokens.  */
11601       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11602
11603       /* ??? Can we actually assume that, if template_id ==
11604          error_mark_node, we will have issued a diagnostic to the
11605          user, as opposed to simply marking the tentative parse as
11606          failed?  */
11607       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11608         error_at (token->location, "parse error in template argument list");
11609     }
11610
11611   pop_deferring_access_checks ();
11612   return template_id;
11613 }
11614
11615 /* Parse a template-name.
11616
11617    template-name:
11618      identifier
11619
11620    The standard should actually say:
11621
11622    template-name:
11623      identifier
11624      operator-function-id
11625
11626    A defect report has been filed about this issue.
11627
11628    A conversion-function-id cannot be a template name because they cannot
11629    be part of a template-id. In fact, looking at this code:
11630
11631    a.operator K<int>()
11632
11633    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11634    It is impossible to call a templated conversion-function-id with an
11635    explicit argument list, since the only allowed template parameter is
11636    the type to which it is converting.
11637
11638    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11639    `template' keyword, in a construction like:
11640
11641      T::template f<3>()
11642
11643    In that case `f' is taken to be a template-name, even though there
11644    is no way of knowing for sure.
11645
11646    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11647    name refers to a set of overloaded functions, at least one of which
11648    is a template, or an IDENTIFIER_NODE with the name of the template,
11649    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11650    names are looked up inside uninstantiated templates.  */
11651
11652 static tree
11653 cp_parser_template_name (cp_parser* parser,
11654                          bool template_keyword_p,
11655                          bool check_dependency_p,
11656                          bool is_declaration,
11657                          bool *is_identifier)
11658 {
11659   tree identifier;
11660   tree decl;
11661   tree fns;
11662   cp_token *token = cp_lexer_peek_token (parser->lexer);
11663
11664   /* If the next token is `operator', then we have either an
11665      operator-function-id or a conversion-function-id.  */
11666   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11667     {
11668       /* We don't know whether we're looking at an
11669          operator-function-id or a conversion-function-id.  */
11670       cp_parser_parse_tentatively (parser);
11671       /* Try an operator-function-id.  */
11672       identifier = cp_parser_operator_function_id (parser);
11673       /* If that didn't work, try a conversion-function-id.  */
11674       if (!cp_parser_parse_definitely (parser))
11675         {
11676           cp_parser_error (parser, "expected template-name");
11677           return error_mark_node;
11678         }
11679     }
11680   /* Look for the identifier.  */
11681   else
11682     identifier = cp_parser_identifier (parser);
11683
11684   /* If we didn't find an identifier, we don't have a template-id.  */
11685   if (identifier == error_mark_node)
11686     return error_mark_node;
11687
11688   /* If the name immediately followed the `template' keyword, then it
11689      is a template-name.  However, if the next token is not `<', then
11690      we do not treat it as a template-name, since it is not being used
11691      as part of a template-id.  This enables us to handle constructs
11692      like:
11693
11694        template <typename T> struct S { S(); };
11695        template <typename T> S<T>::S();
11696
11697      correctly.  We would treat `S' as a template -- if it were `S<T>'
11698      -- but we do not if there is no `<'.  */
11699
11700   if (processing_template_decl
11701       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11702     {
11703       /* In a declaration, in a dependent context, we pretend that the
11704          "template" keyword was present in order to improve error
11705          recovery.  For example, given:
11706
11707            template <typename T> void f(T::X<int>);
11708
11709          we want to treat "X<int>" as a template-id.  */
11710       if (is_declaration
11711           && !template_keyword_p
11712           && parser->scope && TYPE_P (parser->scope)
11713           && check_dependency_p
11714           && dependent_scope_p (parser->scope)
11715           /* Do not do this for dtors (or ctors), since they never
11716              need the template keyword before their name.  */
11717           && !constructor_name_p (identifier, parser->scope))
11718         {
11719           cp_token_position start = 0;
11720
11721           /* Explain what went wrong.  */
11722           error_at (token->location, "non-template %qD used as template",
11723                     identifier);
11724           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11725                   parser->scope, identifier);
11726           /* If parsing tentatively, find the location of the "<" token.  */
11727           if (cp_parser_simulate_error (parser))
11728             start = cp_lexer_token_position (parser->lexer, true);
11729           /* Parse the template arguments so that we can issue error
11730              messages about them.  */
11731           cp_lexer_consume_token (parser->lexer);
11732           cp_parser_enclosed_template_argument_list (parser);
11733           /* Skip tokens until we find a good place from which to
11734              continue parsing.  */
11735           cp_parser_skip_to_closing_parenthesis (parser,
11736                                                  /*recovering=*/true,
11737                                                  /*or_comma=*/true,
11738                                                  /*consume_paren=*/false);
11739           /* If parsing tentatively, permanently remove the
11740              template argument list.  That will prevent duplicate
11741              error messages from being issued about the missing
11742              "template" keyword.  */
11743           if (start)
11744             cp_lexer_purge_tokens_after (parser->lexer, start);
11745           if (is_identifier)
11746             *is_identifier = true;
11747           return identifier;
11748         }
11749
11750       /* If the "template" keyword is present, then there is generally
11751          no point in doing name-lookup, so we just return IDENTIFIER.
11752          But, if the qualifying scope is non-dependent then we can
11753          (and must) do name-lookup normally.  */
11754       if (template_keyword_p
11755           && (!parser->scope
11756               || (TYPE_P (parser->scope)
11757                   && dependent_type_p (parser->scope))))
11758         return identifier;
11759     }
11760
11761   /* Look up the name.  */
11762   decl = cp_parser_lookup_name (parser, identifier,
11763                                 none_type,
11764                                 /*is_template=*/true,
11765                                 /*is_namespace=*/false,
11766                                 check_dependency_p,
11767                                 /*ambiguous_decls=*/NULL,
11768                                 token->location);
11769
11770   /* If DECL is a template, then the name was a template-name.  */
11771   if (TREE_CODE (decl) == TEMPLATE_DECL)
11772     ;
11773   else
11774     {
11775       tree fn = NULL_TREE;
11776
11777       /* The standard does not explicitly indicate whether a name that
11778          names a set of overloaded declarations, some of which are
11779          templates, is a template-name.  However, such a name should
11780          be a template-name; otherwise, there is no way to form a
11781          template-id for the overloaded templates.  */
11782       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11783       if (TREE_CODE (fns) == OVERLOAD)
11784         for (fn = fns; fn; fn = OVL_NEXT (fn))
11785           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11786             break;
11787
11788       if (!fn)
11789         {
11790           /* The name does not name a template.  */
11791           cp_parser_error (parser, "expected template-name");
11792           return error_mark_node;
11793         }
11794     }
11795
11796   /* If DECL is dependent, and refers to a function, then just return
11797      its name; we will look it up again during template instantiation.  */
11798   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11799     {
11800       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11801       if (TYPE_P (scope) && dependent_type_p (scope))
11802         return identifier;
11803     }
11804
11805   return decl;
11806 }
11807
11808 /* Parse a template-argument-list.
11809
11810    template-argument-list:
11811      template-argument ... [opt]
11812      template-argument-list , template-argument ... [opt]
11813
11814    Returns a TREE_VEC containing the arguments.  */
11815
11816 static tree
11817 cp_parser_template_argument_list (cp_parser* parser)
11818 {
11819   tree fixed_args[10];
11820   unsigned n_args = 0;
11821   unsigned alloced = 10;
11822   tree *arg_ary = fixed_args;
11823   tree vec;
11824   bool saved_in_template_argument_list_p;
11825   bool saved_ice_p;
11826   bool saved_non_ice_p;
11827
11828   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11829   parser->in_template_argument_list_p = true;
11830   /* Even if the template-id appears in an integral
11831      constant-expression, the contents of the argument list do
11832      not.  */
11833   saved_ice_p = parser->integral_constant_expression_p;
11834   parser->integral_constant_expression_p = false;
11835   saved_non_ice_p = parser->non_integral_constant_expression_p;
11836   parser->non_integral_constant_expression_p = false;
11837   /* Parse the arguments.  */
11838   do
11839     {
11840       tree argument;
11841
11842       if (n_args)
11843         /* Consume the comma.  */
11844         cp_lexer_consume_token (parser->lexer);
11845
11846       /* Parse the template-argument.  */
11847       argument = cp_parser_template_argument (parser);
11848
11849       /* If the next token is an ellipsis, we're expanding a template
11850          argument pack. */
11851       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11852         {
11853           if (argument == error_mark_node)
11854             {
11855               cp_token *token = cp_lexer_peek_token (parser->lexer);
11856               error_at (token->location,
11857                         "expected parameter pack before %<...%>");
11858             }
11859           /* Consume the `...' token. */
11860           cp_lexer_consume_token (parser->lexer);
11861
11862           /* Make the argument into a TYPE_PACK_EXPANSION or
11863              EXPR_PACK_EXPANSION. */
11864           argument = make_pack_expansion (argument);
11865         }
11866
11867       if (n_args == alloced)
11868         {
11869           alloced *= 2;
11870
11871           if (arg_ary == fixed_args)
11872             {
11873               arg_ary = XNEWVEC (tree, alloced);
11874               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11875             }
11876           else
11877             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11878         }
11879       arg_ary[n_args++] = argument;
11880     }
11881   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11882
11883   vec = make_tree_vec (n_args);
11884
11885   while (n_args--)
11886     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11887
11888   if (arg_ary != fixed_args)
11889     free (arg_ary);
11890   parser->non_integral_constant_expression_p = saved_non_ice_p;
11891   parser->integral_constant_expression_p = saved_ice_p;
11892   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11893 #ifdef ENABLE_CHECKING
11894   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11895 #endif
11896   return vec;
11897 }
11898
11899 /* Parse a template-argument.
11900
11901    template-argument:
11902      assignment-expression
11903      type-id
11904      id-expression
11905
11906    The representation is that of an assignment-expression, type-id, or
11907    id-expression -- except that the qualified id-expression is
11908    evaluated, so that the value returned is either a DECL or an
11909    OVERLOAD.
11910
11911    Although the standard says "assignment-expression", it forbids
11912    throw-expressions or assignments in the template argument.
11913    Therefore, we use "conditional-expression" instead.  */
11914
11915 static tree
11916 cp_parser_template_argument (cp_parser* parser)
11917 {
11918   tree argument;
11919   bool template_p;
11920   bool address_p;
11921   bool maybe_type_id = false;
11922   cp_token *token = NULL, *argument_start_token = NULL;
11923   cp_id_kind idk;
11924
11925   /* There's really no way to know what we're looking at, so we just
11926      try each alternative in order.
11927
11928        [temp.arg]
11929
11930        In a template-argument, an ambiguity between a type-id and an
11931        expression is resolved to a type-id, regardless of the form of
11932        the corresponding template-parameter.
11933
11934      Therefore, we try a type-id first.  */
11935   cp_parser_parse_tentatively (parser);
11936   argument = cp_parser_template_type_arg (parser);
11937   /* If there was no error parsing the type-id but the next token is a
11938      '>>', our behavior depends on which dialect of C++ we're
11939      parsing. In C++98, we probably found a typo for '> >'. But there
11940      are type-id which are also valid expressions. For instance:
11941
11942      struct X { int operator >> (int); };
11943      template <int V> struct Foo {};
11944      Foo<X () >> 5> r;
11945
11946      Here 'X()' is a valid type-id of a function type, but the user just
11947      wanted to write the expression "X() >> 5". Thus, we remember that we
11948      found a valid type-id, but we still try to parse the argument as an
11949      expression to see what happens. 
11950
11951      In C++0x, the '>>' will be considered two separate '>'
11952      tokens.  */
11953   if (!cp_parser_error_occurred (parser)
11954       && cxx_dialect == cxx98
11955       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11956     {
11957       maybe_type_id = true;
11958       cp_parser_abort_tentative_parse (parser);
11959     }
11960   else
11961     {
11962       /* If the next token isn't a `,' or a `>', then this argument wasn't
11963       really finished. This means that the argument is not a valid
11964       type-id.  */
11965       if (!cp_parser_next_token_ends_template_argument_p (parser))
11966         cp_parser_error (parser, "expected template-argument");
11967       /* If that worked, we're done.  */
11968       if (cp_parser_parse_definitely (parser))
11969         return argument;
11970     }
11971   /* We're still not sure what the argument will be.  */
11972   cp_parser_parse_tentatively (parser);
11973   /* Try a template.  */
11974   argument_start_token = cp_lexer_peek_token (parser->lexer);
11975   argument = cp_parser_id_expression (parser,
11976                                       /*template_keyword_p=*/false,
11977                                       /*check_dependency_p=*/true,
11978                                       &template_p,
11979                                       /*declarator_p=*/false,
11980                                       /*optional_p=*/false);
11981   /* If the next token isn't a `,' or a `>', then this argument wasn't
11982      really finished.  */
11983   if (!cp_parser_next_token_ends_template_argument_p (parser))
11984     cp_parser_error (parser, "expected template-argument");
11985   if (!cp_parser_error_occurred (parser))
11986     {
11987       /* Figure out what is being referred to.  If the id-expression
11988          was for a class template specialization, then we will have a
11989          TYPE_DECL at this point.  There is no need to do name lookup
11990          at this point in that case.  */
11991       if (TREE_CODE (argument) != TYPE_DECL)
11992         argument = cp_parser_lookup_name (parser, argument,
11993                                           none_type,
11994                                           /*is_template=*/template_p,
11995                                           /*is_namespace=*/false,
11996                                           /*check_dependency=*/true,
11997                                           /*ambiguous_decls=*/NULL,
11998                                           argument_start_token->location);
11999       if (TREE_CODE (argument) != TEMPLATE_DECL
12000           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12001         cp_parser_error (parser, "expected template-name");
12002     }
12003   if (cp_parser_parse_definitely (parser))
12004     return argument;
12005   /* It must be a non-type argument.  There permitted cases are given
12006      in [temp.arg.nontype]:
12007
12008      -- an integral constant-expression of integral or enumeration
12009         type; or
12010
12011      -- the name of a non-type template-parameter; or
12012
12013      -- the name of an object or function with external linkage...
12014
12015      -- the address of an object or function with external linkage...
12016
12017      -- a pointer to member...  */
12018   /* Look for a non-type template parameter.  */
12019   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12020     {
12021       cp_parser_parse_tentatively (parser);
12022       argument = cp_parser_primary_expression (parser,
12023                                                /*address_p=*/false,
12024                                                /*cast_p=*/false,
12025                                                /*template_arg_p=*/true,
12026                                                &idk);
12027       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12028           || !cp_parser_next_token_ends_template_argument_p (parser))
12029         cp_parser_simulate_error (parser);
12030       if (cp_parser_parse_definitely (parser))
12031         return argument;
12032     }
12033
12034   /* If the next token is "&", the argument must be the address of an
12035      object or function with external linkage.  */
12036   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12037   if (address_p)
12038     cp_lexer_consume_token (parser->lexer);
12039   /* See if we might have an id-expression.  */
12040   token = cp_lexer_peek_token (parser->lexer);
12041   if (token->type == CPP_NAME
12042       || token->keyword == RID_OPERATOR
12043       || token->type == CPP_SCOPE
12044       || token->type == CPP_TEMPLATE_ID
12045       || token->type == CPP_NESTED_NAME_SPECIFIER)
12046     {
12047       cp_parser_parse_tentatively (parser);
12048       argument = cp_parser_primary_expression (parser,
12049                                                address_p,
12050                                                /*cast_p=*/false,
12051                                                /*template_arg_p=*/true,
12052                                                &idk);
12053       if (cp_parser_error_occurred (parser)
12054           || !cp_parser_next_token_ends_template_argument_p (parser))
12055         cp_parser_abort_tentative_parse (parser);
12056       else
12057         {
12058           tree probe;
12059
12060           if (TREE_CODE (argument) == INDIRECT_REF)
12061             {
12062               gcc_assert (REFERENCE_REF_P (argument));
12063               argument = TREE_OPERAND (argument, 0);
12064             }
12065
12066           /* If we're in a template, we represent a qualified-id referring
12067              to a static data member as a SCOPE_REF even if the scope isn't
12068              dependent so that we can check access control later.  */
12069           probe = argument;
12070           if (TREE_CODE (probe) == SCOPE_REF)
12071             probe = TREE_OPERAND (probe, 1);
12072           if (TREE_CODE (probe) == VAR_DECL)
12073             {
12074               /* A variable without external linkage might still be a
12075                  valid constant-expression, so no error is issued here
12076                  if the external-linkage check fails.  */
12077               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12078                 cp_parser_simulate_error (parser);
12079             }
12080           else if (is_overloaded_fn (argument))
12081             /* All overloaded functions are allowed; if the external
12082                linkage test does not pass, an error will be issued
12083                later.  */
12084             ;
12085           else if (address_p
12086                    && (TREE_CODE (argument) == OFFSET_REF
12087                        || TREE_CODE (argument) == SCOPE_REF))
12088             /* A pointer-to-member.  */
12089             ;
12090           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12091             ;
12092           else
12093             cp_parser_simulate_error (parser);
12094
12095           if (cp_parser_parse_definitely (parser))
12096             {
12097               if (address_p)
12098                 argument = build_x_unary_op (ADDR_EXPR, argument,
12099                                              tf_warning_or_error);
12100               return argument;
12101             }
12102         }
12103     }
12104   /* If the argument started with "&", there are no other valid
12105      alternatives at this point.  */
12106   if (address_p)
12107     {
12108       cp_parser_error (parser, "invalid non-type template argument");
12109       return error_mark_node;
12110     }
12111
12112   /* If the argument wasn't successfully parsed as a type-id followed
12113      by '>>', the argument can only be a constant expression now.
12114      Otherwise, we try parsing the constant-expression tentatively,
12115      because the argument could really be a type-id.  */
12116   if (maybe_type_id)
12117     cp_parser_parse_tentatively (parser);
12118   argument = cp_parser_constant_expression (parser,
12119                                             /*allow_non_constant_p=*/false,
12120                                             /*non_constant_p=*/NULL);
12121   argument = fold_non_dependent_expr (argument);
12122   if (!maybe_type_id)
12123     return argument;
12124   if (!cp_parser_next_token_ends_template_argument_p (parser))
12125     cp_parser_error (parser, "expected template-argument");
12126   if (cp_parser_parse_definitely (parser))
12127     return argument;
12128   /* We did our best to parse the argument as a non type-id, but that
12129      was the only alternative that matched (albeit with a '>' after
12130      it). We can assume it's just a typo from the user, and a
12131      diagnostic will then be issued.  */
12132   return cp_parser_template_type_arg (parser);
12133 }
12134
12135 /* Parse an explicit-instantiation.
12136
12137    explicit-instantiation:
12138      template declaration
12139
12140    Although the standard says `declaration', what it really means is:
12141
12142    explicit-instantiation:
12143      template decl-specifier-seq [opt] declarator [opt] ;
12144
12145    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12146    supposed to be allowed.  A defect report has been filed about this
12147    issue.
12148
12149    GNU Extension:
12150
12151    explicit-instantiation:
12152      storage-class-specifier template
12153        decl-specifier-seq [opt] declarator [opt] ;
12154      function-specifier template
12155        decl-specifier-seq [opt] declarator [opt] ;  */
12156
12157 static void
12158 cp_parser_explicit_instantiation (cp_parser* parser)
12159 {
12160   int declares_class_or_enum;
12161   cp_decl_specifier_seq decl_specifiers;
12162   tree extension_specifier = NULL_TREE;
12163
12164   timevar_push (TV_TEMPLATE_INST);
12165
12166   /* Look for an (optional) storage-class-specifier or
12167      function-specifier.  */
12168   if (cp_parser_allow_gnu_extensions_p (parser))
12169     {
12170       extension_specifier
12171         = cp_parser_storage_class_specifier_opt (parser);
12172       if (!extension_specifier)
12173         extension_specifier
12174           = cp_parser_function_specifier_opt (parser,
12175                                               /*decl_specs=*/NULL);
12176     }
12177
12178   /* Look for the `template' keyword.  */
12179   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12180   /* Let the front end know that we are processing an explicit
12181      instantiation.  */
12182   begin_explicit_instantiation ();
12183   /* [temp.explicit] says that we are supposed to ignore access
12184      control while processing explicit instantiation directives.  */
12185   push_deferring_access_checks (dk_no_check);
12186   /* Parse a decl-specifier-seq.  */
12187   cp_parser_decl_specifier_seq (parser,
12188                                 CP_PARSER_FLAGS_OPTIONAL,
12189                                 &decl_specifiers,
12190                                 &declares_class_or_enum);
12191   /* If there was exactly one decl-specifier, and it declared a class,
12192      and there's no declarator, then we have an explicit type
12193      instantiation.  */
12194   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12195     {
12196       tree type;
12197
12198       type = check_tag_decl (&decl_specifiers);
12199       /* Turn access control back on for names used during
12200          template instantiation.  */
12201       pop_deferring_access_checks ();
12202       if (type)
12203         do_type_instantiation (type, extension_specifier,
12204                                /*complain=*/tf_error);
12205     }
12206   else
12207     {
12208       cp_declarator *declarator;
12209       tree decl;
12210
12211       /* Parse the declarator.  */
12212       declarator
12213         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12214                                 /*ctor_dtor_or_conv_p=*/NULL,
12215                                 /*parenthesized_p=*/NULL,
12216                                 /*member_p=*/false);
12217       if (declares_class_or_enum & 2)
12218         cp_parser_check_for_definition_in_return_type (declarator,
12219                                                        decl_specifiers.type,
12220                                                        decl_specifiers.type_location);
12221       if (declarator != cp_error_declarator)
12222         {
12223           if (decl_specifiers.specs[(int)ds_inline])
12224             permerror (input_location, "explicit instantiation shall not use"
12225                        " %<inline%> specifier");
12226           if (decl_specifiers.specs[(int)ds_constexpr])
12227             permerror (input_location, "explicit instantiation shall not use"
12228                        " %<constexpr%> specifier");
12229
12230           decl = grokdeclarator (declarator, &decl_specifiers,
12231                                  NORMAL, 0, &decl_specifiers.attributes);
12232           /* Turn access control back on for names used during
12233              template instantiation.  */
12234           pop_deferring_access_checks ();
12235           /* Do the explicit instantiation.  */
12236           do_decl_instantiation (decl, extension_specifier);
12237         }
12238       else
12239         {
12240           pop_deferring_access_checks ();
12241           /* Skip the body of the explicit instantiation.  */
12242           cp_parser_skip_to_end_of_statement (parser);
12243         }
12244     }
12245   /* We're done with the instantiation.  */
12246   end_explicit_instantiation ();
12247
12248   cp_parser_consume_semicolon_at_end_of_statement (parser);
12249
12250   timevar_pop (TV_TEMPLATE_INST);
12251 }
12252
12253 /* Parse an explicit-specialization.
12254
12255    explicit-specialization:
12256      template < > declaration
12257
12258    Although the standard says `declaration', what it really means is:
12259
12260    explicit-specialization:
12261      template <> decl-specifier [opt] init-declarator [opt] ;
12262      template <> function-definition
12263      template <> explicit-specialization
12264      template <> template-declaration  */
12265
12266 static void
12267 cp_parser_explicit_specialization (cp_parser* parser)
12268 {
12269   bool need_lang_pop;
12270   cp_token *token = cp_lexer_peek_token (parser->lexer);
12271
12272   /* Look for the `template' keyword.  */
12273   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12274   /* Look for the `<'.  */
12275   cp_parser_require (parser, CPP_LESS, RT_LESS);
12276   /* Look for the `>'.  */
12277   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12278   /* We have processed another parameter list.  */
12279   ++parser->num_template_parameter_lists;
12280   /* [temp]
12281
12282      A template ... explicit specialization ... shall not have C
12283      linkage.  */
12284   if (current_lang_name == lang_name_c)
12285     {
12286       error_at (token->location, "template specialization with C linkage");
12287       /* Give it C++ linkage to avoid confusing other parts of the
12288          front end.  */
12289       push_lang_context (lang_name_cplusplus);
12290       need_lang_pop = true;
12291     }
12292   else
12293     need_lang_pop = false;
12294   /* Let the front end know that we are beginning a specialization.  */
12295   if (!begin_specialization ())
12296     {
12297       end_specialization ();
12298       return;
12299     }
12300
12301   /* If the next keyword is `template', we need to figure out whether
12302      or not we're looking a template-declaration.  */
12303   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12304     {
12305       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12306           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12307         cp_parser_template_declaration_after_export (parser,
12308                                                      /*member_p=*/false);
12309       else
12310         cp_parser_explicit_specialization (parser);
12311     }
12312   else
12313     /* Parse the dependent declaration.  */
12314     cp_parser_single_declaration (parser,
12315                                   /*checks=*/NULL,
12316                                   /*member_p=*/false,
12317                                   /*explicit_specialization_p=*/true,
12318                                   /*friend_p=*/NULL);
12319   /* We're done with the specialization.  */
12320   end_specialization ();
12321   /* For the erroneous case of a template with C linkage, we pushed an
12322      implicit C++ linkage scope; exit that scope now.  */
12323   if (need_lang_pop)
12324     pop_lang_context ();
12325   /* We're done with this parameter list.  */
12326   --parser->num_template_parameter_lists;
12327 }
12328
12329 /* Parse a type-specifier.
12330
12331    type-specifier:
12332      simple-type-specifier
12333      class-specifier
12334      enum-specifier
12335      elaborated-type-specifier
12336      cv-qualifier
12337
12338    GNU Extension:
12339
12340    type-specifier:
12341      __complex__
12342
12343    Returns a representation of the type-specifier.  For a
12344    class-specifier, enum-specifier, or elaborated-type-specifier, a
12345    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12346
12347    The parser flags FLAGS is used to control type-specifier parsing.
12348
12349    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12350    in a decl-specifier-seq.
12351
12352    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12353    class-specifier, enum-specifier, or elaborated-type-specifier, then
12354    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12355    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12356    zero.
12357
12358    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12359    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12360    is set to FALSE.  */
12361
12362 static tree
12363 cp_parser_type_specifier (cp_parser* parser,
12364                           cp_parser_flags flags,
12365                           cp_decl_specifier_seq *decl_specs,
12366                           bool is_declaration,
12367                           int* declares_class_or_enum,
12368                           bool* is_cv_qualifier)
12369 {
12370   tree type_spec = NULL_TREE;
12371   cp_token *token;
12372   enum rid keyword;
12373   cp_decl_spec ds = ds_last;
12374
12375   /* Assume this type-specifier does not declare a new type.  */
12376   if (declares_class_or_enum)
12377     *declares_class_or_enum = 0;
12378   /* And that it does not specify a cv-qualifier.  */
12379   if (is_cv_qualifier)
12380     *is_cv_qualifier = false;
12381   /* Peek at the next token.  */
12382   token = cp_lexer_peek_token (parser->lexer);
12383
12384   /* If we're looking at a keyword, we can use that to guide the
12385      production we choose.  */
12386   keyword = token->keyword;
12387   switch (keyword)
12388     {
12389     case RID_ENUM:
12390       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12391         goto elaborated_type_specifier;
12392
12393       /* Look for the enum-specifier.  */
12394       type_spec = cp_parser_enum_specifier (parser);
12395       /* If that worked, we're done.  */
12396       if (type_spec)
12397         {
12398           if (declares_class_or_enum)
12399             *declares_class_or_enum = 2;
12400           if (decl_specs)
12401             cp_parser_set_decl_spec_type (decl_specs,
12402                                           type_spec,
12403                                           token->location,
12404                                           /*user_defined_p=*/true);
12405           return type_spec;
12406         }
12407       else
12408         goto elaborated_type_specifier;
12409
12410       /* Any of these indicate either a class-specifier, or an
12411          elaborated-type-specifier.  */
12412     case RID_CLASS:
12413     case RID_STRUCT:
12414     case RID_UNION:
12415       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12416         goto elaborated_type_specifier;
12417
12418       /* Parse tentatively so that we can back up if we don't find a
12419          class-specifier.  */
12420       cp_parser_parse_tentatively (parser);
12421       /* Look for the class-specifier.  */
12422       type_spec = cp_parser_class_specifier (parser);
12423       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12424       /* If that worked, we're done.  */
12425       if (cp_parser_parse_definitely (parser))
12426         {
12427           if (declares_class_or_enum)
12428             *declares_class_or_enum = 2;
12429           if (decl_specs)
12430             cp_parser_set_decl_spec_type (decl_specs,
12431                                           type_spec,
12432                                           token->location,
12433                                           /*user_defined_p=*/true);
12434           return type_spec;
12435         }
12436
12437       /* Fall through.  */
12438     elaborated_type_specifier:
12439       /* We're declaring (not defining) a class or enum.  */
12440       if (declares_class_or_enum)
12441         *declares_class_or_enum = 1;
12442
12443       /* Fall through.  */
12444     case RID_TYPENAME:
12445       /* Look for an elaborated-type-specifier.  */
12446       type_spec
12447         = (cp_parser_elaborated_type_specifier
12448            (parser,
12449             decl_specs && decl_specs->specs[(int) ds_friend],
12450             is_declaration));
12451       if (decl_specs)
12452         cp_parser_set_decl_spec_type (decl_specs,
12453                                       type_spec,
12454                                       token->location,
12455                                       /*user_defined_p=*/true);
12456       return type_spec;
12457
12458     case RID_CONST:
12459       ds = ds_const;
12460       if (is_cv_qualifier)
12461         *is_cv_qualifier = true;
12462       break;
12463
12464     case RID_VOLATILE:
12465       ds = ds_volatile;
12466       if (is_cv_qualifier)
12467         *is_cv_qualifier = true;
12468       break;
12469
12470     case RID_RESTRICT:
12471       ds = ds_restrict;
12472       if (is_cv_qualifier)
12473         *is_cv_qualifier = true;
12474       break;
12475
12476     case RID_COMPLEX:
12477       /* The `__complex__' keyword is a GNU extension.  */
12478       ds = ds_complex;
12479       break;
12480
12481     default:
12482       break;
12483     }
12484
12485   /* Handle simple keywords.  */
12486   if (ds != ds_last)
12487     {
12488       if (decl_specs)
12489         {
12490           ++decl_specs->specs[(int)ds];
12491           decl_specs->any_specifiers_p = true;
12492         }
12493       return cp_lexer_consume_token (parser->lexer)->u.value;
12494     }
12495
12496   /* If we do not already have a type-specifier, assume we are looking
12497      at a simple-type-specifier.  */
12498   type_spec = cp_parser_simple_type_specifier (parser,
12499                                                decl_specs,
12500                                                flags);
12501
12502   /* If we didn't find a type-specifier, and a type-specifier was not
12503      optional in this context, issue an error message.  */
12504   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12505     {
12506       cp_parser_error (parser, "expected type specifier");
12507       return error_mark_node;
12508     }
12509
12510   return type_spec;
12511 }
12512
12513 /* Parse a simple-type-specifier.
12514
12515    simple-type-specifier:
12516      :: [opt] nested-name-specifier [opt] type-name
12517      :: [opt] nested-name-specifier template template-id
12518      char
12519      wchar_t
12520      bool
12521      short
12522      int
12523      long
12524      signed
12525      unsigned
12526      float
12527      double
12528      void
12529
12530    C++0x Extension:
12531
12532    simple-type-specifier:
12533      auto
12534      decltype ( expression )   
12535      char16_t
12536      char32_t
12537      __underlying_type ( type-id )
12538
12539    GNU Extension:
12540
12541    simple-type-specifier:
12542      __int128
12543      __typeof__ unary-expression
12544      __typeof__ ( type-id )
12545
12546    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12547    appropriately updated.  */
12548
12549 static tree
12550 cp_parser_simple_type_specifier (cp_parser* parser,
12551                                  cp_decl_specifier_seq *decl_specs,
12552                                  cp_parser_flags flags)
12553 {
12554   tree type = NULL_TREE;
12555   cp_token *token;
12556
12557   /* Peek at the next token.  */
12558   token = cp_lexer_peek_token (parser->lexer);
12559
12560   /* If we're looking at a keyword, things are easy.  */
12561   switch (token->keyword)
12562     {
12563     case RID_CHAR:
12564       if (decl_specs)
12565         decl_specs->explicit_char_p = true;
12566       type = char_type_node;
12567       break;
12568     case RID_CHAR16:
12569       type = char16_type_node;
12570       break;
12571     case RID_CHAR32:
12572       type = char32_type_node;
12573       break;
12574     case RID_WCHAR:
12575       type = wchar_type_node;
12576       break;
12577     case RID_BOOL:
12578       type = boolean_type_node;
12579       break;
12580     case RID_SHORT:
12581       if (decl_specs)
12582         ++decl_specs->specs[(int) ds_short];
12583       type = short_integer_type_node;
12584       break;
12585     case RID_INT:
12586       if (decl_specs)
12587         decl_specs->explicit_int_p = true;
12588       type = integer_type_node;
12589       break;
12590     case RID_INT128:
12591       if (!int128_integer_type_node)
12592         break;
12593       if (decl_specs)
12594         decl_specs->explicit_int128_p = true;
12595       type = int128_integer_type_node;
12596       break;
12597     case RID_LONG:
12598       if (decl_specs)
12599         ++decl_specs->specs[(int) ds_long];
12600       type = long_integer_type_node;
12601       break;
12602     case RID_SIGNED:
12603       if (decl_specs)
12604         ++decl_specs->specs[(int) ds_signed];
12605       type = integer_type_node;
12606       break;
12607     case RID_UNSIGNED:
12608       if (decl_specs)
12609         ++decl_specs->specs[(int) ds_unsigned];
12610       type = unsigned_type_node;
12611       break;
12612     case RID_FLOAT:
12613       type = float_type_node;
12614       break;
12615     case RID_DOUBLE:
12616       type = double_type_node;
12617       break;
12618     case RID_VOID:
12619       type = void_type_node;
12620       break;
12621       
12622     case RID_AUTO:
12623       maybe_warn_cpp0x (CPP0X_AUTO);
12624       type = make_auto ();
12625       break;
12626
12627     case RID_DECLTYPE:
12628       /* Parse the `decltype' type.  */
12629       type = cp_parser_decltype (parser);
12630
12631       if (decl_specs)
12632         cp_parser_set_decl_spec_type (decl_specs, type,
12633                                       token->location,
12634                                       /*user_defined_p=*/true);
12635
12636       return type;
12637
12638     case RID_TYPEOF:
12639       /* Consume the `typeof' token.  */
12640       cp_lexer_consume_token (parser->lexer);
12641       /* Parse the operand to `typeof'.  */
12642       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12643       /* If it is not already a TYPE, take its type.  */
12644       if (!TYPE_P (type))
12645         type = finish_typeof (type);
12646
12647       if (decl_specs)
12648         cp_parser_set_decl_spec_type (decl_specs, type,
12649                                       token->location,
12650                                       /*user_defined_p=*/true);
12651
12652       return type;
12653
12654     case RID_UNDERLYING_TYPE:
12655       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12656
12657       if (decl_specs)
12658         cp_parser_set_decl_spec_type (decl_specs, type,
12659                                       token->location,
12660                                       /*user_defined_p=*/true);
12661
12662       return type;
12663
12664     default:
12665       break;
12666     }
12667
12668   /* If the type-specifier was for a built-in type, we're done.  */
12669   if (type)
12670     {
12671       /* Record the type.  */
12672       if (decl_specs
12673           && (token->keyword != RID_SIGNED
12674               && token->keyword != RID_UNSIGNED
12675               && token->keyword != RID_SHORT
12676               && token->keyword != RID_LONG))
12677         cp_parser_set_decl_spec_type (decl_specs,
12678                                       type,
12679                                       token->location,
12680                                       /*user_defined=*/false);
12681       if (decl_specs)
12682         decl_specs->any_specifiers_p = true;
12683
12684       /* Consume the token.  */
12685       cp_lexer_consume_token (parser->lexer);
12686
12687       /* There is no valid C++ program where a non-template type is
12688          followed by a "<".  That usually indicates that the user thought
12689          that the type was a template.  */
12690       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12691
12692       return TYPE_NAME (type);
12693     }
12694
12695   /* The type-specifier must be a user-defined type.  */
12696   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12697     {
12698       bool qualified_p;
12699       bool global_p;
12700
12701       /* Don't gobble tokens or issue error messages if this is an
12702          optional type-specifier.  */
12703       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12704         cp_parser_parse_tentatively (parser);
12705
12706       /* Look for the optional `::' operator.  */
12707       global_p
12708         = (cp_parser_global_scope_opt (parser,
12709                                        /*current_scope_valid_p=*/false)
12710            != NULL_TREE);
12711       /* Look for the nested-name specifier.  */
12712       qualified_p
12713         = (cp_parser_nested_name_specifier_opt (parser,
12714                                                 /*typename_keyword_p=*/false,
12715                                                 /*check_dependency_p=*/true,
12716                                                 /*type_p=*/false,
12717                                                 /*is_declaration=*/false)
12718            != NULL_TREE);
12719       token = cp_lexer_peek_token (parser->lexer);
12720       /* If we have seen a nested-name-specifier, and the next token
12721          is `template', then we are using the template-id production.  */
12722       if (parser->scope
12723           && cp_parser_optional_template_keyword (parser))
12724         {
12725           /* Look for the template-id.  */
12726           type = cp_parser_template_id (parser,
12727                                         /*template_keyword_p=*/true,
12728                                         /*check_dependency_p=*/true,
12729                                         /*is_declaration=*/false);
12730           /* If the template-id did not name a type, we are out of
12731              luck.  */
12732           if (TREE_CODE (type) != TYPE_DECL)
12733             {
12734               cp_parser_error (parser, "expected template-id for type");
12735               type = NULL_TREE;
12736             }
12737         }
12738       /* Otherwise, look for a type-name.  */
12739       else
12740         type = cp_parser_type_name (parser);
12741       /* Keep track of all name-lookups performed in class scopes.  */
12742       if (type
12743           && !global_p
12744           && !qualified_p
12745           && TREE_CODE (type) == TYPE_DECL
12746           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12747         maybe_note_name_used_in_class (DECL_NAME (type), type);
12748       /* If it didn't work out, we don't have a TYPE.  */
12749       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12750           && !cp_parser_parse_definitely (parser))
12751         type = NULL_TREE;
12752       if (type && decl_specs)
12753         cp_parser_set_decl_spec_type (decl_specs, type,
12754                                       token->location,
12755                                       /*user_defined=*/true);
12756     }
12757
12758   /* If we didn't get a type-name, issue an error message.  */
12759   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12760     {
12761       cp_parser_error (parser, "expected type-name");
12762       return error_mark_node;
12763     }
12764
12765   if (type && type != error_mark_node)
12766     {
12767       /* See if TYPE is an Objective-C type, and if so, parse and
12768          accept any protocol references following it.  Do this before
12769          the cp_parser_check_for_invalid_template_id() call, because
12770          Objective-C types can be followed by '<...>' which would
12771          enclose protocol names rather than template arguments, and so
12772          everything is fine.  */
12773       if (c_dialect_objc () && !parser->scope
12774           && (objc_is_id (type) || objc_is_class_name (type)))
12775         {
12776           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12777           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12778
12779           /* Clobber the "unqualified" type previously entered into
12780              DECL_SPECS with the new, improved protocol-qualified version.  */
12781           if (decl_specs)
12782             decl_specs->type = qual_type;
12783
12784           return qual_type;
12785         }
12786
12787       /* There is no valid C++ program where a non-template type is
12788          followed by a "<".  That usually indicates that the user
12789          thought that the type was a template.  */
12790       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12791                                                token->location);
12792     }
12793
12794   return type;
12795 }
12796
12797 /* Parse a type-name.
12798
12799    type-name:
12800      class-name
12801      enum-name
12802      typedef-name
12803
12804    enum-name:
12805      identifier
12806
12807    typedef-name:
12808      identifier
12809
12810    Returns a TYPE_DECL for the type.  */
12811
12812 static tree
12813 cp_parser_type_name (cp_parser* parser)
12814 {
12815   tree type_decl;
12816
12817   /* We can't know yet whether it is a class-name or not.  */
12818   cp_parser_parse_tentatively (parser);
12819   /* Try a class-name.  */
12820   type_decl = cp_parser_class_name (parser,
12821                                     /*typename_keyword_p=*/false,
12822                                     /*template_keyword_p=*/false,
12823                                     none_type,
12824                                     /*check_dependency_p=*/true,
12825                                     /*class_head_p=*/false,
12826                                     /*is_declaration=*/false);
12827   /* If it's not a class-name, keep looking.  */
12828   if (!cp_parser_parse_definitely (parser))
12829     {
12830       /* It must be a typedef-name or an enum-name.  */
12831       return cp_parser_nonclass_name (parser);
12832     }
12833
12834   return type_decl;
12835 }
12836
12837 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12838
12839    enum-name:
12840      identifier
12841
12842    typedef-name:
12843      identifier
12844
12845    Returns a TYPE_DECL for the type.  */
12846
12847 static tree
12848 cp_parser_nonclass_name (cp_parser* parser)
12849 {
12850   tree type_decl;
12851   tree identifier;
12852
12853   cp_token *token = cp_lexer_peek_token (parser->lexer);
12854   identifier = cp_parser_identifier (parser);
12855   if (identifier == error_mark_node)
12856     return error_mark_node;
12857
12858   /* Look up the type-name.  */
12859   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12860
12861   if (TREE_CODE (type_decl) != TYPE_DECL
12862       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12863     {
12864       /* See if this is an Objective-C type.  */
12865       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12866       tree type = objc_get_protocol_qualified_type (identifier, protos);
12867       if (type)
12868         type_decl = TYPE_NAME (type);
12869     }
12870
12871   /* Issue an error if we did not find a type-name.  */
12872   if (TREE_CODE (type_decl) != TYPE_DECL
12873       /* In Objective-C, we have the complication that class names are
12874          normally type names and start declarations (eg, the
12875          "NSObject" in "NSObject *object;"), but can be used in an
12876          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12877          is an expression.  So, a classname followed by a dot is not a
12878          valid type-name.  */
12879       || (objc_is_class_name (TREE_TYPE (type_decl))
12880           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12881     {
12882       if (!cp_parser_simulate_error (parser))
12883         cp_parser_name_lookup_error (parser, identifier, type_decl,
12884                                      NLE_TYPE, token->location);
12885       return error_mark_node;
12886     }
12887   /* Remember that the name was used in the definition of the
12888      current class so that we can check later to see if the
12889      meaning would have been different after the class was
12890      entirely defined.  */
12891   else if (type_decl != error_mark_node
12892            && !parser->scope)
12893     maybe_note_name_used_in_class (identifier, type_decl);
12894   
12895   return type_decl;
12896 }
12897
12898 /* Parse an elaborated-type-specifier.  Note that the grammar given
12899    here incorporates the resolution to DR68.
12900
12901    elaborated-type-specifier:
12902      class-key :: [opt] nested-name-specifier [opt] identifier
12903      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12904      enum-key :: [opt] nested-name-specifier [opt] identifier
12905      typename :: [opt] nested-name-specifier identifier
12906      typename :: [opt] nested-name-specifier template [opt]
12907        template-id
12908
12909    GNU extension:
12910
12911    elaborated-type-specifier:
12912      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12913      class-key attributes :: [opt] nested-name-specifier [opt]
12914                template [opt] template-id
12915      enum attributes :: [opt] nested-name-specifier [opt] identifier
12916
12917    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12918    declared `friend'.  If IS_DECLARATION is TRUE, then this
12919    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12920    something is being declared.
12921
12922    Returns the TYPE specified.  */
12923
12924 static tree
12925 cp_parser_elaborated_type_specifier (cp_parser* parser,
12926                                      bool is_friend,
12927                                      bool is_declaration)
12928 {
12929   enum tag_types tag_type;
12930   tree identifier;
12931   tree type = NULL_TREE;
12932   tree attributes = NULL_TREE;
12933   tree globalscope;
12934   cp_token *token = NULL;
12935
12936   /* See if we're looking at the `enum' keyword.  */
12937   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12938     {
12939       /* Consume the `enum' token.  */
12940       cp_lexer_consume_token (parser->lexer);
12941       /* Remember that it's an enumeration type.  */
12942       tag_type = enum_type;
12943       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12944          enums) is used here.  */
12945       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12946           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12947         {
12948             pedwarn (input_location, 0, "elaborated-type-specifier "
12949                       "for a scoped enum must not use the %<%D%> keyword",
12950                       cp_lexer_peek_token (parser->lexer)->u.value);
12951           /* Consume the `struct' or `class' and parse it anyway.  */
12952           cp_lexer_consume_token (parser->lexer);
12953         }
12954       /* Parse the attributes.  */
12955       attributes = cp_parser_attributes_opt (parser);
12956     }
12957   /* Or, it might be `typename'.  */
12958   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12959                                            RID_TYPENAME))
12960     {
12961       /* Consume the `typename' token.  */
12962       cp_lexer_consume_token (parser->lexer);
12963       /* Remember that it's a `typename' type.  */
12964       tag_type = typename_type;
12965     }
12966   /* Otherwise it must be a class-key.  */
12967   else
12968     {
12969       tag_type = cp_parser_class_key (parser);
12970       if (tag_type == none_type)
12971         return error_mark_node;
12972       /* Parse the attributes.  */
12973       attributes = cp_parser_attributes_opt (parser);
12974     }
12975
12976   /* Look for the `::' operator.  */
12977   globalscope =  cp_parser_global_scope_opt (parser,
12978                                              /*current_scope_valid_p=*/false);
12979   /* Look for the nested-name-specifier.  */
12980   if (tag_type == typename_type && !globalscope)
12981     {
12982       if (!cp_parser_nested_name_specifier (parser,
12983                                            /*typename_keyword_p=*/true,
12984                                            /*check_dependency_p=*/true,
12985                                            /*type_p=*/true,
12986                                             is_declaration))
12987         return error_mark_node;
12988     }
12989   else
12990     /* Even though `typename' is not present, the proposed resolution
12991        to Core Issue 180 says that in `class A<T>::B', `B' should be
12992        considered a type-name, even if `A<T>' is dependent.  */
12993     cp_parser_nested_name_specifier_opt (parser,
12994                                          /*typename_keyword_p=*/true,
12995                                          /*check_dependency_p=*/true,
12996                                          /*type_p=*/true,
12997                                          is_declaration);
12998  /* For everything but enumeration types, consider a template-id.
12999     For an enumeration type, consider only a plain identifier.  */
13000   if (tag_type != enum_type)
13001     {
13002       bool template_p = false;
13003       tree decl;
13004
13005       /* Allow the `template' keyword.  */
13006       template_p = cp_parser_optional_template_keyword (parser);
13007       /* If we didn't see `template', we don't know if there's a
13008          template-id or not.  */
13009       if (!template_p)
13010         cp_parser_parse_tentatively (parser);
13011       /* Parse the template-id.  */
13012       token = cp_lexer_peek_token (parser->lexer);
13013       decl = cp_parser_template_id (parser, template_p,
13014                                     /*check_dependency_p=*/true,
13015                                     is_declaration);
13016       /* If we didn't find a template-id, look for an ordinary
13017          identifier.  */
13018       if (!template_p && !cp_parser_parse_definitely (parser))
13019         ;
13020       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13021          in effect, then we must assume that, upon instantiation, the
13022          template will correspond to a class.  */
13023       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13024                && tag_type == typename_type)
13025         type = make_typename_type (parser->scope, decl,
13026                                    typename_type,
13027                                    /*complain=*/tf_error);
13028       /* If the `typename' keyword is in effect and DECL is not a type
13029          decl. Then type is non existant.   */
13030       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13031         type = NULL_TREE; 
13032       else 
13033         type = TREE_TYPE (decl);
13034     }
13035
13036   if (!type)
13037     {
13038       token = cp_lexer_peek_token (parser->lexer);
13039       identifier = cp_parser_identifier (parser);
13040
13041       if (identifier == error_mark_node)
13042         {
13043           parser->scope = NULL_TREE;
13044           return error_mark_node;
13045         }
13046
13047       /* For a `typename', we needn't call xref_tag.  */
13048       if (tag_type == typename_type
13049           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13050         return cp_parser_make_typename_type (parser, parser->scope,
13051                                              identifier,
13052                                              token->location);
13053       /* Look up a qualified name in the usual way.  */
13054       if (parser->scope)
13055         {
13056           tree decl;
13057           tree ambiguous_decls;
13058
13059           decl = cp_parser_lookup_name (parser, identifier,
13060                                         tag_type,
13061                                         /*is_template=*/false,
13062                                         /*is_namespace=*/false,
13063                                         /*check_dependency=*/true,
13064                                         &ambiguous_decls,
13065                                         token->location);
13066
13067           /* If the lookup was ambiguous, an error will already have been
13068              issued.  */
13069           if (ambiguous_decls)
13070             return error_mark_node;
13071
13072           /* If we are parsing friend declaration, DECL may be a
13073              TEMPLATE_DECL tree node here.  However, we need to check
13074              whether this TEMPLATE_DECL results in valid code.  Consider
13075              the following example:
13076
13077                namespace N {
13078                  template <class T> class C {};
13079                }
13080                class X {
13081                  template <class T> friend class N::C; // #1, valid code
13082                };
13083                template <class T> class Y {
13084                  friend class N::C;                    // #2, invalid code
13085                };
13086
13087              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13088              name lookup of `N::C'.  We see that friend declaration must
13089              be template for the code to be valid.  Note that
13090              processing_template_decl does not work here since it is
13091              always 1 for the above two cases.  */
13092
13093           decl = (cp_parser_maybe_treat_template_as_class
13094                   (decl, /*tag_name_p=*/is_friend
13095                          && parser->num_template_parameter_lists));
13096
13097           if (TREE_CODE (decl) != TYPE_DECL)
13098             {
13099               cp_parser_diagnose_invalid_type_name (parser,
13100                                                     parser->scope,
13101                                                     identifier,
13102                                                     token->location);
13103               return error_mark_node;
13104             }
13105
13106           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13107             {
13108               bool allow_template = (parser->num_template_parameter_lists
13109                                       || DECL_SELF_REFERENCE_P (decl));
13110               type = check_elaborated_type_specifier (tag_type, decl, 
13111                                                       allow_template);
13112
13113               if (type == error_mark_node)
13114                 return error_mark_node;
13115             }
13116
13117           /* Forward declarations of nested types, such as
13118
13119                class C1::C2;
13120                class C1::C2::C3;
13121
13122              are invalid unless all components preceding the final '::'
13123              are complete.  If all enclosing types are complete, these
13124              declarations become merely pointless.
13125
13126              Invalid forward declarations of nested types are errors
13127              caught elsewhere in parsing.  Those that are pointless arrive
13128              here.  */
13129
13130           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13131               && !is_friend && !processing_explicit_instantiation)
13132             warning (0, "declaration %qD does not declare anything", decl);
13133
13134           type = TREE_TYPE (decl);
13135         }
13136       else
13137         {
13138           /* An elaborated-type-specifier sometimes introduces a new type and
13139              sometimes names an existing type.  Normally, the rule is that it
13140              introduces a new type only if there is not an existing type of
13141              the same name already in scope.  For example, given:
13142
13143                struct S {};
13144                void f() { struct S s; }
13145
13146              the `struct S' in the body of `f' is the same `struct S' as in
13147              the global scope; the existing definition is used.  However, if
13148              there were no global declaration, this would introduce a new
13149              local class named `S'.
13150
13151              An exception to this rule applies to the following code:
13152
13153                namespace N { struct S; }
13154
13155              Here, the elaborated-type-specifier names a new type
13156              unconditionally; even if there is already an `S' in the
13157              containing scope this declaration names a new type.
13158              This exception only applies if the elaborated-type-specifier
13159              forms the complete declaration:
13160
13161                [class.name]
13162
13163                A declaration consisting solely of `class-key identifier ;' is
13164                either a redeclaration of the name in the current scope or a
13165                forward declaration of the identifier as a class name.  It
13166                introduces the name into the current scope.
13167
13168              We are in this situation precisely when the next token is a `;'.
13169
13170              An exception to the exception is that a `friend' declaration does
13171              *not* name a new type; i.e., given:
13172
13173                struct S { friend struct T; };
13174
13175              `T' is not a new type in the scope of `S'.
13176
13177              Also, `new struct S' or `sizeof (struct S)' never results in the
13178              definition of a new type; a new type can only be declared in a
13179              declaration context.  */
13180
13181           tag_scope ts;
13182           bool template_p;
13183
13184           if (is_friend)
13185             /* Friends have special name lookup rules.  */
13186             ts = ts_within_enclosing_non_class;
13187           else if (is_declaration
13188                    && cp_lexer_next_token_is (parser->lexer,
13189                                               CPP_SEMICOLON))
13190             /* This is a `class-key identifier ;' */
13191             ts = ts_current;
13192           else
13193             ts = ts_global;
13194
13195           template_p =
13196             (parser->num_template_parameter_lists
13197              && (cp_parser_next_token_starts_class_definition_p (parser)
13198                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13199           /* An unqualified name was used to reference this type, so
13200              there were no qualifying templates.  */
13201           if (!cp_parser_check_template_parameters (parser,
13202                                                     /*num_templates=*/0,
13203                                                     token->location,
13204                                                     /*declarator=*/NULL))
13205             return error_mark_node;
13206           type = xref_tag (tag_type, identifier, ts, template_p);
13207         }
13208     }
13209
13210   if (type == error_mark_node)
13211     return error_mark_node;
13212
13213   /* Allow attributes on forward declarations of classes.  */
13214   if (attributes)
13215     {
13216       if (TREE_CODE (type) == TYPENAME_TYPE)
13217         warning (OPT_Wattributes,
13218                  "attributes ignored on uninstantiated type");
13219       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13220                && ! processing_explicit_instantiation)
13221         warning (OPT_Wattributes,
13222                  "attributes ignored on template instantiation");
13223       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13224         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13225       else
13226         warning (OPT_Wattributes,
13227                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13228     }
13229
13230   if (tag_type != enum_type)
13231     cp_parser_check_class_key (tag_type, type);
13232
13233   /* A "<" cannot follow an elaborated type specifier.  If that
13234      happens, the user was probably trying to form a template-id.  */
13235   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13236
13237   return type;
13238 }
13239
13240 /* Parse an enum-specifier.
13241
13242    enum-specifier:
13243      enum-head { enumerator-list [opt] }
13244
13245    enum-head:
13246      enum-key identifier [opt] enum-base [opt]
13247      enum-key nested-name-specifier identifier enum-base [opt]
13248
13249    enum-key:
13250      enum
13251      enum class   [C++0x]
13252      enum struct  [C++0x]
13253
13254    enum-base:   [C++0x]
13255      : type-specifier-seq
13256
13257    opaque-enum-specifier:
13258      enum-key identifier enum-base [opt] ;
13259
13260    GNU Extensions:
13261      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13262        { enumerator-list [opt] }attributes[opt]
13263
13264    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13265    if the token stream isn't an enum-specifier after all.  */
13266
13267 static tree
13268 cp_parser_enum_specifier (cp_parser* parser)
13269 {
13270   tree identifier;
13271   tree type = NULL_TREE;
13272   tree prev_scope;
13273   tree nested_name_specifier = NULL_TREE;
13274   tree attributes;
13275   bool scoped_enum_p = false;
13276   bool has_underlying_type = false;
13277   bool nested_being_defined = false;
13278   bool new_value_list = false;
13279   bool is_new_type = false;
13280   bool is_anonymous = false;
13281   tree underlying_type = NULL_TREE;
13282   cp_token *type_start_token = NULL;
13283   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13284
13285   parser->colon_corrects_to_scope_p = false;
13286
13287   /* Parse tentatively so that we can back up if we don't find a
13288      enum-specifier.  */
13289   cp_parser_parse_tentatively (parser);
13290
13291   /* Caller guarantees that the current token is 'enum', an identifier
13292      possibly follows, and the token after that is an opening brace.
13293      If we don't have an identifier, fabricate an anonymous name for
13294      the enumeration being defined.  */
13295   cp_lexer_consume_token (parser->lexer);
13296
13297   /* Parse the "class" or "struct", which indicates a scoped
13298      enumeration type in C++0x.  */
13299   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13300       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13301     {
13302       if (cxx_dialect < cxx0x)
13303         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13304
13305       /* Consume the `struct' or `class' token.  */
13306       cp_lexer_consume_token (parser->lexer);
13307
13308       scoped_enum_p = true;
13309     }
13310
13311   attributes = cp_parser_attributes_opt (parser);
13312
13313   /* Clear the qualification.  */
13314   parser->scope = NULL_TREE;
13315   parser->qualifying_scope = NULL_TREE;
13316   parser->object_scope = NULL_TREE;
13317
13318   /* Figure out in what scope the declaration is being placed.  */
13319   prev_scope = current_scope ();
13320
13321   type_start_token = cp_lexer_peek_token (parser->lexer);
13322
13323   push_deferring_access_checks (dk_no_check);
13324   nested_name_specifier
13325       = cp_parser_nested_name_specifier_opt (parser,
13326                                              /*typename_keyword_p=*/true,
13327                                              /*check_dependency_p=*/false,
13328                                              /*type_p=*/false,
13329                                              /*is_declaration=*/false);
13330
13331   if (nested_name_specifier)
13332     {
13333       tree name;
13334
13335       identifier = cp_parser_identifier (parser);
13336       name =  cp_parser_lookup_name (parser, identifier,
13337                                      enum_type,
13338                                      /*is_template=*/false,
13339                                      /*is_namespace=*/false,
13340                                      /*check_dependency=*/true,
13341                                      /*ambiguous_decls=*/NULL,
13342                                      input_location);
13343       if (name)
13344         {
13345           type = TREE_TYPE (name);
13346           if (TREE_CODE (type) == TYPENAME_TYPE)
13347             {
13348               /* Are template enums allowed in ISO? */
13349               if (template_parm_scope_p ())
13350                 pedwarn (type_start_token->location, OPT_pedantic,
13351                          "%qD is an enumeration template", name);
13352               /* ignore a typename reference, for it will be solved by name
13353                  in start_enum.  */
13354               type = NULL_TREE;
13355             }
13356         }
13357       else
13358         error_at (type_start_token->location,
13359                   "%qD is not an enumerator-name", identifier);
13360     }
13361   else
13362     {
13363       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13364         identifier = cp_parser_identifier (parser);
13365       else
13366         {
13367           identifier = make_anon_name ();
13368           is_anonymous = true;
13369         }
13370     }
13371   pop_deferring_access_checks ();
13372
13373   /* Check for the `:' that denotes a specified underlying type in C++0x.
13374      Note that a ':' could also indicate a bitfield width, however.  */
13375   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13376     {
13377       cp_decl_specifier_seq type_specifiers;
13378
13379       /* Consume the `:'.  */
13380       cp_lexer_consume_token (parser->lexer);
13381
13382       /* Parse the type-specifier-seq.  */
13383       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13384                                     /*is_trailing_return=*/false,
13385                                     &type_specifiers);
13386
13387       /* At this point this is surely not elaborated type specifier.  */
13388       if (!cp_parser_parse_definitely (parser))
13389         return NULL_TREE;
13390
13391       if (cxx_dialect < cxx0x)
13392         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13393
13394       has_underlying_type = true;
13395
13396       /* If that didn't work, stop.  */
13397       if (type_specifiers.type != error_mark_node)
13398         {
13399           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13400                                             /*initialized=*/0, NULL);
13401           if (underlying_type == error_mark_node)
13402             underlying_type = NULL_TREE;
13403         }
13404     }
13405
13406   /* Look for the `{' but don't consume it yet.  */
13407   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13408     {
13409       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13410         {
13411           cp_parser_error (parser, "expected %<{%>");
13412           if (has_underlying_type)
13413             {
13414               type = NULL_TREE;
13415               goto out;
13416             }
13417         }
13418       /* An opaque-enum-specifier must have a ';' here.  */
13419       if ((scoped_enum_p || underlying_type)
13420           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13421         {
13422           cp_parser_error (parser, "expected %<;%> or %<{%>");
13423           if (has_underlying_type)
13424             {
13425               type = NULL_TREE;
13426               goto out;
13427             }
13428         }
13429     }
13430
13431   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13432     return NULL_TREE;
13433
13434   if (nested_name_specifier)
13435     {
13436       if (CLASS_TYPE_P (nested_name_specifier))
13437         {
13438           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13439           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13440           push_scope (nested_name_specifier);
13441         }
13442       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13443         {
13444           push_nested_namespace (nested_name_specifier);
13445         }
13446     }
13447
13448   /* Issue an error message if type-definitions are forbidden here.  */
13449   if (!cp_parser_check_type_definition (parser))
13450     type = error_mark_node;
13451   else
13452     /* Create the new type.  We do this before consuming the opening
13453        brace so the enum will be recorded as being on the line of its
13454        tag (or the 'enum' keyword, if there is no tag).  */
13455     type = start_enum (identifier, type, underlying_type,
13456                        scoped_enum_p, &is_new_type);
13457
13458   /* If the next token is not '{' it is an opaque-enum-specifier or an
13459      elaborated-type-specifier.  */
13460   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13461     {
13462       timevar_push (TV_PARSE_ENUM);
13463       if (nested_name_specifier)
13464         {
13465           /* The following catches invalid code such as:
13466              enum class S<int>::E { A, B, C }; */
13467           if (!processing_specialization
13468               && CLASS_TYPE_P (nested_name_specifier)
13469               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13470             error_at (type_start_token->location, "cannot add an enumerator "
13471                       "list to a template instantiation");
13472
13473           /* If that scope does not contain the scope in which the
13474              class was originally declared, the program is invalid.  */
13475           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13476             {
13477               if (at_namespace_scope_p ())
13478                 error_at (type_start_token->location,
13479                           "declaration of %qD in namespace %qD which does not "
13480                           "enclose %qD",
13481                           type, prev_scope, nested_name_specifier);
13482               else
13483                 error_at (type_start_token->location,
13484                           "declaration of %qD in %qD which does not enclose %qD",
13485                           type, prev_scope, nested_name_specifier);
13486               type = error_mark_node;
13487             }
13488         }
13489
13490       if (scoped_enum_p)
13491         begin_scope (sk_scoped_enum, type);
13492
13493       /* Consume the opening brace.  */
13494       cp_lexer_consume_token (parser->lexer);
13495
13496       if (type == error_mark_node)
13497         ; /* Nothing to add */
13498       else if (OPAQUE_ENUM_P (type)
13499                || (cxx_dialect > cxx98 && processing_specialization))
13500         {
13501           new_value_list = true;
13502           SET_OPAQUE_ENUM_P (type, false);
13503           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13504         }
13505       else
13506         {
13507           error_at (type_start_token->location, "multiple definition of %q#T", type);
13508           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13509                     "previous definition here");
13510           type = error_mark_node;
13511         }
13512
13513       if (type == error_mark_node)
13514         cp_parser_skip_to_end_of_block_or_statement (parser);
13515       /* If the next token is not '}', then there are some enumerators.  */
13516       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13517         cp_parser_enumerator_list (parser, type);
13518
13519       /* Consume the final '}'.  */
13520       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13521
13522       if (scoped_enum_p)
13523         finish_scope ();
13524       timevar_pop (TV_PARSE_ENUM);
13525     }
13526   else
13527     {
13528       /* If a ';' follows, then it is an opaque-enum-specifier
13529         and additional restrictions apply.  */
13530       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13531         {
13532           if (is_anonymous)
13533             error_at (type_start_token->location,
13534                       "opaque-enum-specifier without name");
13535           else if (nested_name_specifier)
13536             error_at (type_start_token->location,
13537                       "opaque-enum-specifier must use a simple identifier");
13538         }
13539     }
13540
13541   /* Look for trailing attributes to apply to this enumeration, and
13542      apply them if appropriate.  */
13543   if (cp_parser_allow_gnu_extensions_p (parser))
13544     {
13545       tree trailing_attr = cp_parser_attributes_opt (parser);
13546       trailing_attr = chainon (trailing_attr, attributes);
13547       cplus_decl_attributes (&type,
13548                              trailing_attr,
13549                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13550     }
13551
13552   /* Finish up the enumeration.  */
13553   if (type != error_mark_node)
13554     {
13555       if (new_value_list)
13556         finish_enum_value_list (type);
13557       if (is_new_type)
13558         finish_enum (type);
13559     }
13560
13561   if (nested_name_specifier)
13562     {
13563       if (CLASS_TYPE_P (nested_name_specifier))
13564         {
13565           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13566           pop_scope (nested_name_specifier);
13567         }
13568       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13569         {
13570           pop_nested_namespace (nested_name_specifier);
13571         }
13572     }
13573  out:
13574   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13575   return type;
13576 }
13577
13578 /* Parse an enumerator-list.  The enumerators all have the indicated
13579    TYPE.
13580
13581    enumerator-list:
13582      enumerator-definition
13583      enumerator-list , enumerator-definition  */
13584
13585 static void
13586 cp_parser_enumerator_list (cp_parser* parser, tree type)
13587 {
13588   while (true)
13589     {
13590       /* Parse an enumerator-definition.  */
13591       cp_parser_enumerator_definition (parser, type);
13592
13593       /* If the next token is not a ',', we've reached the end of
13594          the list.  */
13595       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13596         break;
13597       /* Otherwise, consume the `,' and keep going.  */
13598       cp_lexer_consume_token (parser->lexer);
13599       /* If the next token is a `}', there is a trailing comma.  */
13600       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13601         {
13602           if (!in_system_header)
13603             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13604           break;
13605         }
13606     }
13607 }
13608
13609 /* Parse an enumerator-definition.  The enumerator has the indicated
13610    TYPE.
13611
13612    enumerator-definition:
13613      enumerator
13614      enumerator = constant-expression
13615
13616    enumerator:
13617      identifier  */
13618
13619 static void
13620 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13621 {
13622   tree identifier;
13623   tree value;
13624   location_t loc;
13625
13626   /* Save the input location because we are interested in the location
13627      of the identifier and not the location of the explicit value.  */
13628   loc = cp_lexer_peek_token (parser->lexer)->location;
13629
13630   /* Look for the identifier.  */
13631   identifier = cp_parser_identifier (parser);
13632   if (identifier == error_mark_node)
13633     return;
13634
13635   /* If the next token is an '=', then there is an explicit value.  */
13636   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13637     {
13638       /* Consume the `=' token.  */
13639       cp_lexer_consume_token (parser->lexer);
13640       /* Parse the value.  */
13641       value = cp_parser_constant_expression (parser,
13642                                              /*allow_non_constant_p=*/false,
13643                                              NULL);
13644     }
13645   else
13646     value = NULL_TREE;
13647
13648   /* If we are processing a template, make sure the initializer of the
13649      enumerator doesn't contain any bare template parameter pack.  */
13650   if (check_for_bare_parameter_packs (value))
13651     value = error_mark_node;
13652
13653   /* integral_constant_value will pull out this expression, so make sure
13654      it's folded as appropriate.  */
13655   value = fold_non_dependent_expr (value);
13656
13657   /* Create the enumerator.  */
13658   build_enumerator (identifier, value, type, loc);
13659 }
13660
13661 /* Parse a namespace-name.
13662
13663    namespace-name:
13664      original-namespace-name
13665      namespace-alias
13666
13667    Returns the NAMESPACE_DECL for the namespace.  */
13668
13669 static tree
13670 cp_parser_namespace_name (cp_parser* parser)
13671 {
13672   tree identifier;
13673   tree namespace_decl;
13674
13675   cp_token *token = cp_lexer_peek_token (parser->lexer);
13676
13677   /* Get the name of the namespace.  */
13678   identifier = cp_parser_identifier (parser);
13679   if (identifier == error_mark_node)
13680     return error_mark_node;
13681
13682   /* Look up the identifier in the currently active scope.  Look only
13683      for namespaces, due to:
13684
13685        [basic.lookup.udir]
13686
13687        When looking up a namespace-name in a using-directive or alias
13688        definition, only namespace names are considered.
13689
13690      And:
13691
13692        [basic.lookup.qual]
13693
13694        During the lookup of a name preceding the :: scope resolution
13695        operator, object, function, and enumerator names are ignored.
13696
13697      (Note that cp_parser_qualifying_entity only calls this
13698      function if the token after the name is the scope resolution
13699      operator.)  */
13700   namespace_decl = cp_parser_lookup_name (parser, identifier,
13701                                           none_type,
13702                                           /*is_template=*/false,
13703                                           /*is_namespace=*/true,
13704                                           /*check_dependency=*/true,
13705                                           /*ambiguous_decls=*/NULL,
13706                                           token->location);
13707   /* If it's not a namespace, issue an error.  */
13708   if (namespace_decl == error_mark_node
13709       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13710     {
13711       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13712         error_at (token->location, "%qD is not a namespace-name", identifier);
13713       cp_parser_error (parser, "expected namespace-name");
13714       namespace_decl = error_mark_node;
13715     }
13716
13717   return namespace_decl;
13718 }
13719
13720 /* Parse a namespace-definition.
13721
13722    namespace-definition:
13723      named-namespace-definition
13724      unnamed-namespace-definition
13725
13726    named-namespace-definition:
13727      original-namespace-definition
13728      extension-namespace-definition
13729
13730    original-namespace-definition:
13731      namespace identifier { namespace-body }
13732
13733    extension-namespace-definition:
13734      namespace original-namespace-name { namespace-body }
13735
13736    unnamed-namespace-definition:
13737      namespace { namespace-body } */
13738
13739 static void
13740 cp_parser_namespace_definition (cp_parser* parser)
13741 {
13742   tree identifier, attribs;
13743   bool has_visibility;
13744   bool is_inline;
13745
13746   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13747     {
13748       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13749       is_inline = true;
13750       cp_lexer_consume_token (parser->lexer);
13751     }
13752   else
13753     is_inline = false;
13754
13755   /* Look for the `namespace' keyword.  */
13756   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13757
13758   /* Get the name of the namespace.  We do not attempt to distinguish
13759      between an original-namespace-definition and an
13760      extension-namespace-definition at this point.  The semantic
13761      analysis routines are responsible for that.  */
13762   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13763     identifier = cp_parser_identifier (parser);
13764   else
13765     identifier = NULL_TREE;
13766
13767   /* Parse any specified attributes.  */
13768   attribs = cp_parser_attributes_opt (parser);
13769
13770   /* Look for the `{' to start the namespace.  */
13771   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13772   /* Start the namespace.  */
13773   push_namespace (identifier);
13774
13775   /* "inline namespace" is equivalent to a stub namespace definition
13776      followed by a strong using directive.  */
13777   if (is_inline)
13778     {
13779       tree name_space = current_namespace;
13780       /* Set up namespace association.  */
13781       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13782         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13783                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13784       /* Import the contents of the inline namespace.  */
13785       pop_namespace ();
13786       do_using_directive (name_space);
13787       push_namespace (identifier);
13788     }
13789
13790   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13791
13792   /* Parse the body of the namespace.  */
13793   cp_parser_namespace_body (parser);
13794
13795   if (has_visibility)
13796     pop_visibility (1);
13797
13798   /* Finish the namespace.  */
13799   pop_namespace ();
13800   /* Look for the final `}'.  */
13801   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13802 }
13803
13804 /* Parse a namespace-body.
13805
13806    namespace-body:
13807      declaration-seq [opt]  */
13808
13809 static void
13810 cp_parser_namespace_body (cp_parser* parser)
13811 {
13812   cp_parser_declaration_seq_opt (parser);
13813 }
13814
13815 /* Parse a namespace-alias-definition.
13816
13817    namespace-alias-definition:
13818      namespace identifier = qualified-namespace-specifier ;  */
13819
13820 static void
13821 cp_parser_namespace_alias_definition (cp_parser* parser)
13822 {
13823   tree identifier;
13824   tree namespace_specifier;
13825
13826   cp_token *token = cp_lexer_peek_token (parser->lexer);
13827
13828   /* Look for the `namespace' keyword.  */
13829   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13830   /* Look for the identifier.  */
13831   identifier = cp_parser_identifier (parser);
13832   if (identifier == error_mark_node)
13833     return;
13834   /* Look for the `=' token.  */
13835   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13836       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13837     {
13838       error_at (token->location, "%<namespace%> definition is not allowed here");
13839       /* Skip the definition.  */
13840       cp_lexer_consume_token (parser->lexer);
13841       if (cp_parser_skip_to_closing_brace (parser))
13842         cp_lexer_consume_token (parser->lexer);
13843       return;
13844     }
13845   cp_parser_require (parser, CPP_EQ, RT_EQ);
13846   /* Look for the qualified-namespace-specifier.  */
13847   namespace_specifier
13848     = cp_parser_qualified_namespace_specifier (parser);
13849   /* Look for the `;' token.  */
13850   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13851
13852   /* Register the alias in the symbol table.  */
13853   do_namespace_alias (identifier, namespace_specifier);
13854 }
13855
13856 /* Parse a qualified-namespace-specifier.
13857
13858    qualified-namespace-specifier:
13859      :: [opt] nested-name-specifier [opt] namespace-name
13860
13861    Returns a NAMESPACE_DECL corresponding to the specified
13862    namespace.  */
13863
13864 static tree
13865 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13866 {
13867   /* Look for the optional `::'.  */
13868   cp_parser_global_scope_opt (parser,
13869                               /*current_scope_valid_p=*/false);
13870
13871   /* Look for the optional nested-name-specifier.  */
13872   cp_parser_nested_name_specifier_opt (parser,
13873                                        /*typename_keyword_p=*/false,
13874                                        /*check_dependency_p=*/true,
13875                                        /*type_p=*/false,
13876                                        /*is_declaration=*/true);
13877
13878   return cp_parser_namespace_name (parser);
13879 }
13880
13881 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13882    access declaration.
13883
13884    using-declaration:
13885      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13886      using :: unqualified-id ;  
13887
13888    access-declaration:
13889      qualified-id ;  
13890
13891    */
13892
13893 static bool
13894 cp_parser_using_declaration (cp_parser* parser, 
13895                              bool access_declaration_p)
13896 {
13897   cp_token *token;
13898   bool typename_p = false;
13899   bool global_scope_p;
13900   tree decl;
13901   tree identifier;
13902   tree qscope;
13903
13904   if (access_declaration_p)
13905     cp_parser_parse_tentatively (parser);
13906   else
13907     {
13908       /* Look for the `using' keyword.  */
13909       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13910       
13911       /* Peek at the next token.  */
13912       token = cp_lexer_peek_token (parser->lexer);
13913       /* See if it's `typename'.  */
13914       if (token->keyword == RID_TYPENAME)
13915         {
13916           /* Remember that we've seen it.  */
13917           typename_p = true;
13918           /* Consume the `typename' token.  */
13919           cp_lexer_consume_token (parser->lexer);
13920         }
13921     }
13922
13923   /* Look for the optional global scope qualification.  */
13924   global_scope_p
13925     = (cp_parser_global_scope_opt (parser,
13926                                    /*current_scope_valid_p=*/false)
13927        != NULL_TREE);
13928
13929   /* If we saw `typename', or didn't see `::', then there must be a
13930      nested-name-specifier present.  */
13931   if (typename_p || !global_scope_p)
13932     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13933                                               /*check_dependency_p=*/true,
13934                                               /*type_p=*/false,
13935                                               /*is_declaration=*/true);
13936   /* Otherwise, we could be in either of the two productions.  In that
13937      case, treat the nested-name-specifier as optional.  */
13938   else
13939     qscope = cp_parser_nested_name_specifier_opt (parser,
13940                                                   /*typename_keyword_p=*/false,
13941                                                   /*check_dependency_p=*/true,
13942                                                   /*type_p=*/false,
13943                                                   /*is_declaration=*/true);
13944   if (!qscope)
13945     qscope = global_namespace;
13946
13947   if (access_declaration_p && cp_parser_error_occurred (parser))
13948     /* Something has already gone wrong; there's no need to parse
13949        further.  Since an error has occurred, the return value of
13950        cp_parser_parse_definitely will be false, as required.  */
13951     return cp_parser_parse_definitely (parser);
13952
13953   token = cp_lexer_peek_token (parser->lexer);
13954   /* Parse the unqualified-id.  */
13955   identifier = cp_parser_unqualified_id (parser,
13956                                          /*template_keyword_p=*/false,
13957                                          /*check_dependency_p=*/true,
13958                                          /*declarator_p=*/true,
13959                                          /*optional_p=*/false);
13960
13961   if (access_declaration_p)
13962     {
13963       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13964         cp_parser_simulate_error (parser);
13965       if (!cp_parser_parse_definitely (parser))
13966         return false;
13967     }
13968
13969   /* The function we call to handle a using-declaration is different
13970      depending on what scope we are in.  */
13971   if (qscope == error_mark_node || identifier == error_mark_node)
13972     ;
13973   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13974            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13975     /* [namespace.udecl]
13976
13977        A using declaration shall not name a template-id.  */
13978     error_at (token->location,
13979               "a template-id may not appear in a using-declaration");
13980   else
13981     {
13982       if (at_class_scope_p ())
13983         {
13984           /* Create the USING_DECL.  */
13985           decl = do_class_using_decl (parser->scope, identifier);
13986
13987           if (check_for_bare_parameter_packs (decl))
13988             return false;
13989           else
13990             /* Add it to the list of members in this class.  */
13991             finish_member_declaration (decl);
13992         }
13993       else
13994         {
13995           decl = cp_parser_lookup_name_simple (parser,
13996                                                identifier,
13997                                                token->location);
13998           if (decl == error_mark_node)
13999             cp_parser_name_lookup_error (parser, identifier,
14000                                          decl, NLE_NULL,
14001                                          token->location);
14002           else if (check_for_bare_parameter_packs (decl))
14003             return false;
14004           else if (!at_namespace_scope_p ())
14005             do_local_using_decl (decl, qscope, identifier);
14006           else
14007             do_toplevel_using_decl (decl, qscope, identifier);
14008         }
14009     }
14010
14011   /* Look for the final `;'.  */
14012   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14013   
14014   return true;
14015 }
14016
14017 /* Parse a using-directive.
14018
14019    using-directive:
14020      using namespace :: [opt] nested-name-specifier [opt]
14021        namespace-name ;  */
14022
14023 static void
14024 cp_parser_using_directive (cp_parser* parser)
14025 {
14026   tree namespace_decl;
14027   tree attribs;
14028
14029   /* Look for the `using' keyword.  */
14030   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14031   /* And the `namespace' keyword.  */
14032   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14033   /* Look for the optional `::' operator.  */
14034   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14035   /* And the optional nested-name-specifier.  */
14036   cp_parser_nested_name_specifier_opt (parser,
14037                                        /*typename_keyword_p=*/false,
14038                                        /*check_dependency_p=*/true,
14039                                        /*type_p=*/false,
14040                                        /*is_declaration=*/true);
14041   /* Get the namespace being used.  */
14042   namespace_decl = cp_parser_namespace_name (parser);
14043   /* And any specified attributes.  */
14044   attribs = cp_parser_attributes_opt (parser);
14045   /* Update the symbol table.  */
14046   parse_using_directive (namespace_decl, attribs);
14047   /* Look for the final `;'.  */
14048   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14049 }
14050
14051 /* Parse an asm-definition.
14052
14053    asm-definition:
14054      asm ( string-literal ) ;
14055
14056    GNU Extension:
14057
14058    asm-definition:
14059      asm volatile [opt] ( string-literal ) ;
14060      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14061      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14062                           : asm-operand-list [opt] ) ;
14063      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14064                           : asm-operand-list [opt]
14065                           : asm-clobber-list [opt] ) ;
14066      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14067                                : asm-clobber-list [opt]
14068                                : asm-goto-list ) ;  */
14069
14070 static void
14071 cp_parser_asm_definition (cp_parser* parser)
14072 {
14073   tree string;
14074   tree outputs = NULL_TREE;
14075   tree inputs = NULL_TREE;
14076   tree clobbers = NULL_TREE;
14077   tree labels = NULL_TREE;
14078   tree asm_stmt;
14079   bool volatile_p = false;
14080   bool extended_p = false;
14081   bool invalid_inputs_p = false;
14082   bool invalid_outputs_p = false;
14083   bool goto_p = false;
14084   required_token missing = RT_NONE;
14085
14086   /* Look for the `asm' keyword.  */
14087   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14088   /* See if the next token is `volatile'.  */
14089   if (cp_parser_allow_gnu_extensions_p (parser)
14090       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14091     {
14092       /* Remember that we saw the `volatile' keyword.  */
14093       volatile_p = true;
14094       /* Consume the token.  */
14095       cp_lexer_consume_token (parser->lexer);
14096     }
14097   if (cp_parser_allow_gnu_extensions_p (parser)
14098       && parser->in_function_body
14099       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14100     {
14101       /* Remember that we saw the `goto' keyword.  */
14102       goto_p = true;
14103       /* Consume the token.  */
14104       cp_lexer_consume_token (parser->lexer);
14105     }
14106   /* Look for the opening `('.  */
14107   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14108     return;
14109   /* Look for the string.  */
14110   string = cp_parser_string_literal (parser, false, false);
14111   if (string == error_mark_node)
14112     {
14113       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14114                                              /*consume_paren=*/true);
14115       return;
14116     }
14117
14118   /* If we're allowing GNU extensions, check for the extended assembly
14119      syntax.  Unfortunately, the `:' tokens need not be separated by
14120      a space in C, and so, for compatibility, we tolerate that here
14121      too.  Doing that means that we have to treat the `::' operator as
14122      two `:' tokens.  */
14123   if (cp_parser_allow_gnu_extensions_p (parser)
14124       && parser->in_function_body
14125       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14126           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14127     {
14128       bool inputs_p = false;
14129       bool clobbers_p = false;
14130       bool labels_p = false;
14131
14132       /* The extended syntax was used.  */
14133       extended_p = true;
14134
14135       /* Look for outputs.  */
14136       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14137         {
14138           /* Consume the `:'.  */
14139           cp_lexer_consume_token (parser->lexer);
14140           /* Parse the output-operands.  */
14141           if (cp_lexer_next_token_is_not (parser->lexer,
14142                                           CPP_COLON)
14143               && cp_lexer_next_token_is_not (parser->lexer,
14144                                              CPP_SCOPE)
14145               && cp_lexer_next_token_is_not (parser->lexer,
14146                                              CPP_CLOSE_PAREN)
14147               && !goto_p)
14148             outputs = cp_parser_asm_operand_list (parser);
14149
14150             if (outputs == error_mark_node)
14151               invalid_outputs_p = true;
14152         }
14153       /* If the next token is `::', there are no outputs, and the
14154          next token is the beginning of the inputs.  */
14155       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14156         /* The inputs are coming next.  */
14157         inputs_p = true;
14158
14159       /* Look for inputs.  */
14160       if (inputs_p
14161           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14162         {
14163           /* Consume the `:' or `::'.  */
14164           cp_lexer_consume_token (parser->lexer);
14165           /* Parse the output-operands.  */
14166           if (cp_lexer_next_token_is_not (parser->lexer,
14167                                           CPP_COLON)
14168               && cp_lexer_next_token_is_not (parser->lexer,
14169                                              CPP_SCOPE)
14170               && cp_lexer_next_token_is_not (parser->lexer,
14171                                              CPP_CLOSE_PAREN))
14172             inputs = cp_parser_asm_operand_list (parser);
14173
14174             if (inputs == error_mark_node)
14175               invalid_inputs_p = true;
14176         }
14177       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14178         /* The clobbers are coming next.  */
14179         clobbers_p = true;
14180
14181       /* Look for clobbers.  */
14182       if (clobbers_p
14183           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14184         {
14185           clobbers_p = true;
14186           /* Consume the `:' or `::'.  */
14187           cp_lexer_consume_token (parser->lexer);
14188           /* Parse the clobbers.  */
14189           if (cp_lexer_next_token_is_not (parser->lexer,
14190                                           CPP_COLON)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_CLOSE_PAREN))
14193             clobbers = cp_parser_asm_clobber_list (parser);
14194         }
14195       else if (goto_p
14196                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14197         /* The labels are coming next.  */
14198         labels_p = true;
14199
14200       /* Look for labels.  */
14201       if (labels_p
14202           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14203         {
14204           labels_p = true;
14205           /* Consume the `:' or `::'.  */
14206           cp_lexer_consume_token (parser->lexer);
14207           /* Parse the labels.  */
14208           labels = cp_parser_asm_label_list (parser);
14209         }
14210
14211       if (goto_p && !labels_p)
14212         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14213     }
14214   else if (goto_p)
14215     missing = RT_COLON_SCOPE;
14216
14217   /* Look for the closing `)'.  */
14218   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14219                           missing ? missing : RT_CLOSE_PAREN))
14220     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14221                                            /*consume_paren=*/true);
14222   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14223
14224   if (!invalid_inputs_p && !invalid_outputs_p)
14225     {
14226       /* Create the ASM_EXPR.  */
14227       if (parser->in_function_body)
14228         {
14229           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14230                                       inputs, clobbers, labels);
14231           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14232           if (!extended_p)
14233             {
14234               tree temp = asm_stmt;
14235               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14236                 temp = TREE_OPERAND (temp, 0);
14237
14238               ASM_INPUT_P (temp) = 1;
14239             }
14240         }
14241       else
14242         cgraph_add_asm_node (string);
14243     }
14244 }
14245
14246 /* Declarators [gram.dcl.decl] */
14247
14248 /* Parse an init-declarator.
14249
14250    init-declarator:
14251      declarator initializer [opt]
14252
14253    GNU Extension:
14254
14255    init-declarator:
14256      declarator asm-specification [opt] attributes [opt] initializer [opt]
14257
14258    function-definition:
14259      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14260        function-body
14261      decl-specifier-seq [opt] declarator function-try-block
14262
14263    GNU Extension:
14264
14265    function-definition:
14266      __extension__ function-definition
14267
14268    The DECL_SPECIFIERS apply to this declarator.  Returns a
14269    representation of the entity declared.  If MEMBER_P is TRUE, then
14270    this declarator appears in a class scope.  The new DECL created by
14271    this declarator is returned.
14272
14273    The CHECKS are access checks that should be performed once we know
14274    what entity is being declared (and, therefore, what classes have
14275    befriended it).
14276
14277    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14278    for a function-definition here as well.  If the declarator is a
14279    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14280    be TRUE upon return.  By that point, the function-definition will
14281    have been completely parsed.
14282
14283    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14284    is FALSE.
14285
14286    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14287    parsed declaration if it is an uninitialized single declarator not followed
14288    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14289    if present, will not be consumed.  If returned, this declarator will be
14290    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14291
14292 static tree
14293 cp_parser_init_declarator (cp_parser* parser,
14294                            cp_decl_specifier_seq *decl_specifiers,
14295                            VEC (deferred_access_check,gc)* checks,
14296                            bool function_definition_allowed_p,
14297                            bool member_p,
14298                            int declares_class_or_enum,
14299                            bool* function_definition_p,
14300                            tree* maybe_range_for_decl)
14301 {
14302   cp_token *token = NULL, *asm_spec_start_token = NULL,
14303            *attributes_start_token = NULL;
14304   cp_declarator *declarator;
14305   tree prefix_attributes;
14306   tree attributes;
14307   tree asm_specification;
14308   tree initializer;
14309   tree decl = NULL_TREE;
14310   tree scope;
14311   int is_initialized;
14312   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14313      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14314      "(...)".  */
14315   enum cpp_ttype initialization_kind;
14316   bool is_direct_init = false;
14317   bool is_non_constant_init;
14318   int ctor_dtor_or_conv_p;
14319   bool friend_p;
14320   tree pushed_scope = NULL_TREE;
14321   bool range_for_decl_p = false;
14322
14323   /* Gather the attributes that were provided with the
14324      decl-specifiers.  */
14325   prefix_attributes = decl_specifiers->attributes;
14326
14327   /* Assume that this is not the declarator for a function
14328      definition.  */
14329   if (function_definition_p)
14330     *function_definition_p = false;
14331
14332   /* Defer access checks while parsing the declarator; we cannot know
14333      what names are accessible until we know what is being
14334      declared.  */
14335   resume_deferring_access_checks ();
14336
14337   /* Parse the declarator.  */
14338   token = cp_lexer_peek_token (parser->lexer);
14339   declarator
14340     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14341                             &ctor_dtor_or_conv_p,
14342                             /*parenthesized_p=*/NULL,
14343                             /*member_p=*/false);
14344   /* Gather up the deferred checks.  */
14345   stop_deferring_access_checks ();
14346
14347   /* If the DECLARATOR was erroneous, there's no need to go
14348      further.  */
14349   if (declarator == cp_error_declarator)
14350     return error_mark_node;
14351
14352   /* Check that the number of template-parameter-lists is OK.  */
14353   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14354                                                        token->location))
14355     return error_mark_node;
14356
14357   if (declares_class_or_enum & 2)
14358     cp_parser_check_for_definition_in_return_type (declarator,
14359                                                    decl_specifiers->type,
14360                                                    decl_specifiers->type_location);
14361
14362   /* Figure out what scope the entity declared by the DECLARATOR is
14363      located in.  `grokdeclarator' sometimes changes the scope, so
14364      we compute it now.  */
14365   scope = get_scope_of_declarator (declarator);
14366
14367   /* Perform any lookups in the declared type which were thought to be
14368      dependent, but are not in the scope of the declarator.  */
14369   decl_specifiers->type
14370     = maybe_update_decl_type (decl_specifiers->type, scope);
14371
14372   /* If we're allowing GNU extensions, look for an asm-specification
14373      and attributes.  */
14374   if (cp_parser_allow_gnu_extensions_p (parser))
14375     {
14376       /* Look for an asm-specification.  */
14377       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14378       asm_specification = cp_parser_asm_specification_opt (parser);
14379       /* And attributes.  */
14380       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14381       attributes = cp_parser_attributes_opt (parser);
14382     }
14383   else
14384     {
14385       asm_specification = NULL_TREE;
14386       attributes = NULL_TREE;
14387     }
14388
14389   /* Peek at the next token.  */
14390   token = cp_lexer_peek_token (parser->lexer);
14391   /* Check to see if the token indicates the start of a
14392      function-definition.  */
14393   if (function_declarator_p (declarator)
14394       && cp_parser_token_starts_function_definition_p (token))
14395     {
14396       if (!function_definition_allowed_p)
14397         {
14398           /* If a function-definition should not appear here, issue an
14399              error message.  */
14400           cp_parser_error (parser,
14401                            "a function-definition is not allowed here");
14402           return error_mark_node;
14403         }
14404       else
14405         {
14406           location_t func_brace_location
14407             = cp_lexer_peek_token (parser->lexer)->location;
14408
14409           /* Neither attributes nor an asm-specification are allowed
14410              on a function-definition.  */
14411           if (asm_specification)
14412             error_at (asm_spec_start_token->location,
14413                       "an asm-specification is not allowed "
14414                       "on a function-definition");
14415           if (attributes)
14416             error_at (attributes_start_token->location,
14417                       "attributes are not allowed on a function-definition");
14418           /* This is a function-definition.  */
14419           *function_definition_p = true;
14420
14421           /* Parse the function definition.  */
14422           if (member_p)
14423             decl = cp_parser_save_member_function_body (parser,
14424                                                         decl_specifiers,
14425                                                         declarator,
14426                                                         prefix_attributes);
14427           else
14428             decl
14429               = (cp_parser_function_definition_from_specifiers_and_declarator
14430                  (parser, decl_specifiers, prefix_attributes, declarator));
14431
14432           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14433             {
14434               /* This is where the prologue starts...  */
14435               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14436                 = func_brace_location;
14437             }
14438
14439           return decl;
14440         }
14441     }
14442
14443   /* [dcl.dcl]
14444
14445      Only in function declarations for constructors, destructors, and
14446      type conversions can the decl-specifier-seq be omitted.
14447
14448      We explicitly postpone this check past the point where we handle
14449      function-definitions because we tolerate function-definitions
14450      that are missing their return types in some modes.  */
14451   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14452     {
14453       cp_parser_error (parser,
14454                        "expected constructor, destructor, or type conversion");
14455       return error_mark_node;
14456     }
14457
14458   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14459   if (token->type == CPP_EQ
14460       || token->type == CPP_OPEN_PAREN
14461       || token->type == CPP_OPEN_BRACE)
14462     {
14463       is_initialized = SD_INITIALIZED;
14464       initialization_kind = token->type;
14465       if (maybe_range_for_decl)
14466         *maybe_range_for_decl = error_mark_node;
14467
14468       if (token->type == CPP_EQ
14469           && function_declarator_p (declarator))
14470         {
14471           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14472           if (t2->keyword == RID_DEFAULT)
14473             is_initialized = SD_DEFAULTED;
14474           else if (t2->keyword == RID_DELETE)
14475             is_initialized = SD_DELETED;
14476         }
14477     }
14478   else
14479     {
14480       /* If the init-declarator isn't initialized and isn't followed by a
14481          `,' or `;', it's not a valid init-declarator.  */
14482       if (token->type != CPP_COMMA
14483           && token->type != CPP_SEMICOLON)
14484         {
14485           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14486             range_for_decl_p = true;
14487           else
14488             {
14489               cp_parser_error (parser, "expected initializer");
14490               return error_mark_node;
14491             }
14492         }
14493       is_initialized = SD_UNINITIALIZED;
14494       initialization_kind = CPP_EOF;
14495     }
14496
14497   /* Because start_decl has side-effects, we should only call it if we
14498      know we're going ahead.  By this point, we know that we cannot
14499      possibly be looking at any other construct.  */
14500   cp_parser_commit_to_tentative_parse (parser);
14501
14502   /* If the decl specifiers were bad, issue an error now that we're
14503      sure this was intended to be a declarator.  Then continue
14504      declaring the variable(s), as int, to try to cut down on further
14505      errors.  */
14506   if (decl_specifiers->any_specifiers_p
14507       && decl_specifiers->type == error_mark_node)
14508     {
14509       cp_parser_error (parser, "invalid type in declaration");
14510       decl_specifiers->type = integer_type_node;
14511     }
14512
14513   /* Check to see whether or not this declaration is a friend.  */
14514   friend_p = cp_parser_friend_p (decl_specifiers);
14515
14516   /* Enter the newly declared entry in the symbol table.  If we're
14517      processing a declaration in a class-specifier, we wait until
14518      after processing the initializer.  */
14519   if (!member_p)
14520     {
14521       if (parser->in_unbraced_linkage_specification_p)
14522         decl_specifiers->storage_class = sc_extern;
14523       decl = start_decl (declarator, decl_specifiers,
14524                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14525                          attributes, prefix_attributes,
14526                          &pushed_scope);
14527       /* Adjust location of decl if declarator->id_loc is more appropriate:
14528          set, and decl wasn't merged with another decl, in which case its
14529          location would be different from input_location, and more accurate.  */
14530       if (DECL_P (decl)
14531           && declarator->id_loc != UNKNOWN_LOCATION
14532           && DECL_SOURCE_LOCATION (decl) == input_location)
14533         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14534     }
14535   else if (scope)
14536     /* Enter the SCOPE.  That way unqualified names appearing in the
14537        initializer will be looked up in SCOPE.  */
14538     pushed_scope = push_scope (scope);
14539
14540   /* Perform deferred access control checks, now that we know in which
14541      SCOPE the declared entity resides.  */
14542   if (!member_p && decl)
14543     {
14544       tree saved_current_function_decl = NULL_TREE;
14545
14546       /* If the entity being declared is a function, pretend that we
14547          are in its scope.  If it is a `friend', it may have access to
14548          things that would not otherwise be accessible.  */
14549       if (TREE_CODE (decl) == FUNCTION_DECL)
14550         {
14551           saved_current_function_decl = current_function_decl;
14552           current_function_decl = decl;
14553         }
14554
14555       /* Perform access checks for template parameters.  */
14556       cp_parser_perform_template_parameter_access_checks (checks);
14557
14558       /* Perform the access control checks for the declarator and the
14559          decl-specifiers.  */
14560       perform_deferred_access_checks ();
14561
14562       /* Restore the saved value.  */
14563       if (TREE_CODE (decl) == FUNCTION_DECL)
14564         current_function_decl = saved_current_function_decl;
14565     }
14566
14567   /* Parse the initializer.  */
14568   initializer = NULL_TREE;
14569   is_direct_init = false;
14570   is_non_constant_init = true;
14571   if (is_initialized)
14572     {
14573       if (function_declarator_p (declarator))
14574         {
14575           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14576            if (initialization_kind == CPP_EQ)
14577              initializer = cp_parser_pure_specifier (parser);
14578            else
14579              {
14580                /* If the declaration was erroneous, we don't really
14581                   know what the user intended, so just silently
14582                   consume the initializer.  */
14583                if (decl != error_mark_node)
14584                  error_at (initializer_start_token->location,
14585                            "initializer provided for function");
14586                cp_parser_skip_to_closing_parenthesis (parser,
14587                                                       /*recovering=*/true,
14588                                                       /*or_comma=*/false,
14589                                                       /*consume_paren=*/true);
14590              }
14591         }
14592       else
14593         {
14594           /* We want to record the extra mangling scope for in-class
14595              initializers of class members and initializers of static data
14596              member templates.  The former is a C++0x feature which isn't
14597              implemented yet, and I expect it will involve deferring
14598              parsing of the initializer until end of class as with default
14599              arguments.  So right here we only handle the latter.  */
14600           if (!member_p && processing_template_decl)
14601             start_lambda_scope (decl);
14602           initializer = cp_parser_initializer (parser,
14603                                                &is_direct_init,
14604                                                &is_non_constant_init);
14605           if (!member_p && processing_template_decl)
14606             finish_lambda_scope ();
14607         }
14608     }
14609
14610   /* The old parser allows attributes to appear after a parenthesized
14611      initializer.  Mark Mitchell proposed removing this functionality
14612      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14613      attributes -- but ignores them.  */
14614   if (cp_parser_allow_gnu_extensions_p (parser)
14615       && initialization_kind == CPP_OPEN_PAREN)
14616     if (cp_parser_attributes_opt (parser))
14617       warning (OPT_Wattributes,
14618                "attributes after parenthesized initializer ignored");
14619
14620   /* For an in-class declaration, use `grokfield' to create the
14621      declaration.  */
14622   if (member_p)
14623     {
14624       if (pushed_scope)
14625         {
14626           pop_scope (pushed_scope);
14627           pushed_scope = NULL_TREE;
14628         }
14629       decl = grokfield (declarator, decl_specifiers,
14630                         initializer, !is_non_constant_init,
14631                         /*asmspec=*/NULL_TREE,
14632                         prefix_attributes);
14633       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14634         cp_parser_save_default_args (parser, decl);
14635     }
14636
14637   /* Finish processing the declaration.  But, skip member
14638      declarations.  */
14639   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14640     {
14641       cp_finish_decl (decl,
14642                       initializer, !is_non_constant_init,
14643                       asm_specification,
14644                       /* If the initializer is in parentheses, then this is
14645                          a direct-initialization, which means that an
14646                          `explicit' constructor is OK.  Otherwise, an
14647                          `explicit' constructor cannot be used.  */
14648                       ((is_direct_init || !is_initialized)
14649                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14650     }
14651   else if ((cxx_dialect != cxx98) && friend_p
14652            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14653     /* Core issue #226 (C++0x only): A default template-argument
14654        shall not be specified in a friend class template
14655        declaration. */
14656     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14657                              /*is_partial=*/0, /*is_friend_decl=*/1);
14658
14659   if (!friend_p && pushed_scope)
14660     pop_scope (pushed_scope);
14661
14662   return decl;
14663 }
14664
14665 /* Parse a declarator.
14666
14667    declarator:
14668      direct-declarator
14669      ptr-operator declarator
14670
14671    abstract-declarator:
14672      ptr-operator abstract-declarator [opt]
14673      direct-abstract-declarator
14674
14675    GNU Extensions:
14676
14677    declarator:
14678      attributes [opt] direct-declarator
14679      attributes [opt] ptr-operator declarator
14680
14681    abstract-declarator:
14682      attributes [opt] ptr-operator abstract-declarator [opt]
14683      attributes [opt] direct-abstract-declarator
14684
14685    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14686    detect constructor, destructor or conversion operators. It is set
14687    to -1 if the declarator is a name, and +1 if it is a
14688    function. Otherwise it is set to zero. Usually you just want to
14689    test for >0, but internally the negative value is used.
14690
14691    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14692    a decl-specifier-seq unless it declares a constructor, destructor,
14693    or conversion.  It might seem that we could check this condition in
14694    semantic analysis, rather than parsing, but that makes it difficult
14695    to handle something like `f()'.  We want to notice that there are
14696    no decl-specifiers, and therefore realize that this is an
14697    expression, not a declaration.)
14698
14699    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14700    the declarator is a direct-declarator of the form "(...)".
14701
14702    MEMBER_P is true iff this declarator is a member-declarator.  */
14703
14704 static cp_declarator *
14705 cp_parser_declarator (cp_parser* parser,
14706                       cp_parser_declarator_kind dcl_kind,
14707                       int* ctor_dtor_or_conv_p,
14708                       bool* parenthesized_p,
14709                       bool member_p)
14710 {
14711   cp_declarator *declarator;
14712   enum tree_code code;
14713   cp_cv_quals cv_quals;
14714   tree class_type;
14715   tree attributes = NULL_TREE;
14716
14717   /* Assume this is not a constructor, destructor, or type-conversion
14718      operator.  */
14719   if (ctor_dtor_or_conv_p)
14720     *ctor_dtor_or_conv_p = 0;
14721
14722   if (cp_parser_allow_gnu_extensions_p (parser))
14723     attributes = cp_parser_attributes_opt (parser);
14724
14725   /* Check for the ptr-operator production.  */
14726   cp_parser_parse_tentatively (parser);
14727   /* Parse the ptr-operator.  */
14728   code = cp_parser_ptr_operator (parser,
14729                                  &class_type,
14730                                  &cv_quals);
14731   /* If that worked, then we have a ptr-operator.  */
14732   if (cp_parser_parse_definitely (parser))
14733     {
14734       /* If a ptr-operator was found, then this declarator was not
14735          parenthesized.  */
14736       if (parenthesized_p)
14737         *parenthesized_p = true;
14738       /* The dependent declarator is optional if we are parsing an
14739          abstract-declarator.  */
14740       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14741         cp_parser_parse_tentatively (parser);
14742
14743       /* Parse the dependent declarator.  */
14744       declarator = cp_parser_declarator (parser, dcl_kind,
14745                                          /*ctor_dtor_or_conv_p=*/NULL,
14746                                          /*parenthesized_p=*/NULL,
14747                                          /*member_p=*/false);
14748
14749       /* If we are parsing an abstract-declarator, we must handle the
14750          case where the dependent declarator is absent.  */
14751       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14752           && !cp_parser_parse_definitely (parser))
14753         declarator = NULL;
14754
14755       declarator = cp_parser_make_indirect_declarator
14756         (code, class_type, cv_quals, declarator);
14757     }
14758   /* Everything else is a direct-declarator.  */
14759   else
14760     {
14761       if (parenthesized_p)
14762         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14763                                                    CPP_OPEN_PAREN);
14764       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14765                                                 ctor_dtor_or_conv_p,
14766                                                 member_p);
14767     }
14768
14769   if (attributes && declarator && declarator != cp_error_declarator)
14770     declarator->attributes = attributes;
14771
14772   return declarator;
14773 }
14774
14775 /* Parse a direct-declarator or direct-abstract-declarator.
14776
14777    direct-declarator:
14778      declarator-id
14779      direct-declarator ( parameter-declaration-clause )
14780        cv-qualifier-seq [opt]
14781        exception-specification [opt]
14782      direct-declarator [ constant-expression [opt] ]
14783      ( declarator )
14784
14785    direct-abstract-declarator:
14786      direct-abstract-declarator [opt]
14787        ( parameter-declaration-clause )
14788        cv-qualifier-seq [opt]
14789        exception-specification [opt]
14790      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14791      ( abstract-declarator )
14792
14793    Returns a representation of the declarator.  DCL_KIND is
14794    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14795    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14796    we are parsing a direct-declarator.  It is
14797    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14798    of ambiguity we prefer an abstract declarator, as per
14799    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14800    cp_parser_declarator.  */
14801
14802 static cp_declarator *
14803 cp_parser_direct_declarator (cp_parser* parser,
14804                              cp_parser_declarator_kind dcl_kind,
14805                              int* ctor_dtor_or_conv_p,
14806                              bool member_p)
14807 {
14808   cp_token *token;
14809   cp_declarator *declarator = NULL;
14810   tree scope = NULL_TREE;
14811   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14812   bool saved_in_declarator_p = parser->in_declarator_p;
14813   bool first = true;
14814   tree pushed_scope = NULL_TREE;
14815
14816   while (true)
14817     {
14818       /* Peek at the next token.  */
14819       token = cp_lexer_peek_token (parser->lexer);
14820       if (token->type == CPP_OPEN_PAREN)
14821         {
14822           /* This is either a parameter-declaration-clause, or a
14823              parenthesized declarator. When we know we are parsing a
14824              named declarator, it must be a parenthesized declarator
14825              if FIRST is true. For instance, `(int)' is a
14826              parameter-declaration-clause, with an omitted
14827              direct-abstract-declarator. But `((*))', is a
14828              parenthesized abstract declarator. Finally, when T is a
14829              template parameter `(T)' is a
14830              parameter-declaration-clause, and not a parenthesized
14831              named declarator.
14832
14833              We first try and parse a parameter-declaration-clause,
14834              and then try a nested declarator (if FIRST is true).
14835
14836              It is not an error for it not to be a
14837              parameter-declaration-clause, even when FIRST is
14838              false. Consider,
14839
14840                int i (int);
14841                int i (3);
14842
14843              The first is the declaration of a function while the
14844              second is the definition of a variable, including its
14845              initializer.
14846
14847              Having seen only the parenthesis, we cannot know which of
14848              these two alternatives should be selected.  Even more
14849              complex are examples like:
14850
14851                int i (int (a));
14852                int i (int (3));
14853
14854              The former is a function-declaration; the latter is a
14855              variable initialization.
14856
14857              Thus again, we try a parameter-declaration-clause, and if
14858              that fails, we back out and return.  */
14859
14860           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14861             {
14862               tree params;
14863               unsigned saved_num_template_parameter_lists;
14864               bool is_declarator = false;
14865               tree t;
14866
14867               /* In a member-declarator, the only valid interpretation
14868                  of a parenthesis is the start of a
14869                  parameter-declaration-clause.  (It is invalid to
14870                  initialize a static data member with a parenthesized
14871                  initializer; only the "=" form of initialization is
14872                  permitted.)  */
14873               if (!member_p)
14874                 cp_parser_parse_tentatively (parser);
14875
14876               /* Consume the `('.  */
14877               cp_lexer_consume_token (parser->lexer);
14878               if (first)
14879                 {
14880                   /* If this is going to be an abstract declarator, we're
14881                      in a declarator and we can't have default args.  */
14882                   parser->default_arg_ok_p = false;
14883                   parser->in_declarator_p = true;
14884                 }
14885
14886               /* Inside the function parameter list, surrounding
14887                  template-parameter-lists do not apply.  */
14888               saved_num_template_parameter_lists
14889                 = parser->num_template_parameter_lists;
14890               parser->num_template_parameter_lists = 0;
14891
14892               begin_scope (sk_function_parms, NULL_TREE);
14893
14894               /* Parse the parameter-declaration-clause.  */
14895               params = cp_parser_parameter_declaration_clause (parser);
14896
14897               parser->num_template_parameter_lists
14898                 = saved_num_template_parameter_lists;
14899
14900               /* If all went well, parse the cv-qualifier-seq and the
14901                  exception-specification.  */
14902               if (member_p || cp_parser_parse_definitely (parser))
14903                 {
14904                   cp_cv_quals cv_quals;
14905                   cp_virt_specifiers virt_specifiers;
14906                   tree exception_specification;
14907                   tree late_return;
14908
14909                   is_declarator = true;
14910
14911                   if (ctor_dtor_or_conv_p)
14912                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14913                   first = false;
14914                   /* Consume the `)'.  */
14915                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14916
14917                   /* Parse the cv-qualifier-seq.  */
14918                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14919                   /* And the exception-specification.  */
14920                   exception_specification
14921                     = cp_parser_exception_specification_opt (parser);
14922                   /* Parse the virt-specifier-seq.  */
14923                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14924
14925                   late_return
14926                     = cp_parser_late_return_type_opt (parser);
14927
14928                   /* Create the function-declarator.  */
14929                   declarator = make_call_declarator (declarator,
14930                                                      params,
14931                                                      cv_quals,
14932                                                      virt_specifiers,
14933                                                      exception_specification,
14934                                                      late_return);
14935                   /* Any subsequent parameter lists are to do with
14936                      return type, so are not those of the declared
14937                      function.  */
14938                   parser->default_arg_ok_p = false;
14939                 }
14940
14941               /* Remove the function parms from scope.  */
14942               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14943                 pop_binding (DECL_NAME (t), t);
14944               leave_scope();
14945
14946               if (is_declarator)
14947                 /* Repeat the main loop.  */
14948                 continue;
14949             }
14950
14951           /* If this is the first, we can try a parenthesized
14952              declarator.  */
14953           if (first)
14954             {
14955               bool saved_in_type_id_in_expr_p;
14956
14957               parser->default_arg_ok_p = saved_default_arg_ok_p;
14958               parser->in_declarator_p = saved_in_declarator_p;
14959
14960               /* Consume the `('.  */
14961               cp_lexer_consume_token (parser->lexer);
14962               /* Parse the nested declarator.  */
14963               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14964               parser->in_type_id_in_expr_p = true;
14965               declarator
14966                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14967                                         /*parenthesized_p=*/NULL,
14968                                         member_p);
14969               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14970               first = false;
14971               /* Expect a `)'.  */
14972               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14973                 declarator = cp_error_declarator;
14974               if (declarator == cp_error_declarator)
14975                 break;
14976
14977               goto handle_declarator;
14978             }
14979           /* Otherwise, we must be done.  */
14980           else
14981             break;
14982         }
14983       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14984                && token->type == CPP_OPEN_SQUARE)
14985         {
14986           /* Parse an array-declarator.  */
14987           tree bounds;
14988
14989           if (ctor_dtor_or_conv_p)
14990             *ctor_dtor_or_conv_p = 0;
14991
14992           first = false;
14993           parser->default_arg_ok_p = false;
14994           parser->in_declarator_p = true;
14995           /* Consume the `['.  */
14996           cp_lexer_consume_token (parser->lexer);
14997           /* Peek at the next token.  */
14998           token = cp_lexer_peek_token (parser->lexer);
14999           /* If the next token is `]', then there is no
15000              constant-expression.  */
15001           if (token->type != CPP_CLOSE_SQUARE)
15002             {
15003               bool non_constant_p;
15004
15005               bounds
15006                 = cp_parser_constant_expression (parser,
15007                                                  /*allow_non_constant=*/true,
15008                                                  &non_constant_p);
15009               if (!non_constant_p)
15010                 /* OK */;
15011               /* Normally, the array bound must be an integral constant
15012                  expression.  However, as an extension, we allow VLAs
15013                  in function scopes as long as they aren't part of a
15014                  parameter declaration.  */
15015               else if (!parser->in_function_body
15016                        || current_binding_level->kind == sk_function_parms)
15017                 {
15018                   cp_parser_error (parser,
15019                                    "array bound is not an integer constant");
15020                   bounds = error_mark_node;
15021                 }
15022               else if (processing_template_decl && !error_operand_p (bounds))
15023                 {
15024                   /* Remember this wasn't a constant-expression.  */
15025                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15026                   TREE_SIDE_EFFECTS (bounds) = 1;
15027                 }
15028             }
15029           else
15030             bounds = NULL_TREE;
15031           /* Look for the closing `]'.  */
15032           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15033             {
15034               declarator = cp_error_declarator;
15035               break;
15036             }
15037
15038           declarator = make_array_declarator (declarator, bounds);
15039         }
15040       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15041         {
15042           {
15043             tree qualifying_scope;
15044             tree unqualified_name;
15045             special_function_kind sfk;
15046             bool abstract_ok;
15047             bool pack_expansion_p = false;
15048             cp_token *declarator_id_start_token;
15049
15050             /* Parse a declarator-id */
15051             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15052             if (abstract_ok)
15053               {
15054                 cp_parser_parse_tentatively (parser);
15055
15056                 /* If we see an ellipsis, we should be looking at a
15057                    parameter pack. */
15058                 if (token->type == CPP_ELLIPSIS)
15059                   {
15060                     /* Consume the `...' */
15061                     cp_lexer_consume_token (parser->lexer);
15062
15063                     pack_expansion_p = true;
15064                   }
15065               }
15066
15067             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15068             unqualified_name
15069               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15070             qualifying_scope = parser->scope;
15071             if (abstract_ok)
15072               {
15073                 bool okay = false;
15074
15075                 if (!unqualified_name && pack_expansion_p)
15076                   {
15077                     /* Check whether an error occurred. */
15078                     okay = !cp_parser_error_occurred (parser);
15079
15080                     /* We already consumed the ellipsis to mark a
15081                        parameter pack, but we have no way to report it,
15082                        so abort the tentative parse. We will be exiting
15083                        immediately anyway. */
15084                     cp_parser_abort_tentative_parse (parser);
15085                   }
15086                 else
15087                   okay = cp_parser_parse_definitely (parser);
15088
15089                 if (!okay)
15090                   unqualified_name = error_mark_node;
15091                 else if (unqualified_name
15092                          && (qualifying_scope
15093                              || (TREE_CODE (unqualified_name)
15094                                  != IDENTIFIER_NODE)))
15095                   {
15096                     cp_parser_error (parser, "expected unqualified-id");
15097                     unqualified_name = error_mark_node;
15098                   }
15099               }
15100
15101             if (!unqualified_name)
15102               return NULL;
15103             if (unqualified_name == error_mark_node)
15104               {
15105                 declarator = cp_error_declarator;
15106                 pack_expansion_p = false;
15107                 declarator->parameter_pack_p = false;
15108                 break;
15109               }
15110
15111             if (qualifying_scope && at_namespace_scope_p ()
15112                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15113               {
15114                 /* In the declaration of a member of a template class
15115                    outside of the class itself, the SCOPE will sometimes
15116                    be a TYPENAME_TYPE.  For example, given:
15117
15118                    template <typename T>
15119                    int S<T>::R::i = 3;
15120
15121                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15122                    this context, we must resolve S<T>::R to an ordinary
15123                    type, rather than a typename type.
15124
15125                    The reason we normally avoid resolving TYPENAME_TYPEs
15126                    is that a specialization of `S' might render
15127                    `S<T>::R' not a type.  However, if `S' is
15128                    specialized, then this `i' will not be used, so there
15129                    is no harm in resolving the types here.  */
15130                 tree type;
15131
15132                 /* Resolve the TYPENAME_TYPE.  */
15133                 type = resolve_typename_type (qualifying_scope,
15134                                               /*only_current_p=*/false);
15135                 /* If that failed, the declarator is invalid.  */
15136                 if (TREE_CODE (type) == TYPENAME_TYPE)
15137                   {
15138                     if (typedef_variant_p (type))
15139                       error_at (declarator_id_start_token->location,
15140                                 "cannot define member of dependent typedef "
15141                                 "%qT", type);
15142                     else
15143                       error_at (declarator_id_start_token->location,
15144                                 "%<%T::%E%> is not a type",
15145                                 TYPE_CONTEXT (qualifying_scope),
15146                                 TYPE_IDENTIFIER (qualifying_scope));
15147                   }
15148                 qualifying_scope = type;
15149               }
15150
15151             sfk = sfk_none;
15152
15153             if (unqualified_name)
15154               {
15155                 tree class_type;
15156
15157                 if (qualifying_scope
15158                     && CLASS_TYPE_P (qualifying_scope))
15159                   class_type = qualifying_scope;
15160                 else
15161                   class_type = current_class_type;
15162
15163                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15164                   {
15165                     tree name_type = TREE_TYPE (unqualified_name);
15166                     if (class_type && same_type_p (name_type, class_type))
15167                       {
15168                         if (qualifying_scope
15169                             && CLASSTYPE_USE_TEMPLATE (name_type))
15170                           {
15171                             error_at (declarator_id_start_token->location,
15172                                       "invalid use of constructor as a template");
15173                             inform (declarator_id_start_token->location,
15174                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15175                                     "name the constructor in a qualified name",
15176                                     class_type,
15177                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15178                                     class_type, name_type);
15179                             declarator = cp_error_declarator;
15180                             break;
15181                           }
15182                         else
15183                           unqualified_name = constructor_name (class_type);
15184                       }
15185                     else
15186                       {
15187                         /* We do not attempt to print the declarator
15188                            here because we do not have enough
15189                            information about its original syntactic
15190                            form.  */
15191                         cp_parser_error (parser, "invalid declarator");
15192                         declarator = cp_error_declarator;
15193                         break;
15194                       }
15195                   }
15196
15197                 if (class_type)
15198                   {
15199                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15200                       sfk = sfk_destructor;
15201                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15202                       sfk = sfk_conversion;
15203                     else if (/* There's no way to declare a constructor
15204                                 for an anonymous type, even if the type
15205                                 got a name for linkage purposes.  */
15206                              !TYPE_WAS_ANONYMOUS (class_type)
15207                              && constructor_name_p (unqualified_name,
15208                                                     class_type))
15209                       {
15210                         unqualified_name = constructor_name (class_type);
15211                         sfk = sfk_constructor;
15212                       }
15213                     else if (is_overloaded_fn (unqualified_name)
15214                              && DECL_CONSTRUCTOR_P (get_first_fn
15215                                                     (unqualified_name)))
15216                       sfk = sfk_constructor;
15217
15218                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15219                       *ctor_dtor_or_conv_p = -1;
15220                   }
15221               }
15222             declarator = make_id_declarator (qualifying_scope,
15223                                              unqualified_name,
15224                                              sfk);
15225             declarator->id_loc = token->location;
15226             declarator->parameter_pack_p = pack_expansion_p;
15227
15228             if (pack_expansion_p)
15229               maybe_warn_variadic_templates ();
15230           }
15231
15232         handle_declarator:;
15233           scope = get_scope_of_declarator (declarator);
15234           if (scope)
15235             /* Any names that appear after the declarator-id for a
15236                member are looked up in the containing scope.  */
15237             pushed_scope = push_scope (scope);
15238           parser->in_declarator_p = true;
15239           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15240               || (declarator && declarator->kind == cdk_id))
15241             /* Default args are only allowed on function
15242                declarations.  */
15243             parser->default_arg_ok_p = saved_default_arg_ok_p;
15244           else
15245             parser->default_arg_ok_p = false;
15246
15247           first = false;
15248         }
15249       /* We're done.  */
15250       else
15251         break;
15252     }
15253
15254   /* For an abstract declarator, we might wind up with nothing at this
15255      point.  That's an error; the declarator is not optional.  */
15256   if (!declarator)
15257     cp_parser_error (parser, "expected declarator");
15258
15259   /* If we entered a scope, we must exit it now.  */
15260   if (pushed_scope)
15261     pop_scope (pushed_scope);
15262
15263   parser->default_arg_ok_p = saved_default_arg_ok_p;
15264   parser->in_declarator_p = saved_in_declarator_p;
15265
15266   return declarator;
15267 }
15268
15269 /* Parse a ptr-operator.
15270
15271    ptr-operator:
15272      * cv-qualifier-seq [opt]
15273      &
15274      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15275
15276    GNU Extension:
15277
15278    ptr-operator:
15279      & cv-qualifier-seq [opt]
15280
15281    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15282    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15283    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15284    filled in with the TYPE containing the member.  *CV_QUALS is
15285    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15286    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15287    Note that the tree codes returned by this function have nothing
15288    to do with the types of trees that will be eventually be created
15289    to represent the pointer or reference type being parsed. They are
15290    just constants with suggestive names. */
15291 static enum tree_code
15292 cp_parser_ptr_operator (cp_parser* parser,
15293                         tree* type,
15294                         cp_cv_quals *cv_quals)
15295 {
15296   enum tree_code code = ERROR_MARK;
15297   cp_token *token;
15298
15299   /* Assume that it's not a pointer-to-member.  */
15300   *type = NULL_TREE;
15301   /* And that there are no cv-qualifiers.  */
15302   *cv_quals = TYPE_UNQUALIFIED;
15303
15304   /* Peek at the next token.  */
15305   token = cp_lexer_peek_token (parser->lexer);
15306
15307   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15308   if (token->type == CPP_MULT)
15309     code = INDIRECT_REF;
15310   else if (token->type == CPP_AND)
15311     code = ADDR_EXPR;
15312   else if ((cxx_dialect != cxx98) &&
15313            token->type == CPP_AND_AND) /* C++0x only */
15314     code = NON_LVALUE_EXPR;
15315
15316   if (code != ERROR_MARK)
15317     {
15318       /* Consume the `*', `&' or `&&'.  */
15319       cp_lexer_consume_token (parser->lexer);
15320
15321       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15322          `&', if we are allowing GNU extensions.  (The only qualifier
15323          that can legally appear after `&' is `restrict', but that is
15324          enforced during semantic analysis.  */
15325       if (code == INDIRECT_REF
15326           || cp_parser_allow_gnu_extensions_p (parser))
15327         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15328     }
15329   else
15330     {
15331       /* Try the pointer-to-member case.  */
15332       cp_parser_parse_tentatively (parser);
15333       /* Look for the optional `::' operator.  */
15334       cp_parser_global_scope_opt (parser,
15335                                   /*current_scope_valid_p=*/false);
15336       /* Look for the nested-name specifier.  */
15337       token = cp_lexer_peek_token (parser->lexer);
15338       cp_parser_nested_name_specifier (parser,
15339                                        /*typename_keyword_p=*/false,
15340                                        /*check_dependency_p=*/true,
15341                                        /*type_p=*/false,
15342                                        /*is_declaration=*/false);
15343       /* If we found it, and the next token is a `*', then we are
15344          indeed looking at a pointer-to-member operator.  */
15345       if (!cp_parser_error_occurred (parser)
15346           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15347         {
15348           /* Indicate that the `*' operator was used.  */
15349           code = INDIRECT_REF;
15350
15351           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15352             error_at (token->location, "%qD is a namespace", parser->scope);
15353           else
15354             {
15355               /* The type of which the member is a member is given by the
15356                  current SCOPE.  */
15357               *type = parser->scope;
15358               /* The next name will not be qualified.  */
15359               parser->scope = NULL_TREE;
15360               parser->qualifying_scope = NULL_TREE;
15361               parser->object_scope = NULL_TREE;
15362               /* Look for the optional cv-qualifier-seq.  */
15363               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15364             }
15365         }
15366       /* If that didn't work we don't have a ptr-operator.  */
15367       if (!cp_parser_parse_definitely (parser))
15368         cp_parser_error (parser, "expected ptr-operator");
15369     }
15370
15371   return code;
15372 }
15373
15374 /* Parse an (optional) cv-qualifier-seq.
15375
15376    cv-qualifier-seq:
15377      cv-qualifier cv-qualifier-seq [opt]
15378
15379    cv-qualifier:
15380      const
15381      volatile
15382
15383    GNU Extension:
15384
15385    cv-qualifier:
15386      __restrict__
15387
15388    Returns a bitmask representing the cv-qualifiers.  */
15389
15390 static cp_cv_quals
15391 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15392 {
15393   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15394
15395   while (true)
15396     {
15397       cp_token *token;
15398       cp_cv_quals cv_qualifier;
15399
15400       /* Peek at the next token.  */
15401       token = cp_lexer_peek_token (parser->lexer);
15402       /* See if it's a cv-qualifier.  */
15403       switch (token->keyword)
15404         {
15405         case RID_CONST:
15406           cv_qualifier = TYPE_QUAL_CONST;
15407           break;
15408
15409         case RID_VOLATILE:
15410           cv_qualifier = TYPE_QUAL_VOLATILE;
15411           break;
15412
15413         case RID_RESTRICT:
15414           cv_qualifier = TYPE_QUAL_RESTRICT;
15415           break;
15416
15417         default:
15418           cv_qualifier = TYPE_UNQUALIFIED;
15419           break;
15420         }
15421
15422       if (!cv_qualifier)
15423         break;
15424
15425       if (cv_quals & cv_qualifier)
15426         {
15427           error_at (token->location, "duplicate cv-qualifier");
15428           cp_lexer_purge_token (parser->lexer);
15429         }
15430       else
15431         {
15432           cp_lexer_consume_token (parser->lexer);
15433           cv_quals |= cv_qualifier;
15434         }
15435     }
15436
15437   return cv_quals;
15438 }
15439
15440 /* Parse an (optional) virt-specifier-seq.
15441
15442    virt-specifier-seq:
15443      virt-specifier virt-specifier-seq [opt]
15444
15445    virt-specifier:
15446      override
15447      final
15448
15449    Returns a bitmask representing the virt-specifiers.  */
15450
15451 static cp_virt_specifiers
15452 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15453 {
15454   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15455
15456   while (true)
15457     {
15458       cp_token *token;
15459       cp_virt_specifiers virt_specifier;
15460
15461       /* Peek at the next token.  */
15462       token = cp_lexer_peek_token (parser->lexer);
15463       /* See if it's a virt-specifier-qualifier.  */
15464       if (token->type != CPP_NAME)
15465         break;
15466       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15467         virt_specifier = VIRT_SPEC_OVERRIDE;
15468       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15469         virt_specifier = VIRT_SPEC_FINAL;
15470       else
15471         break;
15472
15473       if (virt_specifiers & virt_specifier)
15474         {
15475           error_at (token->location, "duplicate virt-specifier");
15476           cp_lexer_purge_token (parser->lexer);
15477         }
15478       else
15479         {
15480           cp_lexer_consume_token (parser->lexer);
15481           virt_specifiers |= virt_specifier;
15482         }
15483     }
15484   return virt_specifiers;
15485 }
15486
15487 /* Parse a late-specified return type, if any.  This is not a separate
15488    non-terminal, but part of a function declarator, which looks like
15489
15490    -> trailing-type-specifier-seq abstract-declarator(opt)
15491
15492    Returns the type indicated by the type-id.  */
15493
15494 static tree
15495 cp_parser_late_return_type_opt (cp_parser* parser)
15496 {
15497   cp_token *token;
15498
15499   /* Peek at the next token.  */
15500   token = cp_lexer_peek_token (parser->lexer);
15501   /* A late-specified return type is indicated by an initial '->'. */
15502   if (token->type != CPP_DEREF)
15503     return NULL_TREE;
15504
15505   /* Consume the ->.  */
15506   cp_lexer_consume_token (parser->lexer);
15507
15508   return cp_parser_trailing_type_id (parser);
15509 }
15510
15511 /* Parse a declarator-id.
15512
15513    declarator-id:
15514      id-expression
15515      :: [opt] nested-name-specifier [opt] type-name
15516
15517    In the `id-expression' case, the value returned is as for
15518    cp_parser_id_expression if the id-expression was an unqualified-id.
15519    If the id-expression was a qualified-id, then a SCOPE_REF is
15520    returned.  The first operand is the scope (either a NAMESPACE_DECL
15521    or TREE_TYPE), but the second is still just a representation of an
15522    unqualified-id.  */
15523
15524 static tree
15525 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15526 {
15527   tree id;
15528   /* The expression must be an id-expression.  Assume that qualified
15529      names are the names of types so that:
15530
15531        template <class T>
15532        int S<T>::R::i = 3;
15533
15534      will work; we must treat `S<T>::R' as the name of a type.
15535      Similarly, assume that qualified names are templates, where
15536      required, so that:
15537
15538        template <class T>
15539        int S<T>::R<T>::i = 3;
15540
15541      will work, too.  */
15542   id = cp_parser_id_expression (parser,
15543                                 /*template_keyword_p=*/false,
15544                                 /*check_dependency_p=*/false,
15545                                 /*template_p=*/NULL,
15546                                 /*declarator_p=*/true,
15547                                 optional_p);
15548   if (id && BASELINK_P (id))
15549     id = BASELINK_FUNCTIONS (id);
15550   return id;
15551 }
15552
15553 /* Parse a type-id.
15554
15555    type-id:
15556      type-specifier-seq abstract-declarator [opt]
15557
15558    Returns the TYPE specified.  */
15559
15560 static tree
15561 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15562                      bool is_trailing_return)
15563 {
15564   cp_decl_specifier_seq type_specifier_seq;
15565   cp_declarator *abstract_declarator;
15566
15567   /* Parse the type-specifier-seq.  */
15568   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15569                                 is_trailing_return,
15570                                 &type_specifier_seq);
15571   if (type_specifier_seq.type == error_mark_node)
15572     return error_mark_node;
15573
15574   /* There might or might not be an abstract declarator.  */
15575   cp_parser_parse_tentatively (parser);
15576   /* Look for the declarator.  */
15577   abstract_declarator
15578     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15579                             /*parenthesized_p=*/NULL,
15580                             /*member_p=*/false);
15581   /* Check to see if there really was a declarator.  */
15582   if (!cp_parser_parse_definitely (parser))
15583     abstract_declarator = NULL;
15584
15585   if (type_specifier_seq.type
15586       && type_uses_auto (type_specifier_seq.type))
15587     {
15588       /* A type-id with type 'auto' is only ok if the abstract declarator
15589          is a function declarator with a late-specified return type.  */
15590       if (abstract_declarator
15591           && abstract_declarator->kind == cdk_function
15592           && abstract_declarator->u.function.late_return_type)
15593         /* OK */;
15594       else
15595         {
15596           error ("invalid use of %<auto%>");
15597           return error_mark_node;
15598         }
15599     }
15600   
15601   return groktypename (&type_specifier_seq, abstract_declarator,
15602                        is_template_arg);
15603 }
15604
15605 static tree cp_parser_type_id (cp_parser *parser)
15606 {
15607   return cp_parser_type_id_1 (parser, false, false);
15608 }
15609
15610 static tree cp_parser_template_type_arg (cp_parser *parser)
15611 {
15612   tree r;
15613   const char *saved_message = parser->type_definition_forbidden_message;
15614   parser->type_definition_forbidden_message
15615     = G_("types may not be defined in template arguments");
15616   r = cp_parser_type_id_1 (parser, true, false);
15617   parser->type_definition_forbidden_message = saved_message;
15618   return r;
15619 }
15620
15621 static tree cp_parser_trailing_type_id (cp_parser *parser)
15622 {
15623   return cp_parser_type_id_1 (parser, false, true);
15624 }
15625
15626 /* Parse a type-specifier-seq.
15627
15628    type-specifier-seq:
15629      type-specifier type-specifier-seq [opt]
15630
15631    GNU extension:
15632
15633    type-specifier-seq:
15634      attributes type-specifier-seq [opt]
15635
15636    If IS_DECLARATION is true, we are at the start of a "condition" or
15637    exception-declaration, so we might be followed by a declarator-id.
15638
15639    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15640    i.e. we've just seen "->".
15641
15642    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15643
15644 static void
15645 cp_parser_type_specifier_seq (cp_parser* parser,
15646                               bool is_declaration,
15647                               bool is_trailing_return,
15648                               cp_decl_specifier_seq *type_specifier_seq)
15649 {
15650   bool seen_type_specifier = false;
15651   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15652   cp_token *start_token = NULL;
15653
15654   /* Clear the TYPE_SPECIFIER_SEQ.  */
15655   clear_decl_specs (type_specifier_seq);
15656
15657   /* In the context of a trailing return type, enum E { } is an
15658      elaborated-type-specifier followed by a function-body, not an
15659      enum-specifier.  */
15660   if (is_trailing_return)
15661     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15662
15663   /* Parse the type-specifiers and attributes.  */
15664   while (true)
15665     {
15666       tree type_specifier;
15667       bool is_cv_qualifier;
15668
15669       /* Check for attributes first.  */
15670       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15671         {
15672           type_specifier_seq->attributes =
15673             chainon (type_specifier_seq->attributes,
15674                      cp_parser_attributes_opt (parser));
15675           continue;
15676         }
15677
15678       /* record the token of the beginning of the type specifier seq,
15679          for error reporting purposes*/
15680      if (!start_token)
15681        start_token = cp_lexer_peek_token (parser->lexer);
15682
15683       /* Look for the type-specifier.  */
15684       type_specifier = cp_parser_type_specifier (parser,
15685                                                  flags,
15686                                                  type_specifier_seq,
15687                                                  /*is_declaration=*/false,
15688                                                  NULL,
15689                                                  &is_cv_qualifier);
15690       if (!type_specifier)
15691         {
15692           /* If the first type-specifier could not be found, this is not a
15693              type-specifier-seq at all.  */
15694           if (!seen_type_specifier)
15695             {
15696               cp_parser_error (parser, "expected type-specifier");
15697               type_specifier_seq->type = error_mark_node;
15698               return;
15699             }
15700           /* If subsequent type-specifiers could not be found, the
15701              type-specifier-seq is complete.  */
15702           break;
15703         }
15704
15705       seen_type_specifier = true;
15706       /* The standard says that a condition can be:
15707
15708             type-specifier-seq declarator = assignment-expression
15709
15710          However, given:
15711
15712            struct S {};
15713            if (int S = ...)
15714
15715          we should treat the "S" as a declarator, not as a
15716          type-specifier.  The standard doesn't say that explicitly for
15717          type-specifier-seq, but it does say that for
15718          decl-specifier-seq in an ordinary declaration.  Perhaps it
15719          would be clearer just to allow a decl-specifier-seq here, and
15720          then add a semantic restriction that if any decl-specifiers
15721          that are not type-specifiers appear, the program is invalid.  */
15722       if (is_declaration && !is_cv_qualifier)
15723         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15724     }
15725
15726   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15727 }
15728
15729 /* Parse a parameter-declaration-clause.
15730
15731    parameter-declaration-clause:
15732      parameter-declaration-list [opt] ... [opt]
15733      parameter-declaration-list , ...
15734
15735    Returns a representation for the parameter declarations.  A return
15736    value of NULL indicates a parameter-declaration-clause consisting
15737    only of an ellipsis.  */
15738
15739 static tree
15740 cp_parser_parameter_declaration_clause (cp_parser* parser)
15741 {
15742   tree parameters;
15743   cp_token *token;
15744   bool ellipsis_p;
15745   bool is_error;
15746
15747   /* Peek at the next token.  */
15748   token = cp_lexer_peek_token (parser->lexer);
15749   /* Check for trivial parameter-declaration-clauses.  */
15750   if (token->type == CPP_ELLIPSIS)
15751     {
15752       /* Consume the `...' token.  */
15753       cp_lexer_consume_token (parser->lexer);
15754       return NULL_TREE;
15755     }
15756   else if (token->type == CPP_CLOSE_PAREN)
15757     /* There are no parameters.  */
15758     {
15759 #ifndef NO_IMPLICIT_EXTERN_C
15760       if (in_system_header && current_class_type == NULL
15761           && current_lang_name == lang_name_c)
15762         return NULL_TREE;
15763       else
15764 #endif
15765         return void_list_node;
15766     }
15767   /* Check for `(void)', too, which is a special case.  */
15768   else if (token->keyword == RID_VOID
15769            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15770                == CPP_CLOSE_PAREN))
15771     {
15772       /* Consume the `void' token.  */
15773       cp_lexer_consume_token (parser->lexer);
15774       /* There are no parameters.  */
15775       return void_list_node;
15776     }
15777
15778   /* Parse the parameter-declaration-list.  */
15779   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15780   /* If a parse error occurred while parsing the
15781      parameter-declaration-list, then the entire
15782      parameter-declaration-clause is erroneous.  */
15783   if (is_error)
15784     return NULL;
15785
15786   /* Peek at the next token.  */
15787   token = cp_lexer_peek_token (parser->lexer);
15788   /* If it's a `,', the clause should terminate with an ellipsis.  */
15789   if (token->type == CPP_COMMA)
15790     {
15791       /* Consume the `,'.  */
15792       cp_lexer_consume_token (parser->lexer);
15793       /* Expect an ellipsis.  */
15794       ellipsis_p
15795         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15796     }
15797   /* It might also be `...' if the optional trailing `,' was
15798      omitted.  */
15799   else if (token->type == CPP_ELLIPSIS)
15800     {
15801       /* Consume the `...' token.  */
15802       cp_lexer_consume_token (parser->lexer);
15803       /* And remember that we saw it.  */
15804       ellipsis_p = true;
15805     }
15806   else
15807     ellipsis_p = false;
15808
15809   /* Finish the parameter list.  */
15810   if (!ellipsis_p)
15811     parameters = chainon (parameters, void_list_node);
15812
15813   return parameters;
15814 }
15815
15816 /* Parse a parameter-declaration-list.
15817
15818    parameter-declaration-list:
15819      parameter-declaration
15820      parameter-declaration-list , parameter-declaration
15821
15822    Returns a representation of the parameter-declaration-list, as for
15823    cp_parser_parameter_declaration_clause.  However, the
15824    `void_list_node' is never appended to the list.  Upon return,
15825    *IS_ERROR will be true iff an error occurred.  */
15826
15827 static tree
15828 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15829 {
15830   tree parameters = NULL_TREE;
15831   tree *tail = &parameters; 
15832   bool saved_in_unbraced_linkage_specification_p;
15833   int index = 0;
15834
15835   /* Assume all will go well.  */
15836   *is_error = false;
15837   /* The special considerations that apply to a function within an
15838      unbraced linkage specifications do not apply to the parameters
15839      to the function.  */
15840   saved_in_unbraced_linkage_specification_p 
15841     = parser->in_unbraced_linkage_specification_p;
15842   parser->in_unbraced_linkage_specification_p = false;
15843
15844   /* Look for more parameters.  */
15845   while (true)
15846     {
15847       cp_parameter_declarator *parameter;
15848       tree decl = error_mark_node;
15849       bool parenthesized_p;
15850       /* Parse the parameter.  */
15851       parameter
15852         = cp_parser_parameter_declaration (parser,
15853                                            /*template_parm_p=*/false,
15854                                            &parenthesized_p);
15855
15856       /* We don't know yet if the enclosing context is deprecated, so wait
15857          and warn in grokparms if appropriate.  */
15858       deprecated_state = DEPRECATED_SUPPRESS;
15859
15860       if (parameter)
15861         decl = grokdeclarator (parameter->declarator,
15862                                &parameter->decl_specifiers,
15863                                PARM,
15864                                parameter->default_argument != NULL_TREE,
15865                                &parameter->decl_specifiers.attributes);
15866
15867       deprecated_state = DEPRECATED_NORMAL;
15868
15869       /* If a parse error occurred parsing the parameter declaration,
15870          then the entire parameter-declaration-list is erroneous.  */
15871       if (decl == error_mark_node)
15872         {
15873           *is_error = true;
15874           parameters = error_mark_node;
15875           break;
15876         }
15877
15878       if (parameter->decl_specifiers.attributes)
15879         cplus_decl_attributes (&decl,
15880                                parameter->decl_specifiers.attributes,
15881                                0);
15882       if (DECL_NAME (decl))
15883         decl = pushdecl (decl);
15884
15885       if (decl != error_mark_node)
15886         {
15887           retrofit_lang_decl (decl);
15888           DECL_PARM_INDEX (decl) = ++index;
15889           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15890         }
15891
15892       /* Add the new parameter to the list.  */
15893       *tail = build_tree_list (parameter->default_argument, decl);
15894       tail = &TREE_CHAIN (*tail);
15895
15896       /* Peek at the next token.  */
15897       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15898           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15899           /* These are for Objective-C++ */
15900           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15901           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15902         /* The parameter-declaration-list is complete.  */
15903         break;
15904       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15905         {
15906           cp_token *token;
15907
15908           /* Peek at the next token.  */
15909           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15910           /* If it's an ellipsis, then the list is complete.  */
15911           if (token->type == CPP_ELLIPSIS)
15912             break;
15913           /* Otherwise, there must be more parameters.  Consume the
15914              `,'.  */
15915           cp_lexer_consume_token (parser->lexer);
15916           /* When parsing something like:
15917
15918                 int i(float f, double d)
15919
15920              we can tell after seeing the declaration for "f" that we
15921              are not looking at an initialization of a variable "i",
15922              but rather at the declaration of a function "i".
15923
15924              Due to the fact that the parsing of template arguments
15925              (as specified to a template-id) requires backtracking we
15926              cannot use this technique when inside a template argument
15927              list.  */
15928           if (!parser->in_template_argument_list_p
15929               && !parser->in_type_id_in_expr_p
15930               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15931               /* However, a parameter-declaration of the form
15932                  "foat(f)" (which is a valid declaration of a
15933                  parameter "f") can also be interpreted as an
15934                  expression (the conversion of "f" to "float").  */
15935               && !parenthesized_p)
15936             cp_parser_commit_to_tentative_parse (parser);
15937         }
15938       else
15939         {
15940           cp_parser_error (parser, "expected %<,%> or %<...%>");
15941           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15942             cp_parser_skip_to_closing_parenthesis (parser,
15943                                                    /*recovering=*/true,
15944                                                    /*or_comma=*/false,
15945                                                    /*consume_paren=*/false);
15946           break;
15947         }
15948     }
15949
15950   parser->in_unbraced_linkage_specification_p
15951     = saved_in_unbraced_linkage_specification_p;
15952
15953   return parameters;
15954 }
15955
15956 /* Parse a parameter declaration.
15957
15958    parameter-declaration:
15959      decl-specifier-seq ... [opt] declarator
15960      decl-specifier-seq declarator = assignment-expression
15961      decl-specifier-seq ... [opt] abstract-declarator [opt]
15962      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15963
15964    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15965    declares a template parameter.  (In that case, a non-nested `>'
15966    token encountered during the parsing of the assignment-expression
15967    is not interpreted as a greater-than operator.)
15968
15969    Returns a representation of the parameter, or NULL if an error
15970    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15971    true iff the declarator is of the form "(p)".  */
15972
15973 static cp_parameter_declarator *
15974 cp_parser_parameter_declaration (cp_parser *parser,
15975                                  bool template_parm_p,
15976                                  bool *parenthesized_p)
15977 {
15978   int declares_class_or_enum;
15979   cp_decl_specifier_seq decl_specifiers;
15980   cp_declarator *declarator;
15981   tree default_argument;
15982   cp_token *token = NULL, *declarator_token_start = NULL;
15983   const char *saved_message;
15984
15985   /* In a template parameter, `>' is not an operator.
15986
15987      [temp.param]
15988
15989      When parsing a default template-argument for a non-type
15990      template-parameter, the first non-nested `>' is taken as the end
15991      of the template parameter-list rather than a greater-than
15992      operator.  */
15993
15994   /* Type definitions may not appear in parameter types.  */
15995   saved_message = parser->type_definition_forbidden_message;
15996   parser->type_definition_forbidden_message
15997     = G_("types may not be defined in parameter types");
15998
15999   /* Parse the declaration-specifiers.  */
16000   cp_parser_decl_specifier_seq (parser,
16001                                 CP_PARSER_FLAGS_NONE,
16002                                 &decl_specifiers,
16003                                 &declares_class_or_enum);
16004
16005   /* Complain about missing 'typename' or other invalid type names.  */
16006   if (!decl_specifiers.any_type_specifiers_p)
16007     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16008
16009   /* If an error occurred, there's no reason to attempt to parse the
16010      rest of the declaration.  */
16011   if (cp_parser_error_occurred (parser))
16012     {
16013       parser->type_definition_forbidden_message = saved_message;
16014       return NULL;
16015     }
16016
16017   /* Peek at the next token.  */
16018   token = cp_lexer_peek_token (parser->lexer);
16019
16020   /* If the next token is a `)', `,', `=', `>', or `...', then there
16021      is no declarator. However, when variadic templates are enabled,
16022      there may be a declarator following `...'.  */
16023   if (token->type == CPP_CLOSE_PAREN
16024       || token->type == CPP_COMMA
16025       || token->type == CPP_EQ
16026       || token->type == CPP_GREATER)
16027     {
16028       declarator = NULL;
16029       if (parenthesized_p)
16030         *parenthesized_p = false;
16031     }
16032   /* Otherwise, there should be a declarator.  */
16033   else
16034     {
16035       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16036       parser->default_arg_ok_p = false;
16037
16038       /* After seeing a decl-specifier-seq, if the next token is not a
16039          "(", there is no possibility that the code is a valid
16040          expression.  Therefore, if parsing tentatively, we commit at
16041          this point.  */
16042       if (!parser->in_template_argument_list_p
16043           /* In an expression context, having seen:
16044
16045                (int((char ...
16046
16047              we cannot be sure whether we are looking at a
16048              function-type (taking a "char" as a parameter) or a cast
16049              of some object of type "char" to "int".  */
16050           && !parser->in_type_id_in_expr_p
16051           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16052           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16053         cp_parser_commit_to_tentative_parse (parser);
16054       /* Parse the declarator.  */
16055       declarator_token_start = token;
16056       declarator = cp_parser_declarator (parser,
16057                                          CP_PARSER_DECLARATOR_EITHER,
16058                                          /*ctor_dtor_or_conv_p=*/NULL,
16059                                          parenthesized_p,
16060                                          /*member_p=*/false);
16061       parser->default_arg_ok_p = saved_default_arg_ok_p;
16062       /* After the declarator, allow more attributes.  */
16063       decl_specifiers.attributes
16064         = chainon (decl_specifiers.attributes,
16065                    cp_parser_attributes_opt (parser));
16066     }
16067
16068   /* If the next token is an ellipsis, and we have not seen a
16069      declarator name, and the type of the declarator contains parameter
16070      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16071      a parameter pack expansion expression. Otherwise, leave the
16072      ellipsis for a C-style variadic function. */
16073   token = cp_lexer_peek_token (parser->lexer);
16074   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16075     {
16076       tree type = decl_specifiers.type;
16077
16078       if (type && DECL_P (type))
16079         type = TREE_TYPE (type);
16080
16081       if (type
16082           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16083           && declarator_can_be_parameter_pack (declarator)
16084           && (!declarator || !declarator->parameter_pack_p)
16085           && uses_parameter_packs (type))
16086         {
16087           /* Consume the `...'. */
16088           cp_lexer_consume_token (parser->lexer);
16089           maybe_warn_variadic_templates ();
16090           
16091           /* Build a pack expansion type */
16092           if (declarator)
16093             declarator->parameter_pack_p = true;
16094           else
16095             decl_specifiers.type = make_pack_expansion (type);
16096         }
16097     }
16098
16099   /* The restriction on defining new types applies only to the type
16100      of the parameter, not to the default argument.  */
16101   parser->type_definition_forbidden_message = saved_message;
16102
16103   /* If the next token is `=', then process a default argument.  */
16104   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16105     {
16106       /* Consume the `='.  */
16107       cp_lexer_consume_token (parser->lexer);
16108
16109       /* If we are defining a class, then the tokens that make up the
16110          default argument must be saved and processed later.  */
16111       if (!template_parm_p && at_class_scope_p ()
16112           && TYPE_BEING_DEFINED (current_class_type)
16113           && !LAMBDA_TYPE_P (current_class_type))
16114         {
16115           unsigned depth = 0;
16116           int maybe_template_id = 0;
16117           cp_token *first_token;
16118           cp_token *token;
16119
16120           /* Add tokens until we have processed the entire default
16121              argument.  We add the range [first_token, token).  */
16122           first_token = cp_lexer_peek_token (parser->lexer);
16123           while (true)
16124             {
16125               bool done = false;
16126
16127               /* Peek at the next token.  */
16128               token = cp_lexer_peek_token (parser->lexer);
16129               /* What we do depends on what token we have.  */
16130               switch (token->type)
16131                 {
16132                   /* In valid code, a default argument must be
16133                      immediately followed by a `,' `)', or `...'.  */
16134                 case CPP_COMMA:
16135                   if (depth == 0 && maybe_template_id)
16136                     {
16137                       /* If we've seen a '<', we might be in a
16138                          template-argument-list.  Until Core issue 325 is
16139                          resolved, we don't know how this situation ought
16140                          to be handled, so try to DTRT.  We check whether
16141                          what comes after the comma is a valid parameter
16142                          declaration list.  If it is, then the comma ends
16143                          the default argument; otherwise the default
16144                          argument continues.  */
16145                       bool error = false;
16146                       tree t;
16147
16148                       /* Set ITALP so cp_parser_parameter_declaration_list
16149                          doesn't decide to commit to this parse.  */
16150                       bool saved_italp = parser->in_template_argument_list_p;
16151                       parser->in_template_argument_list_p = true;
16152
16153                       cp_parser_parse_tentatively (parser);
16154                       cp_lexer_consume_token (parser->lexer);
16155                       begin_scope (sk_function_parms, NULL_TREE);
16156                       cp_parser_parameter_declaration_list (parser, &error);
16157                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16158                         pop_binding (DECL_NAME (t), t);
16159                       leave_scope ();
16160                       if (!cp_parser_error_occurred (parser) && !error)
16161                         done = true;
16162                       cp_parser_abort_tentative_parse (parser);
16163
16164                       parser->in_template_argument_list_p = saved_italp;
16165                       break;
16166                     }
16167                 case CPP_CLOSE_PAREN:
16168                 case CPP_ELLIPSIS:
16169                   /* If we run into a non-nested `;', `}', or `]',
16170                      then the code is invalid -- but the default
16171                      argument is certainly over.  */
16172                 case CPP_SEMICOLON:
16173                 case CPP_CLOSE_BRACE:
16174                 case CPP_CLOSE_SQUARE:
16175                   if (depth == 0)
16176                     done = true;
16177                   /* Update DEPTH, if necessary.  */
16178                   else if (token->type == CPP_CLOSE_PAREN
16179                            || token->type == CPP_CLOSE_BRACE
16180                            || token->type == CPP_CLOSE_SQUARE)
16181                     --depth;
16182                   break;
16183
16184                 case CPP_OPEN_PAREN:
16185                 case CPP_OPEN_SQUARE:
16186                 case CPP_OPEN_BRACE:
16187                   ++depth;
16188                   break;
16189
16190                 case CPP_LESS:
16191                   if (depth == 0)
16192                     /* This might be the comparison operator, or it might
16193                        start a template argument list.  */
16194                     ++maybe_template_id;
16195                   break;
16196
16197                 case CPP_RSHIFT:
16198                   if (cxx_dialect == cxx98)
16199                     break;
16200                   /* Fall through for C++0x, which treats the `>>'
16201                      operator like two `>' tokens in certain
16202                      cases.  */
16203
16204                 case CPP_GREATER:
16205                   if (depth == 0)
16206                     {
16207                       /* This might be an operator, or it might close a
16208                          template argument list.  But if a previous '<'
16209                          started a template argument list, this will have
16210                          closed it, so we can't be in one anymore.  */
16211                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16212                       if (maybe_template_id < 0)
16213                         maybe_template_id = 0;
16214                     }
16215                   break;
16216
16217                   /* If we run out of tokens, issue an error message.  */
16218                 case CPP_EOF:
16219                 case CPP_PRAGMA_EOL:
16220                   error_at (token->location, "file ends in default argument");
16221                   done = true;
16222                   break;
16223
16224                 case CPP_NAME:
16225                 case CPP_SCOPE:
16226                   /* In these cases, we should look for template-ids.
16227                      For example, if the default argument is
16228                      `X<int, double>()', we need to do name lookup to
16229                      figure out whether or not `X' is a template; if
16230                      so, the `,' does not end the default argument.
16231
16232                      That is not yet done.  */
16233                   break;
16234
16235                 default:
16236                   break;
16237                 }
16238
16239               /* If we've reached the end, stop.  */
16240               if (done)
16241                 break;
16242
16243               /* Add the token to the token block.  */
16244               token = cp_lexer_consume_token (parser->lexer);
16245             }
16246
16247           /* Create a DEFAULT_ARG to represent the unparsed default
16248              argument.  */
16249           default_argument = make_node (DEFAULT_ARG);
16250           DEFARG_TOKENS (default_argument)
16251             = cp_token_cache_new (first_token, token);
16252           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16253         }
16254       /* Outside of a class definition, we can just parse the
16255          assignment-expression.  */
16256       else
16257         {
16258           token = cp_lexer_peek_token (parser->lexer);
16259           default_argument 
16260             = cp_parser_default_argument (parser, template_parm_p);
16261         }
16262
16263       if (!parser->default_arg_ok_p)
16264         {
16265           if (flag_permissive)
16266             warning (0, "deprecated use of default argument for parameter of non-function");
16267           else
16268             {
16269               error_at (token->location,
16270                         "default arguments are only "
16271                         "permitted for function parameters");
16272               default_argument = NULL_TREE;
16273             }
16274         }
16275       else if ((declarator && declarator->parameter_pack_p)
16276                || (decl_specifiers.type
16277                    && PACK_EXPANSION_P (decl_specifiers.type)))
16278         {
16279           /* Find the name of the parameter pack.  */     
16280           cp_declarator *id_declarator = declarator;
16281           while (id_declarator && id_declarator->kind != cdk_id)
16282             id_declarator = id_declarator->declarator;
16283           
16284           if (id_declarator && id_declarator->kind == cdk_id)
16285             error_at (declarator_token_start->location,
16286                       template_parm_p 
16287                       ? "template parameter pack %qD"
16288                       " cannot have a default argument"
16289                       : "parameter pack %qD cannot have a default argument",
16290                       id_declarator->u.id.unqualified_name);
16291           else
16292             error_at (declarator_token_start->location,
16293                       template_parm_p 
16294                       ? "template parameter pack cannot have a default argument"
16295                       : "parameter pack cannot have a default argument");
16296           
16297           default_argument = NULL_TREE;
16298         }
16299     }
16300   else
16301     default_argument = NULL_TREE;
16302
16303   return make_parameter_declarator (&decl_specifiers,
16304                                     declarator,
16305                                     default_argument);
16306 }
16307
16308 /* Parse a default argument and return it.
16309
16310    TEMPLATE_PARM_P is true if this is a default argument for a
16311    non-type template parameter.  */
16312 static tree
16313 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16314 {
16315   tree default_argument = NULL_TREE;
16316   bool saved_greater_than_is_operator_p;
16317   bool saved_local_variables_forbidden_p;
16318
16319   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16320      set correctly.  */
16321   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16322   parser->greater_than_is_operator_p = !template_parm_p;
16323   /* Local variable names (and the `this' keyword) may not
16324      appear in a default argument.  */
16325   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16326   parser->local_variables_forbidden_p = true;
16327   /* Parse the assignment-expression.  */
16328   if (template_parm_p)
16329     push_deferring_access_checks (dk_no_deferred);
16330   default_argument
16331     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16332   if (template_parm_p)
16333     pop_deferring_access_checks ();
16334   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16335   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16336
16337   return default_argument;
16338 }
16339
16340 /* Parse a function-body.
16341
16342    function-body:
16343      compound_statement  */
16344
16345 static void
16346 cp_parser_function_body (cp_parser *parser)
16347 {
16348   cp_parser_compound_statement (parser, NULL, false, true);
16349 }
16350
16351 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16352    true if a ctor-initializer was present.  */
16353
16354 static bool
16355 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16356 {
16357   tree body, list;
16358   bool ctor_initializer_p;
16359   const bool check_body_p =
16360      DECL_CONSTRUCTOR_P (current_function_decl)
16361      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16362   tree last = NULL;
16363
16364   /* Begin the function body.  */
16365   body = begin_function_body ();
16366   /* Parse the optional ctor-initializer.  */
16367   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16368
16369   /* If we're parsing a constexpr constructor definition, we need
16370      to check that the constructor body is indeed empty.  However,
16371      before we get to cp_parser_function_body lot of junk has been
16372      generated, so we can't just check that we have an empty block.
16373      Rather we take a snapshot of the outermost block, and check whether
16374      cp_parser_function_body changed its state.  */
16375   if (check_body_p)
16376     {
16377       list = body;
16378       if (TREE_CODE (list) == BIND_EXPR)
16379         list = BIND_EXPR_BODY (list);
16380       if (TREE_CODE (list) == STATEMENT_LIST
16381           && STATEMENT_LIST_TAIL (list) != NULL)
16382         last = STATEMENT_LIST_TAIL (list)->stmt;
16383     }
16384   /* Parse the function-body.  */
16385   cp_parser_function_body (parser);
16386   if (check_body_p)
16387     check_constexpr_ctor_body (last, list);
16388   /* Finish the function body.  */
16389   finish_function_body (body);
16390
16391   return ctor_initializer_p;
16392 }
16393
16394 /* Parse an initializer.
16395
16396    initializer:
16397      = initializer-clause
16398      ( expression-list )
16399
16400    Returns an expression representing the initializer.  If no
16401    initializer is present, NULL_TREE is returned.
16402
16403    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16404    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16405    set to TRUE if there is no initializer present.  If there is an
16406    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16407    is set to true; otherwise it is set to false.  */
16408
16409 static tree
16410 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16411                        bool* non_constant_p)
16412 {
16413   cp_token *token;
16414   tree init;
16415
16416   /* Peek at the next token.  */
16417   token = cp_lexer_peek_token (parser->lexer);
16418
16419   /* Let our caller know whether or not this initializer was
16420      parenthesized.  */
16421   *is_direct_init = (token->type != CPP_EQ);
16422   /* Assume that the initializer is constant.  */
16423   *non_constant_p = false;
16424
16425   if (token->type == CPP_EQ)
16426     {
16427       /* Consume the `='.  */
16428       cp_lexer_consume_token (parser->lexer);
16429       /* Parse the initializer-clause.  */
16430       init = cp_parser_initializer_clause (parser, non_constant_p);
16431     }
16432   else if (token->type == CPP_OPEN_PAREN)
16433     {
16434       VEC(tree,gc) *vec;
16435       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16436                                                      /*cast_p=*/false,
16437                                                      /*allow_expansion_p=*/true,
16438                                                      non_constant_p);
16439       if (vec == NULL)
16440         return error_mark_node;
16441       init = build_tree_list_vec (vec);
16442       release_tree_vector (vec);
16443     }
16444   else if (token->type == CPP_OPEN_BRACE)
16445     {
16446       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16447       init = cp_parser_braced_list (parser, non_constant_p);
16448       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16449     }
16450   else
16451     {
16452       /* Anything else is an error.  */
16453       cp_parser_error (parser, "expected initializer");
16454       init = error_mark_node;
16455     }
16456
16457   return init;
16458 }
16459
16460 /* Parse an initializer-clause.
16461
16462    initializer-clause:
16463      assignment-expression
16464      braced-init-list
16465
16466    Returns an expression representing the initializer.
16467
16468    If the `assignment-expression' production is used the value
16469    returned is simply a representation for the expression.
16470
16471    Otherwise, calls cp_parser_braced_list.  */
16472
16473 static tree
16474 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16475 {
16476   tree initializer;
16477
16478   /* Assume the expression is constant.  */
16479   *non_constant_p = false;
16480
16481   /* If it is not a `{', then we are looking at an
16482      assignment-expression.  */
16483   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16484     {
16485       initializer
16486         = cp_parser_constant_expression (parser,
16487                                         /*allow_non_constant_p=*/true,
16488                                         non_constant_p);
16489       if (!*non_constant_p)
16490         {
16491           /* We only want to fold if this is really a constant
16492              expression.  FIXME Actually, we don't want to fold here, but in
16493              cp_finish_decl.  */
16494           tree folded = fold_non_dependent_expr (initializer);
16495           folded = maybe_constant_value (folded);
16496           if (TREE_CONSTANT (folded))
16497             initializer = folded;
16498         }
16499     }
16500   else
16501     initializer = cp_parser_braced_list (parser, non_constant_p);
16502
16503   return initializer;
16504 }
16505
16506 /* Parse a brace-enclosed initializer list.
16507
16508    braced-init-list:
16509      { initializer-list , [opt] }
16510      { }
16511
16512    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16513    the elements of the initializer-list (or NULL, if the last
16514    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16515    NULL_TREE.  There is no way to detect whether or not the optional
16516    trailing `,' was provided.  NON_CONSTANT_P is as for
16517    cp_parser_initializer.  */     
16518
16519 static tree
16520 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16521 {
16522   tree initializer;
16523
16524   /* Consume the `{' token.  */
16525   cp_lexer_consume_token (parser->lexer);
16526   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16527   initializer = make_node (CONSTRUCTOR);
16528   /* If it's not a `}', then there is a non-trivial initializer.  */
16529   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16530     {
16531       /* Parse the initializer list.  */
16532       CONSTRUCTOR_ELTS (initializer)
16533         = cp_parser_initializer_list (parser, non_constant_p);
16534       /* A trailing `,' token is allowed.  */
16535       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16536         cp_lexer_consume_token (parser->lexer);
16537     }
16538   /* Now, there should be a trailing `}'.  */
16539   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16540   TREE_TYPE (initializer) = init_list_type_node;
16541   return initializer;
16542 }
16543
16544 /* Parse an initializer-list.
16545
16546    initializer-list:
16547      initializer-clause ... [opt]
16548      initializer-list , initializer-clause ... [opt]
16549
16550    GNU Extension:
16551
16552    initializer-list:
16553      identifier : initializer-clause
16554      initializer-list, identifier : initializer-clause
16555
16556    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16557    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16558    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16559    as for cp_parser_initializer.  */
16560
16561 static VEC(constructor_elt,gc) *
16562 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16563 {
16564   VEC(constructor_elt,gc) *v = NULL;
16565
16566   /* Assume all of the expressions are constant.  */
16567   *non_constant_p = false;
16568
16569   /* Parse the rest of the list.  */
16570   while (true)
16571     {
16572       cp_token *token;
16573       tree identifier;
16574       tree initializer;
16575       bool clause_non_constant_p;
16576
16577       /* If the next token is an identifier and the following one is a
16578          colon, we are looking at the GNU designated-initializer
16579          syntax.  */
16580       if (cp_parser_allow_gnu_extensions_p (parser)
16581           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16582           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16583         {
16584           /* Warn the user that they are using an extension.  */
16585           pedwarn (input_location, OPT_pedantic, 
16586                    "ISO C++ does not allow designated initializers");
16587           /* Consume the identifier.  */
16588           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16589           /* Consume the `:'.  */
16590           cp_lexer_consume_token (parser->lexer);
16591         }
16592       else
16593         identifier = NULL_TREE;
16594
16595       /* Parse the initializer.  */
16596       initializer = cp_parser_initializer_clause (parser,
16597                                                   &clause_non_constant_p);
16598       /* If any clause is non-constant, so is the entire initializer.  */
16599       if (clause_non_constant_p)
16600         *non_constant_p = true;
16601
16602       /* If we have an ellipsis, this is an initializer pack
16603          expansion.  */
16604       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16605         {
16606           /* Consume the `...'.  */
16607           cp_lexer_consume_token (parser->lexer);
16608
16609           /* Turn the initializer into an initializer expansion.  */
16610           initializer = make_pack_expansion (initializer);
16611         }
16612
16613       /* Add it to the vector.  */
16614       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16615
16616       /* If the next token is not a comma, we have reached the end of
16617          the list.  */
16618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16619         break;
16620
16621       /* Peek at the next token.  */
16622       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16623       /* If the next token is a `}', then we're still done.  An
16624          initializer-clause can have a trailing `,' after the
16625          initializer-list and before the closing `}'.  */
16626       if (token->type == CPP_CLOSE_BRACE)
16627         break;
16628
16629       /* Consume the `,' token.  */
16630       cp_lexer_consume_token (parser->lexer);
16631     }
16632
16633   return v;
16634 }
16635
16636 /* Classes [gram.class] */
16637
16638 /* Parse a class-name.
16639
16640    class-name:
16641      identifier
16642      template-id
16643
16644    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16645    to indicate that names looked up in dependent types should be
16646    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16647    keyword has been used to indicate that the name that appears next
16648    is a template.  TAG_TYPE indicates the explicit tag given before
16649    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16650    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16651    is the class being defined in a class-head.
16652
16653    Returns the TYPE_DECL representing the class.  */
16654
16655 static tree
16656 cp_parser_class_name (cp_parser *parser,
16657                       bool typename_keyword_p,
16658                       bool template_keyword_p,
16659                       enum tag_types tag_type,
16660                       bool check_dependency_p,
16661                       bool class_head_p,
16662                       bool is_declaration)
16663 {
16664   tree decl;
16665   tree scope;
16666   bool typename_p;
16667   cp_token *token;
16668   tree identifier = NULL_TREE;
16669
16670   /* All class-names start with an identifier.  */
16671   token = cp_lexer_peek_token (parser->lexer);
16672   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16673     {
16674       cp_parser_error (parser, "expected class-name");
16675       return error_mark_node;
16676     }
16677
16678   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16679      to a template-id, so we save it here.  */
16680   scope = parser->scope;
16681   if (scope == error_mark_node)
16682     return error_mark_node;
16683
16684   /* Any name names a type if we're following the `typename' keyword
16685      in a qualified name where the enclosing scope is type-dependent.  */
16686   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16687                 && dependent_type_p (scope));
16688   /* Handle the common case (an identifier, but not a template-id)
16689      efficiently.  */
16690   if (token->type == CPP_NAME
16691       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16692     {
16693       cp_token *identifier_token;
16694       bool ambiguous_p;
16695
16696       /* Look for the identifier.  */
16697       identifier_token = cp_lexer_peek_token (parser->lexer);
16698       ambiguous_p = identifier_token->ambiguous_p;
16699       identifier = cp_parser_identifier (parser);
16700       /* If the next token isn't an identifier, we are certainly not
16701          looking at a class-name.  */
16702       if (identifier == error_mark_node)
16703         decl = error_mark_node;
16704       /* If we know this is a type-name, there's no need to look it
16705          up.  */
16706       else if (typename_p)
16707         decl = identifier;
16708       else
16709         {
16710           tree ambiguous_decls;
16711           /* If we already know that this lookup is ambiguous, then
16712              we've already issued an error message; there's no reason
16713              to check again.  */
16714           if (ambiguous_p)
16715             {
16716               cp_parser_simulate_error (parser);
16717               return error_mark_node;
16718             }
16719           /* If the next token is a `::', then the name must be a type
16720              name.
16721
16722              [basic.lookup.qual]
16723
16724              During the lookup for a name preceding the :: scope
16725              resolution operator, object, function, and enumerator
16726              names are ignored.  */
16727           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16728             tag_type = typename_type;
16729           /* Look up the name.  */
16730           decl = cp_parser_lookup_name (parser, identifier,
16731                                         tag_type,
16732                                         /*is_template=*/false,
16733                                         /*is_namespace=*/false,
16734                                         check_dependency_p,
16735                                         &ambiguous_decls,
16736                                         identifier_token->location);
16737           if (ambiguous_decls)
16738             {
16739               if (cp_parser_parsing_tentatively (parser))
16740                 cp_parser_simulate_error (parser);
16741               return error_mark_node;
16742             }
16743         }
16744     }
16745   else
16746     {
16747       /* Try a template-id.  */
16748       decl = cp_parser_template_id (parser, template_keyword_p,
16749                                     check_dependency_p,
16750                                     is_declaration);
16751       if (decl == error_mark_node)
16752         return error_mark_node;
16753     }
16754
16755   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16756
16757   /* If this is a typename, create a TYPENAME_TYPE.  */
16758   if (typename_p && decl != error_mark_node)
16759     {
16760       decl = make_typename_type (scope, decl, typename_type,
16761                                  /*complain=*/tf_error);
16762       if (decl != error_mark_node)
16763         decl = TYPE_NAME (decl);
16764     }
16765
16766   /* Check to see that it is really the name of a class.  */
16767   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16768       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16769       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16770     /* Situations like this:
16771
16772          template <typename T> struct A {
16773            typename T::template X<int>::I i;
16774          };
16775
16776        are problematic.  Is `T::template X<int>' a class-name?  The
16777        standard does not seem to be definitive, but there is no other
16778        valid interpretation of the following `::'.  Therefore, those
16779        names are considered class-names.  */
16780     {
16781       decl = make_typename_type (scope, decl, tag_type, tf_error);
16782       if (decl != error_mark_node)
16783         decl = TYPE_NAME (decl);
16784     }
16785   else if (TREE_CODE (decl) != TYPE_DECL
16786            || TREE_TYPE (decl) == error_mark_node
16787            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16788            /* In Objective-C 2.0, a classname followed by '.' starts a
16789               dot-syntax expression, and it's not a type-name.  */
16790            || (c_dialect_objc ()
16791                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16792                && objc_is_class_name (decl)))
16793     decl = error_mark_node;
16794
16795   if (decl == error_mark_node)
16796     cp_parser_error (parser, "expected class-name");
16797   else if (identifier && !parser->scope)
16798     maybe_note_name_used_in_class (identifier, decl);
16799
16800   return decl;
16801 }
16802
16803 /* Parse a class-specifier.
16804
16805    class-specifier:
16806      class-head { member-specification [opt] }
16807
16808    Returns the TREE_TYPE representing the class.  */
16809
16810 static tree
16811 cp_parser_class_specifier_1 (cp_parser* parser)
16812 {
16813   tree type;
16814   tree attributes = NULL_TREE;
16815   bool nested_name_specifier_p;
16816   unsigned saved_num_template_parameter_lists;
16817   bool saved_in_function_body;
16818   bool saved_in_unbraced_linkage_specification_p;
16819   tree old_scope = NULL_TREE;
16820   tree scope = NULL_TREE;
16821   tree bases;
16822   cp_token *closing_brace;
16823
16824   push_deferring_access_checks (dk_no_deferred);
16825
16826   /* Parse the class-head.  */
16827   type = cp_parser_class_head (parser,
16828                                &nested_name_specifier_p,
16829                                &attributes,
16830                                &bases);
16831   /* If the class-head was a semantic disaster, skip the entire body
16832      of the class.  */
16833   if (!type)
16834     {
16835       cp_parser_skip_to_end_of_block_or_statement (parser);
16836       pop_deferring_access_checks ();
16837       return error_mark_node;
16838     }
16839
16840   /* Look for the `{'.  */
16841   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16842     {
16843       pop_deferring_access_checks ();
16844       return error_mark_node;
16845     }
16846
16847   /* Process the base classes. If they're invalid, skip the 
16848      entire class body.  */
16849   if (!xref_basetypes (type, bases))
16850     {
16851       /* Consuming the closing brace yields better error messages
16852          later on.  */
16853       if (cp_parser_skip_to_closing_brace (parser))
16854         cp_lexer_consume_token (parser->lexer);
16855       pop_deferring_access_checks ();
16856       return error_mark_node;
16857     }
16858
16859   /* Issue an error message if type-definitions are forbidden here.  */
16860   cp_parser_check_type_definition (parser);
16861   /* Remember that we are defining one more class.  */
16862   ++parser->num_classes_being_defined;
16863   /* Inside the class, surrounding template-parameter-lists do not
16864      apply.  */
16865   saved_num_template_parameter_lists
16866     = parser->num_template_parameter_lists;
16867   parser->num_template_parameter_lists = 0;
16868   /* We are not in a function body.  */
16869   saved_in_function_body = parser->in_function_body;
16870   parser->in_function_body = false;
16871   /* We are not immediately inside an extern "lang" block.  */
16872   saved_in_unbraced_linkage_specification_p
16873     = parser->in_unbraced_linkage_specification_p;
16874   parser->in_unbraced_linkage_specification_p = false;
16875
16876   /* Start the class.  */
16877   if (nested_name_specifier_p)
16878     {
16879       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16880       old_scope = push_inner_scope (scope);
16881     }
16882   type = begin_class_definition (type, attributes);
16883
16884   if (type == error_mark_node)
16885     /* If the type is erroneous, skip the entire body of the class.  */
16886     cp_parser_skip_to_closing_brace (parser);
16887   else
16888     /* Parse the member-specification.  */
16889     cp_parser_member_specification_opt (parser);
16890
16891   /* Look for the trailing `}'.  */
16892   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16893   /* Look for trailing attributes to apply to this class.  */
16894   if (cp_parser_allow_gnu_extensions_p (parser))
16895     attributes = cp_parser_attributes_opt (parser);
16896   if (type != error_mark_node)
16897     type = finish_struct (type, attributes);
16898   if (nested_name_specifier_p)
16899     pop_inner_scope (old_scope, scope);
16900
16901   /* We've finished a type definition.  Check for the common syntax
16902      error of forgetting a semicolon after the definition.  We need to
16903      be careful, as we can't just check for not-a-semicolon and be done
16904      with it; the user might have typed:
16905
16906      class X { } c = ...;
16907      class X { } *p = ...;
16908
16909      and so forth.  Instead, enumerate all the possible tokens that
16910      might follow this production; if we don't see one of them, then
16911      complain and silently insert the semicolon.  */
16912   {
16913     cp_token *token = cp_lexer_peek_token (parser->lexer);
16914     bool want_semicolon = true;
16915
16916     switch (token->type)
16917       {
16918       case CPP_NAME:
16919       case CPP_SEMICOLON:
16920       case CPP_MULT:
16921       case CPP_AND:
16922       case CPP_OPEN_PAREN:
16923       case CPP_CLOSE_PAREN:
16924       case CPP_COMMA:
16925         want_semicolon = false;
16926         break;
16927
16928         /* While it's legal for type qualifiers and storage class
16929            specifiers to follow type definitions in the grammar, only
16930            compiler testsuites contain code like that.  Assume that if
16931            we see such code, then what we're really seeing is a case
16932            like:
16933
16934            class X { }
16935            const <type> var = ...;
16936
16937            or
16938
16939            class Y { }
16940            static <type> func (...) ...
16941
16942            i.e. the qualifier or specifier applies to the next
16943            declaration.  To do so, however, we need to look ahead one
16944            more token to see if *that* token is a type specifier.
16945
16946            This code could be improved to handle:
16947
16948            class Z { }
16949            static const <type> var = ...;  */
16950       case CPP_KEYWORD:
16951         if (keyword_is_decl_specifier (token->keyword))
16952           {
16953             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16954
16955             /* Handling user-defined types here would be nice, but very
16956                tricky.  */
16957             want_semicolon
16958               = (lookahead->type == CPP_KEYWORD
16959                  && keyword_begins_type_specifier (lookahead->keyword));
16960           }
16961         break;
16962       default:
16963         break;
16964       }
16965
16966     /* If we don't have a type, then something is very wrong and we
16967        shouldn't try to do anything clever.  Likewise for not seeing the
16968        closing brace.  */
16969     if (closing_brace && TYPE_P (type) && want_semicolon)
16970       {
16971         cp_token_position prev
16972           = cp_lexer_previous_token_position (parser->lexer);
16973         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16974         location_t loc = prev_token->location;
16975
16976         if (CLASSTYPE_DECLARED_CLASS (type))
16977           error_at (loc, "expected %<;%> after class definition");
16978         else if (TREE_CODE (type) == RECORD_TYPE)
16979           error_at (loc, "expected %<;%> after struct definition");
16980         else if (TREE_CODE (type) == UNION_TYPE)
16981           error_at (loc, "expected %<;%> after union definition");
16982         else
16983           gcc_unreachable ();
16984
16985         /* Unget one token and smash it to look as though we encountered
16986            a semicolon in the input stream.  */
16987         cp_lexer_set_token_position (parser->lexer, prev);
16988         token = cp_lexer_peek_token (parser->lexer);
16989         token->type = CPP_SEMICOLON;
16990         token->keyword = RID_MAX;
16991       }
16992   }
16993
16994   /* If this class is not itself within the scope of another class,
16995      then we need to parse the bodies of all of the queued function
16996      definitions.  Note that the queued functions defined in a class
16997      are not always processed immediately following the
16998      class-specifier for that class.  Consider:
16999
17000        struct A {
17001          struct B { void f() { sizeof (A); } };
17002        };
17003
17004      If `f' were processed before the processing of `A' were
17005      completed, there would be no way to compute the size of `A'.
17006      Note that the nesting we are interested in here is lexical --
17007      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17008      for:
17009
17010        struct A { struct B; };
17011        struct A::B { void f() { } };
17012
17013      there is no need to delay the parsing of `A::B::f'.  */
17014   if (--parser->num_classes_being_defined == 0)
17015     {
17016       tree fn;
17017       tree class_type = NULL_TREE;
17018       tree pushed_scope = NULL_TREE;
17019       unsigned ix;
17020       cp_default_arg_entry *e;
17021
17022       /* In a first pass, parse default arguments to the functions.
17023          Then, in a second pass, parse the bodies of the functions.
17024          This two-phased approach handles cases like:
17025
17026             struct S {
17027               void f() { g(); }
17028               void g(int i = 3);
17029             };
17030
17031          */
17032       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17033                         ix, e)
17034         {
17035           fn = e->decl;
17036           /* If there are default arguments that have not yet been processed,
17037              take care of them now.  */
17038           if (class_type != e->class_type)
17039             {
17040               if (pushed_scope)
17041                 pop_scope (pushed_scope);
17042               class_type = e->class_type;
17043               pushed_scope = push_scope (class_type);
17044             }
17045           /* Make sure that any template parameters are in scope.  */
17046           maybe_begin_member_template_processing (fn);
17047           /* Parse the default argument expressions.  */
17048           cp_parser_late_parsing_default_args (parser, fn);
17049           /* Remove any template parameters from the symbol table.  */
17050           maybe_end_member_template_processing ();
17051         }
17052       if (pushed_scope)
17053         pop_scope (pushed_scope);
17054       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17055       /* Now parse the body of the functions.  */
17056       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17057         cp_parser_late_parsing_for_member (parser, fn);
17058       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17059     }
17060
17061   /* Put back any saved access checks.  */
17062   pop_deferring_access_checks ();
17063
17064   /* Restore saved state.  */
17065   parser->in_function_body = saved_in_function_body;
17066   parser->num_template_parameter_lists
17067     = saved_num_template_parameter_lists;
17068   parser->in_unbraced_linkage_specification_p
17069     = saved_in_unbraced_linkage_specification_p;
17070
17071   return type;
17072 }
17073
17074 static tree
17075 cp_parser_class_specifier (cp_parser* parser)
17076 {
17077   tree ret;
17078   timevar_push (TV_PARSE_STRUCT);
17079   ret = cp_parser_class_specifier_1 (parser);
17080   timevar_pop (TV_PARSE_STRUCT);
17081   return ret;
17082 }
17083
17084 /* Parse a class-head.
17085
17086    class-head:
17087      class-key identifier [opt] base-clause [opt]
17088      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17089      class-key nested-name-specifier [opt] template-id
17090        base-clause [opt]
17091
17092    class-virt-specifier:
17093      final
17094
17095    GNU Extensions:
17096      class-key attributes identifier [opt] base-clause [opt]
17097      class-key attributes nested-name-specifier identifier base-clause [opt]
17098      class-key attributes nested-name-specifier [opt] template-id
17099        base-clause [opt]
17100
17101    Upon return BASES is initialized to the list of base classes (or
17102    NULL, if there are none) in the same form returned by
17103    cp_parser_base_clause.
17104
17105    Returns the TYPE of the indicated class.  Sets
17106    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17107    involving a nested-name-specifier was used, and FALSE otherwise.
17108
17109    Returns error_mark_node if this is not a class-head.
17110
17111    Returns NULL_TREE if the class-head is syntactically valid, but
17112    semantically invalid in a way that means we should skip the entire
17113    body of the class.  */
17114
17115 static tree
17116 cp_parser_class_head (cp_parser* parser,
17117                       bool* nested_name_specifier_p,
17118                       tree *attributes_p,
17119                       tree *bases)
17120 {
17121   tree nested_name_specifier;
17122   enum tag_types class_key;
17123   tree id = NULL_TREE;
17124   tree type = NULL_TREE;
17125   tree attributes;
17126   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17127   bool template_id_p = false;
17128   bool qualified_p = false;
17129   bool invalid_nested_name_p = false;
17130   bool invalid_explicit_specialization_p = false;
17131   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17132   tree pushed_scope = NULL_TREE;
17133   unsigned num_templates;
17134   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17135   /* Assume no nested-name-specifier will be present.  */
17136   *nested_name_specifier_p = false;
17137   /* Assume no template parameter lists will be used in defining the
17138      type.  */
17139   num_templates = 0;
17140   parser->colon_corrects_to_scope_p = false;
17141
17142   *bases = NULL_TREE;
17143
17144   /* Look for the class-key.  */
17145   class_key = cp_parser_class_key (parser);
17146   if (class_key == none_type)
17147     return error_mark_node;
17148
17149   /* Parse the attributes.  */
17150   attributes = cp_parser_attributes_opt (parser);
17151
17152   /* If the next token is `::', that is invalid -- but sometimes
17153      people do try to write:
17154
17155        struct ::S {};
17156
17157      Handle this gracefully by accepting the extra qualifier, and then
17158      issuing an error about it later if this really is a
17159      class-head.  If it turns out just to be an elaborated type
17160      specifier, remain silent.  */
17161   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17162     qualified_p = true;
17163
17164   push_deferring_access_checks (dk_no_check);
17165
17166   /* Determine the name of the class.  Begin by looking for an
17167      optional nested-name-specifier.  */
17168   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17169   nested_name_specifier
17170     = cp_parser_nested_name_specifier_opt (parser,
17171                                            /*typename_keyword_p=*/false,
17172                                            /*check_dependency_p=*/false,
17173                                            /*type_p=*/false,
17174                                            /*is_declaration=*/false);
17175   /* If there was a nested-name-specifier, then there *must* be an
17176      identifier.  */
17177   if (nested_name_specifier)
17178     {
17179       type_start_token = cp_lexer_peek_token (parser->lexer);
17180       /* Although the grammar says `identifier', it really means
17181          `class-name' or `template-name'.  You are only allowed to
17182          define a class that has already been declared with this
17183          syntax.
17184
17185          The proposed resolution for Core Issue 180 says that wherever
17186          you see `class T::X' you should treat `X' as a type-name.
17187
17188          It is OK to define an inaccessible class; for example:
17189
17190            class A { class B; };
17191            class A::B {};
17192
17193          We do not know if we will see a class-name, or a
17194          template-name.  We look for a class-name first, in case the
17195          class-name is a template-id; if we looked for the
17196          template-name first we would stop after the template-name.  */
17197       cp_parser_parse_tentatively (parser);
17198       type = cp_parser_class_name (parser,
17199                                    /*typename_keyword_p=*/false,
17200                                    /*template_keyword_p=*/false,
17201                                    class_type,
17202                                    /*check_dependency_p=*/false,
17203                                    /*class_head_p=*/true,
17204                                    /*is_declaration=*/false);
17205       /* If that didn't work, ignore the nested-name-specifier.  */
17206       if (!cp_parser_parse_definitely (parser))
17207         {
17208           invalid_nested_name_p = true;
17209           type_start_token = cp_lexer_peek_token (parser->lexer);
17210           id = cp_parser_identifier (parser);
17211           if (id == error_mark_node)
17212             id = NULL_TREE;
17213         }
17214       /* If we could not find a corresponding TYPE, treat this
17215          declaration like an unqualified declaration.  */
17216       if (type == error_mark_node)
17217         nested_name_specifier = NULL_TREE;
17218       /* Otherwise, count the number of templates used in TYPE and its
17219          containing scopes.  */
17220       else
17221         {
17222           tree scope;
17223
17224           for (scope = TREE_TYPE (type);
17225                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17226                scope = (TYPE_P (scope)
17227                         ? TYPE_CONTEXT (scope)
17228                         : DECL_CONTEXT (scope)))
17229             if (TYPE_P (scope)
17230                 && CLASS_TYPE_P (scope)
17231                 && CLASSTYPE_TEMPLATE_INFO (scope)
17232                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17233                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17234               ++num_templates;
17235         }
17236     }
17237   /* Otherwise, the identifier is optional.  */
17238   else
17239     {
17240       /* We don't know whether what comes next is a template-id,
17241          an identifier, or nothing at all.  */
17242       cp_parser_parse_tentatively (parser);
17243       /* Check for a template-id.  */
17244       type_start_token = cp_lexer_peek_token (parser->lexer);
17245       id = cp_parser_template_id (parser,
17246                                   /*template_keyword_p=*/false,
17247                                   /*check_dependency_p=*/true,
17248                                   /*is_declaration=*/true);
17249       /* If that didn't work, it could still be an identifier.  */
17250       if (!cp_parser_parse_definitely (parser))
17251         {
17252           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17253             {
17254               type_start_token = cp_lexer_peek_token (parser->lexer);
17255               id = cp_parser_identifier (parser);
17256             }
17257           else
17258             id = NULL_TREE;
17259         }
17260       else
17261         {
17262           template_id_p = true;
17263           ++num_templates;
17264         }
17265     }
17266
17267   pop_deferring_access_checks ();
17268
17269   if (id)
17270     {
17271       cp_parser_check_for_invalid_template_id (parser, id,
17272                                                type_start_token->location);
17273       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17274     }
17275
17276   /* If it's not a `:' or a `{' then we can't really be looking at a
17277      class-head, since a class-head only appears as part of a
17278      class-specifier.  We have to detect this situation before calling
17279      xref_tag, since that has irreversible side-effects.  */
17280   if (!cp_parser_next_token_starts_class_definition_p (parser))
17281     {
17282       cp_parser_error (parser, "expected %<{%> or %<:%>");
17283       type = error_mark_node;
17284       goto out;
17285     }
17286
17287   /* At this point, we're going ahead with the class-specifier, even
17288      if some other problem occurs.  */
17289   cp_parser_commit_to_tentative_parse (parser);
17290   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17291     {
17292       cp_parser_error (parser,
17293                        "cannot specify %<override%> for a class");
17294       type = error_mark_node;
17295       goto out;
17296     }
17297   /* Issue the error about the overly-qualified name now.  */
17298   if (qualified_p)
17299     {
17300       cp_parser_error (parser,
17301                        "global qualification of class name is invalid");
17302       type = error_mark_node;
17303       goto out;
17304     }
17305   else if (invalid_nested_name_p)
17306     {
17307       cp_parser_error (parser,
17308                        "qualified name does not name a class");
17309       type = error_mark_node;
17310       goto out;
17311     }
17312   else if (nested_name_specifier)
17313     {
17314       tree scope;
17315
17316       /* Reject typedef-names in class heads.  */
17317       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17318         {
17319           error_at (type_start_token->location,
17320                     "invalid class name in declaration of %qD",
17321                     type);
17322           type = NULL_TREE;
17323           goto done;
17324         }
17325
17326       /* Figure out in what scope the declaration is being placed.  */
17327       scope = current_scope ();
17328       /* If that scope does not contain the scope in which the
17329          class was originally declared, the program is invalid.  */
17330       if (scope && !is_ancestor (scope, nested_name_specifier))
17331         {
17332           if (at_namespace_scope_p ())
17333             error_at (type_start_token->location,
17334                       "declaration of %qD in namespace %qD which does not "
17335                       "enclose %qD",
17336                       type, scope, nested_name_specifier);
17337           else
17338             error_at (type_start_token->location,
17339                       "declaration of %qD in %qD which does not enclose %qD",
17340                       type, scope, nested_name_specifier);
17341           type = NULL_TREE;
17342           goto done;
17343         }
17344       /* [dcl.meaning]
17345
17346          A declarator-id shall not be qualified except for the
17347          definition of a ... nested class outside of its class
17348          ... [or] the definition or explicit instantiation of a
17349          class member of a namespace outside of its namespace.  */
17350       if (scope == nested_name_specifier)
17351         {
17352           permerror (nested_name_specifier_token_start->location,
17353                      "extra qualification not allowed");
17354           nested_name_specifier = NULL_TREE;
17355           num_templates = 0;
17356         }
17357     }
17358   /* An explicit-specialization must be preceded by "template <>".  If
17359      it is not, try to recover gracefully.  */
17360   if (at_namespace_scope_p ()
17361       && parser->num_template_parameter_lists == 0
17362       && template_id_p)
17363     {
17364       error_at (type_start_token->location,
17365                 "an explicit specialization must be preceded by %<template <>%>");
17366       invalid_explicit_specialization_p = true;
17367       /* Take the same action that would have been taken by
17368          cp_parser_explicit_specialization.  */
17369       ++parser->num_template_parameter_lists;
17370       begin_specialization ();
17371     }
17372   /* There must be no "return" statements between this point and the
17373      end of this function; set "type "to the correct return value and
17374      use "goto done;" to return.  */
17375   /* Make sure that the right number of template parameters were
17376      present.  */
17377   if (!cp_parser_check_template_parameters (parser, num_templates,
17378                                             type_start_token->location,
17379                                             /*declarator=*/NULL))
17380     {
17381       /* If something went wrong, there is no point in even trying to
17382          process the class-definition.  */
17383       type = NULL_TREE;
17384       goto done;
17385     }
17386
17387   /* Look up the type.  */
17388   if (template_id_p)
17389     {
17390       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17391           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17392               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17393         {
17394           error_at (type_start_token->location,
17395                     "function template %qD redeclared as a class template", id);
17396           type = error_mark_node;
17397         }
17398       else
17399         {
17400           type = TREE_TYPE (id);
17401           type = maybe_process_partial_specialization (type);
17402         }
17403       if (nested_name_specifier)
17404         pushed_scope = push_scope (nested_name_specifier);
17405     }
17406   else if (nested_name_specifier)
17407     {
17408       tree class_type;
17409
17410       /* Given:
17411
17412             template <typename T> struct S { struct T };
17413             template <typename T> struct S<T>::T { };
17414
17415          we will get a TYPENAME_TYPE when processing the definition of
17416          `S::T'.  We need to resolve it to the actual type before we
17417          try to define it.  */
17418       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17419         {
17420           class_type = resolve_typename_type (TREE_TYPE (type),
17421                                               /*only_current_p=*/false);
17422           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17423             type = TYPE_NAME (class_type);
17424           else
17425             {
17426               cp_parser_error (parser, "could not resolve typename type");
17427               type = error_mark_node;
17428             }
17429         }
17430
17431       if (maybe_process_partial_specialization (TREE_TYPE (type))
17432           == error_mark_node)
17433         {
17434           type = NULL_TREE;
17435           goto done;
17436         }
17437
17438       class_type = current_class_type;
17439       /* Enter the scope indicated by the nested-name-specifier.  */
17440       pushed_scope = push_scope (nested_name_specifier);
17441       /* Get the canonical version of this type.  */
17442       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17443       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17444           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17445         {
17446           type = push_template_decl (type);
17447           if (type == error_mark_node)
17448             {
17449               type = NULL_TREE;
17450               goto done;
17451             }
17452         }
17453
17454       type = TREE_TYPE (type);
17455       *nested_name_specifier_p = true;
17456     }
17457   else      /* The name is not a nested name.  */
17458     {
17459       /* If the class was unnamed, create a dummy name.  */
17460       if (!id)
17461         id = make_anon_name ();
17462       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17463                        parser->num_template_parameter_lists);
17464     }
17465
17466   /* Indicate whether this class was declared as a `class' or as a
17467      `struct'.  */
17468   if (TREE_CODE (type) == RECORD_TYPE)
17469     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17470   cp_parser_check_class_key (class_key, type);
17471
17472   /* If this type was already complete, and we see another definition,
17473      that's an error.  */
17474   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17475     {
17476       error_at (type_start_token->location, "redefinition of %q#T",
17477                 type);
17478       error_at (type_start_token->location, "previous definition of %q+#T",
17479                 type);
17480       type = NULL_TREE;
17481       goto done;
17482     }
17483   else if (type == error_mark_node)
17484     type = NULL_TREE;
17485
17486   /* We will have entered the scope containing the class; the names of
17487      base classes should be looked up in that context.  For example:
17488
17489        struct A { struct B {}; struct C; };
17490        struct A::C : B {};
17491
17492      is valid.  */
17493
17494   /* Get the list of base-classes, if there is one.  */
17495   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17496     *bases = cp_parser_base_clause (parser);
17497
17498  done:
17499   /* Leave the scope given by the nested-name-specifier.  We will
17500      enter the class scope itself while processing the members.  */
17501   if (pushed_scope)
17502     pop_scope (pushed_scope);
17503
17504   if (invalid_explicit_specialization_p)
17505     {
17506       end_specialization ();
17507       --parser->num_template_parameter_lists;
17508     }
17509
17510   if (type)
17511     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17512   *attributes_p = attributes;
17513   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17514     CLASSTYPE_FINAL (type) = 1;
17515  out:
17516   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17517   return type;
17518 }
17519
17520 /* Parse a class-key.
17521
17522    class-key:
17523      class
17524      struct
17525      union
17526
17527    Returns the kind of class-key specified, or none_type to indicate
17528    error.  */
17529
17530 static enum tag_types
17531 cp_parser_class_key (cp_parser* parser)
17532 {
17533   cp_token *token;
17534   enum tag_types tag_type;
17535
17536   /* Look for the class-key.  */
17537   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17538   if (!token)
17539     return none_type;
17540
17541   /* Check to see if the TOKEN is a class-key.  */
17542   tag_type = cp_parser_token_is_class_key (token);
17543   if (!tag_type)
17544     cp_parser_error (parser, "expected class-key");
17545   return tag_type;
17546 }
17547
17548 /* Parse an (optional) member-specification.
17549
17550    member-specification:
17551      member-declaration member-specification [opt]
17552      access-specifier : member-specification [opt]  */
17553
17554 static void
17555 cp_parser_member_specification_opt (cp_parser* parser)
17556 {
17557   while (true)
17558     {
17559       cp_token *token;
17560       enum rid keyword;
17561
17562       /* Peek at the next token.  */
17563       token = cp_lexer_peek_token (parser->lexer);
17564       /* If it's a `}', or EOF then we've seen all the members.  */
17565       if (token->type == CPP_CLOSE_BRACE
17566           || token->type == CPP_EOF
17567           || token->type == CPP_PRAGMA_EOL)
17568         break;
17569
17570       /* See if this token is a keyword.  */
17571       keyword = token->keyword;
17572       switch (keyword)
17573         {
17574         case RID_PUBLIC:
17575         case RID_PROTECTED:
17576         case RID_PRIVATE:
17577           /* Consume the access-specifier.  */
17578           cp_lexer_consume_token (parser->lexer);
17579           /* Remember which access-specifier is active.  */
17580           current_access_specifier = token->u.value;
17581           /* Look for the `:'.  */
17582           cp_parser_require (parser, CPP_COLON, RT_COLON);
17583           break;
17584
17585         default:
17586           /* Accept #pragmas at class scope.  */
17587           if (token->type == CPP_PRAGMA)
17588             {
17589               cp_parser_pragma (parser, pragma_external);
17590               break;
17591             }
17592
17593           /* Otherwise, the next construction must be a
17594              member-declaration.  */
17595           cp_parser_member_declaration (parser);
17596         }
17597     }
17598 }
17599
17600 /* Parse a member-declaration.
17601
17602    member-declaration:
17603      decl-specifier-seq [opt] member-declarator-list [opt] ;
17604      function-definition ; [opt]
17605      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17606      using-declaration
17607      template-declaration
17608
17609    member-declarator-list:
17610      member-declarator
17611      member-declarator-list , member-declarator
17612
17613    member-declarator:
17614      declarator pure-specifier [opt]
17615      declarator constant-initializer [opt]
17616      identifier [opt] : constant-expression
17617
17618    GNU Extensions:
17619
17620    member-declaration:
17621      __extension__ member-declaration
17622
17623    member-declarator:
17624      declarator attributes [opt] pure-specifier [opt]
17625      declarator attributes [opt] constant-initializer [opt]
17626      identifier [opt] attributes [opt] : constant-expression  
17627
17628    C++0x Extensions:
17629
17630    member-declaration:
17631      static_assert-declaration  */
17632
17633 static void
17634 cp_parser_member_declaration (cp_parser* parser)
17635 {
17636   cp_decl_specifier_seq decl_specifiers;
17637   tree prefix_attributes;
17638   tree decl;
17639   int declares_class_or_enum;
17640   bool friend_p;
17641   cp_token *token = NULL;
17642   cp_token *decl_spec_token_start = NULL;
17643   cp_token *initializer_token_start = NULL;
17644   int saved_pedantic;
17645   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17646
17647   /* Check for the `__extension__' keyword.  */
17648   if (cp_parser_extension_opt (parser, &saved_pedantic))
17649     {
17650       /* Recurse.  */
17651       cp_parser_member_declaration (parser);
17652       /* Restore the old value of the PEDANTIC flag.  */
17653       pedantic = saved_pedantic;
17654
17655       return;
17656     }
17657
17658   /* Check for a template-declaration.  */
17659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17660     {
17661       /* An explicit specialization here is an error condition, and we
17662          expect the specialization handler to detect and report this.  */
17663       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17664           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17665         cp_parser_explicit_specialization (parser);
17666       else
17667         cp_parser_template_declaration (parser, /*member_p=*/true);
17668
17669       return;
17670     }
17671
17672   /* Check for a using-declaration.  */
17673   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17674     {
17675       /* Parse the using-declaration.  */
17676       cp_parser_using_declaration (parser,
17677                                    /*access_declaration_p=*/false);
17678       return;
17679     }
17680
17681   /* Check for @defs.  */
17682   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17683     {
17684       tree ivar, member;
17685       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17686       ivar = ivar_chains;
17687       while (ivar)
17688         {
17689           member = ivar;
17690           ivar = TREE_CHAIN (member);
17691           TREE_CHAIN (member) = NULL_TREE;
17692           finish_member_declaration (member);
17693         }
17694       return;
17695     }
17696
17697   /* If the next token is `static_assert' we have a static assertion.  */
17698   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17699     {
17700       cp_parser_static_assert (parser, /*member_p=*/true);
17701       return;
17702     }
17703
17704   parser->colon_corrects_to_scope_p = false;
17705
17706   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17707     goto out;
17708
17709   /* Parse the decl-specifier-seq.  */
17710   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17711   cp_parser_decl_specifier_seq (parser,
17712                                 CP_PARSER_FLAGS_OPTIONAL,
17713                                 &decl_specifiers,
17714                                 &declares_class_or_enum);
17715   prefix_attributes = decl_specifiers.attributes;
17716   decl_specifiers.attributes = NULL_TREE;
17717   /* Check for an invalid type-name.  */
17718   if (!decl_specifiers.any_type_specifiers_p
17719       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17720     goto out;
17721   /* If there is no declarator, then the decl-specifier-seq should
17722      specify a type.  */
17723   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17724     {
17725       /* If there was no decl-specifier-seq, and the next token is a
17726          `;', then we have something like:
17727
17728            struct S { ; };
17729
17730          [class.mem]
17731
17732          Each member-declaration shall declare at least one member
17733          name of the class.  */
17734       if (!decl_specifiers.any_specifiers_p)
17735         {
17736           cp_token *token = cp_lexer_peek_token (parser->lexer);
17737           if (!in_system_header_at (token->location))
17738             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17739         }
17740       else
17741         {
17742           tree type;
17743
17744           /* See if this declaration is a friend.  */
17745           friend_p = cp_parser_friend_p (&decl_specifiers);
17746           /* If there were decl-specifiers, check to see if there was
17747              a class-declaration.  */
17748           type = check_tag_decl (&decl_specifiers);
17749           /* Nested classes have already been added to the class, but
17750              a `friend' needs to be explicitly registered.  */
17751           if (friend_p)
17752             {
17753               /* If the `friend' keyword was present, the friend must
17754                  be introduced with a class-key.  */
17755                if (!declares_class_or_enum)
17756                  error_at (decl_spec_token_start->location,
17757                            "a class-key must be used when declaring a friend");
17758                /* In this case:
17759
17760                     template <typename T> struct A {
17761                       friend struct A<T>::B;
17762                     };
17763
17764                   A<T>::B will be represented by a TYPENAME_TYPE, and
17765                   therefore not recognized by check_tag_decl.  */
17766                if (!type
17767                    && decl_specifiers.type
17768                    && TYPE_P (decl_specifiers.type))
17769                  type = decl_specifiers.type;
17770                if (!type || !TYPE_P (type))
17771                  error_at (decl_spec_token_start->location,
17772                            "friend declaration does not name a class or "
17773                            "function");
17774                else
17775                  make_friend_class (current_class_type, type,
17776                                     /*complain=*/true);
17777             }
17778           /* If there is no TYPE, an error message will already have
17779              been issued.  */
17780           else if (!type || type == error_mark_node)
17781             ;
17782           /* An anonymous aggregate has to be handled specially; such
17783              a declaration really declares a data member (with a
17784              particular type), as opposed to a nested class.  */
17785           else if (ANON_AGGR_TYPE_P (type))
17786             {
17787               /* Remove constructors and such from TYPE, now that we
17788                  know it is an anonymous aggregate.  */
17789               fixup_anonymous_aggr (type);
17790               /* And make the corresponding data member.  */
17791               decl = build_decl (decl_spec_token_start->location,
17792                                  FIELD_DECL, NULL_TREE, type);
17793               /* Add it to the class.  */
17794               finish_member_declaration (decl);
17795             }
17796           else
17797             cp_parser_check_access_in_redeclaration
17798                                               (TYPE_NAME (type),
17799                                                decl_spec_token_start->location);
17800         }
17801     }
17802   else
17803     {
17804       bool assume_semicolon = false;
17805
17806       /* See if these declarations will be friends.  */
17807       friend_p = cp_parser_friend_p (&decl_specifiers);
17808
17809       /* Keep going until we hit the `;' at the end of the
17810          declaration.  */
17811       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17812         {
17813           tree attributes = NULL_TREE;
17814           tree first_attribute;
17815
17816           /* Peek at the next token.  */
17817           token = cp_lexer_peek_token (parser->lexer);
17818
17819           /* Check for a bitfield declaration.  */
17820           if (token->type == CPP_COLON
17821               || (token->type == CPP_NAME
17822                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17823                   == CPP_COLON))
17824             {
17825               tree identifier;
17826               tree width;
17827
17828               /* Get the name of the bitfield.  Note that we cannot just
17829                  check TOKEN here because it may have been invalidated by
17830                  the call to cp_lexer_peek_nth_token above.  */
17831               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17832                 identifier = cp_parser_identifier (parser);
17833               else
17834                 identifier = NULL_TREE;
17835
17836               /* Consume the `:' token.  */
17837               cp_lexer_consume_token (parser->lexer);
17838               /* Get the width of the bitfield.  */
17839               width
17840                 = cp_parser_constant_expression (parser,
17841                                                  /*allow_non_constant=*/false,
17842                                                  NULL);
17843
17844               /* Look for attributes that apply to the bitfield.  */
17845               attributes = cp_parser_attributes_opt (parser);
17846               /* Remember which attributes are prefix attributes and
17847                  which are not.  */
17848               first_attribute = attributes;
17849               /* Combine the attributes.  */
17850               attributes = chainon (prefix_attributes, attributes);
17851
17852               /* Create the bitfield declaration.  */
17853               decl = grokbitfield (identifier
17854                                    ? make_id_declarator (NULL_TREE,
17855                                                          identifier,
17856                                                          sfk_none)
17857                                    : NULL,
17858                                    &decl_specifiers,
17859                                    width,
17860                                    attributes);
17861             }
17862           else
17863             {
17864               cp_declarator *declarator;
17865               tree initializer;
17866               tree asm_specification;
17867               int ctor_dtor_or_conv_p;
17868
17869               /* Parse the declarator.  */
17870               declarator
17871                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17872                                         &ctor_dtor_or_conv_p,
17873                                         /*parenthesized_p=*/NULL,
17874                                         /*member_p=*/true);
17875
17876               /* If something went wrong parsing the declarator, make sure
17877                  that we at least consume some tokens.  */
17878               if (declarator == cp_error_declarator)
17879                 {
17880                   /* Skip to the end of the statement.  */
17881                   cp_parser_skip_to_end_of_statement (parser);
17882                   /* If the next token is not a semicolon, that is
17883                      probably because we just skipped over the body of
17884                      a function.  So, we consume a semicolon if
17885                      present, but do not issue an error message if it
17886                      is not present.  */
17887                   if (cp_lexer_next_token_is (parser->lexer,
17888                                               CPP_SEMICOLON))
17889                     cp_lexer_consume_token (parser->lexer);
17890                   goto out;
17891                 }
17892
17893               if (declares_class_or_enum & 2)
17894                 cp_parser_check_for_definition_in_return_type
17895                                             (declarator, decl_specifiers.type,
17896                                              decl_specifiers.type_location);
17897
17898               /* Look for an asm-specification.  */
17899               asm_specification = cp_parser_asm_specification_opt (parser);
17900               /* Look for attributes that apply to the declaration.  */
17901               attributes = cp_parser_attributes_opt (parser);
17902               /* Remember which attributes are prefix attributes and
17903                  which are not.  */
17904               first_attribute = attributes;
17905               /* Combine the attributes.  */
17906               attributes = chainon (prefix_attributes, attributes);
17907
17908               /* If it's an `=', then we have a constant-initializer or a
17909                  pure-specifier.  It is not correct to parse the
17910                  initializer before registering the member declaration
17911                  since the member declaration should be in scope while
17912                  its initializer is processed.  However, the rest of the
17913                  front end does not yet provide an interface that allows
17914                  us to handle this correctly.  */
17915               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17916                 {
17917                   /* In [class.mem]:
17918
17919                      A pure-specifier shall be used only in the declaration of
17920                      a virtual function.
17921
17922                      A member-declarator can contain a constant-initializer
17923                      only if it declares a static member of integral or
17924                      enumeration type.
17925
17926                      Therefore, if the DECLARATOR is for a function, we look
17927                      for a pure-specifier; otherwise, we look for a
17928                      constant-initializer.  When we call `grokfield', it will
17929                      perform more stringent semantics checks.  */
17930                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17931                   if (function_declarator_p (declarator))
17932                     initializer = cp_parser_pure_specifier (parser);
17933                   else
17934                     /* Parse the initializer.  */
17935                     initializer = cp_parser_constant_initializer (parser);
17936                 }
17937               /* Otherwise, there is no initializer.  */
17938               else
17939                 initializer = NULL_TREE;
17940
17941               /* See if we are probably looking at a function
17942                  definition.  We are certainly not looking at a
17943                  member-declarator.  Calling `grokfield' has
17944                  side-effects, so we must not do it unless we are sure
17945                  that we are looking at a member-declarator.  */
17946               if (cp_parser_token_starts_function_definition_p
17947                   (cp_lexer_peek_token (parser->lexer)))
17948                 {
17949                   /* The grammar does not allow a pure-specifier to be
17950                      used when a member function is defined.  (It is
17951                      possible that this fact is an oversight in the
17952                      standard, since a pure function may be defined
17953                      outside of the class-specifier.  */
17954                   if (initializer)
17955                     error_at (initializer_token_start->location,
17956                               "pure-specifier on function-definition");
17957                   decl = cp_parser_save_member_function_body (parser,
17958                                                               &decl_specifiers,
17959                                                               declarator,
17960                                                               attributes);
17961                   /* If the member was not a friend, declare it here.  */
17962                   if (!friend_p)
17963                     finish_member_declaration (decl);
17964                   /* Peek at the next token.  */
17965                   token = cp_lexer_peek_token (parser->lexer);
17966                   /* If the next token is a semicolon, consume it.  */
17967                   if (token->type == CPP_SEMICOLON)
17968                     cp_lexer_consume_token (parser->lexer);
17969                   goto out;
17970                 }
17971               else
17972                 if (declarator->kind == cdk_function)
17973                   declarator->id_loc = token->location;
17974                 /* Create the declaration.  */
17975                 decl = grokfield (declarator, &decl_specifiers,
17976                                   initializer, /*init_const_expr_p=*/true,
17977                                   asm_specification,
17978                                   attributes);
17979             }
17980
17981           /* Reset PREFIX_ATTRIBUTES.  */
17982           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17983             attributes = TREE_CHAIN (attributes);
17984           if (attributes)
17985             TREE_CHAIN (attributes) = NULL_TREE;
17986
17987           /* If there is any qualification still in effect, clear it
17988              now; we will be starting fresh with the next declarator.  */
17989           parser->scope = NULL_TREE;
17990           parser->qualifying_scope = NULL_TREE;
17991           parser->object_scope = NULL_TREE;
17992           /* If it's a `,', then there are more declarators.  */
17993           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17994             cp_lexer_consume_token (parser->lexer);
17995           /* If the next token isn't a `;', then we have a parse error.  */
17996           else if (cp_lexer_next_token_is_not (parser->lexer,
17997                                                CPP_SEMICOLON))
17998             {
17999               /* The next token might be a ways away from where the
18000                  actual semicolon is missing.  Find the previous token
18001                  and use that for our error position.  */
18002               cp_token *token = cp_lexer_previous_token (parser->lexer);
18003               error_at (token->location,
18004                         "expected %<;%> at end of member declaration");
18005
18006               /* Assume that the user meant to provide a semicolon.  If
18007                  we were to cp_parser_skip_to_end_of_statement, we might
18008                  skip to a semicolon inside a member function definition
18009                  and issue nonsensical error messages.  */
18010               assume_semicolon = true;
18011             }
18012
18013           if (decl)
18014             {
18015               /* Add DECL to the list of members.  */
18016               if (!friend_p)
18017                 finish_member_declaration (decl);
18018
18019               if (TREE_CODE (decl) == FUNCTION_DECL)
18020                 cp_parser_save_default_args (parser, decl);
18021             }
18022
18023           if (assume_semicolon)
18024             goto out;
18025         }
18026     }
18027
18028   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18029  out:
18030   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18031 }
18032
18033 /* Parse a pure-specifier.
18034
18035    pure-specifier:
18036      = 0
18037
18038    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18039    Otherwise, ERROR_MARK_NODE is returned.  */
18040
18041 static tree
18042 cp_parser_pure_specifier (cp_parser* parser)
18043 {
18044   cp_token *token;
18045
18046   /* Look for the `=' token.  */
18047   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18048     return error_mark_node;
18049   /* Look for the `0' token.  */
18050   token = cp_lexer_peek_token (parser->lexer);
18051
18052   if (token->type == CPP_EOF
18053       || token->type == CPP_PRAGMA_EOL)
18054     return error_mark_node;
18055
18056   cp_lexer_consume_token (parser->lexer);
18057
18058   /* Accept = default or = delete in c++0x mode.  */
18059   if (token->keyword == RID_DEFAULT
18060       || token->keyword == RID_DELETE)
18061     {
18062       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18063       return token->u.value;
18064     }
18065
18066   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18067   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18068     {
18069       cp_parser_error (parser,
18070                        "invalid pure specifier (only %<= 0%> is allowed)");
18071       cp_parser_skip_to_end_of_statement (parser);
18072       return error_mark_node;
18073     }
18074   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18075     {
18076       error_at (token->location, "templates may not be %<virtual%>");
18077       return error_mark_node;
18078     }
18079
18080   return integer_zero_node;
18081 }
18082
18083 /* Parse a constant-initializer.
18084
18085    constant-initializer:
18086      = constant-expression
18087
18088    Returns a representation of the constant-expression.  */
18089
18090 static tree
18091 cp_parser_constant_initializer (cp_parser* parser)
18092 {
18093   /* Look for the `=' token.  */
18094   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18095     return error_mark_node;
18096
18097   /* It is invalid to write:
18098
18099        struct S { static const int i = { 7 }; };
18100
18101      */
18102   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18103     {
18104       cp_parser_error (parser,
18105                        "a brace-enclosed initializer is not allowed here");
18106       /* Consume the opening brace.  */
18107       cp_lexer_consume_token (parser->lexer);
18108       /* Skip the initializer.  */
18109       cp_parser_skip_to_closing_brace (parser);
18110       /* Look for the trailing `}'.  */
18111       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18112
18113       return error_mark_node;
18114     }
18115
18116   return cp_parser_constant_expression (parser,
18117                                         /*allow_non_constant=*/false,
18118                                         NULL);
18119 }
18120
18121 /* Derived classes [gram.class.derived] */
18122
18123 /* Parse a base-clause.
18124
18125    base-clause:
18126      : base-specifier-list
18127
18128    base-specifier-list:
18129      base-specifier ... [opt]
18130      base-specifier-list , base-specifier ... [opt]
18131
18132    Returns a TREE_LIST representing the base-classes, in the order in
18133    which they were declared.  The representation of each node is as
18134    described by cp_parser_base_specifier.
18135
18136    In the case that no bases are specified, this function will return
18137    NULL_TREE, not ERROR_MARK_NODE.  */
18138
18139 static tree
18140 cp_parser_base_clause (cp_parser* parser)
18141 {
18142   tree bases = NULL_TREE;
18143
18144   /* Look for the `:' that begins the list.  */
18145   cp_parser_require (parser, CPP_COLON, RT_COLON);
18146
18147   /* Scan the base-specifier-list.  */
18148   while (true)
18149     {
18150       cp_token *token;
18151       tree base;
18152       bool pack_expansion_p = false;
18153
18154       /* Look for the base-specifier.  */
18155       base = cp_parser_base_specifier (parser);
18156       /* Look for the (optional) ellipsis. */
18157       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18158         {
18159           /* Consume the `...'. */
18160           cp_lexer_consume_token (parser->lexer);
18161
18162           pack_expansion_p = true;
18163         }
18164
18165       /* Add BASE to the front of the list.  */
18166       if (base != error_mark_node)
18167         {
18168           if (pack_expansion_p)
18169             /* Make this a pack expansion type. */
18170             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18171           
18172
18173           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18174             {
18175               TREE_CHAIN (base) = bases;
18176               bases = base;
18177             }
18178         }
18179       /* Peek at the next token.  */
18180       token = cp_lexer_peek_token (parser->lexer);
18181       /* If it's not a comma, then the list is complete.  */
18182       if (token->type != CPP_COMMA)
18183         break;
18184       /* Consume the `,'.  */
18185       cp_lexer_consume_token (parser->lexer);
18186     }
18187
18188   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18189      base class had a qualified name.  However, the next name that
18190      appears is certainly not qualified.  */
18191   parser->scope = NULL_TREE;
18192   parser->qualifying_scope = NULL_TREE;
18193   parser->object_scope = NULL_TREE;
18194
18195   return nreverse (bases);
18196 }
18197
18198 /* Parse a base-specifier.
18199
18200    base-specifier:
18201      :: [opt] nested-name-specifier [opt] class-name
18202      virtual access-specifier [opt] :: [opt] nested-name-specifier
18203        [opt] class-name
18204      access-specifier virtual [opt] :: [opt] nested-name-specifier
18205        [opt] class-name
18206
18207    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18208    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18209    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18210    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18211
18212 static tree
18213 cp_parser_base_specifier (cp_parser* parser)
18214 {
18215   cp_token *token;
18216   bool done = false;
18217   bool virtual_p = false;
18218   bool duplicate_virtual_error_issued_p = false;
18219   bool duplicate_access_error_issued_p = false;
18220   bool class_scope_p, template_p;
18221   tree access = access_default_node;
18222   tree type;
18223
18224   /* Process the optional `virtual' and `access-specifier'.  */
18225   while (!done)
18226     {
18227       /* Peek at the next token.  */
18228       token = cp_lexer_peek_token (parser->lexer);
18229       /* Process `virtual'.  */
18230       switch (token->keyword)
18231         {
18232         case RID_VIRTUAL:
18233           /* If `virtual' appears more than once, issue an error.  */
18234           if (virtual_p && !duplicate_virtual_error_issued_p)
18235             {
18236               cp_parser_error (parser,
18237                                "%<virtual%> specified more than once in base-specified");
18238               duplicate_virtual_error_issued_p = true;
18239             }
18240
18241           virtual_p = true;
18242
18243           /* Consume the `virtual' token.  */
18244           cp_lexer_consume_token (parser->lexer);
18245
18246           break;
18247
18248         case RID_PUBLIC:
18249         case RID_PROTECTED:
18250         case RID_PRIVATE:
18251           /* If more than one access specifier appears, issue an
18252              error.  */
18253           if (access != access_default_node
18254               && !duplicate_access_error_issued_p)
18255             {
18256               cp_parser_error (parser,
18257                                "more than one access specifier in base-specified");
18258               duplicate_access_error_issued_p = true;
18259             }
18260
18261           access = ridpointers[(int) token->keyword];
18262
18263           /* Consume the access-specifier.  */
18264           cp_lexer_consume_token (parser->lexer);
18265
18266           break;
18267
18268         default:
18269           done = true;
18270           break;
18271         }
18272     }
18273   /* It is not uncommon to see programs mechanically, erroneously, use
18274      the 'typename' keyword to denote (dependent) qualified types
18275      as base classes.  */
18276   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18277     {
18278       token = cp_lexer_peek_token (parser->lexer);
18279       if (!processing_template_decl)
18280         error_at (token->location,
18281                   "keyword %<typename%> not allowed outside of templates");
18282       else
18283         error_at (token->location,
18284                   "keyword %<typename%> not allowed in this context "
18285                   "(the base class is implicitly a type)");
18286       cp_lexer_consume_token (parser->lexer);
18287     }
18288
18289   /* Look for the optional `::' operator.  */
18290   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18291   /* Look for the nested-name-specifier.  The simplest way to
18292      implement:
18293
18294        [temp.res]
18295
18296        The keyword `typename' is not permitted in a base-specifier or
18297        mem-initializer; in these contexts a qualified name that
18298        depends on a template-parameter is implicitly assumed to be a
18299        type name.
18300
18301      is to pretend that we have seen the `typename' keyword at this
18302      point.  */
18303   cp_parser_nested_name_specifier_opt (parser,
18304                                        /*typename_keyword_p=*/true,
18305                                        /*check_dependency_p=*/true,
18306                                        typename_type,
18307                                        /*is_declaration=*/true);
18308   /* If the base class is given by a qualified name, assume that names
18309      we see are type names or templates, as appropriate.  */
18310   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18311   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18312
18313   /* Finally, look for the class-name.  */
18314   type = cp_parser_class_name (parser,
18315                                class_scope_p,
18316                                template_p,
18317                                typename_type,
18318                                /*check_dependency_p=*/true,
18319                                /*class_head_p=*/false,
18320                                /*is_declaration=*/true);
18321
18322   if (type == error_mark_node)
18323     return error_mark_node;
18324
18325   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18326 }
18327
18328 /* Exception handling [gram.exception] */
18329
18330 /* Parse an (optional) exception-specification.
18331
18332    exception-specification:
18333      throw ( type-id-list [opt] )
18334
18335    Returns a TREE_LIST representing the exception-specification.  The
18336    TREE_VALUE of each node is a type.  */
18337
18338 static tree
18339 cp_parser_exception_specification_opt (cp_parser* parser)
18340 {
18341   cp_token *token;
18342   tree type_id_list;
18343   const char *saved_message;
18344
18345   /* Peek at the next token.  */
18346   token = cp_lexer_peek_token (parser->lexer);
18347
18348   /* Is it a noexcept-specification?  */
18349   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18350     {
18351       tree expr;
18352       cp_lexer_consume_token (parser->lexer);
18353
18354       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18355         {
18356           cp_lexer_consume_token (parser->lexer);
18357
18358           /* Types may not be defined in an exception-specification.  */
18359           saved_message = parser->type_definition_forbidden_message;
18360           parser->type_definition_forbidden_message
18361             = G_("types may not be defined in an exception-specification");
18362
18363           expr = cp_parser_constant_expression (parser, false, NULL);
18364
18365           /* Restore the saved message.  */
18366           parser->type_definition_forbidden_message = saved_message;
18367
18368           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18369         }
18370       else
18371         expr = boolean_true_node;
18372
18373       return build_noexcept_spec (expr, tf_warning_or_error);
18374     }
18375
18376   /* If it's not `throw', then there's no exception-specification.  */
18377   if (!cp_parser_is_keyword (token, RID_THROW))
18378     return NULL_TREE;
18379
18380 #if 0
18381   /* Enable this once a lot of code has transitioned to noexcept?  */
18382   if (cxx_dialect == cxx0x && !in_system_header)
18383     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18384              "deprecated in C++0x; use %<noexcept%> instead");
18385 #endif
18386
18387   /* Consume the `throw'.  */
18388   cp_lexer_consume_token (parser->lexer);
18389
18390   /* Look for the `('.  */
18391   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18392
18393   /* Peek at the next token.  */
18394   token = cp_lexer_peek_token (parser->lexer);
18395   /* If it's not a `)', then there is a type-id-list.  */
18396   if (token->type != CPP_CLOSE_PAREN)
18397     {
18398       /* Types may not be defined in an exception-specification.  */
18399       saved_message = parser->type_definition_forbidden_message;
18400       parser->type_definition_forbidden_message
18401         = G_("types may not be defined in an exception-specification");
18402       /* Parse the type-id-list.  */
18403       type_id_list = cp_parser_type_id_list (parser);
18404       /* Restore the saved message.  */
18405       parser->type_definition_forbidden_message = saved_message;
18406     }
18407   else
18408     type_id_list = empty_except_spec;
18409
18410   /* Look for the `)'.  */
18411   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18412
18413   return type_id_list;
18414 }
18415
18416 /* Parse an (optional) type-id-list.
18417
18418    type-id-list:
18419      type-id ... [opt]
18420      type-id-list , type-id ... [opt]
18421
18422    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18423    in the order that the types were presented.  */
18424
18425 static tree
18426 cp_parser_type_id_list (cp_parser* parser)
18427 {
18428   tree types = NULL_TREE;
18429
18430   while (true)
18431     {
18432       cp_token *token;
18433       tree type;
18434
18435       /* Get the next type-id.  */
18436       type = cp_parser_type_id (parser);
18437       /* Parse the optional ellipsis. */
18438       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18439         {
18440           /* Consume the `...'. */
18441           cp_lexer_consume_token (parser->lexer);
18442
18443           /* Turn the type into a pack expansion expression. */
18444           type = make_pack_expansion (type);
18445         }
18446       /* Add it to the list.  */
18447       types = add_exception_specifier (types, type, /*complain=*/1);
18448       /* Peek at the next token.  */
18449       token = cp_lexer_peek_token (parser->lexer);
18450       /* If it is not a `,', we are done.  */
18451       if (token->type != CPP_COMMA)
18452         break;
18453       /* Consume the `,'.  */
18454       cp_lexer_consume_token (parser->lexer);
18455     }
18456
18457   return nreverse (types);
18458 }
18459
18460 /* Parse a try-block.
18461
18462    try-block:
18463      try compound-statement handler-seq  */
18464
18465 static tree
18466 cp_parser_try_block (cp_parser* parser)
18467 {
18468   tree try_block;
18469
18470   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18471   try_block = begin_try_block ();
18472   cp_parser_compound_statement (parser, NULL, true, false);
18473   finish_try_block (try_block);
18474   cp_parser_handler_seq (parser);
18475   finish_handler_sequence (try_block);
18476
18477   return try_block;
18478 }
18479
18480 /* Parse a function-try-block.
18481
18482    function-try-block:
18483      try ctor-initializer [opt] function-body handler-seq  */
18484
18485 static bool
18486 cp_parser_function_try_block (cp_parser* parser)
18487 {
18488   tree compound_stmt;
18489   tree try_block;
18490   bool ctor_initializer_p;
18491
18492   /* Look for the `try' keyword.  */
18493   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18494     return false;
18495   /* Let the rest of the front end know where we are.  */
18496   try_block = begin_function_try_block (&compound_stmt);
18497   /* Parse the function-body.  */
18498   ctor_initializer_p
18499     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18500   /* We're done with the `try' part.  */
18501   finish_function_try_block (try_block);
18502   /* Parse the handlers.  */
18503   cp_parser_handler_seq (parser);
18504   /* We're done with the handlers.  */
18505   finish_function_handler_sequence (try_block, compound_stmt);
18506
18507   return ctor_initializer_p;
18508 }
18509
18510 /* Parse a handler-seq.
18511
18512    handler-seq:
18513      handler handler-seq [opt]  */
18514
18515 static void
18516 cp_parser_handler_seq (cp_parser* parser)
18517 {
18518   while (true)
18519     {
18520       cp_token *token;
18521
18522       /* Parse the handler.  */
18523       cp_parser_handler (parser);
18524       /* Peek at the next token.  */
18525       token = cp_lexer_peek_token (parser->lexer);
18526       /* If it's not `catch' then there are no more handlers.  */
18527       if (!cp_parser_is_keyword (token, RID_CATCH))
18528         break;
18529     }
18530 }
18531
18532 /* Parse a handler.
18533
18534    handler:
18535      catch ( exception-declaration ) compound-statement  */
18536
18537 static void
18538 cp_parser_handler (cp_parser* parser)
18539 {
18540   tree handler;
18541   tree declaration;
18542
18543   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18544   handler = begin_handler ();
18545   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18546   declaration = cp_parser_exception_declaration (parser);
18547   finish_handler_parms (declaration, handler);
18548   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18549   cp_parser_compound_statement (parser, NULL, false, false);
18550   finish_handler (handler);
18551 }
18552
18553 /* Parse an exception-declaration.
18554
18555    exception-declaration:
18556      type-specifier-seq declarator
18557      type-specifier-seq abstract-declarator
18558      type-specifier-seq
18559      ...
18560
18561    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18562    ellipsis variant is used.  */
18563
18564 static tree
18565 cp_parser_exception_declaration (cp_parser* parser)
18566 {
18567   cp_decl_specifier_seq type_specifiers;
18568   cp_declarator *declarator;
18569   const char *saved_message;
18570
18571   /* If it's an ellipsis, it's easy to handle.  */
18572   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18573     {
18574       /* Consume the `...' token.  */
18575       cp_lexer_consume_token (parser->lexer);
18576       return NULL_TREE;
18577     }
18578
18579   /* Types may not be defined in exception-declarations.  */
18580   saved_message = parser->type_definition_forbidden_message;
18581   parser->type_definition_forbidden_message
18582     = G_("types may not be defined in exception-declarations");
18583
18584   /* Parse the type-specifier-seq.  */
18585   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18586                                 /*is_trailing_return=*/false,
18587                                 &type_specifiers);
18588   /* If it's a `)', then there is no declarator.  */
18589   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18590     declarator = NULL;
18591   else
18592     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18593                                        /*ctor_dtor_or_conv_p=*/NULL,
18594                                        /*parenthesized_p=*/NULL,
18595                                        /*member_p=*/false);
18596
18597   /* Restore the saved message.  */
18598   parser->type_definition_forbidden_message = saved_message;
18599
18600   if (!type_specifiers.any_specifiers_p)
18601     return error_mark_node;
18602
18603   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18604 }
18605
18606 /* Parse a throw-expression.
18607
18608    throw-expression:
18609      throw assignment-expression [opt]
18610
18611    Returns a THROW_EXPR representing the throw-expression.  */
18612
18613 static tree
18614 cp_parser_throw_expression (cp_parser* parser)
18615 {
18616   tree expression;
18617   cp_token* token;
18618
18619   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18620   token = cp_lexer_peek_token (parser->lexer);
18621   /* Figure out whether or not there is an assignment-expression
18622      following the "throw" keyword.  */
18623   if (token->type == CPP_COMMA
18624       || token->type == CPP_SEMICOLON
18625       || token->type == CPP_CLOSE_PAREN
18626       || token->type == CPP_CLOSE_SQUARE
18627       || token->type == CPP_CLOSE_BRACE
18628       || token->type == CPP_COLON)
18629     expression = NULL_TREE;
18630   else
18631     expression = cp_parser_assignment_expression (parser,
18632                                                   /*cast_p=*/false, NULL);
18633
18634   return build_throw (expression);
18635 }
18636
18637 /* GNU Extensions */
18638
18639 /* Parse an (optional) asm-specification.
18640
18641    asm-specification:
18642      asm ( string-literal )
18643
18644    If the asm-specification is present, returns a STRING_CST
18645    corresponding to the string-literal.  Otherwise, returns
18646    NULL_TREE.  */
18647
18648 static tree
18649 cp_parser_asm_specification_opt (cp_parser* parser)
18650 {
18651   cp_token *token;
18652   tree asm_specification;
18653
18654   /* Peek at the next token.  */
18655   token = cp_lexer_peek_token (parser->lexer);
18656   /* If the next token isn't the `asm' keyword, then there's no
18657      asm-specification.  */
18658   if (!cp_parser_is_keyword (token, RID_ASM))
18659     return NULL_TREE;
18660
18661   /* Consume the `asm' token.  */
18662   cp_lexer_consume_token (parser->lexer);
18663   /* Look for the `('.  */
18664   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18665
18666   /* Look for the string-literal.  */
18667   asm_specification = cp_parser_string_literal (parser, false, false);
18668
18669   /* Look for the `)'.  */
18670   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18671
18672   return asm_specification;
18673 }
18674
18675 /* Parse an asm-operand-list.
18676
18677    asm-operand-list:
18678      asm-operand
18679      asm-operand-list , asm-operand
18680
18681    asm-operand:
18682      string-literal ( expression )
18683      [ string-literal ] string-literal ( expression )
18684
18685    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18686    each node is the expression.  The TREE_PURPOSE is itself a
18687    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18688    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18689    is a STRING_CST for the string literal before the parenthesis. Returns
18690    ERROR_MARK_NODE if any of the operands are invalid.  */
18691
18692 static tree
18693 cp_parser_asm_operand_list (cp_parser* parser)
18694 {
18695   tree asm_operands = NULL_TREE;
18696   bool invalid_operands = false;
18697
18698   while (true)
18699     {
18700       tree string_literal;
18701       tree expression;
18702       tree name;
18703
18704       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18705         {
18706           /* Consume the `[' token.  */
18707           cp_lexer_consume_token (parser->lexer);
18708           /* Read the operand name.  */
18709           name = cp_parser_identifier (parser);
18710           if (name != error_mark_node)
18711             name = build_string (IDENTIFIER_LENGTH (name),
18712                                  IDENTIFIER_POINTER (name));
18713           /* Look for the closing `]'.  */
18714           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18715         }
18716       else
18717         name = NULL_TREE;
18718       /* Look for the string-literal.  */
18719       string_literal = cp_parser_string_literal (parser, false, false);
18720
18721       /* Look for the `('.  */
18722       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18723       /* Parse the expression.  */
18724       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18725       /* Look for the `)'.  */
18726       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18727
18728       if (name == error_mark_node 
18729           || string_literal == error_mark_node 
18730           || expression == error_mark_node)
18731         invalid_operands = true;
18732
18733       /* Add this operand to the list.  */
18734       asm_operands = tree_cons (build_tree_list (name, string_literal),
18735                                 expression,
18736                                 asm_operands);
18737       /* If the next token is not a `,', there are no more
18738          operands.  */
18739       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18740         break;
18741       /* Consume the `,'.  */
18742       cp_lexer_consume_token (parser->lexer);
18743     }
18744
18745   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18746 }
18747
18748 /* Parse an asm-clobber-list.
18749
18750    asm-clobber-list:
18751      string-literal
18752      asm-clobber-list , string-literal
18753
18754    Returns a TREE_LIST, indicating the clobbers in the order that they
18755    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18756
18757 static tree
18758 cp_parser_asm_clobber_list (cp_parser* parser)
18759 {
18760   tree clobbers = NULL_TREE;
18761
18762   while (true)
18763     {
18764       tree string_literal;
18765
18766       /* Look for the string literal.  */
18767       string_literal = cp_parser_string_literal (parser, false, false);
18768       /* Add it to the list.  */
18769       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18770       /* If the next token is not a `,', then the list is
18771          complete.  */
18772       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18773         break;
18774       /* Consume the `,' token.  */
18775       cp_lexer_consume_token (parser->lexer);
18776     }
18777
18778   return clobbers;
18779 }
18780
18781 /* Parse an asm-label-list.
18782
18783    asm-label-list:
18784      identifier
18785      asm-label-list , identifier
18786
18787    Returns a TREE_LIST, indicating the labels in the order that they
18788    appeared.  The TREE_VALUE of each node is a label.  */
18789
18790 static tree
18791 cp_parser_asm_label_list (cp_parser* parser)
18792 {
18793   tree labels = NULL_TREE;
18794
18795   while (true)
18796     {
18797       tree identifier, label, name;
18798
18799       /* Look for the identifier.  */
18800       identifier = cp_parser_identifier (parser);
18801       if (!error_operand_p (identifier))
18802         {
18803           label = lookup_label (identifier);
18804           if (TREE_CODE (label) == LABEL_DECL)
18805             {
18806               TREE_USED (label) = 1;
18807               check_goto (label);
18808               name = build_string (IDENTIFIER_LENGTH (identifier),
18809                                    IDENTIFIER_POINTER (identifier));
18810               labels = tree_cons (name, label, labels);
18811             }
18812         }
18813       /* If the next token is not a `,', then the list is
18814          complete.  */
18815       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18816         break;
18817       /* Consume the `,' token.  */
18818       cp_lexer_consume_token (parser->lexer);
18819     }
18820
18821   return nreverse (labels);
18822 }
18823
18824 /* Parse an (optional) series of attributes.
18825
18826    attributes:
18827      attributes attribute
18828
18829    attribute:
18830      __attribute__ (( attribute-list [opt] ))
18831
18832    The return value is as for cp_parser_attribute_list.  */
18833
18834 static tree
18835 cp_parser_attributes_opt (cp_parser* parser)
18836 {
18837   tree attributes = NULL_TREE;
18838
18839   while (true)
18840     {
18841       cp_token *token;
18842       tree attribute_list;
18843
18844       /* Peek at the next token.  */
18845       token = cp_lexer_peek_token (parser->lexer);
18846       /* If it's not `__attribute__', then we're done.  */
18847       if (token->keyword != RID_ATTRIBUTE)
18848         break;
18849
18850       /* Consume the `__attribute__' keyword.  */
18851       cp_lexer_consume_token (parser->lexer);
18852       /* Look for the two `(' tokens.  */
18853       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18854       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18855
18856       /* Peek at the next token.  */
18857       token = cp_lexer_peek_token (parser->lexer);
18858       if (token->type != CPP_CLOSE_PAREN)
18859         /* Parse the attribute-list.  */
18860         attribute_list = cp_parser_attribute_list (parser);
18861       else
18862         /* If the next token is a `)', then there is no attribute
18863            list.  */
18864         attribute_list = NULL;
18865
18866       /* Look for the two `)' tokens.  */
18867       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18868       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18869
18870       /* Add these new attributes to the list.  */
18871       attributes = chainon (attributes, attribute_list);
18872     }
18873
18874   return attributes;
18875 }
18876
18877 /* Parse an attribute-list.
18878
18879    attribute-list:
18880      attribute
18881      attribute-list , attribute
18882
18883    attribute:
18884      identifier
18885      identifier ( identifier )
18886      identifier ( identifier , expression-list )
18887      identifier ( expression-list )
18888
18889    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18890    to an attribute.  The TREE_PURPOSE of each node is the identifier
18891    indicating which attribute is in use.  The TREE_VALUE represents
18892    the arguments, if any.  */
18893
18894 static tree
18895 cp_parser_attribute_list (cp_parser* parser)
18896 {
18897   tree attribute_list = NULL_TREE;
18898   bool save_translate_strings_p = parser->translate_strings_p;
18899
18900   parser->translate_strings_p = false;
18901   while (true)
18902     {
18903       cp_token *token;
18904       tree identifier;
18905       tree attribute;
18906
18907       /* Look for the identifier.  We also allow keywords here; for
18908          example `__attribute__ ((const))' is legal.  */
18909       token = cp_lexer_peek_token (parser->lexer);
18910       if (token->type == CPP_NAME
18911           || token->type == CPP_KEYWORD)
18912         {
18913           tree arguments = NULL_TREE;
18914
18915           /* Consume the token.  */
18916           token = cp_lexer_consume_token (parser->lexer);
18917
18918           /* Save away the identifier that indicates which attribute
18919              this is.  */
18920           identifier = (token->type == CPP_KEYWORD) 
18921             /* For keywords, use the canonical spelling, not the
18922                parsed identifier.  */
18923             ? ridpointers[(int) token->keyword]
18924             : token->u.value;
18925           
18926           attribute = build_tree_list (identifier, NULL_TREE);
18927
18928           /* Peek at the next token.  */
18929           token = cp_lexer_peek_token (parser->lexer);
18930           /* If it's an `(', then parse the attribute arguments.  */
18931           if (token->type == CPP_OPEN_PAREN)
18932             {
18933               VEC(tree,gc) *vec;
18934               int attr_flag = (attribute_takes_identifier_p (identifier)
18935                                ? id_attr : normal_attr);
18936               vec = cp_parser_parenthesized_expression_list
18937                     (parser, attr_flag, /*cast_p=*/false,
18938                      /*allow_expansion_p=*/false,
18939                      /*non_constant_p=*/NULL);
18940               if (vec == NULL)
18941                 arguments = error_mark_node;
18942               else
18943                 {
18944                   arguments = build_tree_list_vec (vec);
18945                   release_tree_vector (vec);
18946                 }
18947               /* Save the arguments away.  */
18948               TREE_VALUE (attribute) = arguments;
18949             }
18950
18951           if (arguments != error_mark_node)
18952             {
18953               /* Add this attribute to the list.  */
18954               TREE_CHAIN (attribute) = attribute_list;
18955               attribute_list = attribute;
18956             }
18957
18958           token = cp_lexer_peek_token (parser->lexer);
18959         }
18960       /* Now, look for more attributes.  If the next token isn't a
18961          `,', we're done.  */
18962       if (token->type != CPP_COMMA)
18963         break;
18964
18965       /* Consume the comma and keep going.  */
18966       cp_lexer_consume_token (parser->lexer);
18967     }
18968   parser->translate_strings_p = save_translate_strings_p;
18969
18970   /* We built up the list in reverse order.  */
18971   return nreverse (attribute_list);
18972 }
18973
18974 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18975    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18976    current value of the PEDANTIC flag, regardless of whether or not
18977    the `__extension__' keyword is present.  The caller is responsible
18978    for restoring the value of the PEDANTIC flag.  */
18979
18980 static bool
18981 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18982 {
18983   /* Save the old value of the PEDANTIC flag.  */
18984   *saved_pedantic = pedantic;
18985
18986   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18987     {
18988       /* Consume the `__extension__' token.  */
18989       cp_lexer_consume_token (parser->lexer);
18990       /* We're not being pedantic while the `__extension__' keyword is
18991          in effect.  */
18992       pedantic = 0;
18993
18994       return true;
18995     }
18996
18997   return false;
18998 }
18999
19000 /* Parse a label declaration.
19001
19002    label-declaration:
19003      __label__ label-declarator-seq ;
19004
19005    label-declarator-seq:
19006      identifier , label-declarator-seq
19007      identifier  */
19008
19009 static void
19010 cp_parser_label_declaration (cp_parser* parser)
19011 {
19012   /* Look for the `__label__' keyword.  */
19013   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19014
19015   while (true)
19016     {
19017       tree identifier;
19018
19019       /* Look for an identifier.  */
19020       identifier = cp_parser_identifier (parser);
19021       /* If we failed, stop.  */
19022       if (identifier == error_mark_node)
19023         break;
19024       /* Declare it as a label.  */
19025       finish_label_decl (identifier);
19026       /* If the next token is a `;', stop.  */
19027       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19028         break;
19029       /* Look for the `,' separating the label declarations.  */
19030       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19031     }
19032
19033   /* Look for the final `;'.  */
19034   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19035 }
19036
19037 /* Support Functions */
19038
19039 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19040    NAME should have one of the representations used for an
19041    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19042    is returned.  If PARSER->SCOPE is a dependent type, then a
19043    SCOPE_REF is returned.
19044
19045    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19046    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19047    was formed.  Abstractly, such entities should not be passed to this
19048    function, because they do not need to be looked up, but it is
19049    simpler to check for this special case here, rather than at the
19050    call-sites.
19051
19052    In cases not explicitly covered above, this function returns a
19053    DECL, OVERLOAD, or baselink representing the result of the lookup.
19054    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19055    is returned.
19056
19057    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19058    (e.g., "struct") that was used.  In that case bindings that do not
19059    refer to types are ignored.
19060
19061    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19062    ignored.
19063
19064    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19065    are ignored.
19066
19067    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19068    types.
19069
19070    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19071    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19072    NULL_TREE otherwise.  */
19073
19074 static tree
19075 cp_parser_lookup_name (cp_parser *parser, tree name,
19076                        enum tag_types tag_type,
19077                        bool is_template,
19078                        bool is_namespace,
19079                        bool check_dependency,
19080                        tree *ambiguous_decls,
19081                        location_t name_location)
19082 {
19083   int flags = 0;
19084   tree decl;
19085   tree object_type = parser->context->object_type;
19086
19087   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19088     flags |= LOOKUP_COMPLAIN;
19089
19090   /* Assume that the lookup will be unambiguous.  */
19091   if (ambiguous_decls)
19092     *ambiguous_decls = NULL_TREE;
19093
19094   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19095      no longer valid.  Note that if we are parsing tentatively, and
19096      the parse fails, OBJECT_TYPE will be automatically restored.  */
19097   parser->context->object_type = NULL_TREE;
19098
19099   if (name == error_mark_node)
19100     return error_mark_node;
19101
19102   /* A template-id has already been resolved; there is no lookup to
19103      do.  */
19104   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19105     return name;
19106   if (BASELINK_P (name))
19107     {
19108       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19109                   == TEMPLATE_ID_EXPR);
19110       return name;
19111     }
19112
19113   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19114      it should already have been checked to make sure that the name
19115      used matches the type being destroyed.  */
19116   if (TREE_CODE (name) == BIT_NOT_EXPR)
19117     {
19118       tree type;
19119
19120       /* Figure out to which type this destructor applies.  */
19121       if (parser->scope)
19122         type = parser->scope;
19123       else if (object_type)
19124         type = object_type;
19125       else
19126         type = current_class_type;
19127       /* If that's not a class type, there is no destructor.  */
19128       if (!type || !CLASS_TYPE_P (type))
19129         return error_mark_node;
19130       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19131         lazily_declare_fn (sfk_destructor, type);
19132       if (!CLASSTYPE_DESTRUCTORS (type))
19133           return error_mark_node;
19134       /* If it was a class type, return the destructor.  */
19135       return CLASSTYPE_DESTRUCTORS (type);
19136     }
19137
19138   /* By this point, the NAME should be an ordinary identifier.  If
19139      the id-expression was a qualified name, the qualifying scope is
19140      stored in PARSER->SCOPE at this point.  */
19141   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19142
19143   /* Perform the lookup.  */
19144   if (parser->scope)
19145     {
19146       bool dependent_p;
19147
19148       if (parser->scope == error_mark_node)
19149         return error_mark_node;
19150
19151       /* If the SCOPE is dependent, the lookup must be deferred until
19152          the template is instantiated -- unless we are explicitly
19153          looking up names in uninstantiated templates.  Even then, we
19154          cannot look up the name if the scope is not a class type; it
19155          might, for example, be a template type parameter.  */
19156       dependent_p = (TYPE_P (parser->scope)
19157                      && dependent_scope_p (parser->scope));
19158       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19159           && dependent_p)
19160         /* Defer lookup.  */
19161         decl = error_mark_node;
19162       else
19163         {
19164           tree pushed_scope = NULL_TREE;
19165
19166           /* If PARSER->SCOPE is a dependent type, then it must be a
19167              class type, and we must not be checking dependencies;
19168              otherwise, we would have processed this lookup above.  So
19169              that PARSER->SCOPE is not considered a dependent base by
19170              lookup_member, we must enter the scope here.  */
19171           if (dependent_p)
19172             pushed_scope = push_scope (parser->scope);
19173
19174           /* If the PARSER->SCOPE is a template specialization, it
19175              may be instantiated during name lookup.  In that case,
19176              errors may be issued.  Even if we rollback the current
19177              tentative parse, those errors are valid.  */
19178           decl = lookup_qualified_name (parser->scope, name,
19179                                         tag_type != none_type,
19180                                         /*complain=*/true);
19181
19182           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19183              lookup result and the nested-name-specifier nominates a class C:
19184                * if the name specified after the nested-name-specifier, when
19185                looked up in C, is the injected-class-name of C (Clause 9), or
19186                * if the name specified after the nested-name-specifier is the
19187                same as the identifier or the simple-template-id's template-
19188                name in the last component of the nested-name-specifier,
19189              the name is instead considered to name the constructor of
19190              class C. [ Note: for example, the constructor is not an
19191              acceptable lookup result in an elaborated-type-specifier so
19192              the constructor would not be used in place of the
19193              injected-class-name. --end note ] Such a constructor name
19194              shall be used only in the declarator-id of a declaration that
19195              names a constructor or in a using-declaration.  */
19196           if (tag_type == none_type
19197               && DECL_SELF_REFERENCE_P (decl)
19198               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19199             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19200                                           tag_type != none_type,
19201                                           /*complain=*/true);
19202
19203           /* If we have a single function from a using decl, pull it out.  */
19204           if (TREE_CODE (decl) == OVERLOAD
19205               && !really_overloaded_fn (decl))
19206             decl = OVL_FUNCTION (decl);
19207
19208           if (pushed_scope)
19209             pop_scope (pushed_scope);
19210         }
19211
19212       /* If the scope is a dependent type and either we deferred lookup or
19213          we did lookup but didn't find the name, rememeber the name.  */
19214       if (decl == error_mark_node && TYPE_P (parser->scope)
19215           && dependent_type_p (parser->scope))
19216         {
19217           if (tag_type)
19218             {
19219               tree type;
19220
19221               /* The resolution to Core Issue 180 says that `struct
19222                  A::B' should be considered a type-name, even if `A'
19223                  is dependent.  */
19224               type = make_typename_type (parser->scope, name, tag_type,
19225                                          /*complain=*/tf_error);
19226               decl = TYPE_NAME (type);
19227             }
19228           else if (is_template
19229                    && (cp_parser_next_token_ends_template_argument_p (parser)
19230                        || cp_lexer_next_token_is (parser->lexer,
19231                                                   CPP_CLOSE_PAREN)))
19232             decl = make_unbound_class_template (parser->scope,
19233                                                 name, NULL_TREE,
19234                                                 /*complain=*/tf_error);
19235           else
19236             decl = build_qualified_name (/*type=*/NULL_TREE,
19237                                          parser->scope, name,
19238                                          is_template);
19239         }
19240       parser->qualifying_scope = parser->scope;
19241       parser->object_scope = NULL_TREE;
19242     }
19243   else if (object_type)
19244     {
19245       tree object_decl = NULL_TREE;
19246       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19247          OBJECT_TYPE is not a class.  */
19248       if (CLASS_TYPE_P (object_type))
19249         /* If the OBJECT_TYPE is a template specialization, it may
19250            be instantiated during name lookup.  In that case, errors
19251            may be issued.  Even if we rollback the current tentative
19252            parse, those errors are valid.  */
19253         object_decl = lookup_member (object_type,
19254                                      name,
19255                                      /*protect=*/0,
19256                                      tag_type != none_type);
19257       /* Look it up in the enclosing context, too.  */
19258       decl = lookup_name_real (name, tag_type != none_type,
19259                                /*nonclass=*/0,
19260                                /*block_p=*/true, is_namespace, flags);
19261       parser->object_scope = object_type;
19262       parser->qualifying_scope = NULL_TREE;
19263       if (object_decl)
19264         decl = object_decl;
19265     }
19266   else
19267     {
19268       decl = lookup_name_real (name, tag_type != none_type,
19269                                /*nonclass=*/0,
19270                                /*block_p=*/true, is_namespace, flags);
19271       parser->qualifying_scope = NULL_TREE;
19272       parser->object_scope = NULL_TREE;
19273     }
19274
19275   /* If the lookup failed, let our caller know.  */
19276   if (!decl || decl == error_mark_node)
19277     return error_mark_node;
19278
19279   /* Pull out the template from an injected-class-name (or multiple).  */
19280   if (is_template)
19281     decl = maybe_get_template_decl_from_type_decl (decl);
19282
19283   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19284   if (TREE_CODE (decl) == TREE_LIST)
19285     {
19286       if (ambiguous_decls)
19287         *ambiguous_decls = decl;
19288       /* The error message we have to print is too complicated for
19289          cp_parser_error, so we incorporate its actions directly.  */
19290       if (!cp_parser_simulate_error (parser))
19291         {
19292           error_at (name_location, "reference to %qD is ambiguous",
19293                     name);
19294           print_candidates (decl);
19295         }
19296       return error_mark_node;
19297     }
19298
19299   gcc_assert (DECL_P (decl)
19300               || TREE_CODE (decl) == OVERLOAD
19301               || TREE_CODE (decl) == SCOPE_REF
19302               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19303               || BASELINK_P (decl));
19304
19305   /* If we have resolved the name of a member declaration, check to
19306      see if the declaration is accessible.  When the name resolves to
19307      set of overloaded functions, accessibility is checked when
19308      overload resolution is done.
19309
19310      During an explicit instantiation, access is not checked at all,
19311      as per [temp.explicit].  */
19312   if (DECL_P (decl))
19313     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19314
19315   return decl;
19316 }
19317
19318 /* Like cp_parser_lookup_name, but for use in the typical case where
19319    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19320    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19321
19322 static tree
19323 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19324 {
19325   return cp_parser_lookup_name (parser, name,
19326                                 none_type,
19327                                 /*is_template=*/false,
19328                                 /*is_namespace=*/false,
19329                                 /*check_dependency=*/true,
19330                                 /*ambiguous_decls=*/NULL,
19331                                 location);
19332 }
19333
19334 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19335    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19336    true, the DECL indicates the class being defined in a class-head,
19337    or declared in an elaborated-type-specifier.
19338
19339    Otherwise, return DECL.  */
19340
19341 static tree
19342 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19343 {
19344   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19345      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19346
19347        struct A {
19348          template <typename T> struct B;
19349        };
19350
19351        template <typename T> struct A::B {};
19352
19353      Similarly, in an elaborated-type-specifier:
19354
19355        namespace N { struct X{}; }
19356
19357        struct A {
19358          template <typename T> friend struct N::X;
19359        };
19360
19361      However, if the DECL refers to a class type, and we are in
19362      the scope of the class, then the name lookup automatically
19363      finds the TYPE_DECL created by build_self_reference rather
19364      than a TEMPLATE_DECL.  For example, in:
19365
19366        template <class T> struct S {
19367          S s;
19368        };
19369
19370      there is no need to handle such case.  */
19371
19372   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19373     return DECL_TEMPLATE_RESULT (decl);
19374
19375   return decl;
19376 }
19377
19378 /* If too many, or too few, template-parameter lists apply to the
19379    declarator, issue an error message.  Returns TRUE if all went well,
19380    and FALSE otherwise.  */
19381
19382 static bool
19383 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19384                                                 cp_declarator *declarator,
19385                                                 location_t declarator_location)
19386 {
19387   unsigned num_templates;
19388
19389   /* We haven't seen any classes that involve template parameters yet.  */
19390   num_templates = 0;
19391
19392   switch (declarator->kind)
19393     {
19394     case cdk_id:
19395       if (declarator->u.id.qualifying_scope)
19396         {
19397           tree scope;
19398
19399           scope = declarator->u.id.qualifying_scope;
19400
19401           while (scope && CLASS_TYPE_P (scope))
19402             {
19403               /* You're supposed to have one `template <...>'
19404                  for every template class, but you don't need one
19405                  for a full specialization.  For example:
19406
19407                  template <class T> struct S{};
19408                  template <> struct S<int> { void f(); };
19409                  void S<int>::f () {}
19410
19411                  is correct; there shouldn't be a `template <>' for
19412                  the definition of `S<int>::f'.  */
19413               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19414                 /* If SCOPE does not have template information of any
19415                    kind, then it is not a template, nor is it nested
19416                    within a template.  */
19417                 break;
19418               if (explicit_class_specialization_p (scope))
19419                 break;
19420               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19421                 ++num_templates;
19422
19423               scope = TYPE_CONTEXT (scope);
19424             }
19425         }
19426       else if (TREE_CODE (declarator->u.id.unqualified_name)
19427                == TEMPLATE_ID_EXPR)
19428         /* If the DECLARATOR has the form `X<y>' then it uses one
19429            additional level of template parameters.  */
19430         ++num_templates;
19431
19432       return cp_parser_check_template_parameters 
19433         (parser, num_templates, declarator_location, declarator);
19434
19435
19436     case cdk_function:
19437     case cdk_array:
19438     case cdk_pointer:
19439     case cdk_reference:
19440     case cdk_ptrmem:
19441       return (cp_parser_check_declarator_template_parameters
19442               (parser, declarator->declarator, declarator_location));
19443
19444     case cdk_error:
19445       return true;
19446
19447     default:
19448       gcc_unreachable ();
19449     }
19450   return false;
19451 }
19452
19453 /* NUM_TEMPLATES were used in the current declaration.  If that is
19454    invalid, return FALSE and issue an error messages.  Otherwise,
19455    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19456    declarator and we can print more accurate diagnostics.  */
19457
19458 static bool
19459 cp_parser_check_template_parameters (cp_parser* parser,
19460                                      unsigned num_templates,
19461                                      location_t location,
19462                                      cp_declarator *declarator)
19463 {
19464   /* If there are the same number of template classes and parameter
19465      lists, that's OK.  */
19466   if (parser->num_template_parameter_lists == num_templates)
19467     return true;
19468   /* If there are more, but only one more, then we are referring to a
19469      member template.  That's OK too.  */
19470   if (parser->num_template_parameter_lists == num_templates + 1)
19471     return true;
19472   /* If there are more template classes than parameter lists, we have
19473      something like:
19474
19475        template <class T> void S<T>::R<T>::f ();  */
19476   if (parser->num_template_parameter_lists < num_templates)
19477     {
19478       if (declarator && !current_function_decl)
19479         error_at (location, "specializing member %<%T::%E%> "
19480                   "requires %<template<>%> syntax", 
19481                   declarator->u.id.qualifying_scope,
19482                   declarator->u.id.unqualified_name);
19483       else if (declarator)
19484         error_at (location, "invalid declaration of %<%T::%E%>",
19485                   declarator->u.id.qualifying_scope,
19486                   declarator->u.id.unqualified_name);
19487       else 
19488         error_at (location, "too few template-parameter-lists");
19489       return false;
19490     }
19491   /* Otherwise, there are too many template parameter lists.  We have
19492      something like:
19493
19494      template <class T> template <class U> void S::f();  */
19495   error_at (location, "too many template-parameter-lists");
19496   return false;
19497 }
19498
19499 /* Parse an optional `::' token indicating that the following name is
19500    from the global namespace.  If so, PARSER->SCOPE is set to the
19501    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19502    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19503    Returns the new value of PARSER->SCOPE, if the `::' token is
19504    present, and NULL_TREE otherwise.  */
19505
19506 static tree
19507 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19508 {
19509   cp_token *token;
19510
19511   /* Peek at the next token.  */
19512   token = cp_lexer_peek_token (parser->lexer);
19513   /* If we're looking at a `::' token then we're starting from the
19514      global namespace, not our current location.  */
19515   if (token->type == CPP_SCOPE)
19516     {
19517       /* Consume the `::' token.  */
19518       cp_lexer_consume_token (parser->lexer);
19519       /* Set the SCOPE so that we know where to start the lookup.  */
19520       parser->scope = global_namespace;
19521       parser->qualifying_scope = global_namespace;
19522       parser->object_scope = NULL_TREE;
19523
19524       return parser->scope;
19525     }
19526   else if (!current_scope_valid_p)
19527     {
19528       parser->scope = NULL_TREE;
19529       parser->qualifying_scope = NULL_TREE;
19530       parser->object_scope = NULL_TREE;
19531     }
19532
19533   return NULL_TREE;
19534 }
19535
19536 /* Returns TRUE if the upcoming token sequence is the start of a
19537    constructor declarator.  If FRIEND_P is true, the declarator is
19538    preceded by the `friend' specifier.  */
19539
19540 static bool
19541 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19542 {
19543   bool constructor_p;
19544   tree nested_name_specifier;
19545   cp_token *next_token;
19546
19547   /* The common case is that this is not a constructor declarator, so
19548      try to avoid doing lots of work if at all possible.  It's not
19549      valid declare a constructor at function scope.  */
19550   if (parser->in_function_body)
19551     return false;
19552   /* And only certain tokens can begin a constructor declarator.  */
19553   next_token = cp_lexer_peek_token (parser->lexer);
19554   if (next_token->type != CPP_NAME
19555       && next_token->type != CPP_SCOPE
19556       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19557       && next_token->type != CPP_TEMPLATE_ID)
19558     return false;
19559
19560   /* Parse tentatively; we are going to roll back all of the tokens
19561      consumed here.  */
19562   cp_parser_parse_tentatively (parser);
19563   /* Assume that we are looking at a constructor declarator.  */
19564   constructor_p = true;
19565
19566   /* Look for the optional `::' operator.  */
19567   cp_parser_global_scope_opt (parser,
19568                               /*current_scope_valid_p=*/false);
19569   /* Look for the nested-name-specifier.  */
19570   nested_name_specifier
19571     = (cp_parser_nested_name_specifier_opt (parser,
19572                                             /*typename_keyword_p=*/false,
19573                                             /*check_dependency_p=*/false,
19574                                             /*type_p=*/false,
19575                                             /*is_declaration=*/false));
19576   /* Outside of a class-specifier, there must be a
19577      nested-name-specifier.  */
19578   if (!nested_name_specifier &&
19579       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19580        || friend_p))
19581     constructor_p = false;
19582   else if (nested_name_specifier == error_mark_node)
19583     constructor_p = false;
19584
19585   /* If we have a class scope, this is easy; DR 147 says that S::S always
19586      names the constructor, and no other qualified name could.  */
19587   if (constructor_p && nested_name_specifier
19588       && TYPE_P (nested_name_specifier))
19589     {
19590       tree id = cp_parser_unqualified_id (parser,
19591                                           /*template_keyword_p=*/false,
19592                                           /*check_dependency_p=*/false,
19593                                           /*declarator_p=*/true,
19594                                           /*optional_p=*/false);
19595       if (is_overloaded_fn (id))
19596         id = DECL_NAME (get_first_fn (id));
19597       if (!constructor_name_p (id, nested_name_specifier))
19598         constructor_p = false;
19599     }
19600   /* If we still think that this might be a constructor-declarator,
19601      look for a class-name.  */
19602   else if (constructor_p)
19603     {
19604       /* If we have:
19605
19606            template <typename T> struct S {
19607              S();
19608            };
19609
19610          we must recognize that the nested `S' names a class.  */
19611       tree type_decl;
19612       type_decl = cp_parser_class_name (parser,
19613                                         /*typename_keyword_p=*/false,
19614                                         /*template_keyword_p=*/false,
19615                                         none_type,
19616                                         /*check_dependency_p=*/false,
19617                                         /*class_head_p=*/false,
19618                                         /*is_declaration=*/false);
19619       /* If there was no class-name, then this is not a constructor.  */
19620       constructor_p = !cp_parser_error_occurred (parser);
19621
19622       /* If we're still considering a constructor, we have to see a `(',
19623          to begin the parameter-declaration-clause, followed by either a
19624          `)', an `...', or a decl-specifier.  We need to check for a
19625          type-specifier to avoid being fooled into thinking that:
19626
19627            S (f) (int);
19628
19629          is a constructor.  (It is actually a function named `f' that
19630          takes one parameter (of type `int') and returns a value of type
19631          `S'.  */
19632       if (constructor_p
19633           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19634         constructor_p = false;
19635
19636       if (constructor_p
19637           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19638           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19639           /* A parameter declaration begins with a decl-specifier,
19640              which is either the "attribute" keyword, a storage class
19641              specifier, or (usually) a type-specifier.  */
19642           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19643         {
19644           tree type;
19645           tree pushed_scope = NULL_TREE;
19646           unsigned saved_num_template_parameter_lists;
19647
19648           /* Names appearing in the type-specifier should be looked up
19649              in the scope of the class.  */
19650           if (current_class_type)
19651             type = NULL_TREE;
19652           else
19653             {
19654               type = TREE_TYPE (type_decl);
19655               if (TREE_CODE (type) == TYPENAME_TYPE)
19656                 {
19657                   type = resolve_typename_type (type,
19658                                                 /*only_current_p=*/false);
19659                   if (TREE_CODE (type) == TYPENAME_TYPE)
19660                     {
19661                       cp_parser_abort_tentative_parse (parser);
19662                       return false;
19663                     }
19664                 }
19665               pushed_scope = push_scope (type);
19666             }
19667
19668           /* Inside the constructor parameter list, surrounding
19669              template-parameter-lists do not apply.  */
19670           saved_num_template_parameter_lists
19671             = parser->num_template_parameter_lists;
19672           parser->num_template_parameter_lists = 0;
19673
19674           /* Look for the type-specifier.  */
19675           cp_parser_type_specifier (parser,
19676                                     CP_PARSER_FLAGS_NONE,
19677                                     /*decl_specs=*/NULL,
19678                                     /*is_declarator=*/true,
19679                                     /*declares_class_or_enum=*/NULL,
19680                                     /*is_cv_qualifier=*/NULL);
19681
19682           parser->num_template_parameter_lists
19683             = saved_num_template_parameter_lists;
19684
19685           /* Leave the scope of the class.  */
19686           if (pushed_scope)
19687             pop_scope (pushed_scope);
19688
19689           constructor_p = !cp_parser_error_occurred (parser);
19690         }
19691     }
19692
19693   /* We did not really want to consume any tokens.  */
19694   cp_parser_abort_tentative_parse (parser);
19695
19696   return constructor_p;
19697 }
19698
19699 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19700    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19701    they must be performed once we are in the scope of the function.
19702
19703    Returns the function defined.  */
19704
19705 static tree
19706 cp_parser_function_definition_from_specifiers_and_declarator
19707   (cp_parser* parser,
19708    cp_decl_specifier_seq *decl_specifiers,
19709    tree attributes,
19710    const cp_declarator *declarator)
19711 {
19712   tree fn;
19713   bool success_p;
19714
19715   /* Begin the function-definition.  */
19716   success_p = start_function (decl_specifiers, declarator, attributes);
19717
19718   /* The things we're about to see are not directly qualified by any
19719      template headers we've seen thus far.  */
19720   reset_specialization ();
19721
19722   /* If there were names looked up in the decl-specifier-seq that we
19723      did not check, check them now.  We must wait until we are in the
19724      scope of the function to perform the checks, since the function
19725      might be a friend.  */
19726   perform_deferred_access_checks ();
19727
19728   if (!success_p)
19729     {
19730       /* Skip the entire function.  */
19731       cp_parser_skip_to_end_of_block_or_statement (parser);
19732       fn = error_mark_node;
19733     }
19734   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19735     {
19736       /* Seen already, skip it.  An error message has already been output.  */
19737       cp_parser_skip_to_end_of_block_or_statement (parser);
19738       fn = current_function_decl;
19739       current_function_decl = NULL_TREE;
19740       /* If this is a function from a class, pop the nested class.  */
19741       if (current_class_name)
19742         pop_nested_class ();
19743     }
19744   else
19745     {
19746       timevar_id_t tv;
19747       if (DECL_DECLARED_INLINE_P (current_function_decl))
19748         tv = TV_PARSE_INLINE;
19749       else
19750         tv = TV_PARSE_FUNC;
19751       timevar_push (tv);
19752       fn = cp_parser_function_definition_after_declarator (parser,
19753                                                          /*inline_p=*/false);
19754       timevar_pop (tv);
19755     }
19756
19757   return fn;
19758 }
19759
19760 /* Parse the part of a function-definition that follows the
19761    declarator.  INLINE_P is TRUE iff this function is an inline
19762    function defined within a class-specifier.
19763
19764    Returns the function defined.  */
19765
19766 static tree
19767 cp_parser_function_definition_after_declarator (cp_parser* parser,
19768                                                 bool inline_p)
19769 {
19770   tree fn;
19771   bool ctor_initializer_p = false;
19772   bool saved_in_unbraced_linkage_specification_p;
19773   bool saved_in_function_body;
19774   unsigned saved_num_template_parameter_lists;
19775   cp_token *token;
19776
19777   saved_in_function_body = parser->in_function_body;
19778   parser->in_function_body = true;
19779   /* If the next token is `return', then the code may be trying to
19780      make use of the "named return value" extension that G++ used to
19781      support.  */
19782   token = cp_lexer_peek_token (parser->lexer);
19783   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19784     {
19785       /* Consume the `return' keyword.  */
19786       cp_lexer_consume_token (parser->lexer);
19787       /* Look for the identifier that indicates what value is to be
19788          returned.  */
19789       cp_parser_identifier (parser);
19790       /* Issue an error message.  */
19791       error_at (token->location,
19792                 "named return values are no longer supported");
19793       /* Skip tokens until we reach the start of the function body.  */
19794       while (true)
19795         {
19796           cp_token *token = cp_lexer_peek_token (parser->lexer);
19797           if (token->type == CPP_OPEN_BRACE
19798               || token->type == CPP_EOF
19799               || token->type == CPP_PRAGMA_EOL)
19800             break;
19801           cp_lexer_consume_token (parser->lexer);
19802         }
19803     }
19804   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19805      anything declared inside `f'.  */
19806   saved_in_unbraced_linkage_specification_p
19807     = parser->in_unbraced_linkage_specification_p;
19808   parser->in_unbraced_linkage_specification_p = false;
19809   /* Inside the function, surrounding template-parameter-lists do not
19810      apply.  */
19811   saved_num_template_parameter_lists
19812     = parser->num_template_parameter_lists;
19813   parser->num_template_parameter_lists = 0;
19814
19815   start_lambda_scope (current_function_decl);
19816
19817   /* If the next token is `try', then we are looking at a
19818      function-try-block.  */
19819   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19820     ctor_initializer_p = cp_parser_function_try_block (parser);
19821   /* A function-try-block includes the function-body, so we only do
19822      this next part if we're not processing a function-try-block.  */
19823   else
19824     ctor_initializer_p
19825       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19826
19827   finish_lambda_scope ();
19828
19829   /* Finish the function.  */
19830   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19831                         (inline_p ? 2 : 0));
19832   /* Generate code for it, if necessary.  */
19833   expand_or_defer_fn (fn);
19834   /* Restore the saved values.  */
19835   parser->in_unbraced_linkage_specification_p
19836     = saved_in_unbraced_linkage_specification_p;
19837   parser->num_template_parameter_lists
19838     = saved_num_template_parameter_lists;
19839   parser->in_function_body = saved_in_function_body;
19840
19841   return fn;
19842 }
19843
19844 /* Parse a template-declaration, assuming that the `export' (and
19845    `extern') keywords, if present, has already been scanned.  MEMBER_P
19846    is as for cp_parser_template_declaration.  */
19847
19848 static void
19849 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19850 {
19851   tree decl = NULL_TREE;
19852   VEC (deferred_access_check,gc) *checks;
19853   tree parameter_list;
19854   bool friend_p = false;
19855   bool need_lang_pop;
19856   cp_token *token;
19857
19858   /* Look for the `template' keyword.  */
19859   token = cp_lexer_peek_token (parser->lexer);
19860   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19861     return;
19862
19863   /* And the `<'.  */
19864   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19865     return;
19866   if (at_class_scope_p () && current_function_decl)
19867     {
19868       /* 14.5.2.2 [temp.mem]
19869
19870          A local class shall not have member templates.  */
19871       error_at (token->location,
19872                 "invalid declaration of member template in local class");
19873       cp_parser_skip_to_end_of_block_or_statement (parser);
19874       return;
19875     }
19876   /* [temp]
19877
19878      A template ... shall not have C linkage.  */
19879   if (current_lang_name == lang_name_c)
19880     {
19881       error_at (token->location, "template with C linkage");
19882       /* Give it C++ linkage to avoid confusing other parts of the
19883          front end.  */
19884       push_lang_context (lang_name_cplusplus);
19885       need_lang_pop = true;
19886     }
19887   else
19888     need_lang_pop = false;
19889
19890   /* We cannot perform access checks on the template parameter
19891      declarations until we know what is being declared, just as we
19892      cannot check the decl-specifier list.  */
19893   push_deferring_access_checks (dk_deferred);
19894
19895   /* If the next token is `>', then we have an invalid
19896      specialization.  Rather than complain about an invalid template
19897      parameter, issue an error message here.  */
19898   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19899     {
19900       cp_parser_error (parser, "invalid explicit specialization");
19901       begin_specialization ();
19902       parameter_list = NULL_TREE;
19903     }
19904   else
19905     {
19906       /* Parse the template parameters.  */
19907       parameter_list = cp_parser_template_parameter_list (parser);
19908       fixup_template_parms ();
19909     }
19910
19911   /* Get the deferred access checks from the parameter list.  These
19912      will be checked once we know what is being declared, as for a
19913      member template the checks must be performed in the scope of the
19914      class containing the member.  */
19915   checks = get_deferred_access_checks ();
19916
19917   /* Look for the `>'.  */
19918   cp_parser_skip_to_end_of_template_parameter_list (parser);
19919   /* We just processed one more parameter list.  */
19920   ++parser->num_template_parameter_lists;
19921   /* If the next token is `template', there are more template
19922      parameters.  */
19923   if (cp_lexer_next_token_is_keyword (parser->lexer,
19924                                       RID_TEMPLATE))
19925     cp_parser_template_declaration_after_export (parser, member_p);
19926   else
19927     {
19928       /* There are no access checks when parsing a template, as we do not
19929          know if a specialization will be a friend.  */
19930       push_deferring_access_checks (dk_no_check);
19931       token = cp_lexer_peek_token (parser->lexer);
19932       decl = cp_parser_single_declaration (parser,
19933                                            checks,
19934                                            member_p,
19935                                            /*explicit_specialization_p=*/false,
19936                                            &friend_p);
19937       pop_deferring_access_checks ();
19938
19939       /* If this is a member template declaration, let the front
19940          end know.  */
19941       if (member_p && !friend_p && decl)
19942         {
19943           if (TREE_CODE (decl) == TYPE_DECL)
19944             cp_parser_check_access_in_redeclaration (decl, token->location);
19945
19946           decl = finish_member_template_decl (decl);
19947         }
19948       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19949         make_friend_class (current_class_type, TREE_TYPE (decl),
19950                            /*complain=*/true);
19951     }
19952   /* We are done with the current parameter list.  */
19953   --parser->num_template_parameter_lists;
19954
19955   pop_deferring_access_checks ();
19956
19957   /* Finish up.  */
19958   finish_template_decl (parameter_list);
19959
19960   /* Register member declarations.  */
19961   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19962     finish_member_declaration (decl);
19963   /* For the erroneous case of a template with C linkage, we pushed an
19964      implicit C++ linkage scope; exit that scope now.  */
19965   if (need_lang_pop)
19966     pop_lang_context ();
19967   /* If DECL is a function template, we must return to parse it later.
19968      (Even though there is no definition, there might be default
19969      arguments that need handling.)  */
19970   if (member_p && decl
19971       && (TREE_CODE (decl) == FUNCTION_DECL
19972           || DECL_FUNCTION_TEMPLATE_P (decl)))
19973     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19974 }
19975
19976 /* Perform the deferred access checks from a template-parameter-list.
19977    CHECKS is a TREE_LIST of access checks, as returned by
19978    get_deferred_access_checks.  */
19979
19980 static void
19981 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19982 {
19983   ++processing_template_parmlist;
19984   perform_access_checks (checks);
19985   --processing_template_parmlist;
19986 }
19987
19988 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19989    `function-definition' sequence.  MEMBER_P is true, this declaration
19990    appears in a class scope.
19991
19992    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19993    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19994
19995 static tree
19996 cp_parser_single_declaration (cp_parser* parser,
19997                               VEC (deferred_access_check,gc)* checks,
19998                               bool member_p,
19999                               bool explicit_specialization_p,
20000                               bool* friend_p)
20001 {
20002   int declares_class_or_enum;
20003   tree decl = NULL_TREE;
20004   cp_decl_specifier_seq decl_specifiers;
20005   bool function_definition_p = false;
20006   cp_token *decl_spec_token_start;
20007
20008   /* This function is only used when processing a template
20009      declaration.  */
20010   gcc_assert (innermost_scope_kind () == sk_template_parms
20011               || innermost_scope_kind () == sk_template_spec);
20012
20013   /* Defer access checks until we know what is being declared.  */
20014   push_deferring_access_checks (dk_deferred);
20015
20016   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20017      alternative.  */
20018   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20019   cp_parser_decl_specifier_seq (parser,
20020                                 CP_PARSER_FLAGS_OPTIONAL,
20021                                 &decl_specifiers,
20022                                 &declares_class_or_enum);
20023   if (friend_p)
20024     *friend_p = cp_parser_friend_p (&decl_specifiers);
20025
20026   /* There are no template typedefs.  */
20027   if (decl_specifiers.specs[(int) ds_typedef])
20028     {
20029       error_at (decl_spec_token_start->location,
20030                 "template declaration of %<typedef%>");
20031       decl = error_mark_node;
20032     }
20033
20034   /* Gather up the access checks that occurred the
20035      decl-specifier-seq.  */
20036   stop_deferring_access_checks ();
20037
20038   /* Check for the declaration of a template class.  */
20039   if (declares_class_or_enum)
20040     {
20041       if (cp_parser_declares_only_class_p (parser))
20042         {
20043           decl = shadow_tag (&decl_specifiers);
20044
20045           /* In this case:
20046
20047                struct C {
20048                  friend template <typename T> struct A<T>::B;
20049                };
20050
20051              A<T>::B will be represented by a TYPENAME_TYPE, and
20052              therefore not recognized by shadow_tag.  */
20053           if (friend_p && *friend_p
20054               && !decl
20055               && decl_specifiers.type
20056               && TYPE_P (decl_specifiers.type))
20057             decl = decl_specifiers.type;
20058
20059           if (decl && decl != error_mark_node)
20060             decl = TYPE_NAME (decl);
20061           else
20062             decl = error_mark_node;
20063
20064           /* Perform access checks for template parameters.  */
20065           cp_parser_perform_template_parameter_access_checks (checks);
20066         }
20067     }
20068
20069   /* Complain about missing 'typename' or other invalid type names.  */
20070   if (!decl_specifiers.any_type_specifiers_p
20071       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20072     {
20073       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20074          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20075          the rest of this declaration.  */
20076       decl = error_mark_node;
20077       goto out;
20078     }
20079
20080   /* If it's not a template class, try for a template function.  If
20081      the next token is a `;', then this declaration does not declare
20082      anything.  But, if there were errors in the decl-specifiers, then
20083      the error might well have come from an attempted class-specifier.
20084      In that case, there's no need to warn about a missing declarator.  */
20085   if (!decl
20086       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20087           || decl_specifiers.type != error_mark_node))
20088     {
20089       decl = cp_parser_init_declarator (parser,
20090                                         &decl_specifiers,
20091                                         checks,
20092                                         /*function_definition_allowed_p=*/true,
20093                                         member_p,
20094                                         declares_class_or_enum,
20095                                         &function_definition_p,
20096                                         NULL);
20097
20098     /* 7.1.1-1 [dcl.stc]
20099
20100        A storage-class-specifier shall not be specified in an explicit
20101        specialization...  */
20102     if (decl
20103         && explicit_specialization_p
20104         && decl_specifiers.storage_class != sc_none)
20105       {
20106         error_at (decl_spec_token_start->location,
20107                   "explicit template specialization cannot have a storage class");
20108         decl = error_mark_node;
20109       }
20110     }
20111
20112   /* Look for a trailing `;' after the declaration.  */
20113   if (!function_definition_p
20114       && (decl == error_mark_node
20115           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20116     cp_parser_skip_to_end_of_block_or_statement (parser);
20117
20118  out:
20119   pop_deferring_access_checks ();
20120
20121   /* Clear any current qualification; whatever comes next is the start
20122      of something new.  */
20123   parser->scope = NULL_TREE;
20124   parser->qualifying_scope = NULL_TREE;
20125   parser->object_scope = NULL_TREE;
20126
20127   return decl;
20128 }
20129
20130 /* Parse a cast-expression that is not the operand of a unary "&".  */
20131
20132 static tree
20133 cp_parser_simple_cast_expression (cp_parser *parser)
20134 {
20135   return cp_parser_cast_expression (parser, /*address_p=*/false,
20136                                     /*cast_p=*/false, NULL);
20137 }
20138
20139 /* Parse a functional cast to TYPE.  Returns an expression
20140    representing the cast.  */
20141
20142 static tree
20143 cp_parser_functional_cast (cp_parser* parser, tree type)
20144 {
20145   VEC(tree,gc) *vec;
20146   tree expression_list;
20147   tree cast;
20148   bool nonconst_p;
20149
20150   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20151     {
20152       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20153       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20154       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20155       if (TREE_CODE (type) == TYPE_DECL)
20156         type = TREE_TYPE (type);
20157       return finish_compound_literal (type, expression_list,
20158                                       tf_warning_or_error);
20159     }
20160
20161
20162   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20163                                                  /*cast_p=*/true,
20164                                                  /*allow_expansion_p=*/true,
20165                                                  /*non_constant_p=*/NULL);
20166   if (vec == NULL)
20167     expression_list = error_mark_node;
20168   else
20169     {
20170       expression_list = build_tree_list_vec (vec);
20171       release_tree_vector (vec);
20172     }
20173
20174   cast = build_functional_cast (type, expression_list,
20175                                 tf_warning_or_error);
20176   /* [expr.const]/1: In an integral constant expression "only type
20177      conversions to integral or enumeration type can be used".  */
20178   if (TREE_CODE (type) == TYPE_DECL)
20179     type = TREE_TYPE (type);
20180   if (cast != error_mark_node
20181       && !cast_valid_in_integral_constant_expression_p (type)
20182       && cp_parser_non_integral_constant_expression (parser,
20183                                                      NIC_CONSTRUCTOR))
20184     return error_mark_node;
20185   return cast;
20186 }
20187
20188 /* Save the tokens that make up the body of a member function defined
20189    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20190    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20191    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20192    for the member function.  */
20193
20194 static tree
20195 cp_parser_save_member_function_body (cp_parser* parser,
20196                                      cp_decl_specifier_seq *decl_specifiers,
20197                                      cp_declarator *declarator,
20198                                      tree attributes)
20199 {
20200   cp_token *first;
20201   cp_token *last;
20202   tree fn;
20203
20204   /* Create the FUNCTION_DECL.  */
20205   fn = grokmethod (decl_specifiers, declarator, attributes);
20206   /* If something went badly wrong, bail out now.  */
20207   if (fn == error_mark_node)
20208     {
20209       /* If there's a function-body, skip it.  */
20210       if (cp_parser_token_starts_function_definition_p
20211           (cp_lexer_peek_token (parser->lexer)))
20212         cp_parser_skip_to_end_of_block_or_statement (parser);
20213       return error_mark_node;
20214     }
20215
20216   /* Remember it, if there default args to post process.  */
20217   cp_parser_save_default_args (parser, fn);
20218
20219   /* Save away the tokens that make up the body of the
20220      function.  */
20221   first = parser->lexer->next_token;
20222   /* We can have braced-init-list mem-initializers before the fn body.  */
20223   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20224     {
20225       cp_lexer_consume_token (parser->lexer);
20226       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20227              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20228         {
20229           /* cache_group will stop after an un-nested { } pair, too.  */
20230           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20231             break;
20232
20233           /* variadic mem-inits have ... after the ')'.  */
20234           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20235             cp_lexer_consume_token (parser->lexer);
20236         }
20237     }
20238   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20239   /* Handle function try blocks.  */
20240   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20241     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20242   last = parser->lexer->next_token;
20243
20244   /* Save away the inline definition; we will process it when the
20245      class is complete.  */
20246   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20247   DECL_PENDING_INLINE_P (fn) = 1;
20248
20249   /* We need to know that this was defined in the class, so that
20250      friend templates are handled correctly.  */
20251   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20252
20253   /* Add FN to the queue of functions to be parsed later.  */
20254   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20255
20256   return fn;
20257 }
20258
20259 /* Parse a template-argument-list, as well as the trailing ">" (but
20260    not the opening ">").  See cp_parser_template_argument_list for the
20261    return value.  */
20262
20263 static tree
20264 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20265 {
20266   tree arguments;
20267   tree saved_scope;
20268   tree saved_qualifying_scope;
20269   tree saved_object_scope;
20270   bool saved_greater_than_is_operator_p;
20271   int saved_unevaluated_operand;
20272   int saved_inhibit_evaluation_warnings;
20273
20274   /* [temp.names]
20275
20276      When parsing a template-id, the first non-nested `>' is taken as
20277      the end of the template-argument-list rather than a greater-than
20278      operator.  */
20279   saved_greater_than_is_operator_p
20280     = parser->greater_than_is_operator_p;
20281   parser->greater_than_is_operator_p = false;
20282   /* Parsing the argument list may modify SCOPE, so we save it
20283      here.  */
20284   saved_scope = parser->scope;
20285   saved_qualifying_scope = parser->qualifying_scope;
20286   saved_object_scope = parser->object_scope;
20287   /* We need to evaluate the template arguments, even though this
20288      template-id may be nested within a "sizeof".  */
20289   saved_unevaluated_operand = cp_unevaluated_operand;
20290   cp_unevaluated_operand = 0;
20291   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20292   c_inhibit_evaluation_warnings = 0;
20293   /* Parse the template-argument-list itself.  */
20294   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20295       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20296     arguments = NULL_TREE;
20297   else
20298     arguments = cp_parser_template_argument_list (parser);
20299   /* Look for the `>' that ends the template-argument-list. If we find
20300      a '>>' instead, it's probably just a typo.  */
20301   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20302     {
20303       if (cxx_dialect != cxx98)
20304         {
20305           /* In C++0x, a `>>' in a template argument list or cast
20306              expression is considered to be two separate `>'
20307              tokens. So, change the current token to a `>', but don't
20308              consume it: it will be consumed later when the outer
20309              template argument list (or cast expression) is parsed.
20310              Note that this replacement of `>' for `>>' is necessary
20311              even if we are parsing tentatively: in the tentative
20312              case, after calling
20313              cp_parser_enclosed_template_argument_list we will always
20314              throw away all of the template arguments and the first
20315              closing `>', either because the template argument list
20316              was erroneous or because we are replacing those tokens
20317              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20318              not have been thrown away) is needed either to close an
20319              outer template argument list or to complete a new-style
20320              cast.  */
20321           cp_token *token = cp_lexer_peek_token (parser->lexer);
20322           token->type = CPP_GREATER;
20323         }
20324       else if (!saved_greater_than_is_operator_p)
20325         {
20326           /* If we're in a nested template argument list, the '>>' has
20327             to be a typo for '> >'. We emit the error message, but we
20328             continue parsing and we push a '>' as next token, so that
20329             the argument list will be parsed correctly.  Note that the
20330             global source location is still on the token before the
20331             '>>', so we need to say explicitly where we want it.  */
20332           cp_token *token = cp_lexer_peek_token (parser->lexer);
20333           error_at (token->location, "%<>>%> should be %<> >%> "
20334                     "within a nested template argument list");
20335
20336           token->type = CPP_GREATER;
20337         }
20338       else
20339         {
20340           /* If this is not a nested template argument list, the '>>'
20341             is a typo for '>'. Emit an error message and continue.
20342             Same deal about the token location, but here we can get it
20343             right by consuming the '>>' before issuing the diagnostic.  */
20344           cp_token *token = cp_lexer_consume_token (parser->lexer);
20345           error_at (token->location,
20346                     "spurious %<>>%>, use %<>%> to terminate "
20347                     "a template argument list");
20348         }
20349     }
20350   else
20351     cp_parser_skip_to_end_of_template_parameter_list (parser);
20352   /* The `>' token might be a greater-than operator again now.  */
20353   parser->greater_than_is_operator_p
20354     = saved_greater_than_is_operator_p;
20355   /* Restore the SAVED_SCOPE.  */
20356   parser->scope = saved_scope;
20357   parser->qualifying_scope = saved_qualifying_scope;
20358   parser->object_scope = saved_object_scope;
20359   cp_unevaluated_operand = saved_unevaluated_operand;
20360   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20361
20362   return arguments;
20363 }
20364
20365 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20366    arguments, or the body of the function have not yet been parsed,
20367    parse them now.  */
20368
20369 static void
20370 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20371 {
20372   timevar_push (TV_PARSE_INMETH);
20373   /* If this member is a template, get the underlying
20374      FUNCTION_DECL.  */
20375   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20376     member_function = DECL_TEMPLATE_RESULT (member_function);
20377
20378   /* There should not be any class definitions in progress at this
20379      point; the bodies of members are only parsed outside of all class
20380      definitions.  */
20381   gcc_assert (parser->num_classes_being_defined == 0);
20382   /* While we're parsing the member functions we might encounter more
20383      classes.  We want to handle them right away, but we don't want
20384      them getting mixed up with functions that are currently in the
20385      queue.  */
20386   push_unparsed_function_queues (parser);
20387
20388   /* Make sure that any template parameters are in scope.  */
20389   maybe_begin_member_template_processing (member_function);
20390
20391   /* If the body of the function has not yet been parsed, parse it
20392      now.  */
20393   if (DECL_PENDING_INLINE_P (member_function))
20394     {
20395       tree function_scope;
20396       cp_token_cache *tokens;
20397
20398       /* The function is no longer pending; we are processing it.  */
20399       tokens = DECL_PENDING_INLINE_INFO (member_function);
20400       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20401       DECL_PENDING_INLINE_P (member_function) = 0;
20402
20403       /* If this is a local class, enter the scope of the containing
20404          function.  */
20405       function_scope = current_function_decl;
20406       if (function_scope)
20407         push_function_context ();
20408
20409       /* Push the body of the function onto the lexer stack.  */
20410       cp_parser_push_lexer_for_tokens (parser, tokens);
20411
20412       /* Let the front end know that we going to be defining this
20413          function.  */
20414       start_preparsed_function (member_function, NULL_TREE,
20415                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20416
20417       /* Don't do access checking if it is a templated function.  */
20418       if (processing_template_decl)
20419         push_deferring_access_checks (dk_no_check);
20420
20421       /* Now, parse the body of the function.  */
20422       cp_parser_function_definition_after_declarator (parser,
20423                                                       /*inline_p=*/true);
20424
20425       if (processing_template_decl)
20426         pop_deferring_access_checks ();
20427
20428       /* Leave the scope of the containing function.  */
20429       if (function_scope)
20430         pop_function_context ();
20431       cp_parser_pop_lexer (parser);
20432     }
20433
20434   /* Remove any template parameters from the symbol table.  */
20435   maybe_end_member_template_processing ();
20436
20437   /* Restore the queue.  */
20438   pop_unparsed_function_queues (parser);
20439   timevar_pop (TV_PARSE_INMETH);
20440 }
20441
20442 /* If DECL contains any default args, remember it on the unparsed
20443    functions queue.  */
20444
20445 static void
20446 cp_parser_save_default_args (cp_parser* parser, tree decl)
20447 {
20448   tree probe;
20449
20450   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20451        probe;
20452        probe = TREE_CHAIN (probe))
20453     if (TREE_PURPOSE (probe))
20454       {
20455         cp_default_arg_entry *entry
20456           = VEC_safe_push (cp_default_arg_entry, gc,
20457                            unparsed_funs_with_default_args, NULL);
20458         entry->class_type = current_class_type;
20459         entry->decl = decl;
20460         break;
20461       }
20462 }
20463
20464 /* FN is a FUNCTION_DECL which may contains a parameter with an
20465    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20466    assumes that the current scope is the scope in which the default
20467    argument should be processed.  */
20468
20469 static void
20470 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20471 {
20472   bool saved_local_variables_forbidden_p;
20473   tree parm, parmdecl;
20474
20475   /* While we're parsing the default args, we might (due to the
20476      statement expression extension) encounter more classes.  We want
20477      to handle them right away, but we don't want them getting mixed
20478      up with default args that are currently in the queue.  */
20479   push_unparsed_function_queues (parser);
20480
20481   /* Local variable names (and the `this' keyword) may not appear
20482      in a default argument.  */
20483   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20484   parser->local_variables_forbidden_p = true;
20485
20486   push_defarg_context (fn);
20487
20488   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20489          parmdecl = DECL_ARGUMENTS (fn);
20490        parm && parm != void_list_node;
20491        parm = TREE_CHAIN (parm),
20492          parmdecl = DECL_CHAIN (parmdecl))
20493     {
20494       cp_token_cache *tokens;
20495       tree default_arg = TREE_PURPOSE (parm);
20496       tree parsed_arg;
20497       VEC(tree,gc) *insts;
20498       tree copy;
20499       unsigned ix;
20500
20501       if (!default_arg)
20502         continue;
20503
20504       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20505         /* This can happen for a friend declaration for a function
20506            already declared with default arguments.  */
20507         continue;
20508
20509        /* Push the saved tokens for the default argument onto the parser's
20510           lexer stack.  */
20511       tokens = DEFARG_TOKENS (default_arg);
20512       cp_parser_push_lexer_for_tokens (parser, tokens);
20513
20514       start_lambda_scope (parmdecl);
20515
20516       /* Parse the assignment-expression.  */
20517       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20518       if (parsed_arg == error_mark_node)
20519         {
20520           cp_parser_pop_lexer (parser);
20521           continue;
20522         }
20523
20524       if (!processing_template_decl)
20525         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20526
20527       TREE_PURPOSE (parm) = parsed_arg;
20528
20529       /* Update any instantiations we've already created.  */
20530       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20531            VEC_iterate (tree, insts, ix, copy); ix++)
20532         TREE_PURPOSE (copy) = parsed_arg;
20533
20534       finish_lambda_scope ();
20535
20536       /* If the token stream has not been completely used up, then
20537          there was extra junk after the end of the default
20538          argument.  */
20539       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20540         cp_parser_error (parser, "expected %<,%>");
20541
20542       /* Revert to the main lexer.  */
20543       cp_parser_pop_lexer (parser);
20544     }
20545
20546   pop_defarg_context ();
20547
20548   /* Make sure no default arg is missing.  */
20549   check_default_args (fn);
20550
20551   /* Restore the state of local_variables_forbidden_p.  */
20552   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20553
20554   /* Restore the queue.  */
20555   pop_unparsed_function_queues (parser);
20556 }
20557
20558 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20559    either a TYPE or an expression, depending on the form of the
20560    input.  The KEYWORD indicates which kind of expression we have
20561    encountered.  */
20562
20563 static tree
20564 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20565 {
20566   tree expr = NULL_TREE;
20567   const char *saved_message;
20568   char *tmp;
20569   bool saved_integral_constant_expression_p;
20570   bool saved_non_integral_constant_expression_p;
20571   bool pack_expansion_p = false;
20572
20573   /* Types cannot be defined in a `sizeof' expression.  Save away the
20574      old message.  */
20575   saved_message = parser->type_definition_forbidden_message;
20576   /* And create the new one.  */
20577   tmp = concat ("types may not be defined in %<",
20578                 IDENTIFIER_POINTER (ridpointers[keyword]),
20579                 "%> expressions", NULL);
20580   parser->type_definition_forbidden_message = tmp;
20581
20582   /* The restrictions on constant-expressions do not apply inside
20583      sizeof expressions.  */
20584   saved_integral_constant_expression_p
20585     = parser->integral_constant_expression_p;
20586   saved_non_integral_constant_expression_p
20587     = parser->non_integral_constant_expression_p;
20588   parser->integral_constant_expression_p = false;
20589
20590   /* If it's a `...', then we are computing the length of a parameter
20591      pack.  */
20592   if (keyword == RID_SIZEOF
20593       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20594     {
20595       /* Consume the `...'.  */
20596       cp_lexer_consume_token (parser->lexer);
20597       maybe_warn_variadic_templates ();
20598
20599       /* Note that this is an expansion.  */
20600       pack_expansion_p = true;
20601     }
20602
20603   /* Do not actually evaluate the expression.  */
20604   ++cp_unevaluated_operand;
20605   ++c_inhibit_evaluation_warnings;
20606   /* If it's a `(', then we might be looking at the type-id
20607      construction.  */
20608   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20609     {
20610       tree type;
20611       bool saved_in_type_id_in_expr_p;
20612
20613       /* We can't be sure yet whether we're looking at a type-id or an
20614          expression.  */
20615       cp_parser_parse_tentatively (parser);
20616       /* Consume the `('.  */
20617       cp_lexer_consume_token (parser->lexer);
20618       /* Parse the type-id.  */
20619       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20620       parser->in_type_id_in_expr_p = true;
20621       type = cp_parser_type_id (parser);
20622       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20623       /* Now, look for the trailing `)'.  */
20624       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20625       /* If all went well, then we're done.  */
20626       if (cp_parser_parse_definitely (parser))
20627         {
20628           cp_decl_specifier_seq decl_specs;
20629
20630           /* Build a trivial decl-specifier-seq.  */
20631           clear_decl_specs (&decl_specs);
20632           decl_specs.type = type;
20633
20634           /* Call grokdeclarator to figure out what type this is.  */
20635           expr = grokdeclarator (NULL,
20636                                  &decl_specs,
20637                                  TYPENAME,
20638                                  /*initialized=*/0,
20639                                  /*attrlist=*/NULL);
20640         }
20641     }
20642
20643   /* If the type-id production did not work out, then we must be
20644      looking at the unary-expression production.  */
20645   if (!expr)
20646     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20647                                        /*cast_p=*/false, NULL);
20648
20649   if (pack_expansion_p)
20650     /* Build a pack expansion. */
20651     expr = make_pack_expansion (expr);
20652
20653   /* Go back to evaluating expressions.  */
20654   --cp_unevaluated_operand;
20655   --c_inhibit_evaluation_warnings;
20656
20657   /* Free the message we created.  */
20658   free (tmp);
20659   /* And restore the old one.  */
20660   parser->type_definition_forbidden_message = saved_message;
20661   parser->integral_constant_expression_p
20662     = saved_integral_constant_expression_p;
20663   parser->non_integral_constant_expression_p
20664     = saved_non_integral_constant_expression_p;
20665
20666   return expr;
20667 }
20668
20669 /* If the current declaration has no declarator, return true.  */
20670
20671 static bool
20672 cp_parser_declares_only_class_p (cp_parser *parser)
20673 {
20674   /* If the next token is a `;' or a `,' then there is no
20675      declarator.  */
20676   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20677           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20678 }
20679
20680 /* Update the DECL_SPECS to reflect the storage class indicated by
20681    KEYWORD.  */
20682
20683 static void
20684 cp_parser_set_storage_class (cp_parser *parser,
20685                              cp_decl_specifier_seq *decl_specs,
20686                              enum rid keyword,
20687                              location_t location)
20688 {
20689   cp_storage_class storage_class;
20690
20691   if (parser->in_unbraced_linkage_specification_p)
20692     {
20693       error_at (location, "invalid use of %qD in linkage specification",
20694                 ridpointers[keyword]);
20695       return;
20696     }
20697   else if (decl_specs->storage_class != sc_none)
20698     {
20699       decl_specs->conflicting_specifiers_p = true;
20700       return;
20701     }
20702
20703   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20704       && decl_specs->specs[(int) ds_thread])
20705     {
20706       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20707       decl_specs->specs[(int) ds_thread] = 0;
20708     }
20709
20710   switch (keyword)
20711     {
20712     case RID_AUTO:
20713       storage_class = sc_auto;
20714       break;
20715     case RID_REGISTER:
20716       storage_class = sc_register;
20717       break;
20718     case RID_STATIC:
20719       storage_class = sc_static;
20720       break;
20721     case RID_EXTERN:
20722       storage_class = sc_extern;
20723       break;
20724     case RID_MUTABLE:
20725       storage_class = sc_mutable;
20726       break;
20727     default:
20728       gcc_unreachable ();
20729     }
20730   decl_specs->storage_class = storage_class;
20731
20732   /* A storage class specifier cannot be applied alongside a typedef 
20733      specifier. If there is a typedef specifier present then set 
20734      conflicting_specifiers_p which will trigger an error later
20735      on in grokdeclarator. */
20736   if (decl_specs->specs[(int)ds_typedef])
20737     decl_specs->conflicting_specifiers_p = true;
20738 }
20739
20740 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20741    is true, the type is a user-defined type; otherwise it is a
20742    built-in type specified by a keyword.  */
20743
20744 static void
20745 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20746                               tree type_spec,
20747                               location_t location,
20748                               bool user_defined_p)
20749 {
20750   decl_specs->any_specifiers_p = true;
20751
20752   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20753      (with, for example, in "typedef int wchar_t;") we remember that
20754      this is what happened.  In system headers, we ignore these
20755      declarations so that G++ can work with system headers that are not
20756      C++-safe.  */
20757   if (decl_specs->specs[(int) ds_typedef]
20758       && !user_defined_p
20759       && (type_spec == boolean_type_node
20760           || type_spec == char16_type_node
20761           || type_spec == char32_type_node
20762           || type_spec == wchar_type_node)
20763       && (decl_specs->type
20764           || decl_specs->specs[(int) ds_long]
20765           || decl_specs->specs[(int) ds_short]
20766           || decl_specs->specs[(int) ds_unsigned]
20767           || decl_specs->specs[(int) ds_signed]))
20768     {
20769       decl_specs->redefined_builtin_type = type_spec;
20770       if (!decl_specs->type)
20771         {
20772           decl_specs->type = type_spec;
20773           decl_specs->user_defined_type_p = false;
20774           decl_specs->type_location = location;
20775         }
20776     }
20777   else if (decl_specs->type)
20778     decl_specs->multiple_types_p = true;
20779   else
20780     {
20781       decl_specs->type = type_spec;
20782       decl_specs->user_defined_type_p = user_defined_p;
20783       decl_specs->redefined_builtin_type = NULL_TREE;
20784       decl_specs->type_location = location;
20785     }
20786 }
20787
20788 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20789    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20790
20791 static bool
20792 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20793 {
20794   return decl_specifiers->specs[(int) ds_friend] != 0;
20795 }
20796
20797 /* Issue an error message indicating that TOKEN_DESC was expected.
20798    If KEYWORD is true, it indicated this function is called by
20799    cp_parser_require_keword and the required token can only be
20800    a indicated keyword. */
20801
20802 static void
20803 cp_parser_required_error (cp_parser *parser,
20804                           required_token token_desc,
20805                           bool keyword)
20806 {
20807   switch (token_desc)
20808     {
20809       case RT_NEW:
20810         cp_parser_error (parser, "expected %<new%>");
20811         return;
20812       case RT_DELETE:
20813         cp_parser_error (parser, "expected %<delete%>");
20814         return;
20815       case RT_RETURN:
20816         cp_parser_error (parser, "expected %<return%>");
20817         return;
20818       case RT_WHILE:
20819         cp_parser_error (parser, "expected %<while%>");
20820         return;
20821       case RT_EXTERN:
20822         cp_parser_error (parser, "expected %<extern%>");
20823         return;
20824       case RT_STATIC_ASSERT:
20825         cp_parser_error (parser, "expected %<static_assert%>");
20826         return;
20827       case RT_DECLTYPE:
20828         cp_parser_error (parser, "expected %<decltype%>");
20829         return;
20830       case RT_OPERATOR:
20831         cp_parser_error (parser, "expected %<operator%>");
20832         return;
20833       case RT_CLASS:
20834         cp_parser_error (parser, "expected %<class%>");
20835         return;
20836       case RT_TEMPLATE:
20837         cp_parser_error (parser, "expected %<template%>");
20838         return;
20839       case RT_NAMESPACE:
20840         cp_parser_error (parser, "expected %<namespace%>");
20841         return;
20842       case RT_USING:
20843         cp_parser_error (parser, "expected %<using%>");
20844         return;
20845       case RT_ASM:
20846         cp_parser_error (parser, "expected %<asm%>");
20847         return;
20848       case RT_TRY:
20849         cp_parser_error (parser, "expected %<try%>");
20850         return;
20851       case RT_CATCH:
20852         cp_parser_error (parser, "expected %<catch%>");
20853         return;
20854       case RT_THROW:
20855         cp_parser_error (parser, "expected %<throw%>");
20856         return;
20857       case RT_LABEL:
20858         cp_parser_error (parser, "expected %<__label__%>");
20859         return;
20860       case RT_AT_TRY:
20861         cp_parser_error (parser, "expected %<@try%>");
20862         return;
20863       case RT_AT_SYNCHRONIZED:
20864         cp_parser_error (parser, "expected %<@synchronized%>");
20865         return;
20866       case RT_AT_THROW:
20867         cp_parser_error (parser, "expected %<@throw%>");
20868         return;
20869       default:
20870         break;
20871     }
20872   if (!keyword)
20873     {
20874       switch (token_desc)
20875         {
20876           case RT_SEMICOLON:
20877             cp_parser_error (parser, "expected %<;%>");
20878             return;
20879           case RT_OPEN_PAREN:
20880             cp_parser_error (parser, "expected %<(%>");
20881             return;
20882           case RT_CLOSE_BRACE:
20883             cp_parser_error (parser, "expected %<}%>");
20884             return;
20885           case RT_OPEN_BRACE:
20886             cp_parser_error (parser, "expected %<{%>");
20887             return;
20888           case RT_CLOSE_SQUARE:
20889             cp_parser_error (parser, "expected %<]%>");
20890             return;
20891           case RT_OPEN_SQUARE:
20892             cp_parser_error (parser, "expected %<[%>");
20893             return;
20894           case RT_COMMA:
20895             cp_parser_error (parser, "expected %<,%>");
20896             return;
20897           case RT_SCOPE:
20898             cp_parser_error (parser, "expected %<::%>");
20899             return;
20900           case RT_LESS:
20901             cp_parser_error (parser, "expected %<<%>");
20902             return;
20903           case RT_GREATER:
20904             cp_parser_error (parser, "expected %<>%>");
20905             return;
20906           case RT_EQ:
20907             cp_parser_error (parser, "expected %<=%>");
20908             return;
20909           case RT_ELLIPSIS:
20910             cp_parser_error (parser, "expected %<...%>");
20911             return;
20912           case RT_MULT:
20913             cp_parser_error (parser, "expected %<*%>");
20914             return;
20915           case RT_COMPL:
20916             cp_parser_error (parser, "expected %<~%>");
20917             return;
20918           case RT_COLON:
20919             cp_parser_error (parser, "expected %<:%>");
20920             return;
20921           case RT_COLON_SCOPE:
20922             cp_parser_error (parser, "expected %<:%> or %<::%>");
20923             return;
20924           case RT_CLOSE_PAREN:
20925             cp_parser_error (parser, "expected %<)%>");
20926             return;
20927           case RT_COMMA_CLOSE_PAREN:
20928             cp_parser_error (parser, "expected %<,%> or %<)%>");
20929             return;
20930           case RT_PRAGMA_EOL:
20931             cp_parser_error (parser, "expected end of line");
20932             return;
20933           case RT_NAME:
20934             cp_parser_error (parser, "expected identifier");
20935             return;
20936           case RT_SELECT:
20937             cp_parser_error (parser, "expected selection-statement");
20938             return;
20939           case RT_INTERATION:
20940             cp_parser_error (parser, "expected iteration-statement");
20941             return;
20942           case RT_JUMP:
20943             cp_parser_error (parser, "expected jump-statement");
20944             return;
20945           case RT_CLASS_KEY:
20946             cp_parser_error (parser, "expected class-key");
20947             return;
20948           case RT_CLASS_TYPENAME_TEMPLATE:
20949             cp_parser_error (parser,
20950                  "expected %<class%>, %<typename%>, or %<template%>");
20951             return;
20952           default:
20953             gcc_unreachable ();
20954         }
20955     }
20956   else
20957     gcc_unreachable ();
20958 }
20959
20960
20961
20962 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20963    issue an error message indicating that TOKEN_DESC was expected.
20964
20965    Returns the token consumed, if the token had the appropriate type.
20966    Otherwise, returns NULL.  */
20967
20968 static cp_token *
20969 cp_parser_require (cp_parser* parser,
20970                    enum cpp_ttype type,
20971                    required_token token_desc)
20972 {
20973   if (cp_lexer_next_token_is (parser->lexer, type))
20974     return cp_lexer_consume_token (parser->lexer);
20975   else
20976     {
20977       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20978       if (!cp_parser_simulate_error (parser))
20979         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20980       return NULL;
20981     }
20982 }
20983
20984 /* An error message is produced if the next token is not '>'.
20985    All further tokens are skipped until the desired token is
20986    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20987
20988 static void
20989 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20990 {
20991   /* Current level of '< ... >'.  */
20992   unsigned level = 0;
20993   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20994   unsigned nesting_depth = 0;
20995
20996   /* Are we ready, yet?  If not, issue error message.  */
20997   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20998     return;
20999
21000   /* Skip tokens until the desired token is found.  */
21001   while (true)
21002     {
21003       /* Peek at the next token.  */
21004       switch (cp_lexer_peek_token (parser->lexer)->type)
21005         {
21006         case CPP_LESS:
21007           if (!nesting_depth)
21008             ++level;
21009           break;
21010
21011         case CPP_RSHIFT:
21012           if (cxx_dialect == cxx98)
21013             /* C++0x views the `>>' operator as two `>' tokens, but
21014                C++98 does not. */
21015             break;
21016           else if (!nesting_depth && level-- == 0)
21017             {
21018               /* We've hit a `>>' where the first `>' closes the
21019                  template argument list, and the second `>' is
21020                  spurious.  Just consume the `>>' and stop; we've
21021                  already produced at least one error.  */
21022               cp_lexer_consume_token (parser->lexer);
21023               return;
21024             }
21025           /* Fall through for C++0x, so we handle the second `>' in
21026              the `>>'.  */
21027
21028         case CPP_GREATER:
21029           if (!nesting_depth && level-- == 0)
21030             {
21031               /* We've reached the token we want, consume it and stop.  */
21032               cp_lexer_consume_token (parser->lexer);
21033               return;
21034             }
21035           break;
21036
21037         case CPP_OPEN_PAREN:
21038         case CPP_OPEN_SQUARE:
21039           ++nesting_depth;
21040           break;
21041
21042         case CPP_CLOSE_PAREN:
21043         case CPP_CLOSE_SQUARE:
21044           if (nesting_depth-- == 0)
21045             return;
21046           break;
21047
21048         case CPP_EOF:
21049         case CPP_PRAGMA_EOL:
21050         case CPP_SEMICOLON:
21051         case CPP_OPEN_BRACE:
21052         case CPP_CLOSE_BRACE:
21053           /* The '>' was probably forgotten, don't look further.  */
21054           return;
21055
21056         default:
21057           break;
21058         }
21059
21060       /* Consume this token.  */
21061       cp_lexer_consume_token (parser->lexer);
21062     }
21063 }
21064
21065 /* If the next token is the indicated keyword, consume it.  Otherwise,
21066    issue an error message indicating that TOKEN_DESC was expected.
21067
21068    Returns the token consumed, if the token had the appropriate type.
21069    Otherwise, returns NULL.  */
21070
21071 static cp_token *
21072 cp_parser_require_keyword (cp_parser* parser,
21073                            enum rid keyword,
21074                            required_token token_desc)
21075 {
21076   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21077
21078   if (token && token->keyword != keyword)
21079     {
21080       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21081       return NULL;
21082     }
21083
21084   return token;
21085 }
21086
21087 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21088    function-definition.  */
21089
21090 static bool
21091 cp_parser_token_starts_function_definition_p (cp_token* token)
21092 {
21093   return (/* An ordinary function-body begins with an `{'.  */
21094           token->type == CPP_OPEN_BRACE
21095           /* A ctor-initializer begins with a `:'.  */
21096           || token->type == CPP_COLON
21097           /* A function-try-block begins with `try'.  */
21098           || token->keyword == RID_TRY
21099           /* The named return value extension begins with `return'.  */
21100           || token->keyword == RID_RETURN);
21101 }
21102
21103 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21104    definition.  */
21105
21106 static bool
21107 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21108 {
21109   cp_token *token;
21110
21111   token = cp_lexer_peek_token (parser->lexer);
21112   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21113 }
21114
21115 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21116    C++0x) ending a template-argument.  */
21117
21118 static bool
21119 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21120 {
21121   cp_token *token;
21122
21123   token = cp_lexer_peek_token (parser->lexer);
21124   return (token->type == CPP_COMMA 
21125           || token->type == CPP_GREATER
21126           || token->type == CPP_ELLIPSIS
21127           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21128 }
21129
21130 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21131    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21132
21133 static bool
21134 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21135                                                      size_t n)
21136 {
21137   cp_token *token;
21138
21139   token = cp_lexer_peek_nth_token (parser->lexer, n);
21140   if (token->type == CPP_LESS)
21141     return true;
21142   /* Check for the sequence `<::' in the original code. It would be lexed as
21143      `[:', where `[' is a digraph, and there is no whitespace before
21144      `:'.  */
21145   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21146     {
21147       cp_token *token2;
21148       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21149       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21150         return true;
21151     }
21152   return false;
21153 }
21154
21155 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21156    or none_type otherwise.  */
21157
21158 static enum tag_types
21159 cp_parser_token_is_class_key (cp_token* token)
21160 {
21161   switch (token->keyword)
21162     {
21163     case RID_CLASS:
21164       return class_type;
21165     case RID_STRUCT:
21166       return record_type;
21167     case RID_UNION:
21168       return union_type;
21169
21170     default:
21171       return none_type;
21172     }
21173 }
21174
21175 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21176
21177 static void
21178 cp_parser_check_class_key (enum tag_types class_key, tree type)
21179 {
21180   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21181     permerror (input_location, "%qs tag used in naming %q#T",
21182             class_key == union_type ? "union"
21183              : class_key == record_type ? "struct" : "class",
21184              type);
21185 }
21186
21187 /* Issue an error message if DECL is redeclared with different
21188    access than its original declaration [class.access.spec/3].
21189    This applies to nested classes and nested class templates.
21190    [class.mem/1].  */
21191
21192 static void
21193 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21194 {
21195   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21196     return;
21197
21198   if ((TREE_PRIVATE (decl)
21199        != (current_access_specifier == access_private_node))
21200       || (TREE_PROTECTED (decl)
21201           != (current_access_specifier == access_protected_node)))
21202     error_at (location, "%qD redeclared with different access", decl);
21203 }
21204
21205 /* Look for the `template' keyword, as a syntactic disambiguator.
21206    Return TRUE iff it is present, in which case it will be
21207    consumed.  */
21208
21209 static bool
21210 cp_parser_optional_template_keyword (cp_parser *parser)
21211 {
21212   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21213     {
21214       /* The `template' keyword can only be used within templates;
21215          outside templates the parser can always figure out what is a
21216          template and what is not.  */
21217       if (!processing_template_decl)
21218         {
21219           cp_token *token = cp_lexer_peek_token (parser->lexer);
21220           error_at (token->location,
21221                     "%<template%> (as a disambiguator) is only allowed "
21222                     "within templates");
21223           /* If this part of the token stream is rescanned, the same
21224              error message would be generated.  So, we purge the token
21225              from the stream.  */
21226           cp_lexer_purge_token (parser->lexer);
21227           return false;
21228         }
21229       else
21230         {
21231           /* Consume the `template' keyword.  */
21232           cp_lexer_consume_token (parser->lexer);
21233           return true;
21234         }
21235     }
21236
21237   return false;
21238 }
21239
21240 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21241    set PARSER->SCOPE, and perform other related actions.  */
21242
21243 static void
21244 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21245 {
21246   int i;
21247   struct tree_check *check_value;
21248   deferred_access_check *chk;
21249   VEC (deferred_access_check,gc) *checks;
21250
21251   /* Get the stored value.  */
21252   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21253   /* Perform any access checks that were deferred.  */
21254   checks = check_value->checks;
21255   if (checks)
21256     {
21257       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21258         perform_or_defer_access_check (chk->binfo,
21259                                        chk->decl,
21260                                        chk->diag_decl);
21261     }
21262   /* Set the scope from the stored value.  */
21263   parser->scope = check_value->value;
21264   parser->qualifying_scope = check_value->qualifying_scope;
21265   parser->object_scope = NULL_TREE;
21266 }
21267
21268 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21269    encounter the end of a block before what we were looking for.  */
21270
21271 static bool
21272 cp_parser_cache_group (cp_parser *parser,
21273                        enum cpp_ttype end,
21274                        unsigned depth)
21275 {
21276   while (true)
21277     {
21278       cp_token *token = cp_lexer_peek_token (parser->lexer);
21279
21280       /* Abort a parenthesized expression if we encounter a semicolon.  */
21281       if ((end == CPP_CLOSE_PAREN || depth == 0)
21282           && token->type == CPP_SEMICOLON)
21283         return true;
21284       /* If we've reached the end of the file, stop.  */
21285       if (token->type == CPP_EOF
21286           || (end != CPP_PRAGMA_EOL
21287               && token->type == CPP_PRAGMA_EOL))
21288         return true;
21289       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21290         /* We've hit the end of an enclosing block, so there's been some
21291            kind of syntax error.  */
21292         return true;
21293
21294       /* Consume the token.  */
21295       cp_lexer_consume_token (parser->lexer);
21296       /* See if it starts a new group.  */
21297       if (token->type == CPP_OPEN_BRACE)
21298         {
21299           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21300           /* In theory this should probably check end == '}', but
21301              cp_parser_save_member_function_body needs it to exit
21302              after either '}' or ')' when called with ')'.  */
21303           if (depth == 0)
21304             return false;
21305         }
21306       else if (token->type == CPP_OPEN_PAREN)
21307         {
21308           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21309           if (depth == 0 && end == CPP_CLOSE_PAREN)
21310             return false;
21311         }
21312       else if (token->type == CPP_PRAGMA)
21313         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21314       else if (token->type == end)
21315         return false;
21316     }
21317 }
21318
21319 /* Begin parsing tentatively.  We always save tokens while parsing
21320    tentatively so that if the tentative parsing fails we can restore the
21321    tokens.  */
21322
21323 static void
21324 cp_parser_parse_tentatively (cp_parser* parser)
21325 {
21326   /* Enter a new parsing context.  */
21327   parser->context = cp_parser_context_new (parser->context);
21328   /* Begin saving tokens.  */
21329   cp_lexer_save_tokens (parser->lexer);
21330   /* In order to avoid repetitive access control error messages,
21331      access checks are queued up until we are no longer parsing
21332      tentatively.  */
21333   push_deferring_access_checks (dk_deferred);
21334 }
21335
21336 /* Commit to the currently active tentative parse.  */
21337
21338 static void
21339 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21340 {
21341   cp_parser_context *context;
21342   cp_lexer *lexer;
21343
21344   /* Mark all of the levels as committed.  */
21345   lexer = parser->lexer;
21346   for (context = parser->context; context->next; context = context->next)
21347     {
21348       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21349         break;
21350       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21351       while (!cp_lexer_saving_tokens (lexer))
21352         lexer = lexer->next;
21353       cp_lexer_commit_tokens (lexer);
21354     }
21355 }
21356
21357 /* Abort the currently active tentative parse.  All consumed tokens
21358    will be rolled back, and no diagnostics will be issued.  */
21359
21360 static void
21361 cp_parser_abort_tentative_parse (cp_parser* parser)
21362 {
21363   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21364               || errorcount > 0);
21365   cp_parser_simulate_error (parser);
21366   /* Now, pretend that we want to see if the construct was
21367      successfully parsed.  */
21368   cp_parser_parse_definitely (parser);
21369 }
21370
21371 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21372    token stream.  Otherwise, commit to the tokens we have consumed.
21373    Returns true if no error occurred; false otherwise.  */
21374
21375 static bool
21376 cp_parser_parse_definitely (cp_parser* parser)
21377 {
21378   bool error_occurred;
21379   cp_parser_context *context;
21380
21381   /* Remember whether or not an error occurred, since we are about to
21382      destroy that information.  */
21383   error_occurred = cp_parser_error_occurred (parser);
21384   /* Remove the topmost context from the stack.  */
21385   context = parser->context;
21386   parser->context = context->next;
21387   /* If no parse errors occurred, commit to the tentative parse.  */
21388   if (!error_occurred)
21389     {
21390       /* Commit to the tokens read tentatively, unless that was
21391          already done.  */
21392       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21393         cp_lexer_commit_tokens (parser->lexer);
21394
21395       pop_to_parent_deferring_access_checks ();
21396     }
21397   /* Otherwise, if errors occurred, roll back our state so that things
21398      are just as they were before we began the tentative parse.  */
21399   else
21400     {
21401       cp_lexer_rollback_tokens (parser->lexer);
21402       pop_deferring_access_checks ();
21403     }
21404   /* Add the context to the front of the free list.  */
21405   context->next = cp_parser_context_free_list;
21406   cp_parser_context_free_list = context;
21407
21408   return !error_occurred;
21409 }
21410
21411 /* Returns true if we are parsing tentatively and are not committed to
21412    this tentative parse.  */
21413
21414 static bool
21415 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21416 {
21417   return (cp_parser_parsing_tentatively (parser)
21418           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21419 }
21420
21421 /* Returns nonzero iff an error has occurred during the most recent
21422    tentative parse.  */
21423
21424 static bool
21425 cp_parser_error_occurred (cp_parser* parser)
21426 {
21427   return (cp_parser_parsing_tentatively (parser)
21428           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21429 }
21430
21431 /* Returns nonzero if GNU extensions are allowed.  */
21432
21433 static bool
21434 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21435 {
21436   return parser->allow_gnu_extensions_p;
21437 }
21438 \f
21439 /* Objective-C++ Productions */
21440
21441
21442 /* Parse an Objective-C expression, which feeds into a primary-expression
21443    above.
21444
21445    objc-expression:
21446      objc-message-expression
21447      objc-string-literal
21448      objc-encode-expression
21449      objc-protocol-expression
21450      objc-selector-expression
21451
21452   Returns a tree representation of the expression.  */
21453
21454 static tree
21455 cp_parser_objc_expression (cp_parser* parser)
21456 {
21457   /* Try to figure out what kind of declaration is present.  */
21458   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21459
21460   switch (kwd->type)
21461     {
21462     case CPP_OPEN_SQUARE:
21463       return cp_parser_objc_message_expression (parser);
21464
21465     case CPP_OBJC_STRING:
21466       kwd = cp_lexer_consume_token (parser->lexer);
21467       return objc_build_string_object (kwd->u.value);
21468
21469     case CPP_KEYWORD:
21470       switch (kwd->keyword)
21471         {
21472         case RID_AT_ENCODE:
21473           return cp_parser_objc_encode_expression (parser);
21474
21475         case RID_AT_PROTOCOL:
21476           return cp_parser_objc_protocol_expression (parser);
21477
21478         case RID_AT_SELECTOR:
21479           return cp_parser_objc_selector_expression (parser);
21480
21481         default:
21482           break;
21483         }
21484     default:
21485       error_at (kwd->location,
21486                 "misplaced %<@%D%> Objective-C++ construct",
21487                 kwd->u.value);
21488       cp_parser_skip_to_end_of_block_or_statement (parser);
21489     }
21490
21491   return error_mark_node;
21492 }
21493
21494 /* Parse an Objective-C message expression.
21495
21496    objc-message-expression:
21497      [ objc-message-receiver objc-message-args ]
21498
21499    Returns a representation of an Objective-C message.  */
21500
21501 static tree
21502 cp_parser_objc_message_expression (cp_parser* parser)
21503 {
21504   tree receiver, messageargs;
21505
21506   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21507   receiver = cp_parser_objc_message_receiver (parser);
21508   messageargs = cp_parser_objc_message_args (parser);
21509   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21510
21511   return objc_build_message_expr (receiver, messageargs);
21512 }
21513
21514 /* Parse an objc-message-receiver.
21515
21516    objc-message-receiver:
21517      expression
21518      simple-type-specifier
21519
21520   Returns a representation of the type or expression.  */
21521
21522 static tree
21523 cp_parser_objc_message_receiver (cp_parser* parser)
21524 {
21525   tree rcv;
21526
21527   /* An Objective-C message receiver may be either (1) a type
21528      or (2) an expression.  */
21529   cp_parser_parse_tentatively (parser);
21530   rcv = cp_parser_expression (parser, false, NULL);
21531
21532   if (cp_parser_parse_definitely (parser))
21533     return rcv;
21534
21535   rcv = cp_parser_simple_type_specifier (parser,
21536                                          /*decl_specs=*/NULL,
21537                                          CP_PARSER_FLAGS_NONE);
21538
21539   return objc_get_class_reference (rcv);
21540 }
21541
21542 /* Parse the arguments and selectors comprising an Objective-C message.
21543
21544    objc-message-args:
21545      objc-selector
21546      objc-selector-args
21547      objc-selector-args , objc-comma-args
21548
21549    objc-selector-args:
21550      objc-selector [opt] : assignment-expression
21551      objc-selector-args objc-selector [opt] : assignment-expression
21552
21553    objc-comma-args:
21554      assignment-expression
21555      objc-comma-args , assignment-expression
21556
21557    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21558    selector arguments and TREE_VALUE containing a list of comma
21559    arguments.  */
21560
21561 static tree
21562 cp_parser_objc_message_args (cp_parser* parser)
21563 {
21564   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21565   bool maybe_unary_selector_p = true;
21566   cp_token *token = cp_lexer_peek_token (parser->lexer);
21567
21568   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21569     {
21570       tree selector = NULL_TREE, arg;
21571
21572       if (token->type != CPP_COLON)
21573         selector = cp_parser_objc_selector (parser);
21574
21575       /* Detect if we have a unary selector.  */
21576       if (maybe_unary_selector_p
21577           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21578         return build_tree_list (selector, NULL_TREE);
21579
21580       maybe_unary_selector_p = false;
21581       cp_parser_require (parser, CPP_COLON, RT_COLON);
21582       arg = cp_parser_assignment_expression (parser, false, NULL);
21583
21584       sel_args
21585         = chainon (sel_args,
21586                    build_tree_list (selector, arg));
21587
21588       token = cp_lexer_peek_token (parser->lexer);
21589     }
21590
21591   /* Handle non-selector arguments, if any. */
21592   while (token->type == CPP_COMMA)
21593     {
21594       tree arg;
21595
21596       cp_lexer_consume_token (parser->lexer);
21597       arg = cp_parser_assignment_expression (parser, false, NULL);
21598
21599       addl_args
21600         = chainon (addl_args,
21601                    build_tree_list (NULL_TREE, arg));
21602
21603       token = cp_lexer_peek_token (parser->lexer);
21604     }
21605
21606   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21607     {
21608       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21609       return build_tree_list (error_mark_node, error_mark_node);
21610     }
21611
21612   return build_tree_list (sel_args, addl_args);
21613 }
21614
21615 /* Parse an Objective-C encode expression.
21616
21617    objc-encode-expression:
21618      @encode objc-typename
21619
21620    Returns an encoded representation of the type argument.  */
21621
21622 static tree
21623 cp_parser_objc_encode_expression (cp_parser* parser)
21624 {
21625   tree type;
21626   cp_token *token;
21627
21628   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21629   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21630   token = cp_lexer_peek_token (parser->lexer);
21631   type = complete_type (cp_parser_type_id (parser));
21632   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21633
21634   if (!type)
21635     {
21636       error_at (token->location, 
21637                 "%<@encode%> must specify a type as an argument");
21638       return error_mark_node;
21639     }
21640
21641   /* This happens if we find @encode(T) (where T is a template
21642      typename or something dependent on a template typename) when
21643      parsing a template.  In that case, we can't compile it
21644      immediately, but we rather create an AT_ENCODE_EXPR which will
21645      need to be instantiated when the template is used.
21646   */
21647   if (dependent_type_p (type))
21648     {
21649       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21650       TREE_READONLY (value) = 1;
21651       return value;
21652     }
21653
21654   return objc_build_encode_expr (type);
21655 }
21656
21657 /* Parse an Objective-C @defs expression.  */
21658
21659 static tree
21660 cp_parser_objc_defs_expression (cp_parser *parser)
21661 {
21662   tree name;
21663
21664   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21665   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21666   name = cp_parser_identifier (parser);
21667   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21668
21669   return objc_get_class_ivars (name);
21670 }
21671
21672 /* Parse an Objective-C protocol expression.
21673
21674   objc-protocol-expression:
21675     @protocol ( identifier )
21676
21677   Returns a representation of the protocol expression.  */
21678
21679 static tree
21680 cp_parser_objc_protocol_expression (cp_parser* parser)
21681 {
21682   tree proto;
21683
21684   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21685   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21686   proto = cp_parser_identifier (parser);
21687   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21688
21689   return objc_build_protocol_expr (proto);
21690 }
21691
21692 /* Parse an Objective-C selector expression.
21693
21694    objc-selector-expression:
21695      @selector ( objc-method-signature )
21696
21697    objc-method-signature:
21698      objc-selector
21699      objc-selector-seq
21700
21701    objc-selector-seq:
21702      objc-selector :
21703      objc-selector-seq objc-selector :
21704
21705   Returns a representation of the method selector.  */
21706
21707 static tree
21708 cp_parser_objc_selector_expression (cp_parser* parser)
21709 {
21710   tree sel_seq = NULL_TREE;
21711   bool maybe_unary_selector_p = true;
21712   cp_token *token;
21713   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21714
21715   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21716   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21717   token = cp_lexer_peek_token (parser->lexer);
21718
21719   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21720          || token->type == CPP_SCOPE)
21721     {
21722       tree selector = NULL_TREE;
21723
21724       if (token->type != CPP_COLON
21725           || token->type == CPP_SCOPE)
21726         selector = cp_parser_objc_selector (parser);
21727
21728       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21729           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21730         {
21731           /* Detect if we have a unary selector.  */
21732           if (maybe_unary_selector_p)
21733             {
21734               sel_seq = selector;
21735               goto finish_selector;
21736             }
21737           else
21738             {
21739               cp_parser_error (parser, "expected %<:%>");
21740             }
21741         }
21742       maybe_unary_selector_p = false;
21743       token = cp_lexer_consume_token (parser->lexer);
21744
21745       if (token->type == CPP_SCOPE)
21746         {
21747           sel_seq
21748             = chainon (sel_seq,
21749                        build_tree_list (selector, NULL_TREE));
21750           sel_seq
21751             = chainon (sel_seq,
21752                        build_tree_list (NULL_TREE, NULL_TREE));
21753         }
21754       else
21755         sel_seq
21756           = chainon (sel_seq,
21757                      build_tree_list (selector, NULL_TREE));
21758
21759       token = cp_lexer_peek_token (parser->lexer);
21760     }
21761
21762  finish_selector:
21763   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21764
21765   return objc_build_selector_expr (loc, sel_seq);
21766 }
21767
21768 /* Parse a list of identifiers.
21769
21770    objc-identifier-list:
21771      identifier
21772      objc-identifier-list , identifier
21773
21774    Returns a TREE_LIST of identifier nodes.  */
21775
21776 static tree
21777 cp_parser_objc_identifier_list (cp_parser* parser)
21778 {
21779   tree identifier;
21780   tree list;
21781   cp_token *sep;
21782
21783   identifier = cp_parser_identifier (parser);
21784   if (identifier == error_mark_node)
21785     return error_mark_node;      
21786
21787   list = build_tree_list (NULL_TREE, identifier);
21788   sep = cp_lexer_peek_token (parser->lexer);
21789
21790   while (sep->type == CPP_COMMA)
21791     {
21792       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21793       identifier = cp_parser_identifier (parser);
21794       if (identifier == error_mark_node)
21795         return list;
21796
21797       list = chainon (list, build_tree_list (NULL_TREE,
21798                                              identifier));
21799       sep = cp_lexer_peek_token (parser->lexer);
21800     }
21801   
21802   return list;
21803 }
21804
21805 /* Parse an Objective-C alias declaration.
21806
21807    objc-alias-declaration:
21808      @compatibility_alias identifier identifier ;
21809
21810    This function registers the alias mapping with the Objective-C front end.
21811    It returns nothing.  */
21812
21813 static void
21814 cp_parser_objc_alias_declaration (cp_parser* parser)
21815 {
21816   tree alias, orig;
21817
21818   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21819   alias = cp_parser_identifier (parser);
21820   orig = cp_parser_identifier (parser);
21821   objc_declare_alias (alias, orig);
21822   cp_parser_consume_semicolon_at_end_of_statement (parser);
21823 }
21824
21825 /* Parse an Objective-C class forward-declaration.
21826
21827    objc-class-declaration:
21828      @class objc-identifier-list ;
21829
21830    The function registers the forward declarations with the Objective-C
21831    front end.  It returns nothing.  */
21832
21833 static void
21834 cp_parser_objc_class_declaration (cp_parser* parser)
21835 {
21836   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21837   while (true)
21838     {
21839       tree id;
21840       
21841       id = cp_parser_identifier (parser);
21842       if (id == error_mark_node)
21843         break;
21844       
21845       objc_declare_class (id);
21846
21847       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21848         cp_lexer_consume_token (parser->lexer);
21849       else
21850         break;
21851     }
21852   cp_parser_consume_semicolon_at_end_of_statement (parser);
21853 }
21854
21855 /* Parse a list of Objective-C protocol references.
21856
21857    objc-protocol-refs-opt:
21858      objc-protocol-refs [opt]
21859
21860    objc-protocol-refs:
21861      < objc-identifier-list >
21862
21863    Returns a TREE_LIST of identifiers, if any.  */
21864
21865 static tree
21866 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21867 {
21868   tree protorefs = NULL_TREE;
21869
21870   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21871     {
21872       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21873       protorefs = cp_parser_objc_identifier_list (parser);
21874       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21875     }
21876
21877   return protorefs;
21878 }
21879
21880 /* Parse a Objective-C visibility specification.  */
21881
21882 static void
21883 cp_parser_objc_visibility_spec (cp_parser* parser)
21884 {
21885   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21886
21887   switch (vis->keyword)
21888     {
21889     case RID_AT_PRIVATE:
21890       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21891       break;
21892     case RID_AT_PROTECTED:
21893       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21894       break;
21895     case RID_AT_PUBLIC:
21896       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21897       break;
21898     case RID_AT_PACKAGE:
21899       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21900       break;
21901     default:
21902       return;
21903     }
21904
21905   /* Eat '@private'/'@protected'/'@public'.  */
21906   cp_lexer_consume_token (parser->lexer);
21907 }
21908
21909 /* Parse an Objective-C method type.  Return 'true' if it is a class
21910    (+) method, and 'false' if it is an instance (-) method.  */
21911
21912 static inline bool
21913 cp_parser_objc_method_type (cp_parser* parser)
21914 {
21915   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21916     return true;
21917   else
21918     return false;
21919 }
21920
21921 /* Parse an Objective-C protocol qualifier.  */
21922
21923 static tree
21924 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21925 {
21926   tree quals = NULL_TREE, node;
21927   cp_token *token = cp_lexer_peek_token (parser->lexer);
21928
21929   node = token->u.value;
21930
21931   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21932          && (node == ridpointers [(int) RID_IN]
21933              || node == ridpointers [(int) RID_OUT]
21934              || node == ridpointers [(int) RID_INOUT]
21935              || node == ridpointers [(int) RID_BYCOPY]
21936              || node == ridpointers [(int) RID_BYREF]
21937              || node == ridpointers [(int) RID_ONEWAY]))
21938     {
21939       quals = tree_cons (NULL_TREE, node, quals);
21940       cp_lexer_consume_token (parser->lexer);
21941       token = cp_lexer_peek_token (parser->lexer);
21942       node = token->u.value;
21943     }
21944
21945   return quals;
21946 }
21947
21948 /* Parse an Objective-C typename.  */
21949
21950 static tree
21951 cp_parser_objc_typename (cp_parser* parser)
21952 {
21953   tree type_name = NULL_TREE;
21954
21955   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21956     {
21957       tree proto_quals, cp_type = NULL_TREE;
21958
21959       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21960       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21961
21962       /* An ObjC type name may consist of just protocol qualifiers, in which
21963          case the type shall default to 'id'.  */
21964       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21965         {
21966           cp_type = cp_parser_type_id (parser);
21967           
21968           /* If the type could not be parsed, an error has already
21969              been produced.  For error recovery, behave as if it had
21970              not been specified, which will use the default type
21971              'id'.  */
21972           if (cp_type == error_mark_node)
21973             {
21974               cp_type = NULL_TREE;
21975               /* We need to skip to the closing parenthesis as
21976                  cp_parser_type_id() does not seem to do it for
21977                  us.  */
21978               cp_parser_skip_to_closing_parenthesis (parser,
21979                                                      /*recovering=*/true,
21980                                                      /*or_comma=*/false,
21981                                                      /*consume_paren=*/false);
21982             }
21983         }
21984
21985       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21986       type_name = build_tree_list (proto_quals, cp_type);
21987     }
21988
21989   return type_name;
21990 }
21991
21992 /* Check to see if TYPE refers to an Objective-C selector name.  */
21993
21994 static bool
21995 cp_parser_objc_selector_p (enum cpp_ttype type)
21996 {
21997   return (type == CPP_NAME || type == CPP_KEYWORD
21998           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21999           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22000           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22001           || type == CPP_XOR || type == CPP_XOR_EQ);
22002 }
22003
22004 /* Parse an Objective-C selector.  */
22005
22006 static tree
22007 cp_parser_objc_selector (cp_parser* parser)
22008 {
22009   cp_token *token = cp_lexer_consume_token (parser->lexer);
22010
22011   if (!cp_parser_objc_selector_p (token->type))
22012     {
22013       error_at (token->location, "invalid Objective-C++ selector name");
22014       return error_mark_node;
22015     }
22016
22017   /* C++ operator names are allowed to appear in ObjC selectors.  */
22018   switch (token->type)
22019     {
22020     case CPP_AND_AND: return get_identifier ("and");
22021     case CPP_AND_EQ: return get_identifier ("and_eq");
22022     case CPP_AND: return get_identifier ("bitand");
22023     case CPP_OR: return get_identifier ("bitor");
22024     case CPP_COMPL: return get_identifier ("compl");
22025     case CPP_NOT: return get_identifier ("not");
22026     case CPP_NOT_EQ: return get_identifier ("not_eq");
22027     case CPP_OR_OR: return get_identifier ("or");
22028     case CPP_OR_EQ: return get_identifier ("or_eq");
22029     case CPP_XOR: return get_identifier ("xor");
22030     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22031     default: return token->u.value;
22032     }
22033 }
22034
22035 /* Parse an Objective-C params list.  */
22036
22037 static tree
22038 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22039 {
22040   tree params = NULL_TREE;
22041   bool maybe_unary_selector_p = true;
22042   cp_token *token = cp_lexer_peek_token (parser->lexer);
22043
22044   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22045     {
22046       tree selector = NULL_TREE, type_name, identifier;
22047       tree parm_attr = NULL_TREE;
22048
22049       if (token->keyword == RID_ATTRIBUTE)
22050         break;
22051
22052       if (token->type != CPP_COLON)
22053         selector = cp_parser_objc_selector (parser);
22054
22055       /* Detect if we have a unary selector.  */
22056       if (maybe_unary_selector_p
22057           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22058         {
22059           params = selector; /* Might be followed by attributes.  */
22060           break;
22061         }
22062
22063       maybe_unary_selector_p = false;
22064       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22065         {
22066           /* Something went quite wrong.  There should be a colon
22067              here, but there is not.  Stop parsing parameters.  */
22068           break;
22069         }
22070       type_name = cp_parser_objc_typename (parser);
22071       /* New ObjC allows attributes on parameters too.  */
22072       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22073         parm_attr = cp_parser_attributes_opt (parser);
22074       identifier = cp_parser_identifier (parser);
22075
22076       params
22077         = chainon (params,
22078                    objc_build_keyword_decl (selector,
22079                                             type_name,
22080                                             identifier,
22081                                             parm_attr));
22082
22083       token = cp_lexer_peek_token (parser->lexer);
22084     }
22085
22086   if (params == NULL_TREE)
22087     {
22088       cp_parser_error (parser, "objective-c++ method declaration is expected");
22089       return error_mark_node;
22090     }
22091
22092   /* We allow tail attributes for the method.  */
22093   if (token->keyword == RID_ATTRIBUTE)
22094     {
22095       *attributes = cp_parser_attributes_opt (parser);
22096       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22097           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22098         return params;
22099       cp_parser_error (parser, 
22100                        "method attributes must be specified at the end");
22101       return error_mark_node;
22102     }
22103
22104   if (params == NULL_TREE)
22105     {
22106       cp_parser_error (parser, "objective-c++ method declaration is expected");
22107       return error_mark_node;
22108     }
22109   return params;
22110 }
22111
22112 /* Parse the non-keyword Objective-C params.  */
22113
22114 static tree
22115 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22116                                        tree* attributes)
22117 {
22118   tree params = make_node (TREE_LIST);
22119   cp_token *token = cp_lexer_peek_token (parser->lexer);
22120   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22121
22122   while (token->type == CPP_COMMA)
22123     {
22124       cp_parameter_declarator *parmdecl;
22125       tree parm;
22126
22127       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22128       token = cp_lexer_peek_token (parser->lexer);
22129
22130       if (token->type == CPP_ELLIPSIS)
22131         {
22132           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22133           *ellipsisp = true;
22134           token = cp_lexer_peek_token (parser->lexer);
22135           break;
22136         }
22137
22138       /* TODO: parse attributes for tail parameters.  */
22139       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22140       parm = grokdeclarator (parmdecl->declarator,
22141                              &parmdecl->decl_specifiers,
22142                              PARM, /*initialized=*/0,
22143                              /*attrlist=*/NULL);
22144
22145       chainon (params, build_tree_list (NULL_TREE, parm));
22146       token = cp_lexer_peek_token (parser->lexer);
22147     }
22148
22149   /* We allow tail attributes for the method.  */
22150   if (token->keyword == RID_ATTRIBUTE)
22151     {
22152       if (*attributes == NULL_TREE)
22153         {
22154           *attributes = cp_parser_attributes_opt (parser);
22155           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22156               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22157             return params;
22158         }
22159       else        
22160         /* We have an error, but parse the attributes, so that we can 
22161            carry on.  */
22162         *attributes = cp_parser_attributes_opt (parser);
22163
22164       cp_parser_error (parser, 
22165                        "method attributes must be specified at the end");
22166       return error_mark_node;
22167     }
22168
22169   return params;
22170 }
22171
22172 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22173
22174 static void
22175 cp_parser_objc_interstitial_code (cp_parser* parser)
22176 {
22177   cp_token *token = cp_lexer_peek_token (parser->lexer);
22178
22179   /* If the next token is `extern' and the following token is a string
22180      literal, then we have a linkage specification.  */
22181   if (token->keyword == RID_EXTERN
22182       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22183     cp_parser_linkage_specification (parser);
22184   /* Handle #pragma, if any.  */
22185   else if (token->type == CPP_PRAGMA)
22186     cp_parser_pragma (parser, pragma_external);
22187   /* Allow stray semicolons.  */
22188   else if (token->type == CPP_SEMICOLON)
22189     cp_lexer_consume_token (parser->lexer);
22190   /* Mark methods as optional or required, when building protocols.  */
22191   else if (token->keyword == RID_AT_OPTIONAL)
22192     {
22193       cp_lexer_consume_token (parser->lexer);
22194       objc_set_method_opt (true);
22195     }
22196   else if (token->keyword == RID_AT_REQUIRED)
22197     {
22198       cp_lexer_consume_token (parser->lexer);
22199       objc_set_method_opt (false);
22200     }
22201   else if (token->keyword == RID_NAMESPACE)
22202     cp_parser_namespace_definition (parser);
22203   /* Other stray characters must generate errors.  */
22204   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22205     {
22206       cp_lexer_consume_token (parser->lexer);
22207       error ("stray %qs between Objective-C++ methods",
22208              token->type == CPP_OPEN_BRACE ? "{" : "}");
22209     }
22210   /* Finally, try to parse a block-declaration, or a function-definition.  */
22211   else
22212     cp_parser_block_declaration (parser, /*statement_p=*/false);
22213 }
22214
22215 /* Parse a method signature.  */
22216
22217 static tree
22218 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22219 {
22220   tree rettype, kwdparms, optparms;
22221   bool ellipsis = false;
22222   bool is_class_method;
22223
22224   is_class_method = cp_parser_objc_method_type (parser);
22225   rettype = cp_parser_objc_typename (parser);
22226   *attributes = NULL_TREE;
22227   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22228   if (kwdparms == error_mark_node)
22229     return error_mark_node;
22230   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22231   if (optparms == error_mark_node)
22232     return error_mark_node;
22233
22234   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22235 }
22236
22237 static bool
22238 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22239 {
22240   tree tattr;  
22241   cp_lexer_save_tokens (parser->lexer);
22242   tattr = cp_parser_attributes_opt (parser);
22243   gcc_assert (tattr) ;
22244   
22245   /* If the attributes are followed by a method introducer, this is not allowed.
22246      Dump the attributes and flag the situation.  */
22247   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22248       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22249     return true;
22250
22251   /* Otherwise, the attributes introduce some interstitial code, possibly so
22252      rewind to allow that check.  */
22253   cp_lexer_rollback_tokens (parser->lexer);
22254   return false;  
22255 }
22256
22257 /* Parse an Objective-C method prototype list.  */
22258
22259 static void
22260 cp_parser_objc_method_prototype_list (cp_parser* parser)
22261 {
22262   cp_token *token = cp_lexer_peek_token (parser->lexer);
22263
22264   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22265     {
22266       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22267         {
22268           tree attributes, sig;
22269           bool is_class_method;
22270           if (token->type == CPP_PLUS)
22271             is_class_method = true;
22272           else
22273             is_class_method = false;
22274           sig = cp_parser_objc_method_signature (parser, &attributes);
22275           if (sig == error_mark_node)
22276             {
22277               cp_parser_skip_to_end_of_block_or_statement (parser);
22278               token = cp_lexer_peek_token (parser->lexer);
22279               continue;
22280             }
22281           objc_add_method_declaration (is_class_method, sig, attributes);
22282           cp_parser_consume_semicolon_at_end_of_statement (parser);
22283         }
22284       else if (token->keyword == RID_AT_PROPERTY)
22285         cp_parser_objc_at_property_declaration (parser);
22286       else if (token->keyword == RID_ATTRIBUTE 
22287                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22288         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22289                     OPT_Wattributes, 
22290                     "prefix attributes are ignored for methods");
22291       else
22292         /* Allow for interspersed non-ObjC++ code.  */
22293         cp_parser_objc_interstitial_code (parser);
22294
22295       token = cp_lexer_peek_token (parser->lexer);
22296     }
22297
22298   if (token->type != CPP_EOF)
22299     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22300   else
22301     cp_parser_error (parser, "expected %<@end%>");
22302
22303   objc_finish_interface ();
22304 }
22305
22306 /* Parse an Objective-C method definition list.  */
22307
22308 static void
22309 cp_parser_objc_method_definition_list (cp_parser* parser)
22310 {
22311   cp_token *token = cp_lexer_peek_token (parser->lexer);
22312
22313   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22314     {
22315       tree meth;
22316
22317       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22318         {
22319           cp_token *ptk;
22320           tree sig, attribute;
22321           bool is_class_method;
22322           if (token->type == CPP_PLUS)
22323             is_class_method = true;
22324           else
22325             is_class_method = false;
22326           push_deferring_access_checks (dk_deferred);
22327           sig = cp_parser_objc_method_signature (parser, &attribute);
22328           if (sig == error_mark_node)
22329             {
22330               cp_parser_skip_to_end_of_block_or_statement (parser);
22331               token = cp_lexer_peek_token (parser->lexer);
22332               continue;
22333             }
22334           objc_start_method_definition (is_class_method, sig, attribute,
22335                                         NULL_TREE);
22336
22337           /* For historical reasons, we accept an optional semicolon.  */
22338           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22339             cp_lexer_consume_token (parser->lexer);
22340
22341           ptk = cp_lexer_peek_token (parser->lexer);
22342           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22343                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22344             {
22345               perform_deferred_access_checks ();
22346               stop_deferring_access_checks ();
22347               meth = cp_parser_function_definition_after_declarator (parser,
22348                                                                      false);
22349               pop_deferring_access_checks ();
22350               objc_finish_method_definition (meth);
22351             }
22352         }
22353       /* The following case will be removed once @synthesize is
22354          completely implemented.  */
22355       else if (token->keyword == RID_AT_PROPERTY)
22356         cp_parser_objc_at_property_declaration (parser);
22357       else if (token->keyword == RID_AT_SYNTHESIZE)
22358         cp_parser_objc_at_synthesize_declaration (parser);
22359       else if (token->keyword == RID_AT_DYNAMIC)
22360         cp_parser_objc_at_dynamic_declaration (parser);
22361       else if (token->keyword == RID_ATTRIBUTE 
22362                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22363         warning_at (token->location, OPT_Wattributes,
22364                     "prefix attributes are ignored for methods");
22365       else
22366         /* Allow for interspersed non-ObjC++ code.  */
22367         cp_parser_objc_interstitial_code (parser);
22368
22369       token = cp_lexer_peek_token (parser->lexer);
22370     }
22371
22372   if (token->type != CPP_EOF)
22373     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22374   else
22375     cp_parser_error (parser, "expected %<@end%>");
22376
22377   objc_finish_implementation ();
22378 }
22379
22380 /* Parse Objective-C ivars.  */
22381
22382 static void
22383 cp_parser_objc_class_ivars (cp_parser* parser)
22384 {
22385   cp_token *token = cp_lexer_peek_token (parser->lexer);
22386
22387   if (token->type != CPP_OPEN_BRACE)
22388     return;     /* No ivars specified.  */
22389
22390   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22391   token = cp_lexer_peek_token (parser->lexer);
22392
22393   while (token->type != CPP_CLOSE_BRACE 
22394         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22395     {
22396       cp_decl_specifier_seq declspecs;
22397       int decl_class_or_enum_p;
22398       tree prefix_attributes;
22399
22400       cp_parser_objc_visibility_spec (parser);
22401
22402       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22403         break;
22404
22405       cp_parser_decl_specifier_seq (parser,
22406                                     CP_PARSER_FLAGS_OPTIONAL,
22407                                     &declspecs,
22408                                     &decl_class_or_enum_p);
22409
22410       /* auto, register, static, extern, mutable.  */
22411       if (declspecs.storage_class != sc_none)
22412         {
22413           cp_parser_error (parser, "invalid type for instance variable");         
22414           declspecs.storage_class = sc_none;
22415         }
22416
22417       /* __thread.  */
22418       if (declspecs.specs[(int) ds_thread])
22419         {
22420           cp_parser_error (parser, "invalid type for instance variable");
22421           declspecs.specs[(int) ds_thread] = 0;
22422         }
22423       
22424       /* typedef.  */
22425       if (declspecs.specs[(int) ds_typedef])
22426         {
22427           cp_parser_error (parser, "invalid type for instance variable");
22428           declspecs.specs[(int) ds_typedef] = 0;
22429         }
22430
22431       prefix_attributes = declspecs.attributes;
22432       declspecs.attributes = NULL_TREE;
22433
22434       /* Keep going until we hit the `;' at the end of the
22435          declaration.  */
22436       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22437         {
22438           tree width = NULL_TREE, attributes, first_attribute, decl;
22439           cp_declarator *declarator = NULL;
22440           int ctor_dtor_or_conv_p;
22441
22442           /* Check for a (possibly unnamed) bitfield declaration.  */
22443           token = cp_lexer_peek_token (parser->lexer);
22444           if (token->type == CPP_COLON)
22445             goto eat_colon;
22446
22447           if (token->type == CPP_NAME
22448               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22449                   == CPP_COLON))
22450             {
22451               /* Get the name of the bitfield.  */
22452               declarator = make_id_declarator (NULL_TREE,
22453                                                cp_parser_identifier (parser),
22454                                                sfk_none);
22455
22456              eat_colon:
22457               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22458               /* Get the width of the bitfield.  */
22459               width
22460                 = cp_parser_constant_expression (parser,
22461                                                  /*allow_non_constant=*/false,
22462                                                  NULL);
22463             }
22464           else
22465             {
22466               /* Parse the declarator.  */
22467               declarator
22468                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22469                                         &ctor_dtor_or_conv_p,
22470                                         /*parenthesized_p=*/NULL,
22471                                         /*member_p=*/false);
22472             }
22473
22474           /* Look for attributes that apply to the ivar.  */
22475           attributes = cp_parser_attributes_opt (parser);
22476           /* Remember which attributes are prefix attributes and
22477              which are not.  */
22478           first_attribute = attributes;
22479           /* Combine the attributes.  */
22480           attributes = chainon (prefix_attributes, attributes);
22481
22482           if (width)
22483               /* Create the bitfield declaration.  */
22484               decl = grokbitfield (declarator, &declspecs,
22485                                    width,
22486                                    attributes);
22487           else
22488             decl = grokfield (declarator, &declspecs,
22489                               NULL_TREE, /*init_const_expr_p=*/false,
22490                               NULL_TREE, attributes);
22491
22492           /* Add the instance variable.  */
22493           objc_add_instance_variable (decl);
22494
22495           /* Reset PREFIX_ATTRIBUTES.  */
22496           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22497             attributes = TREE_CHAIN (attributes);
22498           if (attributes)
22499             TREE_CHAIN (attributes) = NULL_TREE;
22500
22501           token = cp_lexer_peek_token (parser->lexer);
22502
22503           if (token->type == CPP_COMMA)
22504             {
22505               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22506               continue;
22507             }
22508           break;
22509         }
22510
22511       cp_parser_consume_semicolon_at_end_of_statement (parser);
22512       token = cp_lexer_peek_token (parser->lexer);
22513     }
22514
22515   if (token->keyword == RID_AT_END)
22516     cp_parser_error (parser, "expected %<}%>");
22517
22518   /* Do not consume the RID_AT_END, so it will be read again as terminating
22519      the @interface of @implementation.  */ 
22520   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22521     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22522     
22523   /* For historical reasons, we accept an optional semicolon.  */
22524   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22525     cp_lexer_consume_token (parser->lexer);
22526 }
22527
22528 /* Parse an Objective-C protocol declaration.  */
22529
22530 static void
22531 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22532 {
22533   tree proto, protorefs;
22534   cp_token *tok;
22535
22536   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22537   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22538     {
22539       tok = cp_lexer_peek_token (parser->lexer);
22540       error_at (tok->location, "identifier expected after %<@protocol%>");
22541       cp_parser_consume_semicolon_at_end_of_statement (parser);
22542       return;
22543     }
22544
22545   /* See if we have a forward declaration or a definition.  */
22546   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22547
22548   /* Try a forward declaration first.  */
22549   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22550     {
22551       while (true)
22552         {
22553           tree id;
22554           
22555           id = cp_parser_identifier (parser);
22556           if (id == error_mark_node)
22557             break;
22558           
22559           objc_declare_protocol (id, attributes);
22560           
22561           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22562             cp_lexer_consume_token (parser->lexer);
22563           else
22564             break;
22565         }
22566       cp_parser_consume_semicolon_at_end_of_statement (parser);
22567     }
22568
22569   /* Ok, we got a full-fledged definition (or at least should).  */
22570   else
22571     {
22572       proto = cp_parser_identifier (parser);
22573       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22574       objc_start_protocol (proto, protorefs, attributes);
22575       cp_parser_objc_method_prototype_list (parser);
22576     }
22577 }
22578
22579 /* Parse an Objective-C superclass or category.  */
22580
22581 static void
22582 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22583                                        bool iface_p,
22584                                        tree *super,
22585                                        tree *categ, bool *is_class_extension)
22586 {
22587   cp_token *next = cp_lexer_peek_token (parser->lexer);
22588
22589   *super = *categ = NULL_TREE;
22590   *is_class_extension = false;
22591   if (next->type == CPP_COLON)
22592     {
22593       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22594       *super = cp_parser_identifier (parser);
22595     }
22596   else if (next->type == CPP_OPEN_PAREN)
22597     {
22598       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22599
22600       /* If there is no category name, and this is an @interface, we
22601          have a class extension.  */
22602       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22603         {
22604           *categ = NULL_TREE;
22605           *is_class_extension = true;
22606         }
22607       else
22608         *categ = cp_parser_identifier (parser);
22609
22610       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22611     }
22612 }
22613
22614 /* Parse an Objective-C class interface.  */
22615
22616 static void
22617 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22618 {
22619   tree name, super, categ, protos;
22620   bool is_class_extension;
22621
22622   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22623   name = cp_parser_identifier (parser);
22624   if (name == error_mark_node)
22625     {
22626       /* It's hard to recover because even if valid @interface stuff
22627          is to follow, we can't compile it (or validate it) if we
22628          don't even know which class it refers to.  Let's assume this
22629          was a stray '@interface' token in the stream and skip it.
22630       */
22631       return;
22632     }
22633   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22634                                          &is_class_extension);
22635   protos = cp_parser_objc_protocol_refs_opt (parser);
22636
22637   /* We have either a class or a category on our hands.  */
22638   if (categ || is_class_extension)
22639     objc_start_category_interface (name, categ, protos, attributes);
22640   else
22641     {
22642       objc_start_class_interface (name, super, protos, attributes);
22643       /* Handle instance variable declarations, if any.  */
22644       cp_parser_objc_class_ivars (parser);
22645       objc_continue_interface ();
22646     }
22647
22648   cp_parser_objc_method_prototype_list (parser);
22649 }
22650
22651 /* Parse an Objective-C class implementation.  */
22652
22653 static void
22654 cp_parser_objc_class_implementation (cp_parser* parser)
22655 {
22656   tree name, super, categ;
22657   bool is_class_extension;
22658
22659   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22660   name = cp_parser_identifier (parser);
22661   if (name == error_mark_node)
22662     {
22663       /* It's hard to recover because even if valid @implementation
22664          stuff is to follow, we can't compile it (or validate it) if
22665          we don't even know which class it refers to.  Let's assume
22666          this was a stray '@implementation' token in the stream and
22667          skip it.
22668       */
22669       return;
22670     }
22671   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22672                                          &is_class_extension);
22673
22674   /* We have either a class or a category on our hands.  */
22675   if (categ)
22676     objc_start_category_implementation (name, categ);
22677   else
22678     {
22679       objc_start_class_implementation (name, super);
22680       /* Handle instance variable declarations, if any.  */
22681       cp_parser_objc_class_ivars (parser);
22682       objc_continue_implementation ();
22683     }
22684
22685   cp_parser_objc_method_definition_list (parser);
22686 }
22687
22688 /* Consume the @end token and finish off the implementation.  */
22689
22690 static void
22691 cp_parser_objc_end_implementation (cp_parser* parser)
22692 {
22693   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22694   objc_finish_implementation ();
22695 }
22696
22697 /* Parse an Objective-C declaration.  */
22698
22699 static void
22700 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22701 {
22702   /* Try to figure out what kind of declaration is present.  */
22703   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22704
22705   if (attributes)
22706     switch (kwd->keyword)
22707       {
22708         case RID_AT_ALIAS:
22709         case RID_AT_CLASS:
22710         case RID_AT_END:
22711           error_at (kwd->location, "attributes may not be specified before"
22712                     " the %<@%D%> Objective-C++ keyword",
22713                     kwd->u.value);
22714           attributes = NULL;
22715           break;
22716         case RID_AT_IMPLEMENTATION:
22717           warning_at (kwd->location, OPT_Wattributes,
22718                       "prefix attributes are ignored before %<@%D%>",
22719                       kwd->u.value);
22720           attributes = NULL;
22721         default:
22722           break;
22723       }
22724
22725   switch (kwd->keyword)
22726     {
22727     case RID_AT_ALIAS:
22728       cp_parser_objc_alias_declaration (parser);
22729       break;
22730     case RID_AT_CLASS:
22731       cp_parser_objc_class_declaration (parser);
22732       break;
22733     case RID_AT_PROTOCOL:
22734       cp_parser_objc_protocol_declaration (parser, attributes);
22735       break;
22736     case RID_AT_INTERFACE:
22737       cp_parser_objc_class_interface (parser, attributes);
22738       break;
22739     case RID_AT_IMPLEMENTATION:
22740       cp_parser_objc_class_implementation (parser);
22741       break;
22742     case RID_AT_END:
22743       cp_parser_objc_end_implementation (parser);
22744       break;
22745     default:
22746       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22747                 kwd->u.value);
22748       cp_parser_skip_to_end_of_block_or_statement (parser);
22749     }
22750 }
22751
22752 /* Parse an Objective-C try-catch-finally statement.
22753
22754    objc-try-catch-finally-stmt:
22755      @try compound-statement objc-catch-clause-seq [opt]
22756        objc-finally-clause [opt]
22757
22758    objc-catch-clause-seq:
22759      objc-catch-clause objc-catch-clause-seq [opt]
22760
22761    objc-catch-clause:
22762      @catch ( objc-exception-declaration ) compound-statement
22763
22764    objc-finally-clause:
22765      @finally compound-statement
22766
22767    objc-exception-declaration:
22768      parameter-declaration
22769      '...'
22770
22771    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22772
22773    Returns NULL_TREE.
22774
22775    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22776    for C.  Keep them in sync.  */   
22777
22778 static tree
22779 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22780 {
22781   location_t location;
22782   tree stmt;
22783
22784   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22785   location = cp_lexer_peek_token (parser->lexer)->location;
22786   objc_maybe_warn_exceptions (location);
22787   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22788      node, lest it get absorbed into the surrounding block.  */
22789   stmt = push_stmt_list ();
22790   cp_parser_compound_statement (parser, NULL, false, false);
22791   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22792
22793   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22794     {
22795       cp_parameter_declarator *parm;
22796       tree parameter_declaration = error_mark_node;
22797       bool seen_open_paren = false;
22798
22799       cp_lexer_consume_token (parser->lexer);
22800       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22801         seen_open_paren = true;
22802       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22803         {
22804           /* We have "@catch (...)" (where the '...' are literally
22805              what is in the code).  Skip the '...'.
22806              parameter_declaration is set to NULL_TREE, and
22807              objc_being_catch_clauses() knows that that means
22808              '...'.  */
22809           cp_lexer_consume_token (parser->lexer);
22810           parameter_declaration = NULL_TREE;
22811         }
22812       else
22813         {
22814           /* We have "@catch (NSException *exception)" or something
22815              like that.  Parse the parameter declaration.  */
22816           parm = cp_parser_parameter_declaration (parser, false, NULL);
22817           if (parm == NULL)
22818             parameter_declaration = error_mark_node;
22819           else
22820             parameter_declaration = grokdeclarator (parm->declarator,
22821                                                     &parm->decl_specifiers,
22822                                                     PARM, /*initialized=*/0,
22823                                                     /*attrlist=*/NULL);
22824         }
22825       if (seen_open_paren)
22826         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22827       else
22828         {
22829           /* If there was no open parenthesis, we are recovering from
22830              an error, and we are trying to figure out what mistake
22831              the user has made.  */
22832
22833           /* If there is an immediate closing parenthesis, the user
22834              probably forgot the opening one (ie, they typed "@catch
22835              NSException *e)".  Parse the closing parenthesis and keep
22836              going.  */
22837           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22838             cp_lexer_consume_token (parser->lexer);
22839           
22840           /* If these is no immediate closing parenthesis, the user
22841              probably doesn't know that parenthesis are required at
22842              all (ie, they typed "@catch NSException *e").  So, just
22843              forget about the closing parenthesis and keep going.  */
22844         }
22845       objc_begin_catch_clause (parameter_declaration);
22846       cp_parser_compound_statement (parser, NULL, false, false);
22847       objc_finish_catch_clause ();
22848     }
22849   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22850     {
22851       cp_lexer_consume_token (parser->lexer);
22852       location = cp_lexer_peek_token (parser->lexer)->location;
22853       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22854          node, lest it get absorbed into the surrounding block.  */
22855       stmt = push_stmt_list ();
22856       cp_parser_compound_statement (parser, NULL, false, false);
22857       objc_build_finally_clause (location, pop_stmt_list (stmt));
22858     }
22859
22860   return objc_finish_try_stmt ();
22861 }
22862
22863 /* Parse an Objective-C synchronized statement.
22864
22865    objc-synchronized-stmt:
22866      @synchronized ( expression ) compound-statement
22867
22868    Returns NULL_TREE.  */
22869
22870 static tree
22871 cp_parser_objc_synchronized_statement (cp_parser *parser)
22872 {
22873   location_t location;
22874   tree lock, stmt;
22875
22876   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22877
22878   location = cp_lexer_peek_token (parser->lexer)->location;
22879   objc_maybe_warn_exceptions (location);
22880   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22881   lock = cp_parser_expression (parser, false, NULL);
22882   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22883
22884   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22885      node, lest it get absorbed into the surrounding block.  */
22886   stmt = push_stmt_list ();
22887   cp_parser_compound_statement (parser, NULL, false, false);
22888
22889   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22890 }
22891
22892 /* Parse an Objective-C throw statement.
22893
22894    objc-throw-stmt:
22895      @throw assignment-expression [opt] ;
22896
22897    Returns a constructed '@throw' statement.  */
22898
22899 static tree
22900 cp_parser_objc_throw_statement (cp_parser *parser)
22901 {
22902   tree expr = NULL_TREE;
22903   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22904
22905   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22906
22907   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22908     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22909
22910   cp_parser_consume_semicolon_at_end_of_statement (parser);
22911
22912   return objc_build_throw_stmt (loc, expr);
22913 }
22914
22915 /* Parse an Objective-C statement.  */
22916
22917 static tree
22918 cp_parser_objc_statement (cp_parser * parser)
22919 {
22920   /* Try to figure out what kind of declaration is present.  */
22921   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22922
22923   switch (kwd->keyword)
22924     {
22925     case RID_AT_TRY:
22926       return cp_parser_objc_try_catch_finally_statement (parser);
22927     case RID_AT_SYNCHRONIZED:
22928       return cp_parser_objc_synchronized_statement (parser);
22929     case RID_AT_THROW:
22930       return cp_parser_objc_throw_statement (parser);
22931     default:
22932       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22933                kwd->u.value);
22934       cp_parser_skip_to_end_of_block_or_statement (parser);
22935     }
22936
22937   return error_mark_node;
22938 }
22939
22940 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22941    look ahead to see if an objc keyword follows the attributes.  This
22942    is to detect the use of prefix attributes on ObjC @interface and 
22943    @protocol.  */
22944
22945 static bool
22946 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22947 {
22948   cp_lexer_save_tokens (parser->lexer);
22949   *attrib = cp_parser_attributes_opt (parser);
22950   gcc_assert (*attrib);
22951   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22952     {
22953       cp_lexer_commit_tokens (parser->lexer);
22954       return true;
22955     }
22956   cp_lexer_rollback_tokens (parser->lexer);
22957   return false;  
22958 }
22959
22960 /* This routine is a minimal replacement for
22961    c_parser_struct_declaration () used when parsing the list of
22962    types/names or ObjC++ properties.  For example, when parsing the
22963    code
22964
22965    @property (readonly) int a, b, c;
22966
22967    this function is responsible for parsing "int a, int b, int c" and
22968    returning the declarations as CHAIN of DECLs.
22969
22970    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22971    similar parsing.  */
22972 static tree
22973 cp_parser_objc_struct_declaration (cp_parser *parser)
22974 {
22975   tree decls = NULL_TREE;
22976   cp_decl_specifier_seq declspecs;
22977   int decl_class_or_enum_p;
22978   tree prefix_attributes;
22979
22980   cp_parser_decl_specifier_seq (parser,
22981                                 CP_PARSER_FLAGS_NONE,
22982                                 &declspecs,
22983                                 &decl_class_or_enum_p);
22984
22985   if (declspecs.type == error_mark_node)
22986     return error_mark_node;
22987
22988   /* auto, register, static, extern, mutable.  */
22989   if (declspecs.storage_class != sc_none)
22990     {
22991       cp_parser_error (parser, "invalid type for property");
22992       declspecs.storage_class = sc_none;
22993     }
22994   
22995   /* __thread.  */
22996   if (declspecs.specs[(int) ds_thread])
22997     {
22998       cp_parser_error (parser, "invalid type for property");
22999       declspecs.specs[(int) ds_thread] = 0;
23000     }
23001   
23002   /* typedef.  */
23003   if (declspecs.specs[(int) ds_typedef])
23004     {
23005       cp_parser_error (parser, "invalid type for property");
23006       declspecs.specs[(int) ds_typedef] = 0;
23007     }
23008
23009   prefix_attributes = declspecs.attributes;
23010   declspecs.attributes = NULL_TREE;
23011
23012   /* Keep going until we hit the `;' at the end of the declaration. */
23013   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23014     {
23015       tree attributes, first_attribute, decl;
23016       cp_declarator *declarator;
23017       cp_token *token;
23018
23019       /* Parse the declarator.  */
23020       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23021                                          NULL, NULL, false);
23022
23023       /* Look for attributes that apply to the ivar.  */
23024       attributes = cp_parser_attributes_opt (parser);
23025       /* Remember which attributes are prefix attributes and
23026          which are not.  */
23027       first_attribute = attributes;
23028       /* Combine the attributes.  */
23029       attributes = chainon (prefix_attributes, attributes);
23030       
23031       decl = grokfield (declarator, &declspecs,
23032                         NULL_TREE, /*init_const_expr_p=*/false,
23033                         NULL_TREE, attributes);
23034
23035       if (decl == error_mark_node || decl == NULL_TREE)
23036         return error_mark_node;
23037       
23038       /* Reset PREFIX_ATTRIBUTES.  */
23039       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23040         attributes = TREE_CHAIN (attributes);
23041       if (attributes)
23042         TREE_CHAIN (attributes) = NULL_TREE;
23043
23044       DECL_CHAIN (decl) = decls;
23045       decls = decl;
23046
23047       token = cp_lexer_peek_token (parser->lexer);
23048       if (token->type == CPP_COMMA)
23049         {
23050           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23051           continue;
23052         }
23053       else
23054         break;
23055     }
23056   return decls;
23057 }
23058
23059 /* Parse an Objective-C @property declaration.  The syntax is:
23060
23061    objc-property-declaration:
23062      '@property' objc-property-attributes[opt] struct-declaration ;
23063
23064    objc-property-attributes:
23065     '(' objc-property-attribute-list ')'
23066
23067    objc-property-attribute-list:
23068      objc-property-attribute
23069      objc-property-attribute-list, objc-property-attribute
23070
23071    objc-property-attribute
23072      'getter' = identifier
23073      'setter' = identifier
23074      'readonly'
23075      'readwrite'
23076      'assign'
23077      'retain'
23078      'copy'
23079      'nonatomic'
23080
23081   For example:
23082     @property NSString *name;
23083     @property (readonly) id object;
23084     @property (retain, nonatomic, getter=getTheName) id name;
23085     @property int a, b, c;
23086
23087    PS: This function is identical to
23088    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23089 static void 
23090 cp_parser_objc_at_property_declaration (cp_parser *parser)
23091 {
23092   /* The following variables hold the attributes of the properties as
23093      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23094      seen.  When we see an attribute, we set them to 'true' (if they
23095      are boolean properties) or to the identifier (if they have an
23096      argument, ie, for getter and setter).  Note that here we only
23097      parse the list of attributes, check the syntax and accumulate the
23098      attributes that we find.  objc_add_property_declaration() will
23099      then process the information.  */
23100   bool property_assign = false;
23101   bool property_copy = false;
23102   tree property_getter_ident = NULL_TREE;
23103   bool property_nonatomic = false;
23104   bool property_readonly = false;
23105   bool property_readwrite = false;
23106   bool property_retain = false;
23107   tree property_setter_ident = NULL_TREE;
23108
23109   /* 'properties' is the list of properties that we read.  Usually a
23110      single one, but maybe more (eg, in "@property int a, b, c;" there
23111      are three).  */
23112   tree properties;
23113   location_t loc;
23114
23115   loc = cp_lexer_peek_token (parser->lexer)->location;
23116
23117   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23118
23119   /* Parse the optional attribute list...  */
23120   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23121     {
23122       /* Eat the '('.  */
23123       cp_lexer_consume_token (parser->lexer);
23124
23125       while (true)
23126         {
23127           bool syntax_error = false;
23128           cp_token *token = cp_lexer_peek_token (parser->lexer);
23129           enum rid keyword;
23130
23131           if (token->type != CPP_NAME)
23132             {
23133               cp_parser_error (parser, "expected identifier");
23134               break;
23135             }
23136           keyword = C_RID_CODE (token->u.value);
23137           cp_lexer_consume_token (parser->lexer);
23138           switch (keyword)
23139             {
23140             case RID_ASSIGN:    property_assign = true;    break;
23141             case RID_COPY:      property_copy = true;      break;
23142             case RID_NONATOMIC: property_nonatomic = true; break;
23143             case RID_READONLY:  property_readonly = true;  break;
23144             case RID_READWRITE: property_readwrite = true; break;
23145             case RID_RETAIN:    property_retain = true;    break;
23146
23147             case RID_GETTER:
23148             case RID_SETTER:
23149               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23150                 {
23151                   if (keyword == RID_GETTER)
23152                     cp_parser_error (parser,
23153                                      "missing %<=%> (after %<getter%> attribute)");
23154                   else
23155                     cp_parser_error (parser,
23156                                      "missing %<=%> (after %<setter%> attribute)");
23157                   syntax_error = true;
23158                   break;
23159                 }
23160               cp_lexer_consume_token (parser->lexer); /* eat the = */
23161               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23162                 {
23163                   cp_parser_error (parser, "expected identifier");
23164                   syntax_error = true;
23165                   break;
23166                 }
23167               if (keyword == RID_SETTER)
23168                 {
23169                   if (property_setter_ident != NULL_TREE)
23170                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23171                   else
23172                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23173                   cp_lexer_consume_token (parser->lexer);
23174                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23175                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23176                   else
23177                     cp_lexer_consume_token (parser->lexer);
23178                 }
23179               else
23180                 {
23181                   if (property_getter_ident != NULL_TREE)
23182                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23183                   else
23184                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23185                   cp_lexer_consume_token (parser->lexer);
23186                 }
23187               break;
23188             default:
23189               cp_parser_error (parser, "unknown property attribute");
23190               syntax_error = true;
23191               break;
23192             }
23193
23194           if (syntax_error)
23195             break;
23196
23197           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23198             cp_lexer_consume_token (parser->lexer);
23199           else
23200             break;
23201         }
23202
23203       /* FIXME: "@property (setter, assign);" will generate a spurious
23204          "error: expected â€˜)’ before â€˜,’ token".  This is because
23205          cp_parser_require, unlike the C counterpart, will produce an
23206          error even if we are in error recovery.  */
23207       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23208         {
23209           cp_parser_skip_to_closing_parenthesis (parser,
23210                                                  /*recovering=*/true,
23211                                                  /*or_comma=*/false,
23212                                                  /*consume_paren=*/true);
23213         }
23214     }
23215
23216   /* ... and the property declaration(s).  */
23217   properties = cp_parser_objc_struct_declaration (parser);
23218
23219   if (properties == error_mark_node)
23220     {
23221       cp_parser_skip_to_end_of_statement (parser);
23222       /* If the next token is now a `;', consume it.  */
23223       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23224         cp_lexer_consume_token (parser->lexer);
23225       return;
23226     }
23227
23228   if (properties == NULL_TREE)
23229     cp_parser_error (parser, "expected identifier");
23230   else
23231     {
23232       /* Comma-separated properties are chained together in
23233          reverse order; add them one by one.  */
23234       properties = nreverse (properties);
23235       
23236       for (; properties; properties = TREE_CHAIN (properties))
23237         objc_add_property_declaration (loc, copy_node (properties),
23238                                        property_readonly, property_readwrite,
23239                                        property_assign, property_retain,
23240                                        property_copy, property_nonatomic,
23241                                        property_getter_ident, property_setter_ident);
23242     }
23243   
23244   cp_parser_consume_semicolon_at_end_of_statement (parser);
23245 }
23246
23247 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23248
23249    objc-synthesize-declaration:
23250      @synthesize objc-synthesize-identifier-list ;
23251
23252    objc-synthesize-identifier-list:
23253      objc-synthesize-identifier
23254      objc-synthesize-identifier-list, objc-synthesize-identifier
23255
23256    objc-synthesize-identifier
23257      identifier
23258      identifier = identifier
23259
23260   For example:
23261     @synthesize MyProperty;
23262     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23263
23264   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23265   for C.  Keep them in sync.
23266 */
23267 static void 
23268 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23269 {
23270   tree list = NULL_TREE;
23271   location_t loc;
23272   loc = cp_lexer_peek_token (parser->lexer)->location;
23273
23274   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23275   while (true)
23276     {
23277       tree property, ivar;
23278       property = cp_parser_identifier (parser);
23279       if (property == error_mark_node)
23280         {
23281           cp_parser_consume_semicolon_at_end_of_statement (parser);
23282           return;
23283         }
23284       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23285         {
23286           cp_lexer_consume_token (parser->lexer);
23287           ivar = cp_parser_identifier (parser);
23288           if (ivar == error_mark_node)
23289             {
23290               cp_parser_consume_semicolon_at_end_of_statement (parser);
23291               return;
23292             }
23293         }
23294       else
23295         ivar = NULL_TREE;
23296       list = chainon (list, build_tree_list (ivar, property));
23297       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23298         cp_lexer_consume_token (parser->lexer);
23299       else
23300         break;
23301     }
23302   cp_parser_consume_semicolon_at_end_of_statement (parser);
23303   objc_add_synthesize_declaration (loc, list);
23304 }
23305
23306 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23307
23308    objc-dynamic-declaration:
23309      @dynamic identifier-list ;
23310
23311    For example:
23312      @dynamic MyProperty;
23313      @dynamic MyProperty, AnotherProperty;
23314
23315   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23316   for C.  Keep them in sync.
23317 */
23318 static void 
23319 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23320 {
23321   tree list = NULL_TREE;
23322   location_t loc;
23323   loc = cp_lexer_peek_token (parser->lexer)->location;
23324
23325   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23326   while (true)
23327     {
23328       tree property;
23329       property = cp_parser_identifier (parser);
23330       if (property == error_mark_node)
23331         {
23332           cp_parser_consume_semicolon_at_end_of_statement (parser);
23333           return;
23334         }
23335       list = chainon (list, build_tree_list (NULL, property));
23336       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23337         cp_lexer_consume_token (parser->lexer);
23338       else
23339         break;
23340     }
23341   cp_parser_consume_semicolon_at_end_of_statement (parser);
23342   objc_add_dynamic_declaration (loc, list);
23343 }
23344
23345 \f
23346 /* OpenMP 2.5 parsing routines.  */
23347
23348 /* Returns name of the next clause.
23349    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23350    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23351    returned and the token is consumed.  */
23352
23353 static pragma_omp_clause
23354 cp_parser_omp_clause_name (cp_parser *parser)
23355 {
23356   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23357
23358   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23359     result = PRAGMA_OMP_CLAUSE_IF;
23360   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23361     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23362   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23363     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23364   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23365     {
23366       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23367       const char *p = IDENTIFIER_POINTER (id);
23368
23369       switch (p[0])
23370         {
23371         case 'c':
23372           if (!strcmp ("collapse", p))
23373             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23374           else if (!strcmp ("copyin", p))
23375             result = PRAGMA_OMP_CLAUSE_COPYIN;
23376           else if (!strcmp ("copyprivate", p))
23377             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23378           break;
23379         case 'f':
23380           if (!strcmp ("firstprivate", p))
23381             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23382           break;
23383         case 'l':
23384           if (!strcmp ("lastprivate", p))
23385             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23386           break;
23387         case 'n':
23388           if (!strcmp ("nowait", p))
23389             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23390           else if (!strcmp ("num_threads", p))
23391             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23392           break;
23393         case 'o':
23394           if (!strcmp ("ordered", p))
23395             result = PRAGMA_OMP_CLAUSE_ORDERED;
23396           break;
23397         case 'r':
23398           if (!strcmp ("reduction", p))
23399             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23400           break;
23401         case 's':
23402           if (!strcmp ("schedule", p))
23403             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23404           else if (!strcmp ("shared", p))
23405             result = PRAGMA_OMP_CLAUSE_SHARED;
23406           break;
23407         case 'u':
23408           if (!strcmp ("untied", p))
23409             result = PRAGMA_OMP_CLAUSE_UNTIED;
23410           break;
23411         }
23412     }
23413
23414   if (result != PRAGMA_OMP_CLAUSE_NONE)
23415     cp_lexer_consume_token (parser->lexer);
23416
23417   return result;
23418 }
23419
23420 /* Validate that a clause of the given type does not already exist.  */
23421
23422 static void
23423 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23424                            const char *name, location_t location)
23425 {
23426   tree c;
23427
23428   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23429     if (OMP_CLAUSE_CODE (c) == code)
23430       {
23431         error_at (location, "too many %qs clauses", name);
23432         break;
23433       }
23434 }
23435
23436 /* OpenMP 2.5:
23437    variable-list:
23438      identifier
23439      variable-list , identifier
23440
23441    In addition, we match a closing parenthesis.  An opening parenthesis
23442    will have been consumed by the caller.
23443
23444    If KIND is nonzero, create the appropriate node and install the decl
23445    in OMP_CLAUSE_DECL and add the node to the head of the list.
23446
23447    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23448    return the list created.  */
23449
23450 static tree
23451 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23452                                 tree list)
23453 {
23454   cp_token *token;
23455   while (1)
23456     {
23457       tree name, decl;
23458
23459       token = cp_lexer_peek_token (parser->lexer);
23460       name = cp_parser_id_expression (parser, /*template_p=*/false,
23461                                       /*check_dependency_p=*/true,
23462                                       /*template_p=*/NULL,
23463                                       /*declarator_p=*/false,
23464                                       /*optional_p=*/false);
23465       if (name == error_mark_node)
23466         goto skip_comma;
23467
23468       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23469       if (decl == error_mark_node)
23470         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23471                                      token->location);
23472       else if (kind != 0)
23473         {
23474           tree u = build_omp_clause (token->location, kind);
23475           OMP_CLAUSE_DECL (u) = decl;
23476           OMP_CLAUSE_CHAIN (u) = list;
23477           list = u;
23478         }
23479       else
23480         list = tree_cons (decl, NULL_TREE, list);
23481
23482     get_comma:
23483       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23484         break;
23485       cp_lexer_consume_token (parser->lexer);
23486     }
23487
23488   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23489     {
23490       int ending;
23491
23492       /* Try to resync to an unnested comma.  Copied from
23493          cp_parser_parenthesized_expression_list.  */
23494     skip_comma:
23495       ending = cp_parser_skip_to_closing_parenthesis (parser,
23496                                                       /*recovering=*/true,
23497                                                       /*or_comma=*/true,
23498                                                       /*consume_paren=*/true);
23499       if (ending < 0)
23500         goto get_comma;
23501     }
23502
23503   return list;
23504 }
23505
23506 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23507    common case for omp clauses.  */
23508
23509 static tree
23510 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23511 {
23512   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23513     return cp_parser_omp_var_list_no_open (parser, kind, list);
23514   return list;
23515 }
23516
23517 /* OpenMP 3.0:
23518    collapse ( constant-expression ) */
23519
23520 static tree
23521 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23522 {
23523   tree c, num;
23524   location_t loc;
23525   HOST_WIDE_INT n;
23526
23527   loc = cp_lexer_peek_token (parser->lexer)->location;
23528   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23529     return list;
23530
23531   num = cp_parser_constant_expression (parser, false, NULL);
23532
23533   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23534     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23535                                            /*or_comma=*/false,
23536                                            /*consume_paren=*/true);
23537
23538   if (num == error_mark_node)
23539     return list;
23540   num = fold_non_dependent_expr (num);
23541   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23542       || !host_integerp (num, 0)
23543       || (n = tree_low_cst (num, 0)) <= 0
23544       || (int) n != n)
23545     {
23546       error_at (loc, "collapse argument needs positive constant integer expression");
23547       return list;
23548     }
23549
23550   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23551   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23552   OMP_CLAUSE_CHAIN (c) = list;
23553   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23554
23555   return c;
23556 }
23557
23558 /* OpenMP 2.5:
23559    default ( shared | none ) */
23560
23561 static tree
23562 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23563 {
23564   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23565   tree c;
23566
23567   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23568     return list;
23569   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23570     {
23571       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23572       const char *p = IDENTIFIER_POINTER (id);
23573
23574       switch (p[0])
23575         {
23576         case 'n':
23577           if (strcmp ("none", p) != 0)
23578             goto invalid_kind;
23579           kind = OMP_CLAUSE_DEFAULT_NONE;
23580           break;
23581
23582         case 's':
23583           if (strcmp ("shared", p) != 0)
23584             goto invalid_kind;
23585           kind = OMP_CLAUSE_DEFAULT_SHARED;
23586           break;
23587
23588         default:
23589           goto invalid_kind;
23590         }
23591
23592       cp_lexer_consume_token (parser->lexer);
23593     }
23594   else
23595     {
23596     invalid_kind:
23597       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23598     }
23599
23600   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23601     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23602                                            /*or_comma=*/false,
23603                                            /*consume_paren=*/true);
23604
23605   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23606     return list;
23607
23608   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23609   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23610   OMP_CLAUSE_CHAIN (c) = list;
23611   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23612
23613   return c;
23614 }
23615
23616 /* OpenMP 2.5:
23617    if ( expression ) */
23618
23619 static tree
23620 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23621 {
23622   tree t, c;
23623
23624   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23625     return list;
23626
23627   t = cp_parser_condition (parser);
23628
23629   if (t == error_mark_node
23630       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23631     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23632                                            /*or_comma=*/false,
23633                                            /*consume_paren=*/true);
23634
23635   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23636
23637   c = build_omp_clause (location, OMP_CLAUSE_IF);
23638   OMP_CLAUSE_IF_EXPR (c) = t;
23639   OMP_CLAUSE_CHAIN (c) = list;
23640
23641   return c;
23642 }
23643
23644 /* OpenMP 2.5:
23645    nowait */
23646
23647 static tree
23648 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23649                              tree list, location_t location)
23650 {
23651   tree c;
23652
23653   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23654
23655   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23656   OMP_CLAUSE_CHAIN (c) = list;
23657   return c;
23658 }
23659
23660 /* OpenMP 2.5:
23661    num_threads ( expression ) */
23662
23663 static tree
23664 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23665                                   location_t location)
23666 {
23667   tree t, c;
23668
23669   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23670     return list;
23671
23672   t = cp_parser_expression (parser, false, NULL);
23673
23674   if (t == error_mark_node
23675       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23676     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23677                                            /*or_comma=*/false,
23678                                            /*consume_paren=*/true);
23679
23680   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23681                              "num_threads", location);
23682
23683   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23684   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23685   OMP_CLAUSE_CHAIN (c) = list;
23686
23687   return c;
23688 }
23689
23690 /* OpenMP 2.5:
23691    ordered */
23692
23693 static tree
23694 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23695                               tree list, location_t location)
23696 {
23697   tree c;
23698
23699   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23700                              "ordered", location);
23701
23702   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23703   OMP_CLAUSE_CHAIN (c) = list;
23704   return c;
23705 }
23706
23707 /* OpenMP 2.5:
23708    reduction ( reduction-operator : variable-list )
23709
23710    reduction-operator:
23711      One of: + * - & ^ | && || */
23712
23713 static tree
23714 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23715 {
23716   enum tree_code code;
23717   tree nlist, c;
23718
23719   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23720     return list;
23721
23722   switch (cp_lexer_peek_token (parser->lexer)->type)
23723     {
23724     case CPP_PLUS:
23725       code = PLUS_EXPR;
23726       break;
23727     case CPP_MULT:
23728       code = MULT_EXPR;
23729       break;
23730     case CPP_MINUS:
23731       code = MINUS_EXPR;
23732       break;
23733     case CPP_AND:
23734       code = BIT_AND_EXPR;
23735       break;
23736     case CPP_XOR:
23737       code = BIT_XOR_EXPR;
23738       break;
23739     case CPP_OR:
23740       code = BIT_IOR_EXPR;
23741       break;
23742     case CPP_AND_AND:
23743       code = TRUTH_ANDIF_EXPR;
23744       break;
23745     case CPP_OR_OR:
23746       code = TRUTH_ORIF_EXPR;
23747       break;
23748     default:
23749       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23750                                "%<|%>, %<&&%>, or %<||%>");
23751     resync_fail:
23752       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23753                                              /*or_comma=*/false,
23754                                              /*consume_paren=*/true);
23755       return list;
23756     }
23757   cp_lexer_consume_token (parser->lexer);
23758
23759   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23760     goto resync_fail;
23761
23762   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23763   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23764     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23765
23766   return nlist;
23767 }
23768
23769 /* OpenMP 2.5:
23770    schedule ( schedule-kind )
23771    schedule ( schedule-kind , expression )
23772
23773    schedule-kind:
23774      static | dynamic | guided | runtime | auto  */
23775
23776 static tree
23777 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23778 {
23779   tree c, t;
23780
23781   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23782     return list;
23783
23784   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23785
23786   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23787     {
23788       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23789       const char *p = IDENTIFIER_POINTER (id);
23790
23791       switch (p[0])
23792         {
23793         case 'd':
23794           if (strcmp ("dynamic", p) != 0)
23795             goto invalid_kind;
23796           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23797           break;
23798
23799         case 'g':
23800           if (strcmp ("guided", p) != 0)
23801             goto invalid_kind;
23802           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23803           break;
23804
23805         case 'r':
23806           if (strcmp ("runtime", p) != 0)
23807             goto invalid_kind;
23808           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23809           break;
23810
23811         default:
23812           goto invalid_kind;
23813         }
23814     }
23815   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23816     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23817   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23818     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23819   else
23820     goto invalid_kind;
23821   cp_lexer_consume_token (parser->lexer);
23822
23823   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23824     {
23825       cp_token *token;
23826       cp_lexer_consume_token (parser->lexer);
23827
23828       token = cp_lexer_peek_token (parser->lexer);
23829       t = cp_parser_assignment_expression (parser, false, NULL);
23830
23831       if (t == error_mark_node)
23832         goto resync_fail;
23833       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23834         error_at (token->location, "schedule %<runtime%> does not take "
23835                   "a %<chunk_size%> parameter");
23836       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23837         error_at (token->location, "schedule %<auto%> does not take "
23838                   "a %<chunk_size%> parameter");
23839       else
23840         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23841
23842       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23843         goto resync_fail;
23844     }
23845   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23846     goto resync_fail;
23847
23848   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23849   OMP_CLAUSE_CHAIN (c) = list;
23850   return c;
23851
23852  invalid_kind:
23853   cp_parser_error (parser, "invalid schedule kind");
23854  resync_fail:
23855   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23856                                          /*or_comma=*/false,
23857                                          /*consume_paren=*/true);
23858   return list;
23859 }
23860
23861 /* OpenMP 3.0:
23862    untied */
23863
23864 static tree
23865 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23866                              tree list, location_t location)
23867 {
23868   tree c;
23869
23870   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23871
23872   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23873   OMP_CLAUSE_CHAIN (c) = list;
23874   return c;
23875 }
23876
23877 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23878    is a bitmask in MASK.  Return the list of clauses found; the result
23879    of clause default goes in *pdefault.  */
23880
23881 static tree
23882 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23883                            const char *where, cp_token *pragma_tok)
23884 {
23885   tree clauses = NULL;
23886   bool first = true;
23887   cp_token *token = NULL;
23888
23889   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23890     {
23891       pragma_omp_clause c_kind;
23892       const char *c_name;
23893       tree prev = clauses;
23894
23895       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23896         cp_lexer_consume_token (parser->lexer);
23897
23898       token = cp_lexer_peek_token (parser->lexer);
23899       c_kind = cp_parser_omp_clause_name (parser);
23900       first = false;
23901
23902       switch (c_kind)
23903         {
23904         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23905           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23906                                                    token->location);
23907           c_name = "collapse";
23908           break;
23909         case PRAGMA_OMP_CLAUSE_COPYIN:
23910           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23911           c_name = "copyin";
23912           break;
23913         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23914           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23915                                             clauses);
23916           c_name = "copyprivate";
23917           break;
23918         case PRAGMA_OMP_CLAUSE_DEFAULT:
23919           clauses = cp_parser_omp_clause_default (parser, clauses,
23920                                                   token->location);
23921           c_name = "default";
23922           break;
23923         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23924           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23925                                             clauses);
23926           c_name = "firstprivate";
23927           break;
23928         case PRAGMA_OMP_CLAUSE_IF:
23929           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23930           c_name = "if";
23931           break;
23932         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23933           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23934                                             clauses);
23935           c_name = "lastprivate";
23936           break;
23937         case PRAGMA_OMP_CLAUSE_NOWAIT:
23938           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23939           c_name = "nowait";
23940           break;
23941         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23942           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23943                                                       token->location);
23944           c_name = "num_threads";
23945           break;
23946         case PRAGMA_OMP_CLAUSE_ORDERED:
23947           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23948                                                   token->location);
23949           c_name = "ordered";
23950           break;
23951         case PRAGMA_OMP_CLAUSE_PRIVATE:
23952           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23953                                             clauses);
23954           c_name = "private";
23955           break;
23956         case PRAGMA_OMP_CLAUSE_REDUCTION:
23957           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23958           c_name = "reduction";
23959           break;
23960         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23961           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23962                                                    token->location);
23963           c_name = "schedule";
23964           break;
23965         case PRAGMA_OMP_CLAUSE_SHARED:
23966           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23967                                             clauses);
23968           c_name = "shared";
23969           break;
23970         case PRAGMA_OMP_CLAUSE_UNTIED:
23971           clauses = cp_parser_omp_clause_untied (parser, clauses,
23972                                                  token->location);
23973           c_name = "nowait";
23974           break;
23975         default:
23976           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23977           goto saw_error;
23978         }
23979
23980       if (((mask >> c_kind) & 1) == 0)
23981         {
23982           /* Remove the invalid clause(s) from the list to avoid
23983              confusing the rest of the compiler.  */
23984           clauses = prev;
23985           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23986         }
23987     }
23988  saw_error:
23989   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23990   return finish_omp_clauses (clauses);
23991 }
23992
23993 /* OpenMP 2.5:
23994    structured-block:
23995      statement
23996
23997    In practice, we're also interested in adding the statement to an
23998    outer node.  So it is convenient if we work around the fact that
23999    cp_parser_statement calls add_stmt.  */
24000
24001 static unsigned
24002 cp_parser_begin_omp_structured_block (cp_parser *parser)
24003 {
24004   unsigned save = parser->in_statement;
24005
24006   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24007      This preserves the "not within loop or switch" style error messages
24008      for nonsense cases like
24009         void foo() {
24010         #pragma omp single
24011           break;
24012         }
24013   */
24014   if (parser->in_statement)
24015     parser->in_statement = IN_OMP_BLOCK;
24016
24017   return save;
24018 }
24019
24020 static void
24021 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24022 {
24023   parser->in_statement = save;
24024 }
24025
24026 static tree
24027 cp_parser_omp_structured_block (cp_parser *parser)
24028 {
24029   tree stmt = begin_omp_structured_block ();
24030   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24031
24032   cp_parser_statement (parser, NULL_TREE, false, NULL);
24033
24034   cp_parser_end_omp_structured_block (parser, save);
24035   return finish_omp_structured_block (stmt);
24036 }
24037
24038 /* OpenMP 2.5:
24039    # pragma omp atomic new-line
24040      expression-stmt
24041
24042    expression-stmt:
24043      x binop= expr | x++ | ++x | x-- | --x
24044    binop:
24045      +, *, -, /, &, ^, |, <<, >>
24046
24047   where x is an lvalue expression with scalar type.  */
24048
24049 static void
24050 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24051 {
24052   tree lhs, rhs;
24053   enum tree_code code;
24054
24055   cp_parser_require_pragma_eol (parser, pragma_tok);
24056
24057   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24058                                     /*cast_p=*/false, NULL);
24059   switch (TREE_CODE (lhs))
24060     {
24061     case ERROR_MARK:
24062       goto saw_error;
24063
24064     case PREINCREMENT_EXPR:
24065     case POSTINCREMENT_EXPR:
24066       lhs = TREE_OPERAND (lhs, 0);
24067       code = PLUS_EXPR;
24068       rhs = integer_one_node;
24069       break;
24070
24071     case PREDECREMENT_EXPR:
24072     case POSTDECREMENT_EXPR:
24073       lhs = TREE_OPERAND (lhs, 0);
24074       code = MINUS_EXPR;
24075       rhs = integer_one_node;
24076       break;
24077
24078     case COMPOUND_EXPR:
24079       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24080          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24081          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24082          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24083          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24084                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24085             == BOOLEAN_TYPE)
24086        /* Undo effects of boolean_increment for post {in,de}crement.  */
24087        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24088       /* FALLTHRU */
24089     case MODIFY_EXPR:
24090       if (TREE_CODE (lhs) == MODIFY_EXPR
24091          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24092        {
24093          /* Undo effects of boolean_increment.  */
24094          if (integer_onep (TREE_OPERAND (lhs, 1)))
24095            {
24096              /* This is pre or post increment.  */
24097              rhs = TREE_OPERAND (lhs, 1);
24098              lhs = TREE_OPERAND (lhs, 0);
24099              code = NOP_EXPR;
24100              break;
24101            }
24102        }
24103       /* FALLTHRU */
24104     default:
24105       switch (cp_lexer_peek_token (parser->lexer)->type)
24106         {
24107         case CPP_MULT_EQ:
24108           code = MULT_EXPR;
24109           break;
24110         case CPP_DIV_EQ:
24111           code = TRUNC_DIV_EXPR;
24112           break;
24113         case CPP_PLUS_EQ:
24114           code = PLUS_EXPR;
24115           break;
24116         case CPP_MINUS_EQ:
24117           code = MINUS_EXPR;
24118           break;
24119         case CPP_LSHIFT_EQ:
24120           code = LSHIFT_EXPR;
24121           break;
24122         case CPP_RSHIFT_EQ:
24123           code = RSHIFT_EXPR;
24124           break;
24125         case CPP_AND_EQ:
24126           code = BIT_AND_EXPR;
24127           break;
24128         case CPP_OR_EQ:
24129           code = BIT_IOR_EXPR;
24130           break;
24131         case CPP_XOR_EQ:
24132           code = BIT_XOR_EXPR;
24133           break;
24134         default:
24135           cp_parser_error (parser,
24136                            "invalid operator for %<#pragma omp atomic%>");
24137           goto saw_error;
24138         }
24139       cp_lexer_consume_token (parser->lexer);
24140
24141       rhs = cp_parser_expression (parser, false, NULL);
24142       if (rhs == error_mark_node)
24143         goto saw_error;
24144       break;
24145     }
24146   finish_omp_atomic (code, lhs, rhs);
24147   cp_parser_consume_semicolon_at_end_of_statement (parser);
24148   return;
24149
24150  saw_error:
24151   cp_parser_skip_to_end_of_block_or_statement (parser);
24152 }
24153
24154
24155 /* OpenMP 2.5:
24156    # pragma omp barrier new-line  */
24157
24158 static void
24159 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24160 {
24161   cp_parser_require_pragma_eol (parser, pragma_tok);
24162   finish_omp_barrier ();
24163 }
24164
24165 /* OpenMP 2.5:
24166    # pragma omp critical [(name)] new-line
24167      structured-block  */
24168
24169 static tree
24170 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24171 {
24172   tree stmt, name = NULL;
24173
24174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24175     {
24176       cp_lexer_consume_token (parser->lexer);
24177
24178       name = cp_parser_identifier (parser);
24179
24180       if (name == error_mark_node
24181           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24182         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24183                                                /*or_comma=*/false,
24184                                                /*consume_paren=*/true);
24185       if (name == error_mark_node)
24186         name = NULL;
24187     }
24188   cp_parser_require_pragma_eol (parser, pragma_tok);
24189
24190   stmt = cp_parser_omp_structured_block (parser);
24191   return c_finish_omp_critical (input_location, stmt, name);
24192 }
24193
24194 /* OpenMP 2.5:
24195    # pragma omp flush flush-vars[opt] new-line
24196
24197    flush-vars:
24198      ( variable-list ) */
24199
24200 static void
24201 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24202 {
24203   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24204     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24205   cp_parser_require_pragma_eol (parser, pragma_tok);
24206
24207   finish_omp_flush ();
24208 }
24209
24210 /* Helper function, to parse omp for increment expression.  */
24211
24212 static tree
24213 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24214 {
24215   tree cond = cp_parser_binary_expression (parser, false, true,
24216                                            PREC_NOT_OPERATOR, NULL);
24217   bool overloaded_p;
24218
24219   if (cond == error_mark_node
24220       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24221     {
24222       cp_parser_skip_to_end_of_statement (parser);
24223       return error_mark_node;
24224     }
24225
24226   switch (TREE_CODE (cond))
24227     {
24228     case GT_EXPR:
24229     case GE_EXPR:
24230     case LT_EXPR:
24231     case LE_EXPR:
24232       break;
24233     default:
24234       return error_mark_node;
24235     }
24236
24237   /* If decl is an iterator, preserve LHS and RHS of the relational
24238      expr until finish_omp_for.  */
24239   if (decl
24240       && (type_dependent_expression_p (decl)
24241           || CLASS_TYPE_P (TREE_TYPE (decl))))
24242     return cond;
24243
24244   return build_x_binary_op (TREE_CODE (cond),
24245                             TREE_OPERAND (cond, 0), ERROR_MARK,
24246                             TREE_OPERAND (cond, 1), ERROR_MARK,
24247                             &overloaded_p, tf_warning_or_error);
24248 }
24249
24250 /* Helper function, to parse omp for increment expression.  */
24251
24252 static tree
24253 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24254 {
24255   cp_token *token = cp_lexer_peek_token (parser->lexer);
24256   enum tree_code op;
24257   tree lhs, rhs;
24258   cp_id_kind idk;
24259   bool decl_first;
24260
24261   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24262     {
24263       op = (token->type == CPP_PLUS_PLUS
24264             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24265       cp_lexer_consume_token (parser->lexer);
24266       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24267       if (lhs != decl)
24268         return error_mark_node;
24269       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24270     }
24271
24272   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24273   if (lhs != decl)
24274     return error_mark_node;
24275
24276   token = cp_lexer_peek_token (parser->lexer);
24277   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24278     {
24279       op = (token->type == CPP_PLUS_PLUS
24280             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24281       cp_lexer_consume_token (parser->lexer);
24282       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24283     }
24284
24285   op = cp_parser_assignment_operator_opt (parser);
24286   if (op == ERROR_MARK)
24287     return error_mark_node;
24288
24289   if (op != NOP_EXPR)
24290     {
24291       rhs = cp_parser_assignment_expression (parser, false, NULL);
24292       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24293       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24294     }
24295
24296   lhs = cp_parser_binary_expression (parser, false, false,
24297                                      PREC_ADDITIVE_EXPRESSION, NULL);
24298   token = cp_lexer_peek_token (parser->lexer);
24299   decl_first = lhs == decl;
24300   if (decl_first)
24301     lhs = NULL_TREE;
24302   if (token->type != CPP_PLUS
24303       && token->type != CPP_MINUS)
24304     return error_mark_node;
24305
24306   do
24307     {
24308       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24309       cp_lexer_consume_token (parser->lexer);
24310       rhs = cp_parser_binary_expression (parser, false, false,
24311                                          PREC_ADDITIVE_EXPRESSION, NULL);
24312       token = cp_lexer_peek_token (parser->lexer);
24313       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24314         {
24315           if (lhs == NULL_TREE)
24316             {
24317               if (op == PLUS_EXPR)
24318                 lhs = rhs;
24319               else
24320                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24321             }
24322           else
24323             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24324                                      NULL, tf_warning_or_error);
24325         }
24326     }
24327   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24328
24329   if (!decl_first)
24330     {
24331       if (rhs != decl || op == MINUS_EXPR)
24332         return error_mark_node;
24333       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24334     }
24335   else
24336     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24337
24338   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24339 }
24340
24341 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24342
24343 static tree
24344 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24345 {
24346   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24347   tree real_decl, initv, condv, incrv, declv;
24348   tree this_pre_body, cl;
24349   location_t loc_first;
24350   bool collapse_err = false;
24351   int i, collapse = 1, nbraces = 0;
24352   VEC(tree,gc) *for_block = make_tree_vector ();
24353
24354   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24355     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24356       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24357
24358   gcc_assert (collapse >= 1);
24359
24360   declv = make_tree_vec (collapse);
24361   initv = make_tree_vec (collapse);
24362   condv = make_tree_vec (collapse);
24363   incrv = make_tree_vec (collapse);
24364
24365   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24366
24367   for (i = 0; i < collapse; i++)
24368     {
24369       int bracecount = 0;
24370       bool add_private_clause = false;
24371       location_t loc;
24372
24373       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24374         {
24375           cp_parser_error (parser, "for statement expected");
24376           return NULL;
24377         }
24378       loc = cp_lexer_consume_token (parser->lexer)->location;
24379
24380       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24381         return NULL;
24382
24383       init = decl = real_decl = NULL;
24384       this_pre_body = push_stmt_list ();
24385       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24386         {
24387           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24388
24389              init-expr:
24390                        var = lb
24391                        integer-type var = lb
24392                        random-access-iterator-type var = lb
24393                        pointer-type var = lb
24394           */
24395           cp_decl_specifier_seq type_specifiers;
24396
24397           /* First, try to parse as an initialized declaration.  See
24398              cp_parser_condition, from whence the bulk of this is copied.  */
24399
24400           cp_parser_parse_tentatively (parser);
24401           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24402                                         /*is_trailing_return=*/false,
24403                                         &type_specifiers);
24404           if (cp_parser_parse_definitely (parser))
24405             {
24406               /* If parsing a type specifier seq succeeded, then this
24407                  MUST be a initialized declaration.  */
24408               tree asm_specification, attributes;
24409               cp_declarator *declarator;
24410
24411               declarator = cp_parser_declarator (parser,
24412                                                  CP_PARSER_DECLARATOR_NAMED,
24413                                                  /*ctor_dtor_or_conv_p=*/NULL,
24414                                                  /*parenthesized_p=*/NULL,
24415                                                  /*member_p=*/false);
24416               attributes = cp_parser_attributes_opt (parser);
24417               asm_specification = cp_parser_asm_specification_opt (parser);
24418
24419               if (declarator == cp_error_declarator) 
24420                 cp_parser_skip_to_end_of_statement (parser);
24421
24422               else 
24423                 {
24424                   tree pushed_scope, auto_node;
24425
24426                   decl = start_decl (declarator, &type_specifiers,
24427                                      SD_INITIALIZED, attributes,
24428                                      /*prefix_attributes=*/NULL_TREE,
24429                                      &pushed_scope);
24430
24431                   auto_node = type_uses_auto (TREE_TYPE (decl));
24432                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24433                     {
24434                       if (cp_lexer_next_token_is (parser->lexer, 
24435                                                   CPP_OPEN_PAREN))
24436                         error ("parenthesized initialization is not allowed in "
24437                                "OpenMP %<for%> loop");
24438                       else
24439                         /* Trigger an error.  */
24440                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24441
24442                       init = error_mark_node;
24443                       cp_parser_skip_to_end_of_statement (parser);
24444                     }
24445                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24446                            || type_dependent_expression_p (decl)
24447                            || auto_node)
24448                     {
24449                       bool is_direct_init, is_non_constant_init;
24450
24451                       init = cp_parser_initializer (parser,
24452                                                     &is_direct_init,
24453                                                     &is_non_constant_init);
24454
24455                       if (auto_node && describable_type (init))
24456                         {
24457                           TREE_TYPE (decl)
24458                             = do_auto_deduction (TREE_TYPE (decl), init,
24459                                                  auto_node);
24460
24461                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24462                               && !type_dependent_expression_p (decl))
24463                             goto non_class;
24464                         }
24465                       
24466                       cp_finish_decl (decl, init, !is_non_constant_init,
24467                                       asm_specification,
24468                                       LOOKUP_ONLYCONVERTING);
24469                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24470                         {
24471                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24472                           init = NULL_TREE;
24473                         }
24474                       else
24475                         init = pop_stmt_list (this_pre_body);
24476                       this_pre_body = NULL_TREE;
24477                     }
24478                   else
24479                     {
24480                       /* Consume '='.  */
24481                       cp_lexer_consume_token (parser->lexer);
24482                       init = cp_parser_assignment_expression (parser, false, NULL);
24483
24484                     non_class:
24485                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24486                         init = error_mark_node;
24487                       else
24488                         cp_finish_decl (decl, NULL_TREE,
24489                                         /*init_const_expr_p=*/false,
24490                                         asm_specification,
24491                                         LOOKUP_ONLYCONVERTING);
24492                     }
24493
24494                   if (pushed_scope)
24495                     pop_scope (pushed_scope);
24496                 }
24497             }
24498           else 
24499             {
24500               cp_id_kind idk;
24501               /* If parsing a type specifier sequence failed, then
24502                  this MUST be a simple expression.  */
24503               cp_parser_parse_tentatively (parser);
24504               decl = cp_parser_primary_expression (parser, false, false,
24505                                                    false, &idk);
24506               if (!cp_parser_error_occurred (parser)
24507                   && decl
24508                   && DECL_P (decl)
24509                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24510                 {
24511                   tree rhs;
24512
24513                   cp_parser_parse_definitely (parser);
24514                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24515                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24516                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24517                                                          rhs,
24518                                                          tf_warning_or_error));
24519                   add_private_clause = true;
24520                 }
24521               else
24522                 {
24523                   decl = NULL;
24524                   cp_parser_abort_tentative_parse (parser);
24525                   init = cp_parser_expression (parser, false, NULL);
24526                   if (init)
24527                     {
24528                       if (TREE_CODE (init) == MODIFY_EXPR
24529                           || TREE_CODE (init) == MODOP_EXPR)
24530                         real_decl = TREE_OPERAND (init, 0);
24531                     }
24532                 }
24533             }
24534         }
24535       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24536       if (this_pre_body)
24537         {
24538           this_pre_body = pop_stmt_list (this_pre_body);
24539           if (pre_body)
24540             {
24541               tree t = pre_body;
24542               pre_body = push_stmt_list ();
24543               add_stmt (t);
24544               add_stmt (this_pre_body);
24545               pre_body = pop_stmt_list (pre_body);
24546             }
24547           else
24548             pre_body = this_pre_body;
24549         }
24550
24551       if (decl)
24552         real_decl = decl;
24553       if (par_clauses != NULL && real_decl != NULL_TREE)
24554         {
24555           tree *c;
24556           for (c = par_clauses; *c ; )
24557             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24558                 && OMP_CLAUSE_DECL (*c) == real_decl)
24559               {
24560                 error_at (loc, "iteration variable %qD"
24561                           " should not be firstprivate", real_decl);
24562                 *c = OMP_CLAUSE_CHAIN (*c);
24563               }
24564             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24565                      && OMP_CLAUSE_DECL (*c) == real_decl)
24566               {
24567                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24568                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24569                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24570                 OMP_CLAUSE_DECL (l) = real_decl;
24571                 OMP_CLAUSE_CHAIN (l) = clauses;
24572                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24573                 clauses = l;
24574                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24575                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24576                 add_private_clause = false;
24577               }
24578             else
24579               {
24580                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24581                     && OMP_CLAUSE_DECL (*c) == real_decl)
24582                   add_private_clause = false;
24583                 c = &OMP_CLAUSE_CHAIN (*c);
24584               }
24585         }
24586
24587       if (add_private_clause)
24588         {
24589           tree c;
24590           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24591             {
24592               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24593                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24594                   && OMP_CLAUSE_DECL (c) == decl)
24595                 break;
24596               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24597                        && OMP_CLAUSE_DECL (c) == decl)
24598                 error_at (loc, "iteration variable %qD "
24599                           "should not be firstprivate",
24600                           decl);
24601               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24602                        && OMP_CLAUSE_DECL (c) == decl)
24603                 error_at (loc, "iteration variable %qD should not be reduction",
24604                           decl);
24605             }
24606           if (c == NULL)
24607             {
24608               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24609               OMP_CLAUSE_DECL (c) = decl;
24610               c = finish_omp_clauses (c);
24611               if (c)
24612                 {
24613                   OMP_CLAUSE_CHAIN (c) = clauses;
24614                   clauses = c;
24615                 }
24616             }
24617         }
24618
24619       cond = NULL;
24620       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24621         cond = cp_parser_omp_for_cond (parser, decl);
24622       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24623
24624       incr = NULL;
24625       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24626         {
24627           /* If decl is an iterator, preserve the operator on decl
24628              until finish_omp_for.  */
24629           if (decl
24630               && ((type_dependent_expression_p (decl)
24631                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24632                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24633             incr = cp_parser_omp_for_incr (parser, decl);
24634           else
24635             incr = cp_parser_expression (parser, false, NULL);
24636         }
24637
24638       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24639         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24640                                                /*or_comma=*/false,
24641                                                /*consume_paren=*/true);
24642
24643       TREE_VEC_ELT (declv, i) = decl;
24644       TREE_VEC_ELT (initv, i) = init;
24645       TREE_VEC_ELT (condv, i) = cond;
24646       TREE_VEC_ELT (incrv, i) = incr;
24647
24648       if (i == collapse - 1)
24649         break;
24650
24651       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24652          in between the collapsed for loops to be still considered perfectly
24653          nested.  Hopefully the final version clarifies this.
24654          For now handle (multiple) {'s and empty statements.  */
24655       cp_parser_parse_tentatively (parser);
24656       do
24657         {
24658           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24659             break;
24660           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24661             {
24662               cp_lexer_consume_token (parser->lexer);
24663               bracecount++;
24664             }
24665           else if (bracecount
24666                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24667             cp_lexer_consume_token (parser->lexer);
24668           else
24669             {
24670               loc = cp_lexer_peek_token (parser->lexer)->location;
24671               error_at (loc, "not enough collapsed for loops");
24672               collapse_err = true;
24673               cp_parser_abort_tentative_parse (parser);
24674               declv = NULL_TREE;
24675               break;
24676             }
24677         }
24678       while (1);
24679
24680       if (declv)
24681         {
24682           cp_parser_parse_definitely (parser);
24683           nbraces += bracecount;
24684         }
24685     }
24686
24687   /* Note that we saved the original contents of this flag when we entered
24688      the structured block, and so we don't need to re-save it here.  */
24689   parser->in_statement = IN_OMP_FOR;
24690
24691   /* Note that the grammar doesn't call for a structured block here,
24692      though the loop as a whole is a structured block.  */
24693   body = push_stmt_list ();
24694   cp_parser_statement (parser, NULL_TREE, false, NULL);
24695   body = pop_stmt_list (body);
24696
24697   if (declv == NULL_TREE)
24698     ret = NULL_TREE;
24699   else
24700     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24701                           pre_body, clauses);
24702
24703   while (nbraces)
24704     {
24705       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24706         {
24707           cp_lexer_consume_token (parser->lexer);
24708           nbraces--;
24709         }
24710       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24711         cp_lexer_consume_token (parser->lexer);
24712       else
24713         {
24714           if (!collapse_err)
24715             {
24716               error_at (cp_lexer_peek_token (parser->lexer)->location,
24717                         "collapsed loops not perfectly nested");
24718             }
24719           collapse_err = true;
24720           cp_parser_statement_seq_opt (parser, NULL);
24721           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24722             break;
24723         }
24724     }
24725
24726   while (!VEC_empty (tree, for_block))
24727     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24728   release_tree_vector (for_block);
24729
24730   return ret;
24731 }
24732
24733 /* OpenMP 2.5:
24734    #pragma omp for for-clause[optseq] new-line
24735      for-loop  */
24736
24737 #define OMP_FOR_CLAUSE_MASK                             \
24738         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24739         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24740         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24741         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24742         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24743         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24744         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24745         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24746
24747 static tree
24748 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24749 {
24750   tree clauses, sb, ret;
24751   unsigned int save;
24752
24753   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24754                                        "#pragma omp for", pragma_tok);
24755
24756   sb = begin_omp_structured_block ();
24757   save = cp_parser_begin_omp_structured_block (parser);
24758
24759   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24760
24761   cp_parser_end_omp_structured_block (parser, save);
24762   add_stmt (finish_omp_structured_block (sb));
24763
24764   return ret;
24765 }
24766
24767 /* OpenMP 2.5:
24768    # pragma omp master new-line
24769      structured-block  */
24770
24771 static tree
24772 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24773 {
24774   cp_parser_require_pragma_eol (parser, pragma_tok);
24775   return c_finish_omp_master (input_location,
24776                               cp_parser_omp_structured_block (parser));
24777 }
24778
24779 /* OpenMP 2.5:
24780    # pragma omp ordered new-line
24781      structured-block  */
24782
24783 static tree
24784 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24785 {
24786   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24787   cp_parser_require_pragma_eol (parser, pragma_tok);
24788   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24789 }
24790
24791 /* OpenMP 2.5:
24792
24793    section-scope:
24794      { section-sequence }
24795
24796    section-sequence:
24797      section-directive[opt] structured-block
24798      section-sequence section-directive structured-block  */
24799
24800 static tree
24801 cp_parser_omp_sections_scope (cp_parser *parser)
24802 {
24803   tree stmt, substmt;
24804   bool error_suppress = false;
24805   cp_token *tok;
24806
24807   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24808     return NULL_TREE;
24809
24810   stmt = push_stmt_list ();
24811
24812   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24813     {
24814       unsigned save;
24815
24816       substmt = begin_omp_structured_block ();
24817       save = cp_parser_begin_omp_structured_block (parser);
24818
24819       while (1)
24820         {
24821           cp_parser_statement (parser, NULL_TREE, false, NULL);
24822
24823           tok = cp_lexer_peek_token (parser->lexer);
24824           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24825             break;
24826           if (tok->type == CPP_CLOSE_BRACE)
24827             break;
24828           if (tok->type == CPP_EOF)
24829             break;
24830         }
24831
24832       cp_parser_end_omp_structured_block (parser, save);
24833       substmt = finish_omp_structured_block (substmt);
24834       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24835       add_stmt (substmt);
24836     }
24837
24838   while (1)
24839     {
24840       tok = cp_lexer_peek_token (parser->lexer);
24841       if (tok->type == CPP_CLOSE_BRACE)
24842         break;
24843       if (tok->type == CPP_EOF)
24844         break;
24845
24846       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24847         {
24848           cp_lexer_consume_token (parser->lexer);
24849           cp_parser_require_pragma_eol (parser, tok);
24850           error_suppress = false;
24851         }
24852       else if (!error_suppress)
24853         {
24854           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24855           error_suppress = true;
24856         }
24857
24858       substmt = cp_parser_omp_structured_block (parser);
24859       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24860       add_stmt (substmt);
24861     }
24862   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24863
24864   substmt = pop_stmt_list (stmt);
24865
24866   stmt = make_node (OMP_SECTIONS);
24867   TREE_TYPE (stmt) = void_type_node;
24868   OMP_SECTIONS_BODY (stmt) = substmt;
24869
24870   add_stmt (stmt);
24871   return stmt;
24872 }
24873
24874 /* OpenMP 2.5:
24875    # pragma omp sections sections-clause[optseq] newline
24876      sections-scope  */
24877
24878 #define OMP_SECTIONS_CLAUSE_MASK                        \
24879         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24880         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24881         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24882         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24883         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24884
24885 static tree
24886 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24887 {
24888   tree clauses, ret;
24889
24890   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24891                                        "#pragma omp sections", pragma_tok);
24892
24893   ret = cp_parser_omp_sections_scope (parser);
24894   if (ret)
24895     OMP_SECTIONS_CLAUSES (ret) = clauses;
24896
24897   return ret;
24898 }
24899
24900 /* OpenMP 2.5:
24901    # pragma parallel parallel-clause new-line
24902    # pragma parallel for parallel-for-clause new-line
24903    # pragma parallel sections parallel-sections-clause new-line  */
24904
24905 #define OMP_PARALLEL_CLAUSE_MASK                        \
24906         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24907         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24908         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24909         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24910         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24911         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24912         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24913         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24914
24915 static tree
24916 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24917 {
24918   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24919   const char *p_name = "#pragma omp parallel";
24920   tree stmt, clauses, par_clause, ws_clause, block;
24921   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24922   unsigned int save;
24923   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24924
24925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24926     {
24927       cp_lexer_consume_token (parser->lexer);
24928       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24929       p_name = "#pragma omp parallel for";
24930       mask |= OMP_FOR_CLAUSE_MASK;
24931       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24932     }
24933   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24934     {
24935       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24936       const char *p = IDENTIFIER_POINTER (id);
24937       if (strcmp (p, "sections") == 0)
24938         {
24939           cp_lexer_consume_token (parser->lexer);
24940           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24941           p_name = "#pragma omp parallel sections";
24942           mask |= OMP_SECTIONS_CLAUSE_MASK;
24943           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24944         }
24945     }
24946
24947   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24948   block = begin_omp_parallel ();
24949   save = cp_parser_begin_omp_structured_block (parser);
24950
24951   switch (p_kind)
24952     {
24953     case PRAGMA_OMP_PARALLEL:
24954       cp_parser_statement (parser, NULL_TREE, false, NULL);
24955       par_clause = clauses;
24956       break;
24957
24958     case PRAGMA_OMP_PARALLEL_FOR:
24959       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24960       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24961       break;
24962
24963     case PRAGMA_OMP_PARALLEL_SECTIONS:
24964       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24965       stmt = cp_parser_omp_sections_scope (parser);
24966       if (stmt)
24967         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24968       break;
24969
24970     default:
24971       gcc_unreachable ();
24972     }
24973
24974   cp_parser_end_omp_structured_block (parser, save);
24975   stmt = finish_omp_parallel (par_clause, block);
24976   if (p_kind != PRAGMA_OMP_PARALLEL)
24977     OMP_PARALLEL_COMBINED (stmt) = 1;
24978   return stmt;
24979 }
24980
24981 /* OpenMP 2.5:
24982    # pragma omp single single-clause[optseq] new-line
24983      structured-block  */
24984
24985 #define OMP_SINGLE_CLAUSE_MASK                          \
24986         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24987         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24988         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24989         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24990
24991 static tree
24992 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24993 {
24994   tree stmt = make_node (OMP_SINGLE);
24995   TREE_TYPE (stmt) = void_type_node;
24996
24997   OMP_SINGLE_CLAUSES (stmt)
24998     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24999                                  "#pragma omp single", pragma_tok);
25000   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25001
25002   return add_stmt (stmt);
25003 }
25004
25005 /* OpenMP 3.0:
25006    # pragma omp task task-clause[optseq] new-line
25007      structured-block  */
25008
25009 #define OMP_TASK_CLAUSE_MASK                            \
25010         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25011         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25012         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25013         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25014         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25015         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25016
25017 static tree
25018 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25019 {
25020   tree clauses, block;
25021   unsigned int save;
25022
25023   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25024                                        "#pragma omp task", pragma_tok);
25025   block = begin_omp_task ();
25026   save = cp_parser_begin_omp_structured_block (parser);
25027   cp_parser_statement (parser, NULL_TREE, false, NULL);
25028   cp_parser_end_omp_structured_block (parser, save);
25029   return finish_omp_task (clauses, block);
25030 }
25031
25032 /* OpenMP 3.0:
25033    # pragma omp taskwait new-line  */
25034
25035 static void
25036 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25037 {
25038   cp_parser_require_pragma_eol (parser, pragma_tok);
25039   finish_omp_taskwait ();
25040 }
25041
25042 /* OpenMP 2.5:
25043    # pragma omp threadprivate (variable-list) */
25044
25045 static void
25046 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25047 {
25048   tree vars;
25049
25050   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25051   cp_parser_require_pragma_eol (parser, pragma_tok);
25052
25053   finish_omp_threadprivate (vars);
25054 }
25055
25056 /* Main entry point to OpenMP statement pragmas.  */
25057
25058 static void
25059 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25060 {
25061   tree stmt;
25062
25063   switch (pragma_tok->pragma_kind)
25064     {
25065     case PRAGMA_OMP_ATOMIC:
25066       cp_parser_omp_atomic (parser, pragma_tok);
25067       return;
25068     case PRAGMA_OMP_CRITICAL:
25069       stmt = cp_parser_omp_critical (parser, pragma_tok);
25070       break;
25071     case PRAGMA_OMP_FOR:
25072       stmt = cp_parser_omp_for (parser, pragma_tok);
25073       break;
25074     case PRAGMA_OMP_MASTER:
25075       stmt = cp_parser_omp_master (parser, pragma_tok);
25076       break;
25077     case PRAGMA_OMP_ORDERED:
25078       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25079       break;
25080     case PRAGMA_OMP_PARALLEL:
25081       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25082       break;
25083     case PRAGMA_OMP_SECTIONS:
25084       stmt = cp_parser_omp_sections (parser, pragma_tok);
25085       break;
25086     case PRAGMA_OMP_SINGLE:
25087       stmt = cp_parser_omp_single (parser, pragma_tok);
25088       break;
25089     case PRAGMA_OMP_TASK:
25090       stmt = cp_parser_omp_task (parser, pragma_tok);
25091       break;
25092     default:
25093       gcc_unreachable ();
25094     }
25095
25096   if (stmt)
25097     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25098 }
25099 \f
25100 /* The parser.  */
25101
25102 static GTY (()) cp_parser *the_parser;
25103
25104 \f
25105 /* Special handling for the first token or line in the file.  The first
25106    thing in the file might be #pragma GCC pch_preprocess, which loads a
25107    PCH file, which is a GC collection point.  So we need to handle this
25108    first pragma without benefit of an existing lexer structure.
25109
25110    Always returns one token to the caller in *FIRST_TOKEN.  This is
25111    either the true first token of the file, or the first token after
25112    the initial pragma.  */
25113
25114 static void
25115 cp_parser_initial_pragma (cp_token *first_token)
25116 {
25117   tree name = NULL;
25118
25119   cp_lexer_get_preprocessor_token (NULL, first_token);
25120   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25121     return;
25122
25123   cp_lexer_get_preprocessor_token (NULL, first_token);
25124   if (first_token->type == CPP_STRING)
25125     {
25126       name = first_token->u.value;
25127
25128       cp_lexer_get_preprocessor_token (NULL, first_token);
25129       if (first_token->type != CPP_PRAGMA_EOL)
25130         error_at (first_token->location,
25131                   "junk at end of %<#pragma GCC pch_preprocess%>");
25132     }
25133   else
25134     error_at (first_token->location, "expected string literal");
25135
25136   /* Skip to the end of the pragma.  */
25137   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25138     cp_lexer_get_preprocessor_token (NULL, first_token);
25139
25140   /* Now actually load the PCH file.  */
25141   if (name)
25142     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25143
25144   /* Read one more token to return to our caller.  We have to do this
25145      after reading the PCH file in, since its pointers have to be
25146      live.  */
25147   cp_lexer_get_preprocessor_token (NULL, first_token);
25148 }
25149
25150 /* Normal parsing of a pragma token.  Here we can (and must) use the
25151    regular lexer.  */
25152
25153 static bool
25154 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25155 {
25156   cp_token *pragma_tok;
25157   unsigned int id;
25158
25159   pragma_tok = cp_lexer_consume_token (parser->lexer);
25160   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25161   parser->lexer->in_pragma = true;
25162
25163   id = pragma_tok->pragma_kind;
25164   switch (id)
25165     {
25166     case PRAGMA_GCC_PCH_PREPROCESS:
25167       error_at (pragma_tok->location,
25168                 "%<#pragma GCC pch_preprocess%> must be first");
25169       break;
25170
25171     case PRAGMA_OMP_BARRIER:
25172       switch (context)
25173         {
25174         case pragma_compound:
25175           cp_parser_omp_barrier (parser, pragma_tok);
25176           return false;
25177         case pragma_stmt:
25178           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25179                     "used in compound statements");
25180           break;
25181         default:
25182           goto bad_stmt;
25183         }
25184       break;
25185
25186     case PRAGMA_OMP_FLUSH:
25187       switch (context)
25188         {
25189         case pragma_compound:
25190           cp_parser_omp_flush (parser, pragma_tok);
25191           return false;
25192         case pragma_stmt:
25193           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25194                     "used in compound statements");
25195           break;
25196         default:
25197           goto bad_stmt;
25198         }
25199       break;
25200
25201     case PRAGMA_OMP_TASKWAIT:
25202       switch (context)
25203         {
25204         case pragma_compound:
25205           cp_parser_omp_taskwait (parser, pragma_tok);
25206           return false;
25207         case pragma_stmt:
25208           error_at (pragma_tok->location,
25209                     "%<#pragma omp taskwait%> may only be "
25210                     "used in compound statements");
25211           break;
25212         default:
25213           goto bad_stmt;
25214         }
25215       break;
25216
25217     case PRAGMA_OMP_THREADPRIVATE:
25218       cp_parser_omp_threadprivate (parser, pragma_tok);
25219       return false;
25220
25221     case PRAGMA_OMP_ATOMIC:
25222     case PRAGMA_OMP_CRITICAL:
25223     case PRAGMA_OMP_FOR:
25224     case PRAGMA_OMP_MASTER:
25225     case PRAGMA_OMP_ORDERED:
25226     case PRAGMA_OMP_PARALLEL:
25227     case PRAGMA_OMP_SECTIONS:
25228     case PRAGMA_OMP_SINGLE:
25229     case PRAGMA_OMP_TASK:
25230       if (context == pragma_external)
25231         goto bad_stmt;
25232       cp_parser_omp_construct (parser, pragma_tok);
25233       return true;
25234
25235     case PRAGMA_OMP_SECTION:
25236       error_at (pragma_tok->location, 
25237                 "%<#pragma omp section%> may only be used in "
25238                 "%<#pragma omp sections%> construct");
25239       break;
25240
25241     default:
25242       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25243       c_invoke_pragma_handler (id);
25244       break;
25245
25246     bad_stmt:
25247       cp_parser_error (parser, "expected declaration specifiers");
25248       break;
25249     }
25250
25251   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25252   return false;
25253 }
25254
25255 /* The interface the pragma parsers have to the lexer.  */
25256
25257 enum cpp_ttype
25258 pragma_lex (tree *value)
25259 {
25260   cp_token *tok;
25261   enum cpp_ttype ret;
25262
25263   tok = cp_lexer_peek_token (the_parser->lexer);
25264
25265   ret = tok->type;
25266   *value = tok->u.value;
25267
25268   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25269     ret = CPP_EOF;
25270   else if (ret == CPP_STRING)
25271     *value = cp_parser_string_literal (the_parser, false, false);
25272   else
25273     {
25274       cp_lexer_consume_token (the_parser->lexer);
25275       if (ret == CPP_KEYWORD)
25276         ret = CPP_NAME;
25277     }
25278
25279   return ret;
25280 }
25281
25282 \f
25283 /* External interface.  */
25284
25285 /* Parse one entire translation unit.  */
25286
25287 void
25288 c_parse_file (void)
25289 {
25290   static bool already_called = false;
25291
25292   if (already_called)
25293     {
25294       sorry ("inter-module optimizations not implemented for C++");
25295       return;
25296     }
25297   already_called = true;
25298
25299   the_parser = cp_parser_new ();
25300   push_deferring_access_checks (flag_access_control
25301                                 ? dk_no_deferred : dk_no_check);
25302   cp_parser_translation_unit (the_parser);
25303   the_parser = NULL;
25304 }
25305
25306 #include "gt-cp-parser.h"